### Question:
PutLogLevel implements Handler { @OpenApi( path = ROUTE, method = HttpMethod.PUT, summary = "Changes the log level without restarting.", tags = {TAG_ADMIN}, requestBody = @OpenApiRequestBody( content = {@OpenApiContent(from = LogLevel.class)}, description = "```\n{\n \"level\": (String; acceptable values: ALL, TRACE, DEBUG, INFO, ERROR, FATAL, OFF ),\n" + " \"log_filter\": [(String; Optional)]\n}\n```"), description = "Changes the log level without restarting. You can change the log level for all logs, or the log level for specific packages or classes.", responses = { @OpenApiResponse( status = RES_NO_CONTENT, description = "The LogLevel was accepted and applied"), @OpenApiResponse(status = RES_BAD_REQUEST, description = INVALID_BODY_SUPPLIED), @OpenApiResponse(status = RES_INTERNAL_ERROR) }) @Override public void handle(final Context ctx) throws Exception { try { final LogLevel params = jsonProvider.jsonToObject(ctx.body(), LogLevel.class); final String[] logFilters = params.getLogFilter().orElseGet(() -> new String[] {""}); for (final String logFilter : logFilters) { LoggingConfigurator.setAllLevels(logFilter, params.getLevel()); } ctx.status(SC_NO_CONTENT); } catch (final IllegalArgumentException | JsonMappingException e) { ctx.result(jsonProvider.objectToJSON(new BadRequest(e.getMessage()))); ctx.status(SC_BAD_REQUEST); } } PutLogLevel(final JsonProvider jsonProvider); @OpenApi( path = ROUTE, method = HttpMethod.PUT, summary = "Changes the log level without restarting.", tags = {TAG_ADMIN}, requestBody = @OpenApiRequestBody( content = {@OpenApiContent(from = LogLevel.class)}, description = "```\n{\n \"level\": (String; acceptable values: ALL, TRACE, DEBUG, INFO, ERROR, FATAL, OFF ),\n" + " \"log_filter\": [(String; Optional)]\n}\n```"), description = "Changes the log level without restarting. You can change the log level for all logs, or the log level for specific packages or classes.", responses = { @OpenApiResponse( status = RES_NO_CONTENT, description = "The LogLevel was accepted and applied"), @OpenApiResponse(status = RES_BAD_REQUEST, description = INVALID_BODY_SUPPLIED), @OpenApiResponse(status = RES_INTERNAL_ERROR) }) @Override void handle(final Context ctx); static final String ROUTE; }### Answer:
@Test public void shouldReturnBadRequestWhenLevelIsMissing() throws Exception { when(context.body()).thenReturn("{\"a\": \"field\"}"); handler.handle(context); verify(context).status(SC_BAD_REQUEST); }
@Test public void shouldReturnBadRequestWhenLevelIsInvalid() throws Exception { when(context.body()).thenReturn("{\"level\": \"I do not exist\"}"); handler.handle(context); verify(context).status(SC_BAD_REQUEST); }
@Test public void shouldReturnNoContentWhenLevelIsValid() throws Exception { when(context.body()).thenReturn("{\"level\": \"inFO\"}"); handler.handle(context); verify(context).status(SC_NO_CONTENT); }
@Test public void shouldReturnBadRequestWhenLFilterIsInvalidJson() throws Exception { when(context.body()) .thenReturn("{\"level\": \"I do not exist\", \"log_filter\": \"a.class.somewhere\"}"); handler.handle(context); verify(context).status(SC_BAD_REQUEST); }
@Test public void shouldReturnNoContentWhenLevelAndFilterAreValid() throws Exception { when(context.body()) .thenReturn("{\"level\": \"InfO\", \"log_filter\": [\"a.class.somewhere\"]}"); handler.handle(context); verify(context).status(SC_NO_CONTENT); }