Commit fd7e31f9 authored by Holger Rudolph's avatar Holger Rudolph
Browse files

[SI-264] JobManager, RabbitMQ Message consumer, service for ImportData, unit tests


Signed-off-by: Holger Rudolph's avatarHolger Rudolph <holger.rudolph@pta.de>
parent 1cc6e89c
......@@ -48,6 +48,10 @@
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
......@@ -64,6 +68,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
......
package org.eclipse.openk.gridfailureinformation.config;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import java.util.Optional;
@Configuration
@Log4j2
public class RabbitMqConfig {
@Value("${rabbitmqconfig.routing-key}")
public String routingKey;
@Value("${rabbitmqconfig.exchange-name}")
public String exchangeName;
@Value("${spring.rabbitmq.queuename}")
public String queueName;
// Initialisisation of RabbitMQ eexchange, queue and binding
/*@Bean
public DirectExchange e1() {
return new DirectExchange(exchangeName);
}
@Bean
public Queue messageImportQueue() {
return new Queue(queueName);
}
@Bean
public Binding b1() {
return BindingBuilder.bind(messageImportQueue()).to(e1()).with(routingKey);
}*/
}
\ No newline at end of file
......@@ -62,7 +62,7 @@ public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {
}
chain.doFilter(request, response);
} catch(AuthenticationException ex) {
} catch(AuthenticationException ex) {
throw new ServletException("Authentication exception.");
}
......
package org.eclipse.openk.gridfailureinformation.config.jobs;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.SimpleMessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.amqp.core.Message;
import java.nio.charset.Charset;
import java.util.Random;
@EnableScheduling
@Component
@Log4j2
public class JobManager {
@Value("${rabbitmqconfig.routing-key}")
public String routingKey;
@Value("${rabbitmqconfig.exchange-name}")
public String exchangeName;
@Autowired
private RabbitTemplate template;
// Testjob for sending data into queue
/*@Scheduled(initialDelay = 1000, fixedRate = 5000)
public void sendRabbitMQMessages() {
MessageProperties testProps = new MessageProperties();
byte[] array = new byte[7];
new Random().nextBytes(array);
String generatedString = new String(array, Charset.forName("UTF-8"));
int rnd = new Random().nextInt(100);
//System.out.println(generatedString);
testProps.setHeader("metaId", "Extern_05.05.2020 13:12:110 " + rnd);
testProps.setHeader("source", "Extern");
testProps.setHeader("description", "Wasserleitung kaputt");
String msgBody = "{ \"Quelle\": \"Extern\", \"Eigenschaften\": {\"Branch\": \"Wasser\", \"Ort\": \"Ulm\", \"Datum\": \"2020-05-05T13:12.110Z\"}}";
//byte[] body = msgBody.getBytes();
//Message testMsg = new Message(body, testProps);
SimpleMessageConverter converter = new SimpleMessageConverter();
Message testMsg = converter.toMessage(msgBody, testProps);
template.send(exchangeName , routingKey, testMsg);
log.info("Message sent!");
}*/
}
......@@ -33,9 +33,6 @@ import java.util.UUID;
@RequestMapping("/hist-grid-failure-informations")
public class HistFailureInformationController {
@Value("${gridFailureInformation.maxListSize}")
int maxListSize;
@Autowired
private HistFailureInformationService histfailureInformationService;
......
/*
*******************************************************************************
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************
*/
package org.eclipse.openk.gridfailureinformation.controller;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.log4j.Log4j2;
import org.eclipse.openk.gridfailureinformation.service.ImportDataService;
import org.eclipse.openk.gridfailureinformation.viewmodel.ImportDataDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.UUID;
@Log4j2
@RestController
@RequestMapping("/import-data")
public class ImportDataController {
@Autowired
private ImportDataService importDataService;
@GetMapping
@ApiOperation(value = "Anzeigen aller importierten Störungsmeldungen")
@ApiResponses(value = { @ApiResponse(code = 200, message = "Erfolgreich durchgeführt"),
@ApiResponse(code = 404, message = "Importdaten wurden nicht gefunden")})
@ResponseStatus(HttpStatus.OK)
public List<ImportDataDto> getImportData() {
return importDataService.getImportData();
}
@GetMapping("/{uuid}")
@ApiOperation(value = "Anzeigen einer bestimmten importierten Störungsmeldung")
@ApiResponses(value = { @ApiResponse(code = 200, message = "Erfolgreich durchgeführt"),
@ApiResponse(code = 404, message = "Importierte Störungsmeldung wurde nicht gefunden")})
@ResponseStatus(HttpStatus.OK)
public ImportDataDto readImportMessage(
@PathVariable UUID uuid) {
return importDataService.getImportDataByUuid(uuid);
}
}
/*
*******************************************************************************
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************
*/
package org.eclipse.openk.gridfailureinformation.mapper;
import org.eclipse.openk.gridfailureinformation.model.RefBranch;
import org.eclipse.openk.gridfailureinformation.model.TblImportData;
import org.eclipse.openk.gridfailureinformation.viewmodel.BranchDto;
import org.eclipse.openk.gridfailureinformation.viewmodel.ImportDataDto;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface ImportDataMapper {
ImportDataDto toImportDataDto(TblImportData importData);
TblImportData toTblImportData(ImportDataDto importDataDto);
}
package org.eclipse.openk.gridfailureinformation.messagebroker;
import org.eclipse.openk.gridfailureinformation.exceptions.NotFoundException;
import org.eclipse.openk.gridfailureinformation.service.ImportDataService;
import org.eclipse.openk.gridfailureinformation.viewmodel.ImportDataDto;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.SimpleMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Objects;
@Component
public class MessageConsumer {
@Autowired
private ImportDataService importDataService;
@RabbitListener(queues="${spring.rabbitmq.queuename}")
public void listenMessage(Message message) {
String meta = (String) message.getMessageProperties().getHeaders().get("metaId");
if (Objects.nonNull(message.getMessageProperties().getHeaders().get("metaId"))) {
importData(message);
}
}
public void importData (Message message) {
ImportDataDto importDataDto = new ImportDataDto();
SimpleMessageConverter converter = new SimpleMessageConverter();
importDataDto.setMessageContent( (String) converter.fromMessage(message));
importDataDto.setMetaId((String) message.getMessageProperties().getHeaders().get("metaId"));
importDataDto.setDescription((String) message.getMessageProperties().getHeaders().get("description"));
importDataDto.setSource((String) message.getMessageProperties().getHeaders().get("source"));
ImportDataDto importDataDtoToSearch = new ImportDataDto();
try {
importDataDtoToSearch = importDataService.getImportDataByMetaId(importDataDto.getMetaId());
} catch(NotFoundException ex) {
importDataService.insertImportData(importDataDto);
}
if (importDataDtoToSearch!=null) {
importDataService.updateImportDataByMetaID(importDataDto);
}
}
}
\ No newline at end of file
......@@ -41,6 +41,8 @@ public class HtblFailureInformation {
private Long id;
private UUID uuid;
private Long versionNumber;
private String title;
private String description;
private String responsibility;
private String internExtern;
private String voltageLevel;
......
......@@ -37,6 +37,8 @@ public class TblFailureInformation {
private Long id;
private UUID uuid;
private Long versionNumber;
private String title;
private String description;
private String responsibility;
private String internExtern;
private String voltageLevel;
......
/*
*******************************************************************************
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************
*/
package org.eclipse.openk.gridfailureinformation.model;
import lombok.Data;
import javax.persistence.*;
import java.util.UUID;
@Data
@Entity
public class TblImportData {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "tbl_import_data_id_seq")
@SequenceGenerator(name = "tbl_import_data_id_seq", sequenceName = "tbl_import_data_id_seq", allocationSize = 1)
@Column(name = "id", updatable = false)
private Long id;
private UUID uuid;
private String metaId;
private String description;
private String source;
private String messageContent;
}
/*
*******************************************************************************
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************
*/
package org.eclipse.openk.gridfailureinformation.repository;
import org.eclipse.openk.gridfailureinformation.model.TblImportData;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@Repository
public interface ImportDataRepository extends JpaRepository<TblImportData, Long > {
List<TblImportData> findAll();
Optional<TblImportData> findByUuid(UUID uuid);
Optional<TblImportData> findByMetaId(String metaid);
}
/*
*******************************************************************************
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************
*/
package org.eclipse.openk.gridfailureinformation.service;
import org.eclipse.openk.gridfailureinformation.exceptions.NotFoundException;
import org.eclipse.openk.gridfailureinformation.mapper.ImportDataMapper;
import org.eclipse.openk.gridfailureinformation.model.TblFailureInformation;
import org.eclipse.openk.gridfailureinformation.model.TblImportData;
import org.eclipse.openk.gridfailureinformation.repository.ImportDataRepository;
import org.eclipse.openk.gridfailureinformation.viewmodel.FailureInformationDto;
import org.eclipse.openk.gridfailureinformation.viewmodel.ImportDataDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
@Service
public class ImportDataService {
@Autowired
private ImportDataRepository importDataRepository;
@Autowired
private ImportDataMapper importDataMapper;
public List<ImportDataDto> getImportData() {
return importDataRepository.findAll().stream()
.map( importDataMapper::toImportDataDto )
.collect(Collectors.toList());
}
public ImportDataDto getImportDataByUuid(UUID uuid) {
TblImportData tblImportData = importDataRepository.findByUuid(uuid)
.orElseThrow(NotFoundException::new);
return importDataMapper.toImportDataDto(tblImportData);
}
public ImportDataDto getImportDataByMetaId(String metaId) {
TblImportData tblImportData = importDataRepository.findByMetaId(metaId)
.orElseThrow(NotFoundException::new);
return importDataMapper.toImportDataDto(tblImportData);
}
public ImportDataDto insertImportData(ImportDataDto importDataDto){
TblImportData tblImportDataToSave = importDataMapper.toTblImportData(importDataDto);
tblImportDataToSave.setUuid(UUID.randomUUID());
return importDataMapper.toImportDataDto(importDataRepository.save(tblImportDataToSave));
}
@Transactional
public ImportDataDto updateImportDataByMetaID(ImportDataDto importDataDto) {
TblImportData tblImportData = importDataRepository.findByMetaId(importDataDto.getMetaId())
.orElseThrow(() -> new NotFoundException("import.data.uuid.not.existing"));
TblImportData tblImportDataToSave = importDataMapper.toTblImportData(importDataDto);
tblImportDataToSave.setId(tblImportData.getId());
tblImportDataToSave.setUuid(tblImportData.getUuid());
TblImportData savedImportData = importDataRepository.save(tblImportDataToSave);
return importDataMapper.toImportDataDto(savedImportData);
}
}
......@@ -27,6 +27,8 @@ import java.util.UUID;
public class FailureInformationDto implements Serializable {
@JsonProperty("id")
private UUID uuid;
private String title;
private String description;
private Long versionNumber;
private String responsibility;
private String internExtern;
......
/*
*******************************************************************************
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************
*/
package org.eclipse.openk.gridfailureinformation.viewmodel;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import java.io.Serializable;
import java.util.UUID;
@Data
public class ImportDataDto implements Serializable {
@JsonProperty("id")
private UUID uuid;
private String name;
private String colorCode;
private String metaId;
private String description;
private String source;
private String messageContent;
}
......@@ -17,6 +17,16 @@ spring:
password: gfi_service
flyway:
enabled: false
rabbitmq:
host: entdockergss
port: 5672
username: guest
password: guest
queuename: messageImportQueue
rabbitmqconfig:
routing-key: messageImportChannel.process
exchange-name: messageImportChannel
server:
max-http-header-size: 262144
......
......@@ -17,6 +17,16 @@ spring:
password: gfi_service
flyway:
enabled: false
rabbitmq:
host: entdockergss
port: 5672
username: guest
password: guest
queuename: messageImportQueue
rabbitmqconfig:
routing-key: messageImportChannel.process
exchange-name: messageImportChannel
server:
port: 9165
......
......@@ -60,6 +60,9 @@ public class TestConfiguration {
return new StatusMapperImpl();
}
@Bean
ImportDataMapper importDataMapper() { return new ImportDataMapperImpl(); }
@Bean
public VersionService myVersionService() {