From 9ede89131044d171187c5be906aefee615a0e568 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Tue, 9 Apr 2019 11:55:57 +0000 Subject: Complete unit test for models-pdp This review completes the unit test for the models-pdp module, for persistence of PDP groups and for PDP group filtering. Added unit test of filters for TOSCA policy types and policies. Added fix to allow filters to pass when the value being checked is null. Issue-ID: POLICY-1095 Change-Id: I982400ef39f0282d813d49e484a58207e03b8a63 Signed-off-by: liamfallon --- .../tosca/authorative/concepts/ToscaEntity.java | 2 +- .../authorative/concepts/ToscaPolicyType.java | 20 ++ .../tosca/authorative/concepts/PojosTest.java | 68 +++++++ .../tosca/authorative/concepts/TestPojos.java | 68 ------- .../authorative/concepts/TestToscaPolicy.java | 49 ----- .../concepts/TestToscaPolicyIdentifier.java | 62 ------ .../TestToscaPolicyIdentifierOptVersion.java | 71 ------- .../concepts/TestToscaPolicyTypeIdentifier.java | 63 ------ .../concepts/ToscaPolicyFilterTest.java | 214 +++++++++++++++++++++ .../ToscaPolicyIdentifierOptVersionTest.java | 71 +++++++ .../concepts/ToscaPolicyIdentifierTest.java | 62 ++++++ .../authorative/concepts/ToscaPolicyTest.java | 65 +++++++ .../concepts/ToscaPolicyTypeFilterTest.java | 175 +++++++++++++++++ .../concepts/ToscaPolicyTypeIdentifierTest.java | 63 ++++++ .../authorative/concepts/ToscaPolicyTypeTest.java | 63 ++++++ 15 files changed, 802 insertions(+), 314 deletions(-) create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/PojosTest.java delete mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestPojos.java delete mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestToscaPolicy.java delete mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestToscaPolicyIdentifier.java delete mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestToscaPolicyIdentifierOptVersion.java delete mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestToscaPolicyTypeIdentifier.java create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierOptVersionTest.java create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierTest.java create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTest.java create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeIdentifierTest.java create mode 100644 models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeTest.java (limited to 'models-tosca/src') diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntity.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntity.java index e89b31635..f5a178a37 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntity.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaEntity.java @@ -54,7 +54,7 @@ public class ToscaEntity implements PfNameVersion { private String description; /** - * Copy COnstructor. + * Copy Constructor. * * @param copyObject object to copy from */ diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyType.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyType.java index 75f17ea5b..3a3b1476d 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyType.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyType.java @@ -23,10 +23,14 @@ package org.onap.policy.models.tosca.authorative.concepts; +import java.util.LinkedHashMap; import java.util.Map; +import java.util.Map.Entry; + import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; +import lombok.NonNull; /** * Class to represent TOSCA policy type matching input/output from/to client. @@ -39,6 +43,22 @@ import lombok.NoArgsConstructor; public class ToscaPolicyType extends ToscaEntity implements Comparable { private Map properties; + /** + * Copy Constructor. + * + * @param copyObject object to copy from + */ + public ToscaPolicyType(@NonNull ToscaPolicyType copyObject) { + super(copyObject); + + if (copyObject.properties != null) { + properties = new LinkedHashMap<>(); + for (final Entry propertyEntry : copyObject.properties.entrySet()) { + properties.put(propertyEntry.getKey(), propertyEntry.getValue()); + } + } + } + @Override public int compareTo(final ToscaPolicyType other) { return compareNameVersion(this, other); diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/PojosTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/PojosTest.java new file mode 100644 index 000000000..d850052b4 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/PojosTest.java @@ -0,0 +1,68 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP Policy Model + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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.authorative.concepts; + +import com.openpojo.reflection.filters.FilterClassName; +import com.openpojo.reflection.filters.FilterPackageInfo; +import com.openpojo.validation.Validator; +import com.openpojo.validation.ValidatorBuilder; +import com.openpojo.validation.rule.impl.GetterMustExistRule; +import com.openpojo.validation.rule.impl.SetterMustExistRule; +import com.openpojo.validation.test.impl.GetterTester; +import com.openpojo.validation.test.impl.SetterTester; +import org.junit.Test; +import org.onap.policy.common.utils.validation.ToStringTester; + +/** + * Class to perform unit tests of all pojos. + * + * @author Chenfei Gao (cgao@research.att.com) + * + */ +public class PojosTest { + + private static final String POJO_PACKAGE = "org.onap.policy.models.tosca.authorative.concepts"; + + @Test + public void testPojos() { + // @formatter:off + final Validator validator = ValidatorBuilder + .create() + .with(new ToStringTester()) + .with(new SetterMustExistRule()) + .with(new GetterMustExistRule()) + .with(new SetterTester()) + .with(new GetterTester()) + .build(); + validator.validate(POJO_PACKAGE, + new FilterPackageInfo(), + new FilterClassName( + org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter.class.getName()), + new FilterClassName( + org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter.class.getName()), + new FilterClassName( + ToscaIdentifierTestBase.class.getName()) + ); + // @formatter:on + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestPojos.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestPojos.java deleted file mode 100644 index 15240665d..000000000 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestPojos.java +++ /dev/null @@ -1,68 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy Model - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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.authorative.concepts; - -import com.openpojo.reflection.filters.FilterClassName; -import com.openpojo.reflection.filters.FilterPackageInfo; -import com.openpojo.validation.Validator; -import com.openpojo.validation.ValidatorBuilder; -import com.openpojo.validation.rule.impl.GetterMustExistRule; -import com.openpojo.validation.rule.impl.SetterMustExistRule; -import com.openpojo.validation.test.impl.GetterTester; -import com.openpojo.validation.test.impl.SetterTester; -import org.junit.Test; -import org.onap.policy.common.utils.validation.ToStringTester; - -/** - * Class to perform unit tests of all pojos. - * - * @author Chenfei Gao (cgao@research.att.com) - * - */ -public class TestPojos { - - private static final String POJO_PACKAGE = "org.onap.policy.models.tosca.authorative.concepts"; - - @Test - public void testPojos() { - // @formatter:off - final Validator validator = ValidatorBuilder - .create() - .with(new ToStringTester()) - .with(new SetterMustExistRule()) - .with(new GetterMustExistRule()) - .with(new SetterTester()) - .with(new GetterTester()) - .build(); - validator.validate(POJO_PACKAGE, - new FilterPackageInfo(), - new FilterClassName( - org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyFilter.class.getName()), - new FilterClassName( - org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeFilter.class.getName()), - new FilterClassName( - ToscaIdentifierTestBase.class.getName()) - ); - // @formatter:on - } -} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestToscaPolicy.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestToscaPolicy.java deleted file mode 100644 index 881a69d07..000000000 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestToscaPolicy.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP Policy Models - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca.authorative.concepts; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -/** - * Tests methods not tested by {@link TestPojos}. - */ -public class TestToscaPolicy { - - @Test - public void testGetIdentifier_testGetTypeIdentifier() { - ToscaPolicy policy = new ToscaPolicy(); - - policy.setName("my_name"); - policy.setVersion("1.2.3"); - policy.setType("my_type"); - policy.setTypeVersion("3.2.1"); - - ToscaPolicyIdentifier ident = policy.getIdentifier(); - assertEquals("my_name", ident.getName()); - assertEquals("1.2.3", ident.getVersion()); - - ToscaPolicyTypeIdentifier type = policy.getTypeIdentifier(); - assertEquals("my_type", type.getName()); - assertEquals("3.2.1", type.getVersion()); - } -} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestToscaPolicyIdentifier.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestToscaPolicyIdentifier.java deleted file mode 100644 index 0dc9eb13c..000000000 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestToscaPolicyIdentifier.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP Policy Models - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca.authorative.concepts; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -/** - * Test the other constructors, as {@link TestPojos} tests the other methods. - */ -public class TestToscaPolicyIdentifier extends ToscaIdentifierTestBase { - private static final String NAME = "my-name"; - private static final String VERSION = "1.2.3"; - - public TestToscaPolicyIdentifier() { - super(ToscaPolicyIdentifier.class); - } - - @Test - public void testAllArgsConstructor() { - assertThatThrownBy(() -> new ToscaPolicyIdentifier(null, VERSION)).isInstanceOf(NullPointerException.class); - assertThatThrownBy(() -> new ToscaPolicyIdentifier(NAME, null)).isInstanceOf(NullPointerException.class); - - ToscaPolicyIdentifier orig = new ToscaPolicyIdentifier(NAME, VERSION); - assertEquals(NAME, orig.getName()); - assertEquals(VERSION, orig.getVersion()); - } - - @Test - public void testCopyConstructor() { - assertThatThrownBy(() -> new ToscaPolicyIdentifier(null)).isInstanceOf(NullPointerException.class); - - ToscaPolicyIdentifier orig = new ToscaPolicyIdentifier(); - - // verify with null values - assertEquals(orig.toString(), new ToscaPolicyIdentifier(orig).toString()); - - // verify with all values - orig = new ToscaPolicyIdentifier(NAME, VERSION); - assertEquals(orig.toString(), new ToscaPolicyIdentifier(orig).toString()); - } -} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestToscaPolicyIdentifierOptVersion.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestToscaPolicyIdentifierOptVersion.java deleted file mode 100644 index 999dca565..000000000 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestToscaPolicyIdentifierOptVersion.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP Policy Models - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca.authorative.concepts; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import org.junit.Test; - -/** - * Test the other constructors, as {@link TestPojos} tests the other methods. - */ -public class TestToscaPolicyIdentifierOptVersion extends ToscaIdentifierTestBase { - private static final String NAME = "my-name"; - private static final String VERSION = "1.2.3"; - - public TestToscaPolicyIdentifierOptVersion() { - super(ToscaPolicyIdentifierOptVersion.class); - } - - @Test - public void testAllArgsConstructor_testIsNullVersion() { - assertThatThrownBy(() -> new ToscaPolicyIdentifierOptVersion(null, VERSION)) - .isInstanceOf(NullPointerException.class); - - // with null version - ToscaPolicyIdentifierOptVersion orig = new ToscaPolicyIdentifierOptVersion(NAME, null); - assertEquals(NAME, orig.getName()); - assertEquals(null, orig.getVersion()); - assertTrue(orig.isNullVersion()); - - orig = new ToscaPolicyIdentifierOptVersion(NAME, VERSION); - assertEquals(NAME, orig.getName()); - assertEquals(VERSION, orig.getVersion()); - assertFalse(orig.isNullVersion()); - } - - @Test - public void testCopyConstructor() throws Exception { - assertThatThrownBy(() -> new ToscaPolicyIdentifierOptVersion(null)).isInstanceOf(NullPointerException.class); - - ToscaPolicyIdentifierOptVersion orig = new ToscaPolicyIdentifierOptVersion(); - - // verify with null values - assertEquals(orig.toString(), new ToscaPolicyIdentifierOptVersion(orig).toString()); - - // verify with all values - orig = makeIdent(NAME, VERSION); - assertEquals(orig.toString(), new ToscaPolicyIdentifierOptVersion(orig).toString()); - } -} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestToscaPolicyTypeIdentifier.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestToscaPolicyTypeIdentifier.java deleted file mode 100644 index 778d60c09..000000000 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/TestToscaPolicyTypeIdentifier.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * ONAP Policy Models - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.tosca.authorative.concepts; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -/** - * Test the other constructors, as {@link TestPojos} tests the other methods. - */ -public class TestToscaPolicyTypeIdentifier extends ToscaIdentifierTestBase { - private static final String NAME = "my-name"; - private static final String VERSION = "1.2.3"; - - public TestToscaPolicyTypeIdentifier() { - super(ToscaPolicyTypeIdentifier.class); - } - - @Test - public void testAllArgsConstructor() { - assertThatThrownBy(() -> new ToscaPolicyTypeIdentifier(null, VERSION)).isInstanceOf(NullPointerException.class); - assertThatThrownBy(() -> new ToscaPolicyTypeIdentifier(NAME, null)).isInstanceOf(NullPointerException.class); - - ToscaPolicyTypeIdentifier orig = new ToscaPolicyTypeIdentifier(NAME, VERSION); - assertEquals(NAME, orig.getName()); - assertEquals(VERSION, orig.getVersion()); - } - - @Test - public void testCopyConstructor() { - assertThatThrownBy(() -> new ToscaPolicyTypeIdentifier(null)).isInstanceOf(NullPointerException.class); - - ToscaPolicyTypeIdentifier orig = new ToscaPolicyTypeIdentifier(); - - // verify with null values - assertEquals(orig.toString(), new ToscaPolicyTypeIdentifier(orig).toString()); - - // verify with all values - orig = new ToscaPolicyTypeIdentifier(NAME, VERSION); - assertEquals(orig.toString(), new ToscaPolicyTypeIdentifier(orig).toString()); - } - -} 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 new file mode 100644 index 000000000..4653296b7 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java @@ -0,0 +1,214 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.authorative.concepts; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import com.google.gson.GsonBuilder; + +import java.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; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.base.PfKey; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.yaml.snakeyaml.Yaml; + +/** + * Test of the {@link ToscaPolicyFilter} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaPolicyFilterTest { + // Logger for this class + private static final Logger LOGGER = LoggerFactory.getLogger(ToscaPolicyFilterTest.class); + + // @formatter:off + private static final String[] policyResourceNames = { + "policies/vCPE.policies.optimization.input.tosca.yaml", + "policies/vCPE.policy.monitoring.input.tosca.json", + "policies/vCPE.policy.monitoring.input.tosca.yaml", + "policies/vCPE.policy.operational.input.tosca.yaml", + "policies/vDNS.policy.guard.frequency.input.tosca.json", + "policies/vDNS.policy.guard.frequency.input.tosca.yaml", + "policies/vDNS.policy.guard.minmax.input.tosca.yaml", + "policies/vDNS.policy.monitoring.input.tosca.json", + "policies/vDNS.policy.monitoring.input.tosca.yaml", + "policies/vDNS.policy.operational.input.tosca.yaml", + "policies/vFirewall.policy.monitoring.input.tosca.json", + "policies/vFirewall.policy.monitoring.input.tosca.yaml", + "policies/vFirewall.policy.operational.input.tosca.json", + "policies/vFirewall.policy.operational.input.tosca.yaml" + }; + // @formatter:on + + private static List policyList = new ArrayList<>(); + + /** + * Set up a Tosca Policy type list for filtering. + * + * @throws CoderException on JSON decoding errors + */ + @BeforeClass + public static void setupTypeList() throws CoderException { + for (String policyResourceName : policyResourceNames) { + String policyString = ResourceUtils.getResourceAsString(policyResourceName); + if (policyResourceName.endsWith("yaml")) { + Object yamlObject = new Yaml().load(policyString); + policyString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject); + } + + ToscaServiceTemplate serviceTemplate = new StandardCoder().decode(policyString, ToscaServiceTemplate.class); + assertNotNull(serviceTemplate); + + for (Map foundPolicyMap : serviceTemplate.getToscaTopologyTemplate().getPolicies()) { + for (Entry policyEntry : foundPolicyMap.entrySet()) { + ToscaPolicy policy = policyEntry.getValue(); + if (policy.getName() == null) { + policy.setName(policyEntry.getKey()); + } + if (policy.getVersion() == null) { + policy.setVersion(PfKey.NULL_KEY_VERSION); + } + if (policy.getTypeVersion() == null) { + policy.setTypeVersion(PfKey.NULL_KEY_VERSION); + } + if (!policyList.contains(policy)) { + policyList.add(policy); + } + } + } + } + + for (ToscaPolicy policy : policyList) { + LOGGER.info("using policy-" + policy.getName() + ":" + policy.getVersion() + ", type-" + policy.getType() + + ":" + policy.getTypeVersion()); + } + } + + @Test + public void testNullList() { + ToscaPolicyFilter filter = ToscaPolicyFilter.builder().build(); + + assertThatThrownBy(() -> { + filter.filter(null); + }).hasMessage("originalList is marked @NonNull but is null"); + } + + @Test + public void testFilterNothing() { + ToscaPolicyFilter filter = ToscaPolicyFilter.builder().build(); + + List filteredList = filter.filter(policyList); + assertTrue(filteredList.containsAll(policyList)); + } + + @Test + public void testFilterLatestVersion() { + ToscaPolicyFilter filter = ToscaPolicyFilter.builder().version(ToscaPolicyFilter.LATEST_VERSION).build(); + + List filteredList = filter.filter(policyList); + assertEquals(15, filteredList.size()); + assertEquals("1.0.0", filteredList.get(7).getVersion()); + assertEquals("1.0.0", filteredList.get(12).getVersion()); + + assertEquals(17, policyList.size()); + assertEquals(15, filteredList.size()); + + policyList.get(10).setVersion("2.0.0"); + policyList.get(16).setVersion("3.4.5"); + filteredList = filter.filter(policyList); + assertEquals(15, filteredList.size()); + assertEquals("2.0.0", filteredList.get(7).getVersion()); + assertEquals("3.4.5", filteredList.get(12).getVersion()); + + policyList.get(10).setVersion("1.0.0"); + policyList.get(16).setVersion("1.0.0"); + filteredList = filter.filter(policyList); + assertEquals(15, filteredList.size()); + assertEquals("1.0.0", filteredList.get(7).getVersion()); + assertEquals("1.0.0", filteredList.get(12).getVersion()); + } + + @Test + public void testFilterNameVersion() { + ToscaPolicyFilter filter = ToscaPolicyFilter.builder().name("operational.modifyconfig").build(); + List filteredList = filter.filter(policyList); + assertEquals(2, filteredList.size()); + + filter = ToscaPolicyFilter.builder().name("guard.frequency.scaleout").build(); + filteredList = filter.filter(policyList); + assertEquals(2, filteredList.size()); + + filter = ToscaPolicyFilter.builder().name("guard.frequency.scalein").build(); + filteredList = filter.filter(policyList); + assertEquals(0, filteredList.size()); + + filter = ToscaPolicyFilter.builder().version("1.0.0").build(); + filteredList = filter.filter(policyList); + assertEquals(17, filteredList.size()); + + filter = ToscaPolicyFilter.builder().name("OSDF_CASABLANCA.SubscriberPolicy_v1").version("1.0.0").build(); + filteredList = filter.filter(policyList); + assertEquals(1, filteredList.size()); + + filter = ToscaPolicyFilter.builder().name("operational.modifyconfig").version("1.0.0").build(); + filteredList = filter.filter(policyList); + assertEquals(2, filteredList.size()); + } + + @Test + public void testFilterTypeVersion() { + ToscaPolicyFilter filter = ToscaPolicyFilter.builder().type("onap.policies.controlloop.Operational").build(); + List filteredList = filter.filter(policyList); + assertEquals(4, filteredList.size()); + + filter = ToscaPolicyFilter.builder().type("onap.policies.monitoring.cdap.tca.hi.lo.app").build(); + filteredList = filter.filter(policyList); + assertEquals(2, filteredList.size()); + + filter = ToscaPolicyFilter.builder().type("onap.policies.controlloop.NonOperational").build(); + filteredList = filter.filter(policyList); + assertEquals(0, filteredList.size()); + + filter = ToscaPolicyFilter.builder().typeVersion("0.0.0").build(); + filteredList = filter.filter(policyList); + assertEquals(17, filteredList.size()); + + filter = ToscaPolicyFilter.builder().type("onap.policies.optimization.HpaPolicy").typeVersion("0.0.0").build(); + filteredList = filter.filter(policyList); + assertEquals(1, filteredList.size()); + + filter = ToscaPolicyFilter.builder().type("onap.policies.controlloop.Operational").typeVersion("0.0.0").build(); + filteredList = filter.filter(policyList); + assertEquals(4, filteredList.size()); + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierOptVersionTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierOptVersionTest.java new file mode 100644 index 000000000..561b4fb21 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierOptVersionTest.java @@ -0,0 +1,71 @@ +/* + * ============LICENSE_START======================================================= + * ONAP Policy Models + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.authorative.concepts; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +/** + * Test the other constructors, as {@link PojosTest} tests the other methods. + */ +public class ToscaPolicyIdentifierOptVersionTest extends ToscaIdentifierTestBase { + private static final String NAME = "my-name"; + private static final String VERSION = "1.2.3"; + + public ToscaPolicyIdentifierOptVersionTest() { + super(ToscaPolicyIdentifierOptVersion.class); + } + + @Test + public void testAllArgsConstructor_testIsNullVersion() { + assertThatThrownBy(() -> new ToscaPolicyIdentifierOptVersion(null, VERSION)) + .isInstanceOf(NullPointerException.class); + + // with null version + ToscaPolicyIdentifierOptVersion orig = new ToscaPolicyIdentifierOptVersion(NAME, null); + assertEquals(NAME, orig.getName()); + assertEquals(null, orig.getVersion()); + assertTrue(orig.isNullVersion()); + + orig = new ToscaPolicyIdentifierOptVersion(NAME, VERSION); + assertEquals(NAME, orig.getName()); + assertEquals(VERSION, orig.getVersion()); + assertFalse(orig.isNullVersion()); + } + + @Test + public void testCopyConstructor() throws Exception { + assertThatThrownBy(() -> new ToscaPolicyIdentifierOptVersion(null)).isInstanceOf(NullPointerException.class); + + ToscaPolicyIdentifierOptVersion orig = new ToscaPolicyIdentifierOptVersion(); + + // verify with null values + assertEquals(orig.toString(), new ToscaPolicyIdentifierOptVersion(orig).toString()); + + // verify with all values + orig = makeIdent(NAME, VERSION); + assertEquals(orig.toString(), new ToscaPolicyIdentifierOptVersion(orig).toString()); + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierTest.java new file mode 100644 index 000000000..a53af7b1f --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyIdentifierTest.java @@ -0,0 +1,62 @@ +/* + * ============LICENSE_START======================================================= + * ONAP Policy Models + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.authorative.concepts; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +/** + * Test the other constructors, as {@link PojosTest} tests the other methods. + */ +public class ToscaPolicyIdentifierTest extends ToscaIdentifierTestBase { + private static final String NAME = "my-name"; + private static final String VERSION = "1.2.3"; + + public ToscaPolicyIdentifierTest() { + super(ToscaPolicyIdentifier.class); + } + + @Test + public void testAllArgsConstructor() { + assertThatThrownBy(() -> new ToscaPolicyIdentifier(null, VERSION)).isInstanceOf(NullPointerException.class); + assertThatThrownBy(() -> new ToscaPolicyIdentifier(NAME, null)).isInstanceOf(NullPointerException.class); + + ToscaPolicyIdentifier orig = new ToscaPolicyIdentifier(NAME, VERSION); + assertEquals(NAME, orig.getName()); + assertEquals(VERSION, orig.getVersion()); + } + + @Test + public void testCopyConstructor() { + assertThatThrownBy(() -> new ToscaPolicyIdentifier(null)).isInstanceOf(NullPointerException.class); + + ToscaPolicyIdentifier orig = new ToscaPolicyIdentifier(); + + // verify with null values + assertEquals(orig.toString(), new ToscaPolicyIdentifier(orig).toString()); + + // verify with all values + orig = new ToscaPolicyIdentifier(NAME, VERSION); + assertEquals(orig.toString(), new ToscaPolicyIdentifier(orig).toString()); + } +} 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 new file mode 100644 index 000000000..f5be66c4a --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTest.java @@ -0,0 +1,65 @@ +/* + * ============LICENSE_START======================================================= + * ONAP Policy Models + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.authorative.concepts; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; + +import java.util.LinkedHashMap; + +import org.junit.Test; + +/** + * Tests methods not tested by {@link PojosTest}. + */ +public class ToscaPolicyTest { + + @Test + public void testGetIdentifier_testGetTypeIdentifier() { + assertThatThrownBy(() -> { + new ToscaPolicy(null); + }).hasMessage("copyObject is marked @NonNull but is null"); + + + ToscaPolicy policy = new ToscaPolicy(); + + policy.setName("my_name"); + policy.setVersion("1.2.3"); + policy.setType("my_type"); + policy.setTypeVersion("3.2.1"); + + ToscaPolicyIdentifier ident = policy.getIdentifier(); + assertEquals("my_name", ident.getName()); + assertEquals("1.2.3", ident.getVersion()); + + ToscaPolicyTypeIdentifier type = policy.getTypeIdentifier(); + assertEquals("my_type", type.getName()); + assertEquals("3.2.1", type.getVersion()); + + ToscaPolicy clonedPolicy0 = new ToscaPolicy(policy); + assertEquals(0, policy.compareTo(clonedPolicy0)); + + policy.setProperties(new LinkedHashMap()); + policy.getProperties().put("PropertyKey", "PropertyValue"); + ToscaPolicy clonedPolicy1 = new ToscaPolicy(policy); + assertEquals(0, policy.compareTo(clonedPolicy1)); + } +} 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 new file mode 100644 index 000000000..12d81ed53 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeFilterTest.java @@ -0,0 +1,175 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.authorative.concepts; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import com.google.gson.GsonBuilder; + +import java.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; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.base.PfKey; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.yaml.snakeyaml.Yaml; + +/** + * Test of the {@link ToscaPolicyTypeFilter} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaPolicyTypeFilterTest { + // Logger for this class + private static final Logger LOGGER = LoggerFactory.getLogger(ToscaPolicyTypeFilterTest.class); + + // @formatter:off + private static final String[] policyTypeResourceNames = { + "policytypes/onap.policies.monitoring.dcaegen2.collectors.datafile.datafile-app-server.yaml", + "policytypes/onap.policies.optimization.AffinityPolicy.yaml", + "policytypes/onap.policies.optimization.DistancePolicy.yaml", + "policytypes/onap.policies.optimization.HpaPolicy.yaml", + "policytypes/onap.policies.optimization.OptimizationPolicy.yaml", + "policytypes/onap.policies.optimization.PciPolicy.yaml", + "policytypes/onap.policies.optimization.QueryPolicy.yaml", + "policytypes/onap.policies.optimization.SubscriberPolicy.yaml", + "policytypes/onap.policies.optimization.Vim_fit.yaml", + "policytypes/onap.policies.optimization.VnfPolicy.yaml", + "policytypes/onap.policy.monitoring.cdap.tca.hi.lo.app.yaml" + }; + // @formatter:on + + private static List typeList = new ArrayList<>(); + + /** + * Set up a Tosca Policy type list for filtering. + * + * @throws CoderException on JSON decoding errors + */ + @BeforeClass + public static void setupTypeList() throws CoderException { + for (String policyTypeResourceName : policyTypeResourceNames) { + String policyTypeString = ResourceUtils.getResourceAsString(policyTypeResourceName); + Object yamlObject = new Yaml().load(policyTypeString); + String yamlAsJsonString = new GsonBuilder().setPrettyPrinting().create().toJson(yamlObject); + + ToscaServiceTemplate serviceTemplate = + new StandardCoder().decode(yamlAsJsonString, ToscaServiceTemplate.class); + assertNotNull(serviceTemplate); + + for (Map foundPolicyTypeMap : serviceTemplate.getPolicyTypes()) { + for (Entry policyTypeEntry : foundPolicyTypeMap.entrySet()) { + ToscaPolicyType policyType = policyTypeEntry.getValue(); + if (policyType.getName() == null) { + policyType.setName(policyTypeEntry.getKey()); + } + if (policyType.getVersion() == null) { + policyType.setVersion(PfKey.NULL_KEY_VERSION); + } + if (!typeList.contains(policyType)) { + typeList.add(policyType); + } + } + } + } + + for (ToscaPolicyType type : typeList) { + LOGGER.info("using policy type-" + type.getName() + ":" + type.getVersion()); + } + } + + @Test + public void testNullList() { + ToscaPolicyTypeFilter filter = ToscaPolicyTypeFilter.builder().build(); + + assertThatThrownBy(() -> { + filter.filter(null); + }).hasMessage("originalList is marked @NonNull but is null"); + } + + @Test + public void testFilterNothing() { + ToscaPolicyTypeFilter filter = ToscaPolicyTypeFilter.builder().build(); + + List filteredList = filter.filter(typeList); + assertTrue(filteredList.containsAll(typeList)); + } + + @Test + public void testFilterLatestVersion() { + ToscaPolicyTypeFilter filter = + ToscaPolicyTypeFilter.builder().version(ToscaPolicyTypeFilter.LATEST_VERSION).build(); + + List filteredList = filter.filter(typeList); + assertEquals(13, filteredList.size()); + assertEquals("1.0.0", filteredList.get(0).getVersion()); + assertEquals("0.0.0", filteredList.get(4).getVersion()); + + typeList.get(12).setVersion("2.0.0"); + filteredList = filter.filter(typeList); + assertEquals(13, filteredList.size()); + assertEquals("2.0.0", filteredList.get(0).getVersion()); + assertEquals("0.0.0", filteredList.get(4).getVersion()); + + typeList.get(12).setVersion("1.0.0"); + filteredList = filter.filter(typeList); + assertEquals(13, filteredList.size()); + assertEquals("1.0.0", filteredList.get(0).getVersion()); + assertEquals("0.0.0", filteredList.get(4).getVersion()); + } + + @Test + public void testFilterNameVersion() { + ToscaPolicyTypeFilter filter = ToscaPolicyTypeFilter.builder().name("onap.policies.Monitoring").build(); + List filteredList = filter.filter(typeList); + assertEquals(2, filteredList.size()); + + filter = ToscaPolicyTypeFilter.builder().name("onap.policy.monitoring.cdap.tca.hi.lo.app").build(); + filteredList = filter.filter(typeList); + assertEquals(1, filteredList.size()); + + filter = ToscaPolicyTypeFilter.builder().name("onap.policies.optimization.LpaPolicy").build(); + filteredList = filter.filter(typeList); + assertEquals(0, filteredList.size()); + + filter = ToscaPolicyTypeFilter.builder().version("0.0.0").build(); + filteredList = filter.filter(typeList); + assertEquals(9, filteredList.size()); + + filter = ToscaPolicyTypeFilter.builder().name("onap.policies.optimization.Vim_fit").version("0.0.0").build(); + filteredList = filter.filter(typeList); + assertEquals(1, filteredList.size()); + + filter = ToscaPolicyTypeFilter.builder().name("onap.policies.optimization.Vim_fit").version("0.0.1").build(); + filteredList = filter.filter(typeList); + assertEquals(0, filteredList.size()); + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeIdentifierTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeIdentifierTest.java new file mode 100644 index 000000000..8388f1061 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeIdentifierTest.java @@ -0,0 +1,63 @@ +/* + * ============LICENSE_START======================================================= + * ONAP Policy Models + * ================================================================================ + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.authorative.concepts; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +/** + * Test the other constructors, as {@link PojosTest} tests the other methods. + */ +public class ToscaPolicyTypeIdentifierTest extends ToscaIdentifierTestBase { + private static final String NAME = "my-name"; + private static final String VERSION = "1.2.3"; + + public ToscaPolicyTypeIdentifierTest() { + super(ToscaPolicyTypeIdentifier.class); + } + + @Test + public void testAllArgsConstructor() { + assertThatThrownBy(() -> new ToscaPolicyTypeIdentifier(null, VERSION)).isInstanceOf(NullPointerException.class); + assertThatThrownBy(() -> new ToscaPolicyTypeIdentifier(NAME, null)).isInstanceOf(NullPointerException.class); + + ToscaPolicyTypeIdentifier orig = new ToscaPolicyTypeIdentifier(NAME, VERSION); + assertEquals(NAME, orig.getName()); + assertEquals(VERSION, orig.getVersion()); + } + + @Test + public void testCopyConstructor() { + assertThatThrownBy(() -> new ToscaPolicyTypeIdentifier(null)).isInstanceOf(NullPointerException.class); + + ToscaPolicyTypeIdentifier orig = new ToscaPolicyTypeIdentifier(); + + // verify with null values + assertEquals(orig.toString(), new ToscaPolicyTypeIdentifier(orig).toString()); + + // verify with all values + orig = new ToscaPolicyTypeIdentifier(NAME, VERSION); + assertEquals(orig.toString(), new ToscaPolicyTypeIdentifier(orig).toString()); + } + +} 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 new file mode 100644 index 000000000..59a5a3336 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyTypeTest.java @@ -0,0 +1,63 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.models.tosca.authorative.concepts; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; + +import java.util.LinkedHashMap; + +import org.junit.Test; + +/** + * Test of the {@link ToscaPolicyType} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class ToscaPolicyTypeTest { + + @Test + public void testToscaPolicyType() { + assertThatThrownBy(() -> { + new ToscaPolicyType(null); + }).hasMessage("copyObject is marked @NonNull but is null"); + + ToscaPolicyType tpt = new ToscaPolicyType(); + tpt.setName("AType"); + tpt.setVersion("1.2.3"); + tpt.setDerivedFrom("AParentType"); + tpt.setDescription("Desc"); + + ToscaPolicyType clonedTpt0 = new ToscaPolicyType(tpt); + assertEquals(0, tpt.compareTo(clonedTpt0)); + + tpt.setMetadata(new LinkedHashMap<>()); + tpt.setProperties(new LinkedHashMap<>()); + + tpt.getMetadata().put("MetaKey0", "Metavalue 0"); + + ToscaProperty tp = new ToscaProperty(); + tpt.getProperties().put("Property0", tp); + + ToscaPolicyType clonedTpt1 = new ToscaPolicyType(tpt); + assertEquals(0, tpt.compareTo(clonedTpt1)); + } +} -- cgit 1.2.3-korg