Search in sources :

Example 1 with Position

use of uk.gov.justice.services.eventsourcing.source.api.service.core.Position in project microservice_framework by CJSCommonPlatform.

the class UrlLinkFactoryTest method shouldReturnEventStreamSelfUrlLink.

@SuppressWarnings("unchecked")
@Test
public void shouldReturnEventStreamSelfUrlLink() throws Exception {
    final UUID streamId = UUID.randomUUID();
    final String eventStreamPathSegmentValue = "eventstreams";
    final String baseURI = "http://localhost:8080/context/";
    final String urlString = baseURI + streamId + "/HEAD/BACKWARD/2";
    final int pageSize = 2;
    final Position position = head();
    final UriInfo uriInfo = mock(UriInfo.class);
    final UriBuilder uriBuilder = mock(UriBuilder.class);
    final List<PathSegment> pathSegmentList = mock(List.class);
    final PathSegment eventStreamPathSegment = mock(PathSegment.class);
    final PathSegment streamIdPathSegment = mock(PathSegment.class);
    when(uriInfo.getBaseUriBuilder()).thenReturn(uriBuilder);
    when(uriInfo.getPathSegments()).thenReturn(pathSegmentList);
    when(pathSegmentList.get(0)).thenReturn(eventStreamPathSegment);
    when(eventStreamPathSegment.getPath()).thenReturn(eventStreamPathSegmentValue);
    when(pathSegmentList.get(1)).thenReturn(streamIdPathSegment);
    when(streamIdPathSegment.getPath()).thenReturn(streamId.toString());
    when(uriBuilder.path(eventStreamPathSegmentValue)).thenReturn(uriBuilder);
    when(uriBuilder.path(streamId.toString())).thenReturn(uriBuilder);
    when(uriBuilder.path(HEAD)).thenReturn(uriBuilder);
    when(uriBuilder.path("BACKWARD")).thenReturn(uriBuilder);
    when(uriBuilder.path("2")).thenReturn(uriBuilder);
    when(uriBuilder.build()).thenReturn(new URL(urlString).toURI());
    when(positionValueFactory.getPositionValue(position)).thenReturn(HEAD);
    final URL link = urlLinkFactory.createEventStreamSelfUrlLink(streamId.toString(), pageSize, uriInfo);
    assertThat(link.toString(), is(urlString));
}
Also used : Position(uk.gov.justice.services.eventsourcing.source.api.service.core.Position) PathSegment(javax.ws.rs.core.PathSegment) UUID(java.util.UUID) UriBuilder(javax.ws.rs.core.UriBuilder) UriInfo(javax.ws.rs.core.UriInfo) URL(java.net.URL) Test(org.junit.Test)

Example 2 with Position

use of uk.gov.justice.services.eventsourcing.source.api.service.core.Position in project microservice_framework by CJSCommonPlatform.

the class EventStreamPageServiceTest method shouldReturnEventsWhenLessRecordsThanPageSizeWhenLookingForNewerEvents.

@Test
public void shouldReturnEventsWhenLessRecordsThanPageSizeWhenLookingForNewerEvents() throws Exception {
    final UUID streamId3 = randomUUID();
    final ResteasyUriInfo uriInfo = new ResteasyUriInfo(create(BASE_URL), create(EVENT_STREAM_PATH));
    final List<EventStreamEntry> eventStreams = new ArrayList<>();
    final EventStreamEntry eventStreamEntry1 = new EventStreamEntry(streamId3.toString(), 3L);
    eventStreams.add(eventStreamEntry1);
    when(service.eventStreamExists(4L)).thenReturn(true);
    when(service.eventStreamExists(2L)).thenReturn(false);
    final Position position = position(3L);
    when(positionFactory.createPosition("3")).thenReturn(position);
    when(service.eventStreams(position, FORWARD, 2)).thenReturn(eventStreams);
    final URL streamId3SelfUrl = new URL(BASE_URL + EVENT_STREAM_PATH + streamId3 + "/HEAD/BACKWARD/2");
    when(urlLinkFactory.createEventStreamSelfUrlLink(streamId3.toString(), 2, uriInfo)).thenReturn(streamId3SelfUrl);
    final URL nextUrl = new URL(BASE_URL + EVENT_STREAM_PATH + "/4/FORWARD/2");
    when(urlLinkFactory.createEventStreamUrlLink(position(4L), FORWARD, 2, uriInfo)).thenReturn(nextUrl);
    final URL headURL = new URL(BASE_URL + EVENT_STREAM_PATH + "/HEAD/BACKWARD/2");
    when(urlLinkFactory.createHeadEventStreamsUrlLink(2, uriInfo)).thenReturn(headURL);
    final URL firstURL = new URL(BASE_URL + EVENT_STREAM_PATH + "/1/FORWARD/2");
    when(urlLinkFactory.createFirstEventStreamsUrlLink(2, uriInfo)).thenReturn(firstURL);
    final Page<EventStreamPageEntry> pageEvents = eventStreamPageService.pageOfEventStream("3", FORWARD, PAGE_SIZE, uriInfo);
    final List<EventStreamPageEntry> pageEventsData = pageEvents.getData();
    final PagingLinks pagingLinks = pageEvents.getPagingLinks();
    assertThat(pageEventsData, hasSize(1));
    assertThat(pageEventsData.get(0).getSelf(), is(streamId3SelfUrl.toString()));
    assertThat(pageEventsData.get(0).getSequenceNumber(), is(3L));
    assertThat(pagingLinks.getPrevious(), is(Optional.empty()));
    assertThat(pagingLinks.getNext().get(), is(nextUrl));
    assertThat(pagingLinks.getHead(), is(headURL));
    assertThat(pagingLinks.getFirst(), is(firstURL));
}
Also used : EventStreamEntry(uk.gov.justice.services.eventsourcing.source.api.service.core.EventStreamEntry) Position(uk.gov.justice.services.eventsourcing.source.api.service.core.Position) ResteasyUriInfo(org.jboss.resteasy.spi.ResteasyUriInfo) ArrayList(java.util.ArrayList) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) URL(java.net.URL) Test(org.junit.Test)

Example 3 with Position

use of uk.gov.justice.services.eventsourcing.source.api.service.core.Position in project microservice_framework by CJSCommonPlatform.

the class EventStreamPageServiceTest method shouldReturnEventsWhenLessRecordsThanPageSizeWhenLookingForOlderEvents.

@Test
public void shouldReturnEventsWhenLessRecordsThanPageSizeWhenLookingForOlderEvents() throws Exception {
    final UUID streamId3 = randomUUID();
    final ResteasyUriInfo uriInfo = new ResteasyUriInfo(create("http://server:123/context/"), create("event-streams/"));
    final List<EventStreamEntry> eventStreams = new ArrayList<>();
    final EventStreamEntry eventsStream1 = new EventStreamEntry(streamId3.toString(), 3L);
    eventStreams.add(eventsStream1);
    when(service.eventStreamExists(4L)).thenReturn(false);
    when(service.eventStreamExists(2L)).thenReturn(true);
    final Position position = position(3L);
    when(positionFactory.createPosition("3")).thenReturn(position);
    when(service.eventStreams(position, BACKWARD, 2L)).thenReturn(eventStreams);
    final URL headURL = new URL(BASE_URL + EVENT_STREAM_PATH + "/HEAD/BACKWARD/2");
    when(urlLinkFactory.createHeadEventStreamsUrlLink(2, uriInfo)).thenReturn(headURL);
    final URL firstURL = new URL(BASE_URL + EVENT_STREAM_PATH + "/1/FORWARD/2");
    when(urlLinkFactory.createFirstEventStreamsUrlLink(2, uriInfo)).thenReturn(firstURL);
    final URL streamId3SelfUrl = new URL(BASE_URL + EVENT_STREAM_PATH + "/" + streamId3 + "/HEAD/BACKWARD/2");
    when(urlLinkFactory.createEventStreamSelfUrlLink(streamId3.toString(), 2, uriInfo)).thenReturn(streamId3SelfUrl);
    final URL previousUrl = new URL(BASE_URL + EVENT_STREAM_PATH + "/2/BACKWARD/2");
    when(urlLinkFactory.createEventStreamUrlLink(position(2L), BACKWARD, 2, uriInfo)).thenReturn(previousUrl);
    final Page<EventStreamPageEntry> feedActual = eventStreamPageService.pageOfEventStream("3", BACKWARD, PAGE_SIZE, uriInfo);
    final List<EventStreamPageEntry> feed = feedActual.getData();
    final PagingLinks paging = feedActual.getPagingLinks();
    assertThat(feed, hasSize(1));
    assertThat(feed.get(0).getSelf(), is(streamId3SelfUrl.toString()));
    assertThat(feed.get(0).getSequenceNumber(), is(3L));
    assertThat(paging.getNext(), is(Optional.empty()));
    assertThat(paging.getPrevious().get(), is(previousUrl));
    assertThat(paging.getHead(), is(headURL));
    assertThat(paging.getFirst(), is(firstURL));
}
Also used : EventStreamEntry(uk.gov.justice.services.eventsourcing.source.api.service.core.EventStreamEntry) Position(uk.gov.justice.services.eventsourcing.source.api.service.core.Position) ResteasyUriInfo(org.jboss.resteasy.spi.ResteasyUriInfo) ArrayList(java.util.ArrayList) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) URL(java.net.URL) Test(org.junit.Test)

Example 4 with Position

use of uk.gov.justice.services.eventsourcing.source.api.service.core.Position in project microservice_framework by CJSCommonPlatform.

the class EventsPageServiceTest method shouldReturnFeedWhenSameNumberOfRecordsAsPageSizeWhenLookingForNewerEvents.

@Test
public void shouldReturnFeedWhenSameNumberOfRecordsAsPageSizeWhenLookingForNewerEvents() throws Exception {
    final UUID streamId = randomUUID();
    final List<EventEntry> events = new ArrayList<>();
    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 JsonObject payloadEvent1 = createObjectBuilder().add("field1", "value1").build();
    final JsonObject payloadEvent2 = createObjectBuilder().add("field2", "value2").build();
    final EventEntry event1 = new EventEntry(randomUUID(), streamId, 1L, "Test Name1", payloadEvent1, new UtcClock().now().toString());
    final EventEntry event2 = new EventEntry(randomUUID(), streamId, 2L, "Test Name2", payloadEvent2, new UtcClock().now().toString());
    events.add(event2);
    events.add(event1);
    when(service.eventExists(streamId, 3L)).thenReturn(true);
    when(service.eventExists(streamId, 0L)).thenReturn(false);
    final URL nextUrl = new URL(BASE_URL + EVENT_STREAM_PATH + streamId + "/3/FORWARD/2");
    when(urlLinkFactory.createEventsUrlLink(position(3L), FORWARD, 2, uriInfo)).thenReturn(nextUrl);
    final Position position = position(3L);
    when(positionFactory.createPosition("3")).thenReturn(position);
    when(service.events(streamId, position, FORWARD, 2L)).thenReturn(events);
    final Page<EventEntry> feedActual = eventsPageService.pageEvents(streamId, "3", FORWARD, 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(2L));
    assertThat(feed.get(0).getPayload(), is(payloadEvent2));
    assertThat(feed.get(1).getStreamId(), is(streamId.toString()));
    assertThat(feed.get(1).getPosition(), is(1L));
    assertThat(feed.get(1).getPayload(), is(payloadEvent1));
    assertThat(pagingLinks.getNext().get(), is(nextUrl));
    assertThat(pagingLinks.getPrevious(), is(empty()));
    assertThat(pagingLinks.getHead(), is(headURL));
    assertThat(pagingLinks.getFirst(), is(firstURL));
}
Also used : UtcClock(uk.gov.justice.services.common.util.UtcClock) EventEntry(uk.gov.justice.services.eventsourcing.source.api.service.core.EventEntry) Position(uk.gov.justice.services.eventsourcing.source.api.service.core.Position) ResteasyUriInfo(org.jboss.resteasy.spi.ResteasyUriInfo) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) URL(java.net.URL) Test(org.junit.Test)

Example 5 with Position

use of uk.gov.justice.services.eventsourcing.source.api.service.core.Position in project microservice_framework by CJSCommonPlatform.

the class EventsPageServiceTest method shouldReturnEventsWhenLessRecordsThanPageSizeWhenLookingForNewerEvents.

@Test
public void shouldReturnEventsWhenLessRecordsThanPageSizeWhenLookingForNewerEvents() throws Exception {
    final UUID streamId = randomUUID();
    final ResteasyUriInfo uriInfo = new ResteasyUriInfo(create("http://server:123/context/"), create("event-stĀ¬reams/" + streamId));
    final List<EventEntry> events = new ArrayList<>();
    final EventEntry event1 = new EventEntry(randomUUID(), streamId, 3L, "Test Name3", createObjectBuilder().add("field3", "value3").build(), new UtcClock().now().toString());
    events.add(event1);
    when(service.eventExists(streamId, 4L)).thenReturn(true);
    when(service.eventExists(streamId, 2L)).thenReturn(false);
    final Position position = position(3L);
    when(positionFactory.createPosition("3")).thenReturn(position);
    when(service.events(streamId, position, FORWARD, 2L)).thenReturn(events);
    final URL nextUrl = new URL(BASE_URL + EVENT_STREAM_PATH + streamId + "/4/FORWARD/2");
    when(urlLinkFactory.createEventsUrlLink(position(4L), FORWARD, 2, uriInfo)).thenReturn(nextUrl);
    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 Page<EventEntry> pageEvents = eventsPageService.pageEvents(streamId, "3", FORWARD, 2, uriInfo);
    final List<EventEntry> pageEventsData = pageEvents.getData();
    final PagingLinks pagingLinks = pageEvents.getPagingLinks();
    assertThat(pageEventsData, hasSize(1));
    assertThat(pageEventsData.get(0).getStreamId(), is(streamId.toString()));
    assertThat(pageEventsData.get(0).getPosition(), is(3L));
    assertThat(pageEventsData.get(0).getPayload(), is(notNullValue()));
    assertThat(pagingLinks.getNext().get(), is(nextUrl));
    assertThat(pagingLinks.getPrevious(), is(empty()));
    assertThat(pagingLinks.getHead(), is(headURL));
    assertThat(pagingLinks.getFirst(), is(firstURL));
}
Also used : UtcClock(uk.gov.justice.services.common.util.UtcClock) EventEntry(uk.gov.justice.services.eventsourcing.source.api.service.core.EventEntry) Position(uk.gov.justice.services.eventsourcing.source.api.service.core.Position) ResteasyUriInfo(org.jboss.resteasy.spi.ResteasyUriInfo) ArrayList(java.util.ArrayList) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) URL(java.net.URL) Test(org.junit.Test)

Aggregations

URL (java.net.URL)11 UUID (java.util.UUID)11 Test (org.junit.Test)11 Position (uk.gov.justice.services.eventsourcing.source.api.service.core.Position)11 UUID.randomUUID (java.util.UUID.randomUUID)10 ResteasyUriInfo (org.jboss.resteasy.spi.ResteasyUriInfo)10 ArrayList (java.util.ArrayList)8 EventEntry (uk.gov.justice.services.eventsourcing.source.api.service.core.EventEntry)5 EventStreamEntry (uk.gov.justice.services.eventsourcing.source.api.service.core.EventStreamEntry)5 UtcClock (uk.gov.justice.services.common.util.UtcClock)4 JsonObject (javax.json.JsonObject)2 Direction (uk.gov.justice.services.eventsourcing.source.api.service.core.Direction)2 PathSegment (javax.ws.rs.core.PathSegment)1 UriBuilder (javax.ws.rs.core.UriBuilder)1 UriInfo (javax.ws.rs.core.UriInfo)1