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

Merged
merged 39 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
39 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
8c47a2a
#311 some cleanup
mcweba Nov 18, 2024
b88a5f6
#311 some cleanup
mcweba Nov 18, 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
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,18 @@ public boolean handle(final RoutingContext ctx) {
}
}

HttpServerResponse response = ctx.response();
String queryParam = null;

if (request.params() != null) {
queryParam = request.getParam("q");
}

if (queryParam != null && !queryParam.isEmpty()) {
this.handleHookSearch(queryParam, response);
steniobhz marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

/*
* 2) Check if we have to queue a request for listeners
*/
Expand All @@ -592,6 +604,39 @@ public boolean handle(final RoutingContext ctx) {
}
}

/**
* Handles hook search requests based on the 'destination' property.
* Searches in both routes and listeners.
*
* @param queryParam the RoutingContext of the request
*/
public void handleHookSearch(String queryParam,HttpServerResponse response) {
steniobhz marked this conversation as resolved.
Show resolved Hide resolved
steniobhz marked this conversation as resolved.
Show resolved Hide resolved
JsonObject result = new JsonObject();
JsonArray matchingRoutes = new JsonArray();
JsonArray matchingListeners = new JsonArray();

// Search routes by destination
routeRepository.getRoutes().forEach((routeKey, route) -> {
if (route.getHook().getDestination().contains(queryParam)) {
matchingRoutes.add(routeKey);
}
});

// Search listeners by destination
listenerRepository.getListeners().forEach(listener -> {
if (listener.getHook().getDestination().contains(queryParam)) {
matchingListeners.add(listener.getListenerId());
}
});

// Build and send the response
result.put("routes", matchingRoutes);
steniobhz marked this conversation as resolved.
Show resolved Hide resolved
result.put("listeners", matchingListeners);

response.putHeader("content-type", "application/json").end(result.encode());
}


/**
* Create a listing of routes in the given parent. This happens
* only if we have a GET request, the routes are listable and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -31,11 +32,13 @@
import org.swisspush.gateleen.queue.queuing.RequestQueue;
import org.swisspush.gateleen.routing.Router;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.Map;

import static io.vertx.core.http.HttpMethod.PUT;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -641,6 +644,144 @@ public void hookRegistration_RouteWithRouteMultiplier(TestContext testContext) t
vertx.eventBus().request("gateleen.hook-route-remove", "pathToRouterResource");
}

@Test
public void testHookHandleSearchWithMatchingRoutesAndListeners(TestContext context) throws Exception {
// Mock the response
HttpServerResponse response = Mockito.mock(HttpServerResponse.class);
Mockito.when(response.putHeader(anyString(), anyString())).thenReturn(response);

// Mock the request and set the query parameter
HttpServerRequest request = Mockito.mock(HttpServerRequest.class);
Mockito.when(request.response()).thenReturn(response);
Mockito.when(request.getParam("q")).thenReturn("destination");

// Mock the route and listener
Route mockRoute = Mockito.mock(Route.class);
HttpHook mockHook = new HttpHook("destination/matching");
Mockito.when(mockRoute.getHook()).thenReturn(mockHook);

Listener mockListener = new Listener("listener1", "monitoredUrl", "destination/matching", mockHook);

// Mock repositories
ListenerRepository listenerRepository = Mockito.mock(ListenerRepository.class);
RouteRepository routeRepository = Mockito.mock(RouteRepository.class);

// Configure mocked behavior for repositories
Mockito.when(routeRepository.getRoutes()).thenReturn(Map.of("route1", mockRoute));
Mockito.when(listenerRepository.getListeners()).thenReturn(Collections.singletonList(mockListener));

// Create HookHandler instance
HookHandler hookHandler = new HookHandler(vertx, httpClient, storage, loggingResourceManager,
logAppenderRepository, monitoringHandler, "userProfilePath", HOOK_ROOT_URI, requestQueue,
false
);

// Use reflection to set private fields
setPrivateField(hookHandler, "listenerRepository", listenerRepository);
steniobhz marked this conversation as resolved.
Show resolved Hide resolved
setPrivateField(hookHandler, "routeRepository", routeRepository);

// Call the method under test
hookHandler.handleHookSearch("destination", response);

// Capture the output and verify the response was sent correctly
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
Mockito.verify(response).end(captor.capture());

// Check the result
String capturedResult = captor.getValue();
JsonObject result = new JsonObject(capturedResult);
JsonArray routes = result.getJsonArray("routes");
JsonArray listeners = result.getJsonArray("listeners");

// Assert the expected results
context.assertTrue(routes.contains("route1"));
context.assertTrue(listeners.contains("listener1"));

// Verify the content-type header was set correctly
Mockito.verify(response).putHeader("content-type", "application/json");
}


private void setPrivateField(Object target, String fieldName, Object value) throws Exception {
Field field = target.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(target, value);
}

@Test
public void testHookHandleSearchWithNoMatchingRoutesOrListeners(TestContext context) throws Exception {
// Mock the response
HttpServerResponse response = Mockito.mock(HttpServerResponse.class);
Mockito.when(response.putHeader(anyString(), anyString())).thenReturn(response);

// Mock the request and set the query parameter
HttpServerRequest request = Mockito.mock(HttpServerRequest.class);
Mockito.when(request.response()).thenReturn(response);
Mockito.when(request.getParam("q")).thenReturn("destination");

// Mock the route and listener
Route mockRoute = Mockito.mock(Route.class);
HttpHook mockHook = new HttpHook("destination/matching");
Mockito.when(mockRoute.getHook()).thenReturn(mockHook);

Listener mockListener = new Listener("listener1", "monitoredUrl", "destination/matching", mockHook);

// Mock repositories
ListenerRepository listenerRepository = Mockito.mock(ListenerRepository.class);
RouteRepository routeRepository = Mockito.mock(RouteRepository.class);

// Configure mocked behavior for repositories with no matching routes or listeners
Mockito.when(routeRepository.getRoutes()).thenReturn(Map.of());
Mockito.when(listenerRepository.getListeners()).thenReturn(Collections.emptyList());

// Create HookHandler instance
HookHandler hookHandler = new HookHandler(vertx, httpClient, storage, loggingResourceManager,
logAppenderRepository, monitoringHandler, "userProfilePath", HOOK_ROOT_URI, requestQueue,
false
);

// Use reflection to set private fields
setPrivateField(hookHandler, "listenerRepository", listenerRepository);
setPrivateField(hookHandler, "routeRepository", routeRepository);

// Call the method under test
hookHandler.handleHookSearch("destination", response);

// Capture the output and verify the response was sent correctly
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
Mockito.verify(response).end(captor.capture());

// Check the result
String capturedResult = captor.getValue();
JsonObject result = new JsonObject(capturedResult);
JsonArray routes = result.getJsonArray("routes");
JsonArray listeners = result.getJsonArray("listeners");

// Assert that there are no matching routes or listeners
context.assertTrue(routes.isEmpty());
context.assertTrue(listeners.isEmpty());

// Verify the content-type header was set correctly
Mockito.verify(response).putHeader("content-type", "application/json");
}

@Test
public void testHookHandleSearchWithInvalidQueryParam(TestContext context) {
// Mocking the HttpServerResponse and request objects
HttpServerResponse response = Mockito.mock(HttpServerResponse.class);
Mockito.when(response.putHeader(anyString(), anyString())).thenReturn(response);

HttpServerRequest request = Mockito.mock(HttpServerRequest.class);
Mockito.when(request.response()).thenReturn(response);
Mockito.when(request.getParam("q")).thenReturn(null);

// Call hookHandleSearch
hookHandler.handleHookSearch(null, response);

// Verify that nothing is returned
Mockito.verify(response, Mockito.never()).end(any(Buffer.class));
steniobhz marked this conversation as resolved.
Show resolved Hide resolved
}


///////////////////////////////////////////////////////////////////////////////
// Helpers
Expand Down
Loading