Skip to content
This repository has been archived by the owner on Jul 11, 2022. It is now read-only.

Commit

Permalink
Bug 1127264 - Creating alert notification of type Resource Operations
Browse files Browse the repository at this point in the history
ignores extraConfig field
  • Loading branch information
Libor Zoubek committed Aug 6, 2014
1 parent 177ee40 commit 9916185
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.ejb.EJB;
Expand Down Expand Up @@ -69,8 +68,6 @@
import org.rhq.core.domain.alert.AlertPriority;
import org.rhq.core.domain.alert.BooleanExpression;
import org.rhq.core.domain.alert.notification.AlertNotification;
import org.rhq.core.domain.configuration.Configuration;
import org.rhq.core.domain.configuration.PropertySimple;
import org.rhq.core.domain.configuration.definition.ConfigurationDefinition;
import org.rhq.core.domain.configuration.definition.PropertyDefinition;
import org.rhq.core.domain.configuration.definition.PropertyDefinitionSimple;
Expand Down Expand Up @@ -297,9 +294,6 @@ public Response createAlertDefinition(@ApiParam("The id of the resource to attac
for (AlertNotificationRest anr : adr.getNotifications()) {

AlertNotification notification = notificationRestToNotification(alertDefinition, anr);
if (pluginManager.getAlertSenderForNotification(notification)==null) {
throw new StuffNotFoundException("AlertSender with name [" + anr.getSenderName() +"]");
}

notifications.add(notification);
}
Expand Down Expand Up @@ -456,14 +450,12 @@ private void setDampeningFromRest(AlertDefinition alertDefinition, AlertDefiniti
private AlertNotification notificationRestToNotification(AlertDefinition alertDefinition,
AlertNotificationRest anr) {
AlertNotification notification = new AlertNotification(anr.getSenderName());
// TODO validate sender
notification.setAlertDefinition(alertDefinition);
Configuration configuration = new Configuration();
for (Map.Entry<String, Object> entry : anr.getConfig().entrySet()) {
configuration.put(new PropertySimple(entry.getKey(), entry.getValue()));
if (notificationMgr.getAlertInfoForSender(anr.getSenderName()) == null) {
throw new StuffNotFoundException("AlertSender with name [" + anr.getSenderName() + "]");
}
notification.setConfiguration(configuration);
// TODO extra configuration (?)
notification.setAlertDefinition(alertDefinition);
notification.setConfiguration(ConfigurationHelper.mapToConfiguration(anr.getConfig()));
notification.setExtraConfiguration(ConfigurationHelper.mapToConfiguration(anr.getExtraConfig()));
return notification;
}

Expand Down Expand Up @@ -999,34 +991,20 @@ public Response addNotificationToDefinition(
@ApiParam("Id of the alert definition that should get the notification definition") @PathParam("id") int definitionId,
@ApiParam("The notification definition to add") AlertNotificationRest notificationRest, @Context UriInfo uriInfo) {

AlertNotification notification = new AlertNotification(notificationRest.getSenderName());

// first check if the sender by name exists
AlertSenderPluginManager pluginManager = alertManager.getAlertPluginManager();
if (pluginManager.getAlertSenderForNotification(notification)==null) {
throw new StuffNotFoundException("AlertSender with name [" + notificationRest.getSenderName() +"]");
}

// Now check if the definition exists as well
AlertDefinition definition = alertDefinitionManager.getAlertDefinition(caller,definitionId);
if (definition==null) {
throw new StuffNotFoundException("AlertDefinition with id " + definitionId);
}

AlertNotification notification = notificationRestToNotification(definition, notificationRest);

// definition and sender are valid, continue
int existingNotificationCount = definition.getAlertNotifications().size();

// notification.setAlertDefinition(definition); setting this will result in duplicated notifications
definition.addAlertNotification(notification);

Configuration configuration = new Configuration();
for (Map.Entry<String,Object> entry: notificationRest.getConfig().entrySet()) {
configuration.put(new PropertySimple(entry.getKey(),entry.getValue()));
}
notification.setConfiguration(configuration);
// TODO extra configuration (?)


alertDefinitionManager.updateAlertDefinitionInternal(caller, definitionId, definition, false, true, true);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,23 @@

package org.rhq.modules.integrationTests.restApi;

import static com.jayway.restassured.RestAssured.delete;
import static com.jayway.restassured.RestAssured.expect;
import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.emptyIterable;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.iterableWithSize;

import java.util.List;
import java.util.Map;

import com.jayway.restassured.config.RedirectConfig;
import com.jayway.restassured.config.RestAssuredConfig;
Expand All @@ -37,21 +53,6 @@
import org.rhq.modules.integrationTests.restApi.d.Availability;
import org.rhq.modules.integrationTests.restApi.d.Group;

import static com.jayway.restassured.RestAssured.delete;
import static com.jayway.restassured.RestAssured.expect;
import static com.jayway.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.emptyIterable;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.iterableWithSize;

/**
* Testing of the Alerting part of the rest-api
* @author Heiko W. Rupp
Expand Down Expand Up @@ -1051,6 +1052,79 @@ public void testCreateDeleteAlertDefinitionWith1Notification() throws Exception
}
}

@Test
public void testCreateDeleteAlertDefinitionWith1NotificationExtraConfig() throws Exception {

int definitionId = createEmptyAlertDefinition(false);

// find operation definition ID for discovery operation
int discoveryDefinitionId = -1;
Response r =
given()
.header(acceptJson)
.queryParam("resourceId",_platformId)
.expect()
.statusCode(200)
.log().ifError()
.when()
.get("/operation/definitions");

discoveryDefinitionId = -1;
List<Map<String,Object>> list = r.as(List.class);
for (Map<String,Object> map : list) {
String name = (String) map.get("name");
Integer id = (Integer) map.get("id");
if (name.equals("discovery")) {
discoveryDefinitionId = id;
}
}
assert discoveryDefinitionId !=-1 : "No discovery operation found";

// Now add a condition
try {

AlertNotification notification = new AlertNotification("Resource Operations"); // short-name from server plugin descriptor
notification.getConfig().put("selection-mode", "SELF");
notification.getConfig().put("operation-definition-id", discoveryDefinitionId);
notification.getExtraConfig().put("detailedDiscovery", false);

given()
.header(acceptJson)
.contentType(ContentType.JSON)
.body(notification)
.pathParam("defId", definitionId)
.log().everything()
.expect()
.statusCode(201)
.log().everything()
.when()
.post("/alert/definition/{defId}/notifications");

// Retrieve the definition with the added condition
AlertDefinition updatedDefinition =
given()
.pathParam("id",definitionId)
.queryParam("full",true)
.expect()
.statusCode(200)
.log().everything()
.when()
.get("/alert/definition/{id}")
.as(AlertDefinition.class);

int size = updatedDefinition.getNotifications().size();
assert size ==1 : "Did not find 1 notification, but " + size;
AlertNotification newNotification = updatedDefinition.getNotifications().get(0);
assert newNotification.getExtraConfig().size() == 1;
assert (Boolean) newNotification.getExtraConfig().get("detailedDiscovery") == false;
}

finally {
// delete the definition again
cleanupDefinition(definitionId);
}
}

@Test
public void testCRUDNotification() throws Exception {

Expand Down

0 comments on commit 9916185

Please sign in to comment.