Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue311 improve hook searching #613

Open
wants to merge 37 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
ae01c87
create a method to search routes and listeners by parameter "q"
steniobhz Sep 30, 2024
9e39c83
Fix null pointer in HookHandler
steniobhz Oct 3, 2024
d9f1c76
Fix tests and implement more validations.
steniobhz Oct 9, 2024
4a0b791
Add integration tests for handleListenerSearch
steniobhz Oct 10, 2024
922708c
Add an integration test at ListenerTest
steniobhz Oct 11, 2024
2f712c0
Fix return method
steniobhz Oct 11, 2024
a0161a2
Added test for hookHandleSearch to verify behavior when no matching l…
steniobhz Oct 11, 2024
322508f
Add tests for hookHandleSearch with no matching listeners and no list…
steniobhz Oct 11, 2024
55ab41b
Implemented a search mechanism that iterates over the provided reposi…
steniobhz Oct 11, 2024
8feb386
Fix error in mock tests
steniobhz Oct 16, 2024
4004646
- add tests for Route
steniobhz Oct 16, 2024
64e6905
Update README_hook.md
steniobhz Oct 17, 2024
6aeec5c
add validation to return 400 instead 404 search parameter != "q" or null
steniobhz Oct 17, 2024
4f7f4ff
add testes to check if parameter is valid
steniobhz Oct 17, 2024
777b906
Fix RouteListingTest and added more validations.
steniobhz Oct 21, 2024
c392df5
Fix README_hook.md
steniobhz Oct 21, 2024
19bc63d
Recreate and fix tests
steniobhz Oct 22, 2024
13b99c2
Fix Url for listeners search
steniobhz Oct 22, 2024
ad49bda
Add test with more listeners to check if search works as expected
steniobhz Oct 22, 2024
8a2a7cf
Improve tests
steniobhz Oct 22, 2024
ac9c720
Improve unit tests
steniobhz Oct 22, 2024
3609204
improve test url as suggested
steniobhz Oct 22, 2024
76adc34
move integration testes to the right project
steniobhz Oct 24, 2024
8096031
improve integration tests
steniobhz Oct 28, 2024
775704d
Improve and optimize the code
steniobhz Oct 28, 2024
97cbb6e
Create new unit tests
steniobhz Oct 28, 2024
db98244
improve and implement new unit testes to cover new hookHandlerSearch
steniobhz Oct 29, 2024
f921c3b
improve the implementation and remove unused methods
steniobhz Oct 30, 2024
0f1c5e7
improve code
steniobhz Oct 31, 2024
8e3d799
Add integration tests to compare results between current routes searc…
steniobhz Nov 4, 2024
4a4fbff
Added validations for route and listener search using parameters
steniobhz Nov 13, 2024
27f561f
Add validation to check if is valid parameters for search request
steniobhz Nov 14, 2024
044ca92
Move specific validations
steniobhz Nov 14, 2024
9956eba
improve code
steniobhz Nov 14, 2024
4fb6453
Fix comments
steniobhz Nov 14, 2024
12f4d78
optimize testes creating reusable functions
steniobhz Nov 14, 2024
d7272ec
Fix testHandleGETRequestWithTrailingSlash
steniobhz Nov 14, 2024
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
5 changes: 1 addition & 4 deletions gateleen-hook/README_hook.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,7 @@ The response will contain the matching listeners. If no match is found, an empty
```json
{
"listeners": [
{
"destination": "/path/to/destination",
"methods": ["GET", "POST"]
}
"first+playground+server+test+nemo+origin+b"
]
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public class HookHandler implements LoggableResource {
public static final String HOOKS_LISTENERS_URI_PART = "/_hooks/listeners/";
public static final String LISTENER_QUEUE_PREFIX = "listener-hook";
private static final String X_QUEUE = "x-queue";
private static final String X_EXPIRE_AFTER = "X-Expire-After";
private static final String LISTENER_HOOK_TARGET_PATH = "listeners/";

public static final String HOOKS_ROUTE_URI_PART = "/_hooks/route";
Expand Down Expand Up @@ -127,7 +126,6 @@ public class HookHandler implements LoggableResource {
private static final String CONTENT_TYPE_JSON = "application/json";
private static final String LISTENERS_KEY = "listeners";
private static final String ROUTES_KEY = "routes";
private static final String DESTINATION_KEY = "destination";
private static final String CONTENT_TYPE_HEADER = "content-type";


Expand Down Expand Up @@ -549,13 +547,12 @@ public void registerListenerRegistrationHandler(Handler<Void> readyHandler) {
public boolean handle(final RoutingContext ctx) {
HttpServerRequest request = ctx.request();
boolean consumed = false;

var requestUri = request.uri();
/*
* 1) Un- / Register Listener / Routes
*/
var requestMethod = request.method();
if (requestMethod == PUT) {
var requestUri = request.uri();
if (requestUri.contains(HOOKS_LISTENERS_URI_PART)) {
handleListenerRegistration(request);
return true;
Expand All @@ -566,7 +563,6 @@ public boolean handle(final RoutingContext ctx) {
}
}
if (requestMethod == DELETE) {
var requestUri = request.uri();
if (requestUri.contains(HOOKS_LISTENERS_URI_PART)) {
handleListenerUnregistration(request);
return true;
Expand All @@ -579,17 +575,22 @@ public boolean handle(final RoutingContext ctx) {

// 1. Check if the request method is GET
if (request.method() == HttpMethod.GET) {
String uri = request.uri();
String queryParam = request.getParam("q");
steniobhz marked this conversation as resolved.
Show resolved Hide resolved
// 2. Check if the URI is for listeners or routes and has a query parameter
if (queryParam != null && !queryParam.isEmpty()) {
if (uri.contains(LISTENERS_KEY)) {
// If the 'q' parameter exists, proceed with search handling
if (queryParam != null) {
// Check if the URI corresponds to listeners or routes
if (requestUri.contains(LISTENERS_KEY)) {
handleListenerSearch(queryParam, request.response());
return true;
} else if (uri.contains(ROUTES_KEY)) {
} else if (requestUri.contains(ROUTES_KEY)) {
handleRouteSearch(queryParam, request.response());
return true;
}
}else{
if (!request.params().isEmpty()) {
request.response().setStatusCode(400).end("Bad Request: Only the 'q' parameter is allowed");
return true;
}
}
}

Expand Down Expand Up @@ -649,16 +650,17 @@ private void handleRouteSearch(String queryParam, HttpServerResponse response) {
* @param response The HTTP response to return the results. Must not be null.
*/
private <T> void handleSearch(Map<String, T> repository, Function<T, String> getDestination, String queryParam, String resultKey, HttpServerResponse response) {
if (repository == null || getDestination == null) {
response.setStatusCode(400).end(); // Bad request for missing parameters

if (repository == null || getDestination == null || resultKey == null || queryParam.isEmpty()) {
response.setStatusCode(400).end("Bad Request: One or more required parameters are missing or null");
return;
}

JsonArray matchingResults = new JsonArray();

repository.forEach((key, value) -> {
String destination = getDestination.apply(value);
if (destination != null && destination.contains(queryParam != null ? queryParam : "")) {
if (destination != null && destination.contains(queryParam)) {
matchingResults.add(key);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -945,8 +945,7 @@ private void checkGETBodyWithAwait(final String requestUrl, final String 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 />
* requestUrl: http://localhost:7012/gateleen/server/listenertest/listener/test?q=testQuery
* requestUrl: http://localhost:7012/playground/server/hooks/v1/registrations/listeners/?q=testQuery
*/
@Test
public void testHookHandleSearch_ListenerPathWithValidQueryParam(TestContext context) {
Expand Down Expand Up @@ -976,8 +975,7 @@ public void testHookHandleSearch_ListenerPathWithValidQueryParam(TestContext con

/**
* Test for hookHandleSearch with listener storage path and valid query param but no match found. <br />
* eg. register / unregister: http://localhost:7012/gateleen/server/listenertest/_hooks/listeners/listener/1 <br />
* requestUrl: http://localhost:7012/gateleen/server/listenertest/listener/test?q=nonMatchingQuery
* requestUrl: http://localhost:7012/playground/server/hooks/v1/registrations/listeners/?q=nonMatchingQuery
*/
@Test
public void testHookHandleSearch_ListenerPathWithNonMatchingQueryParam(TestContext context) {
Expand Down Expand Up @@ -1011,8 +1009,7 @@ public void testHookHandleSearch_ListenerPathWithNonMatchingQueryParam(TestConte

/**
* Test for hookHandleSearch with listener storage path and valid query param but no listeners registered. <br />
* eg. register / unregister: http://localhost:7012/gateleen/server/listenertest/_hooks/listeners/listener/1 <br />
* requestUrl: http://localhost:7012/gateleen/server/listenertest/listener/test?q=someQuery
* requestUrl: http://localhost:7012/playground/server/hooks/v1/registrations/listeners/?q=someQuery
*/
@Test
public void testHookHandleSearch_NoListenersRegistered(TestContext context) {
Expand All @@ -1039,4 +1036,61 @@ public void testHookHandleSearch_NoListenersRegistered(TestContext context) {
async.complete();
}


@Test
public void testHookHandleSearch_ListenerPathInvalidParam(TestContext context) {
Async async = context.async();
delete();
initRoutingRules();

String queryParam = "testQuery";
String listenerPath = "/_hooks/listeners";
steniobhz marked this conversation as resolved.
Show resolved Hide resolved
String requestUrl = requestUrlBase + listenerPath + "?www=" + queryParam;

// Register a listener
TestUtils.registerListener(requestUrlBase + listenerPath, targetUrlBase, new String[]{"GET", "POST"}, null);

// Send GET request
given().queryParam("www", queryParam)
.when().get(requestUrl)
.then().assertThat().statusCode(400);

// Validate the response
checkGETStatusCodeWithAwait(requestUrl, 400);

TestUtils.unregisterListener(requestUrlBase + listenerPath);

async.complete();
}

/**
* Test for hookHandleSearch with listener storage path and no query parameter. <br />
* requestUrl: http://localhost:7012/playground/server/hooks/v1/registrations/listeners/?q=
*/
@Test
public void testHookHandleSearch_NoQueryParameter(TestContext context) {
steniobhz marked this conversation as resolved.
Show resolved Hide resolved
Async async = context.async();
delete();
initRoutingRules();

String queryParam = "";
String listenerPath = "/_hooks/listeners";
String requestUrl = requestUrlBase + listenerPath + "?q=" + queryParam;

// Register a listener
TestUtils.registerListener(requestUrlBase + listenerPath, targetUrlBase, new String[]{"GET", "POST"}, null);

// Send GET request
given().queryParam("q", queryParam)
.when().get(requestUrl)
.then().assertThat().statusCode(400);

// Validate the response
checkGETStatusCodeWithAwait(requestUrl, 400);

TestUtils.unregisterListener(requestUrlBase + listenerPath);

async.complete();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -224,31 +224,35 @@ 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();
delete(); // Remove any pre-existing data
initSettings(); // Initialize routing rules

String queryParam = "testQuery";
String queryParam = "routeTests";
String routePath = "/routes";
steniobhz marked this conversation as resolved.
Show resolved Hide resolved
String requestUrl = requestUrlBase + routePath + "?q=" + queryParam;
String requestUrl = requestUrlBase + routePath;

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

// Send GET request with a valid query param
given().queryParam("q", queryParam)
.when().get(requestUrl)
.then().assertThat().statusCode(200);
// Verify that the route was correctly registered
Response response = given()
.queryParam("q", queryParam)
.when().get(requestUrl + "?q=" + queryParam)
.then().assertThat().statusCode(200)
.extract().response();

// Validate response
checkGETStatusCodeWithAwait(requestUrl, 200);
// Assert that the response contains the expected query param
String responseBody = response.getBody().asString();
Assert.assertTrue(responseBody.contains(queryParam)); // Fails if not found

TestUtils.unregisterRoute(requestUrlBase + routePath);
// Unregister the route
removeRoute(queryParam);

async.complete();
}
Expand All @@ -259,27 +263,29 @@ public void testRouteListing_ValidQueryParam(TestContext context) {
@Test
public void testRouteListing_NonMatchingQueryParam(TestContext context) {
Async async = context.async();
delete();
initSettings();
delete(); // Clean up before the test
initSettings(); // Initialize routing rules

String nonMatchingQueryParam = "nonMatchingQuery";
String queryParam = "other";
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);
String requestUrl = requestUrlBase + routePath;

// Send GET request with non-matching query param
given().queryParam("q", nonMatchingQueryParam)
// 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)
.then().assertThat().statusCode(200)
.body("routes", Matchers.empty());
.extract().response();

// Validate response
checkGETStatusCodeWithAwait(requestUrl, 200);
// Assert the response does not contain the non-matching query param
Assert.assertFalse("Non-matching query param should not be found in response",
response.getBody().asString().contains(nonMatchingQueryParam));

TestUtils.unregisterRoute(requestUrlBase + routePath);
// Unregister the route
removeRoute(queryParam);

async.complete();
}
Expand All @@ -290,8 +296,8 @@ public void testRouteListing_NonMatchingQueryParam(TestContext context) {
@Test
public void testRouteListing_NoRoutesRegistered(TestContext context) {
Async async = context.async();
delete();
initSettings();
delete(); // Ensure there's no previous data
initSettings(); // Initialize routing rules

String queryParam = "someQuery";
String routePath = "/routes";
Expand All @@ -300,15 +306,20 @@ public void testRouteListing_NoRoutesRegistered(TestContext context) {
// No routes registered

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

// Print the body of the response for debugging
System.out.println("Response body: " + response.getBody().asString());
steniobhz marked this conversation as resolved.
Show resolved Hide resolved

// Validate response
checkGETStatusCodeWithAwait(requestUrl, 200);
// Assert that the response body is empty or does not contain routes
Assert.assertTrue("No routes should be registered",
steniobhz marked this conversation as resolved.
Show resolved Hide resolved
response.getBody().asString().contains("routes"));

async.complete();
}


}
Loading