### Question:
OpenTSDBReporter extends TimeWindowReporter { public static Builder builder() { return new Builder(); } private  OpenTSDBReporter(final String name, final OpenTSDBHttpClient client,
      final int windowSeconds,
      final Map<Class<? extends Metric>, Function<Metric, ? extends Aggregator>> defaultAggregators,
      final Map<String, Function<Metric, ? extends Aggregator>> metricAggregators); static Builder builder();  }### Answer:
@Test public void testReportingBaseUriWithPath(@Mocked CloseableHttpClient httpClient, @Mocked CloseableHttpResponse closeableHttpResponse, @Mocked StatusLine statusLine) throws InterruptedException, IOException { new Expectations() {{ httpClient.execute((HttpUriRequest) any); result = closeableHttpResponse; closeableHttpResponse.getStatusLine(); result = statusLine; statusLine.getStatusCode(); result = 200; }}; MetricRegistry registry = new MetricRegistry(); OpenTSDBReporter reporter = OpenTSDBReporter.builder() .withBaseUri(URI.create("http: .build(); registry.addReporter(reporter); Counter counter = registry.counter("counter"); counter.inc("tag", "value"); Thread.sleep(3000); new Verifications() {{ HttpUriRequest request; httpClient.execute(request = withCapture()); assertEquals("/some/path/api/v1/put", request.getURI().getPath()); times = 1; }}; }
@Test public void testSetApiEndpoint(@Mocked CloseableHttpClient httpClient, @Mocked CloseableHttpResponse closeableHttpResponse, @Mocked StatusLine statusLine) throws InterruptedException, IOException { new Expectations() {{ httpClient.execute((HttpUriRequest) any); result = closeableHttpResponse; closeableHttpResponse.getStatusLine(); result = statusLine; statusLine.getStatusCode(); result = 200; }}; MetricRegistry registry = new MetricRegistry(); OpenTSDBReporter reporter = OpenTSDBReporter.builder() .withBaseUri(URI.create("http: .withApiEndpoint("very/special/put") .build(); registry.addReporter(reporter); Counter counter = registry.counter("counter"); counter.inc("tag", "value"); Thread.sleep(3000); new Verifications() {{ HttpUriRequest request; httpClient.execute(request = withCapture()); assertEquals("/very/special/put", request.getURI().getPath()); times = 1; }}; }
@Test public void testSetWindow() { OpenTSDBReporter r = OpenTSDBReporter.builder() .withBaseUri(TEST_URI) .withWindowSize(12765) .build(); long actualWindowSizeMillis = Deencapsulation.getField(r, "windowStepSizeMillis"); assertEquals(12765 * 1000, actualWindowSizeMillis); }
@Test public void testSetBatchSize() { OpenTSDBReporter r = OpenTSDBReporter.builder() .withBaseUri(TEST_URI) .withBatchSize(123) .build(); OpenTSDBHttpClient client = Deencapsulation.getField(r, "client"); int actualBatchSize = Deencapsulation.getField(client, "batchSize"); assertEquals(123, actualBatchSize); }
@Test public void testMissingBaseUri() { assertThrows(IllegalArgumentException.class, () -> OpenTSDBReporter.builder().build()); }
@Test public void testInvalidBaseUri() { assertThrows(IllegalArgumentException.class, () -> OpenTSDBReporter.builder().withBaseUri(URI.create("http: }
@Test public void testInvalidBatchSize() { assertThrows(IllegalArgumentException.class, () -> OpenTSDBReporter.builder().withBatchSize(0)); }