@Test
  public void testAlertFirmnessWithinNonOKStates() throws Exception {
    String definitionName = ALERT_DEFINITION + "1";
    String serviceName = "HDFS";
    String componentName = "NAMENODE";
    String text = serviceName + " " + componentName + " is OK";

    Alert alert = new Alert(definitionName, null, serviceName, componentName, HOST1, AlertState.OK);
    alert.setClusterId(m_cluster.getClusterId());
    alert.setLabel(ALERT_LABEL);
    alert.setText(text);
    alert.setTimestamp(1L);

    // fire the alert, and check that the new entry was created correctly
    AlertReceivedListener listener = m_injector.getInstance(AlertReceivedListener.class);
    AlertReceivedEvent event = new AlertReceivedEvent(m_cluster.getClusterId(), alert);
    listener.onAlertEvent(event);

    List<AlertCurrentEntity> allCurrent = m_dao.findCurrent();
    assertEquals(1, allCurrent.size());

    // check occurrences (should be 1 since it's the first) and state (HARD)
    assertEquals(1, (long) allCurrent.get(0).getOccurrences());
    assertEquals(AlertFirmness.HARD, allCurrent.get(0).getFirmness());

    // move the repeat tolerance to 4 to test out SOFT alerts between states
    AlertDefinitionEntity definition = allCurrent.get(0).getAlertHistory().getAlertDefinition();
    definition.setRepeatTolerance(4);
    definition.setRepeatToleranceEnabled(true);
    m_definitionDao.merge(definition);

    // change state to WARNING, should still be SOFT
    alert.setState(AlertState.WARNING);
    listener.onAlertEvent(event);
    allCurrent = m_dao.findCurrent();
    assertEquals(1, (long) allCurrent.get(0).getOccurrences());
    assertEquals(AlertFirmness.SOFT, allCurrent.get(0).getFirmness());
    assertEquals(AlertState.WARNING, allCurrent.get(0).getAlertHistory().getAlertState());

    // change state to CRITICAL, should still be SOFT, but occurrences of non-OK
    // increases to 2
    alert.setState(AlertState.CRITICAL);
    listener.onAlertEvent(event);
    allCurrent = m_dao.findCurrent();
    assertEquals(2, (long) allCurrent.get(0).getOccurrences());
    assertEquals(AlertFirmness.SOFT, allCurrent.get(0).getFirmness());
    assertEquals(AlertState.CRITICAL, allCurrent.get(0).getAlertHistory().getAlertState());

    // change state to WARNING, should still be SOFT, but occurrences of non-OK
    // increases to 3
    alert.setState(AlertState.WARNING);
    listener.onAlertEvent(event);
    allCurrent = m_dao.findCurrent();
    assertEquals(3, (long) allCurrent.get(0).getOccurrences());
    assertEquals(AlertFirmness.SOFT, allCurrent.get(0).getFirmness());
    assertEquals(AlertState.WARNING, allCurrent.get(0).getAlertHistory().getAlertState());

    // change state to CRITICAL, occurrences is not met, should be HARD
    alert.setState(AlertState.CRITICAL);
    listener.onAlertEvent(event);
    allCurrent = m_dao.findCurrent();
    assertEquals(4, (long) allCurrent.get(0).getOccurrences());
    assertEquals(AlertFirmness.HARD, allCurrent.get(0).getFirmness());
    assertEquals(AlertState.CRITICAL, allCurrent.get(0).getAlertHistory().getAlertState());
  }
Tests that we correctly record alert firmness when an alert moves back and
forth between non-OK states (such as between {@link AlertState#WARNING} and
{@link AlertState#CRITICAL}). These are technically alert state changes and
will fire {@link AlertStateChangeEvent}s but we only want to handle them
when they are HARD.
ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AlertReceivedListenerTest.java