InstanceMetadata { public String metadata(String keyName) { GenericUrl metadataUrl = BASE_URL.clone(); metadataUrl.appendRawPath(keyName); String value; try { HttpRequest request = requestFactory.buildGetRequest(metadataUrl); HttpResponse response = request.execute(); value = inputStreamToString(response.getContent()); } catch (HttpResponseException e) { if (e.getStatusCode() == HttpStatusCodes.STATUS_CODE_NOT_FOUND) { throw new MetadataNotFoundException(keyName); } else { throw new IllegalStateException(e); } } catch (IOException e) { throw new IllegalStateException(e); } return value; } InstanceMetadata(JsonFactory jsonFactory, HttpTransport httpTransport); String metadata(String keyName); static String resourceShortName(String resource); String customMetadata(String keyName); OAuth2ServiceTokenMetadata serviceToken(final String scope); OAuth2ServiceTokenMetadata serviceToken(final String serviceAccount, final String scope); NetworkMetadata network();  }
@Test(expected = MetadataNotFoundException.class) public void testMetadataNotFound() { InstanceMetadata metadata = mockRequest(HttpStatusCodes.STATUS_CODE_NOT_FOUND); metadata.metadata("a-key-that-does-not-exist"); }
@Test(expected = IllegalStateException.class) public void testMetadataError() { InstanceMetadata metadata = mockRequest(HttpStatusCodes.STATUS_CODE_SERVER_ERROR); metadata.metadata("a-key-causing-error"); }