ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nc...@apache.org
Subject git commit: AMBARI-6667. Unit test failures on jenkins for Ambari 1.7.0 related to alerts (Jonathan Hurley via ncole)
Date Wed, 30 Jul 2014 14:17:59 GMT
Repository: ambari
Updated Branches:
  refs/heads/trunk abca346b9 -> 657911553


AMBARI-6667. Unit test failures on jenkins for Ambari 1.7.0 related to alerts (Jonathan Hurley
via ncole)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/65791155
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/65791155
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/65791155

Branch: refs/heads/trunk
Commit: 6579115538861f8985f6cf702e48d33b15e06942
Parents: abca346
Author: Nate Cole <ncole@hortonworks.com>
Authored: Wed Jul 30 09:54:55 2014 -0400
Committer: Nate Cole <ncole@hortonworks.com>
Committed: Wed Jul 30 09:55:03 2014 -0400

----------------------------------------------------------------------
 .../server/orm/entities/AlertGroupEntity.java   |  56 ++++++--
 .../server/orm/entities/AlertTargetEntity.java  |  52 ++++---
 .../apache/ambari/server/orm/OrmTestHelper.java |  25 ++--
 .../server/orm/dao/AlertDefinitionDAOTest.java  | 140 ++-----------------
 .../server/orm/dao/AlertDispatchDAOTest.java    |  87 ++++++------
 .../ambari/server/orm/dao/AlertsDAOTest.java    |  30 ++--
 6 files changed, 156 insertions(+), 234 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/65791155/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertGroupEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertGroupEntity.java
b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertGroupEntity.java
index 4da552f..89f040c 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertGroupEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertGroupEntity.java
@@ -17,6 +17,7 @@
  */
 package org.apache.ambari.server.orm.entities;
 
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.Set;
 
@@ -79,12 +80,6 @@ public class AlertGroupEntity {
   private Set<AlertTargetEntity> alertTargets;
 
   /**
-   * Constructor.
-   */
-  public AlertGroupEntity() {
-  }
-
-  /**
    * Gets the unique ID of this grouping of alerts.
    * 
    * @return the ID (never {@code null}).
@@ -185,13 +180,44 @@ public class AlertGroupEntity {
   }
 
   /**
-   * Gets all of the targets that will receive notifications for alert
-   * definitions in this group.
+   * Gets an immutable set of the targets that will receive notifications for
+   * alert definitions in this group.
    * 
    * @return the targets, or {@code null} if there are none.
    */
   public Set<AlertTargetEntity> getAlertTargets() {
-    return alertTargets;
+    if( null == alertTargets )
+      return Collections.emptySet();
+      
+    return Collections.unmodifiableSet(alertTargets);
+  }
+
+  /**
+   * Adds the specified target to the targets that this group will dispatch to.
+   * 
+   * @param alertTarget
+   *          the target to add (not {@code null}).
+   */
+  public void addAlertTarget(AlertTargetEntity alertTarget) {
+    if (null == alertTargets)
+      alertTargets = new HashSet<AlertTargetEntity>();
+
+    alertTargets.add(alertTarget);
+    alertTarget.addAlertGroup(this);
+  }
+
+  /**
+   * Removes the specified target from the targets that this group will dispatch
+   * to.
+   * 
+   * @param alertTarget
+   *          the target to remove (not {@code null}).
+   */
+  public void removeAlertTarget(AlertTargetEntity alertTarget) {
+    if (null != alertTargets)
+      alertTargets.remove(alertTarget);
+
+    alertTarget.removeAlertGroup(this);
   }
 
   /**
@@ -202,15 +228,17 @@ public class AlertGroupEntity {
    *          the targets, or {@code null} if there are none.
    */
   public void setAlertTargets(Set<AlertTargetEntity> alertTargets) {
+    if (null != this.alertTargets) {
+      for (AlertTargetEntity target : this.alertTargets) {
+        target.removeAlertGroup(this);
+      }
+    }
+
     this.alertTargets = alertTargets;
 
     if (null != alertTargets) {
       for (AlertTargetEntity target : alertTargets) {
-        Set<AlertGroupEntity> groups = target.getAlertGroups();
-        if (null == groups)
-          groups = new HashSet<AlertGroupEntity>();
-
-        groups.add(this);
+        target.addAlertGroup(this);
       }
     }
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/65791155/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertTargetEntity.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertTargetEntity.java
b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertTargetEntity.java
index 91624f2..6f9ab89 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertTargetEntity.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/entities/AlertTargetEntity.java
@@ -17,6 +17,8 @@
  */
 package org.apache.ambari.server.orm.entities;
 
+import java.util.Collections;
+import java.util.HashSet;
 import java.util.Set;
 
 import javax.persistence.CascadeType;
@@ -65,17 +67,11 @@ public class AlertTargetEntity {
   /**
    * Bi-directional many-to-many association to {@link AlertGroupEntity}
    */
-  @ManyToMany(mappedBy = "alertTargets", cascade = { CascadeType.PERSIST,
-      CascadeType.MERGE })
+  @ManyToMany(mappedBy = "alertTargets", cascade = { CascadeType.MERGE,
+      CascadeType.REFRESH })
   private Set<AlertGroupEntity> alertGroups;
 
   /**
-   * Constructor.
-   */
-  public AlertTargetEntity() {
-  }
-
-  /**
    * Gets the unique ID of this alert target.
    * 
    * @return the ID of the target (never {@code null}).
@@ -161,24 +157,42 @@ public class AlertTargetEntity {
   }
 
   /**
-   * Gets all of the alert groups that this target is associated with.
+   * Gets an immutable set of the alert groups that this target is associated
+   * with.
    * 
    * @return the groups that will send to this target when an alert in that
-   *         group is received, or {@code null} for none.
+   *         group is received, or an empty set for none.
    */
   public Set<AlertGroupEntity> getAlertGroups() {
-    return alertGroups;
+    if (null == alertGroups)
+      return Collections.emptySet();
+
+    return Collections.unmodifiableSet(alertGroups);
+  }
+
+  /**
+   * Adds the specified alert group to the groups that this target is associated
+   * with. This is used to complement the JPA bidirectional association.
+   * 
+   * @param alertGroup
+   */
+  protected void addAlertGroup(AlertGroupEntity alertGroup) {
+    if (null == alertGroups)
+      alertGroups = new HashSet<AlertGroupEntity>();
+
+    alertGroups.add(alertGroup);
   }
 
   /**
-   * Sets the alert groups that this target is associated with.
+   * Removes the specified alert group to the groups that this target is
+   * associated with. This is used to complement the JPA bidirectional
+   * association.
    * 
-   * @param alertGroups
-   *          the groups that will send to this target when an alert in that
-   *          group is received, or {@code null} for none.
+   * @param alertGroup
    */
-  public void setAlertGroups(Set<AlertGroupEntity> alertGroups) {
-    this.alertGroups = alertGroups;
+  protected void removeAlertGroup(AlertGroupEntity alertGroup) {
+    if (null != alertGroups)
+      alertGroups.remove(alertGroup);
   }
 
   /**
@@ -192,9 +206,7 @@ public class AlertTargetEntity {
       return;
 
     for (AlertGroupEntity group : groups) {
-      Set<AlertTargetEntity> targets = group.getAlertTargets();
-      if (null != targets)
-        targets.remove(this);
+      group.removeAlertTarget(this);
     }
   }
 

http://git-wip-us.apache.org/repos/asf/ambari/blob/65791155/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java b/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java
index 0258e47..f3a0b0f 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/OrmTestHelper.java
@@ -33,6 +33,7 @@ import org.apache.ambari.server.RoleCommand;
 import org.apache.ambari.server.actionmanager.HostRoleStatus;
 import org.apache.ambari.server.orm.dao.AlertDefinitionDAO;
 import org.apache.ambari.server.orm.dao.AlertDispatchDAO;
+import org.apache.ambari.server.orm.dao.AlertsDAO;
 import org.apache.ambari.server.orm.dao.ClusterDAO;
 import org.apache.ambari.server.orm.dao.HostDAO;
 import org.apache.ambari.server.orm.dao.HostRoleCommandDAO;
@@ -83,6 +84,12 @@ public class OrmTestHelper {
   @Inject
   public AlertDefinitionDAO alertDefinitionDAO;
 
+  @Inject
+  public AlertDispatchDAO alertDispatchDAO;
+
+  @Inject
+  public AlertsDAO alertsDAO;
+
   public EntityManager getEntityManager() {
     return entityManagerProvider.get();
   }
@@ -285,10 +292,8 @@ public class OrmTestHelper {
     target.setProperties("Target Properties");
     target.setTargetName("Target Name " + System.currentTimeMillis());
 
-    AlertDispatchDAO dao = injector.getInstance(AlertDispatchDAO.class);
-    dao.create(target);
-
-    return dao.findTargetById(target.getTargetId());
+    alertDispatchDAO.create(target);
+    return alertDispatchDAO.findTargetById(target.getTargetId());
   }
   
   /**
@@ -313,10 +318,8 @@ public class OrmTestHelper {
     definition.setSource("Source " + System.currentTimeMillis());
     definition.setSourceType("SCRIPT");
     
-    AlertDefinitionDAO dao = injector.getInstance(AlertDefinitionDAO.class);
-    dao.create(definition);
-
-    return dao.findById(definition.getDefinitionId());
+    alertDefinitionDAO.create(definition);
+    return alertDefinitionDAO.findById(definition.getDefinitionId());
   }
 
   /**
@@ -336,9 +339,7 @@ public class OrmTestHelper {
     group.setClusterId(clusterId);
     group.setAlertTargets(targets);
 
-    AlertDispatchDAO dao = injector.getInstance(AlertDispatchDAO.class);
-    dao.create(group);
-
-    return dao.findGroupById(group.getGroupId());
+    alertDispatchDAO.create(group);
+    return alertDispatchDAO.findGroupById(group.getGroupId());
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/65791155/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAOTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAOTest.java
index 4b1f4be..f2ddcd7 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDefinitionDAOTest.java
@@ -18,27 +18,16 @@
 
 package org.apache.ambari.server.orm.dao;
 
-import static org.easymock.EasyMock.createStrictMock;
-import static org.easymock.EasyMock.eq;
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.replay;
-import static org.easymock.EasyMock.reset;
-import static org.easymock.EasyMock.verify;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertSame;
 
 import java.util.Calendar;
-import java.util.Collections;
 import java.util.Date;
 import java.util.List;
 import java.util.TimeZone;
 import java.util.UUID;
 
-import javax.persistence.EntityManager;
-import javax.persistence.TypedQuery;
-
 import org.apache.ambari.server.orm.GuiceJpaInitializer;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.OrmTestHelper;
@@ -51,42 +40,38 @@ import org.apache.ambari.server.state.AlertState;
 import org.apache.ambari.server.state.MaintenanceState;
 import org.apache.ambari.server.state.NotificationState;
 import org.apache.ambari.server.state.alert.Scope;
-import org.junit.AfterClass;
+import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
-import org.junit.BeforeClass;
 import org.junit.Test;
 
 import com.google.inject.Guice;
 import com.google.inject.Injector;
-import com.google.inject.Provider;
 import com.google.inject.persist.PersistService;
 
 /**
  * Tests {@link AlertDefinitionDAO} for interacting with
  * {@link AlertDefinitionEntity}.
  */
-@SuppressWarnings("unchecked")
 public class AlertDefinitionDAOTest {
-  static Injector injector;
-  static Long clusterId;
-  static AlertDefinitionDAO dao;
-  static AlertsDAO alertsDao;
-  static AlertDispatchDAO dispatchDao;
-  static OrmTestHelper helper;
+
   static Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
 
-  AlertDefinitionDAO mockDAO;
-  Provider<EntityManager> mockEntityManagerProvider = createStrictMock(Provider.class);
-  EntityManager entityManager = createStrictMock(EntityManager.class);
+  Injector injector;
+  Long clusterId;
+  AlertDefinitionDAO dao;
+  AlertsDAO alertsDao;
+  AlertDispatchDAO dispatchDao;
+  OrmTestHelper helper;
 
   /**
    * 
    */
-  @BeforeClass
-  public static void beforeClass() {
+  @Before
+  public void setup() {
     injector = Guice.createInjector(new InMemoryDefaultTestModule());
     injector.getInstance(GuiceJpaInitializer.class);
+
     dao = injector.getInstance(AlertDefinitionDAO.class);
     alertsDao = injector.getInstance(AlertsDAO.class);
     dispatchDao = injector.getInstance(AlertDispatchDAO.class);
@@ -108,8 +93,8 @@ public class AlertDefinitionDAOTest {
     }
   }
 
-  @AfterClass
-  public static void afterClass() {
+  @After
+  public void teardown() {
     injector.getInstance(PersistService.class).stop();
     injector = null;
   }
@@ -117,47 +102,8 @@ public class AlertDefinitionDAOTest {
   /**
    * 
    */
-  @Before
-  public void before() {
-    injector = Guice.createInjector(new InMemoryDefaultTestModule());
-    injector.getInstance(GuiceJpaInitializer.class);
-    injector.injectMembers(this);
-
-    reset(mockEntityManagerProvider);
-    expect(mockEntityManagerProvider.get()).andReturn(entityManager).atLeastOnce();
-    replay(mockEntityManagerProvider);
-
-    mockDAO = injector.getInstance(AlertDefinitionDAO.class);
-    mockDAO.entityManagerProvider = mockEntityManagerProvider;
-  }
-
-  /**
-   * 
-   */
   @Test
   public void testFindByName() {
-    AlertDefinitionEntity entity = new AlertDefinitionEntity();
-    TypedQuery<AlertDefinitionEntity> query = createStrictMock(TypedQuery.class);
-
-    expect(query.setParameter("clusterId", 12345L)).andReturn(query);
-
-    expect(query.setParameter("definitionName", "alert-definition-1")).andReturn(
-        query);
-
-    expect(query.getSingleResult()).andReturn(entity);
-
-    expect(
-        entityManager.createNamedQuery(eq("AlertDefinitionEntity.findByName"),
-            eq(AlertDefinitionEntity.class))).andReturn(query);
-
-    replay(query, entityManager);
-
-    AlertDefinitionEntity result = mockDAO.findByName(12345L,
-        "alert-definition-1");
-
-    assertSame(result, entity);
-    verify(mockEntityManagerProvider, entityManager);
-
     List<AlertDefinitionEntity> definitions = dao.findAll();
     Assert.assertNotNull(definitions);
     AlertDefinitionEntity definition = definitions.get(2);
@@ -172,23 +118,6 @@ public class AlertDefinitionDAOTest {
    */
   @Test
   public void testFindAll() {
-    AlertDefinitionEntity entity = new AlertDefinitionEntity();
-    TypedQuery<AlertDefinitionEntity> query = createStrictMock(TypedQuery.class);
-
-    expect(query.getResultList()).andReturn(Collections.singletonList(entity));
-
-    expect(
-        entityManager.createNamedQuery(eq("AlertDefinitionEntity.findAll"),
-            eq(AlertDefinitionEntity.class))).andReturn(query);
-
-    replay(query, entityManager);
-
-    List<AlertDefinitionEntity> entities = mockDAO.findAll();
-
-    assertSame(1, entities.size());
-    assertSame(entity, entities.get(0));
-    verify(mockEntityManagerProvider, entityManager);
-
     List<AlertDefinitionEntity> definitions = dao.findAll();
     Assert.assertNotNull(definitions);
     Assert.assertEquals(8, definitions.size());
@@ -199,18 +128,6 @@ public class AlertDefinitionDAOTest {
    */
   @Test
   public void findById() {
-    AlertDefinitionEntity entity = new AlertDefinitionEntity();
-
-    expect(entityManager.find(eq(AlertDefinitionEntity.class), eq(12345L))).andReturn(
-        entity);
-
-    replay(entityManager);
-
-    AlertDefinitionEntity result = mockDAO.findById(12345L);
-
-    assertSame(result, entity);
-    verify(mockEntityManagerProvider, entityManager);
-
     List<AlertDefinitionEntity> definitions = dao.findAll();
     Assert.assertNotNull(definitions);
     AlertDefinitionEntity definition = definitions.get(2);
@@ -221,45 +138,14 @@ public class AlertDefinitionDAOTest {
 
   @Test
   public void testRefresh() {
-    AlertDefinitionEntity entity = new AlertDefinitionEntity();
-
-    // set expectations
-    entityManager.refresh(eq(entity));
-    replay(entityManager);
-
-    mockDAO.entityManagerProvider = mockEntityManagerProvider;
-    mockDAO.refresh(entity);
-
-    verify(mockEntityManagerProvider, entityManager);
   }
 
   @Test
   public void testCreate() {
-    AlertDefinitionEntity entity = new AlertDefinitionEntity();
-
-    // set expectations
-    entityManager.persist(eq(entity));
-    replay(entityManager);
-
-    mockDAO.entityManagerProvider = mockEntityManagerProvider;
-    mockDAO.create(entity);
-
-    verify(mockEntityManagerProvider, entityManager);
   }
 
   @Test
   public void testMerge() {
-    AlertDefinitionEntity entity = new AlertDefinitionEntity();
-    AlertDefinitionEntity entity2 = new AlertDefinitionEntity();
-
-    // set expectations
-    expect(entityManager.merge(eq(entity))).andReturn(entity2);
-    replay(entityManager);
-
-    mockDAO.entityManagerProvider = mockEntityManagerProvider;
-    assertSame(entity2, mockDAO.merge(entity));
-
-    verify(mockEntityManagerProvider, entityManager);
   }
 
   @Test

http://git-wip-us.apache.org/repos/asf/ambari/blob/65791155/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDispatchDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDispatchDAOTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDispatchDAOTest.java
index cf25bd5..894f2c2 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDispatchDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertDispatchDAOTest.java
@@ -23,7 +23,6 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
-import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
@@ -37,9 +36,8 @@ import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
 import org.apache.ambari.server.orm.entities.AlertGroupEntity;
 import org.apache.ambari.server.orm.entities.AlertTargetEntity;
 import org.apache.ambari.server.state.alert.Scope;
-import org.junit.AfterClass;
+import org.junit.After;
 import org.junit.Before;
-import org.junit.BeforeClass;
 import org.junit.Test;
 
 import com.google.inject.Guice;
@@ -51,50 +49,44 @@ import com.google.inject.persist.PersistService;
  */
 public class AlertDispatchDAOTest {
 
-  static Long clusterId;
-  static Injector injector;
-  static AlertDispatchDAO dao;
-  static OrmTestHelper helper;
-
+  Long clusterId;
+  Injector injector;
+  AlertDispatchDAO dao;
   AlertDefinitionDAO definitionDao;
+  OrmTestHelper helper;
 
   /**
    * 
    */
-  @BeforeClass
-  public static void beforeClass() {
+  @Before
+  public void setup() throws Exception {
     injector = Guice.createInjector(new InMemoryDefaultTestModule());
     injector.getInstance(GuiceJpaInitializer.class);
-    helper = injector.getInstance(OrmTestHelper.class);
-    clusterId = helper.createCluster();
     dao = injector.getInstance(AlertDispatchDAO.class);
+    definitionDao = injector.getInstance(AlertDefinitionDAO.class);
+    helper = injector.getInstance(OrmTestHelper.class);
 
-    Set<AlertTargetEntity> targets = new HashSet<AlertTargetEntity>();
-    for (int i = 0; i < 5; i++) {
-      AlertTargetEntity target = new AlertTargetEntity();
-      target.setDescription("Target Description " + i);
-      target.setNotificationType("EMAIL");
-      target.setProperties("Target Properties " + i);
-      target.setTargetName("Target Name " + i);
-      dao.create(target);
-      targets.add(target);
-    }
+    clusterId = helper.createCluster();
+    Set<AlertTargetEntity> targets = createTargets();
 
     for (int i = 0; i < 10; i++) {
       AlertGroupEntity group = new AlertGroupEntity();
       group.setDefault(false);
       group.setGroupName("Group Name " + i);
       group.setClusterId(clusterId);
-      group.setAlertTargets(targets);
+      for (AlertTargetEntity alertTarget : targets) {
+        group.addAlertTarget(alertTarget);
+      }
+
       dao.create(group);
     }
   }
 
   /**
-   * 
+   * @throws Exception
    */
-  @AfterClass
-  public static void afterClass() {
+  @After
+  public void teardown() throws Exception {
     injector.getInstance(PersistService.class).stop();
     injector = null;
   }
@@ -102,14 +94,6 @@ public class AlertDispatchDAOTest {
   /**
    * 
    */
-  @Before
-  public void setup() {
-    definitionDao = injector.getInstance(AlertDefinitionDAO.class);
-  }
-
-  /**
-   * 
-   */
   @Test
   public void testFindAllTargets() throws Exception {
     List<AlertTargetEntity> targets = dao.findAllTargets();
@@ -248,15 +232,12 @@ public class AlertDispatchDAOTest {
     AlertTargetEntity target = helper.createAlertTarget();
     assertEquals(targetCount + 1, dao.findAllTargets().size());
 
-    Set<AlertTargetEntity> targets = new HashSet<AlertTargetEntity>();
-    targets.add(target);
-
     group = dao.findGroupById(group.getGroupId());
     assertNotNull(group);
     assertNotNull(group.getAlertTargets());
     assertEquals(0, group.getAlertTargets().size());
 
-    group.setAlertTargets(targets);
+    group.addAlertTarget(target);
     dao.merge(group);
 
     group = dao.findGroupById(group.getGroupId());
@@ -298,9 +279,13 @@ public class AlertDispatchDAOTest {
     targets.add(target);
 
     AlertGroupEntity group = helper.createAlertGroup(clusterId, targets);
+    assertEquals(1, group.getAlertTargets().size());
 
     target = dao.findTargetById(target.getTargetId());
+    dao.refresh(target);
+
     assertNotNull(target);
+    assertEquals(1, target.getAlertGroups().size());
 
     dao.remove(target);
     target = dao.findTargetById(target.getTargetId());
@@ -339,7 +324,7 @@ public class AlertDispatchDAOTest {
     assertEquals(0, group.getAlertDefinitions().size());
     assertEquals(0, group.getAlertTargets().size());
 
-    group.getAlertTargets().add(target);
+    group.addAlertTarget(target);
     dao.merge(group);
 
     group = dao.findGroupById(group.getGroupId());
@@ -350,8 +335,6 @@ public class AlertDispatchDAOTest {
    * @return
    */
   private List<AlertDefinitionEntity> createDefinitions() throws Exception {
-    List<AlertDefinitionEntity> alertDefinitions = new ArrayList<AlertDefinitionEntity>();
-
     for (int i = 0; i < 8; i++) {
       AlertDefinitionEntity definition = new AlertDefinitionEntity();
       definition.setDefinitionName("Alert Definition " + i);
@@ -364,9 +347,29 @@ public class AlertDispatchDAOTest {
       definition.setSource("Source " + i);
       definition.setSourceType("SCRIPT");
       definitionDao.create(definition);
-      alertDefinitions.add(definition);
     }
 
+    List<AlertDefinitionEntity> alertDefinitions = definitionDao.findAll();
+    assertEquals(8, alertDefinitions.size());
     return alertDefinitions;
   }
+
+  /**
+   * @return
+   * @throws Exception
+   */
+  private Set<AlertTargetEntity> createTargets() throws Exception {
+    Set<AlertTargetEntity> targets = new HashSet<AlertTargetEntity>();
+    for (int i = 0; i < 5; i++) {
+      AlertTargetEntity target = new AlertTargetEntity();
+      target.setDescription("Target Description " + i);
+      target.setNotificationType("EMAIL");
+      target.setProperties("Target Properties " + i);
+      target.setTargetName("Target Name " + i);
+      dao.create(target);
+      targets.add(target);
+    }
+
+    return targets;
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/65791155/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertsDAOTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertsDAOTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertsDAOTest.java
index 1366f15..caeada1 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertsDAOTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/orm/dao/AlertsDAOTest.java
@@ -39,9 +39,8 @@ import org.apache.ambari.server.orm.entities.AlertHistoryEntity;
 import org.apache.ambari.server.state.AlertState;
 import org.apache.ambari.server.state.MaintenanceState;
 import org.apache.ambari.server.state.alert.Scope;
-import org.junit.AfterClass;
+import org.junit.After;
 import org.junit.Before;
-import org.junit.BeforeClass;
 import org.junit.Test;
 
 import com.google.inject.Guice;
@@ -53,26 +52,25 @@ import com.google.inject.persist.PersistService;
  */
 public class AlertsDAOTest {
 
-  static Long clusterId;
-  static Injector injector;
   static Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
-  static OrmTestHelper helper;
-  static AlertsDAO dao;
-  static AlertDefinitionDAO definitionDao;
-  static AlertDispatchDAO dispatchDao;
+
+  private Long clusterId;
+  private Injector injector;
+  private OrmTestHelper helper;
+  private AlertsDAO dao;
+  private AlertDefinitionDAO definitionDao;
 
   /**
    * 
    */
-  @BeforeClass
-  public static void beforeClass() {
+  @Before
+  public void setup() throws Exception {
     injector = Guice.createInjector(new InMemoryDefaultTestModule());
     injector.getInstance(GuiceJpaInitializer.class);
     helper = injector.getInstance(OrmTestHelper.class);
     clusterId = helper.createCluster();
     dao = injector.getInstance(AlertsDAO.class);
     definitionDao = injector.getInstance(AlertDefinitionDAO.class);
-    dispatchDao = injector.getInstance(AlertDispatchDAO.class);
 
     // create 5 definitions
     for (int i = 0; i < 5; i++) {
@@ -143,18 +141,12 @@ public class AlertsDAOTest {
   /**
    * 
    */
-  @AfterClass
-  public static void afterClass() {
+  @After
+  public void teardown() {
     injector.getInstance(PersistService.class).stop();
     injector = null;
   }
 
-  /**
-   * 
-   */
-  @Before
-  public void setup() {
-  }
 
   /**
    * 


Mime
View raw message