private void testGetResources(Authentication authentication) throws Exception {
    Resource.Type type = Resource.Type.Component;

    MaintenanceStateHelper maintenanceStateHelper = createNiceMock(MaintenanceStateHelper.class);
    AmbariManagementController managementController = createMock(AmbariManagementController.class);
    Clusters clusters = createNiceMock(Clusters.class);
    Cluster cluster = createNiceMock(Cluster.class);
    AmbariMetaInfo ambariMetaInfo = createNiceMock(AmbariMetaInfo.class);
    Service service = createNiceMock(Service.class);
    ServiceComponent serviceComponent1 = createNiceMock(ServiceComponent.class);
    ServiceComponent serviceComponent2 = createNiceMock(ServiceComponent.class);
    ServiceComponent serviceComponent3 = createNiceMock(ServiceComponent.class);
    StackId stackId = new StackId("FOO-1.0");
    final ComponentInfo componentInfo1 = createNiceMock(ComponentInfo.class);
    final ComponentInfo componentInfo2 = createNiceMock(ComponentInfo.class);
    Map <String, Integer> serviceComponentStateCountMap = new HashMap<>();
    serviceComponentStateCountMap.put("startedCount", 1);
    serviceComponentStateCountMap.put("installedCount", 0);
    serviceComponentStateCountMap.put("installedAndMaintenanceOffCount", 0);
    serviceComponentStateCountMap.put("installFailedCount", 0);
    serviceComponentStateCountMap.put("initCount", 0);
    serviceComponentStateCountMap.put("unknownCount", 1);
    serviceComponentStateCountMap.put("totalCount", 2);

    Map<String, ServiceComponent> serviceComponentMap = new HashMap<>();
    serviceComponentMap.put("Component101", serviceComponent1);
    serviceComponentMap.put("Component102", serviceComponent2);
    serviceComponentMap.put("Component103", serviceComponent3);

    // set expectations
    expect(managementController.getClusters()).andReturn(clusters);
    expect(managementController.getAmbariMetaInfo()).andReturn(ambariMetaInfo);
    expect(clusters.getCluster("Cluster100")).andReturn(cluster).anyTimes();
    expect(serviceComponent1.getName()).andReturn("Component100");
    expect(serviceComponent1.getDesiredStackId()).andReturn(stackId).anyTimes();
    expect(serviceComponent2.getName()).andReturn("Component101");
    expect(serviceComponent2.getDesiredStackId()).andReturn(stackId).anyTimes();
    expect(serviceComponent3.getName()).andReturn("Component102");
    expect(serviceComponent3.getDesiredStackId()).andReturn(stackId).anyTimes();

    expect(cluster.getServices()).andReturn(Collections.singletonMap("Service100", service)).anyTimes();

    expect(service.getServiceComponents()).andReturn(serviceComponentMap).anyTimes();

    expect(serviceComponent1.convertToResponse()).andReturn(
      new ServiceComponentResponse(100L, "Cluster100", "Service100", "Component100", stackId, "", serviceComponentStateCountMap,
              true /* recovery enabled */, "Component100 Client", null, null));
    expect(serviceComponent2.convertToResponse()).andReturn(
      new ServiceComponentResponse(100L, "Cluster100", "Service100", "Component101", stackId, "", serviceComponentStateCountMap,
              false /* recovery not enabled */, "Component101 Client", null, null));
    expect(serviceComponent3.convertToResponse()).andReturn(
      new ServiceComponentResponse(100L, "Cluster100", "Service100", "Component102", stackId, "", serviceComponentStateCountMap,
              true /* recovery enabled */, "Component102 Client", "1.1", RepositoryVersionState.CURRENT));

    expect(ambariMetaInfo.getComponent("FOO", "1.0", null, "Component100")).andReturn(
        componentInfo1);
    expect(ambariMetaInfo.getComponent("FOO", "1.0", null, "Component101")).andReturn(
        componentInfo2);
    expect(ambariMetaInfo.getComponent("FOO", "1.0", null, "Component102")).andReturn(
        componentInfo1);

    expect(componentInfo1.getCategory()).andReturn("MASTER").anyTimes();
    expect(componentInfo2.getCategory()).andReturn("SLAVE").anyTimes();

    // replay
    replay(managementController, clusters, cluster, ambariMetaInfo, service,
        serviceComponent1, serviceComponent2, serviceComponent3,
      componentInfo1, componentInfo2);

    SecurityContextHolder.getContext().setAuthentication(authentication);

    ResourceProvider provider = new ComponentResourceProvider(managementController,
        maintenanceStateHelper);

    Set<String> propertyIds = new HashSet<>();

    propertyIds.add(ComponentResourceProvider.CLUSTER_NAME);
    propertyIds.add(ComponentResourceProvider.COMPONENT_NAME);
    propertyIds.add(ComponentResourceProvider.CATEGORY);
    propertyIds.add(ComponentResourceProvider.TOTAL_COUNT);
    propertyIds.add(ComponentResourceProvider.STARTED_COUNT);
    propertyIds.add(ComponentResourceProvider.INSTALLED_COUNT);
    propertyIds.add(ComponentResourceProvider.INSTALLED_AND_MAINTENANCE_OFF_COUNT);
    propertyIds.add(ComponentResourceProvider.INSTALL_FAILED_COUNT);
    propertyIds.add(ComponentResourceProvider.INIT_COUNT);
    propertyIds.add(ComponentResourceProvider.UNKNOWN_COUNT);
    propertyIds.add(ComponentResourceProvider.RECOVERY_ENABLED);
    propertyIds.add(ComponentResourceProvider.DESIRED_VERSION);
    propertyIds.add(ComponentResourceProvider.REPOSITORY_STATE);

    Predicate predicate = new PredicateBuilder()
      .property(ComponentResourceProvider.CLUSTER_NAME)
      .equals("Cluster100")
      .and()
      .property(ComponentResourceProvider.CATEGORY)
      .equals("MASTER").toPredicate();

    Request request = PropertyHelper.getReadRequest(propertyIds);
    Set<Resource> resources = provider.getResources(request, predicate);

    Assert.assertEquals(2, resources.size());
    for (Resource resource : resources) {
      String clusterName = (String) resource.getPropertyValue(
          ComponentResourceProvider.CLUSTER_NAME);
      Assert.assertEquals("Cluster100", clusterName);
      Assert.assertEquals("MASTER", resource.getPropertyValue(
          ComponentResourceProvider.CATEGORY));
      Assert.assertEquals(2, resource.getPropertyValue(
        ComponentResourceProvider.TOTAL_COUNT));
      Assert.assertEquals(1, resource.getPropertyValue(
        ComponentResourceProvider.STARTED_COUNT));
      Assert.assertEquals(0, resource.getPropertyValue(
        ComponentResourceProvider.INSTALLED_COUNT));
      Assert.assertEquals(0, resource.getPropertyValue(
        ComponentResourceProvider.INSTALLED_AND_MAINTENANCE_OFF_COUNT));
      Assert.assertEquals(0, resource.getPropertyValue(
          ComponentResourceProvider.INSTALL_FAILED_COUNT));
      Assert.assertEquals(0, resource.getPropertyValue(
          ComponentResourceProvider.INIT_COUNT));
      Assert.assertEquals(1, resource.getPropertyValue(
          ComponentResourceProvider.UNKNOWN_COUNT));
      Assert.assertEquals(String.valueOf(true), resource.getPropertyValue(
        ComponentResourceProvider.RECOVERY_ENABLED));

      if (resource.getPropertyValue(
          ComponentResourceProvider.COMPONENT_NAME).equals("Component102")) {
        Assert.assertNotNull(resource.getPropertyValue(ComponentResourceProvider.REPOSITORY_STATE));
        Assert.assertNotNull(resource.getPropertyValue(ComponentResourceProvider.DESIRED_VERSION));
        Assert.assertEquals(RepositoryVersionState.CURRENT, resource.getPropertyValue(ComponentResourceProvider.REPOSITORY_STATE));
        Assert.assertEquals("1.1", resource.getPropertyValue(ComponentResourceProvider.DESIRED_VERSION));
      } else {
        Assert.assertNull(resource.getPropertyValue(ComponentResourceProvider.REPOSITORY_STATE));
        Assert.assertNull(resource.getPropertyValue(ComponentResourceProvider.DESIRED_VERSION));
      }
    }

    // verify
    verify(managementController, clusters, cluster, ambariMetaInfo, service, serviceComponent1,
        serviceComponent2, serviceComponent3, componentInfo1, componentInfo2);
  }
Tests for the component resource provider.
ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ComponentResourceProviderTest.java