QoSHandler implements LoggableResource { public boolean handle(final HttpServerRequest request) { if (isQoSSettingsUpdate(request)) { handleQoSSettingsUpdate(request); return true; } return qoSHandledRequest(request); } QoSHandler(final Vertx vertx, final ResourceStorage storage, final String qosSettingsPath, final Map<String, Object> properties, String prefix); @Override void enableResourceLogging(boolean resourceLoggingEnabled); boolean handle(final HttpServerRequest request); QoSSentinel getOldSentinel(String sentinelName);  }
@Test public void testQoSSettingsUpdateWithValidConfigurations(TestContext context){ QoSHandler qosHandler = new QoSHandler(vertx, storage, qosSettingsPath, new HashMap<>(), prefix); HttpServerResponse response = mock(HttpServerResponse.class); String validMinimalConfig = "{" + " \"config\":{" + " \"percentile\":75," + " \"quorum\":40," + " \"period\":5," + " \"minSampleCount\" : 1000," + " \"minSentinelCount\" : 5" + " }" + "}"; CustomHttpServerRequest request = new CustomHttpServerRequest(QOS_URI, HttpMethod.PUT, validMinimalConfig, new CaseInsensitiveHeaders(), response); qosHandler.handle(request); verify(response, times(1)).end(); String validFullConfig = "{" + " \"config\":{" + " \"percentile\":75," + " \"quorum\":40," + " \"period\":5," + " \"minSampleCount\" : 1000," + " \"minSentinelCount\" : 5" + " }," + " \"sentinels\":{" + " \"sentinelA\":{" + " \"percentile\":50" + " }," + " \"sentinelB\":{}," + " \"sentinelC\":{ \"minLowestPercentileValueMs\": 0.1}," + " \"sentinelD\":{}" + " }," + " \"rules\":{" + " \"/test/myapi1/v1/.*\":{" + " \"reject\":1.2," + " \"warn\":0.5" + " }," + " \"/test/myapi2/v1/.*\":{" + " \"reject\":0.3" + " }" + " }" + "}"; response = mock(HttpServerResponse.class); request = new CustomHttpServerRequest(QOS_URI, HttpMethod.PUT, validFullConfig, new CaseInsensitiveHeaders(), response); qosHandler.handle(request); verify(response, times(1)).end(); }
@Test public void testQoSSettingsUpdateWithInvalidPercentileValues(TestContext context){ QoSHandler qosHandler = new QoSHandler(vertx, storage, qosSettingsPath, new HashMap<>(), prefix); HttpServerResponse response = spy(new CustomHttpServerResponse(new CaseInsensitiveHeaders())); String percentileNotAnyOfExpectedNumber = "{" + " \"config\":{" + " \"percentile\":11," + " \"quorum\":40," + " \"period\":5," + " \"minSampleCount\" : 1000," + " \"minSentinelCount\" : 5" + " }" + "}"; CustomHttpServerRequest request = new CustomHttpServerRequest(QOS_URI, HttpMethod.PUT, percentileNotAnyOfExpectedNumber, new CaseInsensitiveHeaders(), response); qosHandler.handle(request); verify(response, times(1)).setStatusCode(eq(StatusCode.BAD_REQUEST.getStatusCode())); verify(response, times(1)).setStatusMessage(eq("Bad Request Validation failed")); String percentileNotANumber = "{" + " \"config\":{" + " \"percentile\": \"boom\"," + " \"quorum\":40," + " \"period\":5," + " \"minSampleCount\" : 1000," + " \"minSentinelCount\" : 5" + " }" + "}"; response = spy(new CustomHttpServerResponse(new CaseInsensitiveHeaders())); request = new CustomHttpServerRequest(QOS_URI, HttpMethod.PUT, percentileNotANumber, new CaseInsensitiveHeaders(), response); qosHandler.handle(request); verify(response, times(1)).setStatusCode(eq(StatusCode.BAD_REQUEST.getStatusCode())); verify(response, times(1)).setStatusMessage(eq("Bad Request Validation failed")); }
@Test public void testQoSSettingsUpdateWithRulesButNoSentinels(TestContext context){ QoSHandler qosHandler = new QoSHandler(vertx, storage, qosSettingsPath, new HashMap<>(), prefix); HttpServerResponse response = spy(new CustomHttpServerResponse(new CaseInsensitiveHeaders())); String rulesWithoutSentinelsConfig = "{" + " \"config\":{" + " \"percentile\":75," + " \"quorum\":40," + " \"period\":5," + " \"minSampleCount\" : 1000," + " \"minSentinelCount\" : 5" + " }," + " \"rules\":{" + " \"/test/myapi1/v1/.*\":{" + " \"reject\":1.2," + " \"warn\":0.5" + " }," + " \"/test/myapi2/v1/.*\":{" + " \"reject\":0.3" + " }" + " }" + "}"; CustomHttpServerRequest request = new CustomHttpServerRequest(QOS_URI, HttpMethod.PUT, rulesWithoutSentinelsConfig, new CaseInsensitiveHeaders(), response); qosHandler.handle(request); verify(response, times(1)).setStatusCode(eq(StatusCode.BAD_REQUEST.getStatusCode())); verify(response, times(1)).setStatusMessage(eq("Bad Request ValidationException: QoS settings contain rules without sentinels")); }