aboutsummaryrefslogtreecommitdiffstats
path: root/models-tosca
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@est.tech>2019-03-25 08:09:14 +0000
committerliamfallon <liam.fallon@est.tech>2019-03-25 08:09:14 +0000
commitbde47d7867610f36042d3ce1440c1598d1248fcc (patch)
tree41824c940daa78c00a6c4b8f75b19faccd4638af /models-tosca
parent2a245ef80e39a101015efb164de53f1753fa5d47 (diff)
Add provider for Tosca Policies
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>
Diffstat (limited to 'models-tosca')
-rw-r--r--models-tosca/pom.xml13
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaDataTypes.java5
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicies.java5
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicyTypes.java5
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaServiceTemplate.java5
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaTopologyTemplate.java12
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java177
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateJsonAdapter.java6
-rw-r--r--models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/ToscaTopologyTemplateTest.java2
-rw-r--r--models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java283
-rw-r--r--models-tosca/src/test/resources/META-INF/persistence.xml74
11 files changed, 564 insertions, 23 deletions
diff --git a/models-tosca/pom.xml b/models-tosca/pom.xml
index 5658a1c0e..c8fa2520a 100644
--- a/models-tosca/pom.xml
+++ b/models-tosca/pom.xml
@@ -56,5 +56,18 @@
<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>
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaDataTypes.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaDataTypes.java
index cbf0e3844..eae98a1bc 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaDataTypes.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaDataTypes.java
@@ -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));
}
/**
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicies.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicies.java
index 9dd12ec88..ee9c2eae5 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicies.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicies.java
@@ -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));
}
/**
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicyTypes.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicyTypes.java
index 23159ccfa..277291364 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicyTypes.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicyTypes.java
@@ -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));
}
/**
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaServiceTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaServiceTemplate.java
index fd8b134ce..c3bb9165e 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaServiceTemplate.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaServiceTemplate.java
@@ -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));
}
/**
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaTopologyTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaTopologyTemplate.java
index 9d156c3df..35381ec49 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaTopologyTemplate.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaTopologyTemplate.java
@@ -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
*/
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java
index 3d563a1cc..2240ef099 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java
@@ -20,12 +20,22 @@
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);
+ }
}
}
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateJsonAdapter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateJsonAdapter.java
index 613b0e292..40fe63c74 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateJsonAdapter.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateJsonAdapter.java
@@ -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;
}
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/ToscaTopologyTemplateTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/ToscaTopologyTemplateTest.java
index 3ea225d19..d35c3d661 100644
--- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/ToscaTopologyTemplateTest.java
+++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/ToscaTopologyTemplateTest.java
@@ -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
index 000000000..ed25a7a25
--- /dev/null
+++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProviderTest.java
@@ -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
index 000000000..68340901b
--- /dev/null
+++ b/models-tosca/src/test/resources/META-INF/persistence.xml
@@ -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>