Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

Fix bug when comparing Events containing BigDecimal attributes #18

Merged
merged 1 commit into from
Jun 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/main/java/uk/gov/justice/services/test/DomainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonNode> generatedEventAsJsonNodeList(final List<Object> generatedEvent) {
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/uk/gov/justice/services/test/DomainTestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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")));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -27,6 +29,12 @@ public Stream<Object> doSomethingTwice(final UUID id) {
return apply(Stream.of(new SomethingHappened(id), new SomethingHappened(id)));
}


public Stream<Object> 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<Object> doNothing() {
return Stream.empty();
}
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
}


}
13 changes: 12 additions & 1 deletion src/test/resources/domain-features/statefulaggregate.feature
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,15 @@ Feature: StatefulAggregate

Given multiple things happened
When you doSomethingTwice on a StatefulAggregate using an argsA
Then multiple things happened
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"_metadata": {
"name": "something-with-bigdecimal-happened"
},
"id": "5c5a1d30-0414-11e7-93ae-92361f002671",
"bd": 500.3
}
7 changes: 7 additions & 0 deletions src/test/resources/json/unknown-event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"_metadata": {
"name": "unknown.event"
},
"id": "5c5a1d30-0414-11e7-93ae-92361f002671",
"stringField": "Abc123"
}