Skip to content
Browse files

Merge branch 'master' into evolution

  • Loading branch information...
2 parents 0b3cab8 + 2c7901e commit 9fc81c3697937a029abf93a87482171a12f412e1 Ryan Morgan committed
View
2 ChangeLog
@@ -92,6 +92,8 @@ Changes in HQApi 3.0
Changes in HQApi 2.6
+ *) [HHQ-4175] Add EmailAction support for alert definitions.
+
*) [HHQ-4151] Add new API to ResourceApi to find a platform Resource
based on the passed resource id. Extended support for this into the
CLI using the --parentPlatform flag to resource list.
View
66 hqapi1-plugin/src/main/groovy/app/AlertdefinitionController.groovy
@@ -26,6 +26,34 @@ public class AlertdefinitionController extends ApiController {
private eventBoss = Bootstrap.getBean(EventsBoss.class)
private aMan = Bootstrap.getBean(AlertDefinitionManager.class)
+ private EMAIL_NOTIFY_TYPE = [1:"email", 2:"users", 3:"roles"]
+
+ private String getNotificationNames(type, id) {
+ if (type == 1) {
+ return id
+ } else if (type == 2) {
+ def ids = id.split(",")
+ return ids.collect { getUser(it.toInteger(), null)?.name }.join(",")
+ } else if (type == 3) {
+ def ids = id.split(",")
+ return ids.collect { getRole(it.toInteger(), null)?.name }.join(",")
+ }
+ return null
+ }
+
+ private String getNotificationIds(type, name) {
+ if (type == 1) {
+ return name
+ } else if (type == 2) {
+ def names = name.split(",")
+ return names.collect {getUser(null, it)?.id}.findAll {it != null}.join(",")
+ } else if (type == 3) {
+ def names = name.split(",")
+ return names.collect {getRole(null, it)?.id}.findAll {it != null}.join(",")
+ }
+ return null
+ }
+
private EVENT_LEVEL_TO_NUM = [
ANY: -1,
ERR : LogTrackPlugin.LOGLEVEL_ERROR,
@@ -178,7 +206,6 @@ public class AlertdefinitionController extends ApiController {
}
for (a in d.actions) {
- // TODO: User and Role notifications only handled through Escalation
if (a.className == "com.hyperic.hq.bizapp.server.action.control.ScriptAction" ||
a.className == "org.hyperic.hq.bizapp.server.action.integrate.OpenNMSAction" ||
a.className == "com.hyperic.hq.bizapp.server.action.alert.SnmpAction") {
@@ -217,6 +244,18 @@ public class AlertdefinitionController extends ApiController {
value: config.getValue('action'))
}
}
+ } else if (a.className == "com.hyperic.hq.bizapp.server.action.email.EmailAction") {
+ def config = ConfigResponse.decode(a.config)
+ def names = config.getValue("names")
+ def listType = config.getValue("listType")?.toInteger()
+
+ AlertAction(id: a.id,
+ className: a.className) {
+ AlertActionConfig(key: 'notifyType',
+ value: EMAIL_NOTIFY_TYPE[listType])
+ AlertActionConfig(key: 'names',
+ value: getNotificationNames(listType,names))
+ }
} else if (showAllActions) {
AlertAction(id: a.id,
className: a.className)
@@ -719,6 +758,31 @@ public class AlertdefinitionController extends ApiController {
xmlAction['AlertActionConfig'])
continue
}
+ } else if (className == "com.hyperic.hq.bizapp.server.action.email.EmailAction") {
+ def typeName = xmlAction['AlertActionConfig'].find {
+ it.'@key' == 'notifyType'
+ }?.'@value'
+
+ def names = xmlAction['AlertActionConfig'].find {
+ it.'@key' == 'names'
+ }?.'@value'
+
+ def type = EMAIL_NOTIFY_TYPE.find { it.value == typeName }?.key
+
+ if (!type) {
+ log.warn("Ignoring invalid EmailAction type " + typeName)
+ continue
+ }
+
+ def notificationIds = getNotificationIds(type, names)
+ if (notificationIds == null || notificationIds.length() == 0) {
+ log.warn("Ignoring invalid EmailAction notification=" + names)
+ continue
+ }
+
+ cfg['listType'] = type.toString()
+ cfg['names'] = notificationIds
+ cfg['sms'] = 'false' // XXX: Legacy a presume..
} else {
for (xmlConfig in xmlAction['AlertActionConfig']) {
cfg[xmlConfig.'@key'] = xmlConfig.'@value'
View
187 hqapi1/src/main/java/org/hyperic/hq/hqapi1/AlertDefinitionBuilder.java
@@ -32,6 +32,8 @@
import org.hyperic.hq.hqapi1.types.AlertAction;
import org.hyperic.hq.hqapi1.types.AlertActionConfig;
import org.hyperic.hq.hqapi1.types.Resource;
+import org.hyperic.hq.hqapi1.types.Role;
+import org.hyperic.hq.hqapi1.types.User;
/**
* This class is used to create {@link org.hyperic.hq.hqapi1.types.AlertCondition}s.
@@ -469,4 +471,189 @@ public static AlertAction createSnmpAction(String address,
return a;
}
+
+ /**
+ * Add the list of {@link User}s to the list of notifications for this alert definition.
+ *
+ * @param d The {@link org.hyperic.hq.hqapi1.types.AlertDefinition} to modify.
+ * @param users The list of Users to notify.
+ */
+ public static void addEmailAction(AlertDefinition d, User[] users) {
+
+ for (AlertAction a : d.getAlertAction()) {
+ if (a.getClassName().equals("com.hyperic.hq.bizapp.server.action.email.EmailAction")) {
+ boolean actionExists = false;
+ AlertActionConfig names = null;
+ for (AlertActionConfig cfg : a.getAlertActionConfig()) {
+ if (cfg.getKey().equals("notifyType") &&
+ cfg.getValue().equals("users")) {
+ actionExists = true;
+ }
+ if (cfg.getKey().equals("names")) {
+ names = cfg;
+ }
+ }
+
+ if (actionExists && names != null) {
+ // Set to the new value
+ StringBuffer nameList = new StringBuffer();
+ for (int i = 0; i < users.length; i++) {
+ nameList.append(users[i].getName());
+ if (i + 1 < users.length) {
+ nameList.append(",");
+ }
+ }
+ names.setValue(nameList.toString());
+ return;
+ }
+ }
+ }
+
+ // Action does not exist, create it.
+ AlertAction a = new AlertAction();
+ a.setClassName("com.hyperic.hq.bizapp.server.action.email.EmailAction");
+
+ AlertActionConfig type = new AlertActionConfig();
+ type.setKey("notifyType");
+ type.setValue("users");
+
+ StringBuffer nameList = new StringBuffer();
+ for (int i = 0; i < users.length; i++) {
+ nameList.append(users[i].getName());
+ if (i + 1 < users.length) {
+ nameList.append(",");
+ }
+ }
+ AlertActionConfig names = new AlertActionConfig();
+ names.setKey("names");
+ names.setValue(nameList.toString());
+
+ a.getAlertActionConfig().add(type);
+ a.getAlertActionConfig().add(names);
+
+ d.getAlertAction().add(a);
+ }
+
+ /**
+ * Add the list of {@link Role}s to the list of notifications for this alert definition.
+ *
+ * @param d The {@link org.hyperic.hq.hqapi1.types.AlertDefinition} to modify.
+ * @param roles The list of Roles to notify.
+ */
+ public static void addEmailAction(AlertDefinition d, Role[] roles) {
+ for (AlertAction a : d.getAlertAction()) {
+ if (a.getClassName().equals("com.hyperic.hq.bizapp.server.action.email.EmailAction")) {
+ boolean actionExists = false;
+ AlertActionConfig names = null;
+ for (AlertActionConfig cfg : a.getAlertActionConfig()) {
+ if (cfg.getKey().equals("notifyType") &&
+ cfg.getValue().equals("roles")) {
+ actionExists = true;
+ }
+ if (cfg.getKey().equals("names")) {
+ names = cfg;
+ }
+ }
+
+ if (actionExists && names != null) {
+ // Set to the new value
+ StringBuffer nameList = new StringBuffer();
+ for (int i = 0; i < roles.length; i++) {
+ nameList.append(roles[i].getName());
+ if (i + 1 < roles.length) {
+ nameList.append(",");
+ }
+ }
+ names.setValue(nameList.toString());
+ return;
+ }
+ }
+ }
+
+ // Action does not exist, create it.
+ AlertAction a = new AlertAction();
+ a.setClassName("com.hyperic.hq.bizapp.server.action.email.EmailAction");
+
+ AlertActionConfig type = new AlertActionConfig();
+ type.setKey("notifyType");
+ type.setValue("roles");
+
+ StringBuffer nameList = new StringBuffer();
+ for (int i = 0; i < roles.length; i++) {
+ nameList.append(roles[i].getName());
+ if (i+1 < roles.length) {
+ nameList.append(",");
+ }
+ }
+ AlertActionConfig names = new AlertActionConfig();
+ names.setKey("names");
+ names.setValue(nameList.toString());
+
+ a.getAlertActionConfig().add(type);
+ a.getAlertActionConfig().add(names);
+
+ d.getAlertAction().add(a);
+ }
+
+ /**
+ * Add the list of email addresses to the list of notifications for this alert definition.
+ *
+ * @param d The {@link org.hyperic.hq.hqapi1.types.AlertDefinition} to modify.
+ * @param otherRecipients The list of email addresses to notify.
+ */
+ public static void addEmailAction(AlertDefinition d, String[] otherRecipients) {
+
+ for (AlertAction a : d.getAlertAction()) {
+ if (a.getClassName().equals("com.hyperic.hq.bizapp.server.action.email.EmailAction")) {
+ boolean actionExists = false;
+ AlertActionConfig names = null;
+ for (AlertActionConfig cfg : a.getAlertActionConfig()) {
+ if (cfg.getKey().equals("notifyType") &&
+ cfg.getValue().equals("email")) {
+ actionExists = true;
+ }
+ if (cfg.getKey().equals("names")) {
+ names = cfg;
+ }
+ }
+
+ if (actionExists && names != null) {
+ // Set to the new value
+ StringBuffer nameList = new StringBuffer();
+ for (int i = 0; i < otherRecipients.length; i++) {
+ nameList.append(otherRecipients[i]);
+ if (i + 1 < otherRecipients.length) {
+ nameList.append(",");
+ }
+ }
+ names.setValue(nameList.toString());
+ return;
+ }
+ }
+ }
+
+ // Action does not exist, create it.
+ AlertAction a = new AlertAction();
+ a.setClassName("com.hyperic.hq.bizapp.server.action.email.EmailAction");
+
+ AlertActionConfig type = new AlertActionConfig();
+ type.setKey("notifyType");
+ type.setValue("email");
+
+ StringBuffer nameList = new StringBuffer();
+ for (int i = 0; i < otherRecipients.length; i++) {
+ nameList.append(otherRecipients[i]);
+ if (i+1 < otherRecipients.length) {
+ nameList.append(",");
+ }
+ }
+ AlertActionConfig names = new AlertActionConfig();
+ names.setKey("names");
+ names.setValue(nameList.toString());
+
+ a.getAlertActionConfig().add(type);
+ a.getAlertActionConfig().add(names);
+
+ d.getAlertAction().add(a);
+ }
}
View
54 hqapi1/src/main/java/org/hyperic/hq/hqapi1/tools/AlertDefinitionCommand.java
@@ -38,16 +38,20 @@
import org.hyperic.hq.hqapi1.types.AlertAction;
import org.hyperic.hq.hqapi1.types.AlertDefinition;
import org.hyperic.hq.hqapi1.types.AlertDefinitionsResponse;
-import org.hyperic.hq.hqapi1.types.Resource;
import org.hyperic.hq.hqapi1.types.ResourceResponse;
import org.hyperic.hq.hqapi1.types.ResourcesResponse;
+import org.hyperic.hq.hqapi1.types.Role;
+import org.hyperic.hq.hqapi1.types.RoleResponse;
import org.hyperic.hq.hqapi1.types.StatusResponse;
import org.hyperic.hq.hqapi1.types.EscalationResponse;
import org.hyperic.hq.hqapi1.types.Escalation;
import org.hyperic.hq.hqapi1.types.AlertCondition;
import org.springframework.stereotype.Component;
+import org.hyperic.hq.hqapi1.types.User;
+import org.hyperic.hq.hqapi1.types.UserResponse;
import java.io.InputStream;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Iterator;
@@ -82,6 +86,9 @@
private static String OPT_ASSIGN_CONTROLACTION = "assignControlAction";
private static String OPT_CLEAR_ESC = "clearEscalation";
private static String OPT_CLEAR_ACTIONS = "clearActions";
+ private static String OPT_ASSIGN_USER_NOTIFICATION = "assignUserNotification";
+ private static String OPT_ASSIGN_ROLE_NOTIFICATION = "assignRoleNotification";
+ private static String OPT_ASSIGN_OTHER_NOTIFICATION = "assignOtherNotification";
private void printUsage() {
System.err.println("One of " + Arrays.toString(COMMANDS) + " required");
@@ -313,6 +320,15 @@ private void sync(String[] args) throws Exception {
"all alert definitions in this sync");
p.accepts(OPT_CLEAR_ACTIONS, "If specified, clear alert actions from " +
"all alert definitions in this sync");
+ p.accepts(OPT_ASSIGN_USER_NOTIFICATION, "If specified, assign notification to the given " +
+ "comma separated list of users").
+ withRequiredArg().ofType(String.class);
+ p.accepts(OPT_ASSIGN_ROLE_NOTIFICATION, "If specified, assign notification to the given " +
+ "comma separated list of roles").
+ withRequiredArg().ofType(String.class);
+ p.accepts(OPT_ASSIGN_OTHER_NOTIFICATION, "If specified, assign notification to the given " +
+ "comma separated list of email addresses").
+ withRequiredArg().ofType(String.class);
OptionSet options = getOptions(p, args);
@@ -374,6 +390,42 @@ private void sync(String[] args) throws Exception {
}
}
+ if (options.has(OPT_ASSIGN_USER_NOTIFICATION)) {
+ String[] users = ((String)getRequired(options, OPT_ASSIGN_USER_NOTIFICATION)).split(",");
+ List<User> checkedUsers = new ArrayList<User>();
+ for (String u : users) {
+ UserResponse uResponse = api.getUserApi().getUser(u);
+ checkSuccess(uResponse);
+ checkedUsers.add(uResponse.getUser());
+ }
+
+ for (AlertDefinition def : definitions) {
+ AlertDefinitionBuilder.addEmailAction(def, checkedUsers.toArray(new User[checkedUsers.size()]));
+ }
+ }
+
+ if (options.has(OPT_ASSIGN_ROLE_NOTIFICATION)) {
+ String[] roles = ((String)getRequired(options, OPT_ASSIGN_ROLE_NOTIFICATION)).split(",");
+ ArrayList<Role> checkedRoles = new ArrayList<Role>();
+ for (String r : roles) {
+ RoleResponse rResponse = api.getRoleApi().getRole(r);
+ checkSuccess(rResponse);
+ checkedRoles.add(rResponse.getRole());
+ }
+
+ for (AlertDefinition def : definitions) {
+ AlertDefinitionBuilder.addEmailAction(def, checkedRoles.toArray(new Role[checkedRoles.size()]));
+ }
+ }
+
+ if (options.has(OPT_ASSIGN_OTHER_NOTIFICATION)) {
+ String[] emails = ((String)getRequired(options, OPT_ASSIGN_OTHER_NOTIFICATION)).split(",");
+
+ for (AlertDefinition def : definitions) {
+ AlertDefinitionBuilder.addEmailAction(def, emails);
+ }
+ }
+
System.out.println("Syncing " + definitions.size() + " alert definitions");
int numSynced = 0;
View
478 src/org/hyperic/hq/hqapi1/test/AlertDefinitionSyncEmailAction_test.java
@@ -0,0 +1,478 @@
+package org.hyperic.hq.hqapi1.test;
+
+import org.hyperic.hq.hqapi1.AlertDefinitionApi;
+import org.hyperic.hq.hqapi1.AlertDefinitionBuilder;
+import org.hyperic.hq.hqapi1.HQApi;
+import org.hyperic.hq.hqapi1.types.AlertAction;
+import org.hyperic.hq.hqapi1.types.AlertActionConfig;
+import org.hyperic.hq.hqapi1.types.AlertDefinition;
+import org.hyperic.hq.hqapi1.types.AlertDefinitionsResponse;
+import org.hyperic.hq.hqapi1.types.Metric;
+import org.hyperic.hq.hqapi1.types.MetricsResponse;
+import org.hyperic.hq.hqapi1.types.Resource;
+import org.hyperic.hq.hqapi1.types.Role;
+import org.hyperic.hq.hqapi1.types.User;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class AlertDefinitionSyncEmailAction_test extends AlertDefinitionTestBase {
+
+ public AlertDefinitionSyncEmailAction_test(String name) {
+ super(name);
+ }
+
+ private AlertDefinition generateDefinition(HQApi api) throws Exception {
+
+ Resource platform = getLocalPlatformResource(false, false);
+
+ MetricsResponse metricsResponse = api.getMetricApi().getMetrics(platform);
+ hqAssertSuccess(metricsResponse);
+ assertTrue("No metrics found for " + platform.getName(),
+ metricsResponse.getMetric().size() > 0);
+ Metric m = metricsResponse.getMetric().get(0);
+
+ AlertDefinition d = generateTestDefinition();
+ d.setResource(platform);
+ d.getAlertCondition().add(
+ AlertDefinitionBuilder.createChangeCondition(true, m.getName()));
+
+ return d;
+ }
+
+ public void testAddUserNotification() throws Exception {
+ HQApi api = getApi();
+ AlertDefinitionApi defApi = api.getAlertDefinitionApi();
+
+ AlertDefinition d = generateDefinition(api);
+
+ List<User> users = createTestUsers(1);
+ User u = users.get(0);
+
+ AlertDefinitionBuilder.addEmailAction(d, users.toArray(new User[users.size()]));
+ List<AlertDefinition> definitions = new ArrayList<AlertDefinition>();
+ definitions.add(d);
+ AlertDefinitionsResponse response = defApi.syncAlertDefinitions(definitions);
+ hqAssertSuccess(response);
+
+ AlertDefinition syncedDef = response.getAlertDefinition().get(0);
+ assertEquals("Wrong number of actions", 1, syncedDef.getAlertAction().size());
+ assertTrue("Wrong action class",
+ "com.hyperic.hq.bizapp.server.action.email.EmailAction".
+ equals(syncedDef.getAlertAction().get(0).getClassName()));
+
+ for (AlertActionConfig c : syncedDef.getAlertAction().get(0).getAlertActionConfig()) {
+ if (c.getKey().equals("names")) {
+ assertEquals("Wrong user name", u.getName(), c.getValue());
+ }
+ }
+
+ // Clear the action's and resync
+ syncedDef.getAlertAction().clear();
+
+ definitions.clear();
+ definitions.add(syncedDef);
+ response = defApi.syncAlertDefinitions(definitions);
+ hqAssertSuccess(response);
+
+ syncedDef = response.getAlertDefinition().get(0);
+ assertEquals("Alert actions not cleared!", 0, syncedDef.getAlertAction().size());
+
+ cleanup(response.getAlertDefinition());
+ deleteTestUsers(users);
+ }
+
+ public void testAddMultiUserNotification() throws Exception {
+ HQApi api = getApi();
+ AlertDefinitionApi defApi = api.getAlertDefinitionApi();
+
+ AlertDefinition d = generateDefinition(api);
+
+ List<User> users = createTestUsers(10);
+
+ AlertDefinitionBuilder.addEmailAction(d, users.toArray(new User[users.size()]));
+
+ List<AlertDefinition> definitions = new ArrayList<AlertDefinition>();
+ definitions.add(d);
+ AlertDefinitionsResponse response = defApi.syncAlertDefinitions(definitions);
+ hqAssertSuccess(response);
+
+ AlertDefinition syncedDef = response.getAlertDefinition().get(0);
+ assertEquals("Wrong number of actions", 1, syncedDef.getAlertAction().size());
+ assertTrue("Wrong action class",
+ "com.hyperic.hq.bizapp.server.action.email.EmailAction".
+ equals(syncedDef.getAlertAction().get(0).getClassName()));
+
+ for (AlertActionConfig c : syncedDef.getAlertAction().get(0).getAlertActionConfig()) {
+ if (c.getKey().equals("names")) {
+ for (User u : users) {
+ assertTrue("User " + u.getName() + " not found in notify list!",
+ c.getValue().indexOf(u.getName()) != -1);
+ }
+ }
+ }
+
+ // Clear the action's and resync
+ syncedDef.getAlertAction().clear();
+
+ definitions.clear();
+ definitions.add(syncedDef);
+ response = defApi.syncAlertDefinitions(definitions);
+ hqAssertSuccess(response);
+
+ syncedDef = response.getAlertDefinition().get(0);
+ assertEquals("Alert actions not cleared!", 0, syncedDef.getAlertAction().size());
+
+ cleanup(response.getAlertDefinition());
+ deleteTestUsers(users);
+ }
+
+ public void testAddNonExistantUserNotification() throws Exception {
+ HQApi api = getApi();
+ AlertDefinitionApi defApi = api.getAlertDefinitionApi();
+
+ AlertDefinition d = generateDefinition(api);
+
+ User u = new User();
+ u.setName("Non-existant user!");
+
+ AlertDefinitionBuilder.addEmailAction(d, new User[] { u });
+
+ List<AlertDefinition> definitions = new ArrayList<AlertDefinition>();
+ definitions.add(d);
+ AlertDefinitionsResponse response = defApi.syncAlertDefinitions(definitions);
+ hqAssertSuccess(response);
+
+ cleanup(response.getAlertDefinition());
+ }
+
+ public void testAddNonExistantUsersNotification() throws Exception {
+ HQApi api = getApi();
+ AlertDefinitionApi defApi = api.getAlertDefinitionApi();
+
+ AlertDefinition d = generateDefinition(api);
+
+ User u = new User();
+ u.setName("Non-existant user!");
+ User u2 = new User();
+ u2.setName("Non-existant user!");
+
+ AlertDefinitionBuilder.addEmailAction(d, new User[] { u, u2 });
+
+ List<AlertDefinition> definitions = new ArrayList<AlertDefinition>();
+ definitions.add(d);
+ AlertDefinitionsResponse response = defApi.syncAlertDefinitions(definitions);
+ hqAssertSuccess(response);
+
+ cleanup(response.getAlertDefinition());
+ }
+
+ public void testAddMultiUserSomeInvalidNotification() throws Exception {
+ HQApi api = getApi();
+ AlertDefinitionApi defApi = api.getAlertDefinitionApi();
+
+ AlertDefinition d = generateDefinition(api);
+
+ List<User> users = createTestUsers(10);
+
+ User invalidUser = new User();
+ invalidUser.setName("Invalid User!");
+
+ User[] userList = users.toArray(new User[users.size()+1]);
+ userList[users.size()] = invalidUser;
+
+ AlertDefinitionBuilder.addEmailAction(d, userList);
+
+ List<AlertDefinition> definitions = new ArrayList<AlertDefinition>();
+ definitions.add(d);
+ AlertDefinitionsResponse response = defApi.syncAlertDefinitions(definitions);
+ hqAssertSuccess(response);
+
+ AlertDefinition syncedDef = response.getAlertDefinition().get(0);
+ assertEquals("Wrong number of actions", 1, syncedDef.getAlertAction().size());
+ assertTrue("Wrong action class",
+ "com.hyperic.hq.bizapp.server.action.email.EmailAction".
+ equals(syncedDef.getAlertAction().get(0).getClassName()));
+
+ for (AlertActionConfig c : syncedDef.getAlertAction().get(0).getAlertActionConfig()) {
+ if (c.getKey().equals("names")) {
+ for (User u : users) {
+ assertTrue("User " + u.getName() + " not found in notify list!",
+ c.getValue().indexOf(u.getName()) != -1);
+ }
+ }
+ }
+
+ // Clear the action's and resync
+ syncedDef.getAlertAction().clear();
+
+ definitions.clear();
+ definitions.add(syncedDef);
+ response = defApi.syncAlertDefinitions(definitions);
+ hqAssertSuccess(response);
+
+ syncedDef = response.getAlertDefinition().get(0);
+ assertEquals("Alert actions not cleared!", 0, syncedDef.getAlertAction().size());
+
+ cleanup(response.getAlertDefinition());
+ deleteTestUsers(users);
+ }
+
+ public void testAddRoleNotification() throws Exception {
+ HQApi api = getApi();
+ AlertDefinitionApi defApi = api.getAlertDefinitionApi();
+
+ AlertDefinition d = generateDefinition(api);
+
+ Role r = createRole(Collections.EMPTY_LIST, Collections.EMPTY_LIST);
+
+ AlertDefinitionBuilder.addEmailAction(d, new Role[] {r});
+
+ List<AlertDefinition> definitions = new ArrayList<AlertDefinition>();
+ definitions.add(d);
+ AlertDefinitionsResponse response = defApi.syncAlertDefinitions(definitions);
+ hqAssertSuccess(response);
+
+ AlertDefinition syncedDef = response.getAlertDefinition().get(0);
+ assertEquals("Wrong number of actions", 1, syncedDef.getAlertAction().size());
+ assertTrue("Wrong action class",
+ "com.hyperic.hq.bizapp.server.action.email.EmailAction".
+ equals(syncedDef.getAlertAction().get(0).getClassName()));
+
+ for (AlertActionConfig c : syncedDef.getAlertAction().get(0).getAlertActionConfig()) {
+ if (c.getKey().equals("names")) {
+ assertEquals("Wrong role name", r.getName(), c.getValue());
+ }
+ }
+
+ // Clear the action's and resync
+ syncedDef.getAlertAction().clear();
+
+ definitions.clear();
+ definitions.add(syncedDef);
+ response = defApi.syncAlertDefinitions(definitions);
+ hqAssertSuccess(response);
+
+ syncedDef = response.getAlertDefinition().get(0);
+ assertEquals("Alert actions not cleared!", 0, syncedDef.getAlertAction().size());
+
+ cleanup(response.getAlertDefinition());
+ cleanupRole(r);
+ }
+
+ public void testAddMultiRoleNotification() throws Exception {
+ HQApi api = getApi();
+ AlertDefinitionApi defApi = api.getAlertDefinitionApi();
+
+ AlertDefinition d = generateDefinition(api);
+
+ Role r = createRole(Collections.EMPTY_LIST, Collections.EMPTY_LIST);
+ Role r2 = createRole(Collections.EMPTY_LIST, Collections.EMPTY_LIST);
+
+ AlertDefinitionBuilder.addEmailAction(d, new Role[] {r, r2});
+
+ List<AlertDefinition> definitions = new ArrayList<AlertDefinition>();
+ definitions.add(d);
+ AlertDefinitionsResponse response = defApi.syncAlertDefinitions(definitions);
+ hqAssertSuccess(response);
+
+ AlertDefinition syncedDef = response.getAlertDefinition().get(0);
+ assertEquals("Wrong number of actions", 1, syncedDef.getAlertAction().size());
+ assertTrue("Wrong action class",
+ "com.hyperic.hq.bizapp.server.action.email.EmailAction".
+ equals(syncedDef.getAlertAction().get(0).getClassName()));
+
+ for (AlertActionConfig c : syncedDef.getAlertAction().get(0).getAlertActionConfig()) {
+ if (c.getKey().equals("names")) {
+ assertTrue("Role name not found in list", c.getValue().indexOf(r.getName()) != -1);
+ assertTrue("Role name not found in list", c.getValue().indexOf(r2.getName()) != -1);
+ }
+ }
+
+ // Clear the action's and resync
+ syncedDef.getAlertAction().clear();
+
+ definitions.clear();
+ definitions.add(syncedDef);
+ response = defApi.syncAlertDefinitions(definitions);
+ hqAssertSuccess(response);
+
+ syncedDef = response.getAlertDefinition().get(0);
+ assertEquals("Alert actions not cleared!", 0, syncedDef.getAlertAction().size());
+
+ cleanup(response.getAlertDefinition());
+ cleanupRole(r);
+ cleanupRole(r2);
+ }
+
+ public void testAddNonExistantRoleNotification() throws Exception {
+ HQApi api = getApi();
+ AlertDefinitionApi defApi = api.getAlertDefinitionApi();
+
+ AlertDefinition d = generateDefinition(api);
+
+ Role r = new Role();
+ r.setName("Non-existant role!");
+
+ AlertDefinitionBuilder.addEmailAction(d, new Role[] { r });
+
+ List<AlertDefinition> definitions = new ArrayList<AlertDefinition>();
+ definitions.add(d);
+ AlertDefinitionsResponse response = defApi.syncAlertDefinitions(definitions);
+ hqAssertSuccess(response);
+
+ cleanup(response.getAlertDefinition());
+ }
+
+ public void testAddNonExistantRolesNotification() throws Exception {
+ HQApi api = getApi();
+ AlertDefinitionApi defApi = api.getAlertDefinitionApi();
+
+ AlertDefinition d = generateDefinition(api);
+
+ Role r = new Role();
+ r.setName("Non-existant role!");
+ Role r2 = new Role();
+ r2.setName("Non-existant role!");
+
+ AlertDefinitionBuilder.addEmailAction(d, new Role[] { r, r2 });
+
+ List<AlertDefinition> definitions = new ArrayList<AlertDefinition>();
+ definitions.add(d);
+ AlertDefinitionsResponse response = defApi.syncAlertDefinitions(definitions);
+ hqAssertSuccess(response);
+
+ cleanup(response.getAlertDefinition());
+ }
+
+ public void testAddMultiRoleSomeInvalidNotification() throws Exception {
+ HQApi api = getApi();
+ AlertDefinitionApi defApi = api.getAlertDefinitionApi();
+
+ AlertDefinition d = generateDefinition(api);
+
+ Role r = createRole(Collections.EMPTY_LIST, Collections.EMPTY_LIST);
+ Role r2 = createRole(Collections.EMPTY_LIST, Collections.EMPTY_LIST);
+ Role r3 = new Role();
+ r3.setName("Non-existant role!");
+
+ AlertDefinitionBuilder.addEmailAction(d, new Role[] {r, r2, r3});
+
+ List<AlertDefinition> definitions = new ArrayList<AlertDefinition>();
+ definitions.add(d);
+ AlertDefinitionsResponse response = defApi.syncAlertDefinitions(definitions);
+ hqAssertSuccess(response);
+
+ AlertDefinition syncedDef = response.getAlertDefinition().get(0);
+ assertEquals("Wrong number of actions", 1, syncedDef.getAlertAction().size());
+ assertTrue("Wrong action class",
+ "com.hyperic.hq.bizapp.server.action.email.EmailAction".
+ equals(syncedDef.getAlertAction().get(0).getClassName()));
+
+ for (AlertActionConfig c : syncedDef.getAlertAction().get(0).getAlertActionConfig()) {
+ if (c.getKey().equals("names")) {
+ assertTrue("Role name not found in list", c.getValue().indexOf(r.getName()) != -1);
+ assertTrue("Role name not found in list", c.getValue().indexOf(r2.getName()) != -1);
+ }
+ }
+
+ // Clear the action's and resync
+ syncedDef.getAlertAction().clear();
+
+ definitions.clear();
+ definitions.add(syncedDef);
+ response = defApi.syncAlertDefinitions(definitions);
+ hqAssertSuccess(response);
+
+ syncedDef = response.getAlertDefinition().get(0);
+ assertEquals("Alert actions not cleared!", 0, syncedDef.getAlertAction().size());
+
+ cleanup(response.getAlertDefinition());
+ cleanupRole(r);
+ cleanupRole(r2);
+ }
+
+ public void testAddOtherRecipientNotification() throws Exception {
+ HQApi api = getApi();
+ AlertDefinitionApi defApi = api.getAlertDefinitionApi();
+
+ AlertDefinition d = generateDefinition(api);
+
+ final String EMAIL = "hqapi@vmware.com";
+ AlertDefinitionBuilder.addEmailAction(d, new String[] {EMAIL});
+
+ List<AlertDefinition> definitions = new ArrayList<AlertDefinition>();
+ definitions.add(d);
+ AlertDefinitionsResponse response = defApi.syncAlertDefinitions(definitions);
+ hqAssertSuccess(response);
+
+ AlertDefinition syncedDef = response.getAlertDefinition().get(0);
+ assertEquals("Wrong number of actions", 1, syncedDef.getAlertAction().size());
+ assertTrue("Wrong action class",
+ "com.hyperic.hq.bizapp.server.action.email.EmailAction".
+ equals(syncedDef.getAlertAction().get(0).getClassName()));
+
+ for (AlertActionConfig c : syncedDef.getAlertAction().get(0).getAlertActionConfig()) {
+ if (c.getKey().equals("names")) {
+ assertEquals("Wrong email", EMAIL, c.getValue());
+ }
+ }
+
+ // Clear the action's and resync
+ syncedDef.getAlertAction().clear();
+
+ definitions.clear();
+ definitions.add(syncedDef);
+ response = defApi.syncAlertDefinitions(definitions);
+ hqAssertSuccess(response);
+
+ syncedDef = response.getAlertDefinition().get(0);
+ assertEquals("Alert actions not cleared!", 0, syncedDef.getAlertAction().size());
+
+ cleanup(response.getAlertDefinition());
+ }
+
+ public void testAddOtherRecipientsNotification() throws Exception {
+ HQApi api = getApi();
+ AlertDefinitionApi defApi = api.getAlertDefinitionApi();
+
+ AlertDefinition d = generateDefinition(api);
+
+ final String EMAIL = "hqapi@vmware.com";
+ final String EMAIL2 = "test@vmare.com";
+
+ AlertDefinitionBuilder.addEmailAction(d, new String[] {EMAIL,EMAIL2});
+
+ List<AlertDefinition> definitions = new ArrayList<AlertDefinition>();
+ definitions.add(d);
+ AlertDefinitionsResponse response = defApi.syncAlertDefinitions(definitions);
+ hqAssertSuccess(response);
+
+ AlertDefinition syncedDef = response.getAlertDefinition().get(0);
+ assertEquals("Wrong number of actions", 1, syncedDef.getAlertAction().size());
+ assertTrue("Wrong action class",
+ "com.hyperic.hq.bizapp.server.action.email.EmailAction".
+ equals(syncedDef.getAlertAction().get(0).getClassName()));
+
+ for (AlertActionConfig c : syncedDef.getAlertAction().get(0).getAlertActionConfig()) {
+ if (c.getKey().equals("names")) {
+ assertTrue("Email not found in list", c.getValue().indexOf(EMAIL) != -1);
+ assertTrue("Email not found in list", c.getValue().indexOf(EMAIL2) != -1);
+ }
+ }
+
+ // Clear the action's and resync
+ syncedDef.getAlertAction().clear();
+
+ definitions.clear();
+ definitions.add(syncedDef);
+ response = defApi.syncAlertDefinitions(definitions);
+ hqAssertSuccess(response);
+
+ syncedDef = response.getAlertDefinition().get(0);
+ assertEquals("Alert actions not cleared!", 0, syncedDef.getAlertAction().size());
+
+ cleanup(response.getAlertDefinition());
+ }
+}

0 comments on commit 9fc81c3

Please sign in to comment.
Something went wrong with that request. Please try again.