From dd92e0c9ad7049a713c363d1c52e29237c07df62 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Mon, 8 Apr 2019 05:27:08 +0000 Subject: Add unit test for PDP groups Unit test for the JPA concepts in models-pdp. Issue-ID: POLICY-1095 Change-Id: Id33626f3789da45e36935edf564003651e9ce7c4 Signed-off-by: liamfallon --- .../models/pdp/persistence/concepts/JpaPdp.java | 10 +- .../pdp/persistence/concepts/JpaPdpGroup.java | 21 +- .../pdp/persistence/concepts/JpaPdpSubGroup.java | 48 ++-- .../pdp/persistence/concepts/JpaPdpGroupTest.java | 237 +++++++++++++++++ .../persistence/concepts/JpaPdpSubGroupTest.java | 279 +++++++++++++++++++++ .../pdp/persistence/concepts/JpaPdpTest.java | 183 ++++++++++++++ .../pdp/persistence/provider/PdpProviderTest.java | 252 +++++++++++++++++++ .../models/pdp/testconcepts/DummyJpaPdpChild.java | 32 +++ .../pdp/testconcepts/DummyJpaPdpSubgroupChild.java | 32 +++ .../persistence/provider/PdpProviderTest.java | 220 ---------------- .../test/resources/testdata/PdpGroupsNoPDPs.json | 35 +++ 11 files changed, 1094 insertions(+), 255 deletions(-) create mode 100644 models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpGroupTest.java create mode 100644 models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpSubGroupTest.java create mode 100644 models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpTest.java create mode 100644 models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpProviderTest.java create mode 100644 models-pdp/src/test/java/org/onap/policy/models/pdp/testconcepts/DummyJpaPdpChild.java create mode 100644 models-pdp/src/test/java/org/onap/policy/models/pdp/testconcepts/DummyJpaPdpSubgroupChild.java delete mode 100644 models-pdp/src/test/java/org/onap/policy/models/persistence/provider/PdpProviderTest.java create mode 100644 models-pdp/src/test/resources/testdata/PdpGroupsNoPDPs.json (limited to 'models-pdp/src') diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdp.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdp.java index 0a9aa5874..627641825 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdp.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdp.java @@ -111,7 +111,7 @@ public class JpaPdp extends PfConcept implements PfAuthorative, Serializabl * * @param copyConcept the concept to copy from */ - public JpaPdp(final JpaPdp copyConcept) { + public JpaPdp(@NonNull final JpaPdp copyConcept) { super(copyConcept); } @@ -120,7 +120,7 @@ public class JpaPdp extends PfConcept implements PfAuthorative, Serializabl * * @param authorativeConcept the authorative concept to copy from */ - public JpaPdp(final Pdp authorativeConcept) { + public JpaPdp(@NonNull final Pdp authorativeConcept) { this.fromAuthorative(authorativeConcept); } @@ -137,8 +137,8 @@ public class JpaPdp extends PfConcept implements PfAuthorative, Serializabl } @Override - public void fromAuthorative(final Pdp pdp) { - if (this.getKey().isNullKey()) { + public void fromAuthorative(@NonNull final Pdp pdp) { + if (this.key == null || this.getKey().isNullKey()) { this.setKey(new PfReferenceKey()); getKey().setLocalName(pdp.getInstanceId()); } @@ -163,7 +163,7 @@ public class JpaPdp extends PfConcept implements PfAuthorative, Serializabl } @Override - public PfValidationResult validate(final PfValidationResult resultIn) { + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { PfValidationResult result = resultIn; if (key.isNullKey()) { diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpGroup.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpGroup.java index 9c896a31b..71f27c48b 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpGroup.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpGroup.java @@ -131,7 +131,7 @@ public class JpaPdpGroup extends PfConcept implements PfAuthorative { * * @param copyConcept the concept to copy from */ - public JpaPdpGroup(final JpaPdpGroup copyConcept) { + public JpaPdpGroup(@NonNull final JpaPdpGroup copyConcept) { super(copyConcept); } @@ -140,7 +140,7 @@ public class JpaPdpGroup extends PfConcept implements PfAuthorative { * * @param authorativeConcept the authorative concept to copy from */ - public JpaPdpGroup(final PdpGroup authorativeConcept) { + public JpaPdpGroup(@NonNull final PdpGroup authorativeConcept) { this.fromAuthorative(authorativeConcept); } @@ -164,8 +164,10 @@ public class JpaPdpGroup extends PfConcept implements PfAuthorative { } @Override - public void fromAuthorative(final PdpGroup pdpGroup) { - this.setKey(new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion())); + public void fromAuthorative(@NonNull final PdpGroup pdpGroup) { + if (this.key == null || this.getKey().isNullKey()) { + this.setKey(new PfConceptKey(pdpGroup.getName(), pdpGroup.getVersion())); + } this.description = pdpGroup.getDescription(); this.pdpGroupState = pdpGroup.getPdpGroupState(); @@ -190,7 +192,6 @@ public class JpaPdpGroup extends PfConcept implements PfAuthorative { keyList.addAll(jpaPdpSubgroup.getKeys()); } - return keyList; } @@ -214,7 +215,7 @@ public class JpaPdpGroup extends PfConcept implements PfAuthorative { } @Override - public PfValidationResult validate(final PfValidationResult resultIn) { + public PfValidationResult validate(@NonNull final PfValidationResult resultIn) { PfValidationResult result = resultIn; if (key.isNullKey()) { @@ -224,11 +225,16 @@ public class JpaPdpGroup extends PfConcept implements PfAuthorative { result = key.validate(result); - if (StringUtils.isBlank(description)) { + if (description != null && StringUtils.isBlank(description)) { result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "description may not be blank")); } + if (pdpGroupState == null) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "pdpGroupState may not be null")); + } + if (properties != null) { for (Entry propertyEntry : properties.entrySet()) { if (!ParameterValidationUtils.validateStringParameter(propertyEntry.getKey())) { @@ -242,7 +248,6 @@ public class JpaPdpGroup extends PfConcept implements PfAuthorative { } } - if (pdpSubGroups == null) { result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "a PDP group must have a list of PDP subgroups")); diff --git a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpSubGroup.java b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpSubGroup.java index 1937cbfbb..d51cfc67e 100644 --- a/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpSubGroup.java +++ b/models-pdp/src/main/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpSubGroup.java @@ -91,12 +91,11 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative pdpInstances; @@ -137,7 +136,7 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative(); - for (ToscaPolicyTypeIdentifier supportedPolicyType : pdpSubgroup.getSupportedPolicyTypes()) { - this.supportedPolicyTypes - .add(new PfConceptKey(supportedPolicyType.getName(), supportedPolicyType.getVersion())); + if (pdpSubgroup.getSupportedPolicyTypes() != null) { + for (ToscaPolicyTypeIdentifier supportedPolicyType : pdpSubgroup.getSupportedPolicyTypes()) { + this.supportedPolicyTypes + .add(new PfConceptKey(supportedPolicyType.getName(), supportedPolicyType.getVersion())); + } } - this.policies = new ArrayList<>(); - for (ToscaPolicyIdentifier toscaPolicyIdentifier : pdpSubgroup.getPolicies()) { - this.policies.add(new PfConceptKey(toscaPolicyIdentifier.getName(), toscaPolicyIdentifier.getVersion())); + if (pdpSubgroup.getPolicies() != null) { + for (ToscaPolicyIdentifier toscaPolicyIdentifier : pdpSubgroup.getPolicies()) { + this.policies + .add(new PfConceptKey(toscaPolicyIdentifier.getName(), toscaPolicyIdentifier.getVersion())); + } } - this.currentInstanceCount = pdpSubgroup.getCurrentInstanceCount(); this.desiredInstanceCount = pdpSubgroup.getDesiredInstanceCount(); this.properties = (pdpSubgroup.getProperties() == null ? null : new LinkedHashMap<>(pdpSubgroup.getProperties())); this.pdpInstances = new ArrayList<>(); - for (Pdp pdp : pdpSubgroup.getPdpInstances()) { - JpaPdp jpaPdp = new JpaPdp(); - jpaPdp.setKey(new PfReferenceKey(getKey(), pdp.getInstanceId())); - jpaPdp.fromAuthorative(pdp); - this.pdpInstances.add(jpaPdp); + if (pdpSubgroup.getPdpInstances() != null) { + for (Pdp pdp : pdpSubgroup.getPdpInstances()) { + JpaPdp jpaPdp = new JpaPdp(); + jpaPdp.setKey(new PfReferenceKey(getKey(), pdp.getInstanceId())); + jpaPdp.fromAuthorative(pdp); + this.pdpInstances.add(jpaPdp); + } } } @@ -262,7 +266,7 @@ public class JpaPdpSubGroup extends PfConcept implements PfAuthorative { + new JpaPdpGroup((JpaPdpGroup) null); + }).hasMessage("copyConcept is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdpGroup((PfConceptKey) null); + }).hasMessage("key is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdpGroup((PdpGroup) null); + }).hasMessage("authorativeConcept is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdpGroup((JpaPdpGroup) null); + }).hasMessage("copyConcept is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdpGroup(null, null, null); + }).hasMessage("key is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdpGroup(new PfConceptKey(), null, null); + }).hasMessage("pdpGroupState is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdpGroup(new PfConceptKey(), PdpState.PASSIVE, null); + }).hasMessage("pdpSubGroups is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdpGroup(null, PdpState.PASSIVE, null); + }).hasMessage("key is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdpGroup(null, PdpState.PASSIVE, new ArrayList<>()); + }).hasMessage("key is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdpGroup(null, null, new ArrayList<>()); + }).hasMessage("key is marked @NonNull but is null"); + + assertNotNull(new JpaPdpGroup((new PfConceptKey()))); + assertNotNull(new JpaPdpGroup((new JpaPdpGroup()))); + + PdpGroup testPdpGroup = new PdpGroup(); + testPdpGroup.setName("PDPGroup0"); + testPdpGroup.setPdpSubgroups(new ArrayList<>()); + JpaPdpGroup testJpaPdpGroup = new JpaPdpGroup(); + testJpaPdpGroup.setKey(null); + + assertThatThrownBy(() -> { + testJpaPdpGroup.fromAuthorative(testPdpGroup); + }).hasMessage("version is marked @NonNull but is null"); + + testJpaPdpGroup.setKey(new PfConceptKey()); + + assertThatThrownBy(() -> { + testJpaPdpGroup.fromAuthorative(testPdpGroup); + }).hasMessage("version is marked @NonNull but is null"); + + testPdpGroup.setVersion("1.0.0"); + testJpaPdpGroup.fromAuthorative(testPdpGroup); + + assertEquals("PDPGroup0", testJpaPdpGroup.getKey().getName()); + testJpaPdpGroup.setKey(PfConceptKey.getNullKey()); + testJpaPdpGroup.fromAuthorative(testPdpGroup); + + assertThatThrownBy(() -> { + testJpaPdpGroup.fromAuthorative(null); + }).hasMessage("pdpGroup is marked @NonNull but is null"); + + testJpaPdpGroup.setKey(new PfConceptKey("PDPGroup0", "1.0.0")); + testJpaPdpGroup.fromAuthorative(testPdpGroup); + + assertThatThrownBy(() -> { + testJpaPdpGroup.copyTo(null); + }).hasMessage("target is marked @NonNull but is null"); + + assertEquals("PDPGroup0", testJpaPdpGroup.getKey().getName()); + assertEquals("PDPGroup0", new JpaPdpGroup(testPdpGroup).getKey().getName()); + assertEquals("PDPGroup0", ((PfConceptKey) new JpaPdpGroup(testPdpGroup).getKeys().get(0)).getName()); + + testJpaPdpGroup.clean(); + assertEquals("PDPGroup0", testJpaPdpGroup.getKey().getName()); + + assertThatThrownBy(() -> { + testJpaPdpGroup.validate(null); + }).hasMessage("resultIn is marked @NonNull but is null"); + + assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk()); + testJpaPdpGroup.setPdpGroupState(PdpState.PASSIVE); + assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk()); + + testJpaPdpGroup.setKey(PfConceptKey.getNullKey()); + assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk()); + testJpaPdpGroup.setKey(new PfConceptKey("PdpGroup0", "1.0.0")); + assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk()); + + testJpaPdpGroup.setDescription(" "); + assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk()); + testJpaPdpGroup.setDescription(" A Description "); + assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk()); + testJpaPdpGroup.setDescription(null); + assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk()); + + testJpaPdpGroup.setProperties(new LinkedHashMap<>()); + testJpaPdpGroup.getProperties().put(null, null); + assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk()); + testJpaPdpGroup.getProperties().remove(null); + assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk()); + + testJpaPdpGroup.setProperties(new LinkedHashMap<>()); + testJpaPdpGroup.getProperties().put("NullKey", null); + assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk()); + testJpaPdpGroup.getProperties().remove("NullKey"); + assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk()); + + List jpaPdpSubgroups = testJpaPdpGroup.getPdpSubGroups(); + assertNotNull(jpaPdpSubgroups); + testJpaPdpGroup.setPdpSubGroups(null); + assertFalse(testJpaPdpGroup.validate(new PfValidationResult()).isOk()); + testJpaPdpGroup.setPdpSubGroups(new ArrayList<>()); + assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk()); + testJpaPdpGroup.setPdpSubGroups(jpaPdpSubgroups); + assertTrue(testJpaPdpGroup.validate(new PfValidationResult()).isOk()); + + JpaPdpGroup otherJpaPdpGroup = new JpaPdpGroup(testJpaPdpGroup); + assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup)); + assertEquals(-1, testJpaPdpGroup.compareTo(null)); + assertEquals(0, testJpaPdpGroup.compareTo(testJpaPdpGroup)); + assertFalse(testJpaPdpGroup.compareTo(new DummyJpaPdpSubgroupChild()) == 0); + + testJpaPdpGroup.getKey().setName("OtherName"); + assertEquals(-1, testJpaPdpGroup.compareTo(otherJpaPdpGroup)); + testJpaPdpGroup.getKey().setName("PdpGroup0"); + assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup)); + + JpaPdpSubGroup anotherPdpSubgroup = + new JpaPdpSubGroup(new PfReferenceKey(testJpaPdpGroup.getKey(), "AnotherPdpSubgroup")); + testJpaPdpGroup.getPdpSubGroups().add(anotherPdpSubgroup); + assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup)); + testJpaPdpGroup.getPdpSubGroups().remove(anotherPdpSubgroup); + assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup)); + + testJpaPdpGroup.setPdpGroupState(PdpState.ACTIVE); + assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup)); + testJpaPdpGroup.setPdpGroupState(PdpState.PASSIVE); + assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup)); + + testJpaPdpGroup.setDescription("A Description"); + assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup)); + testJpaPdpGroup.setDescription(null); + assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup)); + + testJpaPdpGroup.getProperties().put("AnotherProperty", "Some String"); + assertNotEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup)); + testJpaPdpGroup.getProperties().remove("AnotherProperty"); + assertEquals(0, testJpaPdpGroup.compareTo(otherJpaPdpGroup)); + + PdpGroup psg = testJpaPdpGroup.toAuthorative(); + assertEquals(0, psg.getProperties().size()); + + testJpaPdpGroup.setProperties(new LinkedHashMap<>()); + psg = testJpaPdpGroup.toAuthorative(); + assertEquals(0, psg.getProperties().size()); + + testJpaPdpGroup.setProperties(null); + psg = testJpaPdpGroup.toAuthorative(); + assertNull(psg.getProperties()); + testJpaPdpGroup.setProperties(new LinkedHashMap<>()); + + testJpaPdpGroup.clean(); + testJpaPdpGroup.getProperties().put(" PropKey ", " Prop Value "); + testJpaPdpGroup.clean(); + assertEquals("PropKey", testJpaPdpGroup.getProperties().keySet().iterator().next()); + assertEquals("Prop Value", testJpaPdpGroup.getProperties().get("PropKey")); + testJpaPdpGroup.setDescription(" A Description "); + testJpaPdpGroup.clean(); + assertEquals("A Description", testJpaPdpGroup.getDescription()); + + assertEquals(1, testJpaPdpGroup.getKeys().size()); + testJpaPdpGroup.getPdpSubGroups().add(anotherPdpSubgroup); + assertEquals(2, testJpaPdpGroup.getKeys().size()); + testJpaPdpGroup.clean(); + assertEquals(2, testJpaPdpGroup.getKeys().size()); + } +} diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpSubGroupTest.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpSubGroupTest.java new file mode 100644 index 000000000..d066d9be7 --- /dev/null +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpSubGroupTest.java @@ -0,0 +1,279 @@ +/*- + * ============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.pdp.persistence.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.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; + +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.pdp.concepts.PdpSubGroup; +import org.onap.policy.models.pdp.testconcepts.DummyJpaPdpSubgroupChild; + +/** + * Test the {@link JpaPdpSubGroupSubGroup} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class JpaPdpSubGroupTest { + + @Test + public void testJpaPdpSubGroup() { + assertThatThrownBy(() -> { + new JpaPdpSubGroup((JpaPdpSubGroup) null); + }).hasMessage("copyConcept is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdpSubGroup((PfReferenceKey) null); + }).hasMessage("key is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdpSubGroup((PdpSubGroup) null); + }).hasMessage("authorativeConcept is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdpSubGroup(null, null, null, null); + }).hasMessage("key is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdpSubGroup(new PfReferenceKey(), null, null, null); + }).hasMessage("supportedPolicyTypes is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdpSubGroup(new PfReferenceKey(), new ArrayList<>(), null, null); + }).hasMessage("policies is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdpSubGroup(null, new ArrayList<>(), null, null); + }).hasMessage("key is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdpSubGroup(null, new ArrayList<>(), new ArrayList<>(), null); + }).hasMessage("key is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdpSubGroup(null, null, new ArrayList<>(), null); + }).hasMessage("key is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdpSubGroup(null, null, null, new ArrayList<>()); + }).hasMessage("key is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdpSubGroup(new PfReferenceKey(), null, null, new ArrayList<>()); + }).hasMessage("supportedPolicyTypes is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdpSubGroup(new PfReferenceKey(), new ArrayList<>(), null, new ArrayList<>()); + }).hasMessage("policies is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdpSubGroup(null, new ArrayList<>(), null, new ArrayList<>()); + }).hasMessage("key is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdpSubGroup(null, new ArrayList<>(), new ArrayList<>(), new ArrayList<>()); + }).hasMessage("key is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdpSubGroup(null, null, new ArrayList<>(), null); + }).hasMessage("key is marked @NonNull but is null"); + + assertNotNull(new JpaPdpSubGroup((new PfReferenceKey()))); + + PdpSubGroup testPdpSubgroup = new PdpSubGroup(); + testPdpSubgroup.setPdpType("PDP-A"); + JpaPdpSubGroup testJpaPdpSubGroup = new JpaPdpSubGroup(); + testJpaPdpSubGroup.setKey(null); + testJpaPdpSubGroup.fromAuthorative(testPdpSubgroup); + assertEquals("PDP-A", testJpaPdpSubGroup.getKey().getLocalName()); + testJpaPdpSubGroup.setKey(PfReferenceKey.getNullKey()); + testJpaPdpSubGroup.fromAuthorative(testPdpSubgroup); + + assertThatThrownBy(() -> { + testJpaPdpSubGroup.fromAuthorative(null); + }).hasMessage("pdpSubgroup is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + testJpaPdpSubGroup.copyTo(null); + }).hasMessage("target is marked @NonNull but is null"); + + assertEquals("PDP-A", testJpaPdpSubGroup.getKey().getLocalName()); + assertEquals("PDP-A", new JpaPdpSubGroup(testPdpSubgroup).getKey().getLocalName()); + assertEquals("PDP-A", ((PfReferenceKey) new JpaPdpSubGroup(testPdpSubgroup).getKeys().get(0)).getLocalName()); + + testJpaPdpSubGroup.clean(); + assertEquals("PDP-A", testJpaPdpSubGroup.getKey().getLocalName()); + + assertThatThrownBy(() -> { + testJpaPdpSubGroup.validate(null); + }).hasMessage("resultIn is marked @NonNull but is null"); + + assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk()); + assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).toString() + .contains("INVALID:parent of key is a null key")); + + testJpaPdpSubGroup.getKey().setParentConceptKey(new PfConceptKey("Parent:1.0.0")); + assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk()); + assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).toString() + .contains("INVALID:parent of key is a null key")); + assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).toString() + .contains("INVALID:a PDP subgroup must support at least one policy type")); + + testJpaPdpSubGroup.setSupportedPolicyTypes(new ArrayList<>()); + testJpaPdpSubGroup.getSupportedPolicyTypes().add(new PfConceptKey("APolicyType:1.0.0")); + assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk()); + assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).toString() + .contains("INVALID:a PDP subgroup must support at least one policy type")); + + PfReferenceKey savedKey = testJpaPdpSubGroup.getKey(); + testJpaPdpSubGroup.setKey(PfReferenceKey.getNullKey()); + assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk()); + testJpaPdpSubGroup.setKey(savedKey); + assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk()); + + testJpaPdpSubGroup.setProperties(new LinkedHashMap<>()); + testJpaPdpSubGroup.getProperties().put(null, null); + assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk()); + testJpaPdpSubGroup.getProperties().remove(null); + assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk()); + + testJpaPdpSubGroup.setProperties(new LinkedHashMap<>()); + testJpaPdpSubGroup.getProperties().put("NullKey", null); + assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk()); + testJpaPdpSubGroup.getProperties().remove("NullKey"); + assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk()); + + testJpaPdpSubGroup.setDesiredInstanceCount(-1); + assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk()); + testJpaPdpSubGroup.setDesiredInstanceCount(0); + assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk()); + testJpaPdpSubGroup.setProperties(null); + + testJpaPdpSubGroup.setCurrentInstanceCount(-1); + assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk()); + testJpaPdpSubGroup.setCurrentInstanceCount(0); + assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk()); + testJpaPdpSubGroup.setProperties(null); + assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk()); + + List supportedPolicyTypes = testJpaPdpSubGroup.getSupportedPolicyTypes(); + assertNotNull(supportedPolicyTypes); + testJpaPdpSubGroup.setSupportedPolicyTypes(null); + assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk()); + testJpaPdpSubGroup.setSupportedPolicyTypes(new ArrayList<>()); + assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk()); + testJpaPdpSubGroup.setSupportedPolicyTypes(supportedPolicyTypes); + assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk()); + + List supportedPolicies = testJpaPdpSubGroup.getPolicies(); + assertNotNull(supportedPolicies); + testJpaPdpSubGroup.setPolicies(null); + assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk()); + testJpaPdpSubGroup.setPolicies(new ArrayList<>()); + assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk()); + testJpaPdpSubGroup.setPolicies(supportedPolicies); + assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk()); + + List pdpInstances = testJpaPdpSubGroup.getPdpInstances(); + assertNotNull(pdpInstances); + testJpaPdpSubGroup.setPdpInstances(null); + assertFalse(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk()); + testJpaPdpSubGroup.setPdpInstances(new ArrayList<>()); + assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk()); + testJpaPdpSubGroup.setPdpInstances(pdpInstances); + assertTrue(testJpaPdpSubGroup.validate(new PfValidationResult()).isOk()); + + JpaPdpSubGroup otherJpaPdpSubGroup = new JpaPdpSubGroup(testJpaPdpSubGroup); + assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup)); + assertEquals(-1, testJpaPdpSubGroup.compareTo(null)); + assertEquals(0, testJpaPdpSubGroup.compareTo(testJpaPdpSubGroup)); + assertFalse(testJpaPdpSubGroup.compareTo(new DummyJpaPdpSubgroupChild()) == 0); + + testJpaPdpSubGroup.getKey().setParentKeyName("Parent1"); + assertEquals(1, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup)); + testJpaPdpSubGroup.getKey().setParentKeyName("Parent"); + assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup)); + + testJpaPdpSubGroup.setCurrentInstanceCount(1); + assertEquals(1, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup)); + testJpaPdpSubGroup.setCurrentInstanceCount(0); + assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup)); + + testJpaPdpSubGroup.setDesiredInstanceCount(1); + assertEquals(1, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup)); + testJpaPdpSubGroup.setDesiredInstanceCount(0); + assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup)); + + PfConceptKey anotherPolicyType = new PfConceptKey("AnotherPolicyType", "1.0.0"); + testJpaPdpSubGroup.getSupportedPolicyTypes().add(anotherPolicyType); + assertNotEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup)); + testJpaPdpSubGroup.getSupportedPolicyTypes().remove(anotherPolicyType); + assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup)); + + PfConceptKey anotherPolicy = new PfConceptKey("AnotherPolicy", "1.0.0"); + testJpaPdpSubGroup.getPolicies().add(anotherPolicy); + assertNotEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup)); + testJpaPdpSubGroup.getPolicies().remove(anotherPolicy); + assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup)); + + JpaPdp anotherPdp = new JpaPdp(new PfReferenceKey(testJpaPdpSubGroup.getKey(), "AnotherPdp")); + testJpaPdpSubGroup.getPdpInstances().add(anotherPdp); + assertNotEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup)); + testJpaPdpSubGroup.getPdpInstances().remove(anotherPdp); + assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup)); + + testJpaPdpSubGroup.setProperties(new LinkedHashMap<>()); + testJpaPdpSubGroup.getProperties().put("AnotherProperty", "Some String"); + assertNotEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup)); + testJpaPdpSubGroup.getProperties().remove("AnotherProperty"); + testJpaPdpSubGroup.setProperties(null); + assertEquals(0, testJpaPdpSubGroup.compareTo(otherJpaPdpSubGroup)); + + PdpSubGroup psg = testJpaPdpSubGroup.toAuthorative(); + assertNull(psg.getProperties()); + + testJpaPdpSubGroup.setProperties(new LinkedHashMap<>()); + psg = testJpaPdpSubGroup.toAuthorative(); + assertEquals(0, psg.getProperties().size()); + + testJpaPdpSubGroup.getPolicies().add(new PfConceptKey("APolicy:1.0.0")); + testJpaPdpSubGroup.getPdpInstances().add(new JpaPdp()); + + testJpaPdpSubGroup.getProperties().put(" PropKey ", " Prop Value "); + testJpaPdpSubGroup.clean(); + assertEquals("PropKey", testJpaPdpSubGroup.getProperties().keySet().iterator().next()); + assertEquals("Prop Value", testJpaPdpSubGroup.getProperties().get("PropKey")); + + assertEquals(4, testJpaPdpSubGroup.getKeys().size()); + } +} diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpTest.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpTest.java new file mode 100644 index 000000000..5a27210fc --- /dev/null +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/concepts/JpaPdpTest.java @@ -0,0 +1,183 @@ +/*- + * ============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.pdp.persistence.concepts; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfValidationResult; +import org.onap.policy.models.pdp.concepts.Pdp; +import org.onap.policy.models.pdp.enums.PdpHealthStatus; +import org.onap.policy.models.pdp.enums.PdpState; +import org.onap.policy.models.pdp.persistence.concepts.JpaPdp; +import org.onap.policy.models.pdp.testconcepts.DummyJpaPdpChild; + +/** + * Test the {@link JpaPdp} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class JpaPdpTest { + + @Test + public void testJpaPdp() { + assertThatThrownBy(() -> { + new JpaPdp((JpaPdp) null); + }).hasMessage("copyConcept is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdp((PfReferenceKey) null); + }).hasMessage("key is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdp(null, null, null); + }).hasMessage("key is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdp(new PfReferenceKey(), null, null); + }).hasMessage("pdpState is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdp(new PfReferenceKey(), PdpState.ACTIVE, null); + }).hasMessage("healthy is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdp(null, PdpState.ACTIVE, null); + }).hasMessage("key is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdp(null, PdpState.ACTIVE, PdpHealthStatus.UNKNOWN); + }).hasMessage("key is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdp(null, null, PdpHealthStatus.UNKNOWN); + }).hasMessage("key is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new JpaPdp((Pdp) null); + }).hasMessage("authorativeConcept is marked @NonNull but is null"); + + assertNotNull(new JpaPdp((new PfReferenceKey()))); + + Pdp testPdp = new Pdp(); + testPdp.setInstanceId("ThePDP"); + JpaPdp testJpaPdp = new JpaPdp(); + testJpaPdp.setKey(null); + testJpaPdp.fromAuthorative(testPdp); + assertEquals("ThePDP", testJpaPdp.getKey().getLocalName()); + testJpaPdp.setKey(PfReferenceKey.getNullKey()); + testJpaPdp.fromAuthorative(testPdp); + + assertThatThrownBy(() -> { + testJpaPdp.fromAuthorative(null); + }).hasMessage("pdp is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + testJpaPdp.copyTo(null); + }).hasMessage("target is marked @NonNull but is null"); + + assertEquals("ThePDP", testJpaPdp.getKey().getLocalName()); + assertEquals("ThePDP", new JpaPdp(testPdp).getKey().getLocalName()); + assertEquals("ThePDP", ((PfReferenceKey) new JpaPdp(testPdp).getKeys().get(0)).getLocalName()); + + testJpaPdp.clean(); + assertEquals("ThePDP", testJpaPdp.getKey().getLocalName()); + + testJpaPdp.setMessage(" A Message "); + testJpaPdp.clean(); + assertEquals("A Message", testJpaPdp.getMessage()); + + assertThatThrownBy(() -> { + testJpaPdp.validate(null); + }).hasMessage("resultIn is marked @NonNull but is null"); + + assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk()); + assertTrue(testJpaPdp.validate(new PfValidationResult()).toString() + .contains("INVALID:parent of key is a null key")); + + testJpaPdp.getKey().setParentConceptKey(new PfConceptKey("Parent:1.0.0")); + assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk()); + assertFalse(testJpaPdp.validate(new PfValidationResult()).toString() + .contains("INVALID:parent of key is a null key")); + assertTrue(testJpaPdp.validate(new PfValidationResult()).toString() + .contains("INVALID:local name of parent of key is null")); + + testJpaPdp.getKey().setParentLocalName("ParentLocal"); + assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk()); + assertFalse(testJpaPdp.validate(new PfValidationResult()).toString() + .contains("INVALID:local name of parent of key is null")); + assertTrue(testJpaPdp.validate(new PfValidationResult()).toString() + .contains("INVALID:PDP state may not be null")); + + testJpaPdp.setPdpState(PdpState.ACTIVE); + assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk()); + assertFalse(testJpaPdp.validate(new PfValidationResult()).toString() + .contains("INVALID:PDP state may not be null")); + assertTrue(testJpaPdp.validate(new PfValidationResult()).toString() + .contains("INVALID:PDP health status may not be null")); + + testJpaPdp.setHealthy(PdpHealthStatus.HEALTHY); + assertTrue(testJpaPdp.validate(new PfValidationResult()).isOk()); + + PfReferenceKey savedKey = testJpaPdp.getKey(); + testJpaPdp.setKey(PfReferenceKey.getNullKey()); + assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk()); + testJpaPdp.setKey(savedKey); + assertTrue(testJpaPdp.validate(new PfValidationResult()).isOk()); + + testJpaPdp.setMessage(""); + assertFalse(testJpaPdp.validate(new PfValidationResult()).isOk()); + testJpaPdp.setMessage("Valid Message"); + assertTrue(testJpaPdp.validate(new PfValidationResult()).isOk()); + + JpaPdp otherJpaPdp = new JpaPdp(testJpaPdp); + assertEquals(0, testJpaPdp.compareTo(otherJpaPdp)); + assertEquals(-1, testJpaPdp.compareTo(null)); + assertEquals(0, testJpaPdp.compareTo(testJpaPdp)); + assertFalse(testJpaPdp.compareTo(new DummyJpaPdpChild()) == 0); + + testJpaPdp.getKey().setParentLocalName("ParentLocal1"); + assertEquals(1, testJpaPdp.compareTo(otherJpaPdp)); + testJpaPdp.getKey().setParentLocalName("ParentLocal"); + assertEquals(0, testJpaPdp.compareTo(otherJpaPdp)); + + testJpaPdp.setPdpState(PdpState.PASSIVE); + assertEquals(-3, testJpaPdp.compareTo(otherJpaPdp)); + testJpaPdp.setPdpState(PdpState.ACTIVE); + assertEquals(0, testJpaPdp.compareTo(otherJpaPdp)); + + testJpaPdp.setHealthy(PdpHealthStatus.NOT_HEALTHY); + assertEquals(1, testJpaPdp.compareTo(otherJpaPdp)); + testJpaPdp.setHealthy(PdpHealthStatus.HEALTHY); + assertEquals(0, testJpaPdp.compareTo(otherJpaPdp)); + + testJpaPdp.setMessage("Invalid Message"); + assertEquals(-13, testJpaPdp.compareTo(otherJpaPdp)); + testJpaPdp.setMessage("Valid Message"); + assertEquals(0, testJpaPdp.compareTo(otherJpaPdp)); + } +} diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpProviderTest.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpProviderTest.java new file mode 100644 index 000000000..468f3d4f9 --- /dev/null +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/persistence/provider/PdpProviderTest.java @@ -0,0 +1,252 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * Modifications 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.pdp.persistence.provider; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.util.ArrayList; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.resources.ResourceUtils; +import org.onap.policy.models.dao.DaoParameters; +import org.onap.policy.models.dao.PfDao; +import org.onap.policy.models.dao.PfDaoFactory; +import org.onap.policy.models.dao.impl.DefaultPfDao; +import org.onap.policy.models.pdp.concepts.PdpGroups; +import org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup; +import org.onap.policy.models.pdp.persistence.provider.PdpProvider; +import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider; + +/** + * Test the {@link SimpleToscaProvider} class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class PdpProviderTest { + private Connection connection; + private PfDao pfDao; + private StandardCoder standardCoder; + + + /** + * Set up the DAO towards the database. + * + * @throws Exception on database errors + */ + @Before + public void setupDao() throws Exception { + // Use the JDBC UI "jdbc:h2:mem:testdb" to test towards the h2 database + // Use the JDBC UI "jdbc:mariadb://localhost:3306/policy" to test towards a locally installed mariadb instance + connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "policy", "P01icY"); + + final DaoParameters daoParameters = new DaoParameters(); + daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName()); + + // Use the persistence unit ToscaConceptTest to test towards the h2 database + // Use the persistence unit ToscaConceptMariaDBTest to test towards a locally installed mariadb instance + daoParameters.setPersistenceUnit("ToscaConceptTest"); + + pfDao = new PfDaoFactory().createPfDao(daoParameters); + pfDao.init(daoParameters); + } + + /** + * Set up GSON. + */ + @Before + public void setupGson() { + standardCoder = new StandardCoder(); + } + + @After + public void teardown() throws Exception { + pfDao.close(); + connection.close(); + } + + @Test + public void testPoliciesGet() throws Exception { + assertThatThrownBy(() -> { + new PdpProvider().getPdpGroups(null, null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new PdpProvider().getPdpGroups(null, null, "version"); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new PdpProvider().getPdpGroups(null, "name", "version"); + }).hasMessage("dao is marked @NonNull but is null"); + + String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0.json"); + + PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class); + + PdpGroups createdPdpGroups0 = new PdpGroups(); + createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups())); + String createdJson = standardCoder.encode(createdPdpGroups0); + assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", "")); + + PdpGroups gotPdpGroups0 = new PdpGroups(); + gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "PdpGroup0", "1.2.3")); + + String gotJson = standardCoder.encode(gotPdpGroups0); + + assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", "")); + } + + @Test + public void testPolicyCreate() throws Exception { + assertThatThrownBy(() -> { + new PdpProvider().createPdpGroups(null, null); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new PdpProvider().createPdpGroups(null, new ArrayList<>()); + }).hasMessage("dao is marked @NonNull but is null"); + + assertThatThrownBy(() -> { + new PdpProvider().createPdpGroups(pfDao, null); + }).hasMessage("pdpGroups is marked @NonNull but is null"); + + String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0.json"); + + PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class); + + PdpGroups createdPdpGroups0 = new PdpGroups(); + createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups())); + String createdJson = standardCoder.encode(createdPdpGroups0); + assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", "")); + + PdpGroups gotPdpGroups0 = new PdpGroups(); + gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "PdpGroup0", "1.2.3")); + + String gotJson = standardCoder.encode(gotPdpGroups0); + assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", "")); + } + + @Test + public void testPolicyCreateNoPdp() throws Exception { + String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroupsNoPDPs.json"); + + PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class); + + PdpGroups createdPdpGroups0 = new PdpGroups(); + createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups())); + assertNotEquals(pdpGroups0, createdPdpGroups0); + pdpGroups0.getGroups().get(0).getPdpSubgroups().get(0).setPdpInstances(new ArrayList<>()); + String originalTweakedJson = standardCoder.encode(pdpGroups0); + String createdJson = standardCoder.encode(createdPdpGroups0); + assertEquals(originalTweakedJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", "")); + + PdpGroups gotPdpGroups0 = new PdpGroups(); + gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "TestPdpGroup", "1.2.3")); + + String gotJson = standardCoder.encode(gotPdpGroups0); + assertEquals(originalTweakedJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", "")); + } + /* + * @Test public void testPolicyUpdate() throws Exception { try { new SimpleToscaProvider().updatePolicies(null, + * null); fail("test should throw an exception here"); } catch (Exception exc) { + * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); } + * + * try { new SimpleToscaProvider().updatePolicies(null, new JpaToscaServiceTemplate()); + * fail("test should throw an exception here"); } catch (Exception exc) { + * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); } + * + * try { new SimpleToscaProvider().updatePolicies(pfDao, null); fail("test should throw an exception here"); } catch + * (Exception exc) { assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage()); } + * + * ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( + * ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), + * ToscaServiceTemplate.class); + * + * JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate(); + * originalServiceTemplate.fromAuthorative(toscaServiceTemplate); + * + * assertNotNull(originalServiceTemplate); JpaToscaServiceTemplate updatedServiceTemplate = new + * SimpleToscaProvider().updatePolicies(pfDao, originalServiceTemplate); + * + * assertEquals(originalServiceTemplate, updatedServiceTemplate); } + * + * @Test public void testPoliciesDelete() throws Exception { try { new SimpleToscaProvider().deletePolicy(null, + * null); fail("test should throw an exception here"); } catch (Exception exc) { + * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); } + * + * try { new SimpleToscaProvider().deletePolicy(null, new PfConceptKey()); + * fail("test should throw an exception here"); } catch (Exception exc) { + * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); } + * + * try { new SimpleToscaProvider().deletePolicy(pfDao, null); fail("test should throw an exception here"); } catch + * (Exception exc) { assertEquals("policyKey is marked @NonNull but is null", exc.getMessage()); } + * + * ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( + * ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), + * ToscaServiceTemplate.class); + * + * JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate(); + * originalServiceTemplate.fromAuthorative(toscaServiceTemplate); + * + * assertNotNull(originalServiceTemplate); JpaToscaServiceTemplate createdServiceTemplate = new + * SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate); + * + * assertEquals(originalServiceTemplate, createdServiceTemplate); + * + * PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); + * + * JpaToscaServiceTemplate deletedServiceTemplate = new SimpleToscaProvider().deletePolicy(pfDao, new + * PfConceptKey(policyKey)); + * + * assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey), + * deletedServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey)); + * + * try { new SimpleToscaProvider().getPolicies(pfDao, new PfConceptKey(policyKey)); + * fail("test should throw an exception here"); } catch (Exception exc) { + * assertEquals("policy not found: onap.restart.tca:1.0.0", exc.getMessage()); } } + * + * @Test public void testAssertPoliciesExist() throws PfModelException { JpaToscaServiceTemplate testServiceTemplate + * = new JpaToscaServiceTemplate(); + * + * try { new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate); + * fail("test should throw an exception here"); } catch (Exception exc) { + * assertEquals("topology template not specified on service template", exc.getMessage()); } + * + * testServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); try { new + * SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate); fail("test should throw an exception here"); } + * catch (Exception exc) { assertEquals("no policies specified on topology template of service template", + * exc.getMessage()); } + * + * testServiceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); try { new + * SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate); fail("test should throw an exception here"); } + * catch (Exception exc) { + * assertEquals("list of policies specified on topology template of service template is empty", exc.getMessage()); } + * + * } + */ +} diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/testconcepts/DummyJpaPdpChild.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/testconcepts/DummyJpaPdpChild.java new file mode 100644 index 000000000..d9d55ddc2 --- /dev/null +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/testconcepts/DummyJpaPdpChild.java @@ -0,0 +1,32 @@ +/*- + * ============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.pdp.testconcepts; + +import org.onap.policy.models.pdp.persistence.concepts.JpaPdp; + +/** + * Test class for JpaPdp comparisons. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class DummyJpaPdpChild extends JpaPdp { + private static final long serialVersionUID = -5101743610779424064L; +} diff --git a/models-pdp/src/test/java/org/onap/policy/models/pdp/testconcepts/DummyJpaPdpSubgroupChild.java b/models-pdp/src/test/java/org/onap/policy/models/pdp/testconcepts/DummyJpaPdpSubgroupChild.java new file mode 100644 index 000000000..2791e44e7 --- /dev/null +++ b/models-pdp/src/test/java/org/onap/policy/models/pdp/testconcepts/DummyJpaPdpSubgroupChild.java @@ -0,0 +1,32 @@ +/*- + * ============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.pdp.testconcepts; + +import org.onap.policy.models.pdp.persistence.concepts.JpaPdpSubGroup; + +/** + * Dummy sub PDPsubGroup class. + * + * @author Liam Fallon (liam.fallon@est.tech) + */ +public class DummyJpaPdpSubgroupChild extends JpaPdpSubGroup { + private static final long serialVersionUID = 1L; +} diff --git a/models-pdp/src/test/java/org/onap/policy/models/persistence/provider/PdpProviderTest.java b/models-pdp/src/test/java/org/onap/policy/models/persistence/provider/PdpProviderTest.java deleted file mode 100644 index 6f20882f4..000000000 --- a/models-pdp/src/test/java/org/onap/policy/models/persistence/provider/PdpProviderTest.java +++ /dev/null @@ -1,220 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * Modifications 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.persistence.provider; - -import static org.junit.Assert.assertEquals; - -import java.sql.Connection; -import java.sql.DriverManager; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.onap.policy.common.utils.coder.StandardCoder; -import org.onap.policy.common.utils.resources.ResourceUtils; -import org.onap.policy.models.dao.DaoParameters; -import org.onap.policy.models.dao.PfDao; -import org.onap.policy.models.dao.PfDaoFactory; -import org.onap.policy.models.dao.impl.DefaultPfDao; -import org.onap.policy.models.pdp.concepts.PdpGroups; -import org.onap.policy.models.pdp.persistence.provider.PdpProvider; -import org.onap.policy.models.tosca.simple.provider.SimpleToscaProvider; - -/** - * Test the {@link SimpleToscaProvider} class. - * - * @author Liam Fallon (liam.fallon@est.tech) - */ -public class PdpProviderTest { - private Connection connection; - private PfDao pfDao; - private StandardCoder standardCoder; - - - /** - * Set up the DAO towards the database. - * - * @throws Exception on database errors - */ - @Before - public void setupDao() throws Exception { - // Use the JDBC UI "jdbc:h2:mem:testdb" to test towards the h2 database - // Use the JDBC UI "jdbc:mariadb://localhost:3306/policy" to test towards a locally installed mariadb instance - connection = DriverManager.getConnection("jdbc:h2:mem:testdb", "policy", "P01icY"); - - final DaoParameters daoParameters = new DaoParameters(); - daoParameters.setPluginClass(DefaultPfDao.class.getCanonicalName()); - - // Use the persistence unit ToscaConceptTest to test towards the h2 database - // Use the persistence unit ToscaConceptMariaDBTest to test towards a locally installed mariadb instance - daoParameters.setPersistenceUnit("ToscaConceptTest"); - - pfDao = new PfDaoFactory().createPfDao(daoParameters); - pfDao.init(daoParameters); - } - - /** - * Set up GSON. - */ - @Before - public void setupGson() { - standardCoder = new StandardCoder(); - } - - @After - public void teardown() throws Exception { - pfDao.close(); - connection.close(); - } - - @Test - public void testPoliciesGet() throws Exception { - /* - * try { new PdpProvider().gePdpGroupst(null, null); fail("test should throw an exception here"); } catch - * (Exception exc) { assertEquals("dao is marked @NonNull but is null", exc.getMessage()); } - * - * try { new SimpleToscaProvider().getPolicies(null, new PfConceptKey()); - * fail("test should throw an exception here"); } catch (Exception exc) { - * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); } - * - * try { new SimpleToscaProvider().getPolicies(pfDao, null); fail("test should throw an exception here"); } - * catch (Exception exc) { assertEquals("policyKey is marked @NonNull but is null", exc.getMessage()); } - */ - - String originalJson = ResourceUtils.getResourceAsString("testdata/PdpGroups0.json"); - - PdpGroups pdpGroups0 = standardCoder.decode(originalJson, PdpGroups.class); - - PdpGroups createdPdpGroups0 = new PdpGroups(); - createdPdpGroups0.setGroups(new PdpProvider().createPdpGroups(pfDao, pdpGroups0.getGroups())); - String createdJson = standardCoder.encode(createdPdpGroups0); - assertEquals(originalJson.replaceAll("\\s+", ""), createdJson.replaceAll("\\s+", "")); - - PdpGroups gotPdpGroups0 = new PdpGroups(); - gotPdpGroups0.setGroups(new PdpProvider().getPdpGroups(pfDao, "PdpGroup0", "1.2.3")); - - String gotJson = standardCoder.encode(gotPdpGroups0); - - assertEquals(originalJson.replaceAll("\\s+", ""), gotJson.replaceAll("\\s+", "")); - - } - /* - * @Test public void testPolicyCreate() throws Exception { try { new SimpleToscaProvider().createPolicies(null, - * null); fail("test should throw an exception here"); } catch (Exception exc) { - * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); } - * - * try { new SimpleToscaProvider().createPolicies(null, new JpaToscaServiceTemplate()); - * fail("test should throw an exception here"); } catch (Exception exc) { - * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); } - * - * try { new SimpleToscaProvider().createPolicies(pfDao, null); fail("test should throw an exception here"); } catch - * (Exception exc) { assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage()); } - * - * ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( - * ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), - * ToscaServiceTemplate.class); - * - * JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate(); - * originalServiceTemplate.fromAuthorative(toscaServiceTemplate); - * - * assertNotNull(originalServiceTemplate); JpaToscaServiceTemplate createdServiceTemplate = new - * SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate); - * - * assertEquals(originalServiceTemplate, createdServiceTemplate); } - * - * @Test public void testPolicyUpdate() throws Exception { try { new SimpleToscaProvider().updatePolicies(null, - * null); fail("test should throw an exception here"); } catch (Exception exc) { - * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); } - * - * try { new SimpleToscaProvider().updatePolicies(null, new JpaToscaServiceTemplate()); - * fail("test should throw an exception here"); } catch (Exception exc) { - * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); } - * - * try { new SimpleToscaProvider().updatePolicies(pfDao, null); fail("test should throw an exception here"); } catch - * (Exception exc) { assertEquals("serviceTemplate is marked @NonNull but is null", exc.getMessage()); } - * - * ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( - * ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), - * ToscaServiceTemplate.class); - * - * JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate(); - * originalServiceTemplate.fromAuthorative(toscaServiceTemplate); - * - * assertNotNull(originalServiceTemplate); JpaToscaServiceTemplate updatedServiceTemplate = new - * SimpleToscaProvider().updatePolicies(pfDao, originalServiceTemplate); - * - * assertEquals(originalServiceTemplate, updatedServiceTemplate); } - * - * @Test public void testPoliciesDelete() throws Exception { try { new SimpleToscaProvider().deletePolicy(null, - * null); fail("test should throw an exception here"); } catch (Exception exc) { - * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); } - * - * try { new SimpleToscaProvider().deletePolicy(null, new PfConceptKey()); - * fail("test should throw an exception here"); } catch (Exception exc) { - * assertEquals("dao is marked @NonNull but is null", exc.getMessage()); } - * - * try { new SimpleToscaProvider().deletePolicy(pfDao, null); fail("test should throw an exception here"); } catch - * (Exception exc) { assertEquals("policyKey is marked @NonNull but is null", exc.getMessage()); } - * - * ToscaServiceTemplate toscaServiceTemplate = standardCoder.decode( - * ResourceUtils.getResourceAsString("policies/vCPE.policy.monitoring.input.tosca.json"), - * ToscaServiceTemplate.class); - * - * JpaToscaServiceTemplate originalServiceTemplate = new JpaToscaServiceTemplate(); - * originalServiceTemplate.fromAuthorative(toscaServiceTemplate); - * - * assertNotNull(originalServiceTemplate); JpaToscaServiceTemplate createdServiceTemplate = new - * SimpleToscaProvider().createPolicies(pfDao, originalServiceTemplate); - * - * assertEquals(originalServiceTemplate, createdServiceTemplate); - * - * PfConceptKey policyKey = new PfConceptKey("onap.restart.tca:1.0.0"); - * - * JpaToscaServiceTemplate deletedServiceTemplate = new SimpleToscaProvider().deletePolicy(pfDao, new - * PfConceptKey(policyKey)); - * - * assertEquals(originalServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey), - * deletedServiceTemplate.getTopologyTemplate().getPolicies().get(policyKey)); - * - * try { new SimpleToscaProvider().getPolicies(pfDao, new PfConceptKey(policyKey)); - * fail("test should throw an exception here"); } catch (Exception exc) { - * assertEquals("policy not found: onap.restart.tca:1.0.0", exc.getMessage()); } } - * - * @Test public void testAssertPoliciesExist() throws PfModelException { JpaToscaServiceTemplate testServiceTemplate - * = new JpaToscaServiceTemplate(); - * - * try { new SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate); - * fail("test should throw an exception here"); } catch (Exception exc) { - * assertEquals("topology template not specified on service template", exc.getMessage()); } - * - * testServiceTemplate.setTopologyTemplate(new JpaToscaTopologyTemplate()); try { new - * SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate); fail("test should throw an exception here"); } - * catch (Exception exc) { assertEquals("no policies specified on topology template of service template", - * exc.getMessage()); } - * - * testServiceTemplate.getTopologyTemplate().setPolicies(new JpaToscaPolicies()); try { new - * SimpleToscaProvider().createPolicies(pfDao, testServiceTemplate); fail("test should throw an exception here"); } - * catch (Exception exc) { - * assertEquals("list of policies specified on topology template of service template is empty", exc.getMessage()); } - * - * } - */ -} diff --git a/models-pdp/src/test/resources/testdata/PdpGroupsNoPDPs.json b/models-pdp/src/test/resources/testdata/PdpGroupsNoPDPs.json new file mode 100644 index 000000000..e37b6c5a6 --- /dev/null +++ b/models-pdp/src/test/resources/testdata/PdpGroupsNoPDPs.json @@ -0,0 +1,35 @@ +{ + "groups": [ + { + "name": "TestPdpGroup", + "version": "1.2.3", + "description": "group description", + "pdpGroupState": "ACTIVE", + "properties": { + "groupProperty0": "Value of Group Property 0" + }, + "pdpSubgroups": [ + { + "pdpType": "APEX", + "supportedPolicyTypes": [ + { + "name": "onap.policies.controlloop.Operational", + "version": "1.0.0" + } + ], + "policies": [ + { + "name": "onap.policies.controlloop.Operational.apex.sampledomain", + "version": "1.0.0" + } + ], + "currentInstanceCount": 0, + "desiredInstanceCount": 5, + "properties": { + "subgroupProperty0": "Value of sub Group Property 0" + } + } + ] + } + ] +} \ No newline at end of file -- cgit 1.2.3-korg