@Test
 @JUnitTemporaryDatabase
 public void testAcknowledgements() {
   m_alarmRepo.acknowledgeAlarms(new int[] {1}, "agalue", new Date());
   List<OnmsAcknowledgment> acks = m_alarmRepo.getAcknowledgments(1);
   Assert.assertNotNull(acks);
   Assert.assertEquals(1, acks.size());
   Assert.assertEquals("agalue", acks.get(0).getAckUser());
 }
  @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 testCountMatchingAlarms() {
    int alarms =
        m_alarmRepo.countMatchingAlarms(
            AlarmUtil.getOnmsCriteria(new AlarmCriteria(new AlarmIdFilter(1))));
    assertEquals(1, alarms);

    alarms =
        m_alarmRepo.countMatchingAlarms(
            AlarmUtil.getOnmsCriteria(new AlarmCriteria(new AlarmIdFilter(2))));
    assertEquals(0, alarms);
  }
  @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());
  }
  @Test
  @JUnitTemporaryDatabase
  public void testClearAlarms() {
    OnmsAlarm alarm = m_alarmRepo.getAlarm(1);

    assertNotNull(alarm);
    assertEquals(OnmsSeverity.NORMAL.getId(), alarm.getSeverity().getId());

    int[] alarmIds = {1};
    m_alarmRepo.clearAlarms(alarmIds, "TestUser", new Date());

    alarm = m_alarmRepo.getAlarm(1);
    assertNotNull(alarm);
    assertEquals(OnmsSeverity.CLEARED.getId(), alarm.getSeverity().getId());
  }
  @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 testCountMatchingBySeverity() {
   int[] matchingAlarmCount =
       m_alarmRepo.countMatchingAlarmsBySeverity(
           AlarmUtil.getOnmsCriteria(new AlarmCriteria(new SeverityFilter(OnmsSeverity.NORMAL))));
   assertEquals(8, matchingAlarmCount.length);
 }
  @Test
  @Transactional
  @JUnitTemporaryDatabase
  public void testAcknowledgeUnacknowledgeAllAlarms() {
    String user = "******";
    m_alarmRepo.acknowledgeAll(user, new Date());

    int matchingAlarmCount =
        m_alarmRepo.countMatchingAlarms(
            AlarmUtil.getOnmsCriteria(new AlarmCriteria(new AcknowledgedByFilter(user))));
    assertEquals(1, matchingAlarmCount);

    m_alarmRepo.unacknowledgeAll(user);

    matchingAlarmCount =
        m_alarmRepo.countMatchingAlarms(
            AlarmUtil.getOnmsCriteria(new AlarmCriteria(new AcknowledgedByFilter(user))));
    assertEquals(0, matchingAlarmCount);
  }
  @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
  @Transactional
  @JUnitTemporaryDatabase
  public void testGetAlarmById() {
    OnmsAlarm alarm = m_alarmRepo.getAlarm(1);
    assertNotNull(alarm);

    assertEquals(1, alarm.getId().intValue());
    assertEquals("uei.opennms.org/test", alarm.getUei());
    assertEquals("localhost", alarm.getDistPoller().getName());
    assertEquals(1, alarm.getCounter().intValue());
    assertEquals(3, alarm.getSeverity().getId());
  }
  @Test
  @Transactional
  @JUnitTemporaryDatabase
  public void testCountMatchingAlarmsBySeverity() {
    AlarmCriteria criteria = new AlarmCriteria();
    int[] matchingAlarms =
        m_alarmRepo.countMatchingAlarmsBySeverity(AlarmUtil.getOnmsCriteria(criteria));

    assertEquals(8, matchingAlarms.length);

    // Make sure that the count is correct per severity
    assertEquals(0, matchingAlarms[OnmsSeverity.CLEARED.getId()]);
    assertEquals(0, matchingAlarms[OnmsSeverity.CRITICAL.getId()]);
    assertEquals(0, matchingAlarms[OnmsSeverity.INDETERMINATE.getId()]);
    assertEquals(0, matchingAlarms[OnmsSeverity.MINOR.getId()]);
    assertEquals(1, matchingAlarms[OnmsSeverity.NORMAL.getId()]);
    assertEquals(0, matchingAlarms[OnmsSeverity.WARNING.getId()]);
    assertEquals(0, matchingAlarms[OnmsSeverity.MAJOR.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);
  }