-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
70 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 7 additions & 26 deletions
33
.../java/uk/gov/justice/services/eventsourcing/prepublish/MetadataSequenceNumberUpdater.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,15 @@ | ||
package uk.gov.justice.services.eventsourcing.prepublish; | ||
|
||
import uk.gov.justice.services.eventsourcing.PublishQueueException; | ||
import static uk.gov.justice.services.messaging.Envelope.metadataFrom; | ||
|
||
import java.io.IOException; | ||
import java.io.StringReader; | ||
|
||
import javax.inject.Inject; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import uk.gov.justice.services.messaging.Metadata; | ||
|
||
public class MetadataSequenceNumberUpdater { | ||
|
||
@Inject | ||
ObjectMapper objectMapper; | ||
|
||
public String updateMetadataJson(final String metadataJson, final long previousSequenceNumber, final long sequenceNumber) { | ||
final ObjectNode objectNode = asObjectNode(metadataJson); | ||
|
||
objectNode.put("previousEventNumber", previousSequenceNumber); | ||
objectNode.put("eventNumber", sequenceNumber); | ||
|
||
return objectNode.toString(); | ||
} | ||
|
||
private ObjectNode asObjectNode(final String metadataJson) { | ||
try { | ||
return (ObjectNode) objectMapper.readTree(new StringReader(metadataJson)); | ||
} catch (final IOException e) { | ||
throw new PublishQueueException("Failed to parse metadata into a json ObjectNode"); | ||
} | ||
public Metadata updateMetadataJson(final Metadata metadata, final long previousSequenceNumber, final long sequenceNumber) { | ||
return metadataFrom(metadata) | ||
.withEventNumber(sequenceNumber) | ||
.withPreviousEventNumber(previousSequenceNumber) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 24 additions & 27 deletions
51
...a/uk/gov/justice/services/eventsourcing/prepublish/MetadataSequenceNumberUpdaterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,52 @@ | ||
package uk.gov.justice.services.eventsourcing.prepublish; | ||
|
||
import static com.jayway.jsonassert.JsonAssert.with; | ||
import static java.util.Optional.of; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
import static uk.gov.justice.services.messaging.Envelope.metadataBuilder; | ||
|
||
import uk.gov.justice.services.common.converter.jackson.ObjectMapperProducer; | ||
import uk.gov.justice.services.messaging.Metadata; | ||
|
||
import java.util.UUID; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Spy; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class MetadataSequenceNumberUpdaterTest { | ||
|
||
@Spy | ||
@SuppressWarnings("unused") | ||
private ObjectMapper objectMapper = new ObjectMapperProducer().objectMapper(); | ||
|
||
@InjectMocks | ||
private MetadataSequenceNumberUpdater metadataSequenceNumberUpdater; | ||
|
||
@Test | ||
public void shouldAddPreviousAndNextSequenceNumberIntoMetadataJson() throws Exception { | ||
|
||
final String name = "example.first-event"; | ||
final String eventSource = "event source"; | ||
final UUID id = UUID.fromString("07652a0b-3a12-4b93-ad24-a33c7c171f19"); | ||
final UUID streamId = UUID.fromString("031e1bec-8c3d-45fb-b206-c70f33678f58"); | ||
final long previousSequenceNumber = 23; | ||
final long sequenceNumber = 24; | ||
|
||
final String metadataJson = "{\n" + | ||
" \"id\":\"07652a0b-3a12-4b93-ad24-a33c7c171f19\",\n" + | ||
" \"name\":\"example.first-event\",\n" + | ||
" \"stream\":{\n" + | ||
" \"id\":\"031e1bec-8c3d-45fb-b206-c70f33678f58\"\n" + | ||
" },\n" + | ||
" \"source\":\"event source\"\n" + | ||
"}"; | ||
|
||
final Metadata originalMetadata = metadataBuilder() | ||
.withId(id) | ||
.withName(name) | ||
.withStreamId(streamId) | ||
.withSource(eventSource) | ||
.build(); | ||
|
||
final String updatedMetadata = metadataSequenceNumberUpdater.updateMetadataJson( | ||
metadataJson, | ||
final Metadata updatedMetadata = metadataSequenceNumberUpdater.updateMetadataJson( | ||
originalMetadata, | ||
previousSequenceNumber, | ||
sequenceNumber); | ||
|
||
with(updatedMetadata) | ||
.assertThat("$.id", is("07652a0b-3a12-4b93-ad24-a33c7c171f19")) | ||
.assertThat("$.name", is("example.first-event")) | ||
.assertThat("$.stream.id", is("031e1bec-8c3d-45fb-b206-c70f33678f58")) | ||
.assertThat("$.source", is("event source")) | ||
.assertThat("$.previousEventNumber", is((int) previousSequenceNumber)) | ||
.assertThat("$.eventNumber", is((int) sequenceNumber)) | ||
; | ||
assertThat(updatedMetadata.id(), is(id)); | ||
assertThat(updatedMetadata.name(), is(name)); | ||
assertThat(updatedMetadata.streamId(), is(of(streamId))); | ||
assertThat(updatedMetadata.source(), is(of(eventSource))); | ||
assertThat(updatedMetadata.eventNumber(), is(of(sequenceNumber))); | ||
assertThat(updatedMetadata.previousEventNumber(), is(of(previousSequenceNumber))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters