@Test
public void testDowngradeAfterPartialUpgrade() throws Exception {

  Clusters clusters = injector.getInstance(Clusters.class);
  ServiceFactory serviceFactory = injector.getInstance(ServiceFactory.class);
  String clusterName = "c1";
  StackId stackId = new StackId("HDP-2.1.1");
  clusters.addCluster(clusterName, stackId);
  Cluster c = clusters.getCluster(clusterName);
  helper.getOrCreateRepositoryVersion(stackId,
      c.getDesiredStackVersion().getStackVersion());
  c.createClusterVersion(stackId,
      c.getDesiredStackVersion().getStackVersion(), "admin",
      RepositoryVersionState.INSTALLING);
  for (int i = 0; i < 2; i++) {
    String hostName = "h" + (i+1);
    clusters.addHost(hostName);
    Host host = clusters.getHost(hostName);
    Map<String, String> hostAttributes = new HashMap<>();
    hostAttributes.put("os_family", "redhat");
    hostAttributes.put("os_release_version", "6");
    host.setHostAttributes(hostAttributes);
    clusters.mapHostToCluster(hostName, clusterName);
  }
  // !!! add services
  c.addService(serviceFactory.createNew(c, "HDFS"));
  Service s = c.getService("HDFS");
  ServiceComponent sc = s.addServiceComponent("NAMENODE");
  sc.addServiceComponentHost("h1");
  sc.addServiceComponentHost("h2");
  List<ServiceComponentHost> schs = c.getServiceComponentHosts("HDFS", "NAMENODE");
  assertEquals(2, schs.size());
  HostsType type = new HostsType();
  type.master = "h1";
  type.secondary = "h2";
  expect(m_masterHostResolver.getMasterAndHosts("ZOOKEEPER", "ZOOKEEPER_SERVER")).andReturn(null).anyTimes();
  expect(m_masterHostResolver.getMasterAndHosts("HDFS", "NAMENODE")).andReturn(type).anyTimes();
  expect(m_masterHostResolver.getCluster()).andReturn(c).anyTimes();
  replay(m_masterHostResolver);
  UpgradeContext context = m_upgradeContextFactory.create(c, UpgradeType.ROLLING,
      Direction.DOWNGRADE, DOWNGRADE_VERSION, new HashMap<String, Object>());
  context.setResolver(m_masterHostResolver);
  Map<String, UpgradePack> upgrades = ambariMetaInfo.getUpgradePacks("HDP", "2.1.1");
  assertTrue(upgrades.containsKey("upgrade_direction"));
  UpgradePack upgrade = upgrades.get("upgrade_direction");
  assertNotNull(upgrade);
  List<UpgradeGroupHolder> groups = m_upgradeHelper.createSequence(upgrade, context);
  assertEquals(2, groups.size());
  UpgradeGroupHolder group = groups.get(0);
  assertEquals(1, group.items.size());
  assertEquals("PRE_POST_CLUSTER", group.name);
  group = groups.get(1);
  assertEquals("POST_CLUSTER", group.name);
  assertEquals(3, group.items.size());
  StageWrapper stage = group.items.get(1);
  assertEquals("NameNode Finalize", stage.getText());
  assertEquals(1, stage.getTasks().size());
  TaskWrapper task = stage.getTasks().get(0);
  assertEquals(1, task.getHosts().size());
}