@Test
public void testPopulateResourcesForHostComponentHostMetrics() throws Exception {

  setUpCommonMocks();
  TestStreamProviderForHostComponentHostMetricsTest streamProvider =
    new TestStreamProviderForHostComponentHostMetricsTest(null);
  injectCacheEntryFactoryWithStreamProvider(streamProvider);
  TestMetricHostProvider metricHostProvider = new TestMetricHostProvider();
  ComponentSSLConfiguration sslConfiguration = mock(ComponentSSLConfiguration.class);
  TimelineMetricCacheProvider cacheProviderMock = EasyMock.createMock(TimelineMetricCacheProvider.class);
  TimelineMetricCache cacheMock = EasyMock.createMock(TimelineMetricCache.class);
  expect(cacheProviderMock.getTimelineMetricsCache()).andReturn(cacheMock).anyTimes();
  Map<String, Map<String, PropertyInfo>> propertyIds = PropertyHelper.getMetricPropertyIds(Resource.Type.HostComponent);
  AMSPropertyProvider propertyProvider = new AMSHostComponentPropertyProvider(
    propertyIds,
    streamProvider,
    sslConfiguration,
    cacheProviderMock,
    metricHostProvider,
    CLUSTER_NAME_PROPERTY_ID,
    HOST_NAME_PROPERTY_ID,
    COMPONENT_NAME_PROPERTY_ID
  );
  Resource resource = new ResourceImpl(Resource.Type.Host);
  resource.setProperty(CLUSTER_NAME_PROPERTY_ID, "c1");
  resource.setProperty(HOST_NAME_PROPERTY_ID, "h1");
  resource.setProperty(COMPONENT_NAME_PROPERTY_ID, "DATANODE");
  Map<String, TemporalInfo> temporalInfoMap = new HashMap<String, TemporalInfo>();
  // Set same time ranges to make sure the query comes in as grouped and
  // then turns into a separate query to the backend
  temporalInfoMap.put(PROPERTY_ID1, new TemporalInfoImpl(1416445244801L, 1416448936464L, 1L));
  temporalInfoMap.put(PROPERTY_ID3, new TemporalInfoImpl(1416445244801L, 1416448936464L, 1L));
  Request request = PropertyHelper.getReadRequest(
    new HashSet<String>() {{
      add(PROPERTY_ID1);
      add(PROPERTY_ID3);
      add("params/padding/NONE"); // Ignore padding to match result size
    }},
    temporalInfoMap);
  Set<Resource> resources =
    propertyProvider.populateResources(Collections.singleton(resource), request, null);
  Assert.assertEquals(1, resources.size());
  Resource res = resources.iterator().next();
  Map<String, Object> properties = PropertyHelper.getProperties(resources.iterator().next());
  Assert.assertNotNull(properties);
  Set<String> specs = streamProvider.getAllSpecs();
  Assert.assertEquals(2, specs.size());
  String hostMetricSpec = null;
  String hostComponentMetricsSpec = null;
  for (String spec : specs) {
    if (spec.contains("HOST")) {
      hostMetricSpec = spec;
    } else {
      hostComponentMetricsSpec = spec;
    }
  }
  Assert.assertNotNull(hostMetricSpec);
  Assert.assertNotNull(hostComponentMetricsSpec);
  // Verify calls
  URIBuilder uriBuilder1 = AMSPropertyProvider.getAMSUriBuilder("localhost", 6188, false);
  uriBuilder1.addParameter("metricNames", "dfs.datanode.BlocksReplicated");
  uriBuilder1.addParameter("hostname", "h1");
  uriBuilder1.addParameter("appId", "DATANODE");
  uriBuilder1.addParameter("startTime", "1416445244801");
  uriBuilder1.addParameter("endTime", "1416448936464");
  Assert.assertEquals(uriBuilder1.toString(), hostComponentMetricsSpec);
  URIBuilder uriBuilder2 = AMSPropertyProvider.getAMSUriBuilder("localhost", 6188, false);
  uriBuilder2.addParameter("metricNames", "cpu_user");
  uriBuilder2.addParameter("hostname", "h1");
  uriBuilder2.addParameter("appId", "HOST");
  uriBuilder2.addParameter("startTime", "1416445244801");
  uriBuilder2.addParameter("endTime", "1416448936464");
  Assert.assertEquals(uriBuilder2.toString(), hostMetricSpec);
  Number[][] val = (Number[][]) res.getPropertyValue(PROPERTY_ID1);
  Assert.assertEquals(111, val.length);
  val = (Number[][]) res.getPropertyValue(PROPERTY_ID3);
  Assert.assertNotNull("No value for property " + PROPERTY_ID3, val);
  Assert.assertEquals(8, val.length);
}