From: Fiete Ostkamp Date: Wed, 30 Jul 2025 09:31:02 +0000 (+0200) Subject: Improve code coverage in common X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=refs%2Fheads%2Fmaster;p=so.git Improve code coverage in common - 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 --- diff --git a/common/common/pom.xml b/common/common/pom.xml index 718bfae488..3ab92eef95 100644 --- a/common/common/pom.xml +++ b/common/common/pom.xml @@ -358,6 +358,16 @@ ${surefireArgLine} + + org.jacoco + jacoco-maven-plugin + + + org/onap/so/beans/nsmf/**/* + org/onap/so/moi/**/* + + + 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 index 0000000000..298fd5c3e4 --- /dev/null +++ b/common/common/src/test/java/org/onap/so/openpojo/rules/EqualsAndHashCodeTesterTest.java @@ -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 index 0000000000..c719f28f5c --- /dev/null +++ b/common/common/src/test/java/org/onap/so/openpojo/rules/HasEqualsAndHashCodeRuleTest.java @@ -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); + } + } +}