@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");
  // this should get skipped
  ServiceComponent component = service.getServiceComponent("ZOOKEEPER_SERVER");
  ServiceComponentHost sch = component.addServiceComponentHost("h2");
  sch.setVersion("2.2.2.2");
  // 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<StageEntity>());
  requestDao.create(requestEntity);
  UpgradeEntity upgradeEntity = new UpgradeEntity();
  upgradeEntity.setClusterId(cluster.getClusterId());
  upgradeEntity.setDirection(Direction.UPGRADE);
  upgradeEntity.setFromVersion("2.1.1.1");
  upgradeEntity.setToVersion("2.2.2.2");
  upgradeEntity.setUpgradePackage("upgrade_test");
  upgradeEntity.setUpgradeType(UpgradeType.ROLLING);
  upgradeEntity.setRequestId(2L);
  upgradeDao.create(upgradeEntity);
  upgrades = upgradeDao.findUpgrades(cluster.getClusterId());
  assertEquals(1, upgrades.size());
  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_VERSION, "2.1.1.1");
  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());
  UpgradeGroupEntity group = upgradeGroups.get(1);
  assertEquals("ZOOKEEPER", group.getName());
  assertEquals(4, group.getItems().size());
}