diff options
31 files changed, 915 insertions, 186 deletions
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java b/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java index 2ecf8c1c6..949cb96dc 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java @@ -234,10 +234,7 @@ public class PfConceptContainer<C extends PfConcept, A extends PfNameVersion> ex result = key.validate(result); - if (conceptMap.isEmpty()) { - result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, - "conceptMap may not be empty")); - } else { + if (!conceptMap.isEmpty()) { result = validateConceptMap(result); } diff --git a/models-base/src/test/java/org/onap/policy/models/base/PfConceptContainerTest.java b/models-base/src/test/java/org/onap/policy/models/base/PfConceptContainerTest.java index 5e84dff9a..b760e17c0 100644 --- a/models-base/src/test/java/org/onap/policy/models/base/PfConceptContainerTest.java +++ b/models-base/src/test/java/org/onap/policy/models/base/PfConceptContainerTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -33,6 +33,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; + import org.junit.Test; import org.onap.policy.models.base.testconcepts.DummyAuthorativeConcept; import org.onap.policy.models.base.testconcepts.DummyBadPfConceptContainer; @@ -54,7 +55,7 @@ public class PfConceptContainerTest { private static final String DUMMY_VALUE = "Dummy"; private static final String VERSION0 = "0.0.1"; - @SuppressWarnings({ "unchecked", "rawtypes" }) + @SuppressWarnings({"unchecked", "rawtypes"}) @Test public void testConceptContainer() { DummyPfConceptContainer container = new DummyPfConceptContainer(); @@ -74,10 +75,10 @@ public class PfConceptContainerTest { assertThatThrownBy(() -> new DummyPfConceptContainer((PfConceptKey) null, null)).hasMessage(KEY_IS_NULL); assertThatThrownBy(() -> new DummyPfConceptContainer(new PfConceptKey(), null)) - .hasMessage("conceptMap is marked @NonNull but is null"); + .hasMessage("conceptMap is marked @NonNull but is null"); assertThatThrownBy(() -> new DummyPfConceptContainer(null, new TreeMap<PfConceptKey, DummyPfConcept>())) - .hasMessage(KEY_IS_NULL); + .hasMessage(KEY_IS_NULL); container.getKey().setName(DUMMY_VALUE); DummyPfConceptContainer clonedContainer = new DummyPfConceptContainer(container); @@ -85,7 +86,7 @@ public class PfConceptContainerTest { assertEquals(DUMMY_VALUE, clonedContainer.getKey().getName()); assertThatThrownBy(() -> new DummyPfConceptContainer((DummyPfConceptContainer) null)) - .hasMessage("copyConcept is marked @NonNull but is null"); + .hasMessage("copyConcept is marked @NonNull but is null"); List<PfKey> keyList = container.getKeys(); assertEquals(1, keyList.size()); @@ -114,7 +115,7 @@ public class PfConceptContainerTest { assertEquals(0, container.compareTo(clonedContainer)); assertThatThrownBy(() -> new DummyPfConceptContainer((DummyPfConceptContainer) null)) - .isInstanceOf(NullPointerException.class); + .isInstanceOf(NullPointerException.class); assertFalse(container.compareTo(null) == 0); assertEquals(0, container.compareTo(container)); @@ -136,7 +137,7 @@ public class PfConceptContainerTest { DummyPfConceptContainer validateContainer = new DummyPfConceptContainer(); assertFalse(validateContainer.validate(new PfValidationResult()).isOk()); validateContainer.setKey(new PfConceptKey("VCKey", VERSION0)); - assertFalse(validateContainer.validate(new PfValidationResult()).isOk()); + assertTrue(validateContainer.validate(new PfValidationResult()).isOk()); validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey)); assertTrue(validateContainer.validate(new PfValidationResult()).isOk()); @@ -194,11 +195,11 @@ public class PfConceptContainerTest { DummyBadPfConceptContainer badContainer = new DummyBadPfConceptContainer(); assertThatThrownBy(() -> badContainer.fromAuthorative(authorativeList)) - .hasMessage("failed to instantiate instance of container concept class"); + .hasMessage("failed to instantiate instance of container concept class"); authorativeList.clear(); assertThatThrownBy(() -> container.fromAuthorative(authorativeList)) - .hasMessage("An incoming list of concepts must have at least one entry"); + .hasMessage("An incoming list of concepts must have at least one entry"); } @Test(expected = NullPointerException.class) diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypes.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypes.java index 8908ba7bf..94659e720 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypes.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaDataTypes.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -32,10 +32,14 @@ import javax.persistence.Table; import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.NonNull; import lombok.ToString; + import org.onap.policy.models.base.PfConceptContainer; import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType; +import org.onap.policy.models.tosca.utils.ToscaUtils; /** * This class is a container for TOSCA data types. @@ -97,6 +101,18 @@ public class JpaToscaDataTypes extends PfConceptContainer<JpaToscaDataType, Tosc * @param authorativeConceptMapList the authorative concept to copy from */ public JpaToscaDataTypes(final List<Map<String, ToscaDataType>> authorativeConceptMapList) { + super(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION)); this.fromAuthorative(authorativeConceptMapList); } + + @Override + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = super.validate(resultIn); + + for (JpaToscaDataType dataType : this.getConceptMap().values()) { + ToscaUtils.getEntityTypeAncestors(this, dataType, result); + } + + return result; + } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicies.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicies.java index 25e7dc071..74b925b80 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicies.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicies.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -32,11 +32,15 @@ import javax.persistence.Table; import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.NonNull; import lombok.ToString; + import org.onap.policy.models.base.PfAuthorative; import org.onap.policy.models.base.PfConceptContainer; import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.onap.policy.models.tosca.utils.ToscaUtils; /** * This class is a container for TOSCA data types. @@ -99,6 +103,18 @@ public class JpaToscaPolicies extends PfConceptContainer<JpaToscaPolicy, ToscaPo * @param authorativeConceptMapList the authorative concept to copy from */ public JpaToscaPolicies(final List<Map<String, ToscaPolicy>> authorativeConceptMapList) { + super(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION)); this.fromAuthorative(authorativeConceptMapList); } + + @Override + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = super.validate(resultIn); + + for (JpaToscaPolicy policy : this.getConceptMap().values()) { + ToscaUtils.getEntityTypeAncestors(this, policy, result); + } + + return result; + } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypes.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypes.java index 5a54a33a7..9c059b483 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypes.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaPolicyTypes.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -32,10 +32,14 @@ import javax.persistence.Table; import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.NonNull; import lombok.ToString; + import org.onap.policy.models.base.PfConceptContainer; import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfValidationResult; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; +import org.onap.policy.models.tosca.utils.ToscaUtils; /** * This class is a container for TOSCA policy types. @@ -97,6 +101,18 @@ public class JpaToscaPolicyTypes extends PfConceptContainer<JpaToscaPolicyType, * @param authorativeConceptMapList the authorative concept to copy from */ public JpaToscaPolicyTypes(final List<Map<String, ToscaPolicyType>> authorativeConceptMapList) { + super(new PfConceptKey(DEFAULT_NAME, DEFAULT_VERSION)); this.fromAuthorative(authorativeConceptMapList); } + + @Override + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { + PfValidationResult result = super.validate(resultIn); + + for (JpaToscaPolicyType policyType : this.getConceptMap().values()) { + ToscaUtils.getEntityTypeAncestors(this, policyType, result); + } + + return result; + } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java index 31c7df047..f6139ab2d 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplate.java @@ -277,26 +277,13 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp @Override public int compareTo(final PfConcept otherConcept) { - if (otherConcept == null) { - return -1; - } - if (this == otherConcept) { - return 0; - } - if (getClass() != otherConcept.getClass()) { - return getClass().getName().compareTo(otherConcept.getClass().getName()); - } - - final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept; - if (!super.equals(other)) { - return super.compareTo(other); - } - - int result = ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion); + int result = compareToWithoutEntities(otherConcept); if (result != 0) { return result; } + final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept; + result = ObjectUtils.compare(dataTypes, other.dataTypes); if (result != 0) { return result; @@ -309,4 +296,29 @@ public class JpaToscaServiceTemplate extends JpaToscaEntityType<ToscaServiceTemp return ObjectUtils.compare(topologyTemplate, other.topologyTemplate); } + + /** + * Compare this service template to another service template, ignoring contained entitites. + * + * @param otherConcept the other topology template + * @return the result of the comparison + */ + public int compareToWithoutEntities(final PfConcept otherConcept) { + if (otherConcept == null) { + return -1; + } + if (this == otherConcept) { + return 0; + } + if (getClass() != otherConcept.getClass()) { + return getClass().getName().compareTo(otherConcept.getClass().getName()); + } + + final JpaToscaServiceTemplate other = (JpaToscaServiceTemplate) otherConcept; + if (!super.equals(other)) { + return super.compareTo(other); + } + + return ObjectUtils.compare(toscaDefinitionsVersion, other.toscaDefinitionsVersion); + } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java index 5fd1a498c..c8dfa5a36 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTopologyTemplate.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,6 +22,7 @@ package org.onap.policy.models.tosca.simple.concepts; import java.util.List; + import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.EmbeddedId; @@ -31,9 +32,11 @@ import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.OneToOne; import javax.persistence.Table; + import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; + import org.apache.commons.lang3.ObjectUtils; import org.onap.policy.models.base.PfAuthorative; import org.onap.policy.models.base.PfConcept; @@ -78,8 +81,7 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative } /** - * The Key Constructor creates a {@link JpaToscaTopologyTemplate} object with the given concept - * key. + * The Key Constructor creates a {@link JpaToscaTopologyTemplate} object with the given concept key. * * @param key the key */ @@ -178,6 +180,21 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative @Override public int compareTo(final PfConcept otherConcept) { + int result = compareToWithoutEntities(otherConcept); + if (result != 0) { + return result; + } + + return ObjectUtils.compare(policies, ((JpaToscaTopologyTemplate) otherConcept).policies); + } + + /** + * Compare this topology template to another topology template, ignoring contained entitites. + * + * @param otherConcept the other topology template + * @return the result of the comparison + */ + public int compareToWithoutEntities(final PfConcept otherConcept) { if (otherConcept == null) { return -1; } @@ -193,11 +210,6 @@ public class JpaToscaTopologyTemplate extends PfConcept implements PfAuthorative return key.compareTo(other.key); } - int result = ObjectUtils.compare(description, other.description); - if (result != 0) { - return result; - } - - return ObjectUtils.compare(policies, other.policies); + return ObjectUtils.compare(description, other.description); } } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaServiceTemplateUtils.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaServiceTemplateUtils.java new file mode 100644 index 000000000..87b499b4c --- /dev/null +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaServiceTemplateUtils.java @@ -0,0 +1,148 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * 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 java.util.Map; +import java.util.Map.Entry; + +import javax.ws.rs.core.Response; + +import lombok.NonNull; + +import org.onap.policy.models.base.PfConceptContainer; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.base.PfValidationMessage; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; +import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntityType; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate; + +/** + * This utility class provides methods to manage service templates. + */ +public class ToscaServiceTemplateUtils { + /** + * Private constructor to prevent subclassing. + */ + private ToscaServiceTemplateUtils() { + // Private constructor to prevent subclassing + } + + /** + * Add a service template fragment to a service template. All entities in the service template fragment must either + * a) not exist on the original service template or b) be identical to entities on the original service template. + * + * @param originalTemplate the original service template + * @param fragmentTemplate the fragment being added to the original service template + * @return + */ + public static JpaToscaServiceTemplate addFragment(@NonNull final JpaToscaServiceTemplate originalTemplate, + @NonNull final JpaToscaServiceTemplate fragmentTemplate) { + PfValidationResult result = new PfValidationResult(); + + if (originalTemplate.compareToWithoutEntities(fragmentTemplate) != 0) { + result.addValidationMessage(new PfValidationMessage(originalTemplate.getKey(), + ToscaServiceTemplateUtils.class, ValidationResult.INVALID, + "service template in incoming fragment does not equal existing service template")); + } + + JpaToscaServiceTemplate compositeTemplate = new JpaToscaServiceTemplate(originalTemplate); + + compositeTemplate.setDataTypes( + addFragmentEntitites(compositeTemplate.getDataTypes(), fragmentTemplate.getDataTypes(), result)); + compositeTemplate.setPolicyTypes( + addFragmentEntitites(compositeTemplate.getPolicyTypes(), fragmentTemplate.getPolicyTypes(), result)); + + if (originalTemplate.getTopologyTemplate() != null) { + if (originalTemplate.getTopologyTemplate() + .compareToWithoutEntities(fragmentTemplate.getTopologyTemplate()) == 0) { + compositeTemplate.getTopologyTemplate() + .setPolicies(addFragmentEntitites(compositeTemplate.getTopologyTemplate().getPolicies(), + fragmentTemplate.getTopologyTemplate().getPolicies(), result)); + } else { + result.addValidationMessage(new PfValidationMessage(originalTemplate.getTopologyTemplate().getKey(), + ToscaServiceTemplateUtils.class, ValidationResult.INVALID, + "topology template in incoming fragment does not equal existing topology template")); + } + } else if (fragmentTemplate.getTopologyTemplate() != null) { + compositeTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate(fragmentTemplate.getTopologyTemplate())); + } + + if (result.isValid()) { + result = compositeTemplate.validate(result); + } + + if (!result.isValid()) { + String message = result.toString(); + throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, message); + } + + return compositeTemplate; + } + + /** + * Check entities from a fragment container can be added to an original container. + * + * @param <S> The type of container + * + * @param compositeContainer the original container + * @param fragmentContainer the fragment being added to the original container + * @return the composite container with the result + */ + @SuppressWarnings("unchecked") + // @formatter:off + private static + <S extends PfConceptContainer<? extends JpaToscaEntityType<? extends ToscaEntity>, ? extends ToscaEntity>> + S addFragmentEntitites(final S compositeContainer, final S fragmentContainer, + final PfValidationResult result) { + + if (compositeContainer == null) { + return fragmentContainer; + } + + if (fragmentContainer == null) { + return compositeContainer; + } + + for (Entry<PfConceptKey, ? extends JpaToscaEntityType<? extends ToscaEntity>> fragmentEntry : fragmentContainer + .getConceptMap().entrySet()) { + JpaToscaEntityType<? extends ToscaEntity> containerEntry = + compositeContainer.getConceptMap().get(fragmentEntry.getKey()); + if (containerEntry != null && !containerEntry.equals(fragmentEntry.getValue())) { + result.addValidationMessage(new PfValidationMessage(fragmentEntry.getKey(), + ToscaServiceTemplateUtils.class, + ValidationResult.INVALID, "entity in incoming fragment does not equal existing entity")); + } + } + + // This use of a generic map is required to get around typing errors in directly adding the fragment map to the + // original map + @SuppressWarnings("rawtypes") + Map originalContainerMap = compositeContainer.getConceptMap(); + originalContainerMap.putAll(fragmentContainer.getConceptMap()); + + return compositeContainer; + } + // @formatter:on +} diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java index f29854d96..7e106fa53 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/utils/ToscaUtils.java @@ -20,11 +20,24 @@ package org.onap.policy.models.tosca.utils; +import java.util.Collection; +import java.util.Set; import java.util.function.Function; import javax.ws.rs.core.Response; +import lombok.NonNull; + +import org.apache.commons.collections4.CollectionUtils; +import org.onap.policy.models.base.PfConcept; +import org.onap.policy.models.base.PfConceptContainer; +import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.base.PfNameVersion; +import org.onap.policy.models.base.PfValidationMessage; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.base.PfValidationResult.ValidationResult; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntityType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,6 +50,8 @@ import org.slf4j.LoggerFactory; public final class ToscaUtils { private static final Logger LOGGER = LoggerFactory.getLogger(ToscaUtils.class); + private static final String ROOT_KEY_NAME_SUFFIX = ".Root"; + /** * Private constructor to prevent subclassing. */ @@ -171,4 +186,36 @@ public final class ToscaUtils { return null; } + + /** + * Find all the ancestors of an entity type. + * + * @param entityTypes the set of entity types that exist + * @param entityType the entity type for which to get the parents + * @param result the result of the ancestor search with any warnings or errors + * @return + */ + public static Collection<? extends JpaToscaEntityType<?>> getEntityTypeAncestors( + @NonNull PfConceptContainer<? extends PfConcept, ? extends PfNameVersion> entityTypes, + @NonNull JpaToscaEntityType<?> entityType, @NonNull final PfValidationResult result) { + + PfConceptKey parentEntityTypeKey = entityType.getDerivedFrom(); + if (parentEntityTypeKey == null || parentEntityTypeKey.getName().endsWith(ROOT_KEY_NAME_SUFFIX)) { + return CollectionUtils.emptyCollection(); + } + + @SuppressWarnings("unchecked") + Set<JpaToscaEntityType<?>> ancestorEntitySet = (Set<JpaToscaEntityType<?>>) entityTypes + .getAll(parentEntityTypeKey.getName(), parentEntityTypeKey.getVersion()); + + if (ancestorEntitySet.isEmpty()) { + result.addValidationMessage(new PfValidationMessage(entityType.getKey(), ToscaUtils.class, + ValidationResult.INVALID, "parent " + parentEntityTypeKey.getId() + " of entity not found")); + } else { + for (JpaToscaEntityType<?> filteredEntityType : ancestorEntitySet) { + ancestorEntitySet.addAll(getEntityTypeAncestors(entityTypes, filteredEntityType, result)); + } + } + return ancestorEntitySet; + } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java index df935626c..64c3b1365 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -27,10 +27,12 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import com.google.gson.GsonBuilder; + import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; + import org.junit.BeforeClass; import org.junit.Test; import org.onap.policy.common.utils.coder.CoderException; @@ -128,7 +130,7 @@ public class ToscaPolicyFilterTest { assertThatThrownBy(() -> { filter.filter(null); - }).hasMessage("originalList is marked @NonNull but is null"); + }).hasMessageMatching("originalList is marked .*on.*ull but is null"); } @Test @@ -237,7 +239,7 @@ public class ToscaPolicyFilterTest { assertEquals(2, filteredList.size()); filter = ToscaPolicyFilter.builder().type("onap.policies.controlloop.Operational").typeVersion(VERSION_000) - .build(); + .build(); filteredList = filter.filter(policyList); assertEquals(1, filteredList.size()); } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTest.java index 8ee74233b..08035008f 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTest.java @@ -3,6 +3,7 @@ * ONAP Policy Models * ================================================================================ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,8 +37,7 @@ public class ToscaPolicyTest { public void testGetIdentifier_testGetTypeIdentifier() { assertThatThrownBy(() -> { new ToscaPolicy(null); - }).hasMessage("copyObject is marked @NonNull but is null"); - + }).hasMessageMatching("copyObject is marked .*on.*ull but is null"); ToscaPolicy policy = new ToscaPolicy(); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java index f4f822c20..30696ce9d 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -27,10 +27,12 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import com.google.gson.GsonBuilder; + import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; + import org.junit.BeforeClass; import org.junit.Test; import org.onap.policy.common.utils.coder.CoderException; @@ -123,7 +125,7 @@ public class ToscaPolicyTypeFilterTest { assertThatThrownBy(() -> { filter.filter(null); - }).hasMessage("originalList is marked @NonNull but is null"); + }).hasMessageMatching("originalList is marked .*on.*ull but is null"); } @Test diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeTest.java index 59a5a3336..69fae4583 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -38,7 +38,7 @@ public class ToscaPolicyTypeTest { public void testToscaPolicyType() { assertThatThrownBy(() -> { new ToscaPolicyType(null); - }).hasMessage("copyObject is marked @NonNull but is null"); + }).hasMessageMatching("copyObject is marked .*on.*ull but is null"); ToscaPolicyType tpt = new ToscaPolicyType(); tpt.setName("AType"); 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 index ce5ace1c0..2987814a6 100644 --- 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -28,6 +28,7 @@ import static org.junit.Assert.assertNotNull; import java.util.ArrayList; import java.util.List; + import org.junit.Test; import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint; @@ -45,13 +46,13 @@ public class JpaToscaConstraintTest { assertNotNull(new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, CONSTRAINT)); assertThatThrownBy(() -> new JpaToscaConstraintLogical((JpaToscaConstraintOperation) null, null)) - .hasMessage("operation is marked @NonNull but is null"); + .hasMessageMatching("operation is marked .*on.*ull but is null"); assertThatThrownBy(() -> new JpaToscaConstraintLogical((JpaToscaConstraintOperation) null, "Hello")) - .hasMessage("operation is marked @NonNull but is null"); + .hasMessageMatching("operation is marked .*on.*ull but is null"); assertThatThrownBy(() -> new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, null)) - .hasMessage("compareTo is marked @NonNull but is null"); + .hasMessageMatching("compareTo is marked .*on.*ull but is null"); assertNotNull(new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, CONSTRAINT)); 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 281ddfcee..499cf72fb 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019=-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -38,9 +38,6 @@ 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; /** * DAO test for ToscaDatatype. @@ -60,10 +57,10 @@ public class JpaToscaDataTypeTest { assertThatThrownBy(() -> { new JpaToscaDataType((PfConceptKey) null); - }).hasMessage("key is marked @NonNull but is null"); + }).hasMessageMatching("key is marked .*on.*ull but is null"); assertThatThrownBy(() -> new JpaToscaDataType((JpaToscaDataType) null)) - .isInstanceOf(NullPointerException.class); + .isInstanceOf(NullPointerException.class); PfConceptKey dtKey = new PfConceptKey("tdt", VERSION_001); JpaToscaDataType tdt = new JpaToscaDataType(dtKey); @@ -76,7 +73,7 @@ public class JpaToscaDataTypeTest { Map<String, JpaToscaProperty> properties = new LinkedHashMap<>(); JpaToscaProperty tp = - new JpaToscaProperty(new PfReferenceKey(dtKey, "pr"), new PfConceptKey("type", VERSION_001)); + new JpaToscaProperty(new PfReferenceKey(dtKey, "pr"), new PfConceptKey("type", VERSION_001)); properties.put(tp.getKey().getLocalName(), tp); tdt.setProperties(properties); assertEquals(properties, tdt.getProperties()); @@ -126,7 +123,7 @@ public class JpaToscaDataTypeTest { assertThatThrownBy(() -> { tdt.validate(null); - }).hasMessage("resultIn is marked @NonNull but is null"); + }).hasMessageMatching("resultIn is marked .*on.*ull but is null"); ToscaDataType dat = new ToscaDataType(); dat.setName("name"); 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 5421b953e..9a8b29d09 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,21 +21,26 @@ package org.onap.policy.models.tosca.simple.concepts; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; 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.base.PfValidationResult; import org.onap.policy.models.tosca.authorative.concepts.ToscaDataType; public class JpaToscaDataTypesTest { - private static final String KEY_IS_NULL = "key is marked @NonNull but is null"; + private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null"; @Test public void testDataTypes() { @@ -44,22 +49,64 @@ public class JpaToscaDataTypesTest { assertNotNull(new JpaToscaDataTypes(new PfConceptKey(), new TreeMap<PfConceptKey, JpaToscaDataType>())); assertNotNull(new JpaToscaDataTypes(new JpaToscaDataTypes())); - assertThatThrownBy(() -> new JpaToscaDataTypes((PfConceptKey) null)).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaDataTypes((PfConceptKey) null)).hasMessageMatching(KEY_IS_NULL); assertThatThrownBy(() -> new JpaToscaDataTypes((JpaToscaDataTypes) null)) - .hasMessage("copyConcept is marked @NonNull but is null"); + .hasMessageMatching("copyConcept is marked .*on.*ull but is null"); - assertThatThrownBy(() -> new JpaToscaDataTypes(null, null)).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaDataTypes(null, null)).hasMessageMatching(KEY_IS_NULL); assertThatThrownBy(() -> new JpaToscaDataTypes(new PfConceptKey(), null)) - .hasMessage("conceptMap is marked @NonNull but is null"); + .hasMessageMatching("conceptMap is marked .*on.*ull but is null"); assertThatThrownBy(() -> new JpaToscaDataTypes(null, new TreeMap<PfConceptKey, JpaToscaDataType>())) - .hasMessage(KEY_IS_NULL); + .hasMessageMatching(KEY_IS_NULL); List<Map<String, ToscaDataType>> dtMapList = new ArrayList<>(); dtMapList.add(new LinkedHashMap<>()); - dtMapList.get(0).put("policyType", new ToscaDataType()); + + ToscaDataType dt0 = new ToscaDataType(); + dt0.setName("dt0"); + dt0.setVersion("0.0.1"); + dt0.setDescription("dt0 description"); + + dtMapList.get(0).put("dt0", dt0); assertNotNull(new JpaToscaDataTypes(dtMapList)); + assertTrue(new JpaToscaDataTypes(dtMapList).validate(new PfValidationResult()).isValid()); + assertThatThrownBy(() -> new JpaToscaDataTypes(dtMapList).validate(null)) + .hasMessageMatching("resultIn is marked .*on.*ull but is null"); + + dt0.setDerivedFrom(null); + assertTrue(new JpaToscaDataTypes(dtMapList).validate(new PfValidationResult()).isValid()); + + dt0.setDerivedFrom("tosca.datatypes.Root"); + assertTrue(new JpaToscaDataTypes(dtMapList).validate(new PfValidationResult()).isValid()); + + dt0.setDerivedFrom("some.other.Thing"); + PfValidationResult result = new JpaToscaDataTypes(dtMapList).validate(new PfValidationResult()); + assertFalse(result.isValid()); + assertThat(result.toString()).contains("parent some.other.Thing:0.0.0 of entity not found"); + + dt0.setDerivedFrom(null); + assertTrue(new JpaToscaDataTypes(dtMapList).validate(new PfValidationResult()).isValid()); + + ToscaDataType dt1 = new ToscaDataType(); + dt1.setName("dt1"); + dt1.setVersion("0.0.1"); + dt1.setDescription("dt1 description"); + + dtMapList.get(0).put("dt1", dt1); + assertTrue(new JpaToscaDataTypes(dtMapList).validate(new PfValidationResult()).isValid()); + + dt1.setDerivedFrom("dt0"); + assertTrue(new JpaToscaDataTypes(dtMapList).validate(new PfValidationResult()).isValid()); + + dt1.setDerivedFrom("dt2"); + result = new JpaToscaDataTypes(dtMapList).validate(new PfValidationResult()); + assertFalse(result.isValid()); + assertThat(result.toString()).contains("parent dt2:0.0.0 of entity not found"); + + dt1.setDerivedFrom("dt0"); + assertTrue(new JpaToscaDataTypes(dtMapList).validate(new PfValidationResult()).isValid()); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchemaTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchemaTest.java index b306685d0..79fa086c0 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchemaTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEntrySchemaTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -29,6 +29,7 @@ import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.List; + import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfValidationResult; @@ -48,10 +49,10 @@ public class JpaToscaEntrySchemaTest { assertNotNull(new JpaToscaEntrySchema(new JpaToscaEntrySchema(new PfConceptKey()))); assertThatThrownBy(() -> new JpaToscaEntrySchema((PfConceptKey) null)) - .hasMessage("type is marked @NonNull but is null"); + .hasMessageMatching("type is marked .*on.*ull but is null"); assertThatThrownBy(() -> new JpaToscaEntrySchema((JpaToscaEntrySchema) null)) - .hasMessage("copyConcept is marked @NonNull but is null"); + .hasMessageMatching("copyConcept is marked .*on.*ull but is null"); PfConceptKey typeKey = new PfConceptKey("type", "0.0.1"); JpaToscaEntrySchema tes = new JpaToscaEntrySchema(typeKey); @@ -86,7 +87,7 @@ public class JpaToscaEntrySchemaTest { otherEs.setConstraints(constraints); assertEquals(0, tes.compareTo(otherEs)); - assertThatThrownBy(() -> tes.copyTo(null)).hasMessage("target is marked @NonNull but is null"); + assertThatThrownBy(() -> tes.copyTo(null)).hasMessageMatching("target is marked .*on.*ull but is null"); assertEquals(1, tes.getKeys().size()); assertEquals(1, new JpaToscaEntrySchema(typeKey).getKeys().size()); @@ -105,7 +106,8 @@ public class JpaToscaEntrySchemaTest { tes.setType(typeKey); assertTrue(tes.validate(new PfValidationResult()).isValid()); - tes.setDescription("");; + tes.setDescription(""); + assertFalse(tes.validate(new PfValidationResult()).isValid()); tes.setDescription(A_DESCRIPTION); assertTrue(tes.validate(new PfValidationResult()).isValid()); @@ -115,6 +117,6 @@ public class JpaToscaEntrySchemaTest { tes.getConstraints().remove(null); assertTrue(tes.validate(new PfValidationResult()).isValid()); - assertThatThrownBy(() -> tes.validate(null)).hasMessage("resultIn is marked @NonNull but is null"); + assertThatThrownBy(() -> tes.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null"); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEventFilterTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEventFilterTest.java index 7d34a73eb..498a768ff 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEventFilterTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaEventFilterTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -39,7 +39,7 @@ import org.onap.policy.models.base.PfValidationResult; */ public class JpaToscaEventFilterTest { - private static final String KEY_IS_NULL = "key is marked @NonNull but is null"; + private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null"; private static final String A_REQUREMENT = "A Requrement"; private static final String A_CAPABILITY = "A Capability"; private static final String VERSION_001 = "0.0.1"; @@ -51,17 +51,17 @@ public class JpaToscaEventFilterTest { assertNotNull(new JpaToscaEventFilter(new PfReferenceKey(), new PfConceptKey())); assertNotNull(new JpaToscaEventFilter(new JpaToscaEventFilter())); - assertThatThrownBy(() -> new JpaToscaEventFilter((PfReferenceKey) null)).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaEventFilter((PfReferenceKey) null)).hasMessageMatching(KEY_IS_NULL); - assertThatThrownBy(() -> new JpaToscaEventFilter(null, null)).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaEventFilter(null, null)).hasMessageMatching(KEY_IS_NULL); - assertThatThrownBy(() -> new JpaToscaEventFilter(null, new PfConceptKey())).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaEventFilter(null, new PfConceptKey())).hasMessageMatching(KEY_IS_NULL); assertThatThrownBy(() -> new JpaToscaEventFilter(new PfReferenceKey(), null)) - .hasMessage("node is marked @NonNull but is null"); + .hasMessageMatching("node is marked .*on.*ull but is null"); assertThatThrownBy(() -> new JpaToscaEventFilter((JpaToscaEventFilter) null)) - .isInstanceOf(NullPointerException.class); + .isInstanceOf(NullPointerException.class); PfConceptKey efParentKey = new PfConceptKey("tParentKey", VERSION_001); PfReferenceKey efKey = new PfReferenceKey(efParentKey, "trigger0"); @@ -130,6 +130,6 @@ public class JpaToscaEventFilterTest { tef.setNode(nodeKey); assertTrue(tef.validate(new PfValidationResult()).isValid()); - assertThatThrownBy(() -> tef.validate(null)).hasMessage("resultIn is marked @NonNull but is null"); + assertThatThrownBy(() -> tef.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null"); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaModelTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaModelTest.java index 74a4e7c7f..3e8569117 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaModelTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaModelTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -29,6 +29,7 @@ import static org.junit.Assert.assertTrue; import java.util.Map; import java.util.TreeMap; + import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelService; @@ -41,7 +42,7 @@ import org.onap.policy.models.base.PfValidationResult; */ public class JpaToscaModelTest { - private static final String KEY_IS_NULL = "key is marked @NonNull but is null"; + private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null"; private static final String VERSION_001 = "0.0.1"; @Test @@ -51,17 +52,18 @@ public class JpaToscaModelTest { assertNotNull(new JpaToscaModel(new PfConceptKey(), new JpaToscaServiceTemplates())); assertNotNull(new JpaToscaModel(new JpaToscaModel())); - assertThatThrownBy(() -> new JpaToscaModel((PfConceptKey) null)).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaModel((PfConceptKey) null)).hasMessageMatching(KEY_IS_NULL); - assertThatThrownBy(() -> new JpaToscaModel(null, null)).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaModel(null, null)).hasMessageMatching(KEY_IS_NULL); - assertThatThrownBy(() -> new JpaToscaModel(null, new JpaToscaServiceTemplates())).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaModel(null, new JpaToscaServiceTemplates())) + .hasMessageMatching(KEY_IS_NULL); assertThatThrownBy(() -> new JpaToscaModel(new PfConceptKey(), null)) - .hasMessage("serviceTemplates is marked @NonNull but is null"); + .hasMessageMatching("serviceTemplates is marked .*on.*ull but is null"); assertThatThrownBy(() -> new JpaToscaModel((JpaToscaModel) null)) - .hasMessage("copyConcept is marked @NonNull but is null"); + .hasMessageMatching("copyConcept is marked .*on.*ull but is null"); PfConceptKey tstsKey = new PfConceptKey("tsts", VERSION_001); Map<PfConceptKey, JpaToscaServiceTemplate> tstMap = new TreeMap<>(); @@ -90,8 +92,7 @@ public class JpaToscaModelTest { otherDt.setServiceTemplates(tsts); assertEquals(0, tm.compareTo(otherDt)); - assertThatThrownBy(() -> new JpaToscaModel((JpaToscaModel) null)) - .isInstanceOf(NullPointerException.class); + assertThatThrownBy(() -> new JpaToscaModel((JpaToscaModel) null)).isInstanceOf(NullPointerException.class); assertEquals(2, tm.getKeys().size()); assertEquals(2, new JpaToscaModel().getKeys().size()); @@ -101,12 +102,12 @@ public class JpaToscaModelTest { assertEquals(tttClone0, tm); assertFalse(new JpaToscaModel().validate(new PfValidationResult()).isValid()); - assertFalse(tm.validate(new PfValidationResult()).isValid()); + assertTrue(tm.validate(new PfValidationResult()).isValid()); tm.register(); assertTrue(PfModelService.existsModel(tm.getServiceTemplates().getId())); PfModelService.deregisterModel(tm.getServiceTemplates().getId()); - assertThatThrownBy(() -> tm.validate(null)).hasMessage("resultIn is marked @NonNull but is null"); + assertThatThrownBy(() -> tm.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null"); } } 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 10616f2eb..968aa95c2 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,21 +21,26 @@ package org.onap.policy.models.tosca.simple.concepts; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; 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.base.PfValidationResult; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; public class JpaToscaPoliciesTest { - private static final String KEY_IS_NULL = "key is marked @NonNull but is null"; + private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null"; @Test public void testPolicies() { @@ -44,22 +49,68 @@ public class JpaToscaPoliciesTest { assertNotNull(new JpaToscaPolicies(new PfConceptKey(), new TreeMap<PfConceptKey, JpaToscaPolicy>())); assertNotNull(new JpaToscaPolicies(new JpaToscaPolicies())); - assertThatThrownBy(() -> new JpaToscaPolicies((PfConceptKey) null)).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaPolicies((PfConceptKey) null)).hasMessageMatching(KEY_IS_NULL); assertThatThrownBy(() -> new JpaToscaPolicies((JpaToscaPolicies) null)) - .hasMessage("copyConcept is marked @NonNull but is null"); + .hasMessageMatching("copyConcept is marked .*on.*ull but is null"); - assertThatThrownBy(() -> new JpaToscaPolicies(null, null)).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaPolicies(null, null)).hasMessageMatching(KEY_IS_NULL); assertThatThrownBy(() -> new JpaToscaPolicies(new PfConceptKey(), null)) - .hasMessage("conceptMap is marked @NonNull but is null"); + .hasMessageMatching("conceptMap is marked .*on.*ull but is null"); assertThatThrownBy(() -> new JpaToscaPolicies(null, new TreeMap<PfConceptKey, JpaToscaPolicy>())) - .hasMessage(KEY_IS_NULL); + .hasMessageMatching(KEY_IS_NULL); List<Map<String, ToscaPolicy>> polMapList = new ArrayList<>(); polMapList.add(new LinkedHashMap<>()); - polMapList.get(0).put("policyType", new ToscaPolicy()); + + ToscaPolicy pol0 = new ToscaPolicy(); + pol0.setName("pol0"); + pol0.setVersion("0.0.1"); + pol0.setDescription("pol0 description"); + pol0.setType("pt0"); + pol0.setTypeVersion("0.0.1"); + + polMapList.get(0).put("pol0", pol0); assertNotNull(new JpaToscaPolicies(polMapList)); + assertTrue(new JpaToscaPolicies(polMapList).validate(new PfValidationResult()).isValid()); + assertThatThrownBy(() -> new JpaToscaPolicies(polMapList).validate(null)) + .hasMessageMatching("resultIn is marked .*on.*ull but is null"); + + pol0.setDerivedFrom(null); + assertTrue(new JpaToscaPolicies(polMapList).validate(new PfValidationResult()).isValid()); + + pol0.setDerivedFrom("tosca.Policies.Root"); + assertTrue(new JpaToscaPolicies(polMapList).validate(new PfValidationResult()).isValid()); + + pol0.setDerivedFrom("some.other.Thing"); + PfValidationResult result = new JpaToscaPolicies(polMapList).validate(new PfValidationResult()); + assertFalse(result.isValid()); + assertThat(result.toString()).contains("parent some.other.Thing:0.0.0 of entity not found"); + + pol0.setDerivedFrom(null); + assertTrue(new JpaToscaPolicies(polMapList).validate(new PfValidationResult()).isValid()); + + ToscaPolicy pol1 = new ToscaPolicy(); + pol1.setName("pol1"); + pol1.setVersion("0.0.1"); + pol1.setDescription("pol1 description"); + pol1.setType("pt0"); + pol1.setTypeVersion("0.0.1"); + + polMapList.get(0).put("pol1", pol1); + assertTrue(new JpaToscaPolicies(polMapList).validate(new PfValidationResult()).isValid()); + + pol1.setDerivedFrom("pol0"); + assertTrue(new JpaToscaPolicies(polMapList).validate(new PfValidationResult()).isValid()); + + pol1.setDerivedFrom("pol2"); + result = new JpaToscaPolicies(polMapList).validate(new PfValidationResult()); + assertFalse(result.isValid()); + assertThat(result.toString()).contains("parent pol2:0.0.0 of entity not found"); + + pol1.setDerivedFrom("pol0"); + assertTrue(new JpaToscaPolicies(polMapList).validate(new PfValidationResult()).isValid()); } } 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 73e5066c1..c5952546a 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -37,7 +37,6 @@ 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; /** * DAO test for ToscaPolicy. @@ -46,7 +45,7 @@ import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy; */ public class JpaToscaPolicyTest { - private static final String KEY_IS_NULL = "key is marked @NonNull but is null"; + private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null"; private static final String VERSION_001 = "0.0.1"; @Test @@ -62,22 +61,21 @@ public class JpaToscaPolicyTest { assertThatThrownBy(() -> { new JpaToscaPolicy((PfConceptKey) null); - }).hasMessage(KEY_IS_NULL); + }).hasMessageMatching(KEY_IS_NULL); assertThatThrownBy(() -> { new JpaToscaPolicy(null, null); - }).hasMessage(KEY_IS_NULL); + }).hasMessageMatching(KEY_IS_NULL); assertThatThrownBy(() -> { new JpaToscaPolicy(new PfConceptKey(), null); - }).hasMessage("type is marked @NonNull but is null"); + }).hasMessageMatching("type is marked .*on.*ull but is null"); assertThatThrownBy(() -> { new JpaToscaPolicy(null, new PfConceptKey()); - }).hasMessage(KEY_IS_NULL); + }).hasMessageMatching(KEY_IS_NULL); - assertThatThrownBy(() -> new JpaToscaPolicy((JpaToscaPolicy) null)) - .isInstanceOf(NullPointerException.class); + assertThatThrownBy(() -> new JpaToscaPolicy((JpaToscaPolicy) null)).isInstanceOf(NullPointerException.class); PfConceptKey tpKey = new PfConceptKey("tdt", VERSION_001); PfConceptKey ptKey = new PfConceptKey("policyType", VERSION_001); @@ -160,7 +158,7 @@ public class JpaToscaPolicyTest { assertThatThrownBy(() -> { tp.validate(null); - }).hasMessage("resultIn is marked @NonNull but is null"); + }).hasMessageMatching("resultIn is marked .*on.*ull but is null"); assertNotNull(tp.toAuthorative()); tp.getType().setVersion(PfKey.NULL_KEY_VERSION); @@ -170,7 +168,7 @@ public class JpaToscaPolicyTest { assertThatThrownBy(() -> { tp.fromAuthorative(null); - }).hasMessage("toscaPolicy is marked @NonNull but is null"); + }).hasMessageMatching("toscaPolicy is marked .*on.*ull but is null"); pol = new ToscaPolicy(); pol.setName("policy"); 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 1f2a4e111..2ed03fa8d 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -32,6 +32,7 @@ import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; + import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfReferenceKey; @@ -55,10 +56,10 @@ public class JpaToscaPolicyTypeTest { assertNotNull(new JpaToscaPolicyType(new JpaToscaPolicyType())); assertThatThrownBy(() -> new JpaToscaPolicyType((PfConceptKey) null)) - .hasMessage("key is marked @NonNull but is null"); + .hasMessageMatching("key is marked .*on.*ull but is null"); assertThatThrownBy(() -> new JpaToscaPolicyType((JpaToscaPolicyType) null)) - .isInstanceOf(NullPointerException.class); + .isInstanceOf(NullPointerException.class); PfConceptKey ptKey = new PfConceptKey("tdt", VERSION_001); JpaToscaPolicyType tpt = new JpaToscaPolicyType(ptKey); @@ -158,7 +159,8 @@ public class JpaToscaPolicyTypeTest { tpt.getMetadata().remove("nullKey"); assertTrue(tpt.validate(new PfValidationResult()).isValid()); - tpt.setDescription("");; + tpt.setDescription(""); + assertFalse(tpt.validate(new PfValidationResult()).isValid()); tpt.setDescription(A_DESCRIPTION); assertTrue(tpt.validate(new PfValidationResult()).isValid()); @@ -168,13 +170,13 @@ public class JpaToscaPolicyTypeTest { tpt.setDerivedFrom(derivedFromKey); assertTrue(tpt.validate(new PfValidationResult()).isValid()); - assertThatThrownBy(() -> tpt.validate(null)).hasMessage("resultIn is marked @NonNull but is null"); + assertThatThrownBy(() -> tpt.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null"); assertThatThrownBy(() -> new JpaToscaEntityType<ToscaPolicy>((PfConceptKey) null)) - .hasMessage("key is marked @NonNull but is null"); + .hasMessageMatching("key is marked .*on.*ull but is null"); assertThatThrownBy(() -> new JpaToscaEntityType<ToscaPolicy>((JpaToscaEntityType<ToscaPolicy>) null)) - .isInstanceOf(NullPointerException.class); + .isInstanceOf(NullPointerException.class); JpaToscaEntityType<ToscaPolicy> tet = new JpaToscaEntityType<>(tpt.getKey()); assertEquals(-1, tet.compareTo(null)); 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 8288fd80b..f62adbb5e 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,21 +21,26 @@ package org.onap.policy.models.tosca.simple.concepts; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; 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.base.PfValidationResult; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType; public class JpaToscaPolicyTypesTest { - private static final String KEY_IS_NULL = "key is marked @NonNull but is null"; + private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null"; @Test public void testPolicyTypes() { @@ -44,22 +49,64 @@ public class JpaToscaPolicyTypesTest { assertNotNull(new JpaToscaPolicyTypes(new PfConceptKey(), new TreeMap<PfConceptKey, JpaToscaPolicyType>())); assertNotNull(new JpaToscaPolicyTypes(new JpaToscaPolicyTypes())); - assertThatThrownBy(() -> new JpaToscaPolicyTypes((PfConceptKey) null)).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaPolicyTypes((PfConceptKey) null)).hasMessageMatching(KEY_IS_NULL); assertThatThrownBy(() -> new JpaToscaPolicyTypes((JpaToscaPolicyTypes) null)) - .hasMessage("copyConcept is marked @NonNull but is null"); + .hasMessageMatching("copyConcept is marked .*on.*ull but is null"); - assertThatThrownBy(() -> new JpaToscaPolicyTypes(null, null)).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaPolicyTypes(null, null)).hasMessageMatching(KEY_IS_NULL); assertThatThrownBy(() -> new JpaToscaPolicyTypes(new PfConceptKey(), null)) - .hasMessage("conceptMap is marked @NonNull but is null"); + .hasMessageMatching("conceptMap is marked .*on.*ull but is null"); assertThatThrownBy(() -> new JpaToscaPolicyTypes(null, new TreeMap<PfConceptKey, JpaToscaPolicyType>())) - .hasMessage(KEY_IS_NULL); + .hasMessageMatching(KEY_IS_NULL); List<Map<String, ToscaPolicyType>> ptMapList = new ArrayList<>(); ptMapList.add(new LinkedHashMap<>()); - ptMapList.get(0).put("policyType", new ToscaPolicyType()); + + ToscaPolicyType pt0 = new ToscaPolicyType(); + pt0.setName("pt0"); + pt0.setVersion("0.0.1"); + pt0.setDescription("pt0 description"); + + ptMapList.get(0).put("pt0", pt0); assertNotNull(new JpaToscaPolicyTypes(ptMapList)); + assertTrue(new JpaToscaPolicyTypes(ptMapList).validate(new PfValidationResult()).isValid()); + assertThatThrownBy(() -> new JpaToscaPolicyTypes(ptMapList).validate(null)) + .hasMessageMatching("resultIn is marked .*on.*ull but is null"); + + pt0.setDerivedFrom(null); + assertTrue(new JpaToscaPolicyTypes(ptMapList).validate(new PfValidationResult()).isValid()); + + pt0.setDerivedFrom("tosca.PolicyTypes.Root"); + assertTrue(new JpaToscaPolicyTypes(ptMapList).validate(new PfValidationResult()).isValid()); + + pt0.setDerivedFrom("some.other.Thing"); + PfValidationResult result = new JpaToscaPolicyTypes(ptMapList).validate(new PfValidationResult()); + assertFalse(result.isValid()); + assertThat(result.toString()).contains("parent some.other.Thing:0.0.0 of entity not found"); + + pt0.setDerivedFrom(null); + assertTrue(new JpaToscaPolicyTypes(ptMapList).validate(new PfValidationResult()).isValid()); + + ToscaPolicyType pt1 = new ToscaPolicyType(); + pt1.setName("pt1"); + pt1.setVersion("0.0.1"); + pt1.setDescription("pt1 description"); + + ptMapList.get(0).put("pt1", pt1); + assertTrue(new JpaToscaPolicyTypes(ptMapList).validate(new PfValidationResult()).isValid()); + + pt1.setDerivedFrom("pt0"); + assertTrue(new JpaToscaPolicyTypes(ptMapList).validate(new PfValidationResult()).isValid()); + + pt1.setDerivedFrom("pt2"); + result = new JpaToscaPolicyTypes(ptMapList).validate(new PfValidationResult()); + assertFalse(result.isValid()); + assertThat(result.toString()).contains("parent pt2:0.0.0 of entity not found"); + + pt1.setDerivedFrom("pt0"); + assertTrue(new JpaToscaPolicyTypes(ptMapList).validate(new PfValidationResult()).isValid()); } } 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 18837d45d..49e508abc 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -31,6 +31,7 @@ import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.List; import java.util.TreeMap; + import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfReferenceKey; @@ -44,7 +45,7 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty; */ public class JpaToscaPropertyTest { - private static final String KEY_IS_NULL = "key is marked @NonNull but is null"; + private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null"; private static final String DEFAULT_KEY = "defaultKey"; private static final String A_DESCRIPTION = "A Description"; private static final String VERSION_001 = "0.0.1"; @@ -56,14 +57,14 @@ public class JpaToscaPropertyTest { assertNotNull(new JpaToscaProperty(new PfReferenceKey(), new PfConceptKey())); assertNotNull(new JpaToscaProperty(new JpaToscaProperty())); - assertThatThrownBy(() -> new JpaToscaProperty((PfReferenceKey) null)).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaProperty((PfReferenceKey) null)).hasMessageMatching(KEY_IS_NULL); - assertThatThrownBy(() -> new JpaToscaProperty(null, null)).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaProperty(null, null)).hasMessageMatching(KEY_IS_NULL); - assertThatThrownBy(() -> new JpaToscaProperty(null, new PfConceptKey())).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaProperty(null, new PfConceptKey())).hasMessageMatching(KEY_IS_NULL); assertThatThrownBy(() -> new JpaToscaProperty(new PfReferenceKey(), null)) - .hasMessage("type is marked @NonNull but is null"); + .hasMessageMatching("type is marked .*on.*ull but is null"); PfConceptKey pparentKey = new PfConceptKey("tParentKey", VERSION_001); PfReferenceKey pkey = new PfReferenceKey(pparentKey, "trigger0"); @@ -92,7 +93,7 @@ public class JpaToscaPropertyTest { JpaToscaEntrySchema tes = new JpaToscaEntrySchema(typeKey); tp.setEntrySchema(tes); - TreeMap<String,String> metadata = new TreeMap<>(); + TreeMap<String, String> metadata = new TreeMap<>(); metadata.put("metaA", "dataA"); metadata.put("metaB", "dataB"); tp.setMetadata(metadata); @@ -145,7 +146,7 @@ public class JpaToscaPropertyTest { assertEquals(0, tp.compareTo(otherDt)); assertThatThrownBy(() -> new JpaToscaProperty((JpaToscaProperty) null)) - .isInstanceOf(NullPointerException.class); + .isInstanceOf(NullPointerException.class); assertEquals(3, tp.getKeys().size()); assertEquals(2, new JpaToscaProperty().getKeys().size()); @@ -189,7 +190,7 @@ public class JpaToscaPropertyTest { tp.setMetadata(null); assertTrue(tp.validate(new PfValidationResult()).isValid()); - assertThatThrownBy(() -> tp.validate(null)).hasMessage("resultIn is marked @NonNull but is null"); + assertThatThrownBy(() -> tp.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null"); } @Test @@ -221,7 +222,7 @@ public class JpaToscaPropertyTest { JpaToscaEntrySchema tes = new JpaToscaEntrySchema(typeKey); tp.setEntrySchema(tes); - TreeMap<String,String> metadata = new TreeMap<>(); + TreeMap<String, String> metadata = new TreeMap<>(); metadata.put("metaA", "dataA"); metadata.put("metaB", "dataB"); tp.setMetadata(metadata); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplateTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplateTest.java index 911f4a1bf..95c51e912 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplateTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaServiceTemplateTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -29,6 +29,7 @@ import static org.junit.Assert.assertTrue; import java.util.Map; import java.util.TreeMap; + import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfReferenceKey; @@ -41,7 +42,7 @@ import org.onap.policy.models.base.PfValidationResult; */ public class JpaToscaServiceTemplateTest { - private static final String KEY_IS_NULL = "key is marked @NonNull but is null"; + private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null"; private static final String VERSION_001 = "0.0.1"; @Test @@ -51,17 +52,17 @@ public class JpaToscaServiceTemplateTest { assertNotNull(new JpaToscaServiceTemplate(new PfConceptKey(), "")); assertNotNull(new JpaToscaServiceTemplate(new JpaToscaServiceTemplate())); - assertThatThrownBy(() -> new JpaToscaServiceTemplate((PfConceptKey) null)).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaServiceTemplate((PfConceptKey) null)).hasMessageMatching(KEY_IS_NULL); - assertThatThrownBy(() -> new JpaToscaServiceTemplate(null, null)).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaServiceTemplate(null, null)).hasMessageMatching(KEY_IS_NULL); - assertThatThrownBy(() -> new JpaToscaServiceTemplate(null, "")).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaServiceTemplate(null, "")).hasMessageMatching(KEY_IS_NULL); assertThatThrownBy(() -> new JpaToscaServiceTemplate(new PfConceptKey(), null)) - .hasMessage("toscaDefinitionsVersion is marked @NonNull but is null"); + .hasMessageMatching("toscaDefinitionsVersion is marked .*on.*ull but is null"); assertThatThrownBy(() -> new JpaToscaServiceTemplate((JpaToscaServiceTemplate) null)) - .isInstanceOf(NullPointerException.class); + .isInstanceOf(NullPointerException.class); PfConceptKey tstKey = new PfConceptKey("tst", VERSION_001); JpaToscaServiceTemplate tst = new JpaToscaServiceTemplate(tstKey, "Tosca Version"); @@ -133,6 +134,6 @@ public class JpaToscaServiceTemplateTest { tst.setDescription("A Description"); assertTrue(tst.validate(new PfValidationResult()).isValid()); - assertThatThrownBy(() -> tst.validate(null)).hasMessage("resultIn is marked @NonNull but is null"); + assertThatThrownBy(() -> tst.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null"); } } 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 075087774..596640f22 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -33,12 +33,10 @@ 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; public class JpaToscaServiceTemplatesTest { - private static final String KEY_IS_NULL = "key is marked @NonNull but is null"; + private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null"; @Test public void testServiceTemplates() { @@ -50,23 +48,23 @@ public class JpaToscaServiceTemplatesTest { assertThatThrownBy(() -> { new JpaToscaServiceTemplates((PfConceptKey) null); - }).hasMessage(KEY_IS_NULL); + }).hasMessageMatching(KEY_IS_NULL); assertThatThrownBy(() -> { new JpaToscaServiceTemplates((JpaToscaServiceTemplates) null); - }).hasMessage("copyConcept is marked @NonNull but is null"); + }).hasMessageMatching("copyConcept is marked .*on.*ull but is null"); assertThatThrownBy(() -> { new JpaToscaServiceTemplates(null, null); - }).hasMessage(KEY_IS_NULL); + }).hasMessageMatching(KEY_IS_NULL); assertThatThrownBy(() -> { new JpaToscaServiceTemplates(new PfConceptKey(), null); - }).hasMessage("conceptMap is marked @NonNull but is null"); + }).hasMessageMatching("conceptMap is marked .*on.*ull but is null"); assertThatThrownBy(() -> { new JpaToscaServiceTemplates(null, new TreeMap<PfConceptKey, JpaToscaServiceTemplate>()); - }).hasMessage(KEY_IS_NULL); + }).hasMessageMatching(KEY_IS_NULL); List<Map<String, ToscaServiceTemplate>> tsMapList = new ArrayList<>(); tsMapList.add(new LinkedHashMap<>()); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTimeIntervalTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTimeIntervalTest.java index 49cf18e28..ca4cac85f 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTimeIntervalTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTimeIntervalTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -28,6 +28,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.util.Date; + import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfReferenceKey; @@ -40,7 +41,7 @@ import org.onap.policy.models.base.PfValidationResult; */ public class JpaToscaTimeIntervalTest { - private static final String KEY_IS_NULL = "key is marked @NonNull but is null"; + private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null"; @Test public void testTimeIntervalPojo() { @@ -49,27 +50,28 @@ public class JpaToscaTimeIntervalTest { assertNotNull(new JpaToscaTimeInterval(new PfReferenceKey(), new Date(), new Date())); assertNotNull(new JpaToscaTimeInterval(new JpaToscaTimeInterval())); - assertThatThrownBy(() -> new JpaToscaTimeInterval((PfReferenceKey) null)).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaTimeInterval((PfReferenceKey) null)).hasMessageMatching(KEY_IS_NULL); - assertThatThrownBy(() -> new JpaToscaTimeInterval(null, null, null)).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaTimeInterval(null, null, null)).hasMessageMatching(KEY_IS_NULL); - assertThatThrownBy(() -> new JpaToscaTimeInterval(null, null, new Date())).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaTimeInterval(null, null, new Date())).hasMessageMatching(KEY_IS_NULL); - assertThatThrownBy(() -> new JpaToscaTimeInterval(null, new Date(), null)).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaTimeInterval(null, new Date(), null)).hasMessageMatching(KEY_IS_NULL); - assertThatThrownBy(() -> new JpaToscaTimeInterval(null, new Date(), new Date())).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaTimeInterval(null, new Date(), new Date())) + .hasMessageMatching(KEY_IS_NULL); assertThatThrownBy(() -> new JpaToscaTimeInterval(new PfReferenceKey(), null, null)) - .hasMessage("startTime is marked @NonNull but is null"); + .hasMessageMatching("startTime is marked .*on.*ull but is null"); assertThatThrownBy(() -> new JpaToscaTimeInterval(new PfReferenceKey(), null, new Date())) - .hasMessage("startTime is marked @NonNull but is null"); + .hasMessageMatching("startTime is marked .*on.*ull but is null"); assertThatThrownBy(() -> new JpaToscaTimeInterval(new PfReferenceKey(), new Date(), null)) - .hasMessage("endTime is marked @NonNull but is null"); + .hasMessageMatching("endTime is marked .*on.*ull but is null"); assertThatThrownBy(() -> new JpaToscaServiceTemplate((JpaToscaServiceTemplate) null)) - .isInstanceOf(NullPointerException.class); + .isInstanceOf(NullPointerException.class); PfConceptKey ttiParentKey = new PfConceptKey("tParentKey", "0.0.1"); PfReferenceKey ttiKey = new PfReferenceKey(ttiParentKey, "trigger0"); @@ -124,6 +126,6 @@ public class JpaToscaTimeIntervalTest { tti.setEndTime(endTime); assertTrue(tti.validate(new PfValidationResult()).isValid()); - assertThatThrownBy(() -> tti.validate(null)).hasMessage("resultIn is marked @NonNull but is null"); + assertThatThrownBy(() -> tti.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null"); } } 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 94525f0a9..b0450da64 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 @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -29,6 +29,7 @@ import static org.junit.Assert.assertTrue; import java.util.Map; import java.util.TreeMap; + import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfReferenceKey; @@ -53,10 +54,10 @@ public class JpaToscaTopologyTemplateTest { assertNotNull(new JpaToscaTopologyTemplate(new ToscaTopologyTemplate())); assertThatThrownBy(() -> new JpaToscaTopologyTemplate((PfReferenceKey) null)) - .hasMessage("key is marked @NonNull but is null"); + .hasMessageMatching("key is marked .*on.*ull but is null"); assertThatThrownBy(() -> new JpaToscaTopologyTemplate((JpaToscaTopologyTemplate) null)) - .isInstanceOf(NullPointerException.class); + .isInstanceOf(NullPointerException.class); PfReferenceKey tttKey = new PfReferenceKey("tst", VERSION_001, "ttt"); JpaToscaTopologyTemplate ttt = new JpaToscaTopologyTemplate(tttKey); @@ -98,7 +99,7 @@ public class JpaToscaTopologyTemplateTest { assertEquals(0, ttt.compareTo(otherDt)); assertThatThrownBy(() -> new JpaToscaTopologyTemplate((JpaToscaTopologyTemplate) null)) - .isInstanceOf(NullPointerException.class); + .isInstanceOf(NullPointerException.class); assertEquals(4, ttt.getKeys().size()); assertEquals(1, new JpaToscaTopologyTemplate().getKeys().size()); @@ -122,6 +123,6 @@ public class JpaToscaTopologyTemplateTest { ttt.setDescription(A_DESCRIPTION); assertTrue(ttt.validate(new PfValidationResult()).isValid()); - assertThatThrownBy(() -> ttt.validate(null)).hasMessage("resultIn is marked @NonNull but is null"); + assertThatThrownBy(() -> ttt.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null"); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTriggerTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTriggerTest.java index 0baf1e5e2..a7a754f93 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTriggerTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaTriggerTest.java @@ -1,6 +1,6 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -29,6 +29,7 @@ import static org.junit.Assert.assertTrue; import java.time.Duration; import java.util.Date; + import org.junit.Test; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfReferenceKey; @@ -41,7 +42,7 @@ import org.onap.policy.models.base.PfValidationResult; */ public class JpaToscaTriggerTest { - private static final String KEY_IS_NULL = "key is marked @NonNull but is null"; + private static final String KEY_IS_NULL = "key is marked .*on.*ull but is null"; private static final String EVENT_TYPE = "EventType"; private static final String ACTION = "Action"; private static final String A_METHOD = "A Method"; @@ -55,27 +56,26 @@ public class JpaToscaTriggerTest { assertNotNull(new JpaToscaTrigger(new PfReferenceKey(), EVENT_TYPE, ACTION)); assertNotNull(new JpaToscaTrigger(new JpaToscaTrigger())); - assertThatThrownBy(() -> new JpaToscaTrigger((PfReferenceKey) null)).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaTrigger((PfReferenceKey) null)).hasMessageMatching(KEY_IS_NULL); - assertThatThrownBy(() -> new JpaToscaTrigger(null, null, null)).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaTrigger(null, null, null)).hasMessageMatching(KEY_IS_NULL); - assertThatThrownBy(() -> new JpaToscaTrigger(null, EVENT_TYPE, null)).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaTrigger(null, EVENT_TYPE, null)).hasMessageMatching(KEY_IS_NULL); - assertThatThrownBy(() -> new JpaToscaTrigger(null, EVENT_TYPE, ACTION)).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaTrigger(null, EVENT_TYPE, ACTION)).hasMessageMatching(KEY_IS_NULL); - assertThatThrownBy(() -> new JpaToscaTrigger(null, null, ACTION)).hasMessage(KEY_IS_NULL); + assertThatThrownBy(() -> new JpaToscaTrigger(null, null, ACTION)).hasMessageMatching(KEY_IS_NULL); assertThatThrownBy(() -> new JpaToscaTrigger(new PfReferenceKey(), null, null)) - .hasMessage("eventType is marked @NonNull but is null"); + .hasMessageMatching("eventType is marked .*on.*ull but is null"); assertThatThrownBy(() -> new JpaToscaTrigger(new PfReferenceKey(), EVENT_TYPE, null)) - .hasMessage("action is marked @NonNull but is null"); + .hasMessageMatching("action is marked .*on.*ull but is null"); assertThatThrownBy(() -> new JpaToscaTrigger(new PfReferenceKey(), null, ACTION)) - .hasMessage("eventType is marked @NonNull but is null"); + .hasMessageMatching("eventType is marked .*on.*ull but is null"); - assertThatThrownBy(() -> new JpaToscaTrigger((JpaToscaTrigger) null)) - .isInstanceOf(NullPointerException.class); + assertThatThrownBy(() -> new JpaToscaTrigger((JpaToscaTrigger) null)).isInstanceOf(NullPointerException.class); PfConceptKey tparentKey = new PfConceptKey("tParentKey", VERSION_001); PfReferenceKey tkey = new PfReferenceKey(tparentKey, "trigger0"); @@ -175,6 +175,6 @@ public class JpaToscaTriggerTest { tdt.setMethod(A_METHOD); assertTrue(tdt.validate(new PfValidationResult()).isValid()); - assertThatThrownBy(() -> tdt.validate(null)).hasMessage("resultIn is marked @NonNull but is null"); + assertThatThrownBy(() -> tdt.validate(null)).hasMessageMatching("resultIn is marked .*on.*ull but is null"); } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaServiceTemplateUtilsTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaServiceTemplateUtilsTest.java new file mode 100644 index 000000000..ca46de1c3 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaServiceTemplateUtilsTest.java @@ -0,0 +1,205 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * 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 static org.junit.Assert.assertEquals; + +import java.util.Iterator; + +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataType; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicy; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyType; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate; +import org.onap.policy.models.tosca.simple.concepts.JpaToscaTopologyTemplate; + +/** + * Import the {@link ToscaServiceTemplateUtilsTest} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaServiceTemplateUtilsTest { + @Test + public void testAddFragmentNulls() { + assertThatThrownBy(() -> { + ToscaServiceTemplateUtils.addFragment(null, null); + }).hasMessageMatching("originalTemplate is marked .*on.*ull but is null"); + + assertThatThrownBy(() -> { + ToscaServiceTemplateUtils.addFragment(null, new JpaToscaServiceTemplate()); + }).hasMessageMatching("originalTemplate is marked .*on.*ull but is null"); + + assertThatThrownBy(() -> { + ToscaServiceTemplateUtils.addFragment(new JpaToscaServiceTemplate(), null); + }).hasMessageMatching("fragmentTemplate is marked .*on.*ull but is null"); + + assertEquals(new JpaToscaServiceTemplate(), + ToscaServiceTemplateUtils.addFragment(new JpaToscaServiceTemplate(), new JpaToscaServiceTemplate())); + } + + @Test + public void testAddFragment() { + JpaToscaServiceTemplate originalTemplate = new JpaToscaServiceTemplate(); + JpaToscaServiceTemplate fragmentTemplate00 = new JpaToscaServiceTemplate(); + + assertEquals(originalTemplate, ToscaServiceTemplateUtils.addFragment(originalTemplate, originalTemplate)); + assertEquals(originalTemplate, ToscaServiceTemplateUtils.addFragment(originalTemplate, fragmentTemplate00)); + assertEquals(originalTemplate, ToscaServiceTemplateUtils.addFragment(fragmentTemplate00, originalTemplate)); + assertEquals(originalTemplate, ToscaServiceTemplateUtils.addFragment(fragmentTemplate00, fragmentTemplate00)); + assertEquals(fragmentTemplate00, ToscaServiceTemplateUtils.addFragment(originalTemplate, originalTemplate)); + assertEquals(fragmentTemplate00, ToscaServiceTemplateUtils.addFragment(originalTemplate, fragmentTemplate00)); + assertEquals(fragmentTemplate00, ToscaServiceTemplateUtils.addFragment(fragmentTemplate00, originalTemplate)); + assertEquals(fragmentTemplate00, ToscaServiceTemplateUtils.addFragment(fragmentTemplate00, fragmentTemplate00)); + + fragmentTemplate00.setDataTypes(new JpaToscaDataTypes()); + fragmentTemplate00.setPolicyTypes(new JpaToscaPolicyTypes()); + + JpaToscaServiceTemplate compositeTemplate00 = + ToscaServiceTemplateUtils.addFragment(originalTemplate, fragmentTemplate00); + assertEquals(compositeTemplate00, + ToscaServiceTemplateUtils.addFragment(compositeTemplate00, fragmentTemplate00)); + assertEquals(compositeTemplate00, + ToscaServiceTemplateUtils.addFragment(compositeTemplate00, new JpaToscaServiceTemplate())); + assertEquals(compositeTemplate00, + ToscaServiceTemplateUtils.addFragment(new JpaToscaServiceTemplate(), compositeTemplate00)); + + JpaToscaDataType dt0 = new JpaToscaDataType(); + dt0.setKey(new PfConceptKey("dt0", "0.0.1")); + dt0.setDescription("dt0 description"); + JpaToscaServiceTemplate fragmentTemplate01 = new JpaToscaServiceTemplate(); + fragmentTemplate01.setDataTypes(new JpaToscaDataTypes()); + fragmentTemplate01.getDataTypes().getConceptMap().put(dt0.getKey(), dt0); + + JpaToscaServiceTemplate compositeTemplate01 = + ToscaServiceTemplateUtils.addFragment(originalTemplate, fragmentTemplate01); + assertEquals(compositeTemplate01, + ToscaServiceTemplateUtils.addFragment(compositeTemplate01, fragmentTemplate01)); + assertEquals(compositeTemplate01, + ToscaServiceTemplateUtils.addFragment(compositeTemplate01, new JpaToscaServiceTemplate())); + assertEquals(compositeTemplate01, + ToscaServiceTemplateUtils.addFragment(new JpaToscaServiceTemplate(), compositeTemplate01)); + + JpaToscaServiceTemplate compositeTemplate02 = + ToscaServiceTemplateUtils.addFragment(compositeTemplate00, fragmentTemplate01); + assertEquals(compositeTemplate02, + ToscaServiceTemplateUtils.addFragment(compositeTemplate02, fragmentTemplate01)); + assertEquals(compositeTemplate02, + ToscaServiceTemplateUtils.addFragment(compositeTemplate02, new JpaToscaServiceTemplate())); + assertEquals(compositeTemplate02, + ToscaServiceTemplateUtils.addFragment(new JpaToscaServiceTemplate(), compositeTemplate02)); + + JpaToscaDataType otherDt0 = new JpaToscaDataType(); + otherDt0.setKey(new PfConceptKey("dt0", "0.0.1")); + otherDt0.setDescription("dt0 description"); + JpaToscaServiceTemplate fragmentTemplate02 = new JpaToscaServiceTemplate(); + fragmentTemplate02.setDataTypes(new JpaToscaDataTypes()); + fragmentTemplate02.getDataTypes().getConceptMap().put(otherDt0.getKey(), otherDt0); + + compositeTemplate00 = ToscaServiceTemplateUtils.addFragment(compositeTemplate00, fragmentTemplate02); + assertEquals(compositeTemplate00, + ToscaServiceTemplateUtils.addFragment(compositeTemplate00, fragmentTemplate02)); + + JpaToscaDataType badOtherDt0 = new JpaToscaDataType(); + badOtherDt0.setKey(new PfConceptKey("dt0", "0.0.1")); + badOtherDt0.setDescription("dt0 bad description"); + JpaToscaServiceTemplate fragmentTemplate03 = new JpaToscaServiceTemplate(); + fragmentTemplate03.setDataTypes(new JpaToscaDataTypes()); + fragmentTemplate03.getDataTypes().getConceptMap().put(otherDt0.getKey(), otherDt0); + fragmentTemplate03.getDataTypes().getConceptMap().put(badOtherDt0.getKey(), badOtherDt0); + + final JpaToscaServiceTemplate compositeTestTemplate = new JpaToscaServiceTemplate(compositeTemplate00); + assertThatThrownBy(() -> { + ToscaServiceTemplateUtils.addFragment(compositeTestTemplate, fragmentTemplate03); + }).hasMessageContaining("entity in incoming fragment does not equal existing entity"); + + JpaToscaServiceTemplate fragmentTemplate04 = new JpaToscaServiceTemplate(); + fragmentTemplate04.setDescription("Another service template"); + assertThatThrownBy(() -> { + ToscaServiceTemplateUtils.addFragment(compositeTestTemplate, fragmentTemplate04); + }).hasMessageContaining("service template in incoming fragment does not equal existing service template"); + + JpaToscaServiceTemplate fragmentTemplate05 = new JpaToscaServiceTemplate(); + fragmentTemplate05.setTopologyTemplate(new JpaToscaTopologyTemplate()); + fragmentTemplate05.getTopologyTemplate().setDescription("topology template description"); + JpaToscaServiceTemplate compositeTemplate03 = + ToscaServiceTemplateUtils.addFragment(compositeTemplate02, fragmentTemplate05); + assertEquals(fragmentTemplate05.getTopologyTemplate(), compositeTemplate03.getTopologyTemplate()); + + JpaToscaServiceTemplate fragmentTemplate06 = new JpaToscaServiceTemplate(); + fragmentTemplate06.setTopologyTemplate(new JpaToscaTopologyTemplate()); + fragmentTemplate06.getTopologyTemplate().setDescription("topology template description"); + JpaToscaServiceTemplate compositeTemplate04 = + ToscaServiceTemplateUtils.addFragment(compositeTemplate03, fragmentTemplate06); + assertEquals(fragmentTemplate06.getTopologyTemplate(), compositeTemplate04.getTopologyTemplate()); + + JpaToscaServiceTemplate fragmentTemplate07 = new JpaToscaServiceTemplate(); + fragmentTemplate07.setTopologyTemplate(new JpaToscaTopologyTemplate()); + fragmentTemplate07.getTopologyTemplate().setDescription("topology template other description"); + assertThatThrownBy(() -> { + ToscaServiceTemplateUtils.addFragment(compositeTemplate04, fragmentTemplate07); + }).hasMessageContaining("topology template in incoming fragment does not equal existing topology template"); + + JpaToscaDataType dt1 = new JpaToscaDataType(); + dt1.setKey(new PfConceptKey("dt1", "0.0.1")); + dt1.setDescription("dt1 description"); + + JpaToscaPolicyType pt0 = new JpaToscaPolicyType(); + pt0.setKey(new PfConceptKey("pt0", "0.0.1")); + pt0.setDescription("pt0 description"); + + JpaToscaPolicy p0 = new JpaToscaPolicy(); + p0.setKey(new PfConceptKey("p0", "0.0.1")); + p0.setDescription("pt0 description"); + + JpaToscaServiceTemplate fragmentTemplate08 = new JpaToscaServiceTemplate(); + + fragmentTemplate08.setDataTypes(new JpaToscaDataTypes()); + fragmentTemplate08.getDataTypes().getConceptMap().put(dt1.getKey(), dt1); + + fragmentTemplate08.setPolicyTypes(new JpaToscaPolicyTypes()); + fragmentTemplate08.getPolicyTypes().getConceptMap().put(pt0.getKey(), pt0); + + fragmentTemplate08.setTopologyTemplate(new JpaToscaTopologyTemplate()); + fragmentTemplate08.getTopologyTemplate().setDescription("topology template description"); + + fragmentTemplate08.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); + fragmentTemplate08.getTopologyTemplate().getPolicies().getConceptMap().put(p0.getKey(), p0); + + assertThatThrownBy(() -> { + ToscaServiceTemplateUtils.addFragment(compositeTemplate04, fragmentTemplate08); + }).hasMessageContaining("JpaToscaPolicy:INVALID:type is null or a null key"); + + p0.setType(pt0.getKey()); + + JpaToscaServiceTemplate compositeTemplate05 = + ToscaServiceTemplateUtils.addFragment(compositeTemplate04, fragmentTemplate08); + Iterator<JpaToscaDataType> dtIterator = compositeTemplate05.getDataTypes().getAll(null).iterator(); + assertEquals(dt0, dtIterator.next()); + assertEquals(dt1, dtIterator.next()); + assertEquals(pt0, compositeTemplate05.getPolicyTypes().getAll(null).iterator().next()); + assertEquals(p0, compositeTemplate05.getTopologyTemplate().getPolicies().getAll(null).iterator().next()); + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java index 82636940c..ecd066a14 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/utils/ToscaUtilsTest.java @@ -28,6 +28,9 @@ import static org.junit.Assert.assertTrue; 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.simple.concepts.JpaToscaDataType; import org.onap.policy.models.tosca.simple.concepts.JpaToscaDataTypes; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicies; import org.onap.policy.models.tosca.simple.concepts.JpaToscaPolicyTypes; @@ -137,4 +140,107 @@ public class ToscaUtilsTest { ToscaUtils.assertPoliciesExist(jpaToscaServiceTemplate); }).doesNotThrowAnyException(); } + + @Test + public void testGetentityTypeAncestors() { + assertThatThrownBy(() -> { + ToscaUtils.getEntityTypeAncestors(null, null, null); + }).hasMessageMatching("entityTypes is marked .*on.*ull but is null"); + + assertThatThrownBy(() -> { + ToscaUtils.getEntityTypeAncestors(null, null, new PfValidationResult()); + }).hasMessageMatching("entityTypes is marked .*on.*ull but is null"); + + assertThatThrownBy(() -> { + ToscaUtils.getEntityTypeAncestors(null, new JpaToscaDataType(), null); + }).hasMessageMatching("entityTypes is marked .*on.*ull but is null"); + + assertThatThrownBy(() -> { + ToscaUtils.getEntityTypeAncestors(null, new JpaToscaDataType(), new PfValidationResult()); + }).hasMessageMatching("entityTypes is marked .*on.*ull but is null"); + + assertThatThrownBy(() -> { + ToscaUtils.getEntityTypeAncestors(new JpaToscaDataTypes(), null, null); + }).hasMessageMatching("entityType is marked .*on.*ull but is null"); + + assertThatThrownBy(() -> { + ToscaUtils.getEntityTypeAncestors(new JpaToscaDataTypes(), null, new PfValidationResult()); + }).hasMessageMatching("entityType is marked .*on.*ull but is null"); + + assertThatThrownBy(() -> { + ToscaUtils.getEntityTypeAncestors(new JpaToscaDataTypes(), new JpaToscaDataType(), null); + }).hasMessageMatching("result is marked .*on.*ull but is null"); + + JpaToscaDataTypes dataTypes = new JpaToscaDataTypes(); + JpaToscaDataType dt0 = new JpaToscaDataType(); + dt0.setKey(new PfConceptKey("dt0", "0.0.1")); + dt0.setDescription("dt0 description"); + PfValidationResult result = new PfValidationResult(); + + assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty()); + + dataTypes.getConceptMap().put(dt0.getKey(), dt0); + assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty()); + assertTrue(result.isValid()); + + dt0.setDerivedFrom(null); + assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty()); + assertTrue(result.isValid()); + + dt0.setDerivedFrom(new PfConceptKey("tosca.datatyps.Root", PfKey.NULL_KEY_VERSION)); + assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty()); + assertTrue(result.isValid()); + + dt0.setDerivedFrom(new PfConceptKey("some.thing.Else", PfKey.NULL_KEY_VERSION)); + assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty()); + assertFalse(result.isValid()); + assertTrue(result.toString().contains("parent some.thing.Else:0.0.0 of entity not found")); + + result = new PfValidationResult(); + dt0.setDerivedFrom(new PfConceptKey("tosca.datatyps.Root", PfKey.NULL_KEY_VERSION)); + + JpaToscaDataType dt1 = new JpaToscaDataType(); + dt1.setKey(new PfConceptKey("dt1", "0.0.1")); + dt1.setDescription("dt1 description"); + dataTypes.getConceptMap().put(dt1.getKey(), dt1); + assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty()); + assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).isEmpty()); + assertTrue(result.isValid()); + + dt1.setDerivedFrom(dt0.getKey()); + assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty()); + assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).isEmpty()); + assertEquals(1, ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).size()); + assertTrue(result.isValid()); + + JpaToscaDataType dt2 = new JpaToscaDataType(); + dt2.setKey(new PfConceptKey("dt2", "0.0.1")); + dt2.setDescription("dt2 description"); + dataTypes.getConceptMap().put(dt2.getKey(), dt2); + assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty()); + assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).isEmpty()); + assertEquals(1, ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).size()); + assertTrue(result.isValid()); + + dt2.setDerivedFrom(dt1.getKey()); + assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty()); + assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).isEmpty()); + assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).isEmpty()); + assertEquals(1, ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).size()); + assertEquals(2, ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).size()); + assertTrue(result.isValid()); + + dt1.setDerivedFrom(new PfConceptKey("tosca.datatyps.Root", PfKey.NULL_KEY_VERSION)); + assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt0, result).isEmpty()); + assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).isEmpty()); + assertFalse(ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).isEmpty()); + assertEquals(0, ToscaUtils.getEntityTypeAncestors(dataTypes, dt1, result).size()); + assertEquals(1, ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).size()); + assertTrue(result.isValid()); + + dataTypes.getConceptMap().remove(dt1.getKey()); + assertTrue(ToscaUtils.getEntityTypeAncestors(dataTypes, dt2, result).isEmpty()); + assertFalse(result.isValid()); + assertTrue(result.toString().contains("parent dt1:0.0.1 of entity not found")); + } } |