Search in sources :

Example 6 with EventEntry

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

the class EventsPageServiceTest method shouldReturnLinkForPage2IfOnLast.

@Test
public void shouldReturnLinkForPage2IfOnLast() throws Exception {
    final UUID streamId = randomUUID();
    final List<EventEntry> events = new ArrayList<>();
    final EventEntry event1 = new EventEntry(randomUUID(), streamId, 1L, "Test Name1", createObjectBuilder().add("field1", "value1").build(), new UtcClock().now().toString());
    final EventEntry event2 = new EventEntry(randomUUID(), streamId, 2L, "Test Name2", createObjectBuilder().add("field2", "value2").build(), new UtcClock().now().toString());
    events.add(event2);
    events.add(event1);
    when(service.eventExists(streamId, 3L)).thenReturn(true);
    when(positionFactory.createPosition("1")).thenReturn(first());
    when(service.events(streamId, first(), FORWARD, 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 nextUrl = new URL(BASE_URL + EVENT_STREAM_PATH + streamId + "/3/FORWARD/2");
    when(urlLinkFactory.createEventsUrlLink(position(3L), FORWARD, 2, uriInfo)).thenReturn(nextUrl);
    final Page<EventEntry> feed = eventsPageService.pageEvents(streamId, FIRST, FORWARD, 2, uriInfo);
    final PagingLinks pagingLinks = feed.getPagingLinks();
    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) 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 7 with EventEntry

use of uk.gov.justice.services.eventsourcing.source.api.service.core.EventEntry 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));
}
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 8 with EventEntry

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

the class EventPageResourceTest method shouldReturnBadRequestWhenFirstEventsRequestedWithBackwardDirection.

@Test(expected = BadRequestException.class)
public void shouldReturnBadRequestWhenFirstEventsRequestedWithBackwardDirection() throws Exception {
    final String streamId = randomUUID().toString();
    final UriInfo uriInfo = new ResteasyUriInfo("" + "/" + streamId, "", "");
    final URL fixedUrl = new URL("http://localhost:8080/rest/fixed");
    final Page<EventEntry> page = new Page<>(emptyList(), pagingLinksBuilder(fixedUrl, fixedUrl).build());
    when(eventsPageService.pageEvents(UUID.fromString(streamId), FIRST, BACKWARD, 10, uriInfo)).thenReturn(page);
    resource.events(streamId, FIRST, BACKWARD.toString(), 10, uriInfo);
}
Also used : EventEntry(uk.gov.justice.services.eventsourcing.source.api.service.core.EventEntry) ResteasyUriInfo(org.jboss.resteasy.spi.ResteasyUriInfo) Page(uk.gov.justice.services.eventsourcing.source.api.service.Page) ResteasyUriInfo(org.jboss.resteasy.spi.ResteasyUriInfo) UriInfo(javax.ws.rs.core.UriInfo) URL(java.net.URL) Test(org.junit.Test)

Example 9 with EventEntry

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

the class EventPageResourceTest method shouldReturnFeedReturnedByService.

@Test
public void shouldReturnFeedReturnedByService() throws Exception {
    final UUID streamId = randomUUID();
    final String position = "2";
    final String positionValue = "2";
    final int pageSize = 10;
    final UriInfo uriInfo = new ResteasyUriInfo("" + "/" + streamId, "", "");
    final URL fixedUrl = new URL("http://localhost:8080/rest/fixed");
    final Page<EventEntry> page = new Page<>(emptyList(), pagingLinksBuilder(fixedUrl, fixedUrl).build());
    when(eventsPageService.pageEvents(streamId, position, FORWARD, pageSize, uriInfo)).thenReturn(page);
    final JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
    jsonObjectBuilder.add("key", "value");
    when(converter.convert(page)).thenReturn(jsonObjectBuilder.build());
    resource.events(streamId.toString(), positionValue, FORWARD.toString(), pageSize, uriInfo);
    final ArgumentCaptor<UUID> uuidArgumentCaptor = ArgumentCaptor.forClass(UUID.class);
    final ArgumentCaptor<String> positionCaptor = ArgumentCaptor.forClass(String.class);
    final ArgumentCaptor<Integer> pageSizeCaptor = ArgumentCaptor.forClass(Integer.class);
    final ArgumentCaptor<UriInfo> uriInfoCaptor = ArgumentCaptor.forClass(UriInfo.class);
    final ArgumentCaptor<Direction> directionCaptor = ArgumentCaptor.forClass(Direction.class);
    verify(eventsPageService).pageEvents(uuidArgumentCaptor.capture(), positionCaptor.capture(), directionCaptor.capture(), pageSizeCaptor.capture(), uriInfoCaptor.capture());
    assertThat(positionCaptor.getValue(), is(position));
    assertThat(pageSizeCaptor.getValue(), is(pageSize));
    assertThat(uriInfoCaptor.getValue(), is(uriInfo));
    assertThat(directionCaptor.getValue(), is(FORWARD));
    assertThat(uuidArgumentCaptor.getValue(), is(streamId));
}
Also used : ResteasyUriInfo(org.jboss.resteasy.spi.ResteasyUriInfo) Page(uk.gov.justice.services.eventsourcing.source.api.service.Page) Direction(uk.gov.justice.services.eventsourcing.source.api.service.core.Direction) URL(java.net.URL) EventEntry(uk.gov.justice.services.eventsourcing.source.api.service.core.EventEntry) UUID(java.util.UUID) UUID.randomUUID(java.util.UUID.randomUUID) JsonObjectBuilder(javax.json.JsonObjectBuilder) ResteasyUriInfo(org.jboss.resteasy.spi.ResteasyUriInfo) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Example 10 with EventEntry

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

the class EventPageResourceTest method shouldReturnBadRequestWhenHeadEventsRequestedWithForwardDirection.

@Test(expected = BadRequestException.class)
public void shouldReturnBadRequestWhenHeadEventsRequestedWithForwardDirection() throws Exception {
    final String streamId = randomUUID().toString();
    final UriInfo uriInfo = new ResteasyUriInfo("" + "/" + streamId, "", "");
    final URL fixedUrl = new URL("http://localhost:8080/rest/fixed");
    final Page<EventEntry> page = new Page<>(emptyList(), pagingLinksBuilder(fixedUrl, fixedUrl).build());
    when(eventsPageService.pageEvents(UUID.fromString(streamId), HEAD, FORWARD, 10, uriInfo)).thenReturn(page);
    resource.events(streamId, HEAD, FORWARD.toString(), 10, uriInfo);
}
Also used : EventEntry(uk.gov.justice.services.eventsourcing.source.api.service.core.EventEntry) ResteasyUriInfo(org.jboss.resteasy.spi.ResteasyUriInfo) Page(uk.gov.justice.services.eventsourcing.source.api.service.Page) ResteasyUriInfo(org.jboss.resteasy.spi.ResteasyUriInfo) UriInfo(javax.ws.rs.core.UriInfo) URL(java.net.URL) Test(org.junit.Test)

Aggregations

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