private RepositoryVersionEntity createClusterAndHosts(String INSTALLED_VERSION, StackId stackId)
      throws AmbariException {
    Host h1 = clusters.getHost("h1");
    h1.setState(HostState.HEALTHY);

    addHost("h2");
    clusters.mapHostToCluster("h2", "c1");
    addHost("h3");
    clusters.mapHostToCluster("h3", "c1");

    // create the new repo version
    RepositoryVersionEntity repositoryVersionEntity = helper.getOrCreateRepositoryVersion(stackId,
        INSTALLED_VERSION);

    c1.setCurrentStackVersion(stackId);
    assertRepoVersionState(INSTALLED_VERSION, RepositoryVersionState.INSTALLING);

    assertRepoVersionState(CURRENT_VERSION, RepositoryVersionState.CURRENT);

    // Add ZK service with only ZOOKEEPER_SERVER
    List<String> hostList = new ArrayList<>();
    hostList.add("h1");
    hostList.add("h2");
    hostList.add("h3");
    Map<String, List<Integer>> zkTopology = new HashMap<>();
    List<Integer> zkServerHosts = Arrays.asList(0, 1, 2);
    zkTopology.put("ZOOKEEPER_SERVER", new ArrayList<>(zkServerHosts));
    addService(c1, hostList, zkTopology, "ZOOKEEPER", repositoryVersionEntity);

    // install new version
    helper.createHostVersion("h1", repositoryVersionEntity, RepositoryVersionState.INSTALLED);
    helper.createHostVersion("h2", repositoryVersionEntity, RepositoryVersionState.INSTALLED);
    helper.createHostVersion("h3", repositoryVersionEntity, RepositoryVersionState.INSTALLED);

    assertRepoVersionState(INSTALLED_VERSION, RepositoryVersionState.INSTALLED);
    assertRepoVersionState(CURRENT_VERSION, RepositoryVersionState.CURRENT);

    // Add new host and verify that it has all host versions present
    List<HostVersionEntity> h2Versions = hostVersionDAO.findAll();

    // Check before adding service
    for (HostVersionEntity hostVersionEntity : h2Versions) {
      if (hostVersionEntity.getRepositoryVersion().getVersion().equals(INSTALLED_VERSION)) {
        assertEquals(hostVersionEntity.getState(), RepositoryVersionState.INSTALLED);
      }
    }

    return repositoryVersionEntity;
  }
Shared between several test cases.
@param INSTALLED_VERSION Version to treat as INSTALLED
@param stackId Stack Id to use
@throws AmbariException
ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListenerTest.java