@Test
  public void testFindRevertableUpgrade() throws Exception {
    // create upgrade entities
    UpgradeEntity revertable = dao.findRevertable(1L);
    UpgradeEntity revertableViaJPQL = dao.findRevertableUsingJPQL(1L);
    assertEquals(null, revertable);
    assertEquals(null, revertableViaJPQL);

    RequestEntity requestEntity = new RequestEntity();
    requestEntity.setRequestId(1L);
    requestEntity.setClusterId(clusterId.longValue());
    requestEntity.setStatus(HostRoleStatus.PENDING);
    requestEntity.setStages(new ArrayList<StageEntity>());
    requestDAO.create(requestEntity);

    UpgradeEntity entity1 = new UpgradeEntity();
    entity1.setId(11L);
    entity1.setClusterId(clusterId.longValue());
    entity1.setDirection(Direction.UPGRADE);
    entity1.setRequestEntity(requestEntity);
    entity1.setRepositoryVersion(repositoryVersion2500);
    entity1.setUpgradeType(UpgradeType.ROLLING);
    entity1.setUpgradePackage("test-upgrade");
    entity1.setUpgradePackStackId(new StackId((String) null));
    entity1.setDowngradeAllowed(true);
    entity1.setOrchestration(RepositoryType.PATCH);
    entity1.setRevertAllowed(true);
    dao.create(entity1);

    revertable = dao.findRevertable(1L);
    revertableViaJPQL = dao.findRevertableUsingJPQL(1L);
    assertEquals(revertable.getId(), entity1.getId());
    assertEquals(revertableViaJPQL.getId(), entity1.getId());

    UpgradeEntity entity2 = new UpgradeEntity();
    entity2.setId(22L);
    entity2.setClusterId(clusterId.longValue());
    entity2.setDirection(Direction.UPGRADE);
    entity2.setRequestEntity(requestEntity);
    entity2.setRepositoryVersion(repositoryVersion2511);
    entity2.setUpgradeType(UpgradeType.ROLLING);
    entity2.setUpgradePackage("test-upgrade");
    entity2.setUpgradePackStackId(new StackId((String) null));
    entity2.setDowngradeAllowed(true);
    entity2.setOrchestration(RepositoryType.MAINT);
    entity2.setRevertAllowed(true);
    dao.create(entity2);

    revertable = dao.findRevertable(1L);
    revertableViaJPQL = dao.findRevertableUsingJPQL(1L);
    assertEquals(revertable.getId(), entity2.getId());
    assertEquals(revertableViaJPQL.getId(), entity2.getId());

    // now make it look like upgrade ID 22 was reverted
    entity2.setRevertAllowed(false);
    entity2 = dao.merge(entity2);

    // create a downgrade for ID 22
    UpgradeEntity entity3 = new UpgradeEntity();
    entity3.setId(33L);
    entity3.setClusterId(clusterId.longValue());
    entity3.setDirection(Direction.DOWNGRADE);
    entity3.setRequestEntity(requestEntity);
    entity3.setRepositoryVersion(repositoryVersion2511);
    entity3.setUpgradeType(UpgradeType.ROLLING);
    entity3.setUpgradePackage("test-upgrade");
    entity3.setUpgradePackStackId(new StackId((String) null));
    entity3.setOrchestration(RepositoryType.MAINT);
    entity3.setDowngradeAllowed(false);
    dao.create(entity3);

    revertable = dao.findRevertable(1L);
    revertableViaJPQL = dao.findRevertableUsingJPQL(1L);
    assertEquals(revertable.getId(), entity1.getId());
    assertEquals(revertableViaJPQL.getId(), entity1.getId());

    // now make it look like upgrade ID 11 was reverted
    entity1.setRevertAllowed(false);
    entity1 = dao.merge(entity1);

    // create a downgrade for ID 11
    UpgradeEntity entity4 = new UpgradeEntity();
    entity4.setId(44L);
    entity4.setClusterId(clusterId.longValue());
    entity4.setDirection(Direction.DOWNGRADE);
    entity4.setRequestEntity(requestEntity);
    entity4.setRepositoryVersion(repositoryVersion2500);
    entity4.setUpgradeType(UpgradeType.ROLLING);
    entity4.setUpgradePackage("test-upgrade");
    entity4.setUpgradePackStackId(new StackId((String) null));
    entity4.setOrchestration(RepositoryType.MAINT);
    entity4.setDowngradeAllowed(false);
    dao.create(entity4);

    revertable = dao.findRevertable(1L);
    revertableViaJPQL = dao.findRevertableUsingJPQL(1L);
    assertEquals(null, revertable);
    assertEquals(null, revertableViaJPQL);
  }
Tests the logic that finds the one-and-only revertable upgrade.

@throws Exception
ambari-server/src/test/java/org/apache/ambari/server/orm/dao/UpgradeDAOTest.java