Fix NPE in StackdriverWriter

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=131603566
This commit is contained in:
shikhman 2016-08-29 10:00:17 -07:00 committed by Ben McIlwain
parent 942dc58251
commit d77dced024
2 changed files with 24 additions and 1 deletions

View file

@ -232,7 +232,7 @@ public class StackdriverWriter implements MetricWriter {
logger.info(String.format("Registered new metric descriptor %s", descriptor.getType()));
} catch (GoogleJsonResponseException jsonException) {
// Not the error we were expecting, just give up
if (!jsonException.getStatusMessage().equals("ALREADY_EXISTS")) {
if (!"ALREADY_EXISTS".equals(jsonException.getStatusMessage())) {
throw jsonException;
}

View file

@ -203,6 +203,8 @@ public class StackdriverWriterTest {
@Test
public void registerMetric_fetchesStackdriverDefinition() throws Exception {
// Stackdriver throws an Exception with the status message "ALREADY_EXISTS" when you try to
// register a metric that's already been registered, so we fake one here.
ByteArrayInputStream inputStream = new ByteArrayInputStream("".getBytes(UTF_8));
HttpResponse response = GoogleJsonResponseExceptionHelper.createHttpResponse(400, inputStream);
HttpResponseException.Builder httpResponseExceptionBuilder =
@ -220,6 +222,27 @@ public class StackdriverWriterTest {
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);
try {
writer.registerMetric(metric);
fail("Expected GoogleJsonResponseException");
} catch (GoogleJsonResponseException expected) {
assertThat(exception.getStatusCode()).isEqualTo(404);
}
}
@Test
public void getEncodedTimeSeries_nullLabels_encodes() throws Exception {
ByteArrayInputStream inputStream = new ByteArrayInputStream("".getBytes(UTF_8));