@Test
  public void testAlertDefinitionMerging() throws Exception {
    final String stackVersion = "2.0.6";
    final String repoVersion = "2.0.6-1234";

    Injector injector = Guice.createInjector(Modules.override(
        new InMemoryDefaultTestModule()).with(new MockModule()));

    EventBusSynchronizer.synchronizeAmbariEventPublisher(injector);

    injector.getInstance(GuiceJpaInitializer.class);
    injector.getInstance(EntityManager.class);

    OrmTestHelper ormHelper = injector.getInstance(OrmTestHelper.class);
    long clusterId = ormHelper.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, stackVersion));

    RepositoryVersionEntity repositoryVersion = ormHelper.getOrCreateRepositoryVersion(
        cluster.getCurrentStackVersion(), repoVersion);

    cluster.addService("HDFS", repositoryVersion);

    metaInfo.reconcileAlertDefinitions(clusters, false);

    AlertDefinitionDAO dao = injector.getInstance(AlertDefinitionDAO.class);
    List<AlertDefinitionEntity> definitions = dao.findAll(clusterId);
    assertEquals(13, 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(3, hostAlertCount);
    assertEquals(10, definitions.size() - hostAlertCount);

    for (AlertDefinitionEntity definition : definitions) {
      definition.setScheduleInterval(28);
      dao.merge(definition);
    }

    metaInfo.reconcileAlertDefinitions(clusters, false);

    definitions = dao.findAll();
    assertEquals(13, 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(12, 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(13, definitions.size());

    // reconcile, which should disable our bad definition
    metaInfo.reconcileAlertDefinitions(clusters, false);

    // find all enabled for the cluster should find 6
    definitions = dao.findAllEnabled(cluster.getClusterId());
    assertEquals(12, definitions.size());

    // find all should find 6 HDFS + 1 disabled + 1 agent alert + 2 server
    // alerts
    definitions = dao.findAll();
    assertEquals(14, definitions.size());

    entity = dao.findById(entity.getDefinitionId());
    assertFalse(entity.getEnabled());
  }
Tests merging stack-based with existing definitions works

@throws Exception
ambari-server/src/test/java/org/apache/ambari/server/api/services/AmbariMetaInfoTest.java