Fix guard payload
Fix guard payload nullpointerException when guard list is empty or null
Issue-ID: CLAMP-303
Change-Id: I6f714a877036cbc531dd35505202dde227cc224a
Signed-off-by: sebdet <sebastien.determe@intl.att.com>
diff --git a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java
index 1859a4d..1e35ad6 100644
--- a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java
+++ b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java
@@ -183,18 +183,20 @@
/**
* Return a map containing all Guard policies indexed by Guard policy Name.
- *
+ *
* @return The Guards map
*/
public Map<String, String> createGuardPolicyPayloads() {
Map<String, String> result = new HashMap<>();
- JsonObject guard = new JsonObject();
JsonElement guardsList = this.getConfigurationsJson().get("guard_policies");
- for (Entry<String, JsonElement> guardElem : guardsList.getAsJsonObject().entrySet()) {
- guard.addProperty("policy-id", guardElem.getKey());
- guard.add("contents", guardElem.getValue());
- result.put(guardElem.getKey(), new GsonBuilder().create().toJson(guard));
+ if (guardsList != null) {
+ for (Entry<String, JsonElement> guardElem : guardsList.getAsJsonObject().entrySet()) {
+ JsonObject guard = new JsonObject();
+ guard.addProperty("policy-id", guardElem.getKey());
+ guard.add("contents", guardElem.getValue());
+ result.put(guardElem.getKey(), new GsonBuilder().create().toJson(guard));
+ }
}
return result;
}
diff --git a/src/test/java/org/onap/clamp/policy/microservice/OperationalPolicyPayloadTest.java b/src/test/java/org/onap/clamp/policy/microservice/OperationalPolicyPayloadTest.java
index 739708ca..76e29c3 100644
--- a/src/test/java/org/onap/clamp/policy/microservice/OperationalPolicyPayloadTest.java
+++ b/src/test/java/org/onap/clamp/policy/microservice/OperationalPolicyPayloadTest.java
@@ -48,17 +48,28 @@
}
@Test
+ public void testGuardPolicyEmptyPayloadConstruction() throws IOException {
+ JsonObject jsonConfig = new GsonBuilder().create().fromJson(
+ ResourceFileUtil.getResourceAsString("tosca/operational-policy-no-guard-properties.json"),
+ JsonObject.class);
+ OperationalPolicy policy = new OperationalPolicy("testPolicy", null, jsonConfig);
+ Map<String, String> guardsMap = policy.createGuardPolicyPayloads();
+ assertThat(guardsMap).isEmpty();
+ assertThat(guardsMap.entrySet()).isEmpty();
+ }
+
+ @Test
public void testGuardPolicyPayloadConstruction() throws IOException {
JsonObject jsonConfig = new GsonBuilder().create().fromJson(
ResourceFileUtil.getResourceAsString("tosca/operational-policy-properties.json"), JsonObject.class);
OperationalPolicy policy = new OperationalPolicy("testPolicy", null, jsonConfig);
- Map<String, String> guardsList = policy.createGuardPolicyPayloads();
+ Map<String, String> guardsMap = policy.createGuardPolicyPayloads();
JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/guard1-policy-payload.json"),
- guardsList.get("guard1"), false);
+ guardsMap.get("guard1"), false);
JSONAssert.assertEquals(ResourceFileUtil.getResourceAsString("tosca/guard2-policy-payload.json"),
- guardsList.get("guard2"), false);
+ guardsMap.get("guard2"), false);
}
}
diff --git a/src/test/resources/tosca/operational-policy-no-guard-properties.json b/src/test/resources/tosca/operational-policy-no-guard-properties.json
new file mode 100644
index 0000000..30c0440
--- /dev/null
+++ b/src/test/resources/tosca/operational-policy-no-guard-properties.json
@@ -0,0 +1,35 @@
+{
+ "operational_policy": {
+ "controlLoop": {
+ "trigger_policy": "new",
+ "timeout": "0",
+ "abatement": "on",
+ "controlLoopName": "LOOP_iYTIP_v1_0_ResourceInstanceName1_tca"
+ },
+ "policies": [
+ {
+ "id": "new",
+ "recipe": "Restart",
+ "retry": "10",
+ "timeout": "10",
+ "actor": "SO",
+ "payload": "",
+ "success": "",
+ "failure": "",
+ "failure_timeout": "",
+ "failure_retries": "",
+ "failure_exception": "",
+ "failure_guard": "",
+ "target": {
+ "type": "VM",
+ "resourceId": "",
+ "modelInvariantId": "",
+ "modelVersionId": "",
+ "modelName": "",
+ "modelVersion": "",
+ "modelCustomizationId": ""
+ }
+ }
+ ]
+ }
+}
\ No newline at end of file