QueueCircuitBreakerConfigurationResourceManager implements LoggableResource { public boolean handleConfigurationResource(final HttpServerRequest request) { if (request.uri().equals(circuitBreakerConfigUri) && HttpMethod.PUT == request.method()) { request.bodyHandler(configResourceBuffer -> { try { extractConfigurationValues(configResourceBuffer); } catch (ValidationException validationException) { log.error("Could not parse circuit breaker configuration resource: " + validationException.toString()); ResponseStatusCodeLogUtil.info(request, StatusCode.BAD_REQUEST, QueueCircuitBreakerConfigurationResourceManager.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(circuitBreakerConfigUri, configResourceBuffer, status -> { if (status == OK.getStatusCode()) { if(logConfigurationResourceChanges){ RequestLogger.logRequest(vertx.eventBus(), request, OK.getStatusCode(), configResourceBuffer); } vertx.eventBus().publish(UPDATE_ADDRESS, true); } else { request.response().setStatusCode(status); } ResponseStatusCodeLogUtil.info(request, StatusCode.fromCode(status), QueueCircuitBreakerConfigurationResourceManager.class); request.response().end(); }); }); return true; } if (request.uri().equals(circuitBreakerConfigUri) && HttpMethod.DELETE == request.method()) { getConfigurationResource().reset(); log.info("reset circuit breaker configuration resource"); notifyRefreshables(); } return false; } QueueCircuitBreakerConfigurationResourceManager(Vertx vertx, ResourceStorage storage, String circuitBreakerConfigUri); QueueCircuitBreakerConfigurationResource getConfigurationResource(); @Override void enableResourceLogging(boolean resourceLoggingEnabled); void addRefreshable(Refreshable refreshable); boolean handleConfigurationResource(final HttpServerRequest request);  }
@Test public void testValidContentShouldBeStoredInStorage(TestContext context){ Async async = context.async(); QueueCircuitBreakerConfigurationResourceManager manager = new QueueCircuitBreakerConfigurationResourceManager(vertx, storage, CONFIGURATION_URI); final DummyHttpServerResponse response = new DummyHttpServerResponse(); class UpdateConfigResourceWithValidDataRequest extends ConfigResourcePUTRequest { @Override public HttpServerRequest bodyHandler(Handler<Buffer> bodyHandler) { bodyHandler.handle(Buffer.buffer(VALID_CONFIG_RESOURCE)); return this; } @Override public HttpServerResponse response() { return response; } } storage.get(CONFIGURATION_URI, result -> { context.assertEquals(INITIAL_CONFIG_RESOURCE, result.toString()); context.assertTrue(manager.handleConfigurationResource(new UpdateConfigResourceWithValidDataRequest())); storage.get(CONFIGURATION_URI, updatedResult -> { context.assertEquals(VALID_CONFIG_RESOURCE, updatedResult.toString()); async.complete(); }); }); }
@Test public void testInvalidContentShouldNotBeStoredInStorage(TestContext context){ Async async = context.async(); QueueCircuitBreakerConfigurationResourceManager manager = new QueueCircuitBreakerConfigurationResourceManager(vertx, storage, CONFIGURATION_URI); final DummyHttpServerResponse response = new DummyHttpServerResponse(); class UpdateLoggingResourceWithValidDataRequest extends ConfigResourcePUTRequest { @Override public HttpServerRequest bodyHandler(Handler<Buffer> bodyHandler) { bodyHandler.handle(Buffer.buffer(INVALID_CONFIG_RESOURCE)); return this; } @Override public HttpServerResponse response() { return response; } } storage.get(CONFIGURATION_URI, result -> { context.assertEquals(INITIAL_CONFIG_RESOURCE, result.toString()); context.assertTrue(manager.handleConfigurationResource(new UpdateLoggingResourceWithValidDataRequest())); storage.get(CONFIGURATION_URI, updatedResult -> { context.assertEquals(INITIAL_CONFIG_RESOURCE, updatedResult.toString(), "The configuration resource in the storage should still be equal to the initial resource"); context.assertEquals(StatusCode.BAD_REQUEST.getStatusCode(), response.getStatusCode()); async.complete(); }); }); }