Search in sources :

Example 1 with ListLogMetricsResponse

use of com.google.logging.v2.ListLogMetricsResponse 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);
        }
    });
}
Also used : ListLogMetricsResponse(com.google.logging.v2.ListLogMetricsResponse) ListLogMetricsRequest(com.google.logging.v2.ListLogMetricsRequest) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) AsyncPage(com.google.api.gax.paging.AsyncPage)

Example 2 with ListLogMetricsResponse

use of com.google.logging.v2.ListLogMetricsResponse 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));
}
Also used : ListLogMetricsResponse(com.google.logging.v2.ListLogMetricsResponse) ListLogMetricsRequest(com.google.logging.v2.ListLogMetricsRequest) LogMetric(com.google.logging.v2.LogMetric) Test(org.junit.Test)

Example 3 with ListLogMetricsResponse

use of com.google.logging.v2.ListLogMetricsResponse 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));
}
Also used : ListLogMetricsResponse(com.google.logging.v2.ListLogMetricsResponse) ListLogMetricsRequest(com.google.logging.v2.ListLogMetricsRequest) LogMetric(com.google.logging.v2.LogMetric) Test(org.junit.Test)

Example 4 with ListLogMetricsResponse

use of com.google.logging.v2.ListLogMetricsResponse 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));
}
Also used : ListLogMetricsResponse(com.google.logging.v2.ListLogMetricsResponse) ListLogMetricsRequest(com.google.logging.v2.ListLogMetricsRequest) LogMetric(com.google.logging.v2.LogMetric) Test(org.junit.Test)

Example 5 with ListLogMetricsResponse

use of com.google.logging.v2.ListLogMetricsResponse 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());
}
Also used : LogMetric(com.google.logging.v2.LogMetric) ListLogMetricsResponse(com.google.logging.v2.ListLogMetricsResponse) ListLogMetricsPagedResponse(com.google.cloud.logging.spi.v2.PagedResponseWrappers.ListLogMetricsPagedResponse) ParentNameOneof(com.google.logging.v2.ParentNameOneof) ListLogMetricsRequest(com.google.logging.v2.ListLogMetricsRequest) GeneratedMessageV3(com.google.protobuf.GeneratedMessageV3) Test(org.junit.Test)

Aggregations

ListLogMetricsRequest (com.google.logging.v2.ListLogMetricsRequest)10 ListLogMetricsResponse (com.google.logging.v2.ListLogMetricsResponse)10 LogMetric (com.google.logging.v2.LogMetric)9 Test (org.junit.Test)9 AsyncPage (com.google.api.gax.paging.AsyncPage)1 ListLogMetricsPagedResponse (com.google.cloud.logging.spi.v2.PagedResponseWrappers.ListLogMetricsPagedResponse)1 ImmutableList (com.google.common.collect.ImmutableList)1 ParentNameOneof (com.google.logging.v2.ParentNameOneof)1 GeneratedMessageV3 (com.google.protobuf.GeneratedMessageV3)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1