LoggingResourceManager implements LoggableResource { public boolean handleLoggingResource(final HttpServerRequest request) { if (request.uri().equals(loggingUri) && HttpMethod.PUT == request.method()) { request.bodyHandler(loggingResourceBuffer -> { try { extractLoggingFilterValues(loggingResourceBuffer); } catch (ValidationException validationException) { log.error("Could not parse logging resource: " + validationException.toString()); ResponseStatusCodeLogUtil.info(request, StatusCode.BAD_REQUEST, LoggingResourceManager.class); request.response().setStatusCode(StatusCode.BAD_REQUEST.getStatusCode()); request.response().setStatusMessage(StatusCode.BAD_REQUEST.getStatusMessage() + " " + validationException.getMessage()); if(validationException.getValidationDetails() != null){ request.response().headers().add("content-type", "application/json"); request.response().end(validationException.getValidationDetails().encode()); } else { request.response().end(validationException.getMessage()); } return; } storage.put(loggingUri, loggingResourceBuffer, status -> { if (status == StatusCode.OK.getStatusCode()) { if(logConfigurationResourceChanges){ RequestLogger.logRequest(vertx.eventBus(), request, status, loggingResourceBuffer); } vertx.eventBus().publish(UPDATE_ADDRESS, true); } else { request.response().setStatusCode(status); } ResponseStatusCodeLogUtil.info(request, StatusCode.fromCode(status), LoggingResourceManager.class); request.response().end(); }); }); return true; } if (request.uri().equals(loggingUri) && HttpMethod.DELETE == request.method()) { getLoggingResource().reset(); log.info("Reset LoggingResource"); } return false; } LoggingResourceManager(Vertx vertx, final ResourceStorage storage, String loggingUri); LoggingResource getLoggingResource(); @Override void enableResourceLogging(boolean resourceLoggingEnabled); boolean handleLoggingResource(final HttpServerRequest request);  }
@Test public void testValidContentShouldBeStoredInStorage(TestContext context){ Async async = context.async(); LoggingResourceManager manager = new LoggingResourceManager(vertx, storage, LOGGING_URI); final DummyHttpServerResponse response = new DummyHttpServerResponse(); class UpdateLoggingResourceWithValidDataRequest extends LoggingResourcePUTRequest { @Override public HttpServerRequest bodyHandler(Handler<Buffer> bodyHandler) { bodyHandler.handle(Buffer.buffer(VALID_LOGGING_RESOURCE)); return this; } @Override public HttpServerResponse response() { return response; } } storage.get(LOGGING_URI, result -> { context.assertEquals(INITIAL_LOGGING_RESOURCE, result.toString()); context.assertTrue(manager.handleLoggingResource(new UpdateLoggingResourceWithValidDataRequest())); storage.get(LOGGING_URI, updatedResult -> { context.assertEquals(VALID_LOGGING_RESOURCE, updatedResult.toString()); async.complete(); }); }); }
@Test public void testInvalidContentShouldNotBeStoredInStorage(TestContext context){ Async async = context.async(); LoggingResourceManager manager = new LoggingResourceManager(vertx, storage, LOGGING_URI); final DummyHttpServerResponse response = new DummyHttpServerResponse(); class UpdateLoggingResourceWithValidDataRequest extends LoggingResourcePUTRequest { @Override public HttpServerRequest bodyHandler(Handler<Buffer> bodyHandler) { bodyHandler.handle(Buffer.buffer(INVALID_TYPE_LOGGING_RESOURCE)); return this; } @Override public HttpServerResponse response() { return response; } } storage.get(LOGGING_URI, result -> { context.assertEquals(INITIAL_LOGGING_RESOURCE, result.toString()); context.assertTrue(manager.handleLoggingResource(new UpdateLoggingResourceWithValidDataRequest())); storage.get(LOGGING_URI, updatedResult -> { context.assertEquals(INITIAL_LOGGING_RESOURCE, updatedResult.toString(), "The logging resource in the storage should still be equal to the initial resource"); context.assertEquals(StatusCode.BAD_REQUEST.getStatusCode(), response.getStatusCode()); async.complete(); }); }); }
@Test public void invalidRegexGetsRejected( TestContext testContext ) { final String loggingUrl = "/houston/server/admin/v1/logging"; final LoggingResourceManager loggingResourceManager = new LoggingResourceManager( vertx , storage , loggingUrl ); final Integer[] responseStatusCode = new Integer[]{ null }; final DummyHttpServerResponse httpServerResponse = new DummyHttpServerResponse(){ @Override public synchronized HttpServerResponse setStatusCode( int statusCode ) { if( responseStatusCode[0] != null ) logger.debug("Status code "+responseStatusCode[0]+" got overridden with "+statusCode+"."); responseStatusCode[0] = statusCode; return this; } }; final HttpServerRequest request = new DummyHttpServerRequest(){ @Override public String uri() { return loggingUrl; } @Override public HttpMethod method() { return HttpMethod.PUT; } @Override public HttpServerRequest bodyHandler(Handler<Buffer> bodyHandler) { Buffer buffer = new BufferImpl(); buffer.setBytes( 0 , ("{\n" + " \"payload\": {\n" + " \"filters\": [\n" + " {\n" + " \"url\": \"/houston/services/.*(?<!(all/position)$\",\n" + " \"method\": \"PUT|DELETE\"\n" + " }\n" + " ]\n" + " }\n" + "}\n").getBytes(StandardCharsets.UTF_8)); bodyHandler.handle( buffer ); return null; } @Override public MultiMap headers() { return new CaseInsensitiveHeaders(); } @Override public HttpServerResponse response() { return httpServerResponse; } }; final boolean returnValue = loggingResourceManager.handleLoggingResource( request ); testContext.assertTrue( returnValue ); testContext.assertNotNull( responseStatusCode[0] ); testContext.assertEquals( 400 , responseStatusCode[0] ); testContext.assertTrue( httpServerResponse.getResultBuffer().startsWith("ValidationException: ") ); }