aboutsummaryrefslogtreecommitdiffstats
path: root/models-tosca
diff options
context:
space:
mode:
Diffstat (limited to 'models-tosca')
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicy.java4
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyOperationalPolicy.java1
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java82
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider.java268
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyToscaProvider.java139
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaPolicies.java2
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaProperty.java1
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/provider/SimpleToscaProvider.java49
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/simple/serialization/ToscaServiceTemplateJsonAdapter.java31
-rw-r--r--models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java85
-rw-r--r--models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestPojos.java2
-rw-r--r--models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/mapping/PlainToscaServiceTemplateMapperTest.java2
-rw-r--r--models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyTest.java8
-rw-r--r--models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProviderTest.java317
-rw-r--r--models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/serialization/LegacyOperationalPolicySerializationTest.java2
-rw-r--r--models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java1
16 files changed, 750 insertions, 244 deletions
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicy.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicy.java
index 2454b51be..59715e4f9 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicy.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicy.java
@@ -21,7 +21,6 @@
package org.onap.policy.models.tosca.legacy.concepts;
-import java.util.List;
import java.util.Map;
import lombok.Data;
@@ -30,6 +29,7 @@ import lombok.Data;
* Definition of a legacy guard policy stored as a TOSCA policy.
*
* @author Liam Fallon (liam.fallon@est.tech)
+ * @author Chenfei Gao (cgao@research.att.com)
*/
@Data
public class LegacyGuardPolicy {
@@ -38,6 +38,6 @@ public class LegacyGuardPolicy {
private String policyVersion;
- private List<Map<String, String>> content;
+ private Map<String, String> content;
}
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyOperationalPolicy.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyOperationalPolicy.java
index 60a1e454d..1db4d6e20 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyOperationalPolicy.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/concepts/LegacyOperationalPolicy.java
@@ -36,6 +36,7 @@ public class LegacyOperationalPolicy {
@SerializedName("policy-id")
private String policyId;
+ @SerializedName("policy-version")
private String policyVersion;
private String content;
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java
index 2f87020be..65f477572 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapper.java
@@ -23,14 +23,19 @@ package org.onap.policy.models.tosca.legacy.mapping;
import java.util.HashMap;
import java.util.Map;
+import javax.ws.rs.core.Response;
+
import org.onap.policy.models.base.PfConceptKey;
-import org.onap.policy.models.base.PfReferenceKey;
+import org.onap.policy.models.base.PfModelRuntimeException;
import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
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.ToscaServiceTemplate;
import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate;
import org.onap.policy.models.tosca.simple.mapping.ToscaServiceTemplateMapper;
+import org.onap.policy.models.tosca.utils.ToscaUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* This class maps a legacy operational policy to and from a TOSCA service template.
@@ -39,51 +44,72 @@ import org.onap.policy.models.tosca.simple.mapping.ToscaServiceTemplateMapper;
*/
public class LegacyOperationalPolicyMapper
implements ToscaServiceTemplateMapper<LegacyOperationalPolicy, LegacyOperationalPolicy> {
+ private static final Logger LOGGER = LoggerFactory.getLogger(LegacyOperationalPolicyMapper.class);
- // TODO: Do this correctly with an atomic integer
- private static int nextVersion = 1;
+ private static final PfConceptKey LEGACY_OPERATIONAL_TYPE =
+ new PfConceptKey("onap.policies.controlloop.Operational", "1.0.0");
@Override
- public ToscaServiceTemplate toToscaServiceTemplate(LegacyOperationalPolicy legacyOperationalPolicy) {
- PfConceptKey policyKey =
- new PfConceptKey(legacyOperationalPolicy.getPolicyId(), getNextVersion());
+ public ToscaServiceTemplate toToscaServiceTemplate(final LegacyOperationalPolicy legacyOperationalPolicy) {
+ String incomingVersion = legacyOperationalPolicy.getPolicyVersion();
+ if (incomingVersion == null) {
+ incomingVersion = "1";
+ }
+
+ PfConceptKey policyKey = new PfConceptKey(legacyOperationalPolicy.getPolicyId(), incomingVersion + ".0.0");
- ToscaPolicy toscaPolicy = new ToscaPolicy(policyKey);
+ final ToscaPolicy toscaPolicy = new ToscaPolicy(policyKey);
- // TODO: Find out how to parse the PolicyType from the content
- // TODO: Check if this is the correct way to set the policy type version
- toscaPolicy.setType(new PfConceptKey("SomeDerivedPolicyType", "1.0.1"));
+ toscaPolicy.setType(LEGACY_OPERATIONAL_TYPE);
- Map<String, String> propertyMap = new HashMap<>();
+ final Map<String, String> propertyMap = new HashMap<>();
toscaPolicy.setProperties(propertyMap);
toscaPolicy.getProperties().put("Content", legacyOperationalPolicy.getContent());
- PfConceptKey serviceTemplateKey = new PfConceptKey("ServiceTemplate", "1.0.2");
- ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate(serviceTemplateKey);
+ final ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
serviceTemplate.setToscaDefinitionsVersion("tosca_simple_yaml_1_0");
- PfReferenceKey topologyTemplateKey = new PfReferenceKey(serviceTemplateKey, "TopolocyTemplate");
- serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate(topologyTemplateKey));
+ serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplate());
- PfConceptKey policiesKey = new PfConceptKey("Policies", "1.0.3");
- serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies(policiesKey));
+ serviceTemplate.getTopologyTemplate().setPolicies(new ToscaPolicies());
serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policyKey, toscaPolicy);
return serviceTemplate;
}
@Override
- public LegacyOperationalPolicy fromToscaServiceTemplate(ToscaServiceTemplate serviceTemplate) {
- // TODO Auto-generated method stub
- return null;
- }
+ public LegacyOperationalPolicy fromToscaServiceTemplate(final ToscaServiceTemplate serviceTemplate) {
+ ToscaUtils.assertPoliciesExist(serviceTemplate);
+
+ if (serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().size() > 1) {
+ String errorMessage = "more than one policy found in service template";
+ LOGGER.warn(errorMessage);
+ throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+ }
+
+ // Get the policy
+ final ToscaPolicy toscaPolicy =
+ serviceTemplate.getTopologyTemplate().getPolicies().getAll(null).iterator().next();
+
+ final LegacyOperationalPolicy legacyOperationalPolicy = new LegacyOperationalPolicy();
+ legacyOperationalPolicy.setPolicyId(toscaPolicy.getKey().getName());
+ legacyOperationalPolicy.setPolicyVersion(Integer.toString(toscaPolicy.getKey().getMajorVersion()));
+
+ if (toscaPolicy.getProperties() == null) {
+ String errorMessage = "no properties defined on TOSCA policy";
+ LOGGER.warn(errorMessage);
+ throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+ }
+
+ final String content = toscaPolicy.getProperties().get("Content");
+ if (toscaPolicy.getProperties() == null) {
+ String errorMessage = "property \"Content\" not defined on TOSCA policy";
+ LOGGER.warn(errorMessage);
+ throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+ }
+
+ legacyOperationalPolicy.setContent(content);
- /**
- * Get the next policy version.
- *
- * @return the next version
- */
- private static String getNextVersion() {
- return "1.0." + nextVersion++;
+ return legacyOperationalPolicy;
}
}
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider.java
new file mode 100644
index 000000000..42343e1df
--- /dev/null
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyProvider.java
@@ -0,0 +1,268 @@
+/*-
+ * ============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.legacy.provider;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.ws.rs.core.Response;
+
+import lombok.NonNull;
+
+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.legacy.concepts.LegacyGuardPolicy;
+import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
+import org.onap.policy.models.tosca.legacy.mapping.LegacyOperationalPolicyMapper;
+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.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.simple.concepts.ToscaTopologyTemplate;
+import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This class provides the provision of information on TOSCA concepts in the database to callers in legacy formats.
+ *
+ * @author Liam Fallon (liam.fallon@est.tech)
+ */
+public class LegacyProvider {
+ private static final Logger LOGGER = LoggerFactory.getLogger(LegacyProvider.class);
+
+ private static final String FIRST_POLICY_VERSION = "1";
+
+ // Recurring constants
+ private static final String NO_POLICY_FOUND_FOR_POLICY_ID = "no policy found for policy ID: ";
+
+ /**
+ * Get legacy operational policy.
+ *
+ * @param dao the DAO to use to access the database
+ * @param policyId ID of the policy.
+ * @return the policies found
+ * @throws PfModelException on errors getting policies
+ */
+ public LegacyOperationalPolicy getOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId)
+ throws PfModelException {
+
+ ToscaPolicy newestPolicy = getLatestPolicy(dao, policyId);
+
+ if (newestPolicy == null) {
+ String errorMessage = NO_POLICY_FOUND_FOR_POLICY_ID + policyId;
+ LOGGER.warn(errorMessage);
+ throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+ }
+
+ // 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());
+ serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(newestPolicy.getKey(), newestPolicy);
+
+ return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
+ }
+
+ /**
+ * Create legacy operational policy.
+ *
+ * @param dao the DAO to use to access the database
+ * @param legacyOperationalPolicy the definition of the policy to be created.
+ * @return the created policy
+ * @throws PfModelException on errors creating policies
+ */
+ public LegacyOperationalPolicy createOperationalPolicy(@NonNull final PfDao dao,
+ @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
+
+ // We need to find the latest policy and update the major version, if there is no policy with this ID, then
+ // we set it to the first version
+ ToscaPolicy newestPolicy = getLatestPolicy(dao, legacyOperationalPolicy.getPolicyId());
+
+ if (newestPolicy == null) {
+ legacyOperationalPolicy.setPolicyVersion(FIRST_POLICY_VERSION);
+ } else {
+ legacyOperationalPolicy.setPolicyVersion(Integer.toString(newestPolicy.getKey().getMajorVersion() + 1));
+ }
+
+ ToscaServiceTemplate incomingServiceTemplate =
+ new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
+ ToscaServiceTemplate outgoingingServiceTemplate =
+ new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate);
+
+ return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
+ }
+
+ /**
+ * Update legacy operational policy.
+ *
+ * @param dao the DAO to use to access the database
+ * @param legacyOperationalPolicy the definition of the policy to be updated
+ * @return the updated policy
+ * @throws PfModelException on errors updating policies
+ */
+ public LegacyOperationalPolicy updateOperationalPolicy(@NonNull final PfDao dao,
+ @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
+
+ // We need to find the latest policy and use the major version, if there is no policy with this ID, then
+ // we have an error
+ ToscaPolicy newestPolicy = getLatestPolicy(dao, legacyOperationalPolicy.getPolicyId());
+
+ if (newestPolicy == null) {
+ String errorMessage = NO_POLICY_FOUND_FOR_POLICY_ID + legacyOperationalPolicy.getPolicyId();
+ LOGGER.warn(errorMessage);
+ throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+ } else {
+ legacyOperationalPolicy.setPolicyVersion(Integer.toString(newestPolicy.getKey().getMajorVersion()));
+ }
+
+ ToscaServiceTemplate incomingServiceTemplate =
+ new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy);
+ ToscaServiceTemplate outgoingingServiceTemplate =
+ new SimpleToscaProvider().createPolicies(dao, incomingServiceTemplate);
+
+ return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(outgoingingServiceTemplate);
+ }
+
+ /**
+ * Delete legacy operational policy.
+ *
+ * @param dao the DAO to use to access the database
+ * @param policyId ID of the policy.
+ * @return the deleted policy
+ * @throws PfModelException on errors deleting policies
+ */
+ public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId)
+ throws PfModelException {
+
+ // Get all the policies in the database and check the policy ID against the policies returned
+ List<ToscaPolicy> policyList = dao.getAll(ToscaPolicy.class);
+
+ // Find the latest policy that matches the ID
+ List<ToscaPolicy> policyDeleteList = new ArrayList<>();
+
+ for (ToscaPolicy policy : policyList) {
+ if (policyId.equals(policy.getKey().getName())) {
+ policyDeleteList.add(policy);
+ }
+ }
+
+ if (policyDeleteList.isEmpty()) {
+ String errorMessage = NO_POLICY_FOUND_FOR_POLICY_ID + policyId;
+ LOGGER.warn(errorMessage);
+ throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage);
+ }
+
+ // 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());
+
+ for (ToscaPolicy deletePolicy : policyDeleteList) {
+ dao.delete(deletePolicy);
+ serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(deletePolicy.getKey(),
+ deletePolicy);
+ }
+
+ return new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate);
+ }
+
+ /**
+ * Get legacy guard policy.
+ *
+ * @param dao the DAO to use to access the database
+ * @param policyId ID of the policy.
+ * @return the policies found
+ * @throws PfModelException on errors getting policies
+ */
+ public LegacyGuardPolicy getGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId)
+ throws PfModelException {
+ return null;
+ }
+
+ /**
+ * Create legacy guard policy.
+ *
+ * @param dao the DAO to use to access the database
+ * @param legacyGuardPolicy the definition of the policy to be created.
+ * @return the created policy
+ * @throws PfModelException on errors creating policies
+ */
+ public LegacyGuardPolicy createGuardPolicy(@NonNull final PfDao dao,
+ @NonNull final LegacyGuardPolicy legacyGuardPolicy) throws PfModelException {
+ return null;
+ }
+
+ /**
+ * Update legacy guard policy.
+ *
+ * @param dao the DAO to use to access the database
+ * @param legacyGuardPolicy the definition of the policy to be updated
+ * @return the updated policy
+ * @throws PfModelException on errors updating policies
+ */
+ public LegacyGuardPolicy updateGuardPolicy(@NonNull final PfDao dao,
+ @NonNull final LegacyGuardPolicy legacyGuardPolicy) throws PfModelException {
+ return null;
+ }
+
+
+ /**
+ * Delete legacy guard policy.
+ *
+ * @param dao the DAO to use to access the database
+ * @param policyId ID of the policy.
+ * @return the deleted policy
+ * @throws PfModelException on errors deleting policies
+ */
+ public LegacyGuardPolicy deleteGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId)
+ throws PfModelException {
+ return null;
+ }
+
+ /**
+ * Get the latest policy for a policy ID.
+ *
+ * @param dao The DAO to read from
+ * @param policyId the ID of the policy
+ * @return the policy
+ */
+ private ToscaPolicy getLatestPolicy(final PfDao dao, final String policyId) {
+ // Get all the policies in the database and check the policy ID against the policies returned
+ List<ToscaPolicy> policyList = dao.getAll(ToscaPolicy.class);
+
+ // Find the latest policy that matches the ID
+ ToscaPolicy newestPolicy = null;
+
+ for (ToscaPolicy policy : policyList) {
+ if (!policyId.equals(policy.getKey().getName())) {
+ continue;
+ }
+
+ // We found a matching policy
+ if (newestPolicy == null || policy.getKey().isNewerThan(newestPolicy.getKey())) {
+ // First policy found
+ newestPolicy = policy;
+ }
+ }
+ return newestPolicy;
+ }
+}
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyToscaProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyToscaProvider.java
deleted file mode 100644
index da9d929df..000000000
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/provider/LegacyToscaProvider.java
+++ /dev/null
@@ -1,139 +0,0 @@
-/*-
- * ============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.legacy.provider;
-
-import lombok.NonNull;
-
-import org.onap.policy.models.base.PfModelException;
-import org.onap.policy.models.dao.PfDao;
-import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicy;
-import org.onap.policy.models.tosca.legacy.concepts.LegacyOperationalPolicy;
-
-/**
- * This class provides the provision of information on TOSCA concepts in the database to callers in legacy formats.
- *
- * @author Liam Fallon (liam.fallon@est.tech)
- */
-public class LegacyToscaProvider {
- /**
- * Get legacy operational policy.
- *
- * @param dao the DAO to use to access the database
- * @param policyId ID of the policy.
- * @return the policies found
- * @throws PfModelException on errors getting policies
- */
- public LegacyOperationalPolicy getOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId)
- throws PfModelException {
- return null;
- }
-
- /**
- * Create legacy operational policy.
- *
- * @param dao the DAO to use to access the database
- * @param legacyOperationalPolicy the definition of the policy to be created.
- * @return the created policy
- * @throws PfModelException on errors creating policies
- */
- public LegacyOperationalPolicy createOperationalPolicy(@NonNull final PfDao dao,
- @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
- return null;
- }
-
- /**
- * Update legacy operational policy.
- *
- * @param dao the DAO to use to access the database
- * @param legacyOperationalPolicy the definition of the policy to be updated
- * @return the updated policy
- * @throws PfModelException on errors updating policies
- */
- public LegacyOperationalPolicy updateOperationalPolicy(@NonNull final PfDao dao,
- @NonNull final LegacyOperationalPolicy legacyOperationalPolicy) throws PfModelException {
- return null;
- }
-
- /**
- * Delete legacy operational policy.
- *
- * @param dao the DAO to use to access the database
- * @param policyId ID of the policy.
- * @return the deleted policy
- * @throws PfModelException on errors deleting policies
- */
- public LegacyOperationalPolicy deleteOperationalPolicy(@NonNull final PfDao dao, @NonNull final String policyId)
- throws PfModelException {
- return null;
- }
-
- /**
- * Get legacy guard policy.
- *
- * @param dao the DAO to use to access the database
- * @param policyId ID of the policy.
- * @return the policies found
- * @throws PfModelException on errors getting policies
- */
- public LegacyGuardPolicy getGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId)
- throws PfModelException {
- return null;
- }
-
- /**
- * Create legacy guard policy.
- *
- * @param dao the DAO to use to access the database
- * @param legacyGuardPolicy the definition of the policy to be created.
- * @return the created policy
- * @throws PfModelException on errors creating policies
- */
- public LegacyGuardPolicy createGuardPolicy(@NonNull final PfDao dao,
- @NonNull final LegacyGuardPolicy legacyGuardPolicy) throws PfModelException {
- return null;
- }
-
- /**
- * Update legacy guard policy.
- *
- * @param dao the DAO to use to access the database
- * @param legacyGuardPolicy the definition of the policy to be updated
- * @return the updated policy
- * @throws PfModelException on errors updating policies
- */
- public LegacyGuardPolicy updateGuardPolicy(@NonNull final PfDao dao,
- @NonNull final LegacyGuardPolicy legacyGuardPolicy) throws PfModelException {
- return null;
- }
-
- /**
- * Delete legacy guard policy.
- *
- * @param dao the DAO to use to access the database
- * @param policyId ID of the policy.
- * @return the deleted policy
- * @throws PfModelException on errors deleting policies
- */
- public LegacyGuardPolicy deleteGuardPolicy(@NonNull final PfDao dao, @NonNull final String policyId)
- throws PfModelException {
- return null;
- }
-}
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 ee9c2eae5..f318bb6be 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,7 +47,7 @@ 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_NAME = "ToscaPoliciesSimple";
public static final String DEFAULT_VERSION = "1.0.0";
/**
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaProperty.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaProperty.java
index bc454c996..2276f5a7a 100644
--- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaProperty.java
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/ToscaProperty.java
@@ -23,7 +23,6 @@
package org.onap.policy.models.tosca.simple.concepts;
-import com.google.gson.JsonElement;
import com.google.gson.annotations.SerializedName;
import java.util.List;
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 2240ef099..c7984c5ea 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
@@ -34,6 +34,7 @@ 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.onap.policy.models.tosca.utils.ToscaUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -84,7 +85,7 @@ public class SimpleToscaProvider {
public ToscaServiceTemplate createPolicyTypes(@NonNull final PfDao dao,
@NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
- assertPolicyTypesExist(serviceTemplate);
+ ToscaUtils.assertPolicyTypesExist(serviceTemplate);
for (ToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
dao.create(policyType);
@@ -114,7 +115,7 @@ public class SimpleToscaProvider {
public ToscaServiceTemplate updatePolicyTypes(@NonNull final PfDao dao,
@NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
- assertPolicyTypesExist(serviceTemplate);
+ ToscaUtils.assertPolicyTypesExist(serviceTemplate);
for (ToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) {
dao.update(policyType);
@@ -192,7 +193,7 @@ public class SimpleToscaProvider {
public ToscaServiceTemplate createPolicies(@NonNull final PfDao dao,
@NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
- assertPoliciesExist(serviceTemplate);
+ ToscaUtils.assertPoliciesExist(serviceTemplate);
for (ToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
dao.create(policy);
@@ -222,7 +223,7 @@ public class SimpleToscaProvider {
public ToscaServiceTemplate updatePolicies(@NonNull final PfDao dao,
@NonNull final ToscaServiceTemplate serviceTemplate) throws PfModelException {
- assertPoliciesExist(serviceTemplate);
+ ToscaUtils.assertPoliciesExist(serviceTemplate);
for (ToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) {
dao.update(policy);
@@ -258,44 +259,4 @@ public class SimpleToscaProvider {
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 e25adfd3e..78f3153e2 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,13 +32,10 @@ import java.lang.reflect.Type;
import lombok.NonNull;
-import org.onap.policy.models.base.PfConceptKey;
import org.onap.policy.models.tosca.simple.concepts.ToscaDataTypes;
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;
/**
* GSON type adapter for TOSCA policies.
@@ -49,8 +46,6 @@ import org.slf4j.LoggerFactory;
public class ToscaServiceTemplateJsonAdapter
implements JsonSerializer<ToscaServiceTemplate>, JsonDeserializer<ToscaServiceTemplate> {
- private static final Logger LOGGER = LoggerFactory.getLogger(ToscaServiceTemplateJsonAdapter.class);
-
private static final String TOPOLOGY_TEMPLATE = "topology_template";
private static final String TOSCA_DEFINITIONS_VERSION = "tosca_definitions_version";
private static final String POLICY_TYPES = "policy_types";
@@ -64,10 +59,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);
-
- // Set tosca_definitions_version
+ final ToscaServiceTemplate serviceTemplate = new ToscaServiceTemplate();
serviceTemplate
.setToscaDefinitionsVersion(serviceTemplateJsonObject.get(TOSCA_DEFINITIONS_VERSION).getAsString());
@@ -75,19 +67,18 @@ public class ToscaServiceTemplateJsonAdapter
if (serviceTemplateJsonObject.has(TOPOLOGY_TEMPLATE)) {
serviceTemplate.setTopologyTemplate(new ToscaTopologyTemplateJsonAdapter().deserialize(
serviceTemplateJsonObject.get(TOPOLOGY_TEMPLATE), ToscaTopologyTemplate.class, context));
- serviceTemplate.getTopologyTemplate().getKey().setParentConceptKey(serviceTemplateKey);
}
// Set policy_types
if (serviceTemplateJsonObject.has(POLICY_TYPES)) {
- serviceTemplate.setPolicyTypes(new ToscaPolicyTypesJsonAdapter().deserialize(
- serviceTemplateJsonObject.get(POLICY_TYPES), ToscaPolicyTypes.class, context));
+ serviceTemplate.setPolicyTypes(new ToscaPolicyTypesJsonAdapter()
+ .deserialize(serviceTemplateJsonObject.get(POLICY_TYPES), ToscaPolicyTypes.class, context));
}
// Set data_types
if (serviceTemplateJsonObject.has(DATA_TYPES)) {
- serviceTemplate.setDataTypes(new ToscaDataTypesJsonAdapter().deserialize(
- serviceTemplateJsonObject.get(DATA_TYPES), ToscaDataTypes.class, context));
+ serviceTemplate.setDataTypes(new ToscaDataTypesJsonAdapter()
+ .deserialize(serviceTemplateJsonObject.get(DATA_TYPES), ToscaDataTypes.class, context));
}
return serviceTemplate;
@@ -101,8 +92,8 @@ public class ToscaServiceTemplateJsonAdapter
// Serialize tosca_definitions_version
if (serviceTemplate.getToscaDefinitionsVersion() != null) {
- serviceTemplateJsonObject.addProperty(
- TOSCA_DEFINITIONS_VERSION, serviceTemplate.getToscaDefinitionsVersion());
+ serviceTemplateJsonObject.addProperty(TOSCA_DEFINITIONS_VERSION,
+ serviceTemplate.getToscaDefinitionsVersion());
}
// Serialize topoligy_template
@@ -114,15 +105,15 @@ public class ToscaServiceTemplateJsonAdapter
// Serialize policy_types
if (serviceTemplate.getPolicyTypes() != null) {
- JsonElement policyTypesJsonElement = new ToscaPolicyTypesJsonAdapter()
- .serialize(serviceTemplate.getPolicyTypes(), type, context);
+ JsonElement policyTypesJsonElement =
+ new ToscaPolicyTypesJsonAdapter().serialize(serviceTemplate.getPolicyTypes(), type, context);
serviceTemplateJsonObject.add(POLICY_TYPES, policyTypesJsonElement);
}
// Serialize data_types
if (serviceTemplate.getDataTypes() != null) {
- JsonElement dataTypesJsonElement = new ToscaDataTypesJsonAdapter()
- .serialize(serviceTemplate.getDataTypes(), type, context);
+ JsonElement dataTypesJsonElement =
+ new ToscaDataTypesJsonAdapter().serialize(serviceTemplate.getDataTypes(), type, context);
serviceTemplateJsonObject.add(DATA_TYPES, dataTypesJsonElement);
}
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java
new file mode 100644
index 000000000..a02bfa4b7
--- /dev/null
+++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java
@@ -0,0 +1,85 @@
+/*-
+ * ============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.utils;
+
+import javax.ws.rs.core.Response;
+
+import org.onap.policy.models.base.PfModelRuntimeException;
+import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Utility class for TOSCA concepts.
+ *
+ * @author Liam Fallon (liam.fallon@est.tech)
+ */
+public final class ToscaUtils {
+ private static final Logger LOGGER = LoggerFactory.getLogger(ToscaUtils.class);
+
+ /**
+ * Private constructor to prevent subclassing.
+ */
+ private ToscaUtils() {
+ }
+
+ /**
+ * Check if policy types have been specified is initialized.
+ */
+ public static 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.
+ */
+ public static 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/test/java/org/onap/policy/models/tosca/authorative/concepts/TestPojos.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestPojos.java
index 4dd55d562..7c813a625 100644
--- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestPojos.java
+++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestPojos.java
@@ -33,7 +33,7 @@ import org.junit.Test;
import org.onap.policy.common.utils.validation.ToStringTester;
/**
- * Class to perform unit tests of all pojos
+ * Class to perform unit tests of all pojos.
*
* @author Chenfei Gao (cgao@research.att.com)
*
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/mapping/PlainToscaServiceTemplateMapperTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/mapping/PlainToscaServiceTemplateMapperTest.java
index bd6b26bb9..e9223b350 100644
--- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/mapping/PlainToscaServiceTemplateMapperTest.java
+++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/mapping/PlainToscaServiceTemplateMapperTest.java
@@ -37,7 +37,7 @@ import org.onap.policy.models.tosca.simple.concepts.ToscaServiceTemplate;
import org.yaml.snakeyaml.Yaml;
/**
- * This class performs unit test of {@link PlainToscaServiceTemplateMapper}}
+ * This class performs unit test of {@link PlainToscaServiceTemplateMapper}}.
*
* @author Chenfei Gao (cgao@research.att.com)
*/
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyTest.java
index d2b2216e7..764ce063f 100644
--- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyTest.java
+++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/LegacyGuardPolicyTest.java
@@ -23,9 +23,7 @@ package org.onap.policy.models.tosca.legacy.concepts;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
-import java.util.ArrayList;
import java.util.HashMap;
-import java.util.List;
import java.util.Map;
import org.junit.Test;
@@ -41,10 +39,8 @@ public class LegacyGuardPolicyTest {
assertEquals("guard.frequency", guard.getPolicyId());
guard.setPolicyVersion("1");
assertEquals("1", guard.getPolicyVersion());
- Map<String, String> body = new HashMap<>();
- body.put("actor", "SO");
- List<Map<String, String>> content = new ArrayList<>();
- content.add(body);
+ Map<String, String> content = new HashMap<>();
+ content.put("actor", "SO");
guard.setContent(content);
assertEquals(1, guard.getContent().size());
}
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProviderTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProviderTest.java
new file mode 100644
index 000000000..271e019d9
--- /dev/null
+++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/provider/LegacyProviderTest.java
@@ -0,0 +1,317 @@
+/*-
+ * ============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.legacy.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.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.legacy.concepts.LegacyOperationalPolicy;
+
+/**
+ * Test the {@link LegacyProvider} class.
+ *
+ * @author Liam Fallon (liam.fallon@est.tech)
+ */
+public class LegacyProviderTest {
+ 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 Gson();
+ }
+
+ @After
+ public void teardown() throws Exception {
+ pfDao.close();
+ connection.close();
+ }
+
+ @Test
+ public void testPoliciesGet() throws PfModelException {
+ try {
+ new LegacyProvider().getOperationalPolicy(null, null);
+ fail("test should throw an exception here");
+ } catch (Exception exc) {
+ assertEquals("dao is marked @NonNull but is null", exc.getMessage());
+ }
+
+ try {
+ new LegacyProvider().getOperationalPolicy(null, "");
+ fail("test should throw an exception here");
+ } catch (Exception exc) {
+ assertEquals("dao is marked @NonNull but is null", exc.getMessage());
+ }
+
+ try {
+ new LegacyProvider().getOperationalPolicy(pfDao, null);
+ fail("test should throw an exception here");
+ } catch (Exception exc) {
+ assertEquals("policyId is marked @NonNull but is null", exc.getMessage());
+ }
+
+ try {
+ new LegacyProvider().getOperationalPolicy(pfDao, "I Dont Exist");
+ fail("test should throw an exception here");
+ } catch (Exception exc) {
+ assertEquals("no policy found for policy ID: I Dont Exist", exc.getMessage());
+ }
+
+ LegacyOperationalPolicy originalLop =
+ gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
+ LegacyOperationalPolicy.class);
+
+ assertNotNull(originalLop);
+
+ LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
+
+ assertEquals(originalLop, createdLop);
+
+ LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
+
+ assertEquals(gotLop, originalLop);
+
+ String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
+ String actualJsonOutput = gson.toJson(gotLop);
+
+ assertEquals(actualJsonOutput.replaceAll("\\s+", ""), expectedJsonOutput.replaceAll("\\s+", ""));
+
+ LegacyOperationalPolicy createdLopV2 = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
+ LegacyOperationalPolicy gotLopV2 = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
+ assertEquals(gotLopV2, createdLopV2);
+ }
+
+ @Test
+ public void testPolicyCreate() throws PfModelException {
+ try {
+ new LegacyProvider().createOperationalPolicy(null, null);
+ fail("test should throw an exception here");
+ } catch (Exception exc) {
+ assertEquals("dao is marked @NonNull but is null", exc.getMessage());
+ }
+
+ try {
+ new LegacyProvider().createOperationalPolicy(null, new LegacyOperationalPolicy());
+ fail("test should throw an exception here");
+ } catch (Exception exc) {
+ assertEquals("dao is marked @NonNull but is null", exc.getMessage());
+ }
+
+ try {
+ new LegacyProvider().createOperationalPolicy(pfDao, null);
+ fail("test should throw an exception here");
+ } catch (Exception exc) {
+ assertEquals("legacyOperationalPolicy is marked @NonNull but is null", exc.getMessage());
+ }
+
+ LegacyOperationalPolicy originalLop =
+ gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
+ LegacyOperationalPolicy.class);
+
+ assertNotNull(originalLop);
+
+ LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
+
+ assertEquals(originalLop, createdLop);
+
+ LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
+
+ assertEquals(gotLop, originalLop);
+
+ String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
+ String actualJsonOutput = gson.toJson(gotLop);
+
+ assertEquals(actualJsonOutput.replaceAll("\\s+", ""), expectedJsonOutput.replaceAll("\\s+", ""));
+ }
+
+
+ @Test
+ public void testPolicyUpdate() throws PfModelException {
+ try {
+ new LegacyProvider().updateOperationalPolicy(null, null);
+ fail("test should throw an exception here");
+ } catch (Exception exc) {
+ assertEquals("dao is marked @NonNull but is null", exc.getMessage());
+ }
+
+ try {
+ new LegacyProvider().updateOperationalPolicy(null, new LegacyOperationalPolicy());
+ fail("test should throw an exception here");
+ } catch (Exception exc) {
+ assertEquals("dao is marked @NonNull but is null", exc.getMessage());
+ }
+
+ try {
+ new LegacyProvider().updateOperationalPolicy(pfDao, null);
+ fail("test should throw an exception here");
+ } catch (Exception exc) {
+ assertEquals("legacyOperationalPolicy is marked @NonNull but is null", exc.getMessage());
+ }
+
+ try {
+ new LegacyProvider().updateOperationalPolicy(pfDao, new LegacyOperationalPolicy());
+ fail("test should throw an exception here");
+ } catch (Exception exc) {
+ assertEquals("no policy found for policy ID: null", exc.getMessage());
+ }
+
+ LegacyOperationalPolicy originalLop =
+ gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
+ LegacyOperationalPolicy.class);
+
+ assertNotNull(originalLop);
+
+ LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
+ assertEquals(originalLop, createdLop);
+
+ LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
+ assertEquals(gotLop, originalLop);
+
+ originalLop.setContent("Some New Content");
+ LegacyOperationalPolicy updatedLop = new LegacyProvider().updateOperationalPolicy(pfDao, originalLop);
+ assertEquals(originalLop, updatedLop);
+
+ LegacyOperationalPolicy gotUpdatedLop =
+ new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
+ assertEquals(gotUpdatedLop, originalLop);
+ assertEquals("Some New Content", gotUpdatedLop.getContent());
+ }
+
+
+ @Test
+ public void testPoliciesDelete() throws PfModelException {
+ try {
+ new LegacyProvider().deleteOperationalPolicy(null, null);
+ fail("test should throw an exception here");
+ } catch (Exception exc) {
+ assertEquals("dao is marked @NonNull but is null", exc.getMessage());
+ }
+
+ try {
+ new LegacyProvider().deleteOperationalPolicy(null, "");
+ fail("test should throw an exception here");
+ } catch (Exception exc) {
+ assertEquals("dao is marked @NonNull but is null", exc.getMessage());
+ }
+
+ try {
+ new LegacyProvider().deleteOperationalPolicy(pfDao, null);
+ fail("test should throw an exception here");
+ } catch (Exception exc) {
+ assertEquals("policyId is marked @NonNull but is null", exc.getMessage());
+ }
+
+
+ try {
+ new LegacyProvider().deleteOperationalPolicy(pfDao, "I Dont Exist");
+ fail("test should throw an exception here");
+ } catch (Exception exc) {
+ assertEquals("no policy found for policy ID: I Dont Exist", exc.getMessage());
+ }
+
+ LegacyOperationalPolicy originalLop =
+ gson.fromJson(ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"),
+ LegacyOperationalPolicy.class);
+
+ assertNotNull(originalLop);
+
+ LegacyOperationalPolicy createdLop = new LegacyProvider().createOperationalPolicy(pfDao, originalLop);
+ assertEquals(originalLop, createdLop);
+
+ LegacyOperationalPolicy gotLop = new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
+
+ assertEquals(gotLop, originalLop);
+
+ String expectedJsonOutput = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.output.json");
+ String actualJsonOutput = gson.toJson(gotLop);
+
+ assertEquals(actualJsonOutput.replaceAll("\\s+", ""), expectedJsonOutput.replaceAll("\\s+", ""));
+
+ LegacyOperationalPolicy deletedLop =
+ new LegacyProvider().deleteOperationalPolicy(pfDao, originalLop.getPolicyId());
+ assertEquals(deletedLop, originalLop);
+
+ try {
+ new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
+ fail("test should throw an exception here");
+ } catch (Exception exc) {
+ assertEquals("no policy found for policy ID: operational.restart", exc.getMessage());
+ }
+
+ LegacyOperationalPolicy otherLop = new LegacyOperationalPolicy();
+ otherLop.setPolicyId("another-policy");
+ otherLop.setPolicyVersion("1");
+ otherLop.setContent("content");
+
+ LegacyOperationalPolicy createdOtherLop = new LegacyProvider().createOperationalPolicy(pfDao, otherLop);
+ assertEquals(otherLop, createdOtherLop);
+
+ try {
+ new LegacyProvider().getOperationalPolicy(pfDao, originalLop.getPolicyId());
+ fail("test should throw an exception here");
+ } catch (Exception exc) {
+ assertEquals("no policy found for policy ID: operational.restart", exc.getMessage());
+ }
+
+ }
+}
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/serialization/LegacyOperationalPolicySerializationTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/serialization/LegacyOperationalPolicySerializationTest.java
index 3c9deb7df..5d1fa42ad 100644
--- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/serialization/LegacyOperationalPolicySerializationTest.java
+++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/serialization/LegacyOperationalPolicySerializationTest.java
@@ -68,7 +68,7 @@ public class LegacyOperationalPolicySerializationTest {
LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString());
assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid());
- assertEquals("operational.restart:1.0.1",
+ assertEquals("operational.restart:1.0.0",
serviceTemplate.getTopologyTemplate().getPolicies().get("operational.restart").getId());
}
}
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java
index e49156330..505e90e28 100644
--- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java
+++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicySerializationTest.java
@@ -115,6 +115,7 @@ public class MonitoringPolicySerializationTest {
verifyVfwMonitoringOutputserialization(serializedServiceTemplate);
} catch (Exception e) {
+ LOGGER.warn("No exception should be thrown", e);
fail("No exception should be thrown");
}
}