Unit/SONAR/Checkstyle in ONAP-REST

Util package of ONAP-REST, with JUnit added and
SONAR/Checkstyle issues addressed. Coverage increased from 51% to 73% in
large PolicyValidation class.

Issue-ID: POLICY-2131
Change-Id: Iffa90e2dfa155d3e9cac114032fb9ca0df2cbab0
Signed-off-by: liamfallon <liam.fallon@est.tech>
diff --git a/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java b/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java
index e8c0b55..70be832 100644
--- a/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java
+++ b/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java
@@ -880,7 +880,7 @@
         }
     }
 
-    protected String emailValidation(String email, String response) {
+    private String emailValidation(String email, String response) {
         String res = response;
         if (email != null) {
             String validateEmail = PolicyUtils.validateEmailAddress(email.replace("\"", ""));
diff --git a/ONAP-REST/src/test/java/org/onap/policy/rest/util/PolicyValidationTest.java b/ONAP-REST/src/test/java/org/onap/policy/rest/util/PolicyValidationTest.java
index ef825a4..d019c63 100644
--- a/ONAP-REST/src/test/java/org/onap/policy/rest/util/PolicyValidationTest.java
+++ b/ONAP-REST/src/test/java/org/onap/policy/rest/util/PolicyValidationTest.java
@@ -26,6 +26,8 @@
 import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.assertNull;
 
+import com.fasterxml.jackson.databind.ObjectMapper;
+
 import java.io.IOException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
@@ -38,13 +40,22 @@
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
 import org.onap.policy.api.PolicyConfigType;
 import org.onap.policy.api.PolicyParameters;
 import org.onap.policy.common.utils.resources.TextFileUtils;
 import org.onap.policy.rest.adapter.ClosedLoopFaultTrapDatas;
 import org.onap.policy.rest.adapter.PolicyRestAdapter;
+import org.onap.policy.rest.dao.CommonClassDao;
+import org.onap.policy.rest.jpa.MicroServiceModels;
 
+@RunWith(MockitoJUnitRunner.class)
 public class PolicyValidationTest {
+    @Mock
+    private CommonClassDao commonClassDao;
 
     @Before
     public void setUp() throws Exception {
@@ -106,13 +117,6 @@
     }
 
     @Test
-    public final void testEmailValidation() {
-        PolicyValidation validation = new PolicyValidation();
-        String result = validation.emailValidation("testemail@test.com", "SUCCESS");
-        assertEquals("success", result);
-    }
-
-    @Test
     public void testPolicyHeadingValidation() throws IOException {
         PolicyValidation validation = new PolicyValidation();
         PolicyRestAdapter policyData = new PolicyRestAdapter();
@@ -1085,4 +1089,198 @@
         responseString = validation.validatePolicy(policyData).toString();
         assertEquals("success@#", responseString);
     }
+
+    @Test
+    public void testPolicyConfigMicroServiceValidation() throws IOException {
+        PolicyValidation validation = new PolicyValidation(commonClassDao);
+        PolicyRestAdapter policyData = new PolicyRestAdapter();
+        policyData.setPolicyName("ALegalPolicyName");
+        policyData.setPolicyDescription("A Valid Description");
+
+        String responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success", responseString);
+
+        policyData.setApiflag("NOAPI");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success", responseString);
+
+        // Invalid values tested in config base test
+        policyData.setOnapName("AValidOnapName");
+        policyData.setRiskType("AValidRiskType");
+        policyData.setRiskLevel("AValidRiskLevel");
+        policyData.setGuard("AValidGuard");
+        assertEquals("success", responseString);
+
+        policyData.setPolicyType("Config");
+        policyData.setConfigPolicyType("Micro Service");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("Micro Service Model is required");
+
+        policyData.setServiceType("");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("Micro Service Model is required");
+
+        policyData.setServiceType("ServiceType");
+        assertNull(validation.validatePolicy(policyData));
+
+        policyData.setPolicyJSON("");
+        assertNull(validation.validatePolicy(policyData));
+
+        String msJsonContentString0 = TextFileUtils
+                        .getTextFileAsString("src/test/resources/policies/MicroServicePolicyContent0.json");
+        policyData.setPolicyJSON(new ObjectMapper().readTree(msJsonContentString0));
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("Micro Service Version is required");
+
+        String msJsonContentString1 = TextFileUtils
+                        .getTextFileAsString("src/test/resources/policies/MicroServicePolicyContent1.json");
+        policyData.setPolicyJSON(new ObjectMapper().readTree(msJsonContentString1));
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("Micro Service Version is required");
+
+        policyData.setServiceType("ServiceType-vServiceVersion");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("The model name, ServiceType of version, ServiceVersion was not found");
+
+        policyData.setServiceType("ServiceType");
+        policyData.setRuleProvider("MicroService_Model");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).doesNotContain("Priority is required");
+
+        policyData.setVersion("ServiceVersion");
+        Mockito.when(commonClassDao.getDataById(MicroServiceModels.class, "modelName:version",
+                        "ServiceType:ServiceVersion")).thenReturn(null);
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("The model name, ServiceType of version, ServiceVersion was not found");
+
+        List<Object> msModelsList = new ArrayList<>();
+        MicroServiceModels msModels = new MicroServiceModels();
+        msModelsList.add(msModels);
+        Mockito.when(commonClassDao.getDataById(MicroServiceModels.class, "modelName:version",
+                        "ServiceType:ServiceVersion")).thenReturn(msModelsList);
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success@#", responseString);
+
+        msModels.setAnnotation("SomeAnntation");
+        assertNull(validation.validatePolicy(policyData));
+
+        msModels.setAnnotation("annotation0Key=annotation0Value");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success@#", responseString);
+
+        msModels.setAnnotation("annotation0Key=range::10-100");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("annotation0Key is required for the MicroService model");
+
+        msModels.setAnnotation("police-instance-name=range::10-100");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("Model Range:police-instance-name must be between 10 - 100");
+
+        msModels.setAnnotation("police-instance-range=range::12-100");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("Model Range:police-instance-range must be between 12 - 100");
+
+        msModels.setAnnotation("police-instance-range=range::0-10");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("Model Range:police-instance-range must be between 0 - 10");
+
+        msModels.setAnnotation("police-instance-range=range::10-100");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success@#", responseString);
+
+        msModels.setAnnotation(null);
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success@#", responseString);
+
+        policyData.setRuleProvider("Not_MicroService_Model");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("<b>Micro Service Model</b>:<i> location is required for this model");
+
+        policyData.setLocation("AValidLocation");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("<b>Micro Service Model</b>:<i> configName is required for this model");
+
+        policyData.setConfigName("AValidConfigname");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("<b>Micro Service Model</b>:<i> uuid is required for this model");
+
+        policyData.setUuid("AValidUUID");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("<b>Micro Service Model</b>:<i> policyScope is required for this model");
+
+        policyData.setPolicyScope("AValidPolicyScope");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("<b>Priority</b>:<i> Priority is required");
+
+        policyData.setPriority("AValidPriority");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success@#", responseString);
+
+        policyData.setApiflag("API");
+        assertNull(validation.validatePolicy(policyData));
+
+        msModels.setSubAttributes("{}");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success@#", responseString);
+
+        msModels.setSubAttributes("{\"subAttrName0\": \"subAttrValue0\"}");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success@#", responseString);
+
+        msModels.setSubAttributes("{\"subAttrName0\": 10}");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success@#", responseString);
+
+        msModels.setSubAttributes("{\"subAttrName0\": {}}");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success@#", responseString);
+
+        msModels.setSubAttributes("{\"subAttrName0\": []}");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertEquals("success@#", responseString);
+
+        msModels.setSubAttributes("{\"subAttrName0\": \"subAttrValue0-required-true\"}");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("<b>Micro Service Model</b>:<i> subAttrName0 is required");
+
+        msModels.setSubAttributes("{\"subAttrName0\": \":subAttrValue0-required-true\"}");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("<b>Micro Service Model</b>:<i> subAttrName0 is required");
+
+        msModels.setSubAttributes("{\"subAttrName0\": \"UnknownType:subAttrValue0-required-true\"}");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("<b>Micro Service Model</b>:<i> subAttrName0 is required");
+
+        msModels.setSubAttributes("{\"subAttrName0\": \"string:subAttrValue0-required-true\"}");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("<b>Micro Service Model</b>:<i> subAttrName0 is required");
+
+        msModels.setRefAttributes("refAttrName0=");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("<b>Micro Service Model</b>:<i> subAttrName0 is required");
+
+        msModels.setRefAttributes("refAttrName0=refAttrValue0");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("<b>Micro Service Model</b>:<i> subAttrName0 is required");
+
+        msModels.setRefAttributes("refAttrName0=refAttrValue0-required-true");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("<b>Micro Service Model</b>:<i> subAttrName0 is required");
+
+        msModels.setAttributes("");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("<b>Micro Service Model</b>:<i> subAttrName0 is required");
+
+        msModels.setAttributes("modelAttrName0=modelAttrValue0");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("<b>Micro Service Model</b>:<i> subAttrName0 is required");
+
+        msModels.setRefAttributes("refAttrName0=refAttrValue0-required-true,");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).contains("refAttrName0 is required");
+
+        msModels.setRefAttributes("police-instance-range=refAttrValue0-required-true,");
+        responseString = validation.validatePolicy(policyData).toString();
+        assertThat(responseString).doesNotContain("police-instance-range is required");
+    }
 }
diff --git a/ONAP-REST/src/test/resources/policies/MicroServicePolicyContent0.json b/ONAP-REST/src/test/resources/policies/MicroServicePolicyContent0.json
new file mode 100644
index 0000000..f06d2f2
--- /dev/null
+++ b/ONAP-REST/src/test/resources/policies/MicroServicePolicyContent0.json
@@ -0,0 +1,23 @@
+{
+    "police-instance-name": "content0",
+    "naming-models": [
+        {
+            "naming-properties": [
+                {
+                    "property-value": "test",
+                    "source-endpoint": "test",
+                    "property-name": "testPropertyname",
+                    "increment-sequence": {
+                        "scope": "VNF",
+                        "start-value": "1",
+                        "length": "3",
+                        "increment": "2"
+                    },
+                    "source-system": "TOSCA"
+                }
+            ],
+            "naming-type": "testNamingType",
+            "naming-recipe": "testNamingRecipe"
+        }
+    ]
+}
diff --git a/ONAP-REST/src/test/resources/policies/MicroServicePolicyContent1.json b/ONAP-REST/src/test/resources/policies/MicroServicePolicyContent1.json
new file mode 100644
index 0000000..dac78c0
--- /dev/null
+++ b/ONAP-REST/src/test/resources/policies/MicroServicePolicyContent1.json
@@ -0,0 +1,27 @@
+{
+    "police-instance-name": "content1",
+    "police-instance-range": "11",
+    "naming-models": [
+        {
+            "naming-properties": [
+                {
+                    "property-value": "test",
+                    "source-endpoint": "test",
+                    "property-name": "testPropertyname",
+                    "increment-sequence": {
+                        "scope": "VNF",
+                        "start-value": "1",
+                        "length": "3",
+                        "increment": "2"
+                    },
+                    "source-system": "TOSCA"
+                }
+            ],
+            "naming-type": "testNamingType",
+            "naming-recipe": "testNamingRecipe"
+        }
+    ],
+    "some-container": {
+        "container-name": "container-value"
+    }
+}