@Test
public void testTimelineMetricCacheProviderGets() throws Exception {

  Configuration configuration = createNiceMock(Configuration.class);
  expect(configuration.getMetricCacheTTLSeconds()).andReturn(3600);
  expect(configuration.getMetricCacheIdleSeconds()).andReturn(100);
  expect(configuration.getMetricsCacheManagerHeapPercent()).andReturn("10%").anyTimes();
  replay(configuration);
  final long now = System.currentTimeMillis();
  TimelineMetrics metrics = new TimelineMetrics();
  TimelineMetric timelineMetric = new TimelineMetric();
  timelineMetric.setMetricName("cpu_user");
  timelineMetric.setAppId("app1");
  TreeMap<Long, Double> metricValues = new TreeMap<Long, Double>();
  metricValues.put(now + 100, 1.0);
  metricValues.put(now + 200, 2.0);
  metricValues.put(now + 300, 3.0);
  timelineMetric.setMetricValues(metricValues);
  metrics.getMetrics().add(timelineMetric);
  TimelineMetricCacheEntryFactory cacheEntryFactory = createMock(TimelineMetricCacheEntryFactory.class);
  TimelineAppMetricCacheKey queryKey = new TimelineAppMetricCacheKey(
    Collections.singleton("cpu_user"),
    "app1",
    new TemporalInfoImpl(now, now + 1000, 1)
  );
  TimelineMetricsCacheValue value = new TimelineMetricsCacheValue(now, now + 1000, metrics, null);
  TimelineAppMetricCacheKey testKey = new TimelineAppMetricCacheKey(
    Collections.singleton("cpu_user"),
    "app1",
    new TemporalInfoImpl(now, now + 2000, 1)
  );
  expect(cacheEntryFactory.createEntry(anyObject())).andReturn(value);
  cacheEntryFactory.updateEntryValue(testKey, value);
  expectLastCall().once();
  replay(cacheEntryFactory);
  TimelineMetricCacheProvider cacheProvider = createMockBuilder(TimelineMetricCacheProvider.class)
    .addMockedMethod("createCacheConfiguration")
    .withConstructor(configuration, cacheEntryFactory)
    .createNiceMock();
  expect(cacheProvider.createCacheConfiguration()).andReturn(createTestCacheConfiguration(configuration)).anyTimes();
  replay(cacheProvider);
  TimelineMetricCache cache = cacheProvider.getTimelineMetricsCache();
  // call to get
  metrics = cache.getAppTimelineMetricsFromCache(queryKey);
  List<TimelineMetric> metricsList = metrics.getMetrics();
  Assert.assertEquals(1, metricsList.size());
  TimelineMetric metric = metricsList.iterator().next();
  Assert.assertEquals("cpu_user", metric.getMetricName());
  Assert.assertEquals("app1", metric.getAppId());
  Assert.assertSame(metricValues, metric.getMetricValues());
  // call to update with new key
  metrics = cache.getAppTimelineMetricsFromCache(testKey);
  metricsList = metrics.getMetrics();
  Assert.assertEquals(1, metricsList.size());
  Assert.assertEquals("cpu_user", metric.getMetricName());
  Assert.assertEquals("app1", metric.getAppId());
  Assert.assertSame(metricValues, metric.getMetricValues());
  verify(configuration, cacheEntryFactory);
}