@Test
  public void testGetSorted() throws Exception {
    Cluster cluster = createNiceMock(Cluster.class);
    StackId stackId = new StackId("HDP", "2.2.0");

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

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

    AbstractControllerResourceProvider.init(resourceProviderFactory);

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

    expect(cluster.getClusterId()).andReturn(1L).anyTimes();

    String[] versionStrings = new String[] {
      "2.1.0.0-15",  // idx 0, sorted to 0
      "2.1.0.5-17",  // idx 1, sorted to 2
      "2.1.1.5-19",  // idx 2, sorted to 3
      "2.1.0.3-14",  // idx 3, sorted to 1
      "2.1.1.5-74"   // idx 4, sorted to 4
    };

    List<RepositoryVersionEntity> repoVersionList = new ArrayList<>();
    for (int i = 0; i < versionStrings.length; i++) {
      Long id = new Long(i);

      RepositoryVersionEntity repoVersion = createNiceMock(RepositoryVersionEntity.class);
      expect(repoVersion.getVersion()).andReturn(versionStrings[i]).anyTimes();
      expect(repoVersion.getStack()).andReturn(stackEntity).anyTimes();
      expect(repoVersion.getId()).andReturn(id).anyTimes();
      expect(repositoryVersionDAOMock.findByPK(id)).andReturn(repoVersion).anyTimes();

      repoVersionList.add(repoVersion);

      replay(repoVersion);
    }

    expect(repositoryVersionDAOMock.findAll()).andReturn(repoVersionList).atLeastOnce();

    expect(hostVersionDAO.findHostVersionByClusterAndRepository(
        anyLong(), anyObject(RepositoryVersionEntity.class))).andReturn(Collections.<HostVersionEntity>emptyList()).anyTimes();

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

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

//    Field field = ClusterStackVersionResourceProvider.class.getDeclaredField("clusters");
//    field.setAccessible(true);
//    field.set(null, new Provider<Clusters>() {
//      @Override
//      public Clusters get() {
//        return clusters;
//      }
//    });

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

    Set<String> ids = Sets.newHashSet(
        ClusterStackVersionResourceProvider.CLUSTER_STACK_VERSION_STATE_PROPERTY_ID,
        ClusterStackVersionResourceProvider.CLUSTER_STACK_VERSION_VERSION_PROPERTY_ID,
        ClusterStackVersionResourceProvider.CLUSTER_STACK_VERSION_REPOSITORY_VERSION_PROPERTY_ID);

    // get cluster named Cluster100
    Predicate predicate = new PredicateBuilder()
        .property(ClusterStackVersionResourceProvider.CLUSTER_STACK_VERSION_CLUSTER_NAME_PROPERTY_ID).equals("Cluster100")
        .toPredicate();

    // create the request
    Request request = PropertyHelper.getReadRequest(ids);

    Set<Resource> responses = provider.getResources(request, predicate);
    Assert.assertNotNull(responses);

    // verify
    verify(response, clusters, cluster, hostVersionDAO);

    Assert.assertEquals(5, responses.size());

    int i = 0;
    // see the string array above.  this is the order matching the sorted strings
    long[] orders = new long[] { 0, 3, 1, 2, 4 };
    for (Resource res : responses) {
      Assert.assertEquals(orders[i], res.getPropertyValue(
          ClusterStackVersionResourceProvider.CLUSTER_STACK_VERSION_REPOSITORY_VERSION_PROPERTY_ID));

      i++;
    }


  }
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