use of uk.gov.justice.services.eventsourcing.source.api.service.core.Position in project microservice_framework by CJSCommonPlatform.
the class EventsPageServiceTest method shouldReturnResultWhenSameNumberOfRecordsAsPageSizeWhenLookingForOlderEvents.
@Test
public void shouldReturnResultWhenSameNumberOfRecordsAsPageSizeWhenLookingForOlderEvents() throws Exception {
final UUID streamId = randomUUID();
final List<EventEntry> events = new ArrayList<>();
final JsonObject payloadEvent4 = createObjectBuilder().add("field4", "value4").build();
final JsonObject payloadEvent3 = createObjectBuilder().add("field3", "value3").build();
final EventEntry event4 = new EventEntry(randomUUID(), streamId, 4L, "Test Name4", payloadEvent4, new UtcClock().now().toString());
final EventEntry event3 = new EventEntry(randomUUID(), streamId, 3L, "Test Name3", payloadEvent3, new UtcClock().now().toString());
events.add(event4);
events.add(event3);
when(service.eventExists(streamId, 5L)).thenReturn(false);
when(service.eventExists(streamId, 2L)).thenReturn(true);
final Position position = position(4L);
when(positionFactory.createPosition("4")).thenReturn(position);
when(service.events(streamId, position, BACKWARD, 2)).thenReturn(events);
final ResteasyUriInfo uriInfo = new ResteasyUriInfo(create("http://server:123/context/"), create("event-streams/" + streamId));
final URL headURL = new URL(BASE_URL + EVENT_STREAM_PATH + streamId + "/HEAD/BACKWARD/2");
when(urlLinkFactory.createHeadEventsUrlLink(2, uriInfo)).thenReturn(headURL);
final URL firstURL = new URL(BASE_URL + EVENT_STREAM_PATH + streamId + "/1/FORWARD/2");
when(urlLinkFactory.createFirstEventsUrlLink(2, uriInfo)).thenReturn(firstURL);
final URL previousUrl = new URL(BASE_URL + EVENT_STREAM_PATH + streamId + "/2/BACKWARD/2");
when(urlLinkFactory.createEventsUrlLink(position(2L), BACKWARD, 2, uriInfo)).thenReturn(previousUrl);
final Page<EventEntry> feedActual = eventsPageService.pageEvents(streamId, "4", BACKWARD, 2, uriInfo);
final List<EventEntry> feed = feedActual.getData();
final PagingLinks pagingLinks = feedActual.getPagingLinks();
assertThat(feed, hasSize(2));
assertThat(feed.get(0).getStreamId(), is(streamId.toString()));
assertThat(feed.get(0).getPosition(), is(4L));
assertThat(feed.get(0).getPayload(), is(payloadEvent4));
assertThat(feed.get(1).getStreamId(), is(streamId.toString()));
assertThat(feed.get(1).getPosition(), is(3L));
assertThat(feed.get(1).getPayload(), is(payloadEvent3));
assertThat(pagingLinks.getNext(), is(empty()));
assertThat(pagingLinks.getPrevious().get(), is(previousUrl));
assertThat(pagingLinks.getHead(), is(headURL));
assertThat(pagingLinks.getFirst(), is(firstURL));
}
Aggregations