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 2015/03/02 23:24:55 UTC

ambari git commit: AMBARI-9874 - Alert Targets Should Have a Directive to Overwrite Existing On Creation (jonathanhurley)

Repository: ambari
Updated Branches:
  refs/heads/trunk 94c55e865 -> 9a00dec0a


AMBARI-9874 - Alert Targets Should Have a Directive to Overwrite Existing On Creation (jonathanhurley)


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

Branch: refs/heads/trunk
Commit: 9a00dec0afae02369ad6a10561d345570e7de411
Parents: 94c55e8
Author: Jonathan Hurley <jh...@hortonworks.com>
Authored: Mon Mar 2 13:20:30 2015 -0500
Committer: Jonathan Hurley <jh...@hortonworks.com>
Committed: Mon Mar 2 17:24:34 2015 -0500

----------------------------------------------------------------------
 .../resources/AlertDefResourceDefinition.java   |  16 +-
 .../AlertTargetResourceDefinition.java          |   5 +-
 .../AlertDefinitionResourceProvider.java        |  20 ++-
 .../internal/AlertTargetResourceProvider.java   | 149 +++++++++++--------
 .../AlertTargetResourceProviderTest.java        | 104 ++++++++-----
 5 files changed, 178 insertions(+), 116 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/9a00dec0/ambari-server/src/main/java/org/apache/ambari/server/api/resources/AlertDefResourceDefinition.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/resources/AlertDefResourceDefinition.java b/ambari-server/src/main/java/org/apache/ambari/server/api/resources/AlertDefResourceDefinition.java
index 66e5a83..7247067 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/resources/AlertDefResourceDefinition.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/resources/AlertDefResourceDefinition.java
@@ -17,6 +17,8 @@
  */
 package org.apache.ambari.server.api.resources;
 
+import java.util.Collection;
+
 import org.apache.ambari.server.controller.spi.Resource;
 
 /**
@@ -24,18 +26,26 @@ import org.apache.ambari.server.controller.spi.Resource;
  */
 public class AlertDefResourceDefinition extends BaseResourceDefinition {
 
+  public static final String EXECUTE_IMMEDIATE_DIRECTIVE = "run_now";
+
   public AlertDefResourceDefinition() {
     super(Resource.Type.AlertDefinition);
   }
-  
+
   @Override
   public String getPluralName() {
     return "alert_definitions";
   }
-  
+
   @Override
   public String getSingularName() {
     return "alert_definition";
   }
-  
+
+  @Override
+  public Collection<String> getUpdateDirectives() {
+    Collection<String> directives = super.getCreateDirectives();
+    directives.add(EXECUTE_IMMEDIATE_DIRECTIVE);
+    return directives;
+  }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/9a00dec0/ambari-server/src/main/java/org/apache/ambari/server/api/resources/AlertTargetResourceDefinition.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/resources/AlertTargetResourceDefinition.java b/ambari-server/src/main/java/org/apache/ambari/server/api/resources/AlertTargetResourceDefinition.java
index 5e5079e..e7f7f21 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/resources/AlertTargetResourceDefinition.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/resources/AlertTargetResourceDefinition.java
@@ -17,9 +17,10 @@
  */
 package org.apache.ambari.server.api.resources;
 
-import org.apache.ambari.server.controller.spi.Resource;
 import java.util.Collection;
 
+import org.apache.ambari.server.controller.spi.Resource;
+
 /**
  * The {@link AlertTargetResourceDefinition} class is used to register alert
  * targets to be returned via the REST API.
@@ -27,6 +28,7 @@ import java.util.Collection;
 public class AlertTargetResourceDefinition extends BaseResourceDefinition {
 
   public static final String VALIDATE_CONFIG_DIRECTIVE = "validate_config";
+  public static final String OVERWRITE_DIRECTIVE = "overwrite_existing";
 
   /**
    * Constructor.
@@ -55,6 +57,7 @@ public class AlertTargetResourceDefinition extends BaseResourceDefinition {
   public Collection<String> getCreateDirectives() {
     Collection<String> directives = super.getCreateDirectives();
     directives.add(VALIDATE_CONFIG_DIRECTIVE);
+    directives.add(OVERWRITE_DIRECTIVE);
     return directives;
   }
 }

http://git-wip-us.apache.org/repos/asf/ambari/blob/9a00dec0/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AlertDefinitionResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AlertDefinitionResourceProvider.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AlertDefinitionResourceProvider.java
index 886348b..24fb931 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AlertDefinitionResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AlertDefinitionResourceProvider.java
@@ -33,6 +33,7 @@ import org.apache.ambari.server.AmbariException;
 import org.apache.ambari.server.StaticallyInject;
 import org.apache.ambari.server.agent.ActionQueue;
 import org.apache.ambari.server.agent.AlertExecutionCommand;
+import org.apache.ambari.server.api.resources.AlertDefResourceDefinition;
 import org.apache.ambari.server.controller.AmbariManagementController;
 import org.apache.ambari.server.controller.spi.NoSuchParentResourceException;
 import org.apache.ambari.server.controller.spi.NoSuchResourceException;
@@ -86,8 +87,6 @@ public class AlertDefinitionResourceProvider extends AbstractControllerResourceP
   protected static final String ALERT_DEF_SOURCE = "AlertDefinition/source";
   protected static final String ALERT_DEF_SOURCE_TYPE = "AlertDefinition/source/type";
 
-  protected static final String ALERT_DEF_ACTION_RUN_NOW = "AlertDefinition/run_now";
-
   private static Set<String> pkPropertyIds = new HashSet<String>(
       Arrays.asList(ALERT_DEF_ID, ALERT_DEF_NAME));
 
@@ -147,7 +146,6 @@ public class AlertDefinitionResourceProvider extends AbstractControllerResourceP
     PROPERTY_IDS.add(ALERT_DEF_SCOPE);
     PROPERTY_IDS.add(ALERT_DEF_IGNORE_HOST);
     PROPERTY_IDS.add(ALERT_DEF_SOURCE);
-    PROPERTY_IDS.add(ALERT_DEF_ACTION_RUN_NOW);
 
     // keys
     KEY_PROPERTY_IDS.put(Resource.Type.AlertDefinition, ALERT_DEF_ID);
@@ -266,18 +264,18 @@ public class AlertDefinitionResourceProvider extends AbstractControllerResourceP
       throws SystemException, UnsupportedPropertyException,
       NoSuchResourceException, NoSuchParentResourceException {
 
-    // check the predicate to see if there is a reques to run
+    // check the update directives to see if there is a request to run
     // the alert definition immediately
-    if( null != predicate ){
+    Map<String, String> requestInfoProps = request.getRequestInfoProperties();
+    if (null != requestInfoProps
+        && requestInfoProps.containsKey(AlertDefResourceDefinition.EXECUTE_IMMEDIATE_DIRECTIVE)) {
+
       Set<Map<String,Object>> predicateMaps = getPropertyMaps(predicate);
       for (Map<String, Object> propertyMap : predicateMaps) {
-        String runNow = (String) propertyMap.get(ALERT_DEF_ACTION_RUN_NOW);
-        if (null != runNow) {
-          if (Boolean.valueOf(runNow) == Boolean.TRUE) {
-            scheduleImmediateAlert(propertyMap);
-          }
-        }
+        scheduleImmediateAlert(propertyMap);
       }
+
+      return getRequestStatus(null);
     }
 
     // if an AlertDefinition property body was specified, perform the update

http://git-wip-us.apache.org/repos/asf/ambari/blob/9a00dec0/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 19beb01..f341db0 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
@@ -180,7 +180,18 @@ public class AlertTargetResourceProvider extends
     modifyResources(new Command<Void>() {
       @Override
       public Void invoke() throws AmbariException {
-        updateAlertTargets(request.getProperties());
+        Set<Map<String, Object>> requestMaps = request.getProperties();
+        for (Map<String, Object> requestMap : requestMaps) {
+          String stringId = (String) requestMap.get(ALERT_TARGET_ID);
+
+          if (StringUtils.isEmpty(stringId)) {
+            throw new IllegalArgumentException(
+                "The ID of the alert target is required when updating an existing target");
+          }
+
+          long alertTargetId = Long.parseLong(stringId);
+          updateAlertTargets(alertTargetId, requestMap);
+        }
         return null;
       }
     });
@@ -237,10 +248,7 @@ public class AlertTargetResourceProvider extends
   @SuppressWarnings("unchecked")
   private void createAlertTargets(Set<Map<String, Object>> requestMaps, Map<String, String> requestInfoProps)
       throws AmbariException {
-    List<AlertTargetEntity> entities = new ArrayList<AlertTargetEntity>();
     for (Map<String, Object> requestMap : requestMaps) {
-      AlertTargetEntity entity = new AlertTargetEntity();
-
       String name = (String) requestMap.get(ALERT_TARGET_NAME);
       String description = (String) requestMap.get(ALERT_TARGET_DESCRIPTION);
       String notificationType = (String) requestMap.get(ALERT_TARGET_NOTIFICATION_TYPE);
@@ -265,11 +273,17 @@ public class AlertTargetResourceProvider extends
             "Alert targets must be created with their connection properties");
       }
 
-      String validationProperty =  requestInfoProps.get(AlertTargetResourceDefinition.VALIDATE_CONFIG_DIRECTIVE);
-      if (validationProperty != null && validationProperty.equalsIgnoreCase("true")) {
+      String validationDirective = requestInfoProps.get(AlertTargetResourceDefinition.VALIDATE_CONFIG_DIRECTIVE);
+      if (validationDirective != null
+          && validationDirective.equalsIgnoreCase("true")) {
         validateTargetConfig(notificationType, properties);
       }
 
+      boolean overwriteExisting = false;
+      if (requestInfoProps.containsKey(AlertTargetResourceDefinition.OVERWRITE_DIRECTIVE)) {
+        overwriteExisting = true;
+      }
+
       // global not required
       boolean isGlobal = false;
       if (null != globalProperty) {
@@ -287,6 +301,16 @@ public class AlertTargetResourceProvider extends
         alertStateSet = EnumSet.allOf(AlertState.class);
       }
 
+      // if we are overwriting an existing, determine if one exists first
+      AlertTargetEntity entity = null;
+      if( overwriteExisting ) {
+        entity = s_dao.findTargetByName(name);
+      }
+
+      if (null == entity) {
+        entity = new AlertTargetEntity();
+      }
+
       // groups are not required on creation
       if (requestMap.containsKey(ALERT_TARGET_GROUPS)) {
         Collection<Long> groupIds = (Collection<Long>) requestMap.get(ALERT_TARGET_GROUPS);
@@ -305,10 +329,12 @@ public class AlertTargetResourceProvider extends
       entity.setAlertStates(alertStateSet);
       entity.setGlobal(isGlobal);
 
-      entities.add(entity);
+      if (null == entity.getTargetId() || 0 == entity.getTargetId()) {
+        s_dao.create(entity);
+      } else {
+        s_dao.merge(entity);
+      }
     }
-
-    s_dao.createTargets(entities);
   }
 
   /**
@@ -320,81 +346,72 @@ public class AlertTargetResourceProvider extends
    *           if the entity could not be found.
    */
   @SuppressWarnings("unchecked")
-  private void updateAlertTargets(Set<Map<String, Object>> requestMaps)
+  private void updateAlertTargets(long alertTargetId,
+      Map<String, Object> requestMap)
       throws AmbariException {
 
-    for (Map<String, Object> requestMap : requestMaps) {
-      String stringId = (String) requestMap.get(ALERT_TARGET_ID);
+    AlertTargetEntity entity = s_dao.findTargetById(alertTargetId);
 
-      if (StringUtils.isEmpty(stringId)) {
-        throw new IllegalArgumentException(
-            "The ID of the alert target is required when updating an existing target");
-      }
-
-      long id = Long.parseLong(stringId);
-      AlertTargetEntity entity = s_dao.findTargetById(id);
+    if (null == entity) {
+      String message = MessageFormat.format(
+          "The alert target with ID {0} could not be found", alertTargetId);
 
-      if (null == entity) {
-        String message = MessageFormat.format(
-            "The alert target with ID {0} could not be found", id);
-        throw new AmbariException(message);
-      }
+      throw new AmbariException(message);
+    }
 
-      String name = (String) requestMap.get(ALERT_TARGET_NAME);
-      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);
+    String name = (String) requestMap.get(ALERT_TARGET_NAME);
+    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);
-      }
+    if (!StringUtils.isBlank(name)) {
+      entity.setTargetName(name);
+    }
 
-      if (null != description) {
-        entity.setDescription(description);
-      }
+    if (null != description) {
+      entity.setDescription(description);
+    }
 
-      if (!StringUtils.isBlank(notificationType)) {
-        entity.setNotificationType(notificationType);
-      }
+    if (!StringUtils.isBlank(notificationType)) {
+      entity.setNotificationType(notificationType);
+    }
 
-      String properties = s_gson.toJson(extractProperties(requestMap));
-      if (!StringUtils.isEmpty(properties)) {
-        entity.setProperties(properties);
-      }
+    String properties = s_gson.toJson(extractProperties(requestMap));
+    if (!StringUtils.isEmpty(properties)) {
+      entity.setProperties(properties);
+    }
 
-      // a null alert state implies that the key was not set and no update
-      // should occur for this field, while an empty list implies all alert
-      // states should be set
-      if (null != alertStates) {
-        final Set<AlertState> alertStateSet;
-        if (alertStates.isEmpty()) {
-          alertStateSet = EnumSet.allOf(AlertState.class);
-        } else {
-          alertStateSet = new HashSet<AlertState>(alertStates.size());
-          for (String state : alertStates) {
-            alertStateSet.add(AlertState.valueOf(state));
-          }
+    // a null alert state implies that the key was not set and no update
+    // should occur for this field, while an empty list implies all alert
+    // states should be set
+    if (null != alertStates) {
+      final Set<AlertState> alertStateSet;
+      if (alertStates.isEmpty()) {
+        alertStateSet = EnumSet.allOf(AlertState.class);
+      } else {
+        alertStateSet = new HashSet<AlertState>(alertStates.size());
+        for (String state : alertStates) {
+          alertStateSet.add(AlertState.valueOf(state));
         }
-
-        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);
+      entity.setAlertStates(alertStateSet);
+    }
 
-        if (ids.size() > 0) {
-          groups.addAll(s_dao.findGroupsById(ids));
-        }
+    // if groups were supplied, replace existing
+    if (null != groupIds) {
+      Set<AlertGroupEntity> groups = new HashSet<AlertGroupEntity>();
+      List<Long> ids = new ArrayList<Long>(groupIds);
 
-        entity.setAlertGroups(groups);
+      if (ids.size() > 0) {
+        groups.addAll(s_dao.findGroupsById(ids));
       }
 
-      s_dao.merge(entity);
+      entity.setAlertGroups(groups);
     }
+
+    s_dao.merge(entity);
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/ambari/blob/9a00dec0/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 2d08a7a..b610d8e 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
@@ -41,6 +41,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.ambari.server.api.resources.AlertTargetResourceDefinition;
 import org.apache.ambari.server.controller.AmbariManagementController;
 import org.apache.ambari.server.controller.spi.Predicate;
 import org.apache.ambari.server.controller.spi.Request;
@@ -205,9 +206,8 @@ public class AlertTargetResourceProviderTest {
    */
   @Test
   public void testCreateResources() throws Exception {
-    Capture<List<AlertTargetEntity>> listCapture = new Capture<List<AlertTargetEntity>>();
-
-    m_dao.createTargets(capture(listCapture));
+    Capture<AlertTargetEntity> targetCapture = new Capture<AlertTargetEntity>();
+    m_dao.create(capture(targetCapture));
     expectLastCall();
 
     replay(m_amc, m_dao);
@@ -218,8 +218,8 @@ public class AlertTargetResourceProviderTest {
     Request request = PropertyHelper.getCreateRequest(Collections.singleton(requestProps), null);
     provider.createResources(request);
 
-    Assert.assertTrue(listCapture.hasCaptured());
-    AlertTargetEntity entity = listCapture.getValue().get(0);
+    Assert.assertTrue(targetCapture.hasCaptured());
+    AlertTargetEntity entity = targetCapture.getValue();
     Assert.assertNotNull(entity);
 
     assertEquals(ALERT_TARGET_NAME, entity.getTargetName());
@@ -241,8 +241,6 @@ public class AlertTargetResourceProviderTest {
    */
   @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();
@@ -254,7 +252,8 @@ public class AlertTargetResourceProviderTest {
     groups.addAll(Arrays.asList(group1, group2, group3));
     expect(m_dao.findGroupsById(groupIds)).andReturn(groups).once();
 
-    m_dao.createTargets(capture(listCapture));
+    Capture<AlertTargetEntity> targetCapture = new Capture<AlertTargetEntity>();
+    m_dao.create(capture(targetCapture));
     expectLastCall();
 
     replay(m_amc, m_dao);
@@ -268,8 +267,8 @@ public class AlertTargetResourceProviderTest {
     Request request = PropertyHelper.getCreateRequest(Collections.singleton(requestProps), null);
     provider.createResources(request);
 
-    Assert.assertTrue(listCapture.hasCaptured());
-    AlertTargetEntity entity = listCapture.getValue().get(0);
+    Assert.assertTrue(targetCapture.hasCaptured());
+    AlertTargetEntity entity = targetCapture.getValue();
     Assert.assertNotNull(entity);
 
     assertEquals(ALERT_TARGET_NAME, entity.getTargetName());
@@ -292,9 +291,8 @@ public class AlertTargetResourceProviderTest {
    */
   @Test
   public void testCreateGlobalTarget() throws Exception {
-    Capture<List<AlertTargetEntity>> listCapture = new Capture<List<AlertTargetEntity>>();
-
-    m_dao.createTargets(capture(listCapture));
+    Capture<AlertTargetEntity> targetCapture = new Capture<AlertTargetEntity>();
+    m_dao.create(capture(targetCapture));
     expectLastCall();
 
     replay(m_amc, m_dao);
@@ -310,8 +308,8 @@ public class AlertTargetResourceProviderTest {
 
     provider.createResources(request);
 
-    Assert.assertTrue(listCapture.hasCaptured());
-    AlertTargetEntity entity = listCapture.getValue().get(0);
+    Assert.assertTrue(targetCapture.hasCaptured());
+    AlertTargetEntity entity = targetCapture.getValue();
     Assert.assertNotNull(entity);
 
     assertEquals(ALERT_TARGET_NAME, entity.getTargetName());
@@ -333,9 +331,8 @@ public class AlertTargetResourceProviderTest {
    */
   @Test
   public void testCreateResourceWithRecipientArray() throws Exception {
-    Capture<List<AlertTargetEntity>> listCapture = new Capture<List<AlertTargetEntity>>();
-
-    m_dao.createTargets(capture(listCapture));
+    Capture<AlertTargetEntity> targetCapture = new Capture<AlertTargetEntity>();
+    m_dao.create(capture(targetCapture));
     expectLastCall();
 
     replay(m_amc, m_dao);
@@ -348,8 +345,8 @@ public class AlertTargetResourceProviderTest {
 
     provider.createResources(request);
 
-    Assert.assertTrue(listCapture.hasCaptured());
-    AlertTargetEntity entity = listCapture.getValue().get(0);
+    Assert.assertTrue(targetCapture.hasCaptured());
+    AlertTargetEntity entity = targetCapture.getValue();
     Assert.assertNotNull(entity);
 
     assertEquals(ALERT_TARGET_NAME, entity.getTargetName());
@@ -373,9 +370,8 @@ public class AlertTargetResourceProviderTest {
   @Test
   @SuppressWarnings("unchecked")
   public void testCreateResourceWithAlertStates() throws Exception {
-    Capture<List<AlertTargetEntity>> listCapture = new Capture<List<AlertTargetEntity>>();
-
-    m_dao.createTargets(capture(listCapture));
+    Capture<AlertTargetEntity> targetCapture = new Capture<AlertTargetEntity>();
+    m_dao.create(capture(targetCapture));
     expectLastCall();
 
     replay(m_amc, m_dao);
@@ -391,8 +387,8 @@ public class AlertTargetResourceProviderTest {
 
     provider.createResources(request);
 
-    Assert.assertTrue(listCapture.hasCaptured());
-    AlertTargetEntity entity = listCapture.getValue().get(0);
+    Assert.assertTrue(targetCapture.hasCaptured());
+    AlertTargetEntity entity = targetCapture.getValue();
     Assert.assertNotNull(entity);
 
     assertEquals(ALERT_TARGET_NAME, entity.getTargetName());
@@ -417,8 +413,7 @@ public class AlertTargetResourceProviderTest {
   @SuppressWarnings("unchecked")
   public void testUpdateResources() throws Exception {
     Capture<AlertTargetEntity> entityCapture = new Capture<AlertTargetEntity>();
-
-    m_dao.createTargets(EasyMock.anyObject(List.class));
+    m_dao.create(capture(entityCapture));
     expectLastCall().times(1);
 
     AlertTargetEntity target = new AlertTargetEntity();
@@ -468,8 +463,7 @@ public class AlertTargetResourceProviderTest {
   @SuppressWarnings("unchecked")
   public void testUpdateResourcesWithGroups() throws Exception {
     Capture<AlertTargetEntity> entityCapture = new Capture<AlertTargetEntity>();
-
-    m_dao.createTargets(EasyMock.anyObject(List.class));
+    m_dao.create(capture(entityCapture));
     expectLastCall().times(1);
 
     AlertTargetEntity target = new AlertTargetEntity();
@@ -525,10 +519,8 @@ public class AlertTargetResourceProviderTest {
   @Test
   public void testDeleteResources() throws Exception {
     Capture<AlertTargetEntity> entityCapture = new Capture<AlertTargetEntity>();
-    Capture<List<AlertTargetEntity>> listCapture = new Capture<List<AlertTargetEntity>>();
-
-    m_dao.createTargets(capture(listCapture));
-    expectLastCall();
+    m_dao.create(capture(entityCapture));
+    expectLastCall().times(1);
 
     replay(m_amc, m_dao);
 
@@ -538,8 +530,8 @@ public class AlertTargetResourceProviderTest {
     Request request = PropertyHelper.getCreateRequest(Collections.singleton(requestProps), null);
     provider.createResources(request);
 
-    Assert.assertTrue(listCapture.hasCaptured());
-    AlertTargetEntity entity = listCapture.getValue().get(0);
+    Assert.assertTrue(entityCapture.hasCaptured());
+    AlertTargetEntity entity = entityCapture.getValue();
     Assert.assertNotNull(entity);
 
     Predicate p = new PredicateBuilder().property(
@@ -563,6 +555,48 @@ public class AlertTargetResourceProviderTest {
     verify(m_amc, m_dao);
   }
 
+  @Test
+  public void testOverwriteDirective() throws Exception {
+    // mock out returning an existing entity
+    AlertTargetEntity entity = getMockEntities().get(0);
+    expect(m_dao.findTargetByName(ALERT_TARGET_NAME)).andReturn(entity).atLeastOnce();
+    Capture<AlertTargetEntity> targetCapture = new Capture<AlertTargetEntity>();
+    expect(m_dao.merge(capture(targetCapture))).andReturn(entity).once();
+
+    replay(m_amc, m_dao);
+
+    AlertTargetResourceProvider provider = createProvider(m_amc);
+    Map<String, Object> requestProps = getCreationProperties();
+
+    // mock out the directive
+    Map<String, String> requestInfoProperties = new HashMap<String, String>();
+    requestInfoProperties.put(
+        AlertTargetResourceDefinition.OVERWRITE_DIRECTIVE, "true");
+
+    Request request = PropertyHelper.getCreateRequest(
+        Collections.singleton(requestProps), requestInfoProperties);
+
+    provider.createResources(request);
+
+    Assert.assertTrue(targetCapture.hasCaptured());
+    entity = targetCapture.getValue();
+    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());
+
+    // 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);
+  }
+
+
   /**
    * @param amc
    * @return