@Test
public void testGetResourcesWithCluster() throws Exception {

  Resource.Type type = Resource.Type.Request;
  AmbariManagementController managementController = createMock(AmbariManagementController.class);
  ActionManager actionManager = createNiceMock(ActionManager.class);
  Clusters clusters = createNiceMock(Clusters.class);
  Cluster cluster = createNiceMock(Cluster.class);
  expect(cluster.getClusterId()).andReturn(50L).anyTimes();
  RequestEntity requestMock = createNiceMock(RequestEntity.class);
  expect(requestMock.getRequestContext()).andReturn("this is a context").anyTimes();
  expect(requestMock.getClusterId()).andReturn(50L).anyTimes();
  expect(requestMock.getRequestId()).andReturn(100L).anyTimes();
  Capture<Collection<Long>> requestIdsCapture = newCapture();
  // set expectations
  expect(managementController.getActionManager()).andReturn(actionManager).anyTimes();
  expect(managementController.getClusters()).andReturn(clusters).anyTimes();
  expect(clusters.getCluster("c1")).andReturn(cluster).anyTimes();
  expect(clusters.getCluster("bad-cluster")).andThrow(new AmbariException("bad cluster!")).anyTimes();
  expect(requestDAO.findByPks(capture(requestIdsCapture), eq(true))).andReturn(Collections.singletonList(requestMock));
  expect(hrcDAO.findAggregateCounts((Long) anyObject())).andReturn(new HashMap<Long, HostRoleCommandStatusSummaryDTO>(){{
    put(1L, HostRoleCommandStatusSummaryDTO.create().inProgress(1));
  }}).anyTimes();
  // replay
  replay(managementController, actionManager, clusters, cluster, requestMock, requestDAO, hrcDAO);
  ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider(
    type,
    PropertyHelper.getPropertyIds(type),
    PropertyHelper.getKeyPropertyIds(type),
    managementController);
  Set<String> propertyIds = new HashSet<String>();
  propertyIds.add(RequestResourceProvider.REQUEST_ID_PROPERTY_ID);
  propertyIds.add(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID);
  Predicate predicate = new PredicateBuilder().
    property(RequestResourceProvider.REQUEST_CLUSTER_NAME_PROPERTY_ID).equals("c1").and().
    property(RequestResourceProvider.REQUEST_ID_PROPERTY_ID).equals("100").
    toPredicate();
  Request request = PropertyHelper.getReadRequest(propertyIds);
  Set<Resource> resources = provider.getResources(request, predicate);
  Assert.assertEquals(1, resources.size());
  for (Resource resource : resources) {
    Assert.assertEquals(100L, (long) (Long) resource.getPropertyValue(RequestResourceProvider.REQUEST_ID_PROPERTY_ID));
    Assert.assertEquals("IN_PROGRESS", resource.getPropertyValue(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID));
  }
  // try again with a bad cluster name
  predicate = new PredicateBuilder().
    property(RequestResourceProvider.REQUEST_CLUSTER_NAME_PROPERTY_ID).equals("bad-cluster").and().
    property(RequestResourceProvider.REQUEST_ID_PROPERTY_ID).equals("100").
    toPredicate();
  try {
    provider.getResources(request, predicate);
  } catch (NoSuchParentResourceException e) {
    e.printStackTrace();
  }
  // verify
  verify(managementController, actionManager, clusters, cluster, requestMock, requestDAO, hrcDAO);
}