@Test
  public void testAlertRecords() throws AmbariException {
    Alert alert1 = new Alert(ALERT_DEFINITION, null, SERVICE, COMPONENT, HOST1,
        AlertState.OK);
    alert1.setLabel(ALERT_LABEL);
    alert1.setText("Component component1 is OK");
    alert1.setTimestamp(1L);
    alert1.setClusterId(m_cluster.getClusterId());

    Alert alert2 = new Alert(ALERT_DEFINITION, null, SERVICE, COMPONENT, HOST2,
        AlertState.CRITICAL);
    alert2.setLabel(ALERT_LABEL);
    alert2.setText("Component component2 is not OK");
    alert2.setClusterId(m_cluster.getClusterId());


    AlertReceivedListener listener = m_injector.getInstance(AlertReceivedListener.class);

    AlertReceivedEvent event1 = new AlertReceivedEvent(
        m_cluster.getClusterId(),
        alert1);

    AlertReceivedEvent event2 = new AlertReceivedEvent(
        m_cluster.getClusterId(),
        alert2);

    listener.onAlertEvent(event1);
    listener.onAlertEvent(event2);

    List<AlertCurrentEntity> allCurrent = m_dao.findCurrentByService(
        m_cluster.getClusterId(), SERVICE);
    assertEquals(2, allCurrent.size());

    List<AlertHistoryEntity> allHistory = m_dao.findAll(m_cluster.getClusterId());
    assertEquals(2, allHistory.size());

    AlertCurrentEntity current = m_dao.findCurrentByHostAndName(
        m_cluster.getClusterId(), HOST1, ALERT_DEFINITION);
    assertNotNull(current);
    assertEquals(HOST1, current.getAlertHistory().getHostName());
    assertEquals(ALERT_DEFINITION, current.getAlertHistory().getAlertDefinition().getDefinitionName());
    assertEquals(ALERT_LABEL, current.getAlertHistory().getAlertLabel());
    assertEquals("Component component1 is OK", current.getAlertHistory().getAlertText());
    assertEquals(current.getAlertHistory().getAlertState(), AlertState.OK);
    assertEquals(1L, current.getOriginalTimestamp().longValue());
    assertEquals(1L, current.getLatestTimestamp().longValue());

    Long currentId = current.getAlertId();
    Long historyId = current.getAlertHistory().getAlertId();

    // no new history since the state is the same
    Alert alert3 = new Alert(ALERT_DEFINITION, null, SERVICE, COMPONENT, HOST1,
        AlertState.OK);
    alert3.setLabel(ALERT_LABEL);
    alert3.setText("Component component1 is OK");
    alert3.setTimestamp(2L);
    alert3.setClusterId(m_cluster.getClusterId());

    AlertReceivedEvent event3 = new AlertReceivedEvent(
        m_cluster.getClusterId(),
        alert3);

    listener.onAlertEvent(event3);

    current = m_dao.findCurrentByHostAndName(m_cluster.getClusterId(), HOST1,
        ALERT_DEFINITION);
    assertNotNull(current);
    assertEquals(currentId, current.getAlertId());
    assertEquals(historyId, current.getAlertHistory().getAlertId());
    assertEquals(HOST1, current.getAlertHistory().getHostName());
    assertEquals(ALERT_DEFINITION, current.getAlertHistory().getAlertDefinition().getDefinitionName());
    assertEquals(ALERT_LABEL, current.getAlertHistory().getAlertLabel());
    assertEquals("Component component1 is OK", current.getAlertHistory().getAlertText());
    assertEquals(current.getAlertHistory().getAlertState(), AlertState.OK);
    assertEquals(1L, current.getOriginalTimestamp().longValue());
    assertEquals(2L, current.getLatestTimestamp().longValue());

    allCurrent = m_dao.findCurrentByService(m_cluster.getClusterId(), SERVICE);
    assertEquals(2, allCurrent.size());

    allHistory = m_dao.findAll(m_cluster.getClusterId());
    assertEquals(2, allHistory.size());

    // change to warning
    Alert alert4 = new Alert(ALERT_DEFINITION, null, SERVICE, COMPONENT, HOST1,
        AlertState.WARNING);
    alert4.setLabel(ALERT_LABEL);
    alert4.setText("Component component1 is about to go down");
    alert4.setTimestamp(3L);
    alert4.setClusterId(m_cluster.getClusterId());


    AlertReceivedEvent event4 = new AlertReceivedEvent(
        m_cluster.getClusterId(),
        alert4);

    listener.onAlertEvent(event4);

    current = m_dao.findCurrentByHostAndName(m_cluster.getClusterId(), HOST1,
        ALERT_DEFINITION);
    assertNotNull(current);
    assertEquals(current.getAlertId(), currentId);
    assertFalse(historyId.equals(current.getAlertHistory().getAlertId()));
    assertEquals(HOST1, current.getAlertHistory().getHostName());
    assertEquals(ALERT_DEFINITION, current.getAlertHistory().getAlertDefinition().getDefinitionName());
    assertEquals(ALERT_LABEL, current.getAlertHistory().getAlertLabel());
    assertEquals("Component component1 is about to go down", current.getAlertHistory().getAlertText());
    assertEquals(current.getAlertHistory().getAlertState(), AlertState.WARNING);
    assertEquals(3L, current.getOriginalTimestamp().longValue());
    assertEquals(3L, current.getLatestTimestamp().longValue());

    allCurrent = m_dao.findCurrentByService(m_cluster.getClusterId(), SERVICE);
    assertEquals(2, allCurrent.size());

    allHistory = m_dao.findAll(m_cluster.getClusterId());
    assertEquals(3, allHistory.size());
  }
Tests the management of {@link AlertEvent}s in the system.
ambari-server/src/test/java/org/apache/ambari/server/state/cluster/AlertDataManagerTest.java