Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions cwms-data-api/src/test/java/cwms/cda/api/rss/RssHandlerIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static cwms.cda.api.Controllers.PAGE_SIZE;
import static cwms.cda.security.ApiKeyIdentityProvider.AUTH_HEADER;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.either;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -49,6 +50,8 @@
import java.math.BigInteger;
import java.net.URI;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletResponse;
import org.jooq.Configuration;
import org.jooq.impl.DSL;
Expand Down Expand Up @@ -82,10 +85,12 @@ void setup() throws Exception {
@Test
void test_rss_feed_with_pagination() {
// Page 1: verify core RSS elements + 5 items + next link exists
int pagesVisited = 0;
ExtractableResponse<Response> page1 = given()
.log().ifValidationFails(LogDetail.ALL, true)
.accept(Formats.RSS)
.header(AUTH_HEADER, user.toHeaderValue())
.header("page", pagesVisited)
.queryParam(PAGE_SIZE, 5)
.when()
.redirects().follow(true)
Expand All @@ -96,7 +101,7 @@ void test_rss_feed_with_pagination() {
.assertThat()
.statusCode(is(HttpServletResponse.SC_OK))
.extract();

pagesVisited++;
String xmlBody = page1.asString();
XmlPath xml = rssXml(xmlBody);

Expand All @@ -114,7 +119,7 @@ void test_rss_feed_with_pagination() {
assertNotNull(nextHref, "Expected an atom:link rel=\"next\" on the first page");

// Walk pages via nextLine
int pagesVisited = 1;

int maxPages = 500;
while (nextHref != null && pagesVisited < maxPages) {
String nextPath = toPathAndQuery(nextHref);
Expand All @@ -123,30 +128,33 @@ void test_rss_feed_with_pagination() {
.log().ifValidationFails(LogDetail.ALL, true)
.accept(Formats.RSS)
.header(AUTH_HEADER, user.toHeaderValue())
.header("page", pagesVisited)
.when()
.redirects().follow(true)
.redirects().max(3)
.get(nextPath)
.then()
.log().ifValidationFails(LogDetail.ALL, true)
.assertThat()
.statusCode(is(HttpServletResponse.SC_OK))
.statusCode(either(is(HttpServletResponse.SC_OK)).or(is(429)))
.extract();

XmlPath nextXml = rssXml(nextPage.asString());

// Still a valid RSS document with items
assertNotNull(nextXml.getString("rss.channel.title"));
assertNotNull(nextXml.getList("rss.channel.item"));

pagesVisited++;
nextHref = nextLinkHref(nextXml);
if (nextPage.statusCode() == HttpServletResponse.SC_OK) {
XmlPath nextXml = rssXml(nextPage.asString());
// Still a valid RSS document with items
assertNotNull(nextXml.getString("rss.channel.title"));
assertNotNull(nextXml.getList("rss.channel.item"));
pagesVisited++;
nextHref = nextLinkHref(nextXml);
}
String waitStr = nextPage.header("Retry-After");
int wait = waitStr != null && !waitStr.isEmpty() ? Integer.parseInt(waitStr) : 10;
try {
Thread.sleep(wait*1000 + 500 /* extra half second just to avoid the best being brittle */);
} catch (InterruptedException ex) {
LOGGER.atFine().withCause(ex).log("Next query wait was interrupted.");
if (waitStr != null) {
int wait = waitStr != null && !waitStr.isEmpty() ? Integer.parseInt(waitStr) : 10;
try {
TimeUnit.SECONDS.sleep(wait); // NOSONAR - have to wait, 429 response
} catch (InterruptedException ex) {
LOGGER.atFine().withCause(ex).log("Next query wait was interrupted.");
}
}
}

Expand Down
Loading