Improve code coverage in common 00/141600/2 master
authorFiete Ostkamp <Fiete.Ostkamp@telekom.de>
Wed, 30 Jul 2025 09:31:02 +0000 (11:31 +0200)
committerFiete Ostkamp <Fiete.Ostkamp@telekom.de>
Wed, 30 Jul 2025 10:44:47 +0000 (12:44 +0200)
- increase code coverage from 51% -> 66% by
  - excluding entities from coverage analysis
  - adding EqualsAndHashCodeTester test
  - adding HasEqualsAndHashCodeRule test

Issue-ID: SO-4212
Change-Id: I4be78cf3aea90ec1e7eb37e6a45ea96db45637d3
Signed-off-by: Fiete Ostkamp <Fiete.Ostkamp@telekom.de>
common/common/pom.xml
common/common/src/test/java/org/onap/so/openpojo/rules/EqualsAndHashCodeTesterTest.java [new file with mode: 0644]
common/common/src/test/java/org/onap/so/openpojo/rules/HasEqualsAndHashCodeRuleTest.java [new file with mode: 0644]

index 718bfae..3ab92ee 100644 (file)
             <argLine>${surefireArgLine}</argLine>
           </configuration>
         </plugin>
+        <plugin>
+          <groupId>org.jacoco</groupId>
+          <artifactId>jacoco-maven-plugin</artifactId>
+          <configuration>
+            <excludes>
+              <exclude>org/onap/so/beans/nsmf/**/*</exclude>
+              <exclude>org/onap/so/moi/**/*</exclude>
+            </excludes>
+          </configuration>
+        </plugin>
       </plugins>
     </pluginManagement>
   </build>
diff --git a/common/common/src/test/java/org/onap/so/openpojo/rules/EqualsAndHashCodeTesterTest.java b/common/common/src/test/java/org/onap/so/openpojo/rules/EqualsAndHashCodeTesterTest.java
new file mode 100644 (file)
index 0000000..298fd5c
--- /dev/null
@@ -0,0 +1,114 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - SO
+ * ================================================================================
+ * Copyright © 2025 Deutsche Telekom AG 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.so.openpojo.rules;
+
+import com.openpojo.reflection.impl.PojoClassFactory;
+import com.openpojo.reflection.PojoClass;
+import lombok.Data;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+import java.util.Objects;
+import javax.persistence.Id;
+import org.junit.Before;
+import org.junit.Test;
+
+public class EqualsAndHashCodeTesterTest {
+
+
+    private EqualsAndHashCodeTester tester;
+
+    @Before
+    public void setUp() {
+        tester = new EqualsAndHashCodeTester();
+    }
+
+    @Test
+    public void testValidEqualsAndHashCode() {
+        PojoClass pojoClass = PojoClassFactory.getPojoClass(ValidEqualsWithId.class);
+
+        tester.run(pojoClass);
+    }
+
+    @Test
+    // assert that test fails when object fields that are compared in the classes equals implementation
+    // are not annotated with @Id or @BusinessKey
+    public void testInValidEqualsAndHashCode() {
+        PojoClass pojoClass = PojoClassFactory.getPojoClass(InvalidEqualsWithoutId.class);
+
+        AssertionError error = assertThrows(AssertionError.class, () -> tester.run(pojoClass));
+        assertTrue(error.getMessage().contains("Equals test"));
+    }
+
+    @Test
+    public void testOnlyDeclaredMethods() {
+        PojoClass pojoClass = PojoClassFactory.getPojoClass(ValidEqualsWithId.class);
+        EqualsAndHashCodeTester onlyDeclaredMethodsTester = new EqualsAndHashCodeTester().onlyDeclaredMethods();
+
+        onlyDeclaredMethodsTester.run(pojoClass);
+    }
+
+    @Data
+    static class ValidEqualsWithId {
+
+        @Id
+        String requestId;
+
+        @Override
+        public boolean equals(final Object other) {
+            if (this == other) {
+                return true;
+            }
+            if (!(other instanceof ValidEqualsWithId)) {
+                return false;
+            }
+            final ValidEqualsWithId castOther = (ValidEqualsWithId) other;
+            return Objects.equals(getRequestId(), castOther.getRequestId());
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hash(getRequestId());
+        }
+    }
+
+    @Data
+    static class InvalidEqualsWithoutId {
+
+        String requestId;
+
+        @Override
+        public boolean equals(final Object other) {
+            if (this == other) {
+                return true;
+            }
+            if (!(other instanceof InvalidEqualsWithoutId)) {
+                return false;
+            }
+            final InvalidEqualsWithoutId castOther = (InvalidEqualsWithoutId) other;
+            // equals may only compare fields that are annotated with @Id or @BusinessKey
+            return Objects.equals(getRequestId(), castOther.getRequestId());
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hash(getRequestId());
+        }
+    }
+}
diff --git a/common/common/src/test/java/org/onap/so/openpojo/rules/HasEqualsAndHashCodeRuleTest.java b/common/common/src/test/java/org/onap/so/openpojo/rules/HasEqualsAndHashCodeRuleTest.java
new file mode 100644 (file)
index 0000000..c719f28
--- /dev/null
@@ -0,0 +1,93 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - SO
+ * ================================================================================
+ * Copyright © 2025 Deutsche Telekom AG 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.so.openpojo.rules;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.util.Objects;
+import javax.persistence.Id;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.so.openpojo.rules.EqualsAndHashCodeTesterTest.ValidEqualsWithId;
+import com.openpojo.reflection.PojoClass;
+import com.openpojo.reflection.impl.PojoClassFactory;
+import com.openpojo.validation.rule.Rule;
+import lombok.Data;
+
+public class HasEqualsAndHashCodeRuleTest {
+
+    private Rule rule;
+
+    @Before
+    public void setUp() {
+        rule = new HasEqualsAndHashCodeRule();
+    }
+
+    @Test
+    public void testValidEqualsAndHashCode() {
+        PojoClass pojoClass = PojoClassFactory.getPojoClass(HasEqualsAndHashCode.class);
+
+        rule.evaluate(pojoClass);
+    }
+
+    @Test
+    public void testEqualsButNoHashCode() {
+        PojoClass pojoClass = PojoClassFactory.getPojoClass(HasEqualsButNoHashCode.class);
+
+        AssertionError error = assertThrows(AssertionError.class, () -> rule.evaluate(pojoClass));
+        assertTrue(error.getMessage().contains("does not override hashCode"));
+    }
+
+    @Test
+    public void testHashCodeButNoEquals() {
+        PojoClass pojoClass = PojoClassFactory.getPojoClass(HasHashCodeButNoEquals.class);
+
+        AssertionError error = assertThrows(AssertionError.class, () -> rule.evaluate(pojoClass));
+        assertTrue(error.getMessage().contains("does not override equals"));
+    }
+
+    @Data
+    static class HasEqualsAndHashCode {
+
+        @Override
+        public boolean equals(final Object other) {
+            return true;
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hash(this);
+        }
+    }
+
+    static class HasEqualsButNoHashCode {
+        @Override
+        public boolean equals(final Object other) {
+            return true;
+        }
+    }
+
+    static class HasHashCodeButNoEquals {
+        @Override
+        public int hashCode() {
+            return Objects.hash(this);
+        }
+    }
+}