From 24400f789d2a20da08793fa8d5ac046caf46c267 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Wed, 10 Apr 2019 11:34:06 +0000 Subject: Finish unit test on policy-models Unit test coverage well over 90% in policy-models-base policy-models-dao policy-models-pdp policy-models-tosca policy-models-provider Issue-ID: POLICY-1095 Change-Id: I7703e2ae8a93575ca478c3d809ff8c1fb9f0f334 Signed-off-by: liamfallon --- .../provider/AuthorativeToscaProvider.java | 4 - .../legacy/mapping/LegacyGuardPolicyMapper.java | 6 - .../mapping/LegacyOperationalPolicyMapper.java | 4 +- .../tosca/simple/concepts/JpaToscaConstraint.java | 4 +- .../simple/concepts/JpaToscaConstraintLogical.java | 27 +- .../tosca/simple/concepts/JpaToscaPolicy.java | 6 +- .../tosca/simple/provider/SimpleToscaProvider.java | 28 +- .../AuthorativeToscaProviderPolicyTest.java | 4 +- .../AuthorativeToscaProviderPolicyTypeTest.java | 341 +++++++++++---------- .../legacy/concepts/LegacyGuardPolicyTest.java | 8 +- .../models/tosca/legacy/concepts/TestPojos.java | 18 +- .../DummyBadLegacyGuardPolicyContent.java | 34 ++ .../mapping/LegacyGuardPolicyMapperTest.java | 52 ++++ .../mapping/LegacyOperationalPolicyMapperTest.java | 105 +++++++ .../LegacyOperationalPolicySerializationTest.java | 71 ----- .../concepts/JpaToscaConstraintLogicalTest.java | 61 ++-- .../simple/concepts/JpaToscaConstraintTest.java | 88 ++++++ .../simple/concepts/JpaToscaDataTypeTest.java | 49 +-- .../simple/concepts/JpaToscaDataTypesTest.java | 10 + .../simple/concepts/JpaToscaPoliciesTest.java | 10 + .../tosca/simple/concepts/JpaToscaPolicyTest.java | 86 +++--- .../simple/concepts/JpaToscaPolicyTypeTest.java | 5 + .../simple/concepts/JpaToscaPolicyTypesTest.java | 10 + .../simple/concepts/JpaToscaPropertyTest.java | 2 + .../concepts/JpaToscaServiceTemplatesTest.java | 48 ++- .../concepts/JpaToscaTopologyTemplateTest.java | 7 + .../models/tosca/simple/concepts/TestPojos.java | 1 - .../testconcepts/DummyToscaConstraint.java | 5 + .../simple/provider/SimpleToscaProviderTest.java | 153 ++++----- .../policy/models/tosca/utils/ToscaUtilsTest.java | 45 +++ 30 files changed, 816 insertions(+), 476 deletions(-) create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/testconcepts/DummyBadLegacyGuardPolicyContent.java create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapperTest.java create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapperTest.java delete mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/serialization/LegacyOperationalPolicySerializationTest.java create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintTest.java create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java (limited to 'models-tosca/src') diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java index 4bf014644..46f920cf4 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProvider.java @@ -276,10 +276,6 @@ public class AuthorativeToscaProvider { * @return the plain list */ private List asConceptList(final List> listOfMaps) { - if (listOfMaps == null) { - return Collections.emptyList(); - } - List returnList = new ArrayList<>(); for (Map conceptMap : listOfMaps) { for (T concept : conceptMap.values()) { diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java index cc37338e4..85b5e6df8 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapper.java @@ -139,12 +139,6 @@ public class LegacyGuardPolicyMapper propertiesMap.put("content", content); legacyGuardPolicyOutput.setProperties(propertiesMap); - if (toscaPolicy.getProperties() == null) { - String errorMessage = "property \"Content\" not defined on TOSCA policy"; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); - } - legacyGuardPolicyOutputMap.put(toscaPolicy.getKey().getName(), legacyGuardPolicyOutput); } 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 b6c5d3bba..7caba98d8 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 @@ -106,8 +106,8 @@ public class LegacyOperationalPolicyMapper } final String content = toscaPolicy.getProperties().get(CONTENT_PROPERTY); - if (toscaPolicy.getProperties() == null) { - String errorMessage = "property \"Content\" not defined on TOSCA policy"; + if (content == null) { + String errorMessage = "property \"content\" not defined on TOSCA policy"; 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/concepts/JpaToscaConstraint.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraint.java index fad227c34..6369f7996 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraint.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraint.java @@ -53,9 +53,7 @@ public abstract class JpaToscaConstraint } @Override - public int compareTo(JpaToscaConstraint otherConstraint) { - return 0; - } + public abstract int compareTo(JpaToscaConstraint otherConstraint); /** * Create instances of constraints of various types. diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogical.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogical.java index 9841cbe82..632f84add 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogical.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogical.java @@ -73,30 +73,33 @@ public class JpaToscaConstraintLogical extends JpaToscaConstraint { public ToscaConstraint toAuthorative() { ToscaConstraint toscaConstraint = new ToscaConstraint(); + if (operation == null) { + return null; + } + switch (operation) { - case EQ: { + case EQ: toscaConstraint.setEqual(compareTo); break; - } - case GT: { + + case GT: toscaConstraint.setGreaterThan(compareTo); break; - } - case GE: { + + case GE: toscaConstraint.setGreaterOrEqual(compareTo); break; - } - case LT: { + + case LT: toscaConstraint.setLessThan(compareTo); break; - } - case LE: { + + case LE: toscaConstraint.setLessOrEqual(compareTo); break; - } - default: { + + default: // Can't happen - } } return toscaConstraint; 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 671b5ccac..3bc472243 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 @@ -129,6 +129,8 @@ public class JpaToscaPolicy extends JpaToscaEntityType implements P * @param authorativeConcept the authorative concept to copy from */ public JpaToscaPolicy(final ToscaPolicy authorativeConcept) { + super(new PfConceptKey()); + type = new PfConceptKey(); this.fromAuthorative(authorativeConcept); } @@ -215,7 +217,7 @@ public class JpaToscaPolicy extends JpaToscaEntityType implements P PfValidationResult result = super.validate(resultIn); if (type == null || type.isNullKey()) { - result.addValidationMessage(new PfValidationMessage(type, this.getClass(), ValidationResult.INVALID, + result.addValidationMessage(new PfValidationMessage(getKey(), this.getClass(), ValidationResult.INVALID, "type is null or a null key")); } else { result = type.validate(result); @@ -238,7 +240,7 @@ public class JpaToscaPolicy extends JpaToscaEntityType implements P * @param result The result of validations up to now * @return the validation result */ - private PfValidationResult validateProperties(@NonNull final PfValidationResult resultIn) { + private PfValidationResult validateProperties(final PfValidationResult resultIn) { PfValidationResult result = resultIn; for (Entry propertyEntry : properties.entrySet()) { 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 a207c4267..ef8ac05ec 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 @@ -24,14 +24,11 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import javax.ws.rs.core.Response; - import lombok.NonNull; import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelException; -import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.dao.PfDao; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy; @@ -40,8 +37,6 @@ import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes; import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate; import org.onap.policy.models.tosca.utils.ToscaUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * This class provides the provision of information on TOSCA concepts in the database to callers. @@ -49,8 +44,6 @@ import org.slf4j.LoggerFactory; * @author Liam Fallon (liam.fallon@est.tech) */ public class SimpleToscaProvider { - private static final Logger LOGGER = LoggerFactory.getLogger(SimpleToscaProvider.class); - /** * Get policy types. * @@ -69,14 +62,9 @@ public class SimpleToscaProvider { // Add the policy type to the TOSCA service template List jpaPolicyTypeList = dao.getFiltered(JpaToscaPolicyType.class, name, version); - if (jpaPolicyTypeList != null) { - serviceTemplate.getPolicyTypes().getConceptMap().putAll(asConceptMap(jpaPolicyTypeList)); - return serviceTemplate; - } else { - String errorMessage = "policy type not found: " + name + ":" + version; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); - } + serviceTemplate.getPolicyTypes().getConceptMap().putAll(asConceptMap(jpaPolicyTypeList)); + + return serviceTemplate; } /** @@ -178,14 +166,8 @@ public class SimpleToscaProvider { // Add the policy type to the TOSCA service template List jpaPolicyList = dao.getFiltered(JpaToscaPolicy.class, name, version); - if (jpaPolicyList != null) { - serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().putAll(asConceptMap(jpaPolicyList)); - return serviceTemplate; - } else { - String errorMessage = "policy not found: " + name + ":" + version; - LOGGER.warn(errorMessage); - throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage); - } + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().putAll(asConceptMap(jpaPolicyList)); + return serviceTemplate; } /** diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java index 441bbcb18..a7d016bf3 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTest.java @@ -143,8 +143,10 @@ public class AuthorativeToscaProviderPolicyTest { gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, null, "1.0.0"); assertEquals(1, gotPolicyList.size()); assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); - } + gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, "Nonexistant", "1.0.0"); + assertEquals(0, gotPolicyList.size()); + } @Test public void testPoliciesGetFiltered() throws Exception { diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTypeTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTypeTest.java index fbb5abf62..7dc4a9497 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTypeTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/provider/AuthorativeToscaProviderPolicyTypeTest.java @@ -23,15 +23,18 @@ package org.onap.policy.models.tosca.authorative.provider; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; + +import com.google.gson.GsonBuilder; import java.sql.Connection; import java.sql.DriverManager; import java.util.ArrayList; import java.util.List; +import org.apache.commons.lang3.ObjectUtils; import org.junit.After; import org.junit.Before; +import org.junit.BeforeClass; import org.junit.Test; import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; @@ -41,10 +44,11 @@ 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.authorative.concepts.ToscaPolicy; -import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter; import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; +import org.yaml.snakeyaml.Yaml; /** * Test of the {@link AuthorativeToscaProvider} class. @@ -52,10 +56,26 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; * @author Liam Fallon (liam.fallon@est.tech) */ public class AuthorativeToscaProviderPolicyTypeTest { + private static String yamlAsJsonString; private Connection connection; private PfDao pfDao; private StandardCoder standardCoder; + + /** + * Read the policy type definition. + * + * @throws Exception on errors + */ + @BeforeClass + public static void readPolicyDefinition() { + String yamlString = + ResourceUtils.getResourceAsString("policytypes/onap.policies.optimization.AffinityPolicy.yaml"); + + Object yamlObject = new Yaml().load(yamlString); + yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject); + } + /** * Set up the DAO towards the database. * @@ -93,281 +113,272 @@ public class AuthorativeToscaProviderPolicyTypeTest { } @Test - public void testPoliciesGet() throws Exception { + public void testPolicyTypesGet() throws Exception { assertThatThrownBy(() -> { - new AuthorativeToscaProvider().getPolicies(null, null, null); + new AuthorativeToscaProvider().getPolicyTypes(null, null, null); }).hasMessage("dao is marked @NonNull but is null"); assertThatThrownBy(() -> { new AuthorativeToscaProvider().getPolicyList(null, null, null); }).hasMessage("dao is marked @NonNull but is null"); - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( - ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), - ToscaServiceTemplate.class); + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); assertNotNull(toscaServiceTemplate); ToscaServiceTemplate createdServiceTemplate = - new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); + new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate); - PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0"); - ToscaPolicy beforePolicy = - toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); - ToscaPolicy createdPolicy = - createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); - assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); - assertTrue(beforePolicy.getType().equals(createdPolicy.getType())); + ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); + ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName())); + assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); - ToscaServiceTemplate gotServiceTemplate = - new AuthorativeToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion()); + ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao, + policyTypeKey.getName(), policyTypeKey.getVersion()); - ToscaPolicy gotPolicy = - gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); - assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy)); - assertTrue(beforePolicy.getType().equals(gotPolicy.getType())); + ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); + assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); - List gotPolicyList = - new AuthorativeToscaProvider().getPolicyList(pfDao, "onap.restart.tca", "1.0.0"); - assertEquals(1, gotPolicyList.size()); - assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + List gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, + "onap.policies.optimization.AffinityPolicy", "0.0.0"); + assertEquals(1, gotPolicyTypeList.size()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); - gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, "onap.restart.tca", null); - assertEquals(1, gotPolicyList.size()); - assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, + "onap.policies.optimization.AffinityPolicy", null); + assertEquals(1, gotPolicyTypeList.size()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); - gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, null, null); - assertEquals(1, gotPolicyList.size()); - assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, null); + assertEquals(2, gotPolicyTypeList.size()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); - gotPolicyList = new AuthorativeToscaProvider().getPolicyList(pfDao, null, "1.0.0"); - assertEquals(1, gotPolicyList.size()); - assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, null, "0.0.0"); + assertEquals(2, gotPolicyTypeList.size()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); } @Test - public void testPoliciesGetFiltered() throws Exception { + public void testPolicyTypesGetFiltered() throws Exception { assertThatThrownBy(() -> { - new AuthorativeToscaProvider().getFilteredPolicies(null, null); + new AuthorativeToscaProvider().getFilteredPolicyTypes(null, null); }).hasMessage("dao is marked @NonNull but is null"); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().getFilteredPolicies(null, ToscaPolicyFilter.builder().build()); + new AuthorativeToscaProvider().getFilteredPolicyTypes(null, ToscaPolicyTypeFilter.builder().build()); }).hasMessage("dao is marked @NonNull but is null"); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().getFilteredPolicies(pfDao, null); + new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, null); }).hasMessage("filter is marked @NonNull but is null"); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().getFilteredPolicyList(null, null); + new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, null); }).hasMessage("dao is marked @NonNull but is null"); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().getFilteredPolicyList(null, ToscaPolicyFilter.builder().build()); + new AuthorativeToscaProvider().getFilteredPolicyTypeList(null, ToscaPolicyTypeFilter.builder().build()); }).hasMessage("dao is marked @NonNull but is null"); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, null); + new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, null); }).hasMessage("filter is marked @NonNull but is null"); - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( - ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), - ToscaServiceTemplate.class); + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); assertNotNull(toscaServiceTemplate); ToscaServiceTemplate createdServiceTemplate = - new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); + new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate); - PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0"); - ToscaPolicy beforePolicy = - toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); - ToscaPolicy createdPolicy = - createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); - assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); - assertTrue(beforePolicy.getType().equals(createdPolicy.getType())); + ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); + ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName())); + assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); ToscaServiceTemplate gotServiceTemplate = - new AuthorativeToscaProvider().getFilteredPolicies(pfDao, ToscaPolicyFilter.builder().build()); - - ToscaPolicy gotPolicy = - gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); - assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy)); - assertTrue(beforePolicy.getType().equals(gotPolicy.getType())); - - gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicies(pfDao, - ToscaPolicyFilter.builder().name(policyKey.getName()).build()); - - gotPolicy = gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); - assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy)); - assertTrue(beforePolicy.getType().equals(gotPolicy.getType())); - - gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicies(pfDao, - ToscaPolicyFilter.builder().name(policyKey.getName()).version("1.0.0").build()); - - gotPolicy = gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); - assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicy)); - assertTrue(beforePolicy.getType().equals(gotPolicy.getType())); - - List gotPolicyList = - new AuthorativeToscaProvider().getPolicyList(pfDao, "onap.restart.tca", "1.0.0"); - assertEquals(1, gotPolicyList.size()); - assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); - - gotPolicyList = - new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, ToscaPolicyFilter.builder().build()); - assertEquals(1, gotPolicyList.size()); - assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); - - gotPolicyList = new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, - ToscaPolicyFilter.builder().name(policyKey.getName()).build()); - assertEquals(1, gotPolicyList.size()); - assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); - - gotPolicyList = new AuthorativeToscaProvider().getFilteredPolicyList(pfDao, - ToscaPolicyFilter.builder().name(policyKey.getName()).version("1.0.0").build()); - assertEquals(1, gotPolicyList.size()); - assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, gotPolicyList.get(0))); + new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, ToscaPolicyTypeFilter.builder().build()); + + ToscaPolicyType gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); + assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription())); + + gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, + ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build()); + + gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); + assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription())); + + gotServiceTemplate = new AuthorativeToscaProvider().getFilteredPolicyTypes(pfDao, + ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version("0.0.0").build()); + + gotPolicyType = gotServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); + assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), gotPolicyType.getDescription())); + + List gotPolicyTypeList = new AuthorativeToscaProvider().getPolicyTypeList(pfDao, + "onap.policies.optimization.AffinityPolicy", "0.0.0"); + assertEquals(1, gotPolicyTypeList.size()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); + + gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, + ToscaPolicyTypeFilter.builder().build()); + assertEquals(2, gotPolicyTypeList.size()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); + + gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, + ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).build()); + assertEquals(1, gotPolicyTypeList.size()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); + + gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, + ToscaPolicyTypeFilter.builder().name(policyTypeKey.getName()).version("0.0.0").build()); + assertEquals(1, gotPolicyTypeList.size()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); + + gotPolicyTypeList = new AuthorativeToscaProvider().getFilteredPolicyTypeList(pfDao, + ToscaPolicyTypeFilter.builder().version("1.0.0").build()); + assertEquals(1, gotPolicyTypeList.size()); + assertEquals(true, beforePolicyType.getName().equals(gotPolicyType.getName())); } @Test - public void testPolicyCreate() throws Exception { + public void testPolicyTypesCreate() throws Exception { assertThatThrownBy(() -> { - new AuthorativeToscaProvider().createPolicies(null, null); + new AuthorativeToscaProvider().createPolicyTypes(null, null); }).hasMessage("dao is marked @NonNull but is null"); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().createPolicies(null, new ToscaServiceTemplate()); + new AuthorativeToscaProvider().createPolicyTypes(null, new ToscaServiceTemplate()); }).hasMessage("dao is marked @NonNull but is null"); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().createPolicies(pfDao, null); + new AuthorativeToscaProvider().createPolicyTypes(pfDao, null); }).hasMessage("serviceTemplate is marked @NonNull but is null"); - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( - ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), - ToscaServiceTemplate.class); + ToscaServiceTemplate testToscaServiceTemplate = new ToscaServiceTemplate(); + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate); + }).hasMessage("no policy types specified on service template"); + + testToscaServiceTemplate.setPolicyTypes(new ArrayList<>()); + assertThatThrownBy(() -> { + new AuthorativeToscaProvider().createPolicyTypes(pfDao, testToscaServiceTemplate); + }).hasMessage("An incoming list of concepts must have at least one entry"); + + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); assertNotNull(toscaServiceTemplate); ToscaServiceTemplate createdServiceTemplate = - new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); + new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate); - PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0"); - ToscaPolicy beforePolicy = - toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); - ToscaPolicy createdPolicy = - createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); - assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); - assertTrue(beforePolicy.getType().equals(createdPolicy.getType())); + ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); + ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName())); + assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); } - @Test - public void testPolicyUpdate() throws Exception { + public void testPolicyTypesUpdate() throws Exception { assertThatThrownBy(() -> { - new AuthorativeToscaProvider().createPolicies(null, null); + new AuthorativeToscaProvider().createPolicyTypes(null, null); }).hasMessage("dao is marked @NonNull but is null"); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().updatePolicies(null, null); + new AuthorativeToscaProvider().updatePolicyTypes(null, null); }).hasMessage("dao is marked @NonNull but is null"); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().updatePolicies(null, new ToscaServiceTemplate()); + new AuthorativeToscaProvider().updatePolicyTypes(null, new ToscaServiceTemplate()); }).hasMessage("dao is marked @NonNull but is null"); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().updatePolicies(pfDao, null); + new AuthorativeToscaProvider().updatePolicyTypes(pfDao, null); }).hasMessage("serviceTemplate is marked @NonNull but is null"); - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( - ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), - ToscaServiceTemplate.class); + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); assertNotNull(toscaServiceTemplate); ToscaServiceTemplate createdServiceTemplate = - new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); + new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate); - PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0"); - ToscaPolicy beforePolicy = - toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); - ToscaPolicy createdPolicy = - createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); - assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); - assertTrue(beforePolicy.getType().equals(createdPolicy.getType())); + ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); + ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName())); + assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); ToscaServiceTemplate updatedServiceTemplate = - new AuthorativeToscaProvider().updatePolicies(pfDao, toscaServiceTemplate); + new AuthorativeToscaProvider().updatePolicyTypes(pfDao, toscaServiceTemplate); - ToscaPolicy updatedPolicy = - updatedServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); - assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, updatedPolicy)); - assertTrue(beforePolicy.getType().equals(updatedPolicy.getType())); + ToscaPolicyType updatedPolicy = updatedServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + assertEquals(true, beforePolicyType.getName().equals(updatedPolicy.getName())); + assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), updatedPolicy.getDescription())); } @Test - public void testPoliciesDelete() throws Exception { + public void testPolicyTypesDelete() throws Exception { assertThatThrownBy(() -> { - new AuthorativeToscaProvider().deletePolicy(null, null, null); + new AuthorativeToscaProvider().deletePolicyType(null, null, null); }).hasMessage("dao is marked @NonNull but is null"); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().deletePolicy(null, null, "version"); + new AuthorativeToscaProvider().deletePolicyType(null, null, "version"); }).hasMessage("dao is marked @NonNull but is null"); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().deletePolicy(null, "name", null); + new AuthorativeToscaProvider().deletePolicyType(null, "name", null); }).hasMessage("dao is marked @NonNull but is null"); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().deletePolicy(null, "name", "version"); + new AuthorativeToscaProvider().deletePolicyType(null, "name", "version"); }).hasMessage("dao is marked @NonNull but is null"); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().deletePolicy(pfDao, null, null); + new AuthorativeToscaProvider().deletePolicyType(pfDao, null, null); }).hasMessage("name is marked @NonNull but is null"); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().deletePolicy(pfDao, null, "version"); + new AuthorativeToscaProvider().deletePolicyType(pfDao, null, "version"); }).hasMessage("name is marked @NonNull but is null"); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().deletePolicy(pfDao, "name", null); + new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null); }).hasMessage("version is marked @NonNull but is null"); - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( - ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), - ToscaServiceTemplate.class); + ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class); assertNotNull(toscaServiceTemplate); ToscaServiceTemplate createdServiceTemplate = - new AuthorativeToscaProvider().createPolicies(pfDao, toscaServiceTemplate); + new AuthorativeToscaProvider().createPolicyTypes(pfDao, toscaServiceTemplate); - PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + PfConceptKey policyTypeKey = new PfConceptKey("onap.policies.optimization.AffinityPolicy:0.0.0"); - ToscaPolicy beforePolicy = - toscaServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); - ToscaPolicy createdPolicy = - createdServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); - assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); - assertTrue(beforePolicy.getType().equals(createdPolicy.getType())); + ToscaPolicyType beforePolicyType = toscaServiceTemplate.getPolicyTypes().get(1).get(policyTypeKey.getName()); + ToscaPolicyType createdPolicyType = createdServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + assertEquals(true, beforePolicyType.getName().equals(createdPolicyType.getName())); + assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), createdPolicyType.getDescription())); - ToscaServiceTemplate deletedServiceTemplate = - new AuthorativeToscaProvider().deletePolicy(pfDao, policyKey.getName(), policyKey.getVersion()); + ToscaServiceTemplate deletedServiceTemplate = new AuthorativeToscaProvider().deletePolicyType(pfDao, + policyTypeKey.getName(), policyTypeKey.getVersion()); - ToscaPolicy deletedPolicy = - deletedServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).get(policyKey.getName()); - assertEquals(0, beforePolicy.compareNameVersion(beforePolicy, createdPolicy)); - assertTrue(beforePolicy.getType().equals(deletedPolicy.getType())); + ToscaPolicyType deletedPolicy = deletedServiceTemplate.getPolicyTypes().get(0).get(policyTypeKey.getName()); + assertEquals(true, beforePolicyType.getName().equals(deletedPolicy.getName())); + assertEquals(0, ObjectUtils.compare(beforePolicyType.getDescription(), deletedPolicy.getDescription())); - ToscaServiceTemplate gotServiceTemplate = - new AuthorativeToscaProvider().getPolicies(pfDao, policyKey.getName(), policyKey.getVersion()); + ToscaServiceTemplate gotServiceTemplate = new AuthorativeToscaProvider().getPolicyTypes(pfDao, + policyTypeKey.getName(), policyTypeKey.getVersion()); - assertEquals(0, gotServiceTemplate.getToscaTopologyTemplate().getPolicies().get(0).size()); + assertEquals(0, gotServiceTemplate.getPolicyTypes().get(0).size()); } @Test @@ -375,21 +386,21 @@ public class AuthorativeToscaProviderPolicyTypeTest { ToscaServiceTemplate testServiceTemplate = new ToscaServiceTemplate(); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().deletePolicy(pfDao, "name", null); + new AuthorativeToscaProvider().deletePolicyType(pfDao, "name", null); }).hasMessage("version is marked @NonNull but is null"); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().createPolicies(pfDao, testServiceTemplate); - }).hasMessage("topology template not specified on service template"); + new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate); + }).hasMessage("no policy types specified on service template"); testServiceTemplate.setToscaTopologyTemplate(new ToscaTopologyTemplate()); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().createPolicies(pfDao, testServiceTemplate); - }).hasMessage("no policies specified on topology template of service template"); + new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate); + }).hasMessage("no policy types specified on service template"); - testServiceTemplate.getToscaTopologyTemplate().setPolicies(new ArrayList<>()); + testServiceTemplate.setPolicyTypes(new ArrayList<>()); assertThatThrownBy(() -> { - new AuthorativeToscaProvider().createPolicies(pfDao, testServiceTemplate); + new AuthorativeToscaProvider().createPolicyTypes(pfDao, testServiceTemplate); }).hasMessage("An incoming list of concepts must have at least one entry"); } } 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 5720b5594..9c2344080 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 @@ -20,6 +20,7 @@ package org.onap.policy.models.tosca.legacy.concepts; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -28,6 +29,7 @@ import java.util.Map; import org.junit.Test; import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyInput; +import org.onap.policy.models.tosca.legacy.concepts.testconcepts.DummyBadLegacyGuardPolicyContent; public class LegacyGuardPolicyTest { @@ -45,6 +47,10 @@ public class LegacyGuardPolicyTest { content.setActor("SO"); guard.setContent(content); assertEquals("SO", guard.getContent().getActor()); - } + DummyBadLegacyGuardPolicyContent dblgpc = new DummyBadLegacyGuardPolicyContent(); + assertThatThrownBy(() -> { + dblgpc.getAsPropertyMap(); + }).hasMessage("could not convert content to a property map"); + } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/TestPojos.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/TestPojos.java index f35a4e668..26077189e 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/TestPojos.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/TestPojos.java @@ -44,9 +44,19 @@ public class TestPojos { @Test public void testPojos() { - final Validator validator = ValidatorBuilder.create().with(new ToStringTester()) - .with(new SetterMustExistRule()).with(new GetterMustExistRule()).with(new SetterTester()) - .with(new GetterTester()).build(); - validator.validate(POJO_PACKAGE, new FilterPackageInfo()); + // @formatter:off + final Validator validator = ValidatorBuilder + .create() + .with(new ToStringTester()) + .with(new SetterMustExistRule()) + .with(new GetterMustExistRule()) + .with(new SetterTester()) + .with(new GetterTester()) + .build(); + validator.validate(POJO_PACKAGE, + new FilterPackageInfo() + ); + + // @formatter:on } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/testconcepts/DummyBadLegacyGuardPolicyContent.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/testconcepts/DummyBadLegacyGuardPolicyContent.java new file mode 100644 index 000000000..1b149416c --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/concepts/testconcepts/DummyBadLegacyGuardPolicyContent.java @@ -0,0 +1,34 @@ +/*- + * ============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.concepts.testconcepts; + +import org.onap.policy.models.tosca.legacy.concepts.LegacyGuardPolicyContent; + +/** + * Dummy {@link LegacyGuardPolicyContent} class to force exception for testing. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class DummyBadLegacyGuardPolicyContent extends LegacyGuardPolicyContent { + public static final String TO_TRIGGER_EXCEPTION = "Dummy"; + @SuppressWarnings("unused") + private final transient String aaa = "aaa"; +} 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 new file mode 100644 index 000000000..b1dac8f20 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/mapping/LegacyGuardPolicyMapperTest.java @@ -0,0 +1,52 @@ +/*- + * ============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.mapping; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; +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.JpaToscaServiceTemplate; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate; + +/** + * Test the {@link LegacyGuardPolicyMapper} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class LegacyGuardPolicyMapperTest { + + @Test + public void testLegacyGuardPolicyMapper() { + JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); + serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); + serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); + + JpaToscaPolicy policy = new JpaToscaPolicy(new PfConceptKey("PolicyName", "0.0.1")); + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policy.getKey(), policy); + + 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 new file mode 100644 index 000000000..4df62aff0 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/mapping/LegacyOperationalPolicyMapperTest.java @@ -0,0 +1,105 @@ +/*- + * ============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.mapping; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.LinkedHashMap; + +import org.junit.Before; +import org.junit.Test; +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.PfValidationResult; +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.JpaToscaPolicies; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Test serialization of monitoring policies. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class LegacyOperationalPolicyMapperTest { + // Logger for this class + private static final Logger LOGGER = LoggerFactory.getLogger(LegacyOperationalPolicyMapperTest.class); + + private StandardCoder standardCoder; + + @Before + public void setUp() { + standardCoder = new StandardCoder(); + } + + @Test + public void testJsonDeserialization() throws Exception { + String vcpePolicyJson = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"); + + LegacyOperationalPolicy legacyOperationalPolicy = + standardCoder.decode(vcpePolicyJson, LegacyOperationalPolicy.class); + + JpaToscaServiceTemplate serviceTemplate = + new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy); + + assertNotNull(serviceTemplate); + LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString()); + assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid()); + + assertEquals("operational.restart:1.0.0", + serviceTemplate.getTopologyTemplate().getPolicies().get("operational.restart").getId()); + } + + @Test + public void testOperationalPolicyMapper() throws Exception { + JpaToscaServiceTemplate serviceTemplate = new JpaToscaServiceTemplate(); + serviceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); + serviceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); + + JpaToscaPolicy policy0 = new JpaToscaPolicy(new PfConceptKey("PolicyName0", "0.0.1")); + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policy0.getKey(), policy0); + JpaToscaPolicy policy1 = new JpaToscaPolicy(new PfConceptKey("PolicyName1", "0.0.1")); + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().put(policy1.getKey(), policy1); + + assertThatThrownBy(() -> { + new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate); + }).hasMessage("more than one policy found in service template"); + + serviceTemplate.getTopologyTemplate().getPolicies().getConceptMap().remove(policy1.getKey()); + + assertThatThrownBy(() -> { + new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate); + }).hasMessage("no properties defined on TOSCA policy"); + + policy0.setProperties(new LinkedHashMap<>()); + assertThatThrownBy(() -> { + new LegacyOperationalPolicyMapper().fromToscaServiceTemplate(serviceTemplate); + }).hasMessage("property \"content\" not defined on TOSCA policy"); + } +} 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 deleted file mode 100644 index 26242611b..000000000 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/legacy/serialization/LegacyOperationalPolicySerializationTest.java +++ /dev/null @@ -1,71 +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.serialization; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.common.utils.coder.StandardCoder; -import org.onap.policy.common.utils.resources.ResourceUtils; -import org.onap.policy.models.base.PfValidationResult; -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.JpaToscaServiceTemplate; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Test serialization of monitoring policies. - * - * @author Liam Fallon (liam.fallon@est.tech) - */ -public class LegacyOperationalPolicySerializationTest { - // Logger for this class - private static final Logger LOGGER = LoggerFactory.getLogger(LegacyOperationalPolicySerializationTest.class); - - private StandardCoder standardCoder; - - @Before - public void setUp() { - standardCoder = new StandardCoder(); - } - - @Test - public void testJsonDeserialization() throws Exception { - String vcpePolicyJson = ResourceUtils.getResourceAsString("policies/vCPE.policy.operational.input.json"); - - LegacyOperationalPolicy legacyOperationalPolicy = - standardCoder.decode(vcpePolicyJson, LegacyOperationalPolicy.class); - - JpaToscaServiceTemplate serviceTemplate = - new LegacyOperationalPolicyMapper().toToscaServiceTemplate(legacyOperationalPolicy); - - assertNotNull(serviceTemplate); - LOGGER.info(serviceTemplate.validate(new PfValidationResult()).toString()); - assertTrue(serviceTemplate.validate(new PfValidationResult()).isValid()); - - 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/concepts/JpaToscaConstraintLogicalTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogicalTest.java index de0c813e2..d3239da73 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogicalTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintLogicalTest.java @@ -21,44 +21,55 @@ package org.onap.policy.models.tosca.simple.concepts; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNull; + +import java.util.ArrayList; import org.junit.Test; -import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintLogical; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint; /** - * DAO test for ToscaConstraintLogicalString. + * Test the {@link JpaToscaConstraintLogical} class. * * @author Liam Fallon (liam.fallon@est.tech) */ public class JpaToscaConstraintLogicalTest { @Test - public void testConstraintLogicalStringPojo() { - assertNotNull(new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "Constraint")); + public void testLogicalConstraint() { + ToscaConstraint c0 = new ToscaConstraint(); + c0.setEqual("Hello"); + JpaToscaConstraintLogical jc0 = new JpaToscaConstraintLogical(c0); + assertEquals(c0, jc0.toAuthorative()); + + ToscaConstraint c1 = new ToscaConstraint(); + c1.setGreaterOrEqual("Hello"); + JpaToscaConstraintLogical jc1 = new JpaToscaConstraintLogical(c1); + assertEquals(c1, jc1.toAuthorative()); + + ToscaConstraint c2 = new ToscaConstraint(); + c2.setGreaterThan("Hello"); + JpaToscaConstraintLogical jc2 = new JpaToscaConstraintLogical(c2); + assertEquals(c2, jc2.toAuthorative()); - try { - new JpaToscaConstraintLogical((JpaToscaConstraintOperation) null, null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("operation is marked @NonNull but is null", exc.getMessage()); - } + ToscaConstraint c3 = new ToscaConstraint(); + c3.setLessOrEqual("Hello"); + JpaToscaConstraintLogical jc3 = new JpaToscaConstraintLogical(c3); + assertEquals(c3, jc3.toAuthorative()); - try { - new JpaToscaConstraintLogical((JpaToscaConstraintOperation) null, "Hello"); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("operation is marked @NonNull but is null", exc.getMessage()); - } + ToscaConstraint c4 = new ToscaConstraint(); + c4.setLessThan("Hello"); + JpaToscaConstraintLogical jc4 = new JpaToscaConstraintLogical(c4); + assertEquals(c4, jc4.toAuthorative()); - try { - new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("compareTo is marked @NonNull but is null", exc.getMessage()); - } + ToscaConstraint c5 = new ToscaConstraint(); + JpaToscaConstraintLogical jc5 = new JpaToscaConstraintLogical(c5); + assertNull(jc5.toAuthorative()); - assertNotNull(new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "Constraint")); + assertEquals(-1, jc0.compareTo(null)); + assertEquals(0, jc0.compareTo(jc0)); + assertNotEquals(0, jc0.compareTo(new JpaToscaConstraintValidValues(new ArrayList<>()))); + assertEquals(-2, jc0.compareTo(jc1)); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintTest.java new file mode 100644 index 000000000..ff4187a47 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaConstraintTest.java @@ -0,0 +1,88 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.simple.concepts; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintLogical; + +/** + * DAO test for ToscaConstraintLogicalString. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class JpaToscaConstraintTest { + + @Test + public void testConstraintLogicalStringPojo() { + assertNotNull(new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "Constraint")); + + try { + new JpaToscaConstraintLogical((JpaToscaConstraintOperation) null, null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("operation is marked @NonNull but is null", exc.getMessage()); + } + + try { + new JpaToscaConstraintLogical((JpaToscaConstraintOperation) null, "Hello"); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("operation is marked @NonNull but is null", exc.getMessage()); + } + + try { + new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, null); + fail("test should throw an exception"); + } catch (Exception exc) { + assertEquals("compareTo is marked @NonNull but is null", exc.getMessage()); + } + + assertNotNull(new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "Constraint")); + + assertEquals(0, new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "") + .compareTo(new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, ""))); + + JpaToscaConstraintOperation op = JpaToscaConstraintOperation.EQ; + assertEquals("equal_to", op.getToscaToken()); + + List validValues = new ArrayList<>(); + validValues.add("hello"); + validValues.add("goodbye"); + JpaToscaConstraintValidValues cvv0 = new JpaToscaConstraintValidValues(validValues); + assertEquals(-1, cvv0.compareTo(null)); + assertEquals(0, cvv0.compareTo(cvv0)); + assertNotEquals(0, cvv0.compareTo(new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "Constraint"))); + JpaToscaConstraintValidValues cvv1 = new JpaToscaConstraintValidValues(validValues); + assertEquals(0, cvv0.compareTo(cvv1)); + + cvv1.fromAuthorative(new ToscaConstraint()); + assertNotNull(cvv1.getValidValues()); + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypeTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypeTest.java index efdddccb0..66cde51fc 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypeTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypeTest.java @@ -20,11 +20,11 @@ package org.onap.policy.models.tosca.simple.concepts; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.util.ArrayList; import java.util.LinkedHashMap; @@ -35,6 +35,8 @@ import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfReferenceKey; import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint; +import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraint; import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty; @@ -51,20 +53,15 @@ public class JpaToscaDataTypeTest { assertNotNull(new JpaToscaDataType()); assertNotNull(new JpaToscaDataType(new PfConceptKey())); assertNotNull(new JpaToscaDataType(new JpaToscaDataType())); + assertNotNull(new JpaToscaDataType(new ToscaDataType())); - try { + assertThatThrownBy(() -> { new JpaToscaDataType((PfConceptKey) null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("key is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("key is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new JpaToscaDataType((JpaToscaDataType) null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("copyConcept is marked @NonNull but is null"); PfConceptKey dtKey = new PfConceptKey("tdt", "0.0.1"); JpaToscaDataType tdt = new JpaToscaDataType(dtKey); @@ -105,12 +102,9 @@ public class JpaToscaDataTypeTest { otherDt.setProperties(properties); assertEquals(0, tdt.compareTo(otherDt)); - try { + assertThatThrownBy(() -> { tdt.copyTo(null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("target is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("target is marked @NonNull but is null"); assertEquals(3, tdt.getKeys().size()); assertEquals(1, new JpaToscaDataType().getKeys().size()); @@ -132,12 +126,23 @@ public class JpaToscaDataTypeTest { tdt.getProperties().remove(null); assertTrue(tdt.validate(new PfValidationResult()).isValid()); - try { + assertThatThrownBy(() -> { tdt.validate(null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("resultIn is marked @NonNull but is null", exc.getMessage()); - } - + }).hasMessage("resultIn is marked @NonNull but is null"); + + ToscaDataType dat = new ToscaDataType(); + dat.setName("name"); + dat.setVersion("1.2.3"); + dat.setConstraints(new ArrayList<>()); + ToscaConstraint constraint = new ToscaConstraint(); + constraint.setEqual("EqualTo"); + dat.getConstraints().add(constraint); + + JpaToscaDataType tdta = new JpaToscaDataType(); + tdta.fromAuthorative(dat); + assertEquals("name", tdta.getKey().getName()); + + ToscaDataType datOut = tdta.toAuthorative(); + assertNotNull(datOut); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypesTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypesTest.java index ce90e372c..c732fa604 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypesTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypesTest.java @@ -24,10 +24,15 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; import java.util.TreeMap; import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes; @@ -74,5 +79,10 @@ public class JpaToscaDataTypesTest { } catch (Exception exc) { assertEquals("key is marked @NonNull but is null", exc.getMessage()); } + + List> dtMapList = new ArrayList<>(); + dtMapList.add(new LinkedHashMap<>()); + dtMapList.get(0).put("policyType", new ToscaDataType()); + assertNotNull(new JpaToscaDataTypes(dtMapList)); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPoliciesTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPoliciesTest.java index 6f6141c5b..db3635ecb 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPoliciesTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPoliciesTest.java @@ -24,10 +24,15 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; import java.util.TreeMap; import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy; @@ -74,5 +79,10 @@ public class JpaToscaPoliciesTest { } catch (Exception exc) { assertEquals("key is marked @NonNull but is null", exc.getMessage()); } + + List> polMapList = new ArrayList<>(); + polMapList.add(new LinkedHashMap<>()); + polMapList.get(0).put("policyType", new ToscaPolicy()); + assertNotNull(new JpaToscaPolicies(polMapList)); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTest.java index ec8e6da8f..571cde485 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTest.java @@ -20,11 +20,11 @@ package org.onap.policy.models.tosca.simple.concepts; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.util.ArrayList; import java.util.HashMap; @@ -33,7 +33,9 @@ import java.util.Map; import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfKey; import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy; /** @@ -50,40 +52,29 @@ public class JpaToscaPolicyTest { assertNotNull(new JpaToscaPolicy(new PfConceptKey(), new PfConceptKey())); assertNotNull(new JpaToscaPolicy(new JpaToscaPolicy())); - try { + ToscaPolicy pol = new ToscaPolicy(); + pol.setType("type"); + assertNotNull(new JpaToscaPolicy(pol)); + + assertThatThrownBy(() -> { new JpaToscaPolicy((PfConceptKey) null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("key is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("key is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new JpaToscaPolicy(null, null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("key is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("key is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new JpaToscaPolicy(new PfConceptKey(), null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("type is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("type is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new JpaToscaPolicy(null, new PfConceptKey()); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("key is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("key is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new JpaToscaPolicy((JpaToscaPolicy) null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("copyConcept is marked @NonNull but is null"); PfConceptKey tpKey = new PfConceptKey("tdt", "0.0.1"); PfConceptKey ptKey = new PfConceptKey("policyType", "0.0.1"); @@ -126,12 +117,9 @@ public class JpaToscaPolicyTest { otherDt.setTargets(targets); assertEquals(0, tp.compareTo(otherDt)); - try { + assertThatThrownBy(() -> { tp.copyTo(null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("target is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("target is marked @NonNull but is null"); assertEquals(3, tp.getKeys().size()); assertEquals(2, new JpaToscaPolicy().getKeys().size()); @@ -164,11 +152,35 @@ public class JpaToscaPolicyTest { tp.getTargets().remove(null); assertTrue(tp.validate(new PfValidationResult()).isValid()); - try { + PfConceptKey tpTypeKey = tp.getKey(); + assertNotNull(tpTypeKey); + tp.setType(null); + assertFalse(tp.validate(new PfValidationResult()).isValid()); + tp.setType(PfConceptKey.getNullKey()); + assertFalse(tp.validate(new PfValidationResult()).isValid()); + tp.setType(tpTypeKey); + assertTrue(tp.validate(new PfValidationResult()).isValid()); + + assertThatThrownBy(() -> { tp.validate(null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("resultIn is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("resultIn is marked @NonNull but is null"); + + assertNotNull(tp.toAuthorative()); + tp.getType().setVersion(PfKey.NULL_KEY_VERSION); + assertNotNull(tp.toAuthorative()); + tp.setProperties(null); + assertNotNull(tp.toAuthorative()); + + assertThatThrownBy(() -> { + tp.fromAuthorative(null); + }).hasMessage("toscaPolicy is marked @NonNull but is null"); + + pol = new ToscaPolicy(); + pol.setName("policy"); + pol.setVersion("1.2.3"); + pol.setType("poltype"); + pol.setTypeVersion("2.2.3"); + tp.fromAuthorative(pol); + assertEquals("2.2.3", tp.getType().getVersion()); } -} \ No newline at end of file +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypeTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypeTest.java index 870640e75..3cdcd9552 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypeTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypeTest.java @@ -37,6 +37,7 @@ import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfReferenceKey; import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntityType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty; @@ -209,5 +210,9 @@ public class JpaToscaPolicyTypeTest { assertEquals(-1, tet.compareTo(null)); assertEquals(0, tet.compareTo(tet)); assertFalse(tet.compareTo(tet.getKey()) == 0); + + assertNotNull(new JpaToscaPolicyType(new ToscaPolicyType())); + + assertNotNull(new JpaToscaEntityType(new ToscaPolicyType())); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypesTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypesTest.java index d0ea1782f..e02df235f 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypesTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypesTest.java @@ -24,10 +24,15 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; import java.util.TreeMap; import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes; @@ -74,5 +79,10 @@ public class JpaToscaPolicyTypesTest { } catch (Exception exc) { assertEquals("key is marked @NonNull but is null", exc.getMessage()); } + + List> ptMapList = new ArrayList<>(); + ptMapList.add(new LinkedHashMap<>()); + ptMapList.get(0).put("policyType", new ToscaPolicyType()); + assertNotNull(new JpaToscaPolicyTypes(ptMapList)); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPropertyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPropertyTest.java index b8766601b..706011bcf 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPropertyTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPropertyTest.java @@ -191,6 +191,8 @@ public class JpaToscaPropertyTest { tp.setDefaultValue(null); assertTrue(tp.validate(new PfValidationResult()).isValid()); + tp.setDefaultValue(""); + assertFalse(tp.validate(new PfValidationResult()).isValid()); tp.setDefaultValue("defaultKey"); assertTrue(tp.validate(new PfValidationResult()).isValid()); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplatesTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplatesTest.java index 91d6d0bd3..354fe8b78 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplatesTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplatesTest.java @@ -20,14 +20,18 @@ package org.onap.policy.models.tosca.simple.concepts; -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; import java.util.TreeMap; import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate; import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplates; @@ -41,39 +45,29 @@ public class JpaToscaServiceTemplatesTest { new JpaToscaServiceTemplates(new PfConceptKey(), new TreeMap())); assertNotNull(new JpaToscaServiceTemplates(new JpaToscaServiceTemplates())); - try { + assertThatThrownBy(() -> { new JpaToscaServiceTemplates((PfConceptKey) null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("key is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("key is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new JpaToscaServiceTemplates((JpaToscaServiceTemplates) null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("copyConcept is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new JpaToscaServiceTemplates(null, null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("key is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("key is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new JpaToscaServiceTemplates(new PfConceptKey(), null); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("conceptMap is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("conceptMap is marked @NonNull but is null"); - try { + assertThatThrownBy(() -> { new JpaToscaServiceTemplates(null, new TreeMap()); - fail("test should throw an exception"); - } catch (Exception exc) { - assertEquals("key is marked @NonNull but is null", exc.getMessage()); - } + }).hasMessage("key is marked @NonNull but is null"); + + List> tsMapList = new ArrayList<>(); + tsMapList.add(new LinkedHashMap<>()); + tsMapList.get(0).put("serviceTemplate", new ToscaServiceTemplate()); + assertNotNull(new JpaToscaServiceTemplates(tsMapList)); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplateTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplateTest.java index 417f202f5..61ce3d077 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplateTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplateTest.java @@ -33,6 +33,7 @@ import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfReferenceKey; import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaTopologyTemplate; 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.JpaToscaTopologyTemplate; @@ -49,6 +50,7 @@ public class JpaToscaTopologyTemplateTest { assertNotNull(new JpaToscaTopologyTemplate()); assertNotNull(new JpaToscaTopologyTemplate(new PfReferenceKey())); assertNotNull(new JpaToscaTopologyTemplate(new JpaToscaTopologyTemplate())); + assertNotNull(new JpaToscaTopologyTemplate(new ToscaTopologyTemplate())); try { new JpaToscaTopologyTemplate((PfReferenceKey) null); @@ -121,6 +123,11 @@ public class JpaToscaTopologyTemplateTest { assertTrue(new JpaToscaTopologyTemplate().validate(new PfValidationResult()).isValid()); assertTrue(ttt.validate(new PfValidationResult()).isValid()); + ttt.setKey(PfReferenceKey.getNullKey()); + assertFalse(ttt.validate(new PfValidationResult()).isValid()); + ttt.setKey(tttKey); + assertTrue(ttt.validate(new PfValidationResult()).isValid()); + ttt.setDescription(null); assertTrue(ttt.validate(new PfValidationResult()).isValid()); ttt.setDescription(""); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/TestPojos.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/TestPojos.java index e5be49860..edb6e2e58 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/TestPojos.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/TestPojos.java @@ -40,7 +40,6 @@ import org.onap.policy.common.utils.validation.ToStringTester; * */ public class TestPojos { - private static final String POJO_PACKAGE = "org.onap.policy.models.tosca.simple.concepts"; @Test diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/testconcepts/DummyToscaConstraint.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/testconcepts/DummyToscaConstraint.java index b13cb4b0b..f8e422160 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/testconcepts/DummyToscaConstraint.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/testconcepts/DummyToscaConstraint.java @@ -45,4 +45,9 @@ public class DummyToscaConstraint extends JpaToscaConstraint { @Override public void fromAuthorative(ToscaConstraint authorativeConcept) { } + + @Override + public int compareTo(JpaToscaConstraint otherConstraint) { + return 0; + } } 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 d94c39936..ddfb5676a 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 @@ -20,6 +20,7 @@ package org.onap.policy.models.tosca.simple.provider; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -92,13 +93,6 @@ public class SimpleToscaProviderTest { @Test public void testPoliciesGet() throws Exception { - try { - new SimpleToscaProvider().getPolicies(null, null, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), ToscaServiceTemplate.class); @@ -124,27 +118,6 @@ public class SimpleToscaProviderTest { @Test public void testPolicyCreate() throws Exception { - try { - new SimpleToscaProvider().createPolicies(null, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } - - try { - new SimpleToscaProvider().createPolicies(null, new JpaToscaServiceTemplate()); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } - - try { - new SimpleToscaProvider().createPolicies(pfDao, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage()); - } - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), ToscaServiceTemplate.class); @@ -161,27 +134,6 @@ public class SimpleToscaProviderTest { @Test public void testPolicyUpdate() throws Exception { - try { - new SimpleToscaProvider().updatePolicies(null, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } - - try { - new SimpleToscaProvider().updatePolicies(null, new JpaToscaServiceTemplate()); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } - - try { - new SimpleToscaProvider().updatePolicies(pfDao, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage()); - } - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), ToscaServiceTemplate.class); @@ -198,27 +150,6 @@ public class SimpleToscaProviderTest { @Test public void testPoliciesDelete() throws Exception { - try { - new SimpleToscaProvider().deletePolicy(null, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } - - try { - new SimpleToscaProvider().deletePolicy(null, new PfConceptKey()); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("dao is marked @NonNull but is null", exc.getMessage()); - } - - try { - new SimpleToscaProvider().deletePolicy(pfDao, null); - fail("test should throw an exception here"); - } catch (Exception exc) { - assertEquals("policyKey is marked @NonNull but is null", exc.getMessage()); - } - ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), ToscaServiceTemplate.class); @@ -271,6 +202,88 @@ public class SimpleToscaProviderTest { assertEquals("list of policies specified on topology template of service template is empty", exc.getMessage()); } + } + + @Test + public void testNonNulls() { + assertThatThrownBy(() -> { + new SimpleToscaProvider().getPolicyTypes(null, null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().createPolicyTypes(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().createPolicyTypes(null, new JpaToscaServiceTemplate()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().createPolicyTypes(pfDao, null); + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().updatePolicyTypes(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().updatePolicyTypes(null, new JpaToscaServiceTemplate()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().updatePolicyTypes(pfDao, null); + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + assertThatThrownBy(() -> { + new SimpleToscaProvider().deletePolicyType(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().deletePolicyType(null, new PfConceptKey()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().deletePolicyType(pfDao, null); + }).hasMessage("policyTypeKey is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().getPolicies(null, null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().createPolicies(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().createPolicies(null, new JpaToscaServiceTemplate()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().createPolicies(pfDao, null); + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().updatePolicies(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().updatePolicies(null, new JpaToscaServiceTemplate()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().updatePolicies(pfDao, null); + }).hasMessage("serviceTemplate is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().deletePolicy(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().deletePolicy(null, new PfConceptKey()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new SimpleToscaProvider().deletePolicy(pfDao, null); + }).hasMessage("policyKey is marked @NonNull but is null"); } } 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 new file mode 100644 index 000000000..40a2fd29a --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java @@ -0,0 +1,45 @@ +/*- + * ============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 static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.junit.Test; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; + +/** + * Import the {@link ToscaUtils} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaUtilsTest { + + @Test + public void test() { + JpaToscaServiceTemplate jpaToscaServiceTemplate = new JpaToscaServiceTemplate(); + jpaToscaServiceTemplate.setPolicyTypes(new JpaToscaPolicyTypes()); + + assertThatThrownBy(() -> { + ToscaUtils.assertPolicyTypesExist(jpaToscaServiceTemplate); + }).hasMessage("list of policy types specified on service template is empty"); + } +} -- cgit 1.2.3-korg