@Test
  @JUnitTemporaryDatabase
  public void testGetUnacknowledgedAlarms() {
    OnmsCriteria acked =
        AlarmUtil.getOnmsCriteria(new AlarmCriteria(new Filter[0], AcknowledgeType.ACKNOWLEDGED));
    OnmsCriteria unacked =
        AlarmUtil.getOnmsCriteria(new AlarmCriteria(new Filter[0], AcknowledgeType.UNACKNOWLEDGED));
    OnmsCriteria all =
        AlarmUtil.getOnmsCriteria(new AlarmCriteria(new Filter[0], AcknowledgeType.BOTH));

    int countAll = m_alarmRepo.countMatchingAlarms(all);
    int countAcked = m_alarmRepo.countMatchingAlarms(acked);
    int countUnacked = m_alarmRepo.countMatchingAlarms(unacked);

    assertEquals(countAll, countAcked + countUnacked);
    assertTrue(countAll > 0);
    assertTrue(countAcked == 0);
    assertTrue(countUnacked > 0);

    OnmsAlarm[] unackedAlarms = m_alarmRepo.getMatchingAlarms(unacked);
    assertEquals(countUnacked, unackedAlarms.length);

    OnmsAlarm[] ackedAlarms = m_alarmRepo.getMatchingAlarms(acked);
    assertEquals(countAcked, ackedAlarms.length);

    OnmsAlarm[] allAlarms = m_alarmRepo.getMatchingAlarms(all);
    assertEquals(countAll, allAlarms.length);

    m_alarmRepo.acknowledgeMatchingAlarms(
        "TestUser", new Date(), AlarmUtil.getOnmsCriteria(new AlarmCriteria(new AlarmIdFilter(1))));

    assertEquals(countAcked + 1, m_alarmRepo.countMatchingAlarms(acked));
    assertEquals(countUnacked - 1, m_alarmRepo.countMatchingAlarms(unacked));
  }
  @Test
  @JUnitTemporaryDatabase
  public void testGetMatchingAlarms() {
    OnmsAlarm[] alarms =
        m_alarmRepo.getMatchingAlarms(
            AlarmUtil.getOnmsCriteria(
                new AlarmCriteria(new SeverityFilter(OnmsSeverity.NORMAL), new AlarmIdFilter(1))));
    assertNotNull(alarms);
    assertEquals(1, alarms.length);

    alarms =
        m_alarmRepo.getMatchingAlarms(
            AlarmUtil.getOnmsCriteria(new AlarmCriteria(new SeverityFilter(OnmsSeverity.MAJOR))));
    assertNotNull(alarms);
    assertEquals(0, alarms.length);
  }
  @Test
  @Transactional
  @JUnitTemporaryDatabase
  public void testSortAndSearchBySameProperty() {

    Filter[] filters = new Filter[] {new NodeNameLikeFilter("node")};

    AlarmCriteria sorted =
        new AlarmCriteria(filters, SortStyle.NODE, AcknowledgeType.UNACKNOWLEDGED, 100, 0);
    OnmsAlarm[] alarms = m_alarmRepo.getMatchingAlarms(AlarmUtil.getOnmsCriteria(sorted));
    assertTrue("Failed to sort with style " + SortStyle.NODE, alarms.length > 0);
  }
  @Test
  @Transactional
  @JUnitTemporaryDatabase
  public void testSort() {

    for (SortStyle style : SortStyle.values()) {
      AlarmCriteria sorted =
          new AlarmCriteria(new Filter[0], style, AcknowledgeType.UNACKNOWLEDGED, 100, 0);
      OnmsAlarm[] alarms = m_alarmRepo.getMatchingAlarms(AlarmUtil.getOnmsCriteria(sorted));
      assertTrue("Failed to sort with style " + style, alarms.length > 0);
    }
  }
  @Test
  @JUnitTemporaryDatabase
  public void testEscalateAlarms() {
    int[] alarmIds = {1};
    m_alarmRepo.escalateAlarms(alarmIds, "TestUser", new Date());

    OnmsAlarm[] alarms =
        m_alarmRepo.getMatchingAlarms(
            AlarmUtil.getOnmsCriteria(new AlarmCriteria(new AlarmIdFilter(1))));

    assertNotNull(alarms);

    assertEquals(OnmsSeverity.WARNING.getId(), alarms[0].getSeverity().getId());
  }
  /**
   * {@inheritDoc}
   *
   * <p>Purge or purge all action of the selected alarms specified in the POST and then redirect the
   * client to an appropriate URL for display.
   */
  protected ModelAndView handleRequestInternal(
      HttpServletRequest request, HttpServletResponse response) throws Exception {

    logger.info("Enter into the AlarmPurgeController action");

    // Handle the alarm and actionCode parameter
    String[] alarmIdStrings = request.getParameterValues("alarm");
    String action = request.getParameter("actionCode");

    List<Integer> alarmIds = new ArrayList<Integer>();
    if (alarmIdStrings != null && action.equals(PURGE_ACTION)) {
      for (int i = 0; i < alarmIdStrings.length; i++) {
        try {
          alarmIds.add(WebSecurityUtils.safeParseInt(alarmIdStrings[i]));
        } catch (Exception e) {
          logger.error("Could not parse alarm ID '{}' to integer.", alarmIdStrings[i]);
        }
      }
    }

    // Handle the acknowledge type parameter
    String ackTypeString = request.getParameter("acktype");
    AcknowledgeType alarmAckType = m_defaultAlarmAcknowledgeType;

    if (ackTypeString != null) {
      try {
        alarmAckType = AcknowledgeType.getAcknowledgeType(ackTypeString);
      } catch (Exception e) {
        logger.error("Could not retrieve acknowledge type for this '{}'.", ackTypeString);
      }
    }

    // Handle the filter parameter
    List<Filter> filterList = new ArrayList<Filter>();
    String[] filterStrings = request.getParameterValues("filter");

    if (action.equals(PURGEALL_ACTION)) {
      if (filterStrings != null) {
        for (int i = 0; i < filterStrings.length; i++) {
          Filter filter = AlarmUtil.getFilter(filterStrings[i], getServletContext());
          if (filter != null) {
            filterList.add(filter);
          }
        }
      }
    }

    // Get the alarms by alarm criteria
    Filter[] alarmFilters = filterList.toArray(new Filter[0]);

    if (action.equals(PURGEALL_ACTION)) {
      alarmIds.clear();
      AlarmCriteria alarmQueryCriteria = new AlarmCriteria(alarmAckType, alarmFilters);
      OnmsAlarm[] alarms =
          m_webAlarmRepository.getMatchingAlarms(AlarmUtil.getOnmsCriteria(alarmQueryCriteria));
      for (OnmsAlarm alarm : alarms) {
        alarmIds.add(alarm.getId());
      }
    }

    // Handle the purge action
    if (action.equals(PURGE_ACTION) || action.equals(PURGEALL_ACTION)) {
      try {
        m_webAlarmRepository.purgeAlarms(alarmIds);
        request.getSession().setAttribute("actionStatus", alarmIds.size() + "," + SUCCESS_ACTION);
        logger.info(
            "The Purge action is successfully completed for the alarm Id's " + alarmIds + " ");
      } catch (final Exception ex) {
        ex.printStackTrace();
        request.getSession().setAttribute("actionStatus", alarmIds.size() + "," + FAILURE_ACTION);
        logger.error("Unable to do purge action for this alarm Id's.", alarmIds);
      }
    } else {
      logger.error("Unknown alarm action: " + action);
    }

    // Handle the redirect parameters
    String redirectParms = request.getParameter("redirectParms");
    String viewName = m_redirectView;
    if (redirectParms != null) {
      viewName = m_redirectView + "?" + redirectParms;
    }

    RedirectView view = new RedirectView(viewName, true);
    logger.info("Terminated from the AlarmPurgeController action");

    return new ModelAndView(view);
  }