### Question:
TimeSeriesDataSource { @VisibleForTesting String getResponse( @Nullable String metricsQuery, @Nullable String sinceQuery) throws MetricException { if (metricsQuery == null) { return gson.toJson(ImmutableList.copyOf(timeSeriesRepo.getAvailableSeries())); } List<Iterable<Number>> tsData = Lists.newArrayList(); tsData.add(timeSeriesRepo.getTimestamps()); Iterable<String> names = Iterables.filter( Splitter.on(",").split(metricsQuery), Predicates.not(Predicates.equalTo(TIME_METRIC))); for (String metric : names) { TimeSeries series = timeSeriesRepo.get(metric); if (series == null) { JsonObject response = new JsonObject(); response.addProperty("error", "Unknown metric " + metric); throw new MetricException(gson.toJson(response)); } tsData.add(series.getSamples()); } final long since = Long.parseLong(Optional.ofNullable(sinceQuery).orElse("0")); Predicate<List<Number>> sinceFilter = next -> next.get(0).longValue() > since; ResponseStruct response = new ResponseStruct( ImmutableList.<String>builder().add(TIME_METRIC).addAll(names).build(), FluentIterable.from(Iterables2.zip(tsData, 0)).filter(sinceFilter).toList()); return gson.toJson(response); } @Inject  TimeSeriesDataSource(TimeSeriesRepository timeSeriesRepo); @GET @Produces(MediaType.APPLICATION_JSON) String getData(
      @QueryParam(METRICS) String metrics,
      @QueryParam(SINCE) String since);  }### Answer:
@Test public void testGetColumns() throws Exception { expect(timeSeriesRepo.getAvailableSeries()).andReturn(TS_DATA.keySet()); control.replay(); List<String> columns = gson.fromJson( dataSource.getResponse(null, null), new TypeToken<List<String>>() { }.getType()); assertEquals(ImmutableList.copyOf(TS_DATA.keySet()), columns); }
@Test @SuppressWarnings("unchecked") public void testGetAllData() throws Exception { expect(timeSeriesRepo.getTimestamps()).andReturn(TIMESTAMPS); expect(timeSeriesRepo.get(TIME_SERIES_1)).andReturn(TS_DATA.get(TIME_SERIES_1)); expect(timeSeriesRepo.get(TIME_SERIES_2)).andReturn(TS_DATA.get(TIME_SERIES_2)); control.replay(); String colString = Joiner.on(',').join( Arrays.asList(TIME_SERIES_1, TIME_SERIES_2, TIME_COLUMN)); TimeSeriesDataSource.ResponseStruct response = gson.fromJson( dataSource.getResponse(colString, null), TimeSeriesDataSource.ResponseStruct.class); assertEquals(ImmutableList.of(TIME_COLUMN, TIME_SERIES_1, TIME_SERIES_2), response.names); Iterable<List<Number>> expectedData = Iterables2.zip(0, TIMESTAMPS, getSamples(TIME_SERIES_1), getSamples(TIME_SERIES_2)); checkRows(expectedData, response.data); }
@Test @SuppressWarnings("unchecked") public void testFilterByTime() throws Exception { expect(timeSeriesRepo.getTimestamps()).andReturn(TIMESTAMPS); expect(timeSeriesRepo.get(TIME_SERIES_1)).andReturn(TS_DATA.get(TIME_SERIES_1)); expect(timeSeriesRepo.get(TIME_SERIES_2)).andReturn(TS_DATA.get(TIME_SERIES_2)); control.replay(); String colString = Joiner.on(',').join( Arrays.asList(TIME_SERIES_1, TIME_SERIES_2, TIME_COLUMN)); TimeSeriesDataSource.ResponseStruct response = gson.fromJson( dataSource.getResponse(colString, "2"), TimeSeriesDataSource.ResponseStruct.class); Iterable<List<Number>> expectedData = Iterables2.zip(0, TIMESTAMPS, getSamples(TIME_SERIES_1), getSamples(TIME_SERIES_2)); expectedData = Iterables.filter(expectedData, row -> row.get(0).intValue() >= 3); checkRows(expectedData, response.data); }