diff options
author | Jim Hahn <jrh3@att.com> | 2021-02-12 13:55:31 -0500 |
---|---|---|
committer | Ajith Sreekumar <ajith.sreekumar@bell.ca> | 2021-02-16 11:18:54 +0000 |
commit | 5cd377c68b1f8e0520b7030b887e8a90803e2a3a (patch) | |
tree | 7ed94dc32782c091a4e74ebec5b5f19307d99522 /models-tosca/src/test/java | |
parent | 55fc900c3fbed2819b5b8a168dc29c5d583eccf3 (diff) |
Fix duplicate code in Jpa classes
Addressed the following issues:
- remove duplicate code; extracted a common class containing properties
Issue-ID: POLICY-2905
Change-Id: I80c423ab45c0c6823d338a034762d222ec334288
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'models-tosca/src/test/java')
3 files changed, 264 insertions, 2 deletions
diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaWithObjectPropertiesTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaWithObjectPropertiesTest.java new file mode 100644 index 000000000..7186a3ff0 --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaWithObjectPropertiesTest.java @@ -0,0 +1,45 @@ +/*- + * ============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 ToscaWithObjectPropertiesTest { + + @Test + public void testCopyConstructor() { + ToscaWithObjectProperties tosca = new ToscaWithObjectProperties(); + assertEquals(tosca, new ToscaWithObjectProperties(tosca)); + + tosca.setProperties(Map.of("abc", 10, "def", "world")); + assertEquals(tosca, new ToscaWithObjectProperties(tosca)); + + assertNotEquals(tosca, new ToscaWithObjectProperties()); + + assertThatThrownBy(() -> new ToscaWithObjectProperties(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 new file mode 100644 index 000000000..23e0e6a7e --- /dev/null +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/JpaToscaWithStringPropertiesTest.java @@ -0,0 +1,214 @@ +/*- + * ============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.List; +import java.util.Map; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.common.parameters.annotations.NotNull; +import org.onap.policy.models.base.PfConceptKey; +import org.onap.policy.models.tosca.authorative.concepts.ToscaWithObjectProperties; + +public class JpaToscaWithStringPropertiesTest { + private static final String SOME_DESCRIPTION = "some description"; + private static final String KEY1 = "abc"; + private static final String KEY2 = "def"; + private static final String STRING1 = "10"; + private static final String STRING2 = "20"; + private static final int INT1 = 10; + private static final int INT2 = 20; + + private MyJpa jpa; + + @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, STRING1, KEY2, STRING2)); + + // properties should be ignored + assertThat(jpa.getKeys()).isEqualTo(List.of(key)); + } + + @Test + public void testClean() { + jpa.setDescription(" some description "); + jpa.setProperties(Map.of(KEY1, "10 ", KEY2, " 20")); + + jpa.clean(); + assertEquals(SOME_DESCRIPTION, jpa.getDescription()); + assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, STRING1, KEY2, STRING2)); + } + + @Test + public void testToAuthorative() { + jpa.setDescription(SOME_DESCRIPTION); + jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2)); + + MyTosca tosca = jpa.toAuthorative(); + assertEquals(SOME_DESCRIPTION, tosca.getDescription()); + assertThat(tosca.getProperties()).isEqualTo(Map.of(KEY1, INT1, KEY2, INT2)); + } + + @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, INT1, KEY2, INT2)); + + jpa = new MyJpa(); + jpa.fromAuthorative(tosca); + assertEquals(SOME_DESCRIPTION, jpa.getDescription()); + assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, STRING1, KEY2, STRING2)); + } + + @Test + public void testCompareTo() { + 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(); + + MyJpa jpa2 = new MyJpa(); + jpa2.setDescription(SOME_DESCRIPTION); + jpa2.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2)); + assertThat(jpa.compareTo(jpa2)).isZero(); + + jpa2.setProperties(Map.of(KEY1, STRING1)); + assertThat(jpa.compareTo(jpa2)).isNotZero(); + } + + @Test + public void testJpaToscaWithStringProperties() { + assertThat(jpa.getProperties()).isNull(); + assertThat(jpa.getKey().isNullKey()).isTrue(); + + } + + @Test + public void testJpaToscaWithStringPropertiesPfConceptKey() { + PfConceptKey key = new PfConceptKey("hello", "1.2.3"); + + jpa = new MyJpa(key); + assertEquals(key, jpa.getKey()); + } + + @Test + public void testJpaToscaWithStringPropertiesJpaToscaWithStringPropertiesOfT() { + jpa.setDescription(SOME_DESCRIPTION); + assertEquals(jpa, new MyJpa(jpa)); + + jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2)); + assertEquals(jpa, new MyJpa(jpa)); + } + + @Test + public void testJpaToscaWithStringPropertiesT() { + MyTosca tosca = new MyTosca(); + tosca.setName("world"); + tosca.setVersion("3.2.1"); + tosca.setDescription(SOME_DESCRIPTION); + tosca.setProperties(Map.of(KEY1, INT1, KEY2, INT2)); + + jpa = new MyJpa(tosca); + assertEquals(SOME_DESCRIPTION, jpa.getDescription()); + assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, STRING1, KEY2, STRING2)); + 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(); + } + + private static class MyTosca extends ToscaWithObjectProperties { + + } + + @NoArgsConstructor + protected static class MyJpa extends JpaToscaWithStringProperties<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(); + } + + @Override + protected Object deserializePropertyValue(String propValue) { + return Integer.parseInt(propValue); + } + + @Override + protected String serializePropertyValue(Object propValue) { + return propValue.toString(); + } + } +} diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/TestPojos.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/TestPojos.java index 9e8edc77c..250a819e6 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/TestPojos.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/simple/concepts/TestPojos.java @@ -2,7 +2,7 @@ * ============LICENSE_START======================================================= * ONAP Policy Model * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -53,6 +53,9 @@ public class TestPojos { .build(); // @formatter:on - validator.validate(POJO_PACKAGE, new FilterPackageInfo()); + validator.validate(POJO_PACKAGE, + new FilterPackageInfo(), + pc -> !pc.getName().startsWith("Test"), + pc -> !pc.getName().endsWith("Test")); } } |