CloudWatch { public static Iterable<Metric> listMetrics(final MetricApi metricApi, final ListMetricsOptions options) { return Iterables.concat(PagedIterables.advance(metricApi.list(options), new Function<Object, IterableWithMarker<Metric>>() { @Override public IterableWithMarker<Metric> apply(Object input) { return metricApi.list(options.clone().afterMarker(input)); } @Override public String toString() { return "listMetrics(" + options + ")"; } })); }  static Iterable<Metric> listMetrics(final MetricApi metricApi, final ListMetricsOptions options); static Iterable<Metric> listMetrics(CloudWatchApi cloudWatchApi, String region,
            final ListMetricsOptions options); static void putMetricData(CloudWatchApi cloudWatchApi, String region, Iterable<MetricDatum> metrics,
            String namespace);  }
@Test public void testSinglePageResult() throws Exception { CloudWatchApi api = createMock(CloudWatchApi.class); MetricApi metricApi = createMock(MetricApi.class); ListMetricsOptions options = new ListMetricsOptions(); IterableWithMarker<Metric> response = IterableWithMarkers.from(ImmutableSet.of(createMock(Metric.class)), null); expect(api.getMetricApiForRegion(null)) .andReturn(metricApi) .atLeastOnce(); expect(metricApi.list(options)) .andReturn(response) .once(); EasyMock.replay(api, metricApi); Assert.assertEquals(1, Iterables.size(CloudWatch.listMetrics(api, null, options))); }
@Test public void testMultiPageResult() throws Exception { CloudWatchApi api = createMock(CloudWatchApi.class); MetricApi metricApi = createMock(MetricApi.class); ListMetricsOptions options = new ListMetricsOptions(); IterableWithMarker<Metric> response1 = IterableWithMarkers.from(ImmutableSet.of(createMock(Metric.class)), "NEXTTOKEN"); IterableWithMarker<Metric> response2 = IterableWithMarkers.from(ImmutableSet.of(createMock(Metric.class)), null); expect(api.getMetricApiForRegion(EasyMock.eq(""))) .andReturn(metricApi) .atLeastOnce(); expect(metricApi.list(anyObject(ListMetricsOptions.class))) .andReturn(response1) .once(); expect(metricApi.list(anyObject(ListMetricsOptions.class))) .andReturn(response2) .once(); EasyMock.replay(api, metricApi); Assert.assertEquals(2, Iterables.size(CloudWatch.listMetrics(api, "", options))); }