@Test
public void testGetResources() throws Exception{

  Injector injector = createInjector();
  AmbariManagementController managementController = injector.getInstance(AmbariManagementController.class);
  Clusters clusters = injector.getInstance(Clusters.class);
  UpgradeHelper upgradeHelper = injector.getInstance(UpgradeHelper.class);
  RepositoryVersionDAO repoDao = injector.getInstance(RepositoryVersionDAO.class);
  RepositoryVersionEntity repo = createNiceMock(RepositoryVersionEntity.class);
  UpgradePack upgradePack = createNiceMock(UpgradePack.class);
  PrerequisiteCheckConfig config = createNiceMock(PrerequisiteCheckConfig.class);
  Cluster cluster = createNiceMock(Cluster.class);
  Service service = createNiceMock(Service.class);
  ServiceInfo serviceInfo = createNiceMock(ServiceInfo.class);
  StackId currentStackId = createNiceMock(StackId.class);
  StackId targetStackId = createNiceMock(StackId.class);
  ServiceFactory serviceFactory = createNiceMock(ServiceFactory.class);
  AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
  Map<String, Service> allServiceMap = new HashMap<String, Service>();
  allServiceMap.put("Service100", service);
  Map<String, ServiceInfo> allServiceInfoMap = new HashMap<String, ServiceInfo>();
  allServiceInfoMap.put("Service100", serviceInfo);
  // set expectations
  expect(managementController.getClusters()).andReturn(clusters).anyTimes();
  expect(managementController.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes();
  expect(clusters.getCluster("Cluster100")).andReturn(cluster).anyTimes();
  expect(cluster.getServices()).andReturn(allServiceMap).anyTimes();
  expect(cluster.getService("Service100")).andReturn(service).anyTimes();
  expect(cluster.getCurrentStackVersion()).andReturn(currentStackId).anyTimes();
  expect(currentStackId.getStackName()).andReturn("Stack100").anyTimes();
  expect(currentStackId.getStackVersion()).andReturn("1.0").anyTimes();
  expect(targetStackId.getStackName()).andReturn("Stack100").anyTimes();
  expect(targetStackId.getStackVersion()).andReturn("1.1").anyTimes();
  expect(repoDao.findByStackNameAndVersion("Stack100", "Repo100")).andReturn(repo).anyTimes();
  expect(repo.getStackId()).andReturn(targetStackId).atLeastOnce();
  expect(upgradeHelper.suggestUpgradePack("Cluster100", "1.0", "Repo100", Direction.UPGRADE, UpgradeType.NON_ROLLING, "upgrade_pack11")).andReturn(upgradePack);
  List<AbstractCheckDescriptor> upgradeChecksToRun = new LinkedList<AbstractCheckDescriptor>();
  List<String> prerequisiteChecks = new LinkedList<String>();
  prerequisiteChecks.add("org.apache.ambari.server.sample.checks.SampleServiceCheck");
  expect(upgradePack.getPrerequisiteCheckConfig()).andReturn(config);
  expect(upgradePack.getPrerequisiteChecks()).andReturn(prerequisiteChecks).anyTimes();
  expect(upgradePack.getTarget()).andReturn("1.1.*.*").anyTimes();
  expect(ambariMetaInfo.getServices("Stack100", "1.0")).andReturn(allServiceInfoMap).anyTimes();
  String checks = ClassLoader.getSystemClassLoader().getResource("checks").getPath();
  expect(serviceInfo.getChecksFolder()).andReturn(new File(checks));
  // replay
  replay(managementController, clusters, cluster, service, serviceInfo, repoDao, repo, upgradeHelper,
      ambariMetaInfo, upgradePack, config, currentStackId, targetStackId, serviceFactory);
  ResourceProvider provider = getPreUpgradeCheckResourceProvider(managementController, injector);
  // create the request
  Request request = PropertyHelper.getReadRequest(new HashSet<String>());
  PredicateBuilder builder = new PredicateBuilder();
  Predicate predicate = builder.property(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_CLUSTER_NAME_PROPERTY_ID).equals("Cluster100").and()
      .property(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_UPGRADE_PACK_PROPERTY_ID).equals("upgrade_pack11").and()
      .property(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_UPGRADE_TYPE_PROPERTY_ID).equals(UpgradeType.NON_ROLLING).and()
      .property(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_REPOSITORY_VERSION_PROPERTY_ID).equals("Repo100").toPredicate();
  System.out.println("PreUpgradeCheckResourceProvider - " + provider);
  Set<Resource> resources = Collections.emptySet();
  try {
    resources = provider.getResources(request, predicate);
  }
  catch (Exception e) {
    e.printStackTrace();
  }
  Assert.assertEquals(1, resources.size());
  for (Resource resource : resources) {
    String id = (String) resource.getPropertyValue(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_ID_PROPERTY_ID);
    Assert.assertEquals("SAMPLE_SERVICE_CHECK", id);
    String description = (String) resource.getPropertyValue(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_CHECK_PROPERTY_ID);
    Assert.assertEquals("Sample service check description.", description);
    PrereqCheckStatus status = (PrereqCheckStatus) resource.getPropertyValue(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_STATUS_PROPERTY_ID);
    Assert.assertEquals(PrereqCheckStatus.FAIL, status);
    String reason = (String) resource.getPropertyValue(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_REASON_PROPERTY_ID);
    Assert.assertEquals("Sample service check always fails.", reason);
    PrereqCheckType checkType = (PrereqCheckType) resource.getPropertyValue(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_CHECK_TYPE_PROPERTY_ID);
    Assert.assertEquals(PrereqCheckType.HOST, checkType);
    String clusterName = (String) resource.getPropertyValue(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_CLUSTER_NAME_PROPERTY_ID);
    Assert.assertEquals("Cluster100", clusterName);
    UpgradeType upgradeType = (UpgradeType) resource.getPropertyValue(PreUpgradeCheckResourceProvider.UPGRADE_CHECK_UPGRADE_TYPE_PROPERTY_ID);
    Assert.assertEquals(UpgradeType.NON_ROLLING, upgradeType);
  }
  // verify
  verify(managementController, clusters, cluster, service, serviceInfo, repoDao, repo, upgradeHelper,
          ambariMetaInfo, upgradePack, config, currentStackId, targetStackId, serviceFactory);
}