@Test
public void testAlertDefinitionMerging() throws Exception {

  Injector injector = Guice.createInjector(Modules.override(
      new InMemoryDefaultTestModule()).with(new MockModule()));
  EventBusSynchronizer.synchronizeAmbariEventPublisher(injector);
  injector.getInstance(GuiceJpaInitializer.class);
  injector.getInstance(EntityManager.class);
  long clusterId = injector.getInstance(OrmTestHelper.class).createCluster(
      "cluster" + System.currentTimeMillis());
  Class<?> c = metaInfo.getClass().getSuperclass();
  Field f = c.getDeclaredField("alertDefinitionDao");
  f.setAccessible(true);
  f.set(metaInfo, injector.getInstance(AlertDefinitionDAO.class));
  f = c.getDeclaredField("ambariServiceAlertDefinitions");
  f.setAccessible(true);
  f.set(metaInfo, injector.getInstance(AmbariServiceAlertDefinitions.class));
  Clusters clusters = injector.getInstance(Clusters.class);
  Cluster cluster = clusters.getClusterById(clusterId);
  cluster.setDesiredStackVersion(
      new StackId(STACK_NAME_HDP, "2.0.6"));
  cluster.addService("HDFS");
  metaInfo.reconcileAlertDefinitions(clusters);
  AlertDefinitionDAO dao = injector.getInstance(AlertDefinitionDAO.class);
  List<AlertDefinitionEntity> definitions = dao.findAll(clusterId);
  assertEquals(11, definitions.size());
  // figure out how many of these alerts were merged into from the
  // non-stack alerts.json
  int hostAlertCount = 0;
  for (AlertDefinitionEntity definition : definitions) {
    if (definition.getServiceName().equals("AMBARI")
        && definition.getComponentName().equals("AMBARI_AGENT")) {
      hostAlertCount++;
    }
  }
  assertEquals(2, hostAlertCount);
  assertEquals(9, definitions.size() - hostAlertCount);
  for (AlertDefinitionEntity definition : definitions) {
    definition.setScheduleInterval(28);
    dao.merge(definition);
  }
  metaInfo.reconcileAlertDefinitions(clusters);
  definitions = dao.findAll();
  assertEquals(11, definitions.size());
  for (AlertDefinitionEntity definition : definitions) {
    assertEquals(28, definition.getScheduleInterval().intValue());
  }
  // find all enabled for the cluster should find 6 (the ones from HDFS;
  // it will not find the agent alert since it's not bound to the cluster)
  definitions = dao.findAllEnabled(cluster.getClusterId());
  assertEquals(10, definitions.size());
  // create new definition
  AlertDefinitionEntity entity = new AlertDefinitionEntity();
  entity.setClusterId(clusterId);
  entity.setDefinitionName("bad_hdfs_alert");
  entity.setLabel("Bad HDFS Alert");
  entity.setDescription("A way to fake a component being removed");
  entity.setEnabled(true);
  entity.setHash(UUID.randomUUID().toString());
  entity.setScheduleInterval(1);
  entity.setServiceName("HDFS");
  entity.setComponentName("BAD_COMPONENT");
  entity.setSourceType(SourceType.METRIC);
  entity.setSource("{\"type\" : \"METRIC\"}");
  dao.create(entity);
  // verify the new definition is found (6 HDFS + 1 new one)
  definitions = dao.findAllEnabled(cluster.getClusterId());
  assertEquals(11, definitions.size());
  // reconcile, which should disable our bad definition
  metaInfo.reconcileAlertDefinitions(clusters);
  // find all enabled for the cluster should find 6
  definitions = dao.findAllEnabled(cluster.getClusterId());
  assertEquals(10, definitions.size());
  // find all should find 6 HDFS + 1 disabled + 1 agent alert + 2 server
  // alerts
  definitions = dao.findAll();
  assertEquals(12, definitions.size());
  entity = dao.findById(entity.getDefinitionId());
  assertFalse(entity.getEnabled());
}