Skip to content

Commit

Permalink
improve integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
steniobhz committed Oct 28, 2024
1 parent 76adc34 commit 8096031
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class ListenerTest extends AbstractTest {
private final static int WIREMOCK_PORT = 8881;
private String requestUrlBase;
private String targetUrlBase;
private String searchUrlBase;

@Rule
public WireMockRule wireMockRule = new WireMockRule(WIREMOCK_PORT);
Expand All @@ -56,6 +57,8 @@ public void initRestAssured() {

requestUrlBase = "/tests/gateleen/monitoredresource";
targetUrlBase = "http://localhost:" + MAIN_PORT + SERVER_ROOT + "/tests/gateleen/targetresource";
searchUrlBase = "http://localhost:" + MAIN_PORT + SERVER_ROOT + "/hooks/v1/registrations/listeners";

}

/**
Expand Down Expand Up @@ -956,17 +959,16 @@ public void testHookHandleSearch_ListenerPathWithValidQueryParam(TestContext con
initRoutingRules();

// Settings
String subresource = "validQueryParameter";
String subresource = "matchingQueryParam";
String listenerName = "myListener";

String registerUrlListener1 = requestUrlBase + "/" + subresource + TestUtils.getHookListenersUrlSuffix() + listenerName;
String registerUrlListener1 = requestUrlBase + "/" + subresource + TestUtils.getHookListenersUrlSuffix() + listenerName;
String targetListener1 = targetUrlBase + "/" + listenerName;
String[] methodsListener1 = new String[]{"PUT", "DELETE", "POST"};
final String targetUrlListener1 = targetUrlBase + "/" + listenerName;

final String requestUrl = requestUrlBase + "/" + subresource;

delete(requestUrl);
delete(searchUrlBase);
delete(targetUrlListener1);

//Sending request, one listener hooked
Expand All @@ -976,11 +978,12 @@ public void testHookHandleSearch_ListenerPathWithValidQueryParam(TestContext con
// Verify that the listener was correctly registered
Response response = given()
.queryParam("q", listenerName)
.when().get(registerUrlListener1)
.when().get(searchUrlBase)
.then().assertThat().statusCode(200)
.extract().response();

// Assert that the response contains the expected query param
System.out.println(response.getBody().asString());
Assert.assertTrue(response.getBody().asString().contains(listenerName)); // Fails if not found
TestUtils.unregisterListener(registerUrlListener1);

Expand All @@ -1006,9 +1009,8 @@ public void testHookHandleSearch_ListenerPathWithNonMatchingQueryParam(TestConte
String[] methodsListener1 = new String[]{"PUT", "DELETE", "POST"};
final String targetUrlListener1 = targetUrlBase + "/" + listenerName + "/" + "test";

final String requestUrl = requestUrlBase + "/" + subresource + "/" + "test";

delete(requestUrl);
delete(searchUrlBase);
delete(targetUrlListener1);

TestUtils.registerListener(registerUrlListener1, targetListener1, methodsListener1, null, null,
Expand All @@ -1017,7 +1019,7 @@ public void testHookHandleSearch_ListenerPathWithNonMatchingQueryParam(TestConte
// Verify that the listener was correctly registered
Response response = given()
.queryParam("q", listenerName)
.when().get(registerUrlListener1)
.when().get(searchUrlBase)
.then().assertThat().statusCode(200)
.extract().response();

Expand All @@ -1028,7 +1030,7 @@ public void testHookHandleSearch_ListenerPathWithNonMatchingQueryParam(TestConte
// Verify that the listener search with a no registered listener
response = given()
.queryParam("q", listenerName)
.when().get(registerUrlListener1)
.when().get(searchUrlBase)
.then().assertThat().statusCode(200)
.extract().response();

Expand All @@ -1050,13 +1052,12 @@ public void testHookHandleSearch_NoListenersRegistered(TestContext context) {
initRoutingRules();

String queryParam = "someQuery";
String listenerPath = "/_hooks/listeners";
String requestUrl = requestUrlBase + listenerPath;
String listenerPath = "/hooks/listeners";

// Verify that the listener search with a no registered listener
Response response = given()
.queryParam("q", queryParam)
.when().get(requestUrl)
.when().get(searchUrlBase)
.then().assertThat().statusCode(200)
.extract().response();

Expand All @@ -1074,7 +1075,7 @@ public void testHookHandleSearch_ListenerPathInvalidParam(TestContext context) {

String queryParam = "testQuery";
String listenerPath = "/_hooks/listeners";
String requestUrl = requestUrlBase + listenerPath + "?www=" + queryParam;
String requestUrl = searchUrlBase+ "?www=" + queryParam;

// Validate the response
checkGETStatusCodeWithAwait(requestUrl, 400);
Expand All @@ -1100,9 +1101,7 @@ public void testHookHandleSearch_NoQueryParameter(TestContext context) {
String[] methodsListener1 = new String[]{"PUT", "DELETE", "POST"};
final String targetUrlListener1 = targetUrlBase + "/" + listenerName;

final String requestUrl = requestUrlBase + "/" + subresource;

delete(requestUrl);
delete(searchUrlBase);
delete(targetUrlListener1);

//Sending request, one listener hooked
Expand All @@ -1112,7 +1111,7 @@ public void testHookHandleSearch_NoQueryParameter(TestContext context) {
// Verify that the listener was correctly registered
Response response = given()
.queryParam("q", listenerName)
.when().get(registerUrlListener1)
.when().get(searchUrlBase)
.then().assertThat().statusCode(400)
.extract().response();

Expand Down Expand Up @@ -1159,7 +1158,7 @@ public void testHookHandleSearch_ReturnsTwoOutOfThreeListeners(TestContext conte
// Perform a search for the listeners that should return only listenerOne and listenerTwo
Response response = given()
.queryParam("q", "listener")
.when().get(requestUrlBase + "/" + subresource + TestUtils.getHookListenersUrlSuffix())
.when().get(searchUrlBase)
.then().assertThat().statusCode(200)
.extract().response();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class RouteListingTest extends AbstractTest {
private String requestUrlBase;
private String targetUrlBase;
private String parentKey;

private String searchUrlBase;

/**
* Overwrite RestAssured configuration
Expand All @@ -39,6 +39,7 @@ public void initRestAssured() {
parentKey = "routesource";
requestUrlBase = "/tests/gateleen/" + parentKey;
targetUrlBase = "http://localhost:" + MAIN_PORT + SERVER_ROOT + "/tests/gateleen/routetarget";
searchUrlBase = "http://localhost:" + MAIN_PORT + SERVER_ROOT + "/hooks/v1/registrations/routes";
}


Expand Down Expand Up @@ -236,14 +237,14 @@ public void testRouteListing_ValidQueryParam(TestContext context) {

String queryParam = "routeTests";
String routePath = "/routes";
String requestUrl = requestUrlBase + routePath;


addRoute(queryParam, true, true);

// Verify that the route was correctly registered
Response response = given()
.queryParam("q", queryParam)
.when().get(requestUrl )
.when().get(searchUrlBase )
.then().assertThat().statusCode(200)
.extract().response();

Expand All @@ -268,16 +269,23 @@ public void testRouteListing_NonMatchingQueryParam(TestContext context) {

String nonMatchingQueryParam = "nonMatchingQuery";
String queryParam = "other";
String routePath = "/routes";
String requestUrl = requestUrlBase + routePath;

// Register a route using the addRoute method
addRoute(queryParam, true, true);
assertResponse(get(requestUrlBase), new String[]{queryParam+"/"});

// Send GET request with a non-matching query param
Response response = given().queryParam("q", nonMatchingQueryParam)
.when().get(requestUrl)
Response response = given().queryParam("q", queryParam)
.when().get(searchUrlBase)
.then().assertThat().statusCode(200)
.extract().response();

Assert.assertTrue("Query param should be found in response",
response.getBody().asString().contains(queryParam));

// Send GET request with a non-matching query param
response = given().queryParam("q", nonMatchingQueryParam)
.when().get(searchUrlBase)
.then().assertThat().statusCode(200)
.extract().response();

Expand All @@ -287,7 +295,7 @@ public void testRouteListing_NonMatchingQueryParam(TestContext context) {

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

Expand All @@ -311,14 +319,10 @@ public void testRouteListing_NoRoutesRegistered(TestContext context) {
initSettings(); // Initialize routing rules

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

// No routes registered

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

Expand Down

0 comments on commit 8096031

Please sign in to comment.