Merge "Change getCanonicalName to getName in models"
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / legacy / provider / LegacyProvider4LegacyOperationalTest.java
index 7ab5c58..4dd998e 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2019 AT&T 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.
@@ -49,6 +50,10 @@ import org.yaml.snakeyaml.Yaml;
  * @author Liam Fallon (liam.fallon@est.tech)
  */
 public class LegacyProvider4LegacyOperationalTest {
+    private static final String POLICY_ID_IS_NULL = "policyId is marked @NonNull but is null";
+    private static final String VCPE_OUTPUT_JSON = "policies/vCPE.policy.operational.output.json";
+    private static final String VCPE_INPUT_JSON = "policies/vCPE.policy.operational.input.json";
+    private static final String DAO_IS_NULL = "dao is marked @NonNull but is null";
     private PfDao pfDao;
     private StandardCoder standardCoder;
 
@@ -60,7 +65,7 @@ public class LegacyProvider4LegacyOperationalTest {
     @Before
     public void setupDao() throws Exception {
         final DaoParameters daoParameters = new DaoParameters();
-        daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
+        daoParameters.setPluginClass(DefaultPfDao.class.getName());
 
         daoParameters.setPersistenceUnit("ToscaConceptTest");
 
@@ -72,7 +77,7 @@ public class LegacyProvider4LegacyOperationalTest {
         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_DRIVER, "org.h2.Driver");
         jdbcProperties.setProperty(PersistenceUnitProperties.JDBC_URL, "jdbc:h2:mem:testdb");
 
-        daoParameters.setJdbcProperties(jdbcProperties );
+        daoParameters.setJdbcProperties(jdbcProperties);
 
         pfDao = new PfDaoFactory().createPfDao(daoParameters);
         pfDao.init(daoParameters);
@@ -87,32 +92,32 @@ public class LegacyProvider4LegacyOperationalTest {
     }
 
     @After
-    public void teardown() throws Exception {
+    public void teardown() {
         pfDao.close();
     }
 
     @Test
     public void testPoliciesGet() throws Exception {
         assertThatThrownBy(() -> {
-            new LegacyProvider().getOperationalPolicy(null, null);
-        }).hasMessage("dao is marked @NonNull but is null");
+            new LegacyProvider().getOperationalPolicy(null, null, null);
+        }).hasMessage(DAO_IS_NULL);
 
         assertThatThrownBy(() -> {
-            new LegacyProvider().getOperationalPolicy(null, "");
-        }).hasMessage("dao is marked @NonNull but is null");
+            new LegacyProvider().getOperationalPolicy(null, "", null);
+        }).hasMessage(DAO_IS_NULL);
 
         assertThatThrownBy(() -> {
-            new LegacyProvider().getOperationalPolicy(pfDao, null);
-        }).hasMessage("policyId is marked @NonNull but is null");
+            new LegacyProvider().getOperationalPolicy(pfDao, null, null);
+        }).hasMessage(POLICY_ID_IS_NULL);
 
         assertThatThrownBy(() -> {
-            new LegacyProvider().getOperationalPolicy(pfDao, "I Dont Exist");
-        }).hasMessage("no policy found for policy ID: I Dont Exist");
+            new LegacyProvider().getOperationalPolicy(pfDao, "I Dont Exist", null);
+        }).hasMessage("no policy found for policy: I Dont Exist:null");
 
         createPolicyTypes();
 
         LegacyOperationalPolicy originalLop =
-                standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
+                standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON),
                         LegacyOperationalPolicy.class);
 
         assertNotNull(originalLop);
@@ -121,17 +126,19 @@ public class LegacyProvider4LegacyOperationalTest {
 
         assertEquals(originalLop, createdLop);
 
-        LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
+        LegacyOperationalPolicy gotLop =
+                new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
 
         assertEquals(gotLop, originalLop);
 
-        String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
+        String expectedJsonOutput = ResourceUtils.getResourceAsString(VCPE_OUTPUT_JSON);
         String actualJsonOutput = standardCoder.encode(gotLop);
 
         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
 
         LegacyOperationalPolicy createdLopV2 = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
-        LegacyOperationalPolicy gotLopV2 = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
+        LegacyOperationalPolicy gotLopV2 =
+                new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
         assertEquals(gotLopV2, createdLopV2);
     }
 
@@ -139,11 +146,11 @@ public class LegacyProvider4LegacyOperationalTest {
     public void testPolicyCreate() throws Exception {
         assertThatThrownBy(() -> {
             new LegacyProvider().createOperationalPolicy(null, null);
-        }).hasMessage("dao is marked @NonNull but is null");
+        }).hasMessage(DAO_IS_NULL);
 
         assertThatThrownBy(() -> {
             new LegacyProvider().createOperationalPolicy(null, new LegacyOperationalPolicy());
-        }).hasMessage("dao is marked @NonNull but is null");
+        }).hasMessage(DAO_IS_NULL);
 
         assertThatThrownBy(() -> {
             new LegacyProvider().createOperationalPolicy(pfDao, null);
@@ -152,7 +159,7 @@ public class LegacyProvider4LegacyOperationalTest {
         createPolicyTypes();
 
         LegacyOperationalPolicy originalLop =
-                standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
+                standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON),
                         LegacyOperationalPolicy.class);
 
         assertNotNull(originalLop);
@@ -161,11 +168,12 @@ public class LegacyProvider4LegacyOperationalTest {
 
         assertEquals(originalLop, createdLop);
 
-        LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
+        LegacyOperationalPolicy gotLop =
+                new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
 
         assertEquals(gotLop, originalLop);
 
-        String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
+        String expectedJsonOutput = ResourceUtils.getResourceAsString(VCPE_OUTPUT_JSON);
         String actualJsonOutput = standardCoder.encode(gotLop);
 
         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
@@ -175,11 +183,11 @@ public class LegacyProvider4LegacyOperationalTest {
     public void testPolicyUpdate() throws Exception {
         assertThatThrownBy(() -> {
             new LegacyProvider().updateOperationalPolicy(null, null);
-        }).hasMessage("dao is marked @NonNull but is null");
+        }).hasMessage(DAO_IS_NULL);
 
         assertThatThrownBy(() -> {
             new LegacyProvider().updateOperationalPolicy(null, new LegacyOperationalPolicy());
-        }).hasMessage("dao is marked @NonNull but is null");
+        }).hasMessage(DAO_IS_NULL);
 
         assertThatThrownBy(() -> {
             new LegacyProvider().updateOperationalPolicy(pfDao, null);
@@ -187,12 +195,12 @@ public class LegacyProvider4LegacyOperationalTest {
 
         assertThatThrownBy(() -> {
             new LegacyProvider().updateOperationalPolicy(pfDao, new LegacyOperationalPolicy());
-        }).hasMessage("no policy found for policy ID: null");
+        }).hasMessage("name is marked @NonNull but is null");
 
         createPolicyTypes();
 
         LegacyOperationalPolicy originalLop =
-                standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
+                standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON),
                         LegacyOperationalPolicy.class);
 
         assertNotNull(originalLop);
@@ -200,7 +208,8 @@ public class LegacyProvider4LegacyOperationalTest {
         LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
         assertEquals(originalLop, createdLop);
 
-        LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
+        LegacyOperationalPolicy gotLop =
+                new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
         assertEquals(gotLop, originalLop);
 
         originalLop.setContent("Some New Content");
@@ -208,7 +217,7 @@ public class LegacyProvider4LegacyOperationalTest {
         assertEquals(originalLop, updatedLop);
 
         LegacyOperationalPolicy gotUpdatedLop =
-                new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
+                new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
         assertEquals(gotUpdatedLop, originalLop);
         assertEquals("Some New Content", gotUpdatedLop.getContent());
     }
@@ -216,26 +225,43 @@ public class LegacyProvider4LegacyOperationalTest {
     @Test
     public void testPoliciesDelete() throws Exception {
         assertThatThrownBy(() -> {
-            new LegacyProvider().deleteOperationalPolicy(null, null);
-        }).hasMessage("dao is marked @NonNull but is null");
+            new LegacyProvider().deleteOperationalPolicy(null, null, null);
+        }).hasMessage(DAO_IS_NULL);
 
         assertThatThrownBy(() -> {
-            new LegacyProvider().deleteOperationalPolicy(null, "");
+            new LegacyProvider().deleteOperationalPolicy(null, null, "");
 
-        }).hasMessage("dao is marked @NonNull but is null");
+        }).hasMessage(DAO_IS_NULL);
 
         assertThatThrownBy(() -> {
-            new LegacyProvider().deleteOperationalPolicy(pfDao, null);
-        }).hasMessage("policyId is marked @NonNull but is null");
+            new LegacyProvider().deleteOperationalPolicy(null, "", null);
+        }).hasMessage(DAO_IS_NULL);
 
         assertThatThrownBy(() -> {
-            new LegacyProvider().deleteOperationalPolicy(pfDao, "I Dont Exist");
-        }).hasMessage("no policy found for policy ID: I Dont Exist");
+            new LegacyProvider().deleteOperationalPolicy(null, "", "");
+
+        }).hasMessage(DAO_IS_NULL);
+
+        assertThatThrownBy(() -> {
+            new LegacyProvider().deleteOperationalPolicy(pfDao, null, null);
+        }).hasMessage(POLICY_ID_IS_NULL);
+
+        assertThatThrownBy(() -> {
+            new LegacyProvider().deleteOperationalPolicy(pfDao, null, "");
+        }).hasMessage(POLICY_ID_IS_NULL);
+
+        assertThatThrownBy(() -> {
+            new LegacyProvider().deleteOperationalPolicy(pfDao, "", null);
+        }).hasMessage("policyVersion is marked @NonNull but is null");
+
+        assertThatThrownBy(() -> {
+            new LegacyProvider().deleteOperationalPolicy(pfDao, "IDontExist", "0");
+        }).hasMessage("no policy found for policy: IDontExist:0");
 
         createPolicyTypes();
 
         LegacyOperationalPolicy originalLop =
-                standardCoder.decode(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
+                standardCoder.decode(ResourceUtils.getResourceAsString(VCPE_INPUT_JSON),
                         LegacyOperationalPolicy.class);
 
         assertNotNull(originalLop);
@@ -243,22 +269,27 @@ public class LegacyProvider4LegacyOperationalTest {
         LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
         assertEquals(originalLop, createdLop);
 
-        LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
+        LegacyOperationalPolicy gotLop =
+                new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
 
         assertEquals(gotLop, originalLop);
 
-        String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
+        String expectedJsonOutput = ResourceUtils.getResourceAsString(VCPE_OUTPUT_JSON);
         String actualJsonOutput = standardCoder.encode(gotLop);
 
         assertEquals(expectedJsonOutput.replaceAll("\\s+", ""), actualJsonOutput.replaceAll("\\s+", ""));
 
+        assertThatThrownBy(() -> {
+            new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
+        }).hasMessage("policyVersion is marked @NonNull but is null");
+
         LegacyOperationalPolicy deletedLop =
-                new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId());
+                new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId(), "1");
         assertEquals(originalLop, deletedLop);
 
         assertThatThrownBy(() -> {
-            new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
-        }).hasMessage("no policy found for policy ID: operational.restart");
+            new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
+        }).hasMessage("no policy found for policy: operational.restart:null");
 
         LegacyOperationalPolicy otherLop = new LegacyOperationalPolicy();
         otherLop.setPolicyId("another-policy");
@@ -269,13 +300,13 @@ public class LegacyProvider4LegacyOperationalTest {
         assertEquals(otherLop, createdOtherLop);
 
         assertThatThrownBy(() -> {
-            new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
-        }).hasMessage("no policy found for policy ID: operational.restart");
+            new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId(), null);
+        }).hasMessage("no policy found for policy: operational.restart:null");
     }
 
     private void createPolicyTypes() throws CoderException, PfModelException {
-        Object yamlObject = new Yaml().load(
-                ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.Operational.yaml"));
+        Object yamlObject = new Yaml()
+                .load(ResourceUtils.getResourceAsString("policytypes/onap.policies.controlloop.Operational.yaml"));
         String yamlAsJsonString = new StandardCoder().encode(yamlObject);
 
         ToscaServiceTemplate toscaServiceTemplatePolicyType =