Fix for sonar issue

Fix for some sonars. Increase code coverage.

Issue-ID: CLAMP-211
Change-Id: If1b5169ff832fc94886b178226570dc0559fe3c2
Signed-off-by: Gabriel <adam.krysiak@nokia.com>
diff --git a/src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyAttributesConstructorTest.java b/src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyAttributesConstructorTest.java
new file mode 100644
index 0000000..c257ec3
--- /dev/null
+++ b/src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyAttributesConstructorTest.java
@@ -0,0 +1,164 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP CLAMP
+ * ================================================================================
+ * Copyright (C) 2018 Nokia Intellectual Property. All rights
+ *                             reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END============================================
+ * ===================================================================
+ *
+ */
+
+package org.onap.clamp.clds.client.req.policy;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.ImmutableMap;
+import org.assertj.core.api.Assertions;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Matchers;
+import org.mockito.Mockito;
+import org.onap.clamp.clds.config.ClampProperties;
+import org.onap.clamp.clds.model.properties.ModelProperties;
+import org.onap.clamp.clds.model.properties.PolicyChain;
+import org.onap.clamp.clds.util.ResourceFileUtil;
+import org.onap.policy.api.AttributeType;
+import org.onap.policy.controlloop.policy.ControlLoopPolicy;
+import org.onap.policy.controlloop.policy.Policy;
+import org.onap.policy.controlloop.policy.Target;
+import org.onap.policy.controlloop.policy.TargetType;
+import org.onap.policy.controlloop.policy.builder.BuilderException;
+import org.yaml.snakeyaml.Yaml;
+
+import java.io.IOException;
+import java.net.URLDecoder;
+import java.util.Map;
+
+public class OperationalPolicyAttributesConstructorTest {
+
+    private static final String CONTROL_NAME = "ClosedLoop-d4629aee-970f-11e8-86c9-02552dda865e";
+    private ModelProperties modelProperties;
+    private PolicyChain policyChain;
+
+    private OperationalPolicyYamlFormatter operationalPolicyYamlFormatter = new OperationalPolicyYamlFormatter();
+    private OperationalPolicyAttributesConstructor operationalPolicyAttributesConstructor =
+            new OperationalPolicyAttributesConstructor(operationalPolicyYamlFormatter);
+
+    @Before
+    public void setUp() throws Exception {
+        String modelProp = ResourceFileUtil
+                .getResourceAsString("example/model-properties/policy/modelBpmnProperties.json");
+        modelProperties = new ModelProperties("CLAMPDemoVFW_v1_0_3af8daec-6f10-4027-a3540",
+                CONTROL_NAME, "PUT", false, "{}", modelProp);
+        policyChain = readPolicyChainFromResources();
+    }
+
+
+    @Test
+    public void shouldFormatRequestAttributes() throws IOException, BuilderException {
+        // given
+        ClampProperties mockClampProperties = createMockClampProperties(ImmutableMap.<String, String>builder()
+                .put("op.templateName", "ClosedLoopControlName")
+                .put("op.notificationTopic", "POLICY-CL-MGT")
+                .put("op.controller", "amsterdam")
+                .put("op.recipeTopic", "APPC")
+                .build());
+
+        //when
+        Map<AttributeType, Map<String, String>> requestAttributes
+                = operationalPolicyAttributesConstructor.formatAttributes(mockClampProperties, modelProperties,
+                "789875c1-e788-432f-9a76-eac8ed889734", policyChain);
+        //then
+        Assertions.assertThat(requestAttributes).containsKeys(AttributeType.MATCHING, AttributeType.RULE);
+        Assertions.assertThat(requestAttributes.get(AttributeType.MATCHING))
+                .contains(Assertions.entry(OperationalPolicyAttributesConstructor.CONTROLLER, "amsterdam"));
+
+        Map<String, String> ruleParameters = requestAttributes.get(AttributeType.RULE);
+        Assertions.assertThat(ruleParameters).containsExactly(
+                Assertions.entry(OperationalPolicyAttributesConstructor.MAX_RETRIES, "3"),
+                Assertions.entry(OperationalPolicyAttributesConstructor.TEMPLATE_NAME, "ClosedLoopControlName"),
+                Assertions.entry(OperationalPolicyAttributesConstructor.NOTIFICATION_TOPIC, "POLICY-CL-MGT"),
+                Assertions.entry(OperationalPolicyAttributesConstructor.RECIPE_TOPIC, "APPC"),
+                Assertions.entry(OperationalPolicyAttributesConstructor.RECIPE, "healthCheck"),
+                Assertions.entry(OperationalPolicyAttributesConstructor.RESOURCE_ID,
+                        "cdb69724-57d5-4a22-b96c-4c345150fd0e"),
+                Assertions.entry(OperationalPolicyAttributesConstructor.RETRY_TIME_LIMIT, "180"),
+                Assertions.entry(OperationalPolicyAttributesConstructor.CLOSED_LOOP_CONTROL_NAME, CONTROL_NAME + "_1")
+        );
+    }
+
+    @Test
+    public void shouldFormatRequestAttributesWithProperControlLoopYaml() throws IOException, BuilderException {
+        //given
+        ClampProperties mockClampProperties = createMockClampProperties(ImmutableMap.<String, String>builder()
+                .put("op.templateName", "ClosedLoopControlName")
+                .put("op.operationTopic", "APPP-CL")
+                .put("op.notificationTopic", "POLICY-CL-MGT")
+                .put("op.controller", "amsterdam")
+                .put("op.recipeTopic", "APPC")
+                .build());
+
+        Policy expectedPolicy = new Policy("6f76ad0b-ea9d-4a92-8d7d-6a6367ce2c77", "healthCheck Policy",
+                "healthCheck Policy - the trigger (no parent) policy - created by CLDS", "APPC",
+                null, new Target(TargetType.VM, "cdb69724-57d5-4a22-b96c-4c345150fd0e"),
+                "healthCheck", 3, 180);
+
+        //when
+        Map<AttributeType, Map<String, String>> requestAttributes = operationalPolicyAttributesConstructor
+                .formatAttributes(mockClampProperties, modelProperties,
+                        "789875c1-e788-432f-9a76-eac8ed889734", policyChain);
+
+        //then
+        Assertions.assertThat(requestAttributes)
+                .containsKeys(AttributeType.MATCHING, AttributeType.RULE);
+        Assertions.assertThat(requestAttributes
+                .get(AttributeType.MATCHING))
+                .contains(Assertions.entry("controller", "amsterdam"));
+
+        Map<String, String> ruleParameters = requestAttributes.get(AttributeType.RULE);
+        Assertions.assertThat(ruleParameters).contains(
+                Assertions.entry(OperationalPolicyAttributesConstructor.OPERATION_TOPIC, "APPP-CL"),
+                Assertions.entry(OperationalPolicyAttributesConstructor.TEMPLATE_NAME, "ClosedLoopControlName"),
+                Assertions.entry(OperationalPolicyAttributesConstructor.NOTIFICATION_TOPIC, "POLICY-CL-MGT"),
+                Assertions.entry(OperationalPolicyAttributesConstructor.CLOSED_LOOP_CONTROL_NAME, CONTROL_NAME + "_1")
+        );
+
+        String controlLoopYaml = URLDecoder.decode(
+                ruleParameters.get(OperationalPolicyAttributesConstructor.CONTROL_LOOP_YAML), "UTF-8");
+        ControlLoopPolicy controlLoopPolicy = new Yaml().load(controlLoopYaml);
+
+        Assertions.assertThat(controlLoopPolicy.getControlLoop().getControlLoopName()).isEqualTo(CONTROL_NAME);
+        Assertions.assertThat(controlLoopPolicy.getPolicies())
+                .usingElementComparatorIgnoringFields("id")
+                .containsExactly(expectedPolicy);
+    }
+
+
+    private ClampProperties createMockClampProperties(ImmutableMap<String, String> propertiesMap) {
+        ClampProperties props = Mockito.mock(ClampProperties.class);
+        propertiesMap.forEach((property, value) ->
+                Mockito.when(props.getStringValue(Matchers.matches(property), Matchers.any())).thenReturn(value)
+        );
+        return props;
+    }
+
+    private PolicyChain readPolicyChainFromResources() throws IOException {
+        String policyChainText = ResourceFileUtil
+                .getResourceAsString("example/operational-policy/json-policy-chain.json");
+        JsonNode policyChainNode = new ObjectMapper().readTree(policyChainText);
+        return new PolicyChain(policyChainNode);
+    }
+}
diff --git a/src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyReqTest.java b/src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyYamlFormatterTest.java
similarity index 64%
rename from src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyReqTest.java
rename to src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyYamlFormatterTest.java
index d5e3069..046d739 100644
--- a/src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyReqTest.java
+++ b/src/test/java/org/onap/clamp/clds/client/req/policy/OperationalPolicyYamlFormatterTest.java
@@ -17,53 +17,55 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * ============LICENSE_END============================================
+ * Modifications copyright (c) 2018 Nokia
  * ===================================================================
- * 
+ *
  */
 
 package org.onap.clamp.clds.client.req.policy;
 
-import static org.assertj.core.api.Assertions.assertThat;
-
-import java.lang.reflect.InvocationTargetException;
 import java.util.Arrays;
 import java.util.List;
+
+import org.assertj.core.api.Assertions;
 import org.junit.Test;
 import org.onap.policy.controlloop.policy.PolicyResult;
 import org.onap.policy.sdc.Resource;
 import org.onap.policy.sdc.ResourceType;
 
-public class OperationalPolicyReqTest {
+public class OperationalPolicyYamlFormatterTest {
+
+    private OperationalPolicyYamlFormatter policyYamlFormatter = new OperationalPolicyYamlFormatter();
 
     @Test
     public void shouldConvertGivenStringsToResourceObjects()
-        throws NoSuchMethodException, SecurityException, IllegalAccessException,
-        IllegalArgumentException, InvocationTargetException {
+            throws SecurityException,
+            IllegalArgumentException {
 
         //given
         List<String> stringList = Arrays.asList("test1", "test2", "test3", "test4");
 
         //when
-        Resource[] resources = OperationalPolicyReq.convertToResource(stringList, ResourceType.VF);
+        Resource[] resources = policyYamlFormatter.convertToResources(stringList, ResourceType.VF);
 
         //then
-        assertThat(resources).extracting(Resource::getResourceName)
-            .containsExactly("test1", "test2", "test3", "test4");
+        Assertions.assertThat(resources).extracting(Resource::getResourceName)
+                .containsExactly("test1", "test2", "test3", "test4");
     }
 
     @Test
     public void shouldConvertGivenStringsToPolicyResults()
-        throws NoSuchMethodException, SecurityException, IllegalAccessException,
-        IllegalArgumentException, InvocationTargetException {
+            throws SecurityException,
+            IllegalArgumentException {
         //given
         List<String> stringList = Arrays.asList("FAILURE", "SUCCESS", "FAILURE_GUARD", "FAILURE_TIMEOUT");
 
         //when
-        PolicyResult[] policyResults = OperationalPolicyReq.convertToPolicyResult(stringList);
+        PolicyResult[] policyResults = policyYamlFormatter.convertToPolicyResults(stringList);
 
         //then
-        assertThat(policyResults)
-            .containsExactly(PolicyResult.FAILURE, PolicyResult.SUCCESS,
-                PolicyResult.FAILURE_GUARD, PolicyResult.FAILURE_TIMEOUT);
+        Assertions.assertThat(policyResults)
+                .containsExactly(PolicyResult.FAILURE, PolicyResult.SUCCESS,
+                        PolicyResult.FAILURE_GUARD, PolicyResult.FAILURE_TIMEOUT);
     }
-}
+}
\ No newline at end of file
diff --git a/src/test/java/org/onap/clamp/clds/it/OperationPolicyReqItCase.java b/src/test/java/org/onap/clamp/clds/it/OperationPolicyReqItCase.java
index 69dad53..ad58ea1 100644
--- a/src/test/java/org/onap/clamp/clds/it/OperationPolicyReqItCase.java
+++ b/src/test/java/org/onap/clamp/clds/it/OperationPolicyReqItCase.java
@@ -17,6 +17,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * ============LICENSE_END============================================
+ * Modifications copyright (c) 2018 Nokia
  * ===================================================================
  * 
  */
@@ -35,7 +36,7 @@
 
 import org.junit.Test;
 import org.junit.runner.RunWith;
-import org.onap.clamp.clds.client.req.policy.OperationalPolicyReq;
+import org.onap.clamp.clds.client.req.policy.OperationalPolicyAttributesConstructor;
 import org.onap.clamp.clds.config.ClampProperties;
 import org.onap.clamp.clds.model.CldsEvent;
 import org.onap.clamp.clds.model.properties.ModelProperties;
@@ -55,6 +56,9 @@
     @Autowired
     private ClampProperties refProp;
 
+    @Autowired
+    private OperationalPolicyAttributesConstructor operationalPolicyAttributesConstructor;
+
     @Test
     public void formatAttributesTest() throws IOException, BuilderException {
         String modelBpmnProp = ResourceFileUtil
@@ -65,7 +69,7 @@
         List<Map<AttributeType, Map<String, String>>> attributes = new ArrayList<>();
         if (modelProperties.getType(Policy.class).isFound()) {
             for (PolicyChain policyChain : modelProperties.getType(Policy.class).getPolicyChains()) {
-                attributes.add(OperationalPolicyReq.formatAttributes(refProp, modelProperties,
+                attributes.add(operationalPolicyAttributesConstructor.formatAttributes(refProp, modelProperties,
                         modelProperties.getType(Policy.class).getId(), policyChain));
             }
         }
diff --git a/src/test/java/org/onap/clamp/clds/it/PolicyClientItCase.java b/src/test/java/org/onap/clamp/clds/it/PolicyClientItCase.java
index c6fc09d..2400d4a 100644
--- a/src/test/java/org/onap/clamp/clds/it/PolicyClientItCase.java
+++ b/src/test/java/org/onap/clamp/clds/it/PolicyClientItCase.java
@@ -17,6 +17,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * ============LICENSE_END============================================
+ * Modifications copyright (c) 2018 Nokia
  * ===================================================================
  * 
  */
@@ -35,7 +36,7 @@
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
-import org.onap.clamp.clds.client.req.policy.OperationalPolicyReq;
+import org.onap.clamp.clds.client.req.policy.OperationalPolicyAttributesConstructor;
 import org.onap.clamp.clds.client.req.policy.PolicyClient;
 import org.onap.clamp.clds.client.req.tca.TcaRequestFormatter;
 import org.onap.clamp.clds.config.ClampProperties;
@@ -64,7 +65,10 @@
     @Autowired
     private ClampProperties refProp;
     @Autowired
-    protected PolicyClient policyClient;
+    private PolicyClient policyClient;
+    @Autowired
+    private OperationalPolicyAttributesConstructor operationalPolicyAttributesConstructor;
+
     String modelProp;
     String modelBpmnProp;
     String modelName;
@@ -87,8 +91,8 @@
         if (policy.isFound()) {
             for (PolicyChain policyChain : policy.getPolicyChains()) {
                 String operationalPolicyRequestUuid = UUID.randomUUID().toString();
-                Map<AttributeType, Map<String, String>> attributes = OperationalPolicyReq.formatAttributes(refProp,
-                        prop, policy.getId(), policyChain);
+                Map<AttributeType, Map<String, String>> attributes = operationalPolicyAttributesConstructor
+                        .formatAttributes(refProp, prop, policy.getId(), policyChain);
                 policyClient.sendBrmsPolicy(attributes, prop, operationalPolicyRequestUuid);
             }
         }
diff --git a/src/test/resources/example/operational-policy/json-policy-chain.json b/src/test/resources/example/operational-policy/json-policy-chain.json
new file mode 100644
index 0000000..037b4d6
--- /dev/null
+++ b/src/test/resources/example/operational-policy/json-policy-chain.json
@@ -0,0 +1,63 @@
+[
+  {
+    "name": "pname",
+    "value": "healthCheck Policy"
+  },
+  {
+    "name": "pid",
+    "value": "1"
+  },
+  {
+    "name": "timeout",
+    "value": "180"
+  },
+  {
+    "policyConfigurations": [
+      [
+        {
+          "name": "recipe",
+          "value": [
+            "healthCheck"
+          ]
+        },
+        {
+          "name": "maxRetries",
+          "value": [
+            "3"
+          ]
+        },
+        {
+          "name": "retryTimeLimit",
+          "value": [
+            "180"
+          ]
+        },
+        {
+          "name": "_id",
+          "value": [
+            "789875c1-e788-432f-9a76-eac8ed889734"
+          ]
+        },
+        {
+          "name": "parentPolicy",
+          "value": [
+            ""
+          ]
+        },
+        {
+          "name": "actor",
+          "value": [
+            "APPC"
+          ]
+        },
+        {
+          "name": "targetResourceId",
+          "value": [
+            "cdb69724-57d5-4a22-b96c-4c345150fd0e"
+          ]
+        }
+
+      ]
+    ]
+  }
+]
\ No newline at end of file