@Test
  public void testAlertNoticeCreationFromEvent() throws Exception {
    AlertDefinition aggregateDefinition = getAggregateAlertDefinition();
    AlertCurrentEntity currentEntityMock = EasyMock.createNiceMock(AlertCurrentEntity.class);
    AlertHistoryEntity historyEntityMock = EasyMock.createNiceMock(AlertHistoryEntity.class);

    EasyMock.expect(currentEntityMock.getAlertHistory()).andReturn(historyEntityMock).atLeastOnce();

    EasyMock.expect(
        m_aggregateMapping.getAggregateDefinition(EasyMock.anyLong(), EasyMock.eq("mock-alert"))).andReturn(
        aggregateDefinition).atLeastOnce();

    AlertSummaryDTO summaryDTO = new AlertSummaryDTO(5,0,0,0,0);
    EasyMock.expect(
        m_alertsDao.findAggregateCounts(EasyMock.anyLong(), EasyMock.eq("mock-aggregate-alert"))).andReturn(
        summaryDTO).atLeastOnce();
    m_alertsDao.saveEntities(EasyMock.anyObject(), EasyMock.anyObject());
    EasyMock.expectLastCall().anyTimes();

    EasyMock.replay(m_alertsDao, m_aggregateMapping, currentEntityMock);

    // check that we're starting at 0
    Assert.assertEquals(0, m_listener.getAlertEventReceivedCount(AlertReceivedEvent.class));

    // trigger an alert which will trigger the aggregate
    Alert alert = new Alert("mock-alert", null, null, null, null, null);
    AlertAggregateListener aggregateListener = m_injector.getInstance(AlertAggregateListener.class);
    AlertStateChangeEvent event = new AlertStateChangeEvent(0, alert, currentEntityMock, null,
        AlertFirmness.HARD);
    aggregateListener.onAlertStateChangeEvent(event);

    // verify that one AlertReceivedEvent was fired (it's the one the listener
    // creates for the aggregate)
    Assert.assertEquals(1, m_listener.getAlertEventReceivedCount(AlertReceivedEvent.class));

    // fire the same alert event again; the cache in the aggregate listener
    // should prevent it from firing a new alert received event of its own
    aggregateListener.onAlertStateChangeEvent(event);

    // check that we're still at 1
    Assert.assertEquals(1, m_listener.getAlertEventReceivedCount(AlertReceivedEvent.class));

    // now change the returned summary DTO so that a new alert will get generated
    summaryDTO.setOkCount(0);
    summaryDTO.setCriticalCount(5);
    aggregateListener.onAlertStateChangeEvent(event);
    Assert.assertEquals(2, m_listener.getAlertEventReceivedCount(AlertReceivedEvent.class));
  }
Tests that the {@link AlertAggregateListener} caches values of the
aggregates and only triggers events when needed.

@throws Exception
ambari-server/src/test/java/org/apache/ambari/server/state/alerts/AggregateAlertListenerTest.java