Skip to content

Commit

Permalink
- add tests for Route
Browse files Browse the repository at this point in the history
- improve listeners tests
  • Loading branch information
steniobhz committed Oct 16, 2024
1 parent 8feb386 commit 4004646
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,7 @@ private void checkGETStatusCodeWithAwait(final String request, final Integer sta
private void checkGETBodyWithAwait(final String requestUrl, final String body) {
await().atMost(TEN_SECONDS).until(() -> when().get(requestUrl).then().extract().body().asString(), equalTo(body));
}

/**
* Test for hookHandleSearch with listener storage path and valid query param. <br />
* eg. register / unregister: http://localhost:7012/gateleen/server/listenertest/_hooks/listeners/listener/1 <br />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.swisspush.gateleen.TestUtils;

import static io.restassured.RestAssured.*;
import static org.swisspush.gateleen.TestUtils.checkGETStatusCodeWithAwait;

/**
* Test class for the hook route feature.
Expand Down Expand Up @@ -223,5 +224,91 @@ private void assertResponse(final Response response, final String[] expectedArra
Assert.assertEquals(expectedArray.length, array.size());
Assert.assertThat(array, Matchers.contains(expectedArray));
}
/**
* Test for route listing with a valid query parameter.
*/
@Test
public void testRouteListing_ValidQueryParam(TestContext context) {
Async async = context.async();
delete();
initSettings();

String queryParam = "testQuery";
String routePath = "/routes";
String requestUrl = requestUrlBase + routePath + "?q=" + queryParam;

// Register a route
TestUtils.registerRoute(requestUrlBase + routePath, targetUrlBase, new String[]{"GET", "POST"}, null, true, true);

// Send GET request with a valid query param
given().queryParam("q", queryParam)
.when().get(requestUrl)
.then().assertThat().statusCode(200);

// Validate response
checkGETStatusCodeWithAwait(requestUrl, 200);

TestUtils.unregisterRoute(requestUrlBase + routePath);

async.complete();
}

/**
* Test for route listing with a non-matching query parameter.
*/
@Test
public void testRouteListing_NonMatchingQueryParam(TestContext context) {
Async async = context.async();
delete();
initSettings();

String nonMatchingQueryParam = "nonMatchingQuery";
String routePath = "/routes";
String requestUrl = requestUrlBase + routePath + "?q=" + nonMatchingQueryParam;

// Register a route with a different query param
String differentQueryParam = "differentQuery";
TestUtils.registerRoute(requestUrlBase + routePath + "?q=" + differentQueryParam, targetUrlBase, new String[]{"GET", "POST"}, null, true, true);

// Send GET request with non-matching query param
given().queryParam("q", nonMatchingQueryParam)
.when().get(requestUrl)
.then().assertThat().statusCode(200)
.body("routes", Matchers.empty());

// Validate response
checkGETStatusCodeWithAwait(requestUrl, 200);

TestUtils.unregisterRoute(requestUrlBase + routePath);

async.complete();
}

/**
* Test for route listing when no routes are registered.
*/
@Test
public void testRouteListing_NoRoutesRegistered(TestContext context) {
Async async = context.async();
delete();
initSettings();

String queryParam = "someQuery";
String routePath = "/routes";
String requestUrl = requestUrlBase + routePath + "?q=" + queryParam;

// No routes registered

// Send GET request with a query param
given().queryParam("q", queryParam)
.when().get(requestUrl)
.then().assertThat().statusCode(200)
.body("routes", Matchers.empty());

// Validate response
checkGETStatusCodeWithAwait(requestUrl, 200);

async.complete();
}

}

0 comments on commit 4004646

Please sign in to comment.