@Test
  @SuppressWarnings("unchecked")
  public void testMergeIntoCacheOnly() throws Exception {
    EntityManager entityManager = m_injector.getInstance(EntityManager.class);
    DaoUtils daoUtils = m_injector.getInstance(DaoUtils.class);

    AlertHistoryEntity history = EasyMock.createNiceMock(AlertHistoryEntity.class);
    AlertDefinitionEntity definition = EasyMock.createNiceMock(AlertDefinitionEntity.class);
    mock(definition, history);

    AlertCurrentEntity jpaCurrent = new AlertCurrentEntity();
    jpaCurrent.setAlertHistory(history);
    jpaCurrent.setOriginalTimestamp(1L);
    jpaCurrent.setLatestTimestamp(2L);

    AlertCurrentEntity memoryCurrent = new AlertCurrentEntity();
    memoryCurrent.setAlertHistory(history);
    memoryCurrent.setOriginalTimestamp(1L);
    memoryCurrent.setLatestTimestamp(3L);

    // mock the EM; notice we do not mock merge since this should not call merge
    TypedQuery<AlertCurrentEntity> typedQuery = EasyMock.createNiceMock(TypedQuery.class);
    EasyMock.expect(entityManager.createNamedQuery(CachedAlertTestArea.FIND_ALL.getNamedQuery(),
        AlertCurrentEntity.class)).andReturn(typedQuery).atLeastOnce();

    // create the data to return from mocked JPA
    List<AlertCurrentEntity> jpaCurrentAlerts = Lists.newArrayList(jpaCurrent);
    EasyMock.expect(daoUtils.selectList(typedQuery)).andReturn(jpaCurrentAlerts).atLeastOnce();

    EasyMock.replay(entityManager, daoUtils, typedQuery);

    // invoke merge on our in-memory current entity
    AlertsDAO alertsDAO = m_injector.getInstance(AlertsDAO.class);
    alertsDAO.merge(memoryCurrent, true);
    List<AlertCurrentEntity> testCurrentAlerts = alertsDAO.findCurrent();

    // verify that the stale JPA data is augemented with the cached data
    Assert.assertEquals(1, testCurrentAlerts.size());
    Assert.assertEquals(Long.valueOf(3), testCurrentAlerts.get(0).getLatestTimestamp());

    EasyMock.verify(definition, history, entityManager, daoUtils);
  }
Tests that finding alerts by service supplements with the cache.

@throws Exception
ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertsDAOCachedTest.java