### Question:
StackdriverWriter implements MetricWriter { @VisibleForTesting MetricDescriptor registerMetric(final com.google.monitoring.metrics.Metric<?> metric) throws IOException { if (registeredDescriptors.containsKey(metric)) { logger.fine( String.format("Using existing metric descriptor %s", metric.getMetricSchema().name())); return registeredDescriptors.get(metric); } MetricDescriptor descriptor = encodeMetricDescriptor(metric); rateLimiter.acquire(); try { descriptor = monitoringClient .projects() .metricDescriptors() .create(projectResource, descriptor) .execute(); logger.info(String.format("Registered new metric descriptor %s", descriptor.getType())); } catch (GoogleJsonResponseException jsonException) { if (!"ALREADY_EXISTS".equals(jsonException.getStatusMessage())) { throw jsonException; } descriptor = monitoringClient .projects() .metricDescriptors() .get(projectResource + "/metricDescriptors/" + descriptor.getType()) .execute(); logger.info( String.format("Fetched existing metric descriptor %s", metric.getMetricSchema().name())); } registeredDescriptors.put(metric, descriptor); return descriptor; } StackdriverWriter(
      Monitoring monitoringClient,
      String project,
      MonitoredResource monitoredResource,
      int maxQps,
      int maxPointsPerRequest); @Override void write(com.google.monitoring.metrics.MetricPoint<V> point); @Override void flush();  }### Answer:
@Test public void registerMetric_fetchesStackdriverDefinition() throws Exception { ByteArrayInputStream inputStream = new ByteArrayInputStream("".getBytes(UTF_8)); HttpResponse response = GoogleJsonResponseExceptionHelper.createHttpResponse(400, inputStream); HttpResponseException.Builder httpResponseExceptionBuilder = new HttpResponseException.Builder(response); httpResponseExceptionBuilder.setStatusCode(400); httpResponseExceptionBuilder.setStatusMessage("ALREADY_EXISTS"); GoogleJsonResponseException exception = new GoogleJsonResponseException(httpResponseExceptionBuilder, null); when(metricDescriptorCreate.execute()).thenThrow(exception); StackdriverWriter writer = new StackdriverWriter(client, PROJECT, MONITORED_RESOURCE, MAX_QPS, MAX_POINTS_PER_REQUEST); writer.registerMetric(metric); verify(client.projects().metricDescriptors().get("metric")).execute(); }
@Test public void registerMetric_rethrowsException() throws Exception { ByteArrayInputStream inputStream = new ByteArrayInputStream("".getBytes(UTF_8)); HttpResponse response = GoogleJsonResponseExceptionHelper.createHttpResponse(400, inputStream); HttpResponseException.Builder httpResponseExceptionBuilder = new HttpResponseException.Builder(response); httpResponseExceptionBuilder.setStatusCode(404); GoogleJsonResponseException exception = new GoogleJsonResponseException(httpResponseExceptionBuilder, null); when(metricDescriptorCreate.execute()).thenThrow(exception); StackdriverWriter writer = new StackdriverWriter(client, PROJECT, MONITORED_RESOURCE, MAX_QPS, MAX_POINTS_PER_REQUEST); assertThrows(GoogleJsonResponseException.class, () -> writer.registerMetric(metric)); assertThat(exception.getStatusCode()).isEqualTo(404); }