@Test
  public void testCreatePartialDowngrade() throws Exception {
    clusters.addHost("h2");
    Host host = clusters.getHost("h2");
    Map<String, String> hostAttributes = new HashMap<>();
    hostAttributes.put("os_family", "redhat");
    hostAttributes.put("os_release_version", "6.3");
    host.setHostAttributes(hostAttributes);

    clusters.mapHostToCluster("h2", "c1");
    Cluster cluster = clusters.getCluster("c1");
    Service service = cluster.getService("ZOOKEEPER");

    // start out with 0 (sanity check)
    List<UpgradeEntity> upgrades = upgradeDao.findUpgrades(cluster.getClusterId());
    assertEquals(0, upgrades.size());

    // a downgrade MUST have an upgrade to come from, so populate an upgrade in
    // the DB
    RequestEntity requestEntity = new RequestEntity();
    requestEntity.setRequestId(2L);
    requestEntity.setClusterId(cluster.getClusterId());
    requestEntity.setStages(new ArrayList<>());
    requestDao.create(requestEntity);

    UpgradeEntity upgradeEntity = new UpgradeEntity();
    upgradeEntity.setClusterId(cluster.getClusterId());
    upgradeEntity.setDirection(Direction.UPGRADE);
    upgradeEntity.setRepositoryVersion(repoVersionEntity2200);
    upgradeEntity.setUpgradePackage("upgrade_test");
    upgradeEntity.setUpgradePackStackId(repoVersionEntity2200.getStackId());
    upgradeEntity.setUpgradeType(UpgradeType.ROLLING);
    upgradeEntity.setRequestEntity(requestEntity);

    UpgradeHistoryEntity history = new UpgradeHistoryEntity();
    history.setUpgrade(upgradeEntity);
    history.setFromRepositoryVersion(service.getDesiredRepositoryVersion());
    history.setTargetRepositoryVersion(repoVersionEntity2200);
    history.setServiceName(service.getName());
    history.setComponentName("ZOKKEEPER_SERVER");
    upgradeEntity.addHistory(history);

    history = new UpgradeHistoryEntity();
    history.setUpgrade(upgradeEntity);
    history.setFromRepositoryVersion(service.getDesiredRepositoryVersion());
    history.setTargetRepositoryVersion(repoVersionEntity2200);
    history.setServiceName(service.getName());
    history.setComponentName("ZOKKEEPER_CLIENT");
    upgradeEntity.addHistory(history);

    upgradeDao.create(upgradeEntity);
    upgrades = upgradeDao.findUpgrades(cluster.getClusterId());
    assertEquals(1, upgrades.size());

    // push a ZK server foward to the new repo version, leaving the old one on
    // the old version
    ServiceComponent component = service.getServiceComponent("ZOOKEEPER_SERVER");
    ServiceComponentHost sch = component.addServiceComponentHost("h2");
    sch.setVersion(repoVersionEntity2200.getVersion());

    UpgradeEntity lastUpgrade = upgradeDao.findLastUpgradeForCluster(cluster.getClusterId(), Direction.UPGRADE);
    assertNotNull(lastUpgrade);

    Map<String, Object> requestProps = new HashMap<>();
    requestProps.put(UpgradeResourceProvider.UPGRADE_CLUSTER_NAME, "c1");
    requestProps.put(UpgradeResourceProvider.UPGRADE_PACK, "upgrade_test");
    requestProps.put(UpgradeResourceProvider.UPGRADE_SKIP_PREREQUISITE_CHECKS, "true");
    requestProps.put(UpgradeResourceProvider.UPGRADE_DIRECTION, Direction.DOWNGRADE.name());

    Map<String, String> requestInfoProperties = new HashMap<>();

    ResourceProvider upgradeResourceProvider = createProvider(amc);

    Request request = PropertyHelper.getCreateRequest(Collections.singleton(requestProps), requestInfoProperties);
    upgradeResourceProvider.createResources(request);

    upgrades = upgradeDao.findUpgrades(cluster.getClusterId());
    assertEquals(2, upgrades.size());

    UpgradeEntity downgrade = upgrades.get(1);
    assertEquals(cluster.getClusterId(), downgrade.getClusterId().longValue());

    List<UpgradeGroupEntity> upgradeGroups = downgrade.getUpgradeGroups();
    assertEquals(3, upgradeGroups.size());

    // the ZK restart group should only have 3 entries since the ZK server on h1
    // didn't get upgraded
    UpgradeGroupEntity group = upgradeGroups.get(1);
    assertEquals("ZOOKEEPER", group.getName());
    assertEquals(3, group.getItems().size());
  }
Tests that retrieving an upgrade correctly populates less common upgrade
options correctly.
ambari-server/src/test/java/org/apache/ambari/server/controller/internal/UpgradeResourceProviderTest.java