Skip to content

Commit

Permalink
#768: try to write e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanStrehlerCGI committed Nov 28, 2023
1 parent 07dd42c commit c89c263
Show file tree
Hide file tree
Showing 15 changed files with 443 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.Message;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

Expand All @@ -27,15 +28,13 @@ public class MessageProcessor {

private static final String RESPONSE = "response";


private final IntegrationOutPort integration;

private final GetPersonInPort getPersonInPort;
private final GetPersonErweitertInPort getPersonErweitertInPort;
private final SearchPersonInPort searchPersonInPort;
private final SearchPersonErweitertInPort searchPersonErweitertInPort;


/**
* The Consumer expects an {@link OkEwoEventRequest} which represents an {@link OrdnungsmerkmalDto} for OK.EWO.
* <p>
Expand All @@ -44,14 +43,15 @@ public class MessageProcessor {
* In case of an error the error message is returned as a JSON representing {@link OkEwoErrorDto}.
*/
@Bean
public Consumer<Message<OkEwoEventRequest<OrdnungsmerkmalDto>>> getPerson() {
public Consumer<Message<OkEwoEventRequest<HashMap<String, String>>>> getPerson() {
return message -> {
log.debug("Processing new request \"getPerson\" from eventbus: {}", message);
val payload = message.getPayload();
val headers = message.getHeaders();

val request = payload.getRequest();
val om = request.get("ordnungsmerkmal");
try {
val response = getPersonInPort.getPerson(payload.getRequest().getOrdnungsmerkmal());
val response = getPersonInPort.getPerson(om);
Map<String, Object> result = Map.of(RESPONSE, response);
integration.correlateProcessMessage(headers, result);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import de.muenchen.oss.digiwf.okewo.integration.client.api.PersonErweitertApi;
import de.muenchen.oss.digiwf.okewo.integration.client.model.*;
import de.muenchen.oss.digiwf.okewo.integration.domain.exception.OkEwoIntegrationClientErrorException;
import lombok.RequiredArgsConstructor;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClientResponseException;

@Component
@RequiredArgsConstructor
@AllArgsConstructor
public class OkEwoAdapter implements OkEwoClientOutPort {

private PersonErweitertApi personErweitertApi;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import jakarta.validation.constraints.NotBlank;

import java.net.URL;

@Getter
@Setter
@Validated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ de:
digiwf:
okewo:
password: 'password'
url: 'url'
url: 'http://localhost:8089'
username: 'username'
benutzerId: 'benutzerId'
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package de.muenchen.oss.digiwf.okewo.integration.adapter.out;

import de.muenchen.oss.digiwf.okewo.integration.client.api.PersonApi;
import de.muenchen.oss.digiwf.okewo.integration.client.api.PersonErweitertApi;
import de.muenchen.oss.digiwf.okewo.integration.client.model.*;
import de.muenchen.oss.digiwf.okewo.integration.domain.exception.OkEwoIntegrationClientErrorException;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import reactor.core.publisher.Mono;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class OkEwoAdapterTest {

private final PersonErweitertApi personErweitertApi = mock(PersonErweitertApi.class);
private final PersonApi personApi = mock(PersonApi.class);
private final OkEwoAdapter adapter = new OkEwoAdapter(
personErweitertApi,
personApi
);

private final WebClientResponseException notFoundException = new WebClientResponseException(HttpStatus.NOT_FOUND, "not found", null, null, null, null);
private final WebClientResponseException badRequestException = new WebClientResponseException(HttpStatus.BAD_REQUEST, "bad request", null, null, null, null);

@Test
void getPerson_shouldReturnPersonWhenRequestWasSuccessfully() throws OkEwoIntegrationClientErrorException {
final Person serverResponse = new Person().ordnungsmerkmal("om");
when(personApi.deMuenchenEaiEwoRouteROUTEPROCESSGETPERSON("om", "benutzerId")).thenReturn(Mono.just(serverResponse));

final Person result = adapter.getPerson("om", "benutzerId");
assertEquals("om", result.getOrdnungsmerkmal());
}

@Test
void getPerson_shouldThrowExceptionIfRequestFailed() {
when(personApi.deMuenchenEaiEwoRouteROUTEPROCESSGETPERSON("om", "benutzerId")).thenThrow(notFoundException);

final OkEwoIntegrationClientErrorException exception= assertThrows(OkEwoIntegrationClientErrorException.class, () -> {
adapter.getPerson("om", "benutzerId");
});

assertTrue(exception.getMessage().contains("404"));
}

@Test
void getExtendedPerson_shouldReturnPersonErweitertWhenRequestWasSuccessfully() throws OkEwoIntegrationClientErrorException {
final PersonErweitert serverResponse = new PersonErweitert().ordnungsmerkmal("om");
when(personErweitertApi.deMuenchenEaiEwoRouteROUTEPROCESSGETPERSONERWEITERT("om", "benutzerId")).thenReturn(Mono.just(serverResponse));

final PersonErweitert result = adapter.getExtendedPerson("om", "benutzerId");
assertEquals("om", result.getOrdnungsmerkmal());
}

@Test
void getExtendedPerson_shouldThrowExceptionIfRequestFailed() {
when(personErweitertApi.deMuenchenEaiEwoRouteROUTEPROCESSGETPERSONERWEITERT("om", "benutzerId")).thenThrow(notFoundException);

final OkEwoIntegrationClientErrorException exception= assertThrows(OkEwoIntegrationClientErrorException.class, () -> {
adapter.getExtendedPerson("om", "benutzerId");
});

assertTrue(exception.getMessage().contains("404"));
}

@Test
void searchPerson_shouldReturnPersonErweitertWhenRequestWasSuccessfully() throws OkEwoIntegrationClientErrorException {
final SuchePersonAnfrage request = new SuchePersonAnfrage();
final Person person = new Person().ordnungsmerkmal("om");
final SuchePersonAntwort serverResponse = new SuchePersonAntwort().addPersonenItem(person);
when(personApi.deMuenchenEaiEwoRouteROUTEPROCESSSEARCHPERSON(request)).thenReturn(Mono.just(serverResponse));

final SuchePersonAntwort result = adapter.searchPerson(request);
assertEquals("om", result.getPersonen().get(0).getOrdnungsmerkmal());
}

@Test
void searchPerson_shouldThrowExceptionIfRequestFailed() {
final SuchePersonAnfrage request = new SuchePersonAnfrage();
when(personApi.deMuenchenEaiEwoRouteROUTEPROCESSSEARCHPERSON(request)).thenThrow(badRequestException);

final OkEwoIntegrationClientErrorException exception= assertThrows(OkEwoIntegrationClientErrorException.class, () -> {
adapter.searchPerson(request);
});

assertTrue(exception.getMessage().contains("400"));
}

@Test
void searchExtendedPerson() throws OkEwoIntegrationClientErrorException {
final SuchePersonerweitertAnfrage request = new SuchePersonerweitertAnfrage();
final PersonErweitert person = new PersonErweitert().ordnungsmerkmal("om");
final SuchePersonerweitertAntwort response = new SuchePersonerweitertAntwort().addPersonenItem(person);
when(personErweitertApi.deMuenchenEaiEwoRouteROUTEPROCESSSEARCHPERSONERWEITERT(request)).thenReturn(Mono.just(response));

final SuchePersonerweitertAntwort result = adapter.searchExtendedPerson(request);
assertEquals("om", result.getPersonen().get(0).getOrdnungsmerkmal());

}

@Test
void searchExtendedPerson_shouldThrowExceptionIfRequestFailed() {
SuchePersonerweitertAnfrage request = new SuchePersonerweitertAnfrage();
when(personErweitertApi.deMuenchenEaiEwoRouteROUTEPROCESSSEARCHPERSONERWEITERT(request)).thenThrow(badRequestException);

final OkEwoIntegrationClientErrorException exception= assertThrows(OkEwoIntegrationClientErrorException.class, () -> {

adapter.searchExtendedPerson(request);
});

assertTrue(exception.getMessage().contains("400"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
<version>3.3.1</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ spring:
brokers: ${KAFKA_BOOTSTRAP_SERVER:localhost}:${KAFKA_BOOTSTRAP_SERVER_PORT:29092}
bindings:
functionRouter-in-0:
group: "dwf-alw-service"
destination: dwf-alw-${DIGIWF_ENV}
group: "dwf-okewo-service"
destination: dwf-okewo-${DIGIWF_ENV}
sendMessage-out-0:
destination: dwf-connector-${DIGIWF_ENV}
default:
Expand Down Expand Up @@ -95,7 +95,7 @@ de:
oss:
digiwf:
okewo:
url: ""
url: "http://localhost:8080"
username: ""
password: ""
benutzerId: ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.muenchen.oss.digiwf.okewo.integration;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.test.EmbeddedKafkaBroker;

@Configuration
public class EmbeddedKafkaConfig {

@Bean
@ConditionalOnMissingBean
public EmbeddedKafkaBroker embeddedKafkaConfig() {
return new EmbeddedKafkaBroker(
1,
true,
"dwf-okewo-e2e-test",
"dwf-connector-e2e-test",
"dwf-connector-bpmnerror-e2e-test",
"dwf-connector-incident-e2e-test"
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package de.muenchen.oss.digiwf.okewo.integration;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import de.muenchen.oss.digiwf.okewo.integration.client.model.Person;
import de.muenchen.oss.digiwf.okewo.integration.domain.model.request.OkEwoEventRequest;
import de.muenchen.oss.digiwf.okewo.integration.domain.model.request.OrdnungsmerkmalDto;
import de.muenchen.oss.digiwf.okewo.integration.utility.DigiWFIntegrationE2eTest;
import lombok.val;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.kafka.test.context.EmbeddedKafka;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;

import java.util.Map;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("itest")
@DirtiesContext
@EmbeddedKafka(partitions = 1,
brokerProperties = {"listeners=PLAINTEXT://localhost:29092"},
topics = {
"${spring.cloud.stream.bindings.functionRouter-in-0.destination}",
"${spring.cloud.stream.bindings.sendMessage-out-0.destination}",
"${spring.cloud.stream.bindings.integrationTestConsumer-in-0.destination}"
})
@WireMockTest(httpPort = 8089)
class OkEwoIntegrationApplicationE2eTest extends DigiWFIntegrationE2eTest {


@Autowired
private ObjectMapper objectMapper;

private String processInstanceId;

@BeforeEach
void setup() {
this.processInstanceId = UUID.randomUUID().toString();
}

@Test
public void shouldStart() {
// test fails if application context can not start
}

@Test
void shouldProcessGetPersonEvent() throws InterruptedException, JsonProcessingException {
val ordnungsmerkmal = new OrdnungsmerkmalDto();
ordnungsmerkmal.setOrdnungsmerkmal("om");
val request = new OkEwoEventRequest<OrdnungsmerkmalDto>();
request.setRequest(ordnungsmerkmal);

val person = new Person().ordnungsmerkmal("om");
// http://localhost:8089/personen/2.0/rest
// http://localhost:8089/personen/2.0/rest/person/om?benutzerId=benutzerId
this.setupWiremock("/personen/2.0/rest/person/om?benutzerId=benutzerId", objectMapper.writeValueAsString(person));

// send and receive messages
final Map<String, Object> payload = super.runIntegration(request, processInstanceId, "getPerson");
assertNotNull(payload);

}

private void setupWiremock(final String url, final String expectedResponse) {
WireMock.stubFor(WireMock
.get(url)
.willReturn(WireMock
.aResponse()
.withBody(expectedResponse)
.withHeader("Content-Type", "application/json")
.withStatus(200)));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package de.muenchen.oss.digiwf.okewo.integration;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

import static de.muenchen.oss.digiwf.message.common.MessageConstants.DIGIWF_PROCESS_INSTANCE_ID;

@Component
@Slf4j
public class TestMessageConsumer {

private final Map<String, Map<String, Object>> receivedMessages = new HashMap<>();

@Bean
public Consumer<Message<Map<String, Object>>> integrationTestConsumer() {
return message -> {
log.info("TestMessageConsumer::message: {}", message);
final Map<String, Object> payloadVariables = (Map<String, Object>) message.getPayload().get("payloadVariables");
receivedMessages.put(message.getHeaders().get(DIGIWF_PROCESS_INSTANCE_ID).toString(), payloadVariables);
};
}

public Map<String, Object> receiveMessage(final String processInstanceId) {
return receivedMessages.get(processInstanceId);
}

}
Loading

0 comments on commit c89c263

Please sign in to comment.