Skip to content

Commit

Permalink
Fix tests and implement more validations.
Browse files Browse the repository at this point in the history
Add new tests for HookHandler
Create testes with storage
  • Loading branch information
steniobhz committed Oct 9, 2024
1 parent 9e39c83 commit d9f1c76
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -123,6 +124,13 @@ public class HookHandler implements LoggableResource {
public static final String LISTABLE = "listable";
public static final String COLLECTION = "collection";

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";


private final Comparator<String> collectionContentComparator;
private static final Logger log = LoggerFactory.getLogger(HookHandler.class);

Expand Down Expand Up @@ -569,16 +577,24 @@ public boolean handle(final RoutingContext ctx) {
}
}

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

if (request.params() != null) {
queryParam = request.getParam("q");
}
// 1. Check if the request method is GET
if (request.method() == HttpMethod.GET) {
String uri = request.uri();
String queryParam = request.getParam("q");

if (queryParam != null && !queryParam.isEmpty()) {
this.handleHookSearch(queryParam, response);
return true;
// 2. Check if the URI is for listeners or routes and has a query parameter
if (queryParam != null && !queryParam.isEmpty()) {
if (uri.contains(HOOK_LISTENER_STORAGE_PATH)) {
handleListenerSearch(queryParam, request.response());
return true;
} else if (uri.contains(HOOK_ROUTE_STORAGE_PATH)) {
handleRouteSearch(queryParam, request.response());
return true;
}
}
else {
return false;
}
}

/*
Expand All @@ -604,38 +620,45 @@ 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) {
JsonObject result = new JsonObject();
JsonArray matchingRoutes = new JsonArray();
JsonArray matchingListeners = new JsonArray();
private void handleListenerSearch(String queryParam, HttpServerResponse response) {
handleSearch(
listenerRepository.getListeners().stream().collect(Collectors.toMap(Listener::getListenerId, listener -> listener)),
listener -> listener.getHook().getDestination(),
queryParam,
LISTENERS_KEY,
response
);
}

// Search routes by destination
routeRepository.getRoutes().forEach((routeKey, route) -> {
if (route.getHook().getDestination().contains(queryParam)) {
matchingRoutes.add(routeKey);
}
});
private void handleRouteSearch(String queryParam, HttpServerResponse response) {
handleSearch(
routeRepository.getRoutes(),
route -> route.getHook().getDestination(),
queryParam,
ROUTES_KEY,
response
);
}

private <T> void handleSearch(Map<String, T> repository, Function<T, String> getDestination, String queryParam, String resultKey, HttpServerResponse response) {
JsonArray matchingResults = new JsonArray();

// Search listeners by destination
listenerRepository.getListeners().forEach(listener -> {
if (listener.getHook().getDestination().contains(queryParam)) {
matchingListeners.add(listener.getListenerId());
repository.forEach((key, value) -> {
String destination = getDestination.apply(value);
if (destination != null && destination.contains(queryParam)) {
matchingResults.add(key);
}
});

// Build and send the response
result.put("routes", matchingRoutes);
result.put("listeners", matchingListeners);
JsonObject result = new JsonObject();
result.put(resultKey, matchingResults);

response.putHeader("content-type", "application/json").end(result.encode());
}
// Set headers safely before writing the response
response.putHeader(CONTENT_TYPE_HEADER, CONTENT_TYPE_JSON);
response.write(result.encode());
response.end();

}

/**
* Create a listing of routes in the given parent. This happens
Expand Down
Loading

0 comments on commit d9f1c76

Please sign in to comment.