@Test
  public void testHostVersionsAfterUpgrade() throws Exception {
    String hostName = "h1";
    Cluster cluster = createUpgradeCluster(repositoryVersion2110, hostName);
    createHostVersions(repositoryVersion2111, hostName);
    createHostVersions(repositoryVersion2201, hostName);

    // Install ZK with some components
    Service zk = installService(cluster, "ZOOKEEPER", repositoryVersion2110);
    addServiceComponent(cluster, zk, "ZOOKEEPER_SERVER");
    addServiceComponent(cluster, zk, "ZOOKEEPER_CLIENT");
    createNewServiceComponentHost(cluster, "ZOOKEEPER", "ZOOKEEPER_SERVER", hostName);
    createNewServiceComponentHost(cluster, "ZOOKEEPER", "ZOOKEEPER_CLIENT", hostName);

    List<HostVersionEntity> hostVersions = hostVersionDAO.findAll();
    assertEquals(3, hostVersions.size());

    // repo 2110 - CURRENT (upgrading from)
    // repo 2111 - CURRENT (all hosts reported in during upgrade)
    // repo 2201 - NOT_REQUIRED (different stack)
    for (HostVersionEntity hostVersion : hostVersions) {
      RepositoryVersionEntity hostRepoVersion = hostVersion.getRepositoryVersion();
      if (repositoryVersion2110.equals(hostRepoVersion)) {
        hostVersion.setState(RepositoryVersionState.CURRENT);
      } else if (repositoryVersion2111.equals(hostRepoVersion)) {
        hostVersion.setState(RepositoryVersionState.CURRENT);
      } else {
        hostVersion.setState(RepositoryVersionState.NOT_REQUIRED);
      }

      hostVersionDAO.merge(hostVersion);
    }

    // upgrade to 2111
    createUpgrade(cluster, repositoryVersion2111);

    // push all services to the correct repo version for finalize
    Map<String, Service> services = cluster.getServices();
    assertTrue(services.size() > 0);
    for (Service service : services.values()) {
      service.setDesiredRepositoryVersion(repositoryVersion2111);
    }

    // push all components to the correct version
    List<HostComponentStateEntity> hostComponentStates = hostComponentStateDAO.findByHost(hostName);
    for (HostComponentStateEntity hostComponentState : hostComponentStates) {
      hostComponentState.setVersion(repositoryVersion2111.getVersion());
      hostComponentStateDAO.merge(hostComponentState);
    }

    Map<String, String> commandParams = new HashMap<>();
    ExecutionCommand executionCommand = new ExecutionCommand();
    executionCommand.setCommandParams(commandParams);
    executionCommand.setClusterName(clusterName);

    HostRoleCommand hostRoleCommand = hostRoleCommandFactory.create(null, null, null, null);
    hostRoleCommand.setExecutionCommandWrapper(new ExecutionCommandWrapper(executionCommand));

    finalizeUpgradeAction.setExecutionCommand(executionCommand);
    finalizeUpgradeAction.setHostRoleCommand(hostRoleCommand);

    // finalize
    CommandReport report = finalizeUpgradeAction.execute(null);
    assertNotNull(report);
    assertEquals(HostRoleStatus.COMPLETED.name(), report.getStatus());

    for (HostVersionEntity hostVersion : hostVersions) {
      RepositoryVersionEntity hostRepoVersion = hostVersion.getRepositoryVersion();
      if (repositoryVersion2110.equals(hostRepoVersion)) {
        assertEquals(RepositoryVersionState.INSTALLED, hostVersion.getState());
      } else if (repositoryVersion2111.equals(hostRepoVersion)) {
        assertEquals(RepositoryVersionState.CURRENT, hostVersion.getState());
      } else {
        assertEquals(RepositoryVersionState.NOT_REQUIRED, hostVersion.getState());
      }
    }
  }
Tests that all host versions are correct after upgrade. This test will
ensure that the prior CURRENT versions are moved to INSTALLED while not
touching any others.

@throws Exception
ambari-server/src/test/java/org/apache/ambari/server/serveraction/upgrades/UpgradeActionTest.java