CounterShortIdCache { public void storeNewShortIds( final ReportWorkItemStatusRequest request, final ReportWorkItemStatusResponse reply) { checkArgument( request.getWorkItemStatuses() != null && reply.getWorkItemServiceStates() != null && request.getWorkItemStatuses().size() == reply.getWorkItemServiceStates().size(), "RequestWorkItemStatus request and response are unbalanced, status: %s, states: %s", request.getWorkItemStatuses(), reply.getWorkItemServiceStates()); for (int i = 0; i < request.getWorkItemStatuses().size(); i++) { WorkItemServiceState state = reply.getWorkItemServiceStates().get(i); WorkItemStatus status = request.getWorkItemStatuses().get(i); if (state.getMetricShortId() == null) { continue; } checkArgument( status.getCounterUpdates() != null, "Response has shortids but no corresponding CounterUpdate"); for (MetricShortId shortIdMsg : state.getMetricShortId()) { int metricIndex = MoreObjects.firstNonNull(shortIdMsg.getMetricIndex(), 0); checkArgument( metricIndex < status.getCounterUpdates().size(), "Received aggregate index outside range of sent update %s >= %s", shortIdMsg.getMetricIndex(), status.getCounterUpdates().size()); CounterUpdate update = status.getCounterUpdates().get(metricIndex); cache.insert(update, checkNotNull(shortIdMsg.getShortId(), "Shortid should be non-null")); } } }  void storeNewShortIds(
      final ReportWorkItemStatusRequest request, final ReportWorkItemStatusResponse reply); void shortenIdsIfAvailable(java.util.@Nullable List<CounterUpdate> counters);  }
@Test public void testValidateNumberStatusesAndStates() { CounterShortIdCache cache = new CounterShortIdCache(); ReportWorkItemStatusRequest request = new ReportWorkItemStatusRequest(); ReportWorkItemStatusResponse reply = new ReportWorkItemStatusResponse(); request.setWorkItemStatuses( createWorkStatusNameAndKind(new String[] {"counter"}, new String[] {"counter2"})); reply.setWorkItemServiceStates( createWorkServiceState(new MetricShortId[] {createMetricShortId(0, 1000L)})); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("RequestWorkItemStatus request and response are unbalanced"); cache.storeNewShortIds(request, reply); }
@Test public void testValidateShortIdsButNoUpdate() { CounterShortIdCache cache = new CounterShortIdCache(); ReportWorkItemStatusRequest request = new ReportWorkItemStatusRequest(); ReportWorkItemStatusResponse reply = new ReportWorkItemStatusResponse(); request.setWorkItemStatuses(Arrays.asList(new WorkItemStatus())); reply.setWorkItemServiceStates(createWorkServiceState(new Long[] {1000L})); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Response has shortids but no corresponding CounterUpdate"); cache.storeNewShortIds(request, reply); }
@Test public void testValidateAggregateIndexOutOfRange() { CounterShortIdCache cache = new CounterShortIdCache(); ReportWorkItemStatusRequest request = new ReportWorkItemStatusRequest(); ReportWorkItemStatusResponse reply = new ReportWorkItemStatusResponse(); request.setWorkItemStatuses(createWorkStatusNameAndKind(new String[] {"counter"})); reply.setWorkItemServiceStates( createWorkServiceState(new MetricShortId[] {createMetricShortId(1000, 1000L)})); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("Received aggregate index outside range of sent update"); cache.storeNewShortIds(request, reply); }