You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by jo...@apache.org on 2014/12/09 16:19:03 UTC

ambari git commit: AMBARI-8595 - AlertTargets Should Allow AlertGroup Associations To Be Modified From POST (jonathanhurley)

Repository: ambari
Updated Branches:
  refs/heads/trunk f3aa618f9 -> 3022e2749


AMBARI-8595 - AlertTargets Should Allow AlertGroup Associations To Be Modified From POST (jonathanhurley)


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

Branch: refs/heads/trunk
Commit: 3022e2749460089896f681bd64ed549237fd1f9d
Parents: f3aa618
Author: Jonathan Hurley <jh...@hortonworks.com>
Authored: Mon Dec 8 16:55:17 2014 -0500
Committer: Jonathan Hurley <jh...@hortonworks.com>
Committed: Tue Dec 9 10:18:53 2014 -0500

----------------------------------------------------------------------
 .../internal/AlertTargetResourceProvider.java   |  22 ++++
 .../ambari/server/orm/dao/AlertDispatchDAO.java |  16 +++
 .../server/orm/entities/AlertGroupEntity.java   |   3 +-
 .../server/orm/entities/AlertTargetEntity.java  |  21 ++++
 .../AlertTargetResourceProviderTest.java        | 111 +++++++++++++++++++
 .../server/orm/dao/AlertDispatchDAOTest.java    |  18 +++
 6 files changed, 190 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/3022e274/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AlertTargetResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AlertTargetResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AlertTargetResourceProvider.java
index adb761b..4473dc1 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AlertTargetResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AlertTargetResourceProvider.java
@@ -273,6 +273,14 @@ public class AlertTargetResourceProvider extends
         alertStateSet = EnumSet.allOf(AlertState.class);
       }
 
+      // groups are not required on creation
+      if (requestMap.containsKey(ALERT_TARGET_GROUPS)) {
+        List<Long> groupIds = (List<Long>) requestMap.get(ALERT_TARGET_GROUPS);
+        Set<AlertGroupEntity> groups = new HashSet<AlertGroupEntity>();
+        groups.addAll(s_dao.findGroupsById(groupIds));
+        entity.setAlertGroups(groups);
+      }
+
       entity.setDescription(description);
       entity.setNotificationType(notificationType);
       entity.setProperties(properties);
@@ -319,6 +327,7 @@ public class AlertTargetResourceProvider extends
       String description = (String) requestMap.get(ALERT_TARGET_DESCRIPTION);
       String notificationType = (String) requestMap.get(ALERT_TARGET_NOTIFICATION_TYPE);
       Collection<String> alertStates = (Collection<String>) requestMap.get(ALERT_TARGET_STATES);
+      Collection<Long> groupIds = (Collection<Long>) requestMap.get(ALERT_TARGET_GROUPS);
 
       if (!StringUtils.isBlank(name)) {
         entity.setTargetName(name);
@@ -354,6 +363,19 @@ public class AlertTargetResourceProvider extends
         entity.setAlertStates(alertStateSet);
       }
 
+      // if groups were supplied, replace existing
+      if (null != groupIds) {
+        Set<AlertGroupEntity> groups = new HashSet<AlertGroupEntity>();
+
+        List<Long> ids = new ArrayList<Long>(groupIds);
+
+        if (ids.size() > 0) {
+          groups.addAll(s_dao.findGroupsById(ids));
+        }
+
+        entity.setAlertGroups(groups);
+      }
+
       s_dao.merge(entity);
     }
   }

http://git-wip-us.apache.org/repos/asf/ambari/blob/3022e274/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDispatchDAO.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDispatchDAO.java b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDispatchDAO.java
index 35c9ae4..d355d3d 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDispatchDAO.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/orm/dao/AlertDispatchDAO.java
@@ -94,6 +94,22 @@ public class AlertDispatchDAO {
   }
 
   /**
+   * Gets all of the alert groups for the list of IDs given.
+   *
+   * @param groupIds
+   *          the IDs of the groups to retrieve.
+   * @return the groups or an empty list (never {@code null}).
+   */
+  public List<AlertGroupEntity> findGroupsById(List<Long> groupIds) {
+    TypedQuery<AlertGroupEntity> query = entityManagerProvider.get().createNamedQuery(
+        "AlertGroupEntity.findByIds", AlertGroupEntity.class);
+
+    query.setParameter("groupIds", groupIds);
+
+    return daoUtils.selectList(query);
+  }
+
+  /**
    * Gets an alert target with the specified ID.
    *
    * @param targetId

http://git-wip-us.apache.org/repos/asf/ambari/blob/3022e274/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 6607fb0..7d2c6c7 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
@@ -51,7 +51,8 @@ import javax.persistence.UniqueConstraint;
     @NamedQuery(name = "AlertGroupEntity.findByName", query = "SELECT alertGroup FROM AlertGroupEntity alertGroup WHERE alertGroup.groupName = :groupName"),
     @NamedQuery(name = "AlertGroupEntity.findByNameInCluster", query = "SELECT alertGroup FROM AlertGroupEntity alertGroup WHERE alertGroup.groupName = :groupName AND alertGroup.clusterId = :clusterId"),
     @NamedQuery(name = "AlertGroupEntity.findByAssociatedDefinition", query = "SELECT alertGroup FROM AlertGroupEntity alertGroup WHERE :alertDefinition MEMBER OF alertGroup.alertDefinitions"),
-    @NamedQuery(name = "AlertGroupEntity.findServiceDefaultGroup", query = "SELECT alertGroup FROM AlertGroupEntity alertGroup WHERE alertGroup.clusterId = :clusterId AND alertGroup.serviceName = :serviceName AND alertGroup.isDefault = 1") })
+    @NamedQuery(name = "AlertGroupEntity.findServiceDefaultGroup", query = "SELECT alertGroup FROM AlertGroupEntity alertGroup WHERE alertGroup.clusterId = :clusterId AND alertGroup.serviceName = :serviceName AND alertGroup.isDefault = 1"),
+    @NamedQuery(name = "AlertGroupEntity.findByIds", query = "SELECT alertGroup FROM AlertGroupEntity alertGroup WHERE alertGroup.groupId IN :groupIds") })
 public class AlertGroupEntity {
 
   @Id

http://git-wip-us.apache.org/repos/asf/ambari/blob/3022e274/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 146fe49..e1248db 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
@@ -248,6 +248,27 @@ public class AlertTargetEntity {
   }
 
   /**
+   * Sets all of the groups that are associated with this target.
+   *
+   * @param alertGroups
+   *          the groups, or {@code null} if there are none.
+   */
+  public void setAlertGroups(Set<AlertGroupEntity> alertGroups) {
+    Set<AlertGroupEntity> groups = getAlertGroups();
+    for (AlertGroupEntity group : groups) {
+      group.removeAlertTarget(this);
+    }
+
+    this.alertGroups = alertGroups;
+
+    if (null != alertGroups) {
+      for (AlertGroupEntity group : alertGroups) {
+        group.addAlertTarget(this);
+      }
+    }
+  }
+
+  /**
    * Adds the specified alert group to the groups that this target is associated
    * with. This is used to complement the JPA bidirectional association.
    *

http://git-wip-us.apache.org/repos/asf/ambari/blob/3022e274/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AlertTargetResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AlertTargetResourceProviderTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AlertTargetResourceProviderTest.java
index 367e6d4..2d08a7a 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AlertTargetResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AlertTargetResourceProviderTest.java
@@ -50,6 +50,7 @@ import org.apache.ambari.server.controller.utilities.PropertyHelper;
 import org.apache.ambari.server.metadata.ActionMetadata;
 import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
 import org.apache.ambari.server.orm.dao.AlertDispatchDAO;
+import org.apache.ambari.server.orm.entities.AlertGroupEntity;
 import org.apache.ambari.server.orm.entities.AlertTargetEntity;
 import org.apache.ambari.server.state.AlertState;
 import org.apache.ambari.server.state.Cluster;
@@ -239,6 +240,57 @@ public class AlertTargetResourceProviderTest {
    * @throws Exception
    */
   @Test
+  public void testCreateResourcesWithGroups() throws Exception {
+    Capture<List<AlertTargetEntity>> listCapture = new Capture<List<AlertTargetEntity>>();
+
+    List<Long> groupIds = Arrays.asList(1L, 2L, 3L);
+    List<AlertGroupEntity> groups = new ArrayList<AlertGroupEntity>();
+    AlertGroupEntity group1 = new AlertGroupEntity();
+    AlertGroupEntity group2 = new AlertGroupEntity();
+    AlertGroupEntity group3 = new AlertGroupEntity();
+    group1.setGroupId(1L);
+    group2.setGroupId(2L);
+    group3.setGroupId(3L);
+    groups.addAll(Arrays.asList(group1, group2, group3));
+    expect(m_dao.findGroupsById(groupIds)).andReturn(groups).once();
+
+    m_dao.createTargets(capture(listCapture));
+    expectLastCall();
+
+    replay(m_amc, m_dao);
+
+    AlertTargetResourceProvider provider = createProvider(m_amc);
+    Map<String, Object> requestProps = getCreationProperties();
+
+    // add the group IDs to the request so that we're associating groups
+    requestProps.put(AlertTargetResourceProvider.ALERT_TARGET_GROUPS, groupIds);
+
+    Request request = PropertyHelper.getCreateRequest(Collections.singleton(requestProps), null);
+    provider.createResources(request);
+
+    Assert.assertTrue(listCapture.hasCaptured());
+    AlertTargetEntity entity = listCapture.getValue().get(0);
+    Assert.assertNotNull(entity);
+
+    assertEquals(ALERT_TARGET_NAME, entity.getTargetName());
+    assertEquals(ALERT_TARGET_DESC, entity.getDescription());
+    assertEquals(ALERT_TARGET_TYPE, entity.getNotificationType());
+    assertEquals(ALERT_TARGET_PROPS, entity.getProperties());
+    assertEquals(false, entity.isGlobal());
+    assertEquals(3, entity.getAlertGroups().size());
+
+    // no alert states were set explicitely in the request, so all should be set
+    // by the backend
+    assertNotNull(entity.getAlertStates());
+    assertEquals(EnumSet.allOf(AlertState.class), entity.getAlertStates());
+
+    verify(m_amc, m_dao);
+  }
+
+  /**
+   * @throws Exception
+   */
+  @Test
   public void testCreateGlobalTarget() throws Exception {
     Capture<List<AlertTargetEntity>> listCapture = new Capture<List<AlertTargetEntity>>();
 
@@ -293,6 +345,7 @@ public class AlertTargetResourceProviderTest {
 
     Request request = PropertyHelper.getCreateRequest(
         Collections.singleton(requestProps), null);
+
     provider.createResources(request);
 
     Assert.assertTrue(listCapture.hasCaptured());
@@ -412,6 +465,64 @@ public class AlertTargetResourceProviderTest {
    * @throws Exception
    */
   @Test
+  @SuppressWarnings("unchecked")
+  public void testUpdateResourcesWithGroups() throws Exception {
+    Capture<AlertTargetEntity> entityCapture = new Capture<AlertTargetEntity>();
+
+    m_dao.createTargets(EasyMock.anyObject(List.class));
+    expectLastCall().times(1);
+
+    AlertTargetEntity target = new AlertTargetEntity();
+    expect(m_dao.findTargetById(ALERT_TARGET_ID)).andReturn(target).times(1);
+
+    List<Long> groupIds = Arrays.asList(1L, 2L, 3L);
+    List<AlertGroupEntity> groups = new ArrayList<AlertGroupEntity>();
+    AlertGroupEntity group1 = new AlertGroupEntity();
+    AlertGroupEntity group2 = new AlertGroupEntity();
+    AlertGroupEntity group3 = new AlertGroupEntity();
+    group1.setGroupId(1L);
+    group2.setGroupId(2L);
+    group3.setGroupId(3L);
+    groups.addAll(Arrays.asList(group1, group2, group3));
+    expect(m_dao.findGroupsById(EasyMock.eq(groupIds))).andReturn(groups).once();
+
+    expect(m_dao.merge(capture(entityCapture))).andReturn(target).once();
+
+    replay(m_amc, m_dao);
+
+    AlertTargetResourceProvider provider = createProvider(m_amc);
+    Map<String, Object> requestProps = getCreationProperties();
+    Request request = PropertyHelper.getCreateRequest(
+        Collections.singleton(requestProps), null);
+    provider.createResources(request);
+
+    // create new properties, and include the ID since we're not going through
+    // a service layer which would add it for us automatically
+    requestProps = new HashMap<String, Object>();
+    requestProps.put(AlertTargetResourceProvider.ALERT_TARGET_ID,
+        ALERT_TARGET_ID.toString());
+
+    // add the group IDs to the request so that we're associating groups
+    requestProps.put(AlertTargetResourceProvider.ALERT_TARGET_GROUPS, groupIds);
+
+    Predicate predicate = new PredicateBuilder().property(
+        AlertTargetResourceProvider.ALERT_TARGET_ID).equals(
+        ALERT_TARGET_ID.toString()).toPredicate();
+
+    request = PropertyHelper.getUpdateRequest(requestProps, null);
+    provider.updateResources(request, predicate);
+
+    assertTrue(entityCapture.hasCaptured());
+
+    AlertTargetEntity entity = entityCapture.getValue();
+    assertEquals(3, entity.getAlertGroups().size());
+    verify(m_amc, m_dao);
+  }
+
+  /**
+   * @throws Exception
+   */
+  @Test
   public void testDeleteResources() throws Exception {
     Capture<AlertTargetEntity> entityCapture = new Capture<AlertTargetEntity>();
     Capture<List<AlertTargetEntity>> listCapture = new Capture<List<AlertTargetEntity>>();

http://git-wip-us.apache.org/repos/asf/ambari/blob/3022e274/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 76a9ee2..44c40f2 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
@@ -228,6 +228,24 @@ public class AlertDispatchDAOTest {
   }
 
   /**
+   * @throws Exception
+   */
+  @Test
+  public void testFindGroupsByIds() throws Exception {
+    List<AlertGroupEntity> groups = m_dao.findAllGroups();
+    assertNotNull(groups);
+    assertEquals(10, groups.size());
+
+    List<Long> ids = new ArrayList<Long>();
+    ids.add(groups.get(0).getGroupId());
+    ids.add(groups.get(1).getGroupId());
+    ids.add(99999L);
+
+    groups = m_dao.findGroupsById(ids);
+    assertEquals(2, groups.size());
+  }
+
+  /**
    *
    */
   @Test