@Test
  public void testCreateResourcesInInstalledState() throws Exception {
    Cluster cluster = createNiceMock(Cluster.class);
    StackId stackId = new StackId("HDP", "2.2.0");
    String repoVersion = "2.2.0.1-885";

    File f = new File("src/test/resources/hbase_version_test.xml");

    StackEntity stackEntity = new StackEntity();
    stackEntity.setStackName(stackId.getStackName());
    stackEntity.setStackVersion(stackId.getStackVersion());

    RepositoryVersionEntity repoVersionEntity = new RepositoryVersionEntity();
    repoVersionEntity.setId(1l);
    repoVersionEntity.addRepoOsEntities(REPO_OS_ENTITIES);
    repoVersionEntity.setVersionXml(IOUtils.toString(new FileInputStream(f)));
    repoVersionEntity.setVersionXsd("version_definition.xsd");
    repoVersionEntity.setType(RepositoryType.STANDARD);
    repoVersionEntity.setVersion(repoVersion);
    repoVersionEntity.setStack(stackEntity);

    List<Host> hostsNeedingInstallCommands = new ArrayList<>();
    Map<String, Host> hostsForCluster = new HashMap<>();
    List<HostVersionEntity> hostVersionEntitiesMergedWithNotRequired = new ArrayList<>();
    int hostCount = 10;

    for (int i = 0; i < hostCount; i++) {
      String hostname = "host" + i;
      Host host = createNiceMock(hostname, Host.class);
      expect(host.getHostName()).andReturn(hostname).anyTimes();
      expect(host.getOsFamily()).andReturn("redhat6").anyTimes();
      expect(host.getMaintenanceState(EasyMock.anyLong())).andReturn(MaintenanceState.OFF).anyTimes();

      // ensure that 2 hosts don't have versionable components so they
      // transition correct into the not required state
      if (i < hostCount - 2) {
        expect(host.hasComponentsAdvertisingVersions(eq(stackId))).andReturn(true).atLeastOnce();
        hostsNeedingInstallCommands.add(host);
        expect(host.getAllHostVersions()).andReturn(Collections.emptyList()).anyTimes();
      } else {
        expect(host.hasComponentsAdvertisingVersions(eq(stackId))).andReturn(false).atLeastOnce();

        // mock out the host versions so that we can test hosts being
        // transitioned into NOT_REQUIRED
        HostVersionEntity hostVersionEntity = EasyMock.createNiceMock(HostVersionEntity.class);
        expect(hostVersionEntity.getRepositoryVersion()).andReturn(repoVersionEntity).atLeastOnce();
        replay(hostVersionEntity);

        hostVersionEntitiesMergedWithNotRequired.add(hostVersionEntity);
        expect(host.getAllHostVersions()).andReturn(hostVersionEntitiesMergedWithNotRequired).anyTimes();
      }

      replay(host);

      hostsForCluster.put(hostname, host);
    }

    Service hdfsService = createNiceMock(Service.class);
    expect(hdfsService.getName()).andReturn("HDFS").anyTimes();
    expect(hdfsService.getServiceComponents()).andReturn(new HashMap<>());

    Map<String, Service> serviceMap = new HashMap<>();
    serviceMap.put("HDFS", hdfsService);

    final ServiceComponentHost schDatanode = createMock(ServiceComponentHost.class);
    expect(schDatanode.getServiceName()).andReturn("HDFS").anyTimes();
    expect(schDatanode.getServiceComponentName()).andReturn("DATANODE").anyTimes();

    final List<ServiceComponentHost> serviceComponentHosts = Arrays.asList(schDatanode);

    ServiceOsSpecific.Package hdfsPackage = new ServiceOsSpecific.Package();
    hdfsPackage.setName("hdfs");

    List<ServiceOsSpecific.Package> packages = Collections.singletonList(hdfsPackage);

    RequestStatusResponse response = createNiceMock(RequestStatusResponse.class);
    ResourceProviderFactory resourceProviderFactory = createNiceMock(ResourceProviderFactory.class);
    ResourceProvider csvResourceProvider = createNiceMock(ResourceProvider.class);

    AbstractControllerResourceProvider.init(resourceProviderFactory);

    expect(managementController.getClusters()).andReturn(clusters).anyTimes();
    expect(managementController.getAmbariMetaInfo()).andReturn(ambariMetaInfo).anyTimes();
    expect(managementController.getAuthName()).andReturn("admin").anyTimes();
    expect(managementController.getJdkResourceUrl()).andReturn("/JdkResourceUrl").anyTimes();
    expect(managementController.getPackagesForServiceHost(anyObject(ServiceInfo.class),
        EasyMock.anyObject(), anyObject(String.class))).andReturn(
            packages).anyTimes(); // only one host has the versionable component

    expect(resourceProviderFactory.getHostResourceProvider(eq(managementController))).andReturn(csvResourceProvider).anyTimes();

    expect(clusters.getCluster(anyObject(String.class))).andReturn(cluster);
    expect(clusters.getHostsForCluster(anyObject(String.class))).andReturn(
        hostsForCluster).anyTimes();

    String clusterName = "Cluster100";
    expect(cluster.getClusterId()).andReturn(1L).anyTimes();
    expect(cluster.getHosts()).andReturn(hostsForCluster.values()).atLeastOnce();
    expect(cluster.getServices()).andReturn(serviceMap).anyTimes();
    expect(cluster.getServiceComponentHosts(anyObject(String.class))).andReturn(serviceComponentHosts).anyTimes();
    expect(cluster.getCurrentStackVersion()).andReturn(stackId).atLeastOnce();

    expect(repositoryVersionDAOMock.findByStackAndVersion(anyObject(StackId.class),
        anyObject(String.class))).andReturn(repoVersionEntity);

    ClusterEntity clusterEntity = new ClusterEntity();
    clusterEntity.setClusterId(1l);
    clusterEntity.setClusterName(clusterName);


    // now the important expectations - that the cluster transition methods were
    // called correctly
    expect(cluster.transitionHostsToInstalling(repoVersionEntity,
        repoVersionEntity.getRepositoryXml(), true)).andReturn(
            hostsNeedingInstallCommands).once();

    // replay
    replay(managementController, response, clusters, hdfsService, resourceProviderFactory,
        csvResourceProvider, cluster, repositoryVersionDAOMock, configHelper, schDatanode,
        stageFactory, hostVersionDAO);

    ResourceProvider provider = createProvider(managementController);
    injector.injectMembers(provider);

    // add the property map to a set for the request. add more maps for multiple
    // creates
    Set<Map<String, Object>> propertySet = new LinkedHashSet<>();

    Map<String, Object> properties = new LinkedHashMap<>();

    // add properties to the request map
    properties.put(
        ClusterStackVersionResourceProvider.CLUSTER_STACK_VERSION_CLUSTER_NAME_PROPERTY_ID,
        clusterName);

    properties.put(
        ClusterStackVersionResourceProvider.CLUSTER_STACK_VERSION_REPOSITORY_VERSION_PROPERTY_ID,
        repoVersion);

    properties.put(ClusterStackVersionResourceProvider.CLUSTER_STACK_VERSION_STACK_PROPERTY_ID,
        stackId.getStackName());

    properties.put(ClusterStackVersionResourceProvider.CLUSTER_STACK_VERSION_VERSION_PROPERTY_ID,
        stackId.getStackVersion());

    properties.put(ClusterStackVersionResourceProvider.CLUSTER_STACK_VERSION_FORCE, "true");

    propertySet.add(properties);

    // set the security auth
    SecurityContextHolder.getContext().setAuthentication(
        TestAuthenticationFactory.createAdministrator());

    // create the request
    Request request = PropertyHelper.getCreateRequest(propertySet, null);

    RequestStatus status = provider.createResources(request);
    Assert.assertNotNull(status);

    // verify
    verify(managementController, response, clusters, cluster, hostVersionDAO);
   }
Tests that forcing the host versions into
{@link RepositoryVersionState#INSTALLED}

@throws Exception
ambari-server/src/test/java/org/apache/ambari/server/controller/internal/ClusterStackVersionResourceProviderTest.java