/** Example of deleting the metric. */ // [TARGET delete()] public boolean delete() { // [START delete] boolean deleted = metric.delete(); if (deleted) { // the metric was deleted } else { // the metric was not found } // [END delete] return deleted; }
/** Example of asynchronously deleting the metric. */ // [TARGET deleteAsync()] public boolean deleteAsync() throws ExecutionException, InterruptedException { // [START deleteAsync] Future<Boolean> future = metric.deleteAsync(); // ... boolean deleted = future.get(); if (deleted) { // the metric was deleted } else { // the metric was not found } // [END deleteAsync] return deleted; } }
/** Example of asynchronously updating the metric's information. */ // [TARGET updateAsync()] public Metric updateAsync() throws ExecutionException, InterruptedException { // [START updateAsync] Future<Metric> future = metric.toBuilder().setDescription("A more detailed description").build().updateAsync(); // ... Metric updatedMetric = future.get(); // [END updateAsync] return updatedMetric; }
private void compareMetric(Metric expected, Metric value) { assertEquals(expected, value); assertEquals(expected.getName(), value.getName()); assertEquals(expected.getDescription(), value.getDescription()); assertEquals(expected.getFilter(), value.getFilter()); assertEquals(expected.hashCode(), value.hashCode()); assertEquals(expected.toString(), value.toString()); } }
@Test public void testListMetricsAsync() throws ExecutionException, InterruptedException { String cursor = "cursor"; EasyMock.replay(rpcFactoryMock); logging = options.getService(); ListLogMetricsRequest request = ListLogMetricsRequest.newBuilder().setParent(PROJECT_PB).build(); List<Metric> sinkList = ImmutableList.of( new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO))); ListLogMetricsResponse response = ListLogMetricsResponse.newBuilder() .setNextPageToken(cursor) .addAllMetrics(Lists.transform(sinkList, METRIC_TO_PB_FUNCTION)) .build(); ApiFuture<ListLogMetricsResponse> futureResponse = ApiFutures.immediateFuture(response); EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); EasyMock.replay(loggingRpcMock); AsyncPage<Metric> page = logging.listMetricsAsync().get(); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Metric.class)); }
@Test public void testListMetrics() { String cursor = "cursor"; EasyMock.replay(rpcFactoryMock); logging = options.getService(); ListLogMetricsRequest request = ListLogMetricsRequest.newBuilder().setParent(PROJECT_PB).build(); List<Metric> sinkList = ImmutableList.of( new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO))); ListLogMetricsResponse response = ListLogMetricsResponse.newBuilder() .setNextPageToken(cursor) .addAllMetrics(Lists.transform(sinkList, METRIC_TO_PB_FUNCTION)) .build(); ApiFuture<ListLogMetricsResponse> futureResponse = ApiFutures.immediateFuture(response); EasyMock.expect(loggingRpcMock.list(request)).andReturn(futureResponse); EasyMock.replay(loggingRpcMock); Page<Metric> page = logging.listMetrics(); assertEquals(cursor, page.getNextPageToken()); assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Metric.class)); }
@Test public void testDeleteAsyncFalse() throws ExecutionException, InterruptedException { initializeExpectedMetric(1); expect(logging.getOptions()).andReturn(mockOptions); expect(logging.deleteMetricAsync(NAME)).andReturn(ApiFutures.immediateFuture(false)); replay(logging); initializeMetric(); assertFalse(metric.deleteAsync().get()); }
@Test public void testDeleteTrue() { initializeExpectedMetric(1); expect(logging.getOptions()).andReturn(mockOptions); expect(logging.deleteMetric(NAME)).andReturn(true); replay(logging); initializeMetric(); assertTrue(metric.delete()); }
private void initializeExpectedMetric(int optionsCalls) { expect(serviceMockReturnsOptions.getOptions()).andReturn(mockOptions).times(optionsCalls); replay(serviceMockReturnsOptions); logging = createStrictMock(Logging.class); expectedMetric = new Metric(serviceMockReturnsOptions, new Metric.BuilderImpl(METRIC_INFO)); }
@Test public void testUpdate() { initializeExpectedMetric(2); MetricInfo updatedInfo = METRIC_INFO.toBuilder().setFilter(NEW_FILTER).build(); Metric expectedMetric = new Metric(serviceMockReturnsOptions, new MetricInfo.BuilderImpl(updatedInfo)); expect(logging.getOptions()).andReturn(mockOptions).times(2); expect(logging.update(expectedMetric)).andReturn(expectedMetric); replay(logging); initializeMetric(); Metric updatedMetric = metric.toBuilder().setFilter(NEW_FILTER).build().update(); compareMetric(expectedMetric, updatedMetric); }
@Test public void testUpdateAsync() throws ExecutionException, InterruptedException { initializeExpectedMetric(2); MetricInfo updatedInfo = METRIC_INFO.toBuilder().setFilter(NEW_FILTER).build(); Metric expectedMetric = new Metric(serviceMockReturnsOptions, new MetricInfo.BuilderImpl(updatedInfo)); expect(logging.getOptions()).andReturn(mockOptions).times(2); expect(logging.updateAsync(expectedMetric)) .andReturn(ApiFutures.immediateFuture(expectedMetric)); replay(logging); initializeMetric(); Metric updatedMetric = metric.toBuilder().setFilter(NEW_FILTER).build().updateAsync().get(); compareMetric(expectedMetric, updatedMetric); }
@Test public void testReload() { initializeExpectedMetric(2); MetricInfo updatedInfo = METRIC_INFO.toBuilder().setFilter(NEW_FILTER).build(); Metric expectedMetric = new Metric(serviceMockReturnsOptions, new MetricInfo.BuilderImpl(updatedInfo)); expect(logging.getOptions()).andReturn(mockOptions); expect(logging.getMetric(NAME)).andReturn(expectedMetric); replay(logging); initializeMetric(); Metric updatedMetric = metric.reload(); compareMetric(expectedMetric, updatedMetric); }
@Test public void testGetMetric() { LogMetric sinkPb = METRIC_INFO.toPb(); ApiFuture<LogMetric> response = ApiFutures.immediateFuture(sinkPb); GetLogMetricRequest request = GetLogMetricRequest.newBuilder().setMetricName(METRIC_NAME_PB).build(); EasyMock.expect(loggingRpcMock.get(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); Metric sink = logging.getMetric(METRIC_NAME); assertEquals(new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), sink); }
@Test public void testUpdateMetric() { LogMetric sinkPb = METRIC_INFO.toPb(); ApiFuture<LogMetric> response = ApiFutures.immediateFuture(sinkPb); UpdateLogMetricRequest request = UpdateLogMetricRequest.newBuilder().setMetricName(METRIC_NAME_PB).setMetric(sinkPb).build(); EasyMock.expect(loggingRpcMock.update(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); Metric sink = logging.update(METRIC_INFO); assertEquals(new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), sink); }
@Test public void testGetMetricAsync() throws ExecutionException, InterruptedException { LogMetric sinkPb = METRIC_INFO.toPb(); ApiFuture<LogMetric> response = ApiFutures.immediateFuture(sinkPb); GetLogMetricRequest request = GetLogMetricRequest.newBuilder().setMetricName(METRIC_NAME_PB).build(); EasyMock.expect(loggingRpcMock.get(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); Metric sink = logging.getMetricAsync(METRIC_NAME).get(); assertEquals(new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), sink); }
@Test public void testReloadAsyncNull() throws ExecutionException, InterruptedException { initializeExpectedMetric(1); expect(logging.getOptions()).andReturn(mockOptions); expect(logging.getMetricAsync(NAME)).andReturn(ApiFutures.<Metric>immediateFuture(null)); replay(logging); initializeMetric(); assertNull(metric.reloadAsync().get()); }
@Test public void testCreateMetric() { LogMetric metricPb = METRIC_INFO.toPb(); ApiFuture<LogMetric> response = ApiFutures.immediateFuture(metricPb); CreateLogMetricRequest request = CreateLogMetricRequest.newBuilder().setParent(PROJECT_PB).setMetric(metricPb).build(); EasyMock.expect(loggingRpcMock.create(request)).andReturn(response); EasyMock.replay(rpcFactoryMock, loggingRpcMock); logging = options.getService(); Metric metric = logging.create(METRIC_INFO); assertEquals(new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), metric); }