--- /dev/null
+/*-
+ * ============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());
+ }
+ }
+}
--- /dev/null
+/*-
+ * ============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);
+ }
+ }
+}