Skip to content

Commit

Permalink
#311 some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mcweba committed Nov 18, 2024
1 parent d7272ec commit 8c47a2a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.eventbus.Message;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpClientResponse;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.http.*;
import io.vertx.core.http.impl.headers.HeadersMultiMap;
import io.vertx.core.json.DecodeException;
import io.vertx.core.json.JsonArray;
Expand Down Expand Up @@ -120,8 +115,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 CONTENT_TYPE_HEADER = "content-type";


private final Comparator<String> collectionContentComparator;
private static final Logger log = LoggerFactory.getLogger(HookHandler.class);
Expand Down Expand Up @@ -544,7 +537,7 @@ public void registerListenerRegistrationHandler(Handler<Void> readyHandler) {
public boolean handle(final RoutingContext ctx) {
HttpServerRequest request = ctx.request();
boolean consumed = false;
var requestUri = request.uri().replaceAll("/+$", "");
var requestUri = request.uri();
/*
* 1) Un- / Register Listener / Routes
*/
Expand Down Expand Up @@ -614,7 +607,7 @@ private void handleListenerSearch(HttpServerRequest request) {

private void handleRouteSearch(HttpServerRequest request) {
handleSearch(
routeRepository.getRoutes().entrySet().stream().collect(Collectors.toMap(entry -> entry.getValue().getHookIdentify(), Map.Entry::getValue)),
routeRepository.getRoutes().entrySet().stream().collect(Collectors.toMap(entry -> entry.getValue().getHookDisplayText(), Map.Entry::getValue)),
route -> route.getHook().getDestination(),
ROUTES_KEY,
request
Expand All @@ -634,8 +627,9 @@ private void handleRouteSearch(HttpServerRequest request) {
private <T> void handleSearch(Map<String, T> repository, Function<T, String> getDestination, String resultKey, HttpServerRequest request) {
String queryParam = request.getParam("q");
if (request.params().size() > 1 || StringUtils.isEmpty(queryParam)) {
request.response().setStatusCode(StatusCode.BAD_REQUEST.getStatusCode())
.end("Bad Request: Only the 'q' parameter is allowed and can't be empty or null");
request.response().setStatusCode(StatusCode.BAD_REQUEST.getStatusCode());
request.response().setStatusMessage(StatusCode.BAD_REQUEST.getStatusMessage());
request.response().end("Only the 'q' parameter is allowed and can't be empty or null");
return ;
}

Expand All @@ -652,12 +646,10 @@ private <T> void handleSearch(Map<String, T> repository, Function<T, String> get

String encodedResult = result.encode();

request.response().putHeader(CONTENT_TYPE_HEADER, CONTENT_TYPE_JSON);
request.response().putHeader(HttpHeaders.CONTENT_TYPE, CONTENT_TYPE_JSON);
request.response().end(encodedResult);
}



/**
* 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 Expand Up @@ -1669,13 +1661,12 @@ private void registerRoute(Buffer buffer) {

boolean mustCreateNewRoute = true;


Route existingRoute = routeRepository.getRoutes().get(routedUrl);
if (existingRoute != null) {
mustCreateNewRoute = mustCreateNewRouteForHook(existingRoute, hook);
}
if (mustCreateNewRoute) {
routeRepository.addRoute(routedUrl, createRoute(routedUrl , hook, requestUrl));
routeRepository.addRoute(routedUrl, createRoute(routedUrl, hook, requestUrl));
} else {
// see comment in #mustCreateNewRouteForHook()
existingRoute.getRule().setHeaderFunction(hook.getHeaderFunction());
Expand Down Expand Up @@ -1726,11 +1717,12 @@ private boolean headersFilterPatternEquals(Pattern headersFilterPatternLeft, Pat
*
* @param urlPattern urlPattern
* @param hook hook
* @param hookDisplayText text used for display only like in API
* @return Route
*/
private Route createRoute(String urlPattern, HttpHook hook, String hookIdentify) {
private Route createRoute(String urlPattern, HttpHook hook, String hookDisplayText) {
return new Route(vertx, userProfileStorage, loggingResourceManager, logAppenderRepository, monitoringHandler,
userProfilePath, hook, urlPattern, selfClient, hookIdentify);
userProfilePath, hook, urlPattern, selfClient, hookDisplayText);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class Route {
private MonitoringHandler monitoringHandler;
private String userProfilePath;
private ResourceStorage storage;
private String hookIdentify;
private String hookDisplayText;

private String urlPattern;
private HttpHook httpHook;
Expand Down Expand Up @@ -79,8 +79,9 @@ public class Route {
* @param httpHook httpHook
* @param urlPattern - this can be a listener or a normal urlPattern (eg. for a route)
*/
public Route(Vertx vertx, ResourceStorage storage, LoggingResourceManager loggingResourceManager, LogAppenderRepository logAppenderRepository,
MonitoringHandler monitoringHandler, String userProfilePath, HttpHook httpHook, String urlPattern, HttpClient selfClient, String hookIdentify) {
public Route(Vertx vertx, ResourceStorage storage, LoggingResourceManager loggingResourceManager,
LogAppenderRepository logAppenderRepository, MonitoringHandler monitoringHandler, String userProfilePath,
HttpHook httpHook, String urlPattern, HttpClient selfClient, String hookDisplayText) {
this.vertx = vertx;
this.storage = storage;
this.loggingResourceManager = loggingResourceManager;
Expand All @@ -90,7 +91,7 @@ public Route(Vertx vertx, ResourceStorage storage, LoggingResourceManager loggin
this.httpHook = httpHook;
this.urlPattern = urlPattern;
this.selfClient = selfClient;
this.hookIdentify = hookIdentify;
this.hookDisplayText = hookDisplayText;

createRule();

Expand Down Expand Up @@ -276,7 +277,7 @@ public HttpHook getHook() {
return httpHook;
}

public String getHookIdentify() {
return hookIdentify;
public String getHookDisplayText() {
return hookDisplayText;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ public void testHandleGETRequestWithEmptyParam(TestContext testContext) {
String jsonResponse = responseCaptor.getValue();
testContext.assertNotNull(jsonResponse);
// Confirm the response contains "Bad Request"
testContext.assertTrue(jsonResponse.contains("Bad Request"));
testContext.assertTrue(jsonResponse.contains("Only the 'q' parameter is allowed and can't be empty or null"));
}

@Test
Expand Down Expand Up @@ -720,7 +720,6 @@ public void testHandleGETRequestWithListenersSearchSingleResult() throws Interru
GETRequest request = new GETRequest(HOOK_LISTENER_URI, mockResponse);
request.addParameter("q", singleListener);


setListenerStorageEntryAndTriggerUpdate(buildListenerConfigWithHeadersFilter(null, singleListener, "x-foo: (A|B)"));
// wait a moment to let the listener be registered
Thread.sleep(200);
Expand Down Expand Up @@ -776,7 +775,6 @@ public void testHandleGETRequestWithListenersSearchMultipleResults() throws Inte
assertFalse(jsonResponse.contains(notMatchListener));
}


@Test
public void testHandleGETRequestWithRoutesSearchEmptyResult() {
// Define URI and configure request with specific 'q' parameter for routes search
Expand Down Expand Up @@ -861,7 +859,6 @@ public void testHandleListenerWithStorageAndEmptyList() {
// Validate the response JSON for an empty listener list
String actualResponse = responseCaptor.getValue();
assertEmptyResult(actualResponse);

}

@Test
Expand Down Expand Up @@ -890,13 +887,13 @@ public void testHandleGETRequestWithExtraParam(TestContext testContext) {
String jsonResponse = responseCaptor.getValue();
testContext.assertNotNull(jsonResponse);
// Confirm that the response contains "Bad Request"
testContext.assertTrue(jsonResponse.contains("Bad Request"));
testContext.assertTrue(jsonResponse.contains("Only the 'q' parameter is allowed and can't be empty or null"));
}

@Test
public void testHandleGETRequestWithTrailingSlash(TestContext testContext) {
// Define URI with trailing slash and configure the request
GETRequest request = new GETRequest(HOOK_LISTENER_URI+"/", mockResponse);
GETRequest request = new GETRequest(HOOK_LISTENER_URI + "/", mockResponse);
request.addParameter("q", "validQueryParam");

// Mock the RoutingContext
Expand Down Expand Up @@ -940,7 +937,7 @@ public void testHandleGETRequestWithInvalidParam(TestContext testContext) {
String jsonResponse = responseCaptor.getValue();
testContext.assertNotNull(jsonResponse);
// Confirm that the response contains "Bad Request"
testContext.assertTrue(jsonResponse.contains("Bad Request"));
testContext.assertTrue(jsonResponse.contains("Only the 'q' parameter is allowed and can't be empty or null"));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ public void initRestAssured() {
defaultRegisterUrlListener = requestUrlBase + "/" + defaultQueryParam + TestUtils.getHookListenersUrlSuffix() + defaultListenerName;
defaultTargetListener = targetUrlBase + "/" + defaultListenerName;
defaultMethodsListener = new String[]{"PUT", "DELETE", "POST"};


}

/**
Expand Down Expand Up @@ -1172,7 +1170,7 @@ private void checkGETBodyWithAwait(final String requestUrl, final String body) {
private Response searchWithQueryParam(String searchParam, String queryParam, int expectedStatusCode ) {
return given()
.queryParam(searchParam, queryParam)
.when().get(searchUrlBase )
.when().get(searchUrlBase)
.then().assertThat().statusCode(expectedStatusCode)
.extract().response();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,10 @@ private void assertResponse(final Response response, final String[] expectedArra
Assert.assertThat(array, Matchers.contains(expectedArray));
}

private Response searchWithQueryParam(String searchParam, String queryParam, int expectedStatusCode ) {
private Response searchWithQueryParam(String queryParamName, String queryParamValue, int expectedStatusCode ) {
return given()
.queryParam(searchParam, queryParam)
.when().get(searchUrlBase )
.queryParam(queryParamName, queryParamValue)
.when().get(searchUrlBase)
.then().assertThat().statusCode(expectedStatusCode)
.extract().response();
}
Expand Down

0 comments on commit 8c47a2a

Please sign in to comment.