@Test
  public void testOnServiceEvent() throws AmbariException {
    String INSTALLED_VERSION = "2.2.0-1000";
    String INSTALLED_VERSION_2 = "2.1.1-2000";
    StackId stackId = new StackId(this.stackId);
    StackId yaStackId = new StackId(yetAnotherStackId);

    // get new hosts installed with the first repo
    RepositoryVersionEntity repositoryVersion = createClusterAndHosts(INSTALLED_VERSION, stackId);

    // register the new repo
    addRepoVersion(INSTALLED_VERSION_2, yaStackId);

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

    //Add HDFS service
    List<String> hostList = new ArrayList<>();
    hostList.add("h1");
    hostList.add("h2");
    hostList.add("h3");
    Map<String, List<Integer>> hdfsTopology = new HashMap<>();
    hdfsTopology.put("NAMENODE", Collections.singletonList(0));
    hdfsTopology.put("SECONDARY_NAMENODE", Collections.singletonList(1));
    List<Integer> datanodeHosts = Arrays.asList(0, 1);
    hdfsTopology.put("DATANODE", new ArrayList<>(datanodeHosts));
    addService(c1, hostList, hdfsTopology, "HDFS", repositoryVersion);

    // Check result
    Set<String> changedHosts = new HashSet<>();
    changedHosts.add("h1");
    changedHosts.add("h2");

    List<HostVersionEntity> hostVersions = hostVersionDAO.findAll();

    for (HostVersionEntity hostVersionEntity : hostVersions) {
      if (hostVersionEntity.getRepositoryVersion().getVersion().equals(INSTALLED_VERSION) ||
              hostVersionEntity.getRepositoryVersion().getVersion().equals(INSTALLED_VERSION_2)) {
        if (changedHosts.contains(hostVersionEntity.getHostName())) {
          assertEquals(hostVersionEntity.getState(), RepositoryVersionState.OUT_OF_SYNC);
        } else {
          assertEquals(hostVersionEntity.getState(), RepositoryVersionState.INSTALLED);
        }
      }
    }


    assertRepoVersionState(INSTALLED_VERSION, RepositoryVersionState.OUT_OF_SYNC);
    assertRepoVersionState(INSTALLED_VERSION_2, RepositoryVersionState.OUT_OF_SYNC);
    assertRepoVersionState(CURRENT_VERSION, RepositoryVersionState.CURRENT);
  }
When a service is added to a cluster, all non-CURRENT host versions on
all affected hosts (where host new components are installed)
should transition to OUT_OF_SYNC state
ambari-server/src/test/java/org/apache/ambari/server/events/listeners/upgrade/HostVersionOutOfSyncListenerTest.java