diff --git a/spring-boot/command/pom.xml b/spring-boot/command/pom.xml index 5115be6..12cac55 100644 --- a/spring-boot/command/pom.xml +++ b/spring-boot/command/pom.xml @@ -84,6 +84,10 @@ spring-boot-starter-test test + + junit + junit + org.junit.vintage junit-vintage-engine @@ -91,15 +95,113 @@ + + org.junit.jupiter + junit-jupiter-api + test + + + + org.junit.jupiter + junit-jupiter-engine + test + + + + io.rest-assured + rest-assured + test + + + + io.rest-assured + spring-mock-mvc + test + + + + org.awaitility + awaitility + test + + + + org.springframework.boot spring-boot-maven-plugin + + + maven-failsafe-plugin + + + + integration-test + verify + + + + + + + io.fabric8 + docker-maven-plugin + 0.31.0 + + + + + eventstore/eventstore:release-4.1.3 + + + bridge + + + 1113:1113 + 2113:2113 + + + false + + + + http://localhost:2113/web/index.html#/ + GET + + + + + + + + + + + + start-images + pre-integration-test + + start + + + + stop-images + post-integration-test + + stop + + + + + + + diff --git a/spring-boot/command/src/test/java/org/fuin/cqrs4j/example/spring/command/api/PersonControllerIT.java b/spring-boot/command/src/test/java/org/fuin/cqrs4j/example/spring/command/api/PersonControllerIT.java new file mode 100644 index 0000000..c247a09 --- /dev/null +++ b/spring-boot/command/src/test/java/org/fuin/cqrs4j/example/spring/command/api/PersonControllerIT.java @@ -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 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))); + + } + +} \ No newline at end of file