diff options
Diffstat (limited to 'models-tosca/src')
5 files changed, 347 insertions, 13 deletions
diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithStringProperties.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithStringProperties.java index 54bd54073..afe4a84d1 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithStringProperties.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithStringProperties.java @@ -55,7 +55,7 @@ public abstract class JpaToscaWithStringProperties<T extends ToscaWithObjectProp * The Default Constructor creates a {@link JpaToscaWithStringProperties} object with * a null key. */ - public JpaToscaWithStringProperties() { + protected JpaToscaWithStringProperties() { this(new PfConceptKey()); } @@ -65,7 +65,7 @@ public abstract class JpaToscaWithStringProperties<T extends ToscaWithObjectProp * * @param key the key */ - public JpaToscaWithStringProperties(@NonNull final PfConceptKey key) { + protected JpaToscaWithStringProperties(@NonNull final PfConceptKey key) { super(key); } @@ -74,7 +74,7 @@ public abstract class JpaToscaWithStringProperties<T extends ToscaWithObjectProp * * @param copyConcept the concept to copy from */ - public JpaToscaWithStringProperties(@NonNull final JpaToscaWithStringProperties<T> copyConcept) { + protected JpaToscaWithStringProperties(@NonNull final JpaToscaWithStringProperties<T> copyConcept) { super(copyConcept); this.properties = (copyConcept.properties != null ? new LinkedHashMap<>(copyConcept.properties) : null); } @@ -84,7 +84,7 @@ public abstract class JpaToscaWithStringProperties<T extends ToscaWithObjectProp * * @param authorativeConcept the authorative concept to copy from */ - public JpaToscaWithStringProperties(final T authorativeConcept) { + protected JpaToscaWithStringProperties(final T authorativeConcept) { super(new PfConceptKey()); this.fromAuthorative(authorativeConcept); } diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithToscaProperties.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithToscaProperties.java index 873d900b0..33e4e86b4 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithToscaProperties.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithToscaProperties.java @@ -66,7 +66,7 @@ public abstract class JpaToscaWithToscaProperties<T extends ToscaWithToscaProper * The Default Constructor creates a {@link JpaToscaWithToscaProperties} object with a * null key. */ - public JpaToscaWithToscaProperties() { + protected JpaToscaWithToscaProperties() { this(new PfConceptKey()); } @@ -76,7 +76,7 @@ public abstract class JpaToscaWithToscaProperties<T extends ToscaWithToscaProper * * @param key the key */ - public JpaToscaWithToscaProperties(@NonNull final PfConceptKey key) { + protected JpaToscaWithToscaProperties(@NonNull final PfConceptKey key) { super(key); } @@ -85,7 +85,7 @@ public abstract class JpaToscaWithToscaProperties<T extends ToscaWithToscaProper * * @param copyConcept the concept to copy from */ - public JpaToscaWithToscaProperties(final JpaToscaWithToscaProperties<T> copyConcept) { + protected JpaToscaWithToscaProperties(final JpaToscaWithToscaProperties<T> copyConcept) { super(copyConcept); this.properties = copyConcept.properties == null ? null : new LinkedHashMap<>(copyConcept.properties); } @@ -95,7 +95,7 @@ public abstract class JpaToscaWithToscaProperties<T extends ToscaWithToscaProper * * @param authorativeConcept the authorative concept to copy from */ - public JpaToscaWithToscaProperties(final T authorativeConcept) { + protected JpaToscaWithToscaProperties(final T authorativeConcept) { this.fromAuthorative(authorativeConcept); } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaWithToscaPropertiesTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaWithToscaPropertiesTest.java new file mode 100644 index 000000000..213f93c89 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaWithToscaPropertiesTest.java @@ -0,0 +1,51 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 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.assertNotEquals; + +import java.util.Map; +import org.junit.Test; + +public class ToscaWithToscaPropertiesTest { + + @Test + public void test() { + ToscaProperty prop1 = new ToscaProperty(); + prop1.setDescription("description A"); + + ToscaProperty prop2 = new ToscaProperty(); + prop2.setDescription("description B"); + + ToscaWithToscaProperties tosca = new ToscaWithToscaProperties(); + assertEquals(tosca, new ToscaWithToscaProperties(tosca)); + + tosca.setProperties(Map.of("abc", prop1, "def", prop2)); + assertEquals(tosca, new ToscaWithToscaProperties(tosca)); + + assertNotEquals(tosca, new ToscaWithToscaProperties()); + + assertThatThrownBy(() -> new ToscaWithToscaProperties(null)).hasMessageContaining("copyObject") + .hasMessageContaining("is null"); + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithStringPropertiesTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithStringPropertiesTest.java index 23e0e6a7e..7cd6facf0 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithStringPropertiesTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithStringPropertiesTest.java @@ -104,17 +104,15 @@ public class JpaToscaWithStringPropertiesTest { jpa.setDescription(SOME_DESCRIPTION); jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2)); - assertThat(jpa.compareTo(null)).isNegative(); - assertThat(jpa.compareTo(jpa)).isZero(); - assertThat(jpa.compareTo(new PfConceptKey())).isNotZero(); + assertThat(jpa).isNotEqualByComparingTo(null).isEqualByComparingTo(jpa).isNotEqualByComparingTo(new MyJpa2()); MyJpa jpa2 = new MyJpa(); jpa2.setDescription(SOME_DESCRIPTION); jpa2.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2)); - assertThat(jpa.compareTo(jpa2)).isZero(); + assertThat(jpa).isEqualByComparingTo(jpa2); jpa2.setProperties(Map.of(KEY1, STRING1)); - assertThat(jpa.compareTo(jpa2)).isNotZero(); + assertThat(jpa).isNotEqualByComparingTo(jpa2); } @Test @@ -211,4 +209,8 @@ public class JpaToscaWithStringPropertiesTest { return propValue.toString(); } } + + private static class MyJpa2 extends MyJpa { + private static final long serialVersionUID = 1L; + } } diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithToscaPropertiesTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithToscaPropertiesTest.java new file mode 100644 index 000000000..e259edfef --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithToscaPropertiesTest.java @@ -0,0 +1,281 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP + * ================================================================================ + * Copyright (C) 2021 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.simple.concepts; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.common.parameters.annotations.NotNull; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.base.PfKey; +import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty; +import org.onap.policy.models.tosca.authorative.concepts.ToscaWithToscaProperties; + +public class JpaToscaWithToscaPropertiesTest { + private static final String SOME_DESCRIPTION = "some description"; + private static final String KEY1 = "abc"; + private static final String KEY2 = "def"; + private static final PfConceptKey CONCEPT_KEY1 = new PfConceptKey("hello", "1.2.3"); + private static final PfConceptKey CONCEPT_KEY2 = new PfConceptKey("world", "3.2.1"); + private static final PfReferenceKey REF_KEY1 = new PfReferenceKey(CONCEPT_KEY1); + private static final PfReferenceKey REF_KEY2 = new PfReferenceKey(CONCEPT_KEY2); + private static final JpaToscaProperty JPA_PROP1 = new JpaToscaProperty(REF_KEY1); + private static final JpaToscaProperty JPA_PROP2 = new JpaToscaProperty(REF_KEY2); + private static ToscaProperty PROP1; + private static ToscaProperty PROP2; + private static final String DESCRIPT1 = "description A"; + private static final String DESCRIPT2 = "description B"; + + private MyJpa jpa; + + /** + * Initializes the properties. + */ + @BeforeClass + public static void setUpBeforeClass() { + JPA_PROP1.setDescription(DESCRIPT1); + JPA_PROP2.setDescription(DESCRIPT2); + + PROP1 = JPA_PROP1.toAuthorative(); + PROP2 = JPA_PROP2.toAuthorative(); + } + + @Before + public void setUp() { + jpa = new MyJpa(); + } + + @Test + public void testGetKeys() { + PfConceptKey key = new PfConceptKey("bye", "9.8.7"); + + jpa = new MyJpa(key); + jpa.setDescription(SOME_DESCRIPTION); + jpa.setProperties(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2)); + + // properties should be included + + List<PfKey> keys = jpa.getKeys(); + Collections.sort(keys); + + assertThat(keys).isEqualTo( + List.of(PfConceptKey.getNullKey(), PfConceptKey.getNullKey(), key, REF_KEY1, REF_KEY2)); + } + + @Test + public void testClean() { + jpa.clean(); + + jpa.setDescription(" some description "); + + JpaToscaProperty prop1 = new JpaToscaProperty(JPA_PROP1); + prop1.setDescription(DESCRIPT1 + " "); + + JpaToscaProperty prop2 = new JpaToscaProperty(JPA_PROP2); + prop2.setDescription(" " + DESCRIPT2); + + jpa.setProperties(Map.of(KEY1, prop1, KEY2, prop2)); + + jpa.clean(); + assertEquals(SOME_DESCRIPTION, jpa.getDescription()); + assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2)); + } + + @Test + public void testToAuthorative() { + jpa.setDescription(SOME_DESCRIPTION); + jpa.setProperties(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2)); + + MyTosca tosca = jpa.toAuthorative(); + assertEquals(SOME_DESCRIPTION, tosca.getDescription()); + assertThat(tosca.getProperties()).isEqualTo(Map.of(KEY1, PROP1, KEY2, PROP2)); + } + + @Test + public void testFromAuthorative() { + MyTosca tosca = new MyTosca(); + tosca.setDescription(SOME_DESCRIPTION); + + jpa.fromAuthorative(tosca); + assertEquals(SOME_DESCRIPTION, jpa.getDescription()); + assertThat(jpa.getProperties()).isNull(); + + tosca.setProperties(Map.of(KEY1, PROP1, KEY2, PROP2)); + + JpaToscaProperty jpa1 = new JpaToscaProperty(PROP1); + jpa1.setKey(new PfReferenceKey(jpa.getKey(), KEY1)); + + JpaToscaProperty jpa2 = new JpaToscaProperty(PROP2); + jpa2.setKey(new PfReferenceKey(jpa.getKey(), KEY2)); + + jpa = new MyJpa(); + jpa.fromAuthorative(tosca); + assertEquals(SOME_DESCRIPTION, jpa.getDescription()); + assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, jpa1, KEY2, jpa2)); + } + + @Test + public void testCompareTo() { + jpa.setDescription(SOME_DESCRIPTION); + jpa.setProperties(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2)); + + assertThat(jpa).isNotEqualByComparingTo(null).isEqualByComparingTo(jpa).isNotEqualByComparingTo(new MyJpa2()); + + MyJpa jpa2 = new MyJpa(); + jpa2.setDescription(SOME_DESCRIPTION); + jpa2.setProperties(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2)); + assertThat(jpa).isEqualByComparingTo(jpa2); + + jpa2.setProperties(Map.of(KEY1, JPA_PROP1)); + assertThat(jpa).isNotEqualByComparingTo(jpa2); + } + + @Test + public void testJpaToscaWithToscaProperties() { + assertThat(jpa.getProperties()).isNull(); + assertThat(jpa.getKey().isNullKey()).isTrue(); + } + + @Test + public void testJpaToscaWithToscaPropertiesPfConceptKey() { + jpa = new MyJpa(CONCEPT_KEY1); + assertEquals(CONCEPT_KEY1, jpa.getKey()); + } + + @Test + public void testJpaToscaWithToscaPropertiesJpaToscaWithToscaPropertiesOfT() { + jpa.setDescription(SOME_DESCRIPTION); + assertEquals(jpa, new MyJpa(jpa)); + + jpa.setProperties(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2)); + assertEquals(jpa, new MyJpa(jpa)); + } + + @Test + public void testJpaToscaWithToscaPropertiesT() { + MyTosca tosca = new MyTosca(); + tosca.setName("world"); + tosca.setVersion("3.2.1"); + tosca.setDescription(SOME_DESCRIPTION); + tosca.setProperties(Map.of(KEY1, PROP1, KEY2, PROP2)); + + jpa = new MyJpa(tosca); + assertEquals(SOME_DESCRIPTION, jpa.getDescription()); + + JpaToscaProperty jpa1 = new JpaToscaProperty(PROP1); + jpa1.setKey(new PfReferenceKey(jpa.getKey(), KEY1)); + + JpaToscaProperty jpa2 = new JpaToscaProperty(PROP2); + jpa2.setKey(new PfReferenceKey(jpa.getKey(), KEY2)); + + assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, jpa1, KEY2, jpa2)); + + assertEquals(new PfConceptKey("world", "3.2.1"), jpa.getKey()); + } + + @Test + public void testValidateWithKey() { + // null key - should fail + jpa.setText("some text"); + assertThat(jpa.validateWithKey("fieldA").isValid()).isFalse(); + + // valid + jpa.setKey(new PfConceptKey("xyz", "2.3.4")); + assertThat(jpa.validateWithKey("fieldB").isValid()).isTrue(); + + // null text - bean validator should fail + jpa.setText(null); + assertThat(jpa.validateWithKey("fieldA").isValid()).isFalse(); + } + + @Test + public void testGetReferencedDataTypes() { + assertThat(jpa.getReferencedDataTypes()).isEmpty(); + + // one with a schema + PfConceptKey schemaKey = new PfConceptKey("schemaZ", "9.8.7"); + JpaToscaSchemaDefinition schema = new JpaToscaSchemaDefinition(); + schema.setType(schemaKey); + JpaToscaProperty prop1 = new JpaToscaProperty(JPA_PROP1); + prop1.setType(CONCEPT_KEY1); + prop1.setEntrySchema(schema); + + // one property without a schema + JpaToscaProperty prop2 = new JpaToscaProperty(JPA_PROP2); + prop2.setType(CONCEPT_KEY2); + prop2.setEntrySchema(null); + + jpa.setProperties(Map.of(KEY1, prop1, KEY2, prop2)); + + List<PfConceptKey> keys = new ArrayList<>(jpa.getReferencedDataTypes()); + Collections.sort(keys); + + assertThat(keys).isEqualTo(List.of(CONCEPT_KEY1, schemaKey, CONCEPT_KEY2)); + } + + + private static class MyTosca extends ToscaWithToscaProperties { + + } + + @NoArgsConstructor + protected static class MyJpa extends JpaToscaWithToscaProperties<MyTosca> { + private static final long serialVersionUID = 1L; + + @NotNull + @Getter + @Setter + private String text; + + public MyJpa(MyJpa jpa) { + super(jpa); + } + + public MyJpa(PfConceptKey key) { + super(key); + } + + public MyJpa(MyTosca tosca) { + super(tosca); + } + + @Override + public MyTosca toAuthorative() { + this.setToscaEntity(new MyTosca()); + return super.toAuthorative(); + } + } + + private static class MyJpa2 extends MyJpa { + private static final long serialVersionUID = 1L; + } +} |