Search in sources :

Example 6 with ResteasyUriInfo

use of org.jboss.resteasy.spi.ResteasyUriInfo 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 7 with ResteasyUriInfo

use of org.jboss.resteasy.spi.ResteasyUriInfo 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)

Example 8 with ResteasyUriInfo

use of org.jboss.resteasy.spi.ResteasyUriInfo in project microservice_framework by CJSCommonPlatform.

the class EventsPageServiceTest method shouldReturnEmptyListWithPagingLinks.

@Test
public void shouldReturnEmptyListWithPagingLinks() throws Exception {
    final UUID streamId = randomUUID();
    final Position sequence = position(3L);
    final Direction backward = BACKWARD;
    final ResteasyUriInfo uriInfo = new ResteasyUriInfo(create("http://server:123/context/"), create("event-stĀ¬reams/" + streamId));
    final List<EventEntry> entries = emptyList();
    when(service.events(streamId, sequence, backward, 2)).thenReturn(entries);
    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> eventEntryPage = eventsPageService.pageEvents(streamId, "3", backward, 2, uriInfo);
    assertThat(eventEntryPage.getData(), is(entries));
    assertThat(eventEntryPage.getPagingLinks().getFirst(), is(firstURL));
    assertThat(eventEntryPage.getPagingLinks().getHead(), is(headURL));
    assertThat(eventEntryPage.getPagingLinks().getNext(), is(empty()));
    assertThat(eventEntryPage.getPagingLinks().getPrevious(), is(empty()));
}
Also used : Position(uk.gov.justice.services.eventsourcing.source.api.service.core.Position) EventEntry(uk.gov.justice.services.eventsourcing.source.api.service.core.EventEntry) ResteasyUriInfo(org.jboss.resteasy.spi.ResteasyUriInfo) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) Direction(uk.gov.justice.services.eventsourcing.source.api.service.core.Direction) URL(java.net.URL) Test(org.junit.Test)

Example 9 with ResteasyUriInfo

use of org.jboss.resteasy.spi.ResteasyUriInfo in project microservice_framework by CJSCommonPlatform.

the class EventsPageServiceTest method shouldReturnLinkForPage2OnHead.

@Test
public void shouldReturnLinkForPage2OnHead() throws Exception {
    final UUID streamId = randomUUID();
    final List<EventEntry> events = new ArrayList<>();
    final EventEntry event2 = new EventEntry(randomUUID(), streamId, 2L, "Test Name2", createObjectBuilder().add("field2", "value2").build(), new UtcClock().now().toString());
    final EventEntry event3 = new EventEntry(randomUUID(), streamId, 3L, "Test Name3", createObjectBuilder().add("field3", "value3").build(), new UtcClock().now().toString());
    events.add(event3);
    events.add(event2);
    when(service.eventExists(streamId, 1L)).thenReturn(true);
    when(positionFactory.createPosition(HEAD)).thenReturn(head());
    when(service.events(streamId, head(), BACKWARD, 2L)).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 + "1/FORWARD/2");
    when(urlLinkFactory.createEventsUrlLink(position(1L), BACKWARD, 2, uriInfo)).thenReturn(previousURL);
    final Page<EventEntry> feed = eventsPageService.pageEvents(streamId, HEAD, BACKWARD, 2, uriInfo);
    assertTrue(feed.getPagingLinks().getNext().equals(empty()));
    assertThat(feed.getPagingLinks().getPrevious().get(), is(previousURL));
    assertThat(feed.getPagingLinks().getHead(), is(headURL));
    assertThat(feed.getPagingLinks().getFirst(), is(firstURL));
}
Also used : UtcClock(uk.gov.justice.services.common.util.UtcClock) EventEntry(uk.gov.justice.services.eventsourcing.source.api.service.core.EventEntry) 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 10 with ResteasyUriInfo

use of org.jboss.resteasy.spi.ResteasyUriInfo in project microservice_framework by CJSCommonPlatform.

the class EventsPageServiceTest method shouldReturnEventsWhenLessRecordsThanPageSizeWhenLookingForOlderEvents.

@Test
public void shouldReturnEventsWhenLessRecordsThanPageSizeWhenLookingForOlderEvents() throws Exception {
    final UUID streamId = randomUUID();
    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 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(false);
    when(service.eventExists(streamId, 2L)).thenReturn(true);
    final Position position = position(3L);
    when(positionFactory.createPosition("3")).thenReturn(position);
    when(service.events(streamId, position, BACKWARD, 2L)).thenReturn(events);
    final Page<EventEntry> feedActual = eventsPageService.pageEvents(streamId, "3", BACKWARD, 2, uriInfo);
    final List<EventEntry> feed = feedActual.getData();
    final PagingLinks pagingLinks = feedActual.getPagingLinks();
    assertThat(feed, hasSize(1));
    assertThat(feed.get(0).getStreamId(), is(streamId.toString()));
    assertThat(feed.get(0).getPosition(), is(3L));
    assertThat(feed.get(0).getPayload(), is(notNullValue()));
    assertThat(pagingLinks.getPrevious().get(), is(previousUrl));
    assertThat(pagingLinks.getNext(), 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

ResteasyUriInfo (org.jboss.resteasy.spi.ResteasyUriInfo)24 Test (org.junit.Test)23 URL (java.net.URL)20 UUID (java.util.UUID)14 UUID.randomUUID (java.util.UUID.randomUUID)14 ArrayList (java.util.ArrayList)12 EventEntry (uk.gov.justice.services.eventsourcing.source.api.service.core.EventEntry)10 Position (uk.gov.justice.services.eventsourcing.source.api.service.core.Position)10 EventStreamEntry (uk.gov.justice.services.eventsourcing.source.api.service.core.EventStreamEntry)7 UriInfo (javax.ws.rs.core.UriInfo)6 UtcClock (uk.gov.justice.services.common.util.UtcClock)6 Page (uk.gov.justice.services.eventsourcing.source.api.service.Page)6 Direction (uk.gov.justice.services.eventsourcing.source.api.service.core.Direction)4 EventStreamPageEntry (uk.gov.justice.services.eventsourcing.source.api.service.EventStreamPageEntry)3 JsonObject (javax.json.JsonObject)2 JsonObjectBuilder (javax.json.JsonObjectBuilder)2 ResteasyHttpHeaders (org.jboss.resteasy.specimpl.ResteasyHttpHeaders)2 WebQuery (com.peterphi.std.guice.restclient.jaxb.webquery.WebQuery)1 Swagger (io.swagger.models.Swagger)1 URI (java.net.URI)1