diff --git a/src/main/java/uk/gov/justice/services/test/DomainTest.java b/src/main/java/uk/gov/justice/services/test/DomainTest.java index 68b09bb..23c0618 100644 --- a/src/main/java/uk/gov/justice/services/test/DomainTest.java +++ b/src/main/java/uk/gov/justice/services/test/DomainTest.java @@ -103,7 +103,16 @@ public static Object[] getMethodArguments(final JsonNode jsonNode, final Method } public static JsonNode generatedEventAsJsonNode(final Object generatedEvent) { - return OBJECT_MAPPER.valueToTree(generatedEvent); + + try { + // We don't use OBJECT_MAPPER.valueToTree(generatedEvent) as this creates a Jackson ObjectNode (JsonNode) that + // doesn't allow us to compare to the JsonNodes created from resources/json/.json files when the event class + // contains BigDecimals + return OBJECT_MAPPER.readTree(OBJECT_MAPPER.writeValueAsBytes(generatedEvent)); + } catch (IOException e) { + throw new IllegalArgumentException(format("Error creating JsonNode from event object: %s", generatedEvent), e); + } + } public static List generatedEventAsJsonNodeList(final List generatedEvent) { diff --git a/src/test/java/uk/gov/justice/services/test/DomainTestTest.java b/src/test/java/uk/gov/justice/services/test/DomainTestTest.java index abab7de..7f5d8a4 100644 --- a/src/test/java/uk/gov/justice/services/test/DomainTestTest.java +++ b/src/test/java/uk/gov/justice/services/test/DomainTestTest.java @@ -3,6 +3,10 @@ import static org.hamcrest.Matchers.hasSize; import static org.junit.Assert.assertThat; +import uk.gov.justice.services.test.util.CantBeSerializedByJackson; + +import java.io.ByteArrayOutputStream; + import com.google.common.collect.Lists; import org.junit.Test; @@ -13,4 +17,19 @@ public void testEmptyInputOutput() { assertThat(DomainTest.generatedEventAsJsonNodeList(Lists.newArrayList()), hasSize(0)); } + @Test(expected = IllegalArgumentException.class) + public void shouldThrowIllegalArgumentExceptionWhenNoEventMatchesEventNameInJsonFile() throws Exception { + + String filename = "unknown-event"; + DomainTest.eventsFromFileNames(filename); + + } + + @Test(expected = IllegalArgumentException.class) + public void shouldThrowIllegalArgumentExceptionWhenObjectWontSerializeToJson() throws Exception { + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + System.err.println(DomainTest.generatedEventAsJsonNode(new CantBeSerializedByJackson("one", "two", "three"))); + } + } diff --git a/src/test/java/uk/gov/justice/services/test/domain/aggregate/StatefulAggregate.java b/src/test/java/uk/gov/justice/services/test/domain/aggregate/StatefulAggregate.java index b4440be..2483b7a 100644 --- a/src/test/java/uk/gov/justice/services/test/domain/aggregate/StatefulAggregate.java +++ b/src/test/java/uk/gov/justice/services/test/domain/aggregate/StatefulAggregate.java @@ -8,7 +8,9 @@ import uk.gov.justice.services.test.domain.event.CakeOrdered; import uk.gov.justice.services.test.domain.event.SomethingElseHappened; import uk.gov.justice.services.test.domain.event.SomethingHappened; +import uk.gov.justice.services.test.domain.event.SomethingWithBigDecimalHappened; +import java.math.BigDecimal; import java.util.UUID; import java.util.stream.Stream; @@ -27,6 +29,12 @@ public Stream doSomethingTwice(final UUID id) { return apply(Stream.of(new SomethingHappened(id), new SomethingHappened(id))); } + + public Stream doSomethingReturningMultipleEvents(final UUID id) { + return apply(Stream.of(new SomethingHappened(id), new SomethingHappened(id), new SomethingElseHappened(id), new SomethingWithBigDecimalHappened(id, new BigDecimal("500.3")), new SomethingElseHappened(id), new SomethingElseHappened(id))); + } + + public Stream doNothing() { return Stream.empty(); } @@ -36,6 +44,8 @@ public Object apply(final Object event) { return match(event).with( when(SomethingHappened.class) .apply(somethingHappenedEvent -> this.eventApplied = true), + when(SomethingWithBigDecimalHappened.class) + .apply(somethingWithBigDecimalHappenedEvent -> this.eventApplied = true), when(CakeOrdered.class) .apply(cakeOrderedEvent -> doNothing()), when(SomethingElseHappened.class) diff --git a/src/test/java/uk/gov/justice/services/test/domain/event/SomethingWithBigDecimalHappened.java b/src/test/java/uk/gov/justice/services/test/domain/event/SomethingWithBigDecimalHappened.java new file mode 100644 index 0000000..1b01b4d --- /dev/null +++ b/src/test/java/uk/gov/justice/services/test/domain/event/SomethingWithBigDecimalHappened.java @@ -0,0 +1,27 @@ +package uk.gov.justice.services.test.domain.event; + +import uk.gov.justice.domain.annotation.Event; + +import java.math.BigDecimal; +import java.util.UUID; + +@Event("something-with-bigdecimal-happened") +public class SomethingWithBigDecimalHappened { + + private final UUID id; + private BigDecimal bd; + + public SomethingWithBigDecimalHappened(final UUID id, BigDecimal bd) { + this.id = id; + this.bd = bd; + } + + public UUID getId() { + return id; + } + + public BigDecimal getBd() { + return bd; + } + +} diff --git a/src/test/java/uk/gov/justice/services/test/util/CantBeSerializedByJackson.java b/src/test/java/uk/gov/justice/services/test/util/CantBeSerializedByJackson.java new file mode 100644 index 0000000..afcae67 --- /dev/null +++ b/src/test/java/uk/gov/justice/services/test/util/CantBeSerializedByJackson.java @@ -0,0 +1,22 @@ +package uk.gov.justice.services.test.util; + +import uk.gov.justice.domain.annotation.Event; + +/* + Jackson can't serialise this class as its attributes are all private, results in an error which + we need for testing + */ +public class CantBeSerializedByJackson { + + private String attrOne; + private String attrTwo; + private String attrThree; + + public CantBeSerializedByJackson(String attrOne, String attrTwo, String attrThree) { + this.attrOne = attrOne; + this.attrTwo = attrTwo; + this.attrThree = attrThree; + } + + +} diff --git a/src/test/resources/domain-features/statefulaggregate.feature b/src/test/resources/domain-features/statefulaggregate.feature index 4892f7b..dd1f6d2 100644 --- a/src/test/resources/domain-features/statefulaggregate.feature +++ b/src/test/resources/domain-features/statefulaggregate.feature @@ -28,4 +28,15 @@ Feature: StatefulAggregate Given multiple things happened When you doSomethingTwice on a StatefulAggregate using an argsA - Then multiple things happened \ No newline at end of file + Then multiple things happened + + Scenario: Multiple previous events from a single file are applied, many new events occur + + Given multiple things happened + When you doSomethingReturningMultipleEvents on a StatefulAggregate using an argsA + Then something happened + And something happened + And something-else-happened + And something-with-bigdecimal-happened + And something-else-happened + And something-else-happened diff --git a/src/test/resources/json/something-with-bigdecimal-happened.json b/src/test/resources/json/something-with-bigdecimal-happened.json new file mode 100644 index 0000000..4345e45 --- /dev/null +++ b/src/test/resources/json/something-with-bigdecimal-happened.json @@ -0,0 +1,7 @@ +{ + "_metadata": { + "name": "something-with-bigdecimal-happened" + }, + "id": "5c5a1d30-0414-11e7-93ae-92361f002671", + "bd": 500.3 +} \ No newline at end of file diff --git a/src/test/resources/json/unknown-event.json b/src/test/resources/json/unknown-event.json new file mode 100644 index 0000000..4d2a66d --- /dev/null +++ b/src/test/resources/json/unknown-event.json @@ -0,0 +1,7 @@ +{ + "_metadata": { + "name": "unknown.event" + }, + "id": "5c5a1d30-0414-11e7-93ae-92361f002671", + "stringField": "Abc123" +} \ No newline at end of file