@Test
  public void testAlertHistorySorting() throws Exception {
    m_helper.installHdfsService(m_cluster, m_serviceFactory,
        m_componentFactory, m_schFactory, HOSTNAME);
    m_alertHelper.populateData(m_cluster);

    List<SortRequestProperty> sortProperties = new ArrayList<>();
    SortRequest sortRequest = new SortRequestImpl(sortProperties);
    AlertHistoryRequest request = new AlertHistoryRequest();
    request.Sort = sortRequest;

    Predicate clusterPredicate = new PredicateBuilder().property(
        AlertHistoryResourceProvider.ALERT_HISTORY_CLUSTER_NAME).equals("c1").toPredicate();

    request.Predicate = clusterPredicate;

    sortProperties.add(new SortRequestProperty(
        AlertHistoryResourceProvider.ALERT_HISTORY_SERVICE_NAME, Order.ASC));

    // get back all 3
    List<AlertHistoryEntity> histories = m_dao.findAll(request);
    assertEquals(3, histories.size());

    // assert sorting ASC
    String lastServiceName = null;
    for (AlertHistoryEntity history : histories) {
      if (null == lastServiceName) {
        lastServiceName = history.getServiceName();
        continue;
      }

      String currentServiceName = history.getServiceName();
      assertTrue(lastServiceName.compareTo(currentServiceName) <= 0);
      lastServiceName = currentServiceName;
    }

    // clear and do DESC
    sortProperties.clear();
    sortProperties.add(new SortRequestProperty(
        AlertHistoryResourceProvider.ALERT_HISTORY_SERVICE_NAME, Order.DESC));

    // get back all 3
    histories = m_dao.findAll(request);
    assertEquals(3, histories.size());

    // assert sorting DESC
    lastServiceName = null;
    for (AlertHistoryEntity history : histories) {
      if (null == lastServiceName) {
        lastServiceName = history.getServiceName();
        continue;
      }

      String currentServiceName = history.getServiceName();
      assertTrue(lastServiceName.compareTo(currentServiceName) >= 0);
      lastServiceName = currentServiceName;
    }
  }
Tests that JPA does the sorting work for us.

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