diff options
author | Jim Hahn <jrh3@att.com> | 2021-02-17 10:10:39 -0500 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2021-02-17 10:48:40 -0500 |
commit | 9fb6de04f2402c29a4a792cc118f5bbb53906f81 (patch) | |
tree | 08f09ec4128c1bca05307f26815b0296e1f8bbe3 /models-tosca/src/test | |
parent | 9f0fbd02f184071c2de30c944bec311741e6180e (diff) |
Fix sonars from removal of duplicate code
The work to remove duplicate code from models introduced as many new
sonars as it eliminated. :-( Addressed the following new issues:
- visibility of constructor
- use of more specific assertThat() calls
Also added two junit test file that were somehow previously left out.
Issue-ID: POLICY-2905
Change-Id: I7a0206bd157412d3c6d98bfe21797f106b37ac65
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'models-tosca/src/test')
3 files changed, 339 insertions, 5 deletions
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; + } +} |