@Test
  public void testFindLastUpgradeForCluster() throws Exception {
    // create upgrade entities
    RequestEntity requestEntity = new RequestEntity();
    requestEntity.setRequestId(1L);
    requestEntity.setClusterId(clusterId.longValue());
    requestEntity.setStatus(HostRoleStatus.PENDING);
    requestEntity.setStages(new ArrayList<>());
    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);
    dao.create(entity1);

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

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

    UpgradeEntity lastUpgradeForCluster = dao.findLastUpgradeForCluster(clusterId.longValue(), Direction.UPGRADE);
    assertNotNull(lastUpgradeForCluster);
    assertEquals(33L, (long)lastUpgradeForCluster.getId());
  }
Create upgrades and downgrades and verify only latest upgrade is given

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