Bladeren bron

AMBARI-14766 - Selecting ALL groups does not work under Manage Alert Notifications (Keta Patel via jonathanhurley)

Jonathan Hurley 9 jaren geleden
bovenliggende
commit
f57f9b2910

+ 11 - 1
ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AlertTargetResourceProvider.java

@@ -380,7 +380,10 @@ public class AlertTargetResourceProvider extends
     String notificationType = (String) requestMap.get(ALERT_TARGET_NOTIFICATION_TYPE);
     String notificationType = (String) requestMap.get(ALERT_TARGET_NOTIFICATION_TYPE);
     Collection<String> alertStates = (Collection<String>) requestMap.get(ALERT_TARGET_STATES);
     Collection<String> alertStates = (Collection<String>) requestMap.get(ALERT_TARGET_STATES);
     Collection<Long> groupIds = (Collection<Long>) requestMap.get(ALERT_TARGET_GROUPS);
     Collection<Long> groupIds = (Collection<Long>) requestMap.get(ALERT_TARGET_GROUPS);
-
+    String isGlobal = (String) requestMap.get(ALERT_TARGET_GLOBAL);
+    if(null != isGlobal){
+      entity.setGlobal(Boolean.parseBoolean(isGlobal));
+    }
     if (!StringUtils.isBlank(name)) {
     if (!StringUtils.isBlank(name)) {
       entity.setTargetName(name);
       entity.setTargetName(name);
     }
     }
@@ -424,6 +427,13 @@ public class AlertTargetResourceProvider extends
         groups.addAll(s_dao.findGroupsById(ids));
         groups.addAll(s_dao.findGroupsById(ids));
       }
       }
 
 
+      entity.setAlertGroups(groups);
+    } else if (entity.isGlobal()){
+      Set<AlertGroupEntity> groups = new HashSet<AlertGroupEntity>(s_dao.findAllGroups());
+      for (AlertGroupEntity group : groups) {
+        group.addAlertTarget(entity);
+        s_dao.merge(group);
+      }
       entity.setAlertGroups(groups);
       entity.setAlertGroups(groups);
     }
     }
 
 

+ 144 - 6
ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AlertTargetResourceProviderTest.java

@@ -894,6 +894,126 @@ public class AlertTargetResourceProviderTest {
     verify(m_amc, m_dao);
     verify(m_amc, m_dao);
   }
   }
 
 
+  @Test
+  public void testUpdateAlertTargetsWithCustomGroups() throws Exception{
+    Capture<AlertTargetEntity> entityCapture = new Capture<AlertTargetEntity>();
+    m_dao.create(capture(entityCapture));
+    expectLastCall().times(1);
+
+    AlertTargetEntity target = new AlertTargetEntity();
+    expect(m_dao.findTargetById(ALERT_TARGET_ID)).andReturn(target).once();
+
+    Capture<AlertGroupEntity> groupEntityCapture = new Capture<AlertGroupEntity>();
+
+    //All Groups in the Database with CLuster ID = 1L
+    List<AlertGroupEntity> groups = getMockGroupEntities();
+
+    //List of group ids to be selected in Custom option
+    List<Long> groupIds = Arrays.asList(1L, 2L, 3L);
+
+    expect(m_dao.findGroupsById(EasyMock.eq(groupIds))).andReturn(groups).anyTimes();
+    expect(m_dao.findAllGroups()).andReturn(groups).anyTimes();
+    for(AlertGroupEntity group: groups){
+      expect(m_dao.merge(capture(groupEntityCapture))).andReturn(group).anyTimes();
+    }
+    expect(m_dao.merge(capture(entityCapture))).andReturn(target).anyTimes();
+
+    //start execution with the above Expectation setters
+    replay(m_amc, m_dao);
+
+    SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createAdministrator());
+
+    AlertTargetResourceProvider provider = createProvider(m_amc);
+    Map<String, Object> requestProps = getCreationProperties();
+    Request request = PropertyHelper.getCreateRequest(
+      Collections.singleton(requestProps), null);
+    provider.createResources(request);
+
+    requestProps = new HashMap<String, Object>();
+    requestProps.put(AlertTargetResourceProvider.ALERT_TARGET_ID, ALERT_TARGET_ID.toString());
+
+    //selecting CUSTOM option for Groups, 2 group ids selected from the available options
+    requestProps.put(AlertTargetResourceProvider.ALERT_TARGET_GLOBAL, "false");
+    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();
+
+    //verify that only the selected 2 groups were mapped with the target
+    assertEquals(3, entity.getAlertGroups().size());
+
+    //target must not be global for CUSTOM option
+    assertEquals(false,entity.isGlobal());
+
+    verify(m_amc, m_dao);
+  }
+
+  @Test
+  public void testUpdateAlertTargetsWithAllGroups() throws Exception{
+    Capture<AlertTargetEntity> entityCapture = new Capture<AlertTargetEntity>();
+    m_dao.create(capture(entityCapture));
+    expectLastCall().times(1);
+
+    AlertTargetEntity target = new AlertTargetEntity();
+    expect(m_dao.findTargetById(ALERT_TARGET_ID)).andReturn(target).once();
+
+    Capture<AlertGroupEntity> groupEntityCapture = new Capture<AlertGroupEntity>();
+
+    //All Groups in the Database with CLuster ID = 1L
+    List<AlertGroupEntity> groups = getMockGroupEntities();
+
+    //Groups to be selected for ALL option
+    List<Long> groupIds = Arrays.asList(1L, 2L, 3L);
+
+    expect(m_dao.findGroupsById(EasyMock.eq(groupIds))).andReturn(groups).anyTimes();
+    expect(m_dao.findAllGroups()).andReturn(groups).once();
+    for(AlertGroupEntity group: groups){
+      expect(m_dao.merge(capture(groupEntityCapture))).andReturn(group).once();
+    }
+    expect(m_dao.merge(capture(entityCapture))).andReturn(target).anyTimes();
+
+    //start execution with these Expectation setters
+    replay(m_amc, m_dao);
+
+    SecurityContextHolder.getContext().setAuthentication(TestAuthenticationFactory.createAdministrator());
+
+    AlertTargetResourceProvider provider = createProvider(m_amc);
+    Map<String, Object> requestProps = getCreationProperties();
+    Request request = PropertyHelper.getCreateRequest(
+      Collections.singleton(requestProps), null);
+    provider.createResources(request);
+
+    requestProps = new HashMap<String, Object>();
+    requestProps.put(AlertTargetResourceProvider.ALERT_TARGET_ID, ALERT_TARGET_ID.toString());
+
+    //selecting ALL option for Groups
+    requestProps.put(AlertTargetResourceProvider.ALERT_TARGET_GLOBAL, "true");
+
+    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();
+
+    //verify that all the groups got mapped with target
+    assertEquals(3, entity.getAlertGroups().size());
+
+    //Target must be global for ALL option
+    assertEquals(true,entity.isGlobal());
+
+    verify(m_amc, m_dao);
+  }
 
 
   /**
   /**
    * @param amc
    * @param amc
@@ -932,20 +1052,20 @@ public class AlertTargetResourceProviderTest {
   private Map<String, Object> getCreationProperties() throws Exception {
   private Map<String, Object> getCreationProperties() throws Exception {
     Map<String, Object> requestProps = new HashMap<String, Object>();
     Map<String, Object> requestProps = new HashMap<String, Object>();
     requestProps.put(AlertTargetResourceProvider.ALERT_TARGET_NAME,
     requestProps.put(AlertTargetResourceProvider.ALERT_TARGET_NAME,
-        ALERT_TARGET_NAME);
+            ALERT_TARGET_NAME);
 
 
     requestProps.put(AlertTargetResourceProvider.ALERT_TARGET_DESCRIPTION,
     requestProps.put(AlertTargetResourceProvider.ALERT_TARGET_DESCRIPTION,
-        ALERT_TARGET_DESC);
+            ALERT_TARGET_DESC);
 
 
     requestProps.put(
     requestProps.put(
-        AlertTargetResourceProvider.ALERT_TARGET_NOTIFICATION_TYPE,
-        ALERT_TARGET_TYPE);
+            AlertTargetResourceProvider.ALERT_TARGET_NOTIFICATION_TYPE,
+            ALERT_TARGET_TYPE);
 
 
     requestProps.put(AlertTargetResourceProvider.ALERT_TARGET_PROPERTIES
     requestProps.put(AlertTargetResourceProvider.ALERT_TARGET_PROPERTIES
-        + "/foo", "bar");
+            + "/foo", "bar");
 
 
     requestProps.put(AlertTargetResourceProvider.ALERT_TARGET_PROPERTIES
     requestProps.put(AlertTargetResourceProvider.ALERT_TARGET_PROPERTIES
-        + "/foobar", "baz");
+            + "/foobar", "baz");
 
 
     return requestProps;
     return requestProps;
   }
   }
@@ -975,6 +1095,24 @@ public class AlertTargetResourceProviderTest {
     return requestProps;
     return requestProps;
   }
   }
 
 
+  /**
+   * @return
+   */
+  @SuppressWarnings({ "rawtypes", "unchecked" })
+  private List<AlertGroupEntity> getMockGroupEntities() throws Exception {
+    AlertGroupEntity group1 = new AlertGroupEntity();
+    AlertGroupEntity group2 = new AlertGroupEntity();
+    AlertGroupEntity group3 = new AlertGroupEntity();
+    group1.setGroupId(1L);
+    group1.setClusterId(1L);
+    group2.setGroupId(2L);
+    group2.setClusterId(1L);
+    group3.setGroupId(3L);
+    group3.setClusterId(1L);
+
+    return Arrays.asList(group1, group2, group3);
+  }
+
   /**
   /**
   *
   *
   */
   */

+ 7 - 0
ambari-web/app/controllers/main/alerts/manage_alert_notifications_controller.js

@@ -702,6 +702,13 @@ App.ManageAlertNotificationsController = Em.Controller.extend({
    * @method updateAlertNotification
    * @method updateAlertNotification
    */
    */
   updateAlertNotification: function (apiObject) {
   updateAlertNotification: function (apiObject) {
+    if(apiObject!=null){
+      if(apiObject.AlertTarget.global == false){
+        this.get('selectedAlertNotification').set('global',false);
+      } else {
+        this.get('selectedAlertNotification').set('global',true);
+      }
+    }
     return App.ajax.send({
     return App.ajax.send({
       name: 'alerts.update_alert_notification',
       name: 'alerts.update_alert_notification',
       sender: this,
       sender: this,