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/test/java | |
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/test/java')
6 files changed, 340 insertions, 16 deletions
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()); |