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

Commit 41a68bd

Browse files
Libor Zoubekjkremser
authored andcommitted
Bug 1041393 - alert notification of type Resource operations is not
correctly rendered by API AlertNotificationRest extended with extraConfig field where we now output notification extra config (in case of Resource Operations alert sender, it's input parameters for operation) With this commit we now also properly render alert notification config (simple property types mapped correctly to JSON/XML, not "just" string values) This commit fixes ConfigurationHelper#configurationToMap which transforms Configuration to generic Map, now it can work without Configuration Definition - as a fall-back it maps all simple properties to string values. (cherry picked from commit dad83f3) Signed-off-by: Jirka Kremser <jkremser@redhat.com>
1 parent c529c47 commit 41a68bd

File tree

5 files changed

+108
-21
lines changed

5 files changed

+108
-21
lines changed

modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/AlertDefinitionHandlerBean.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
import org.rhq.core.domain.criteria.AlertDefinitionCriteria;
7878
import org.rhq.core.domain.measurement.DataType;
7979
import org.rhq.core.domain.measurement.MeasurementDefinition;
80+
import org.rhq.core.domain.operation.OperationDefinition;
8081
import org.rhq.core.domain.resource.Resource;
8182
import org.rhq.core.domain.resource.ResourceType;
8283
import org.rhq.core.domain.resource.group.GroupCategory;
@@ -89,6 +90,7 @@
8990
import org.rhq.enterprise.server.alert.AlertDefinitionManagerLocal;
9091
import org.rhq.enterprise.server.alert.AlertManagerLocal;
9192
import org.rhq.enterprise.server.alert.AlertNotificationManagerLocal;
93+
import org.rhq.enterprise.server.operation.OperationManagerLocal;
9294
import org.rhq.enterprise.server.plugin.pc.alert.AlertSenderInfo;
9395
import org.rhq.enterprise.server.plugin.pc.alert.AlertSenderPluginManager;
9496
import org.rhq.enterprise.server.resource.ResourceTypeManagerLocal;
@@ -98,6 +100,7 @@
98100
import org.rhq.enterprise.server.rest.domain.AlertNotificationRest;
99101
import org.rhq.enterprise.server.rest.domain.AlertSender;
100102
import org.rhq.enterprise.server.rest.domain.Link;
103+
import org.rhq.enterprise.server.rest.helper.ConfigurationHelper;
101104

102105
/**
103106
* Deal with Alert Definitions. Note that this class shares the /alert/ sub-context with the
@@ -128,6 +131,9 @@ public class AlertDefinitionHandlerBean extends AbstractRestBean {
128131
@EJB
129132
private ResourceTypeManagerLocal resourceTypeMgr;
130133

134+
@EJB
135+
private OperationManagerLocal operationMgr;
136+
131137

132138
@PersistenceContext(unitName = RHQConstants.PERSISTENCE_UNIT_NAME)
133139
private EntityManager entityManager;
@@ -453,8 +459,8 @@ private AlertNotification notificationRestToNotification(AlertDefinition alertDe
453459
// TODO validate sender
454460
notification.setAlertDefinition(alertDefinition);
455461
Configuration configuration = new Configuration();
456-
for (Map.Entry<String,Object> entry: anr.getConfig().entrySet()) {
457-
configuration.put(new PropertySimple(entry.getKey(),entry.getValue()));
462+
for (Map.Entry<String, Object> entry : anr.getConfig().entrySet()) {
463+
configuration.put(new PropertySimple(entry.getKey(), entry.getValue()));
458464
}
459465
notification.setConfiguration(configuration);
460466
// TODO extra configuration (?)
@@ -1186,12 +1192,17 @@ private AlertNotificationRest notificationToNotificationRest(AlertNotification n
11861192
AlertNotificationRest anr = new AlertNotificationRest();
11871193
anr.setId(notification.getId());
11881194
anr.setSenderName(notification.getSenderName());
1189-
1190-
for (Map.Entry<String, PropertySimple> entry : notification.getConfiguration().getSimpleProperties().entrySet()) {
1191-
anr.getConfig().put(entry.getKey(),entry.getValue().getStringValue()); // TODO correct type conversion of 2nd argument
1195+
ConfigurationDefinition configDef = notificationMgr.getConfigurationDefinitionForSender(notification
1196+
.getSenderName());
1197+
anr.setConfig(ConfigurationHelper.configurationToMap(notification.getConfiguration(), configDef, false));
1198+
ConfigurationDefinition extraConfigDef = null;
1199+
if ("Resource Operations".equals(notification.getSenderName())) {
1200+
OperationDefinition opDef = operationMgr.getOperationDefinition(caller,
1201+
Integer.valueOf(notification.getConfiguration().getSimpleValue("operation-definition-id", "0")));
1202+
extraConfigDef = opDef.getParametersConfigurationDefinition();
11921203
}
1193-
// TODO Extra Configuration
1194-
1204+
anr.setExtraConfig(ConfigurationHelper.configurationToMap(notification.getExtraConfiguration(), extraConfigDef,
1205+
false));
11951206
return anr;
11961207
}
11971208

modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/domain/AlertNotificationRest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class AlertNotificationRest {
3838
private int id;
3939
private String senderName;
4040
private Map<String,Object> config = new HashMap<String, Object>();
41+
private Map<String, Object> extraConfig = new HashMap<String, Object>();
4142

4243
public AlertNotificationRest() {
4344
}
@@ -68,4 +69,12 @@ public Map<String, Object> getConfig() {
6869
public void setConfig(Map<String, Object> config) {
6970
this.config = config;
7071
}
72+
73+
public void setExtraConfig(Map<String, Object> extraConfig) {
74+
this.extraConfig = extraConfig;
75+
}
76+
77+
public Map<String, Object> getExtraConfig() {
78+
return extraConfig;
79+
}
7180
}

modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/rest/helper/ConfigurationHelper.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ else if (mapValue instanceof List) {
7373

7474
}
7575

76+
/**
77+
* convert passed configuration to generic map
78+
* @param configuration to be converted
79+
* @param definition to convert to proper types, can be null, but strict has to be set to false. If null, all simple properties will be rendered as strings
80+
* @param strict if enabled all configuration properties must have appropriate property definitions
81+
* @return generic map
82+
* @throws IllegalArgumentException if strict is true and configuration contains property without matching property definition
83+
*/
7684
public static Map<String,Object> configurationToMap(Configuration configuration, ConfigurationDefinition definition,
7785
boolean strict) {
7886

@@ -89,13 +97,13 @@ public static Map<String,Object> configurationToMap(Configuration configuration,
8997
for (Property property : configuration.getProperties()) {
9098

9199
String propertyName = property.getName();
92-
PropertyDefinition propertyDefinition = definition.get(propertyName);
100+
PropertyDefinition propertyDefinition = null;
101+
if (definition != null) {
102+
propertyDefinition = definition.get(propertyName);
103+
}
93104
if (propertyDefinition==null) {
94105
if (strict) {
95106
throw new IllegalArgumentException("No definition for property " + propertyName + "found");
96-
} else {
97-
// no definition found and not strict, so skip the property
98-
continue;
99107
}
100108
}
101109

@@ -117,7 +125,7 @@ private static Object convertProperty(Property property, PropertyDefinition prop
117125
target = getInnerList(propertyList, (PropertyDefinitionList)propertyDefinition, strict);
118126
} else {
119127
target= convertSimplePropertyValue((PropertySimple) property,
120-
((PropertyDefinitionSimple)propertyDefinition));
128+
((PropertyDefinitionSimple) propertyDefinition), strict);
121129
}
122130
return target;
123131
}
@@ -131,7 +139,10 @@ private static Map<String, Object> getInnerMap(PropertyMap propertyMap, Property
131139
Set<String> names = map.keySet();
132140
for (String name : names ) {
133141
Property property = map.get(name);
134-
PropertyDefinition definition = propertyDefinition.get(name);
142+
PropertyDefinition definition = null;
143+
if (propertyDefinition != null) {
144+
definition = propertyDefinition.get(name);
145+
}
135146

136147
Object target = convertProperty(property,definition, strict);
137148
result.put(name,target);
@@ -148,13 +159,13 @@ private static List<Object> getInnerList(PropertyList propertyList, PropertyDefi
148159
if (definition==null) {
149160
if (strict) {
150161
throw new IllegalArgumentException("No Definition exists for " + propertyList.getName());
151-
} else {
152-
return result;
153162
}
154163
}
155164

156-
157-
PropertyDefinition memberDefinition = definition.getMemberDefinition();
165+
PropertyDefinition memberDefinition = null;
166+
if (definition != null) {
167+
memberDefinition = definition.getMemberDefinition();
168+
}
158169
for (Property property : propertyList.getList()) {
159170
Object target = convertProperty(property,memberDefinition, strict);
160171
result.add(target);
@@ -208,16 +219,21 @@ private static PropertyMap getPropertyMap(String propertyName, Map<String, Objec
208219
* @param definition Definition of the Property
209220
* @return Object with the correct type
210221
*/
211-
public static Object convertSimplePropertyValue(PropertySimple property, PropertyDefinitionSimple definition) {
222+
public static Object convertSimplePropertyValue(PropertySimple property, PropertyDefinitionSimple definition,
223+
boolean strict) {
212224

213-
if (definition==null) {
225+
if (definition == null && strict) {
214226
throw new IllegalArgumentException("No definition provided");
215227
}
216228

217229
if (property==null) {
218230
return null;
219231
}
220232

233+
if (definition == null) {
234+
return property.getStringValue();
235+
}
236+
221237
PropertySimpleType type = definition.getType();
222238
String val = property.getStringValue();
223239

modules/enterprise/server/jar/src/test/java/org/rhq/enterprise/server/configuration/ConfigurationHelperTest.java

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,47 @@ public void testConfigToMapComplexList() throws Exception {
639639

640640
}
641641

642+
@Test(expectedExceptions={IllegalArgumentException.class})
643+
public void testConfigToMapNoDefinitionStrict() throws Exception {
644+
ConfigurationHelper.configurationToMap(Configuration.builder().addSimple("test", "test").build(), null, true);
645+
}
646+
647+
@Test()
648+
public void testConfigToMapNoDefinition() throws Exception {
649+
Configuration config = Configuration.builder()
650+
.openList("list1", "map")
651+
.openMap()
652+
.addSimple("val1", "true")
653+
.addSimple("val2", "123")
654+
.closeMap()
655+
.closeList()
656+
.addSimple("simple1", "simple1")
657+
.openMap("map2")
658+
.addSimple("val3", "FOO")
659+
.closeMap()
660+
.build();
661+
662+
Map<String, Object> map = ConfigurationHelper.configurationToMap(config, null, false);
663+
assert map != null;
664+
assert map.size() == 3;
665+
assert map.get("list1") instanceof List;
666+
List list1 = (List) map.get("list1");
667+
assert list1.size() == 1;
668+
assert list1.get(0) instanceof Map;
669+
Map<String, Object> map1 = (Map<String, Object>) list1.get(0);
670+
assert map1.get("val1") instanceof String;
671+
assert "true".equals(map1.get("val1"));
672+
assert map1.get("val2") instanceof String;
673+
assert "123".equals(map1.get("val2"));
674+
assert map.get("simple1") instanceof String;
675+
assert "simple1".equals(map.get("simple1"));
676+
assert map.get("map2") instanceof Map;
677+
Map<String, Object> map2 = (Map<String, Object>) map.get("map2");
678+
assert map2.size() == 1;
679+
assert map2.get("val3") instanceof String;
680+
assert "FOO".equals(map2.get("val3"));
681+
}
682+
642683
@Test
643684
public void testConfigToMapComplexMap() throws Exception {
644685

@@ -735,7 +776,8 @@ public void testConfigToMapComplexMapWithBadSetupLenient() throws Exception {
735776
@Test
736777
public void testConvertSingleValueNoProperty() throws Exception {
737778

738-
Object o = ConfigurationHelper.convertSimplePropertyValue(null,new PropertyDefinitionSimple("dummy",null,false,PropertySimpleType.STRING));
779+
Object o = ConfigurationHelper.convertSimplePropertyValue(null, new PropertyDefinitionSimple("dummy", null,
780+
false, PropertySimpleType.STRING), true);
739781

740782
assert o == null;
741783

@@ -745,7 +787,7 @@ public void testConvertSingleValueNoProperty() throws Exception {
745787
public void testConvertSingleValueNoDefinition() throws Exception {
746788

747789
try {
748-
ConfigurationHelper.convertSimplePropertyValue(new PropertySimple("foo","bar"),null);
790+
ConfigurationHelper.convertSimplePropertyValue(new PropertySimple("foo", "bar"), null, true);
749791
assert false;
750792
}
751793
catch (IllegalArgumentException iae) {

modules/integration-tests/rest-api/src/test/java/org/rhq/modules/integrationTests/restApi/d/AlertNotification.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class AlertNotification {
3131
private int id;
3232
private String senderName;
3333
private Map<String,Object> config = new HashMap<String, Object>();
34+
private Map<String, Object> extraConfig = new HashMap<String, Object>();
3435

3536
public AlertNotification() {
3637
}
@@ -62,4 +63,12 @@ public Map<String, Object> getConfig() {
6263
public void setConfig(Map<String, Object> config) {
6364
this.config = config;
6465
}
66+
67+
public void setExtraConfig(Map<String, Object> extraConfig) {
68+
this.extraConfig = extraConfig;
69+
}
70+
71+
public Map<String, Object> getExtraConfig() {
72+
return extraConfig;
73+
}
6574
}

0 commit comments

Comments
 (0)