public GetMetricStatisticsResult unmarshall(StaxUnmarshallerContext context) throws Exception { GetMetricStatisticsResult getMetricStatisticsResult = new GetMetricStatisticsResult(); int originalDepth = context.getCurrentDepth(); int targetDepth = originalDepth + 1; if (context.isStartOfDocument()) targetDepth += 2; while (true) { XMLEvent xmlEvent = context.nextEvent(); if (xmlEvent.isEndDocument()) return getMetricStatisticsResult; if (xmlEvent.isAttribute() || xmlEvent.isStartElement()) { if (context.testExpression("Label", targetDepth)) { getMetricStatisticsResult.setLabel(StringStaxUnmarshaller.getInstance().unmarshall(context)); continue; } if (context.testExpression("Datapoints", targetDepth)) { getMetricStatisticsResult.withDatapoints(new ArrayList<Datapoint>()); continue; } if (context.testExpression("Datapoints/member", targetDepth)) { getMetricStatisticsResult.withDatapoints(DatapointStaxUnmarshaller.getInstance().unmarshall(context)); continue; } } else if (xmlEvent.isEndElement()) { if (context.getCurrentDepth() < originalDepth) { return getMetricStatisticsResult; } } } }
public GetMetricStatisticsResult unmarshall(StaxUnmarshallerContext context) throws Exception { GetMetricStatisticsResult getMetricStatisticsResult = new GetMetricStatisticsResult(); int originalDepth = context.getCurrentDepth(); int targetDepth = originalDepth + 1; if (context.isStartOfDocument()) targetDepth += 2; while (true) { int xmlEvent = context.nextEvent(); if (xmlEvent == XmlPullParser.END_DOCUMENT) break; if (xmlEvent == XmlPullParser.START_TAG) { if (context.testExpression("Label", targetDepth)) { getMetricStatisticsResult.setLabel(StringStaxUnmarshaller.getInstance() .unmarshall(context)); continue; } if (context.testExpression("Datapoints/member", targetDepth)) { getMetricStatisticsResult.withDatapoints(DatapointStaxUnmarshaller .getInstance().unmarshall(context)); continue; } } else if (xmlEvent == XmlPullParser.END_TAG) { if (context.getCurrentDepth() < originalDepth) { break; } } } return getMetricStatisticsResult; }
@Test public void shouldCountBytesWhenSingleDataPointReturned() throws Exception { Instant countSince = new Instant("2017-04-06T10:00:00.000Z"); Instant countTo = new Instant("2017-04-06T11:00:00.000Z"); Minutes periodTime = Minutes.minutesBetween(countSince, countTo); GetMetricStatisticsRequest metricStatisticsRequest = underTest.createMetricStatisticsRequest(STREAM, countSince, countTo, periodTime); GetMetricStatisticsResult result = new GetMetricStatisticsResult().withDatapoints(new Datapoint().withSum(1.0)); given(cloudWatch.getMetricStatistics(metricStatisticsRequest)).willReturn(result); long backlogBytes = underTest.getBacklogBytes(STREAM, countSince, countTo); assertThat(backlogBytes).isEqualTo(1L); }
@Test public void shouldCountBytesWhenMultipleDataPointsReturned() throws Exception { Instant countSince = new Instant("2017-04-06T10:00:00.000Z"); Instant countTo = new Instant("2017-04-06T11:00:00.000Z"); Minutes periodTime = Minutes.minutesBetween(countSince, countTo); GetMetricStatisticsRequest metricStatisticsRequest = underTest.createMetricStatisticsRequest(STREAM, countSince, countTo, periodTime); GetMetricStatisticsResult result = new GetMetricStatisticsResult() .withDatapoints( new Datapoint().withSum(1.0), new Datapoint().withSum(3.0), new Datapoint().withSum(2.0)); given(cloudWatch.getMetricStatistics(metricStatisticsRequest)).willReturn(result); long backlogBytes = underTest.getBacklogBytes(STREAM, countSince, countTo); assertThat(backlogBytes).isEqualTo(6L); }