Added integration test

This commit is contained in:
Michael Schnell
2019-12-28 16:21:24 +01:00
parent 69dc18f424
commit 919c82971b
2 changed files with 199 additions and 0 deletions

View File

@@ -84,6 +84,10 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
@@ -91,15 +95,113 @@
</exclusions>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>spring-mock-mvc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.31.0</version>
<configuration>
<images>
<image>
<name>eventstore/eventstore:release-4.1.3</name>
<run>
<network>
<mode>bridge</mode>
</network>
<ports>
<port>1113:1113</port>
<port>2113:2113</port>
</ports>
<log>
<enabled>false</enabled>
</log>
<wait>
<http>
<url>http://localhost:2113/web/index.html#/</url>
<method>GET</method>
</http>
<time>20000</time>
</wait>
</run>
</image>
</images>
</configuration>
<executions>
<execution>
<id>start-images</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-images</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,97 @@
package org.fuin.cqrs4j.example.spring.command.api;
import static io.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import javax.json.bind.Jsonb;
import org.fuin.cqrs4j.ResultType;
import org.fuin.cqrs4j.SimpleResult;
import org.fuin.cqrs4j.example.spring.command.app.CmdApplication;
import org.fuin.cqrs4j.example.spring.shared.CreatePersonCommand;
import org.fuin.cqrs4j.example.spring.shared.PersonCreatedEvent;
import org.fuin.cqrs4j.example.spring.shared.PersonId;
import org.fuin.cqrs4j.example.spring.shared.PersonName;
import org.fuin.esc.api.CommonEvent;
import org.fuin.esc.api.EventStore;
import org.fuin.esc.api.SimpleStreamId;
import org.fuin.esc.api.StreamEventsSlice;
import org.fuin.esc.api.TypeName;
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.boot.web.server.LocalServerPort;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.web.context.WebApplicationContext;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.module.mockmvc.RestAssuredMockMvc;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = CmdApplication.class)
public class PersonControllerIT {
@LocalServerPort
int port;
@Autowired
WebApplicationContext wac;
@Autowired
EventStore eventStore;
@Autowired
Jsonb jsonb;
@BeforeEach
public void initRestAssuredMockMvcStandalone() {
RestAssured.port = port;
RestAssuredMockMvc.webAppContextSetup(wac);
}
@Test
public void testCreate() {
// PREPARE
final PersonId personId = new PersonId(UUID.randomUUID());
final PersonName personName = new PersonName("Peter Parker");
final CreatePersonCommand cmd = new CreatePersonCommand(personId, personName);
final String json = jsonb.toJson(cmd);
// TEST & VERIFY
final SimpleResult result =
given()
.accept(ContentType.JSON)
.contentType(ContentType.JSON)
.body(json)
.when()
.post("/persons/create")
.then()
.statusCode(200)
.extract()
.as(SimpleResult.class);
assertThat(result.getType(), is(equalTo(ResultType.OK)));
final SimpleStreamId personStreamId = new SimpleStreamId(PersonId.TYPE + "-" + personId);
final StreamEventsSlice slice = eventStore.readEventsForward(personStreamId, 0, 1);
final List<CommonEvent> events = slice.getEvents();
assertThat(Arrays.asList(events), is(not(empty())));
final CommonEvent ce = events.get(0);
assertThat(ce.getDataType(), is(equalTo(new TypeName(PersonCreatedEvent.TYPE.asBaseType()))));
final PersonCreatedEvent event = (PersonCreatedEvent) ce.getData();
assertThat(event.getEntityId(), is(equalTo(personId)));
assertThat(event.getName(), is(equalTo(personName)));
}
}