@Test(expected = AmbariException.class)
  public void testWrongUpgradeBeingReverted() throws Exception {
    Long upgradeIdBeingReverted = 1L;
    Long upgradeIdWhichCanBeReverted = 99L;

    UpgradeHelper upgradeHelper = createNiceMock(UpgradeHelper.class);
    ConfigHelper configHelper = createNiceMock(ConfigHelper.class);
    UpgradePack upgradePack = createNiceMock(UpgradePack.class);
    AmbariMetaInfo ami = createNiceMock(AmbariMetaInfo.class);

    expect(upgradeHelper.suggestUpgradePack(EasyMock.anyString(), EasyMock.anyObject(StackId.class),
        EasyMock.anyObject(StackId.class), EasyMock.anyObject(Direction.class),
        EasyMock.anyObject(UpgradeType.class), EasyMock.anyString())).andReturn(upgradePack).once();

    RepositoryVersionEntity repositoryVersionEntity = createNiceMock(RepositoryVersionEntity.class);
    expect(repositoryVersionEntity.getVersion()).andReturn("1.2.3.4").anyTimes();

    UpgradeEntity wrongRevertableUpgrade = createNiceMock(UpgradeEntity.class);
    expect(wrongRevertableUpgrade.getId()).andReturn(upgradeIdWhichCanBeReverted).atLeastOnce();
    expect(wrongRevertableUpgrade.getRepositoryVersion()).andReturn(repositoryVersionEntity).atLeastOnce();

    expect(m_upgradeDAO.findRevertable(1L)).andReturn(wrongRevertableUpgrade).once();

    Map<String, Object> requestMap = new HashMap<>();
    requestMap.put(UpgradeResourceProvider.UPGRADE_TYPE, UpgradeType.ROLLING.name());
    requestMap.put(UpgradeResourceProvider.UPGRADE_REVERT_UPGRADE_ID, upgradeIdBeingReverted.toString());

    replayAll();

    UpgradeContext context = new UpgradeContext(m_cluster, requestMap, null, upgradeHelper,
        m_upgradeDAO, m_repositoryVersionDAO, configHelper, ami);

    assertEquals(Direction.DOWNGRADE, context.getDirection());
    assertEquals(RepositoryType.PATCH, context.getOrchestrationType());
    assertEquals(1, context.getSupportedServices().size());
    assertTrue(context.isPatchRevert());

    verifyAll();
  }
Tests that if a different {@link UpgradeEntity} is returned instead of the one
specified by the

@throws Exception
ambari-server/src/test/java/org/apache/ambari/server/stack/upgrade/orchestrate/UpgradeContextTest.java