Add provider for Tosca Policies 50/83150/1
authorliamfallon <liam.fallon@est.tech>
Mon, 25 Mar 2019 08:09:14 +0000 (08:09 +0000)
committerliamfallon <liam.fallon@est.tech>
Mon, 25 Mar 2019 08:09:14 +0000 (08:09 +0000)
Provider working from JAVA API call through to database and
back for TOSCA policies with full unit test.

Issue-ID: POLICY-1195
Change-Id: I82cf3b513b4921dcb2e6726856aa4fbeb7d0d816
Signed-off-by: liamfallon <liam.fallon@est.tech>
13 files changed:
models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java
models-provider/src/test/resources/META-INF/persistence.xml
models-tosca/pom.xml
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaDataTypes.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicies.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicyTypes.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaServiceTemplate.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaTopologyTemplate.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java
models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateJsonAdapter.java
models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/ToscaTopologyTemplateTest.java
models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java [new file with mode: 0644]
models-tosca/src/test/resources/META-INF/persistence.xml [new file with mode: 0644]

index 3b30f98..498000a 100644 (file)
@@ -37,6 +37,9 @@ import org.onap.policy.models.provider.PolicyModelsProviderParameters;
 import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy;
 import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
 import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Test the database models provider implementation.
@@ -44,6 +47,8 @@ import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
  * @author Liam Fallon (liam.fallon@est.tech)
  */
 public class DatabasePolicyModelsProviderTest {
+    private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class);
+
     PolicyModelsProviderParameters parameters;
 
     /**
@@ -266,15 +271,51 @@ public class DatabasePolicyModelsProviderTest {
                 new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters)) {
             databaseProvider.init();
 
-            assertNull(databaseProvider.getPolicyTypes(new PfConceptKey()));
-            assertNull(databaseProvider.createPolicyTypes(new ToscaServiceTemplate()));
-            assertNull(databaseProvider.updatePolicyTypes(new ToscaServiceTemplate()));
-            assertNull(databaseProvider.deletePolicyTypes(new PfConceptKey()));
+            try {
+                databaseProvider.getPolicyTypes(new PfConceptKey());
+                fail("test should throw an exception");
+            } catch (Exception npe) {
+                assertEquals("policy type not found: NULL:0.0.0", npe.getMessage());
+            }
+            try {
+                databaseProvider.createPolicyTypes(new ToscaServiceTemplate());
+            } catch (Exception npe) {
+                assertEquals("no policy types specified on service template", npe.getMessage());
+            }
+            try {
+                databaseProvider.updatePolicyTypes(new ToscaServiceTemplate());
+            } catch (Exception npe) {
+                assertEquals("no policy types specified on service template", npe.getMessage());
+            }
+            try {
+                databaseProvider.deletePolicyTypes(new PfConceptKey());
+                fail("test should throw an exception");
+            } catch (Exception npe) {
+                assertEquals("policy type not found: NULL:0.0.0", npe.getMessage());
+            }
 
-            assertNull(databaseProvider.getPolicies(new PfConceptKey()));
-            assertNull(databaseProvider.createPolicies(new ToscaServiceTemplate()));
-            assertNull(databaseProvider.updatePolicies(new ToscaServiceTemplate()));
-            assertNull(databaseProvider.deletePolicies(new PfConceptKey()));
+            try {
+                databaseProvider.getPolicies(new PfConceptKey());
+                fail("test should throw an exception");
+            } catch (Exception npe) {
+                assertEquals("policy not found: NULL:0.0.0", npe.getMessage());
+            }
+            try {
+                databaseProvider.createPolicies(new ToscaServiceTemplate());
+            } catch (Exception npe) {
+                assertEquals("topology template not specified on service template", npe.getMessage());
+            }
+            try {
+                databaseProvider.updatePolicies(new ToscaServiceTemplate());
+            } catch (Exception npe) {
+                assertEquals("topology template not specified on service template", npe.getMessage());
+            }
+            try {
+                databaseProvider.deletePolicies(new PfConceptKey());
+                fail("test should throw an exception");
+            } catch (Exception npe) {
+                assertEquals("policy not found: NULL:0.0.0", npe.getMessage());
+            }
 
             assertNull(databaseProvider.getOperationalPolicy("policy_id"));
             assertNull(databaseProvider.createOperationalPolicy(new LegacyOperationalPolicy()));
@@ -292,6 +333,7 @@ public class DatabasePolicyModelsProviderTest {
             assertNotNull(databaseProvider.deletePdpGroups("filter"));
 
         } catch (Exception exc) {
+            LOGGER.warn("test should not throw an exception", exc);
             fail("test should not throw an exception");
         }
     }
index 4915053..6834090 100644 (file)
@@ -26,6 +26,7 @@
         <class>org.onap.policy.models.dao.converters.CDataConditioner</class>
         <class>org.onap.policy.models.dao.converters.Uuid2String</class>
         <class>org.onap.policy.models.base.PfConceptKey</class>
+        <class>org.onap.policy.models.tosca.simple.concepts.ToscaPolicyType</class>
         <class>org.onap.policy.models.tosca.simple.concepts.ToscaPolicy</class>
 
         <properties>
index 5658a1c..c8fa252 100644 (file)
             <artifactId>gson</artifactId>
             <version>${policy.common.version}</version>
         </dependency>
+
+        <dependency>
+            <groupId>com.h2database</groupId>
+            <artifactId>h2</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.mariadb.jdbc</groupId>
+            <artifactId>mariadb-java-client</artifactId>
+            <scope>test</scope>
+        </dependency>
+
     </dependencies>
 </project>
index cbf0e38..eae98a1 100644 (file)
@@ -47,12 +47,15 @@ import org.onap.policy.models.base.PfConceptKey;
 public class ToscaDataTypes extends PfConceptContainer<ToscaDataType> {
     private static final long serialVersionUID = 2941102271022190348L;
 
+    public static final String DEFAULT_NAME = "ToscaDataTypesSimple";
+    public static final String DEFAULT_VERSION = "1.0.0";
+
     /**
      * The Default Constructor creates a {@link ToscaDataTypes} object with a null artifact key
      * and creates an empty concept map.
      */
     public ToscaDataTypes() {
-        super(new PfConceptKey());
+        super(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION));
     }
 
     /**
index 9dd12ec..ee9c2ea 100644 (file)
@@ -47,12 +47,15 @@ import org.onap.policy.models.base.PfConceptKey;
 public class ToscaPolicies extends PfConceptContainer<ToscaPolicy> {
     private static final long serialVersionUID = -7526648702327776101L;
 
+    public static final String DEFAULT_NAME = "ToscaPoliciessSimple";
+    public static final String DEFAULT_VERSION = "1.0.0";
+
     /**
      * The Default Constructor creates a {@link ToscaPolicies} object with a null artifact key and
      * creates an empty concept map.
      */
     public ToscaPolicies() {
-        super(new PfConceptKey());
+        super(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION));
     }
 
     /**
index 23159cc..2772913 100644 (file)
@@ -47,12 +47,15 @@ import org.onap.policy.models.base.PfConceptKey;
 public class ToscaPolicyTypes extends PfConceptContainer<ToscaPolicyType> {
     private static final long serialVersionUID = -4157979965271220098L;
 
+    public static final String DEFAULT_NAME = "ToscaPolicyTypesSimple";
+    public static final String DEFAULT_VERSION = "1.0.0";
+
     /**
      * The Default Constructor creates a {@link ToscaPolicyTypes} object with a null artifact key
      * and creates an empty concept map.
      */
     public ToscaPolicyTypes() {
-        super(new PfConceptKey());
+        super(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION));
     }
 
     /**
index fd8b134..c3bb916 100644 (file)
@@ -60,6 +60,9 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult;
 public class ToscaServiceTemplate extends ToscaEntityType {
     private static final long serialVersionUID = 8084846046148349401L;
 
+    public static final String DEFAULT_NAME = "ToscaServiceTemplateSimple";
+    public static final String DEFAULT_VERSION = "1.0.0";
+
     @Column
     @SerializedName("tosca_definitions_version")
     private String toscaDefinitionsVersion;
@@ -80,7 +83,7 @@ public class ToscaServiceTemplate extends ToscaEntityType {
      * The Default Constructor creates a {@link ToscaServiceTemplate} object with a null key.
      */
     public ToscaServiceTemplate() {
-        this(new PfConceptKey());
+        this(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION));
     }
 
     /**
index 9d156c3..35381ec 100644 (file)
@@ -45,8 +45,8 @@ import org.onap.policy.models.base.PfValidationResult;
 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
 
 /**
- * This class holds a TOSCA topology template. Note: Only the policy specific parts of the TOSCA
- * topology template are implemented.
+ * This class holds a TOSCA topology template. Note: Only the policy specific parts of the TOSCA topology template are
+ * implemented.
  *
  * @author Liam Fallon (liam.fallon@est.tech)
  */
@@ -58,6 +58,8 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult;
 public class ToscaTopologyTemplate extends PfConcept {
     private static final long serialVersionUID = 8969698734673232603L;
 
+    public static final String DEFAULT_LOCAL_NAME = "ToscaTopologyTemplateSimple";
+
     @EmbeddedId
     private PfReferenceKey key;
 
@@ -71,12 +73,12 @@ public class ToscaTopologyTemplate extends PfConcept {
      * The Default Constructor creates a {@link ToscaTopologyTemplate} object with a null key.
      */
     public ToscaTopologyTemplate() {
-        this(new PfReferenceKey());
+        this(new PfReferenceKey(ToscaServiceTemplate.DEFAULT_NAME, ToscaServiceTemplate.DEFAULT_VERSION,
+                DEFAULT_LOCAL_NAME));
     }
 
     /**
-     * The Key Constructor creates a {@link ToscaTopologyTemplate} object with the given concept
-     * key.
+     * The Key Constructor creates a {@link ToscaTopologyTemplate} object with the given concept key.
      *
      * @param key the key
      */
index 3d563a1..2240ef0 100644 (file)
 
 package org.onap.policy.models.tosca.simple.provider;
 
+import javax.ws.rs.core.Response;
+
 import lombok.NonNull;
 
 import org.onap.policy.models.base.PfConceptKey;
 import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.base.PfModelRuntimeException;
 import org.onap.policy.models.dao.PfDao;
+import org.onap.policy.models.tosca.simple.concepts.ToscaPolicies;
+import org.onap.policy.models.tosca.simple.concepts.ToscaPolicy;
+import org.onap.policy.models.tosca.simple.concepts.ToscaPolicyType;
+import org.onap.policy.models.tosca.simple.concepts.ToscaPolicyTypes;
 import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * This class provides the provision of information on TOSCA concepts in the database to callers.
@@ -33,6 +43,8 @@ import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
  * @author Liam Fallon (liam.fallon@est.tech)
  */
 public class SimpleToscaProvider {
+    private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class);
+
     /**
      * Get policy types.
      *
@@ -44,7 +56,21 @@ public class SimpleToscaProvider {
      */
     public ToscaServiceTemplate getPolicyTypes(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey)
             throws PfModelException {
-        return null;
+
+        // Create the structure of the TOSCA service template to contain the policy type
+        ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
+        serviceTemplate.setPolicyTypes(new ToscaPolicyTypes());
+
+        // Add the policy type to the TOSCA service template
+        ToscaPolicyType policyType = dao.get(ToscaPolicyType.class, policyTypeKey);
+        if (policyType != null) {
+            serviceTemplate.getPolicyTypes().getConceptMap().put(policyTypeKey, policyType);
+            return serviceTemplate;
+        } else {
+            String errorMessage = "policy type not found: " + policyTypeKey.getId();
+            LOGGER.warn(errorMessage);
+            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+        }
     }
 
     /**
@@ -57,7 +83,24 @@ public class SimpleToscaProvider {
      */
     public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
-        return null;
+
+        assertPolicyTypesExist(serviceTemplate);
+
+        for (ToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
+            dao.create(policyType);
+        }
+
+        // Return the created policy types
+        ToscaPolicyTypes returnPolicyTypes = new ToscaPolicyTypes();
+
+        for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
+            returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(ToscaPolicyType.class, policyTypeKey));
+        }
+
+        ToscaServiceTemplate returnServiceTemplate = new ToscaServiceTemplate();
+        returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
+
+        return returnServiceTemplate;
     }
 
     /**
@@ -70,7 +113,24 @@ public class SimpleToscaProvider {
      */
     public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
-        return null;
+
+        assertPolicyTypesExist(serviceTemplate);
+
+        for (ToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
+            dao.update(policyType);
+        }
+
+        // Return the created policy types
+        ToscaPolicyTypes returnPolicyTypes = new ToscaPolicyTypes();
+
+        for (PfConceptKey policyTypeKey : serviceTemplate.getPolicyTypes().getConceptMap().keySet()) {
+            returnPolicyTypes.getConceptMap().put(policyTypeKey, dao.get(ToscaPolicyType.class, policyTypeKey));
+        }
+
+        ToscaServiceTemplate returnServiceTemplate = new ToscaServiceTemplate();
+        returnServiceTemplate.setPolicyTypes(returnPolicyTypes);
+
+        return returnServiceTemplate;
     }
 
     /**
@@ -84,7 +144,12 @@ public class SimpleToscaProvider {
      */
     public ToscaServiceTemplate deletePolicyTypes(@NonNull final PfDao dao, @NonNull final PfConceptKey policyTypeKey)
             throws PfModelException {
-        return null;
+
+        ToscaServiceTemplate serviceTemplate = getPolicyTypes(dao, policyTypeKey);
+
+        dao.delete(ToscaPolicyType.class, policyTypeKey);
+
+        return serviceTemplate;
     }
 
     /**
@@ -98,7 +163,22 @@ public class SimpleToscaProvider {
      */
     public ToscaServiceTemplate getPolicies(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
             throws PfModelException {
-        return null;
+
+        // Create the structure of the TOSCA service template to contain the policy type
+        ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
+        serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate());
+        serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies());
+
+        // Add the policy to the TOSCA service template
+        ToscaPolicy policy = dao.get(ToscaPolicy.class, policyKey);
+        if (policy != null) {
+            serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, policy);
+            return serviceTemplate;
+        } else {
+            String errorMessage = "policy not found: " + policyKey.getId();
+            LOGGER.warn(errorMessage);
+            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+        }
     }
 
     /**
@@ -111,7 +191,24 @@ public class SimpleToscaProvider {
      */
     public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
-        return null;
+
+        assertPoliciesExist(serviceTemplate);
+
+        for (ToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
+            dao.create(policy);
+        }
+
+        // Return the created policy types
+        ToscaPolicies returnPolicies = new ToscaPolicies();
+        returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
+
+        for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
+            returnPolicies.getConceptMap().put(policyKey, dao.get(ToscaPolicy.class, policyKey));
+        }
+
+        serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
+
+        return serviceTemplate;
     }
 
     /**
@@ -124,7 +221,24 @@ public class SimpleToscaProvider {
      */
     public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
             @NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
-        return null;
+
+        assertPoliciesExist(serviceTemplate);
+
+        for (ToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
+            dao.update(policy);
+        }
+
+        // Return the created policy types
+        ToscaPolicies returnPolicies = new ToscaPolicies();
+        returnPolicies.setKey(serviceTemplate.getTopologyTemplate().getPolicies().getKey());
+
+        for (PfConceptKey policyKey : serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().keySet()) {
+            returnPolicies.getConceptMap().put(policyKey, dao.get(ToscaPolicy.class, policyKey));
+        }
+
+        serviceTemplate.getTopologyTemplate().setPolicies(returnPolicies);
+
+        return serviceTemplate;
     }
 
     /**
@@ -132,11 +246,56 @@ public class SimpleToscaProvider {
      *
      * @param dao the DAO to use to access the database
      * @param policyKey the policy key
-     * @return the TOSCA service template containing the policy types that were deleted
+     * @return the TOSCA service template containing the policies that were deleted
      * @throws PfModelException on errors deleting policies
      */
     public ToscaServiceTemplate deletePolicies(@NonNull final PfDao dao, @NonNull final PfConceptKey policyKey)
             throws PfModelException {
-        return null;
+
+        ToscaServiceTemplate serviceTemplate = getPolicies(dao, policyKey);
+
+        dao.delete(ToscaPolicy.class, policyKey);
+
+        return serviceTemplate;
+    }
+
+    /**
+     * Check if policy types have been specified is initialized.
+     */
+    private void assertPolicyTypesExist(final ToscaServiceTemplate serviceTemplate) {
+        if (serviceTemplate.getPolicyTypes() == null) {
+            String errorMessage = "no policy types specified on service template";
+            LOGGER.warn(errorMessage);
+            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+        }
+
+        if (serviceTemplate.getPolicyTypes().getConceptMap().isEmpty()) {
+            String errorMessage = "list of policy types specified on service template is empty";
+            LOGGER.warn(errorMessage);
+            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+        }
+    }
+
+    /**
+     * Check if policy types have been specified is initialized.
+     */
+    private void assertPoliciesExist(final ToscaServiceTemplate serviceTemplate) {
+        if (serviceTemplate.getTopologyTemplate() == null) {
+            String errorMessage = "topology template not specified on service template";
+            LOGGER.warn(errorMessage);
+            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+        }
+
+        if (serviceTemplate.getTopologyTemplate().getPolicies() == null) {
+            String errorMessage = "no policies specified on topology template of service template";
+            LOGGER.warn(errorMessage);
+            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+        }
+
+        if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().isEmpty()) {
+            String errorMessage = "list of policies specified on topology template of service template is empty";
+            LOGGER.warn(errorMessage);
+            throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+        }
     }
 }
index 613b0e2..40fe63c 100644 (file)
@@ -32,7 +32,6 @@ import java.lang.reflect.Type;
 
 import lombok.NonNull;
 
-import org.onap.policy.models.base.PfConceptKey;
 import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
 import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate;
 
@@ -56,8 +55,7 @@ public class ToscaServiceTemplateJsonAdapter
         final JsonObject serviceTemplateJsonObject = serviceTemplateElement.getAsJsonObject();
 
         // The outgoing object
-        final PfConceptKey serviceTemplateKey = new PfConceptKey("IncomingServiceTemplate", "0.0.1");
-        final ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(serviceTemplateKey);
+        final ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
         serviceTemplate
                 .setToscaDefinitionsVersion(serviceTemplateJsonObject.get(TOSCA_DEFINITIONS_VERSION).getAsString());
 
@@ -67,7 +65,7 @@ public class ToscaServiceTemplateJsonAdapter
         }
 
         // Set the parent key of the topology template to be this service template
-        serviceTemplate.getTopologyTemplate().getKey().setParentConceptKey(serviceTemplateKey);
+        serviceTemplate.getTopologyTemplate().getKey().setParentConceptKey(serviceTemplate.getKey());
 
         return serviceTemplate;
     }
index 3ea225d..d35c3d6 100644 (file)
@@ -118,7 +118,7 @@ public class ToscaTopologyTemplateTest {
         ttt.clean();
         assertEquals(tttClone0, ttt);
 
-        assertFalse(new ToscaTopologyTemplate().validate(new PfValidationResult()).isValid());
+        assertTrue(new ToscaTopologyTemplate().validate(new PfValidationResult()).isValid());
         assertTrue(ttt.validate(new PfValidationResult()).isValid());
 
         ttt.setDescription(null);
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java
new file mode 100644 (file)
index 0000000..ed25a7a
--- /dev/null
@@ -0,0 +1,283 @@
+/*-
+ * ============LICENSE_START=======================================================
+ *  Copyright (C) 2019 Nordix Foundation.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.tosca.simple.provider;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+
+import com.google.gson.Gson;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.policy.common.utils.resources.ResourceUtils;
+import org.onap.policy.models.base.PfConceptKey;
+import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.dao.DaoParameters;
+import org.onap.policy.models.dao.PfDao;
+import org.onap.policy.models.dao.PfDaoFactory;
+import org.onap.policy.models.dao.impl.DefaultPfDao;
+import org.onap.policy.models.tosca.simple.concepts.ToscaPolicies;
+import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate;
+import org.onap.policy.models.tosca.simple.serialization.ToscaServiceTemplateMessageBodyHandler;
+
+/**
+ * Test the {@link SimpleToscaProvider} class.
+ *
+ * @author Liam Fallon (liam.fallon@est.tech)
+ */
+public class SimpleToscaProviderTest {
+    private Connection connection;
+    private PfDao pfDao;
+    private Gson gson;
+
+
+    /**
+     * Set up the DAO towards the database.
+     *
+     * @throws Exception on database errors
+     */
+    @Before
+    public void setupDao() throws Exception {
+        // Use the JDBC UI "jdbc:h2:mem:testdb" to test towards the h2 database
+        // Use the JDBC UI "jdbc:mariadb://localhost:3306/policy" to test towards a locally installed mariadb instance
+        connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "policy", "P01icY");
+
+        final DaoParameters daoParameters = new DaoParameters();
+        daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName());
+
+        // Use the persistence unit ToscaConceptTest to test towards the h2 database
+        // Use the persistence unit ToscaConceptMariaDBTest to test towards a locally installed mariadb instance
+        daoParameters.setPersistenceUnit("ToscaConceptTest");
+
+        pfDao = new PfDaoFactory().createPfDao(daoParameters);
+        pfDao.init(daoParameters);
+    }
+
+    /**
+     * Set up GSON.
+     */
+    @Before
+    public void setupGson() {
+        gson = new ToscaServiceTemplateMessageBodyHandler().getGson();
+    }
+
+    @After
+    public void teardown() throws Exception {
+        pfDao.close();
+        connection.close();
+    }
+
+    @Test
+    public void testPoliciesGet() throws PfModelException {
+        try {
+            new SimpleToscaProvider().getPolicies(null, null);
+            fail("test should throw an exception here");
+        } catch (Exception exc) {
+            assertEquals("dao is marked @NonNull but is null", exc.getMessage());
+        }
+
+        try {
+            new SimpleToscaProvider().getPolicies(null, new PfConceptKey());
+            fail("test should throw an exception here");
+        } catch (Exception exc) {
+            assertEquals("dao is marked @NonNull but is null", exc.getMessage());
+        }
+
+        try {
+            new SimpleToscaProvider().getPolicies(pfDao, null);
+            fail("test should throw an exception here");
+        } catch (Exception exc) {
+            assertEquals("policyKey is marked @NonNull but is null", exc.getMessage());
+        }
+
+        ToscaServiceTemplate originalServiceTemplate =
+                gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
+                        ToscaServiceTemplate.class);
+
+        assertNotNull(originalServiceTemplate);
+        ToscaServiceTemplate createdServiceTemplate =
+                new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
+
+        assertEquals(originalServiceTemplate, createdServiceTemplate);
+
+        PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
+
+        ToscaServiceTemplate gotServiceTemplate =
+                new SimpleToscaProvider().getPolicies(pfDao, new PfConceptKey(policyKey));
+
+        assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
+                gotServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
+
+    }
+
+    @Test
+    public void testPolicyCreate() throws PfModelException {
+        try {
+            new SimpleToscaProvider().createPolicies(null, null);
+            fail("test should throw an exception here");
+        } catch (Exception exc) {
+            assertEquals("dao is marked @NonNull but is null", exc.getMessage());
+        }
+
+        try {
+            new SimpleToscaProvider().createPolicies(null, new ToscaServiceTemplate());
+            fail("test should throw an exception here");
+        } catch (Exception exc) {
+            assertEquals("dao is marked @NonNull but is null", exc.getMessage());
+        }
+
+        try {
+            new SimpleToscaProvider().createPolicies(pfDao, null);
+            fail("test should throw an exception here");
+        } catch (Exception exc) {
+            assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage());
+        }
+
+        ToscaServiceTemplate originalServiceTemplate =
+                gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
+                        ToscaServiceTemplate.class);
+
+        assertNotNull(originalServiceTemplate);
+        ToscaServiceTemplate createdServiceTemplate =
+                new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
+
+        assertEquals(originalServiceTemplate, createdServiceTemplate);
+    }
+
+    @Test
+    public void testPolicyUpdate() throws PfModelException {
+        try {
+            new SimpleToscaProvider().updatePolicies(null, null);
+            fail("test should throw an exception here");
+        } catch (Exception exc) {
+            assertEquals("dao is marked @NonNull but is null", exc.getMessage());
+        }
+
+        try {
+            new SimpleToscaProvider().updatePolicies(null, new ToscaServiceTemplate());
+            fail("test should throw an exception here");
+        } catch (Exception exc) {
+            assertEquals("dao is marked @NonNull but is null", exc.getMessage());
+        }
+
+        try {
+            new SimpleToscaProvider().updatePolicies(pfDao, null);
+            fail("test should throw an exception here");
+        } catch (Exception exc) {
+            assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage());
+        }
+
+        ToscaServiceTemplate originalServiceTemplate =
+                gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
+                        ToscaServiceTemplate.class);
+
+        assertNotNull(originalServiceTemplate);
+        ToscaServiceTemplate updatedServiceTemplate =
+                new SimpleToscaProvider().updatePolicies(pfDao, originalServiceTemplate);
+
+        assertEquals(originalServiceTemplate, updatedServiceTemplate);
+    }
+
+    @Test
+    public void testPoliciesDelete() throws PfModelException {
+        try {
+            new SimpleToscaProvider().deletePolicies(null, null);
+            fail("test should throw an exception here");
+        } catch (Exception exc) {
+            assertEquals("dao is marked @NonNull but is null", exc.getMessage());
+        }
+
+        try {
+            new SimpleToscaProvider().deletePolicies(null, new PfConceptKey());
+            fail("test should throw an exception here");
+        } catch (Exception exc) {
+            assertEquals("dao is marked @NonNull but is null", exc.getMessage());
+        }
+
+        try {
+            new SimpleToscaProvider().deletePolicies(pfDao, null);
+            fail("test should throw an exception here");
+        } catch (Exception exc) {
+            assertEquals("policyKey is marked @NonNull but is null", exc.getMessage());
+        }
+
+        ToscaServiceTemplate originalServiceTemplate =
+                gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"),
+                        ToscaServiceTemplate.class);
+
+        assertNotNull(originalServiceTemplate);
+        ToscaServiceTemplate createdServiceTemplate =
+                new SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate);
+
+        assertEquals(originalServiceTemplate, createdServiceTemplate);
+
+        PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0");
+
+        ToscaServiceTemplate deletedServiceTemplate =
+                new SimpleToscaProvider().deletePolicies(pfDao, new PfConceptKey(policyKey));
+
+        assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey),
+                deletedServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey));
+
+        try {
+            new SimpleToscaProvider().getPolicies(pfDao, new PfConceptKey(policyKey));
+            fail("test should throw an exception here");
+        } catch (Exception exc) {
+            assertEquals("policy not found: onap.restart.tca:1.0.0", exc.getMessage());
+        }
+    }
+
+    @Test
+    public void testAssertPoliciesExist() throws PfModelException {
+        ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate();
+
+        try {
+            new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate);
+            fail("test should throw an exception here");
+        } catch (Exception exc) {
+            assertEquals("topology template not specified on service template", exc.getMessage());
+        }
+
+        testServiceTemplate.setTopologyTemplate(new ToscaTopologyTemplate());
+        try {
+            new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate);
+            fail("test should throw an exception here");
+        } catch (Exception exc) {
+            assertEquals("no policies specified on topology template of service template", exc.getMessage());
+        }
+
+        testServiceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies());
+        try {
+            new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate);
+            fail("test should throw an exception here");
+        } catch (Exception exc) {
+            assertEquals("list of policies specified on topology template of service template is empty",
+                    exc.getMessage());
+        }
+
+    }
+}
diff --git a/models-tosca/src/test/resources/META-INF/persistence.xml b/models-tosca/src/test/resources/META-INF/persistence.xml
new file mode 100644 (file)
index 0000000..6834090
--- /dev/null
@@ -0,0 +1,74 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ============LICENSE_START=======================================================
+   Copyright (C) 2019 Nordix Foundation.
+  ================================================================================
+  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.
+  
+  SPDX-License-Identifier: Apache-2.0
+  ============LICENSE_END=========================================================
+-->
+
+<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
+    <persistence-unit name="ToscaConceptTest" transaction-type="RESOURCE_LOCAL">
+        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
+
+        <class>org.onap.policy.models.dao.converters.CDataConditioner</class>
+        <class>org.onap.policy.models.dao.converters.Uuid2String</class>
+        <class>org.onap.policy.models.base.PfConceptKey</class>
+        <class>org.onap.policy.models.tosca.simple.concepts.ToscaPolicyType</class>
+        <class>org.onap.policy.models.tosca.simple.concepts.ToscaPolicy</class>
+
+        <properties>
+            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
+            <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:testdb" />
+            <property name="javax.persistence.jdbc.user" value="policy" />
+            <property name="javax.persistence.jdbc.password" value="P01icY" />
+            <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
+            <property name="eclipselink.ddl-generation.output-mode" value="database" />
+            <property name="eclipselink.logging.level" value="INFO" />
+        </properties>
+    </persistence-unit>
+
+    <persistence-unit name="ToscaConceptMariaDBTest" transaction-type="RESOURCE_LOCAL">
+        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
+
+        <class>org.onap.policy.models.dao.converters.CDataConditioner</class>
+        <class>org.onap.policy.models.dao.converters.Uuid2String</class>
+        <class>org.onap.policy.models.base.PfConceptKey</class>
+        <class>org.onap.policy.models.tosca.simple.concepts.ToscaPolicy</class>
+
+        <properties>
+            <property name="javax.persistence.jdbc.driver" value="org.mariadb.jdbc.Driver" />
+            <property name="javax.persistence.jdbc.url" value="jdbc:mariadb://localhost:3306/policy" />
+            <property name="javax.persistence.jdbc.user" value="policy" />
+            <property name="javax.persistence.jdbc.password" value="P01icY" />
+            <property name="javax.persistence.schema-generation.database.action" value="create" />
+
+            <!-- property name="eclipselink.logging.level" value="ALL" />
+            <property name="eclipselink.logging.level.jpa" value="ALL" />
+            <property name="eclipselink.logging.level.ddl" value="ALL" />
+            <property name="eclipselink.logging.level.connection" value="ALL" />
+            <property name="eclipselink.logging.level.sql" value="ALL" />
+            <property name="eclipselink.logging.level.transaction" value="ALL" />
+            <property name="eclipselink.logging.level.sequencing" value="ALL" />
+            <property name="eclipselink.logging.level.server" value="ALL" />
+            <property name="eclipselink.logging.level.query" value="ALL" />
+            <property name="eclipselink.logging.level.properties" value="ALL" /-->
+
+            <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
+            <property name="eclipselink.ddl-generation.output-mode" value="database" />
+            <property name="eclipselink.logging.level" value="INFO" />
+        </properties>
+    </persistence-unit>
+</persistence>