@Test
public void testUpgradeOrchestration() throws Exception {

  Map<String, UpgradePack> upgrades = ambariMetaInfo.getUpgradePacks("foo", "bar");
  assertTrue(upgrades.isEmpty());
  upgrades = ambariMetaInfo.getUpgradePacks("HDP", "2.1.1");
  ServiceInfo si = ambariMetaInfo.getService("HDP", "2.1.1", "ZOOKEEPER");
  si.setDisplayName("Zk");
  ComponentInfo ci = si.getComponentByName("ZOOKEEPER_SERVER");
  ci.setDisplayName("ZooKeeper1 Server2");
  assertTrue(upgrades.containsKey("upgrade_test"));
  UpgradePack upgrade = upgrades.get("upgrade_test");
  assertNotNull(upgrade);
  Cluster cluster = makeCluster();
  UpgradeContext context = m_upgradeContextFactory.create(cluster, UpgradeType.ROLLING,
      Direction.UPGRADE, UPGRADE_VERSION, new HashMap<String, Object>());
  context.setResolver(m_masterHostResolver);
  List<UpgradeGroupHolder> groups = m_upgradeHelper.createSequence(upgrade, context);
  assertEquals(7, groups.size());
  assertEquals("PRE_CLUSTER", groups.get(0).name);
  assertEquals("ZOOKEEPER", groups.get(1).name);
  assertEquals("CORE_MASTER", groups.get(2).name);
  assertEquals("CORE_SLAVES", groups.get(3).name);
  assertEquals("HIVE", groups.get(4).name);
  assertEquals("OOZIE", groups.get(5).name);
  UpgradeGroupHolder holder = groups.get(2);
  boolean found = false;
  for (StageWrapper sw : holder.items) {
    if (sw.getTasksJson().contains("Upgrading your database")) {
      found = true;
    }
  }
  assertTrue("Expected to find replaced text for Upgrading", found);
  UpgradeGroupHolder group = groups.get(1);
  // check that the display name is being used
  assertTrue(group.items.get(1).getText().contains("ZooKeeper1 Server2"));
  assertEquals(group.items.get(5).getText(), "Service Check Zk");
  group = groups.get(3);
  assertEquals(8, group.items.size());
  StageWrapper sw = group.items.get(3);
  assertEquals("Validate Partial Upgrade", sw.getText());
  assertEquals(1, sw.getTasks().size());
  assertEquals(1, sw.getTasks().get(0).getTasks().size());
  Task t = sw.getTasks().get(0).getTasks().get(0);
  assertEquals(ManualTask.class, t.getClass());
  ManualTask mt = (ManualTask) t;
  assertTrue(mt.messages.get(0).contains("DataNode and NodeManager"));
  assertNotNull(mt.structuredOut);
  assertTrue(mt.structuredOut.contains("DATANODE"));
  assertTrue(mt.structuredOut.contains("NODEMANAGER"));
  UpgradeGroupHolder postGroup = groups.get(6);
  assertEquals("POST_CLUSTER", postGroup.name);
  assertEquals("Finalize Upgrade", postGroup.title);
  assertEquals(3, postGroup.items.size());
  assertEquals("Confirm Finalize", postGroup.items.get(0).getText());
  assertEquals("Execute HDFS Finalize", postGroup.items.get(1).getText());
  assertEquals("Save Cluster State", postGroup.items.get(2).getText());
  assertEquals(StageWrapper.Type.SERVER_SIDE_ACTION, postGroup.items.get(2).getType());
  assertEquals(4, groups.get(0).items.size());
  assertEquals(6, groups.get(1).items.size());
  assertEquals(9, groups.get(2).items.size());
  assertEquals(8, groups.get(3).items.size());
  // Do stacks cleanup
  stackManagerMock.invalidateCurrentPaths();
  ambariMetaInfo.init();
}