use of com.google.logging.v2.ListLogMetricsRequest in project google-cloud-java by GoogleCloudPlatform.
the class LoggingImpl method listMetricsAsync.
private static ApiFuture<AsyncPage<Metric>> listMetricsAsync(final LoggingOptions serviceOptions, final Map<Option.OptionType, ?> options) {
final ListLogMetricsRequest request = listMetricsRequest(serviceOptions, options);
ApiFuture<ListLogMetricsResponse> list = serviceOptions.getLoggingRpcV2().list(request);
return transform(list, new Function<ListLogMetricsResponse, AsyncPage<Metric>>() {
@Override
public AsyncPage<Metric> apply(ListLogMetricsResponse listMetricsResponse) {
List<Metric> metrics = listMetricsResponse.getMetricsList() == null ? ImmutableList.<Metric>of() : Lists.transform(listMetricsResponse.getMetricsList(), Metric.fromPbFunction(serviceOptions.getService()));
String cursor = listMetricsResponse.getNextPageToken().equals("") ? null : listMetricsResponse.getNextPageToken();
return new AsyncPageImpl<>(new MetricPageFetcher(serviceOptions, cursor, options), cursor, metrics);
}
});
}
use of com.google.logging.v2.ListLogMetricsRequest in project google-cloud-java by GoogleCloudPlatform.
the class LoggingImplTest method testListMetricsAsyncEmpty.
@Test
public void testListMetricsAsyncEmpty() throws ExecutionException, InterruptedException {
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListLogMetricsRequest request = ListLogMetricsRequest.newBuilder().setParent(PROJECT_PB).build();
List<Metric> sinkList = ImmutableList.of();
ListLogMetricsResponse response = ListLogMetricsResponse.newBuilder().setNextPageToken("").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();
assertNull(page.getNextPageToken());
assertNull(page.getNextPage());
assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Metric.class));
}
use of com.google.logging.v2.ListLogMetricsRequest in project google-cloud-java by GoogleCloudPlatform.
the class LoggingImplTest method testListMetricsWithOptions.
@Test
public void testListMetricsWithOptions() {
String cursor = "cursor";
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListLogMetricsRequest request = ListLogMetricsRequest.newBuilder().setPageToken(cursor).setPageSize(42).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(ListOption.pageSize(42), ListOption.pageToken(cursor));
assertEquals(cursor, page.getNextPageToken());
assertArrayEquals(sinkList.toArray(), Iterables.toArray(page.getValues(), Metric.class));
}
use of com.google.logging.v2.ListLogMetricsRequest in project google-cloud-java by GoogleCloudPlatform.
the class LoggingImplTest method testListMetricsNextPage.
@Test
public void testListMetricsNextPage() {
String cursor1 = "cursor";
EasyMock.replay(rpcFactoryMock);
logging = options.getService();
ListLogMetricsRequest request1 = ListLogMetricsRequest.newBuilder().setParent(PROJECT_PB).build();
ListLogMetricsRequest request2 = ListLogMetricsRequest.newBuilder().setParent(PROJECT_PB).setPageToken(cursor1).build();
List<Metric> sinkList1 = ImmutableList.of(new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)), new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)));
List<Metric> sinkList2 = ImmutableList.of(new Metric(logging, new MetricInfo.BuilderImpl(METRIC_INFO)));
ListLogMetricsResponse response1 = ListLogMetricsResponse.newBuilder().setNextPageToken(cursor1).addAllMetrics(Lists.transform(sinkList1, METRIC_TO_PB_FUNCTION)).build();
String cursor2 = "nextCursor";
ListLogMetricsResponse response2 = ListLogMetricsResponse.newBuilder().setNextPageToken(cursor2).addAllMetrics(Lists.transform(sinkList2, METRIC_TO_PB_FUNCTION)).build();
ApiFuture<ListLogMetricsResponse> futureResponse1 = ApiFutures.immediateFuture(response1);
ApiFuture<ListLogMetricsResponse> futureResponse2 = ApiFutures.immediateFuture(response2);
EasyMock.expect(loggingRpcMock.list(request1)).andReturn(futureResponse1);
EasyMock.expect(loggingRpcMock.list(request2)).andReturn(futureResponse2);
EasyMock.replay(loggingRpcMock);
Page<Metric> page = logging.listMetrics();
assertEquals(cursor1, page.getNextPageToken());
assertArrayEquals(sinkList1.toArray(), Iterables.toArray(page.getValues(), Metric.class));
page = page.getNextPage();
assertEquals(cursor2, page.getNextPageToken());
assertArrayEquals(sinkList2.toArray(), Iterables.toArray(page.getValues(), Metric.class));
}
use of com.google.logging.v2.ListLogMetricsRequest in project google-cloud-java by GoogleCloudPlatform.
the class MetricsClientTest method listLogMetricsTest.
@Test
@SuppressWarnings("all")
public void listLogMetricsTest() {
String nextPageToken = "";
LogMetric metricsElement = LogMetric.newBuilder().build();
List<LogMetric> metrics = Arrays.asList(metricsElement);
ListLogMetricsResponse expectedResponse = ListLogMetricsResponse.newBuilder().setNextPageToken(nextPageToken).addAllMetrics(metrics).build();
mockMetricsServiceV2.addResponse(expectedResponse);
ParentNameOneof parent = ParentNameOneof.from(ProjectName.create("[PROJECT]"));
ListLogMetricsPagedResponse pagedListResponse = client.listLogMetrics(parent);
List<LogMetric> resources = Lists.newArrayList(pagedListResponse.iterateAll());
Assert.assertEquals(1, resources.size());
Assert.assertEquals(expectedResponse.getMetricsList().get(0), resources.get(0));
List<GeneratedMessageV3> actualRequests = mockMetricsServiceV2.getRequests();
Assert.assertEquals(1, actualRequests.size());
ListLogMetricsRequest actualRequest = (ListLogMetricsRequest) actualRequests.get(0);
Assert.assertEquals(parent, actualRequest.getParentAsParentNameOneof());
}
Aggregations