### Question:
MetricsService implements HttpEndpointService { @Override public HttpServiceResponse handle(HttpServiceRequest request) throws Exception { HttpServiceResponse response = new HttpServiceResponse(); if (Method.GET != request.getMethod()) { response.setCode(StatusCode.FORBIDDEN); response.setBody(request.getMethod() + " is forbidden. Should be GET method"); return response; } if (null == statsProvider) { response.setCode(StatusCode.INTERNAL_ERROR); response.setBody("Stats provider is not enabled. Please enable it by set statsProviderClass" + " on bookie configuration"); return response; } try (StringWriter writer = new StringWriter(1024)) { statsProvider.writeAllMetrics(writer); writer.flush(); response.setCode(StatusCode.OK); response.setBody(writer.getBuffer().toString()); } catch (UnsupportedOperationException uoe) { response.setCode(StatusCode.INTERNAL_ERROR); response.setBody("Currently stats provider doesn't support exporting metrics in http service"); } catch (IOException ioe) { response.setCode(StatusCode.INTERNAL_ERROR); response.setBody("Exceptions are thrown when exporting metrics : " + ioe.getMessage()); } return response; } MetricsService(ServerConfiguration conf,
                          StatsProvider statsProvider); @Override HttpServiceResponse handle(HttpServiceRequest request);  }### Answer:
@Test public void testForbiddenMethods() throws Exception { HttpServiceRequest request = new HttpServiceRequest().setMethod(Method.PUT); HttpServiceResponse response = service.handle(request); assertEquals(StatusCode.FORBIDDEN.getValue(), response.getStatusCode()); assertEquals( "PUT is forbidden. Should be GET method", response.getBody()); }
@Test public void testNullStatsProvider() throws Exception { service = new MetricsService(new ServerConfiguration(), null); HttpServiceRequest request = new HttpServiceRequest().setMethod(Method.GET); HttpServiceResponse response = service.handle(request); assertEquals(StatusCode.INTERNAL_ERROR.getValue(), response.getStatusCode()); assertEquals( "Stats provider is not enabled. Please enable it by set statsProviderClass" + " on bookie configuration", response.getBody()); }
@Test public void testWriteMetrics() throws Exception { String content = "test-metrics"; doAnswer(invocationOnMock -> { Writer writer = invocationOnMock.getArgument(0); writer.write(content); return null; }).when(mockStatsProvider).writeAllMetrics(any(Writer.class)); HttpServiceRequest request = new HttpServiceRequest().setMethod(Method.GET); HttpServiceResponse response = service.handle(request); assertEquals(StatusCode.OK.getValue(), response.getStatusCode()); assertEquals(content, response.getBody()); }
@Test public void testWriteMetricsException() throws Exception { doThrow(new IOException("write-metrics-exception")) .when(mockStatsProvider).writeAllMetrics(any(Writer.class)); HttpServiceRequest request = new HttpServiceRequest().setMethod(Method.GET); HttpServiceResponse response = service.handle(request); assertEquals(StatusCode.INTERNAL_ERROR.getValue(), response.getStatusCode()); assertEquals("Exceptions are thrown when exporting metrics : write-metrics-exception", response.getBody()); }
@Test public void testWriteMetricsUnimplemented() throws Exception { mockStatsProvider = mock(StatsProvider.class, CALLS_REAL_METHODS); service = new MetricsService(new ServerConfiguration(), mockStatsProvider); HttpServiceRequest request = new HttpServiceRequest().setMethod(Method.GET); HttpServiceResponse response = service.handle(request); assertEquals(StatusCode.INTERNAL_ERROR.getValue(), response.getStatusCode()); assertEquals("Currently stats provider doesn't support exporting metrics in http service", response.getBody()); }