Skip to content

Commit

Permalink
add test for whole sitemap subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
TAKeanice committed Nov 19, 2023
1 parent 5f89d3b commit 1a28715
Showing 1 changed file with 41 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,27 @@ public void whenSitemapsAreProvidedShouldReturnSitemapBeans() {
assertThat(dto.link, is(SITEMAP_PATH + "/" + SITEMAP_MODEL_NAME));
}

@Test
public void whenLongPollingWholeSitemapShouldObserveAllItems() {
ItemEvent itemEvent = mock(ItemEvent.class);
when(itemEvent.getItemName()).thenReturn(item.getName());
executeWithDelay(() -> sitemapResource.receive(itemEvent));

// non-null is sufficient here.
when(headersMock.getRequestHeader(HTTP_HEADER_X_ATMOSPHERE_TRANSPORT)).thenReturn(List.of());

Response response = sitemapResource.getPageData(headersMock, null, SITEMAP_MODEL_NAME, null, false);

SitemapDTO sitemapDTO = (SitemapDTO) response.getEntity();
// assert that the item state change did trigger the blocking method to return
assertThat(sitemapDTO.timeout, is(false));
}

@Test
public void whenLongPollingShouldObserveItems() {
ItemEvent itemEvent = mock(ItemEvent.class);
when(itemEvent.getItemName()).thenReturn(item.getName());
new Thread(() -> {
try {
Thread.sleep(STATE_UPDATE_WAIT_TIME); // wait for the #getPageData call and listeners to attach to the
// item
sitemapResource.receive(itemEvent);
} catch (InterruptedException e) {
}
}).start();
executeWithDelay(() -> sitemapResource.receive(itemEvent));

// non-null is sufficient here.
when(headersMock.getRequestHeader(HTTP_HEADER_X_ATMOSPHERE_TRANSPORT)).thenReturn(List.of());
Expand All @@ -187,22 +196,15 @@ public void whenLongPollingShouldObserveItems() {
false);

PageDTO pageDTO = (PageDTO) response.getEntity();
assertThat(pageDTO.timeout, is(false)); // assert that the item state change did trigger the blocking method to
// return
// assert that the item state change did trigger the blocking method to return
assertThat(pageDTO.timeout, is(false));
}

@Test
public void whenLongPollingShouldObserveItemsFromVisibilityRules() {
ItemEvent itemEvent = mock(ItemEvent.class);
when(itemEvent.getItemName()).thenReturn(visibilityRuleItem.getName());
new Thread(() -> {
try {
Thread.sleep(STATE_UPDATE_WAIT_TIME); // wait for the #getPageData call and listeners to attach to the
// item
sitemapResource.receive(itemEvent);
} catch (InterruptedException e) {
}
}).start();
executeWithDelay(() -> sitemapResource.receive(itemEvent));

// non-null is sufficient here.
when(headersMock.getRequestHeader(HTTP_HEADER_X_ATMOSPHERE_TRANSPORT)).thenReturn(List.of());
Expand All @@ -211,22 +213,15 @@ public void whenLongPollingShouldObserveItemsFromVisibilityRules() {
false);

PageDTO pageDTO = (PageDTO) response.getEntity();
assertThat(pageDTO.timeout, is(false)); // assert that the item state change did trigger the blocking method to
// return
// assert that the item state change did trigger the blocking method to return
assertThat(pageDTO.timeout, is(false));
}

@Test
public void whenLongPollingShouldObserveItemsFromLabelColorConditions() {
ItemEvent itemEvent = mock(ItemEvent.class);
when(itemEvent.getItemName()).thenReturn(labelColorItem.getName());
new Thread(() -> {
try {
Thread.sleep(STATE_UPDATE_WAIT_TIME); // wait for the #getPageData call and listeners to attach to the
// item
sitemapResource.receive(itemEvent);
} catch (InterruptedException e) {
}
}).start();
executeWithDelay(() -> sitemapResource.receive(itemEvent));

// non-null is sufficient here.
when(headersMock.getRequestHeader(HTTP_HEADER_X_ATMOSPHERE_TRANSPORT)).thenReturn(List.of());
Expand All @@ -235,22 +230,15 @@ public void whenLongPollingShouldObserveItemsFromLabelColorConditions() {
false);

PageDTO pageDTO = (PageDTO) response.getEntity();
assertThat(pageDTO.timeout, is(false)); // assert that the item state change did trigger the blocking method to
// return
// assert that the item state change did trigger the blocking method to return
assertThat(pageDTO.timeout, is(false));
}

@Test
public void whenLongPollingShouldObserveItemsFromValueColorConditions() {
ItemEvent itemEvent = mock(ItemEvent.class);
when(itemEvent.getItemName()).thenReturn(valueColorItem.getName());
new Thread(() -> {
try {
Thread.sleep(STATE_UPDATE_WAIT_TIME); // wait for the #getPageData call and listeners to attach to the
// item
sitemapResource.receive(itemEvent);
} catch (InterruptedException e) {
}
}).start();
executeWithDelay(() -> sitemapResource.receive(itemEvent));

// non-null is sufficient here.
when(headersMock.getRequestHeader(HTTP_HEADER_X_ATMOSPHERE_TRANSPORT)).thenReturn(List.of());
Expand All @@ -267,14 +255,7 @@ public void whenLongPollingShouldObserveItemsFromValueColorConditions() {
public void whenLongPollingShouldObserveItemsFromIconColorConditions() {
ItemEvent itemEvent = mock(ItemEvent.class);
when(itemEvent.getItemName()).thenReturn(iconColorItem.getName());
new Thread(() -> {
try {
Thread.sleep(STATE_UPDATE_WAIT_TIME); // wait for the #getPageData call and listeners to attach to the
// item
sitemapResource.receive(itemEvent);
} catch (InterruptedException e) {
}
}).start();
executeWithDelay(() -> sitemapResource.receive(itemEvent));

// non-null is sufficient here.
when(headersMock.getRequestHeader(HTTP_HEADER_X_ATMOSPHERE_TRANSPORT)).thenReturn(List.of());
Expand All @@ -291,14 +272,7 @@ public void whenLongPollingShouldObserveItemsFromIconColorConditions() {
public void whenLongPollingShouldObserveItemsFromIconConditions() {
ItemEvent itemEvent = mock(ItemEvent.class);
when(itemEvent.getItemName()).thenReturn(iconItem.getName());
new Thread(() -> {
try {
Thread.sleep(STATE_UPDATE_WAIT_TIME); // wait for the #getPageData call and listeners to attach to the
// item
sitemapResource.receive(itemEvent);
} catch (InterruptedException e) {
}
}).start();
executeWithDelay(() -> sitemapResource.receive(itemEvent));

// non-null is sufficient here.
when(headersMock.getRequestHeader(HTTP_HEADER_X_ATMOSPHERE_TRANSPORT)).thenReturn(List.of());
Expand All @@ -307,8 +281,19 @@ public void whenLongPollingShouldObserveItemsFromIconConditions() {
false);

PageDTO pageDTO = (PageDTO) response.getEntity();
assertThat(pageDTO.timeout, is(false)); // assert that the item state change did trigger the blocking method to
// return
// assert that the item state change did trigger the blocking method to return
assertThat(pageDTO.timeout, is(false));
}

private static void executeWithDelay(Runnable executionWithDelay) {
new Thread(() -> {
try {
// wait for the #getPageData call and listeners to attach to the item
Thread.sleep(STATE_UPDATE_WAIT_TIME);
executionWithDelay.run();
} catch (InterruptedException e) {
}
}).start();
}

@Test
Expand Down

0 comments on commit 1a28715

Please sign in to comment.