Commit 1cc6e89c authored by Ina Curdt's avatar Ina Curdt
Browse files

SI-283: Testklassen erstellt

parent a9984fea
package org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.config;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
......@@ -19,16 +18,19 @@ import org.springframework.messaging.MessageHandler;
@Log4j2
public class EventProducerConfig {
@Value("${rabbitmqconfig.routing-key}")
@Value("${spring.rabbitmq.routingkey}")
public String routingKey;
@Value("${rabbitmqconfig.exchange-name}")
@Value("${spring.rabbitmq.exchangename}")
public String exchangeName;
@Value("${spring.rabbitmq.queuename}")
public String queueName;
@Autowired
RabbitTemplate rabbitTemplate;
@Bean
public Exchange eventExchange() {
public DirectExchange exchange() {
return new DirectExchange(exchangeName);
}
......@@ -37,9 +39,17 @@ public class EventProducerConfig {
return new DirectChannel();
}
@Bean
public Queue messageImportQueue() { return new Queue(queueName); }
@Bean
public Binding binding() { return BindingBuilder.bind(messageImportQueue()).to(exchange()).with(routingKey); }
@Bean
@ServiceActivator(inputChannel = "messageImportChannel")
public MessageHandler messageHandler() {
return message -> rabbitTemplate.convertAndSend(exchangeName, routingKey, message.getPayload(), message1 -> {
message1.getMessageProperties().getHeaders().put("metaId", message.getHeaders().get("metaId"));
message1.getMessageProperties().getHeaders().put("description", message.getHeaders().get("description"));
......
......@@ -29,32 +29,17 @@ import org.springframework.web.bind.annotation.*;
@RequestMapping("/import")
public class ImportController {
@Autowired
private ImportService importService;
@PostMapping
@ApiOperation(value = "Import einer externen Störungsinformation")
@ApiResponses(value = {
@ApiResponse(code = 201, message = "Störungsinformation erfolgreich importiert"),
@ApiResponse(code = 500, message = "Konnte nicht durchgeführt werden")
})
public void pushForeignFailureInfo(
// public ResponseEntity<ForeignFailureDto> pushForeignFailureInfo(
@Validated @RequestBody ForeignFailureDto foreignFailureDto) {
ForeignFailureDto savedgfDto = importService.pushForeignFailure(foreignFailureDto);
// URI location = ServletUriComponentsBuilder
// .fromCurrentRequestUri()
// .path("/{uuid}")
// .buildAndExpand(foreignFailureDto.getUuid())
// .toUri();
// return ResponseEntity.created(location).body(savedgfDto);
}
public void pushForeignFailureInfo(@Validated @RequestBody ForeignFailureDto foreignFailureDto) {
importService.pushForeignFailure(foreignFailureDto);
}
}
/*
*******************************************************************************
* 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.jobs.importgridfailures.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
public class BadRequestException extends RuntimeException{
public BadRequestException() {
}
public BadRequestException(String message) {
super(message);
}
}
/*
*******************************************************************************
* 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.jobs.importgridfailures.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR)
public class InternalServerErrorException extends RuntimeException{
public InternalServerErrorException(String message) {
super(message);
}
}
/*
*******************************************************************************
* 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.jobs.importgridfailures.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(code = HttpStatus.NOT_FOUND)
public class NotFoundException extends RuntimeException{
public NotFoundException() {
}
public NotFoundException(String message) {
super(message);
}
}
package org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(code = HttpStatus.UNAUTHORIZED)
public class UnauthorizedException extends RuntimeException{
public UnauthorizedException(String message) {
super(message);
}
}
......@@ -15,8 +15,10 @@
package org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.BuiltinExchangeType;
import lombok.extern.log4j.Log4j2;
import org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.dtos.ForeignFailureDto;
import org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.exceptions.InternalServerErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
......@@ -24,24 +26,18 @@ import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.messaging.MessageChannel;
import java.text.SimpleDateFormat;
@Service
@Log4j2
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "rabbitmq")
public class ImportService {
private static final String INDEXING_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
@Autowired
private MessageChannel messageImportChannel;
public ForeignFailureDto pushForeignFailure(ForeignFailureDto foreignFailureDto) {
public void pushForeignFailure(ForeignFailureDto foreignFailureDto) {
try {
//messageImportChannel.send(MessageBuilder.withPayload(new ObjectMapper().setDateFormat(new SimpleDateFormat(INDEXING_DATE_FORMAT)).writeValueAsString(foreignFailureDto))
messageImportChannel.send(MessageBuilder.withPayload(foreignFailureDto.getPayload())
.setHeader("metaId", foreignFailureDto.getMetaId())
.setHeader("description", foreignFailureDto.getDescription())
......@@ -50,10 +46,9 @@ public class ImportService {
} catch (Exception e) {
log.debug(e.getMessage(), e);
throw new InternalServerErrorException("could.not.push.message");
}
return foreignFailureDto;
}
}
......@@ -22,10 +22,9 @@ spring:
port: 5672
username: guest
password: guest
rabbitmqconfig:
routing-key: messageImportChannel.process
exchange-name: messageImportChannel
routingkey: messageImportChannel.testkey
exchangename: messageImportChannelTest
queuename: messageImportQueueTest
server:
max-http-header-size: 262144
......
......@@ -22,10 +22,9 @@ spring:
port: 5672
username: guest
password: guest
rabbitmqconfig:
routing-key: messageImportChannel.process
exchange-name: messageImportChannel
routingkey: messageImportChannel.testkey
exchangename: messageImportChannelTest
queuename: messageImportQueueTest
eureka:
......
#Fehlermeldungen
could.not.push.message=Das Objekt konnte nicht an RabbitMQ gesendet werden.
/*
*******************************************************************************
* 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.jobs.importgridfailures.config;
import org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.TestImportGridFailuresApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
@EntityScan(basePackageClasses = TestImportGridFailuresApplication.class)
@ContextConfiguration( initializers = {ConfigFileApplicationContextInitializer.class})
@TestPropertySource("spring.config.location=classpath:application.yml")
public class TestConfiguration {
@Bean
public EventProducerConfig myEventProducerConfig() {return new EventProducerConfig();}
}
/*
*******************************************************************************
* 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.jobs.importgridfailures.controller;
import org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.TestImportGridFailuresApplication;
import org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.dtos.ForeignFailureDto;
import org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.service.ImportService;
import org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.support.MockDataHelper;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import java.util.List;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest(classes = TestImportGridFailuresApplication.class)
@AutoConfigureMockMvc
@ActiveProfiles("test")
public class ImportControllerTest {
@MockBean
private ImportService importService;
@MockBean
private ImportService messageImportChannel;
@Autowired
private MockMvc mockMvc;
// @Test
// public void shouldCallImport() throws Exception {
// ForeignFailureDto foreignFailureDto = MockDataHelper.mockForeignFailureDto();
//
// ImportService spy = Mockito.spy(messageImportChannel);
// Mockito.doNothing().when(spy).setHeader(anyString(), anyString());
//
// mockMvc.perform(post("/import"))
// .andExpect(status().is2xxSuccessful());
//
//
// }
}
\ No newline at end of file
/*
*******************************************************************************
* 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.jobs.importgridfailures.service;
import org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.config.TestConfiguration;
import org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.dtos.ForeignFailureDto;
import org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.support.MockDataHelper;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ContextConfiguration;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
//@RunWith(SpringRunner.class)
@DataJpaTest
@ContextConfiguration(classes = {TestConfiguration.class})
public class ImportServiceTest {
@Qualifier("myBranchService")
@Autowired
private ImportService importService;
@MockBean
private ImportService messageImportChannel;
// @Test
// public void shouldPushFailure() {
//
// ForeignFailureDto foreignFailureDto = MockDataHelper.mockForeignFailureDto();
//
// ImportService spy = Mockito.spy(messageImportChannel);
// Mockito.doNothing().when(spy).send(any(Message.class));
//
// importService.pushForeignFailure(foreignFailureDto);
// }
}
/*
*******************************************************************************
* 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.jobs.importgridfailures.support;
import org.eclipse.openk.gridfailureinformation.jobs.importgridfailures.dtos.ForeignFailureDto;
public class MockDataHelper {
private MockDataHelper() {}
public static ForeignFailureDto mockForeignFailureDto() {
ForeignFailureDto foreignFailureDto = new ForeignFailureDto();
foreignFailureDto.setMetaId("XYZ_111_gdfr");
foreignFailureDto.setSource("FremdsystemXY");
foreignFailureDto.setDescription("Rohrbruch");
foreignFailureDto.setPayload("{\"field1\":11111,\"field2\":\"22222\"\"field3\":abcde}");
foreignFailureDto.setRabbitMqExchangeName("messageImportChannelTest");
foreignFailureDto.setRabbitMqRoutingKey("messageImportQueueTest");
return foreignFailureDto;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment