diff options
author | liamfallon <liam.fallon@est.tech> | 2020-02-18 16:14:59 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2020-02-18 17:40:33 +0000 |
commit | b477554c29b2d666e7bb0bc860afacc6e935c337 (patch) | |
tree | 22c40a3cbc378d042eab7d37df7328f5eb19176f /models-tosca/src | |
parent | 88fb2e33c81fa0d69846e8d0d218b0ef4015a4ba (diff) |
Add safe entity delete, fix multiple entity get
This review implements checks on whether entities are being used or
referenced propr to allowing deletes.
The review also fixes bugs on multiple entity get returning 4040 not
found
Issue-ID: POLICY-1402
Change-Id: I9cebb9a873098740e9ff4be6284d6307e19838bd
Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'models-tosca/src')
13 files changed, 433 insertions, 33 deletions
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 index 09cc6c0ca..dc8affc77 100644 --- 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 @@ -53,7 +53,7 @@ import org.slf4j.LoggerFactory; public class LegacyProvider { private static final Logger LOGGER = LoggerFactory.getLogger(LegacyProvider.class); - private static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0"; + public static final String LEGACY_MINOR_PATCH_SUFFIX = ".0.0"; // Recurring constants private static final String NO_POLICY_FOUND_FOR_POLICY = "no policy found for policy: "; diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintValidValues.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintValidValues.java index 2a121573c..664855e42 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintValidValues.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintValidValues.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,7 @@ package org.onap.policy.models.tosca.simple.concepts; import java.util.ArrayList; import java.util.List; + import javax.persistence.ElementCollection; import lombok.EqualsAndHashCode; @@ -77,8 +78,8 @@ public class JpaToscaConstraintValidValues extends JpaToscaConstraint { @Override public void fromAuthorative(final ToscaConstraint toscaConstraint) { + validValues = new ArrayList<>(); if (toscaConstraint.getValidValues() != null) { - validValues = new ArrayList<>(); validValues.addAll(toscaConstraint.getValidValues()); } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchema.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchema.java index 881d87c4b..b432486ae 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchema.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchema.java @@ -3,7 +3,7 @@ * ONAP Policy Model * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -47,7 +47,6 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult; import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint; import org.onap.policy.models.tosca.authorative.concepts.ToscaEntrySchema; - /** * Class to represent the EntrySchema of list/map property in TOSCA definition. * @@ -72,7 +71,7 @@ public class JpaToscaEntrySchema private String description; @ElementCollection - private List<JpaToscaConstraint> constraints; + private List<JpaToscaConstraint> constraints = new ArrayList<>(); /** * The full constructor creates a {@link JpaToscaEntrySchema} object with mandatory fields. @@ -170,7 +169,6 @@ public class JpaToscaEntrySchema this.getClass(), ValidationResult.INVALID, "entry schema description may not be blank")); } - if (constraints != null) { for (JpaToscaConstraint constraint : constraints) { if (constraint == null) { diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java index b500a8bc9..b5da49751 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicy.java @@ -88,7 +88,7 @@ public class JpaToscaPolicy extends JpaToscaEntityType<ToscaPolicy> implements P @ElementCollection @Lob - private Map<String, String> properties; + private Map<String, String> properties = new LinkedHashMap<>(); @ElementCollection private List<PfConceptKey> targets = new ArrayList<>(); diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaProperty.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaProperty.java index 0e8201f0f..a2127f37c 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaProperty.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaProperty.java @@ -3,7 +3,7 @@ * ONAP Policy Model * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +28,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; + import javax.persistence.Column; import javax.persistence.ElementCollection; import javax.persistence.EmbeddedId; @@ -35,9 +36,11 @@ import javax.persistence.Entity; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.Table; + import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; + import org.apache.commons.lang3.ObjectUtils; import org.onap.policy.models.base.PfAuthorative; import org.onap.policy.models.base.PfConcept; @@ -85,13 +88,13 @@ public class JpaToscaProperty extends PfConcept implements PfAuthorative<ToscaPr private Status status = Status.SUPPORTED; @ElementCollection - private List<JpaToscaConstraint> constraints; + private List<JpaToscaConstraint> constraints = new ArrayList<>(); @Column private JpaToscaEntrySchema entrySchema; @ElementCollection - private Map<String, String> metadata; + private Map<String, String> metadata = new LinkedHashMap<>(); /** * The Default Constructor creates a {@link JpaToscaProperty} object with a null 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 c537bbcb5..a3e18ca41 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 @@ -37,8 +37,10 @@ import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.dao.PfDao; +import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity; import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntityType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType; @@ -58,8 +60,11 @@ public class SimpleToscaProvider { private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class); // Recurring string constants + private static final String DATA_TYPE = "data type "; + private static final String POLICY_TYPE = "policy type "; private static final String SERVICE_TEMPLATE_NOT_FOUND_IN_DATABASE = "service template not found in database"; private static final String DO_NOT_EXIST = " do not exist"; + private static final String NOT_FOUND = " not found"; /** * Get Service Template. @@ -226,7 +231,32 @@ public class SimpleToscaProvider { throws PfModelException { LOGGER.debug("->deleteDataType: key={}", dataTypeKey); - JpaToscaServiceTemplate serviceTemplate = getDataTypes(dao, dataTypeKey.getName(), dataTypeKey.getVersion()); + JpaToscaServiceTemplate serviceTemplate = getServiceTemplate(dao); + + if (!ToscaUtils.doDataTypesExist(serviceTemplate)) { + throw new PfModelRuntimeException(Response.Status.NOT_FOUND, "no data types found"); + } + + JpaToscaDataType dataType4Deletion = serviceTemplate.getDataTypes().get(dataTypeKey); + if (dataType4Deletion == null) { + throw new PfModelRuntimeException(Response.Status.NOT_FOUND, DATA_TYPE + dataTypeKey.getId() + NOT_FOUND); + } + + for (JpaToscaDataType dataType : serviceTemplate.getDataTypes().getAll(null)) { + if (dataType.getReferencedDataTypes().contains(dataTypeKey)) { + throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, DATA_TYPE + dataTypeKey.getId() + + " is in use, it is referenced in data type " + dataType.getId()); + } + } + + if (ToscaUtils.doPolicyTypesExist(serviceTemplate)) { + for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) { + if (policyType.getReferencedDataTypes().contains(dataTypeKey)) { + throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, DATA_TYPE + dataTypeKey.getId() + + " is in use, it is referenced in policy type " + policyType.getId()); + } + } + } dao.delete(JpaToscaDataType.class, dataTypeKey); @@ -355,8 +385,37 @@ public class SimpleToscaProvider { throws PfModelException { LOGGER.debug("->deletePolicyType: key={}", policyTypeKey); - JpaToscaServiceTemplate serviceTemplate = - getPolicyTypes(dao, policyTypeKey.getName(), policyTypeKey.getVersion()); + JpaToscaServiceTemplate serviceTemplate = getServiceTemplate(dao); + + if (!ToscaUtils.doPolicyTypesExist(serviceTemplate)) { + throw new PfModelRuntimeException(Response.Status.NOT_FOUND, "no policy types found"); + } + + JpaToscaEntityType<? extends ToscaEntity> policyType4Deletion = + serviceTemplate.getPolicyTypes().get(policyTypeKey); + if (policyType4Deletion == null) { + throw new PfModelRuntimeException(Response.Status.NOT_FOUND, + POLICY_TYPE + policyTypeKey.getId() + NOT_FOUND); + } + + for (JpaToscaPolicyType policyType : serviceTemplate.getPolicyTypes().getAll(null)) { + Collection<JpaToscaEntityType<ToscaEntity>> ancestorList = ToscaUtils + .getEntityTypeAncestors(serviceTemplate.getPolicyTypes(), policyType, new PfValidationResult()); + + if (ancestorList.contains(policyType4Deletion)) { + throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, POLICY_TYPE + policyTypeKey.getId() + + " is in use, it is referenced in policy type " + policyType.getId()); + } + } + + if (ToscaUtils.doPoliciesExist(serviceTemplate)) { + for (JpaToscaPolicy policy : serviceTemplate.getTopologyTemplate().getPolicies().getAll(null)) { + if (policyTypeKey.equals(policy.getType())) { + throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, POLICY_TYPE + + policyTypeKey.getId() + " is in use, it is referenced in policy " + policy.getId()); + } + } + } dao.delete(JpaToscaPolicyType.class, policyTypeKey); @@ -476,7 +535,16 @@ public class SimpleToscaProvider { throws PfModelException { LOGGER.debug("->deletePolicy: key={}", policyKey); - JpaToscaServiceTemplate serviceTemplate = getPolicies(dao, policyKey.getName(), policyKey.getVersion()); + JpaToscaServiceTemplate serviceTemplate = getServiceTemplate(dao); + + if (!ToscaUtils.doPoliciesExist(serviceTemplate)) { + throw new PfModelRuntimeException(Response.Status.NOT_FOUND, "no policies found"); + } + + JpaToscaPolicy policy4Deletion = serviceTemplate.getTopologyTemplate().getPolicies().get(policyKey); + if (policy4Deletion == null) { + throw new PfModelRuntimeException(Response.Status.NOT_FOUND, "policy " + policyKey.getId() + NOT_FOUND); + } dao.delete(JpaToscaPolicy.class, policyKey); @@ -507,7 +575,7 @@ public class SimpleToscaProvider { if (policyType == null) { String errorMessage = - "policy type " + policyTypeKey.getId() + " for policy " + policy.getId() + " does not exist"; + POLICY_TYPE + policyTypeKey.getId() + " for policy " + policy.getId() + " does not exist"; throw new PfModelRuntimeException(Response.Status.NOT_ACCEPTABLE, errorMessage); } } 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 index b75273e5e..77633bd27 100644 --- 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 @@ -211,7 +211,7 @@ public final class ToscaUtils { } /** - * Find all the ancestors of an entity type. + * getLatestPolicyTypeVersion Find all the ancestors of an entity type. * * @param entityTypes the set of entity types that exist * @param entityType the entity type for which to get the parents @@ -227,6 +227,12 @@ public final class ToscaUtils { return CollectionUtils.emptyCollection(); } + if (entityType.getKey().equals(parentEntityTypeKey)) { + result.addValidationMessage(new PfValidationMessage(entityType.getKey(), ToscaUtils.class, + ValidationResult.INVALID, "entity cannot be an ancestor of itself")); + throw new PfModelRuntimeException(Response.Status.CONFLICT, result.toString()); + } + @SuppressWarnings("unchecked") Set<JpaToscaEntityType<ToscaEntity>> ancestorEntitySet = (Set<JpaToscaEntityType<ToscaEntity>>) entityTypes .getAll(parentEntityTypeKey.getName(), parentEntityTypeKey.getVersion()); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapperTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapperTest.java index 62c088c83..332552a73 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapperTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapperTest.java @@ -50,7 +50,13 @@ public class LegacyGuardPolicyMapperTest { JpaToscaPolicy policy = new JpaToscaPolicy(new PfConceptKey("PolicyName", "2.0.0")); serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policy.getKey(), policy); + policy.setMetadata(null); + assertThatThrownBy(() -> { + new LegacyGuardPolicyMapper().fromToscaServiceTemplate(serviceTemplate); + }).hasMessageContaining("no metadata defined on TOSCA policy"); + policy.setMetadata(new LinkedHashMap<>()); + policy.setProperties(null); assertThatThrownBy(() -> { new LegacyGuardPolicyMapper().fromToscaServiceTemplate(serviceTemplate); }).hasMessageContaining("no properties defined on TOSCA policy"); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapperTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapperTest.java index 4dcfeafc9..79de6f9d9 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapperTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapperTest.java @@ -106,6 +106,7 @@ public class LegacyOperationalPolicyMapperTest { serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().remove(policy1.getKey()); + policy0.setProperties(null); assertThatThrownBy(() -> { new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate); }).hasMessage("no properties defined on TOSCA policy"); 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 index f34c0c869..4937c5cca 100644 --- 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 @@ -36,6 +36,7 @@ import org.onap.policy.common.utils.coder.StandardCoder; 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.base.PfReferenceKey; import org.onap.policy.models.dao.DaoParameters; import org.onap.policy.models.dao.PfDao; import org.onap.policy.models.dao.PfDaoFactory; @@ -45,8 +46,10 @@ import org.onap.policy.models.tosca.authorative.provider.AuthorativeToscaProvide import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty; import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate; import org.yaml.snakeyaml.Yaml; @@ -135,13 +138,64 @@ public class SimpleToscaProviderTest { assertEquals(dataType0, gotServiceTemplate.getDataTypes().get(dataType0Key)); assertEquals("Updated Description", gotServiceTemplate.getDataTypes().get(dataType0Key).getDescription()); + assertThatThrownBy(() -> new SimpleToscaProvider().deleteDataType(pfDao, new PfConceptKey("IDontExist:0.0.1"))) + .hasMessage("data type IDontExist:0.0.1 not found"); + JpaToscaServiceTemplate deletedServiceTemplate = new SimpleToscaProvider().deleteDataType(pfDao, dataType0Key); assertEquals(dataType0, deletedServiceTemplate.getDataTypes().get(dataType0Key)); assertEquals("Updated Description", deletedServiceTemplate.getDataTypes().get(dataType0Key).getDescription()); + // Create the data type again + new SimpleToscaProvider().createDataTypes(pfDao, serviceTemplate); + + updatedServiceTemplate.setPolicyTypes(new JpaToscaPolicyTypes()); + JpaToscaPolicyType pt0 = new JpaToscaPolicyType(new PfConceptKey("pt0:0.0.1")); + updatedServiceTemplate.getPolicyTypes().getConceptMap().put(pt0.getKey(), pt0); + new SimpleToscaProvider().createPolicyTypes(pfDao, updatedServiceTemplate); + + deletedServiceTemplate = new SimpleToscaProvider().deleteDataType(pfDao, dataType0Key); + + assertEquals(dataType0, deletedServiceTemplate.getDataTypes().get(dataType0Key)); + assertEquals("Updated Description", deletedServiceTemplate.getDataTypes().get(dataType0Key).getDescription()); + assertThatThrownBy(() -> new SimpleToscaProvider().deleteDataType(pfDao, dataType0Key)) - .hasMessage("data types for DataType0:0.0.1 do not exist"); + .hasMessage("no data types found"); + + // Create the data type again + new SimpleToscaProvider().createDataTypes(pfDao, serviceTemplate); + + JpaToscaPolicyType pt0v2 = new JpaToscaPolicyType(new PfConceptKey("pt0:0.0.2")); + JpaToscaProperty prop0 = new JpaToscaProperty(new PfReferenceKey(pt0v2.getKey(), "prop0")); + prop0.setType(dataType0Key); + pt0v2.getProperties().put(prop0.getKey().getLocalName(), prop0); + updatedServiceTemplate.getPolicyTypes().getConceptMap().put(pt0v2.getKey(), pt0v2); + new SimpleToscaProvider().createPolicyTypes(pfDao, updatedServiceTemplate); + + assertThatThrownBy(() -> new SimpleToscaProvider().deleteDataType(pfDao, dataType0Key)) + .hasMessage("data type DataType0:0.0.1 is in use, it is referenced in policy type pt0:0.0.2"); + + JpaToscaDataType dataType0v2 = new JpaToscaDataType(new PfConceptKey("DataType0:0.0.2")); + updatedServiceTemplate.getDataTypes().getConceptMap().put(dataType0v2.getKey(), dataType0v2); + new SimpleToscaProvider().createDataTypes(pfDao, updatedServiceTemplate); + + deletedServiceTemplate = new SimpleToscaProvider().deleteDataType(pfDao, dataType0v2.getKey()); + + assertEquals(dataType0, deletedServiceTemplate.getDataTypes().get(dataType0Key)); + assertEquals("Updated Description", deletedServiceTemplate.getDataTypes().get(dataType0Key).getDescription()); + + assertThatThrownBy(() -> new SimpleToscaProvider().deleteDataType(pfDao, dataType0Key)) + .hasMessage("data type DataType0:0.0.1 is in use, it is referenced in policy type pt0:0.0.2"); + + JpaToscaDataType dataType1 = new JpaToscaDataType(new PfConceptKey("DataType1:0.0.3")); + JpaToscaProperty prop1 = new JpaToscaProperty(new PfReferenceKey(dataType1.getKey(), "prop1")); + prop1.setType(dataType0v2.getKey()); + dataType1.getProperties().put(prop1.getKey().getLocalName(), prop1); + updatedServiceTemplate.getDataTypes().getConceptMap().put(dataType1.getKey(), dataType1); + new SimpleToscaProvider().createDataTypes(pfDao, updatedServiceTemplate); + + assertThatThrownBy(() -> new SimpleToscaProvider().deleteDataType(pfDao, dataType0v2.getKey())) + .hasMessage("data type DataType0:0.0.2 is in use, it is referenced in data type DataType1:0.0.3"); } @Test @@ -182,6 +236,42 @@ public class SimpleToscaProviderTest { assertEquals(policyType0, gotServiceTemplate.getPolicyTypes().get(policyType0Key)); assertEquals("Updated Description", gotServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription()); + assertThatThrownBy(() -> { + new SimpleToscaProvider().deletePolicyType(pfDao, new PfConceptKey("IDontExist:0.0.1")); + }).hasMessage("policy type IDontExist:0.0.1 not found"); + + JpaToscaPolicyType pt1 = new JpaToscaPolicyType(new PfConceptKey("pt1:0.0.2")); + pt1.setDerivedFrom(policyType0Key); + serviceTemplate.getPolicyTypes().getConceptMap().put(pt1.getKey(), pt1); + new SimpleToscaProvider().createPolicyTypes(pfDao, serviceTemplate); + + assertThatThrownBy(() -> new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key)) + .hasMessage("policy type PolicyType0:0.0.1 is in use, it is referenced in policy type pt1:0.0.2"); + + serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); + serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); + + JpaToscaPolicy p0 = new JpaToscaPolicy(new PfConceptKey("p0:0.0.1")); + p0.setType(policyType0Key); + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(p0.getKey(), p0); + + JpaToscaPolicy p1 = new JpaToscaPolicy(new PfConceptKey("p1:0.0.1")); + p1.setType(pt1.getKey()); + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(p1.getKey(), p1); + new SimpleToscaProvider().createPolicies(pfDao, serviceTemplate); + + assertThatThrownBy(() -> new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key)) + .hasMessage("policy type PolicyType0:0.0.1 is in use, it is referenced in policy type pt1:0.0.2"); + + assertThatThrownBy(() -> new SimpleToscaProvider().deletePolicyType(pfDao, pt1.getKey())) + .hasMessage("policy type pt1:0.0.2 is in use, it is referenced in policy p1:0.0.1"); + + new SimpleToscaProvider().deletePolicy(pfDao, p1.getKey()); + + new SimpleToscaProvider().deletePolicyType(pfDao, pt1.getKey()); + + new SimpleToscaProvider().deletePolicy(pfDao, p0.getKey()); + JpaToscaServiceTemplate deletedServiceTemplate = new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key); @@ -190,7 +280,7 @@ public class SimpleToscaProviderTest { deletedServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription()); assertThatThrownBy(() -> new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key)) - .hasMessage("policy types for PolicyType0:0.0.1 do not exist"); + .hasMessage("no policy types found"); } @Test @@ -232,7 +322,7 @@ public class SimpleToscaProviderTest { deletedServiceTemplate.getPolicyTypes().get(policyType0Key).getDescription()); assertThatThrownBy(() -> new SimpleToscaProvider().deletePolicyType(pfDao, policyType0Key)) - .hasMessage("policy types for PolicyType0:0.0.1 do not exist"); + .hasMessage("no policy types found"); } @Test @@ -259,6 +349,9 @@ public class SimpleToscaProviderTest { assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey), gotServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey)); + + JpaToscaServiceTemplate deletedServiceTemplate = new SimpleToscaProvider().deletePolicy(pfDao, policyKey); + assertEquals(1, deletedServiceTemplate.getTopologyTemplate().getPolicies().getConceptMap().size()); } @Test @@ -314,17 +407,20 @@ public class SimpleToscaProviderTest { PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); - JpaToscaServiceTemplate deletedServiceTemplate = - new SimpleToscaProvider().deletePolicy(pfDao, new PfConceptKey(policyKey)); + assertThatThrownBy(() -> new SimpleToscaProvider().deletePolicy(pfDao, new PfConceptKey("IDontExist:0.0.1"))) + .hasMessage("policy IDontExist:0.0.1 not found"); + + JpaToscaServiceTemplate deletedServiceTemplate = new SimpleToscaProvider().deletePolicy(pfDao, policyKey); assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey), deletedServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey)); - // @formatter:off - assertThatThrownBy( - () -> new SimpleToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion())) - .hasMessage("policies for onap.restart.tca:1.0.0 do not exist"); - // @formatter:on + assertThatThrownBy(() -> { + new SimpleToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion()); + }).hasMessage("policies for onap.restart.tca:1.0.0 do not exist"); + + assertThatThrownBy(() -> new SimpleToscaProvider().deletePolicy(pfDao, policyKey)) + .hasMessage("no policies found"); } @Test @@ -344,8 +440,219 @@ public class SimpleToscaProviderTest { } @Test + public void testGetServiceTemplate() throws PfModelException { + assertThatThrownBy(() -> new SimpleToscaProvider().getServiceTemplate(pfDao)) + .hasMessage("service template not found in database"); + } + + @Test + public void testAppendToServiceTemplate() throws PfModelException { + JpaToscaServiceTemplate serviceTemplateFragment = new JpaToscaServiceTemplate(); + serviceTemplateFragment.setPolicyTypes(new JpaToscaPolicyTypes()); + JpaToscaPolicyType badPt = new JpaToscaPolicyType(); + serviceTemplateFragment.getPolicyTypes().getConceptMap().put(badPt.getKey(), badPt); + + assertThatThrownBy(() -> new SimpleToscaProvider().appendToServiceTemplate(pfDao, serviceTemplateFragment)) + .hasMessageContaining( + "key on concept entry PfConceptKey(name=NULL, version=0.0.0) may not be the null key"); + } + + @Test + public void testGetDataTypesCornerCases() throws PfModelException { + assertThatThrownBy(() -> { + new SimpleToscaProvider().getDataTypes(pfDao, "hello", "0.0.1"); + }).hasMessageMatching("service template not found in database"); + + JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); + serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes()); + JpaToscaPolicyType p0 = new JpaToscaPolicyType(new PfConceptKey("p0:0.0.1")); + serviceTemplate.getPolicyTypes().getConceptMap().put(p0.getKey(), p0); + + new SimpleToscaProvider().createPolicyTypes(pfDao, serviceTemplate); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().getDataTypes(pfDao, "hello", "0.0.1"); + }).hasMessageMatching("data types for hello:0.0.1 do not exist"); + + serviceTemplate.setDataTypes(new JpaToscaDataTypes()); + + JpaToscaDataType p01 = new JpaToscaDataType(new PfConceptKey("dt0:0.0.1")); + serviceTemplate.getDataTypes().getConceptMap().put(p01.getKey(), p01); + + new SimpleToscaProvider().createDataTypes(pfDao, serviceTemplate); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().getDataTypes(pfDao, "hello", "0.0.1"); + }).hasMessageMatching("data types for hello:0.0.1 do not exist"); + + JpaToscaServiceTemplate gotSt = new SimpleToscaProvider().getDataTypes(pfDao, p01.getName(), p01.getVersion()); + + assertEquals(p01, gotSt.getDataTypes().get(p01.getKey())); + assertEquals(p01, gotSt.getDataTypes().get(p01.getName())); + assertEquals(p01, gotSt.getDataTypes().get(p01.getName(), null)); + assertEquals(p01, gotSt.getDataTypes().get(p01.getName(), p01.getVersion())); + assertEquals(1, gotSt.getDataTypes().getAll(null).size()); + assertEquals(1, gotSt.getDataTypes().getAll(null, null).size()); + assertEquals(1, gotSt.getDataTypes().getAll(p01.getName(), null).size()); + assertEquals(1, gotSt.getDataTypes().getAll(p01.getName(), p01.getVersion()).size()); + + JpaToscaDataType p02 = new JpaToscaDataType(new PfConceptKey("dt0:0.0.2")); + serviceTemplate.getDataTypes().getConceptMap().put(p02.getKey(), p02); + + new SimpleToscaProvider().createDataTypes(pfDao, serviceTemplate); + gotSt = new SimpleToscaProvider().getDataTypes(pfDao, p01.getName(), p01.getVersion()); + + assertEquals(p01, gotSt.getDataTypes().get(p01.getKey())); + assertEquals(p02, gotSt.getDataTypes().get(p01.getName())); + assertEquals(p02, gotSt.getDataTypes().get(p01.getName(), null)); + assertEquals(p01, gotSt.getDataTypes().get(p01.getName(), p01.getVersion())); + assertEquals(p02, gotSt.getDataTypes().get(p01.getName(), p02.getVersion())); + assertEquals(2, gotSt.getDataTypes().getAll(null).size()); + assertEquals(2, gotSt.getDataTypes().getAll(null, null).size()); + assertEquals(2, gotSt.getDataTypes().getAll(p01.getName(), null).size()); + assertEquals(1, gotSt.getDataTypes().getAll(p01.getName(), p02.getVersion()).size()); + } + + @Test + public void testGetPolicyTypesCornerCases() throws PfModelException { + assertThatThrownBy(() -> { + new SimpleToscaProvider().getPolicyTypes(pfDao, "hello", "0.0.1"); + }).hasMessageMatching("service template not found in database"); + + JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); + serviceTemplate.setDataTypes(new JpaToscaDataTypes()); + JpaToscaDataType dt0 = new JpaToscaDataType(new PfConceptKey("dt0:0.0.1")); + serviceTemplate.getDataTypes().getConceptMap().put(dt0.getKey(), dt0); + + new SimpleToscaProvider().createDataTypes(pfDao, serviceTemplate); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().getPolicyTypes(pfDao, "hello", "0.0.1"); + }).hasMessageMatching("policy types for hello:0.0.1 do not exist"); + + serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes()); + + JpaToscaPolicyType pt01 = new JpaToscaPolicyType(new PfConceptKey("p0:0.0.1")); + serviceTemplate.getPolicyTypes().getConceptMap().put(pt01.getKey(), pt01); + + new SimpleToscaProvider().createPolicyTypes(pfDao, serviceTemplate); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().getPolicyTypes(pfDao, "hello", "0.0.1"); + }).hasMessageMatching("policy types for hello:0.0.1 do not exist"); + + JpaToscaServiceTemplate gotSt = + new SimpleToscaProvider().getPolicyTypes(pfDao, pt01.getName(), pt01.getVersion()); + + assertEquals(pt01, gotSt.getPolicyTypes().get(pt01.getKey())); + assertEquals(pt01, gotSt.getPolicyTypes().get(pt01.getName())); + assertEquals(pt01, gotSt.getPolicyTypes().get(pt01.getName(), null)); + assertEquals(pt01, gotSt.getPolicyTypes().get(pt01.getName(), pt01.getVersion())); + assertEquals(1, gotSt.getPolicyTypes().getAll(null).size()); + assertEquals(1, gotSt.getPolicyTypes().getAll(null, null).size()); + assertEquals(1, gotSt.getPolicyTypes().getAll(pt01.getName(), null).size()); + assertEquals(1, gotSt.getPolicyTypes().getAll(pt01.getName(), pt01.getVersion()).size()); + + JpaToscaPolicyType pt02 = new JpaToscaPolicyType(new PfConceptKey("p0:0.0.2")); + serviceTemplate.getPolicyTypes().getConceptMap().put(pt02.getKey(), pt02); + + new SimpleToscaProvider().createPolicyTypes(pfDao, serviceTemplate); + gotSt = new SimpleToscaProvider().getPolicyTypes(pfDao, pt01.getName(), pt01.getVersion()); + + assertEquals(pt01, gotSt.getPolicyTypes().get(pt01.getKey())); + assertEquals(pt02, gotSt.getPolicyTypes().get(pt01.getName())); + assertEquals(pt02, gotSt.getPolicyTypes().get(pt01.getName(), null)); + assertEquals(pt01, gotSt.getPolicyTypes().get(pt01.getName(), pt01.getVersion())); + assertEquals(pt02, gotSt.getPolicyTypes().get(pt01.getName(), pt02.getVersion())); + assertEquals(2, gotSt.getPolicyTypes().getAll(null).size()); + assertEquals(2, gotSt.getPolicyTypes().getAll(null, null).size()); + assertEquals(2, gotSt.getPolicyTypes().getAll(pt01.getName(), null).size()); + assertEquals(1, gotSt.getPolicyTypes().getAll(pt01.getName(), pt02.getVersion()).size()); + } + + @Test + public void testGetPoliciesCornerCases() throws PfModelException { + assertThatThrownBy(() -> { + new SimpleToscaProvider().getPolicies(pfDao, "hello", "0.0.1"); + }).hasMessageMatching("service template not found in database"); + + JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); + serviceTemplate.setDataTypes(new JpaToscaDataTypes()); + JpaToscaDataType dt0 = new JpaToscaDataType(new PfConceptKey("dt0:0.0.1")); + serviceTemplate.getDataTypes().getConceptMap().put(dt0.getKey(), dt0); + + new SimpleToscaProvider().createDataTypes(pfDao, serviceTemplate); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().getPolicies(pfDao, "hello", "0.0.1"); + }).hasMessageMatching("policies for hello:0.0.1 do not exist"); + + serviceTemplate.setPolicyTypes(new JpaToscaPolicyTypes()); + + JpaToscaPolicyType pt01 = new JpaToscaPolicyType(new PfConceptKey("pt0:0.0.1")); + serviceTemplate.getPolicyTypes().getConceptMap().put(pt01.getKey(), pt01); + + serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); + serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); + + JpaToscaPolicy p01 = new JpaToscaPolicy(new PfConceptKey("p0:0.0.1")); + p01.setType(pt01.getKey()); + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(p01.getKey(), p01); + + new SimpleToscaProvider().createPolicies(pfDao, serviceTemplate); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().getPolicies(pfDao, "hello", "0.0.1"); + }).hasMessageMatching("policies for hello:0.0.1 do not exist"); + + JpaToscaServiceTemplate gotSt = new SimpleToscaProvider().getPolicies(pfDao, p01.getName(), p01.getVersion()); + + assertEquals(p01, gotSt.getTopologyTemplate().getPolicies().get(p01.getKey())); + assertEquals(p01, gotSt.getTopologyTemplate().getPolicies().get(p01.getName())); + assertEquals(p01, gotSt.getTopologyTemplate().getPolicies().get(p01.getName(), null)); + assertEquals(p01, gotSt.getTopologyTemplate().getPolicies().get(p01.getName(), p01.getVersion())); + assertEquals(1, gotSt.getTopologyTemplate().getPolicies().getAll(null).size()); + assertEquals(1, gotSt.getTopologyTemplate().getPolicies().getAll(null, null).size()); + assertEquals(1, gotSt.getTopologyTemplate().getPolicies().getAll(p01.getName(), null).size()); + assertEquals(1, gotSt.getTopologyTemplate().getPolicies().getAll(p01.getName(), p01.getVersion()).size()); + + JpaToscaPolicy p02 = new JpaToscaPolicy(new PfConceptKey("p0:0.0.2")); + p02.setType(pt01.getKey()); + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(p02.getKey(), p02); + + new SimpleToscaProvider().createPolicies(pfDao, serviceTemplate); + gotSt = new SimpleToscaProvider().getPolicies(pfDao, p01.getName(), p01.getVersion()); + + assertEquals(p01, gotSt.getTopologyTemplate().getPolicies().get(p01.getKey())); + assertEquals(p02, gotSt.getTopologyTemplate().getPolicies().get(p01.getName())); + assertEquals(p02, gotSt.getTopologyTemplate().getPolicies().get(p01.getName(), null)); + assertEquals(p01, gotSt.getTopologyTemplate().getPolicies().get(p01.getName(), p01.getVersion())); + assertEquals(p02, gotSt.getTopologyTemplate().getPolicies().get(p01.getName(), p02.getVersion())); + assertEquals(2, gotSt.getTopologyTemplate().getPolicies().getAll(null).size()); + assertEquals(2, gotSt.getTopologyTemplate().getPolicies().getAll(null, null).size()); + assertEquals(2, gotSt.getTopologyTemplate().getPolicies().getAll(p01.getName(), null).size()); + assertEquals(1, gotSt.getTopologyTemplate().getPolicies().getAll(p01.getName(), p02.getVersion()).size()); + } + + @Test public void testNonNulls() { assertThatThrownBy(() -> { + new SimpleToscaProvider().getServiceTemplate(null); + }).hasMessageMatching(DAO_IS_NULL); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().appendToServiceTemplate(null, null); + }).hasMessageMatching(DAO_IS_NULL); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().appendToServiceTemplate(null, new JpaToscaServiceTemplate()); + }).hasMessageMatching(DAO_IS_NULL); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().appendToServiceTemplate(pfDao, null); + }).hasMessageMatching("^incomingServiceTemplateFragment is marked .*on.*ull but is null$"); + + assertThatThrownBy(() -> { new SimpleToscaProvider().getDataTypes(null, null, null); }).hasMessageMatching(DAO_IS_NULL); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicyTypeSerializationTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicyTypeSerializationTest.java index 0a8283e98..270bc6c77 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicyTypeSerializationTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/MonitoringPolicyTypeSerializationTest.java @@ -199,8 +199,9 @@ public class MonitoringPolicyTypeSerializationTest { assertTrue(firstDataTypeFirstProperty.getConstraints().size() == 1); assertEquals("org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintValidValues", firstDataTypeFirstProperty.getConstraints().iterator().next().getClass().getName()); - assertTrue(((JpaToscaConstraintValidValues) (firstDataTypeFirstProperty.getConstraints().iterator().next())) - .getValidValues().size() == 2); + assertEquals(2, + ((JpaToscaConstraintValidValues) (firstDataTypeFirstProperty.getConstraints().iterator().next())) + .getValidValues().size()); JpaToscaProperty firstDataTypeSecondProperty = firstDataTypePropertiesIter.next(); assertEquals(METRICS, firstDataTypeSecondProperty.getKey().getParentKeyName()); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/OptimizationPolicyTypeSerializationTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/OptimizationPolicyTypeSerializationTest.java index e710faa31..fb258a215 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/OptimizationPolicyTypeSerializationTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/serialization/OptimizationPolicyTypeSerializationTest.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,11 +21,11 @@ package org.onap.policy.models.tosca.simple.serialization; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import java.util.List; import java.util.Map; + import org.junit.Before; import org.junit.Test; import org.onap.policy.common.utils.coder.CoderException; @@ -115,8 +116,8 @@ public class OptimizationPolicyTypeSerializationTest { return coder.encode(auth); } - private void validate(String testnm, JpaToscaServiceTemplate svctmpl, String derivedFrom, - String typeName, boolean checkResource, boolean checkService) { + private void validate(String testnm, JpaToscaServiceTemplate svctmpl, String derivedFrom, String typeName, + boolean checkResource, boolean checkService) { JpaToscaPolicyTypes policyTypes = svctmpl.getPolicyTypes(); assertEquals(testnm + " type count", 1, policyTypes.getConceptMap().size()); @@ -194,7 +195,7 @@ public class OptimizationPolicyTypeSerializationTest { String testnm = testName + " identity"; assertNotNull(testnm, prop); - assertNull(testnm + " metadata", prop.getMetadata()); + assertEquals(testnm + " metadata", 0, prop.getMetadata().size()); } private void validateMatchable(String testName, Map<String, String> metadata) { diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java index a5d145e39..d75e37be2 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java @@ -231,6 +231,14 @@ public class ToscaUtilsTest { assertEquals(2, ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).size()); assertTrue(result.isValid()); + dt0.setDerivedFrom(dt0.getKey()); + assertThatThrownBy(() -> { + ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, new PfValidationResult()); + }).hasMessageContaining("entity cannot be an ancestor of itself"); + + dt0.setDerivedFrom(null); + assertEquals(2, ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).size()); + dt1.setDerivedFrom(new PfConceptKey("tosca.datatyps.Root", PfKey.NULL_KEY_VERSION)); assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty()); assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).isEmpty()); |