Fix equals and hashCode methods 83/56383/1
authorBogumil Zebek <bogumil.zebek@nokia.com>
Fri, 13 Jul 2018 12:28:17 +0000 (14:28 +0200)
committerBogumil Zebek <bogumil.zebek@nokia.com>
Fri, 13 Jul 2018 12:28:17 +0000 (14:28 +0200)
Change-Id: I3e3ebcd1ba29bc20a41c138ec071f137ccd6cf18
Issue-ID: AAI-1375
Signed-off-by: Bogumil Zebek <bogumil.zebek@nokia.com>
champ-lib/champ-core/src/main/java/org/onap/aai/champcore/model/ChampObjectConstraint.java
champ-lib/champ-core/src/main/java/org/onap/aai/champcore/model/ChampObjectIndex.java
champ-lib/champ-core/src/main/java/org/onap/aai/champcore/model/ChampRelationship.java
champ-lib/champ-core/src/main/java/org/onap/aai/champcore/model/ChampRelationshipConstraint.java
champ-lib/champ-core/src/main/java/org/onap/aai/champcore/model/ChampRelationshipIndex.java
champ-lib/champ-core/src/test/java/org/onap/aai/champcore/core/ChampObjectIndexTest.java
champ-lib/champ-core/src/test/java/org/onap/aai/champcore/core/ChampRelationshipIndexTest.java
champ-lib/champ-core/src/test/java/org/onap/aai/champcore/core/ChampRelationshipTest.java
champ-lib/champ-core/src/test/java/org/onap/aai/champcore/model/ChampObjectConstraintTest.java [new file with mode: 0644]
champ-lib/champ-core/src/test/java/org/onap/aai/champcore/model/ChampRelationshipConstraintTest.java [new file with mode: 0644]

index c999e7c..3bd00fa 100644 (file)
@@ -23,6 +23,7 @@ package org.onap.aai.champcore.model;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Optional;
 import java.util.Set;
 
@@ -88,6 +89,10 @@ public final class ChampObjectConstraint {
 
        @Override
        public boolean equals(Object o) {
+               if (o == this) {
+                       return true;
+               }
+
                if (o instanceof ChampObjectConstraint) {
                        final ChampObjectConstraint objectConstraint = (ChampObjectConstraint) o;
 
@@ -97,6 +102,11 @@ public final class ChampObjectConstraint {
                return false;
        }
 
+       @Override
+       public int hashCode() {
+               return Objects.hashCode(getType());
+       }
+
        @Override
        public String toString() {
                return "{type: " + getType() +
index 489e26d..68ecbcc 100644 (file)
@@ -20,6 +20,7 @@
  */
 package org.onap.aai.champcore.model;
 
+import java.util.Objects;
 import org.onap.aai.champcore.model.fluent.index.CreateObjectIndexable;
 import org.onap.aai.champcore.model.fluent.index.impl.CreateObjectIndexableImpl;
 
@@ -83,4 +84,9 @@ public final class ChampObjectIndex {
                
                return false;
        }
+
+       @Override
+       public int hashCode() {
+               return Objects.hash(getName(), getField().getName());
+       }
 }
index 6083d52..4878ed9 100644 (file)
@@ -23,6 +23,7 @@ package org.onap.aai.champcore.model;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Objects;
 import java.util.Optional;
 
 import org.onap.aai.champcore.model.fluent.relationship.CreateChampRelationshipable;
@@ -151,6 +152,11 @@ public final class ChampRelationship implements ChampElement {
                return false;
        }
 
+       @Override
+       public int hashCode() {
+               return Objects.hashCode(getKey());
+       }
+
        @Override
        public String toString() {
                return "{key: " + (getKey().isPresent() ? getKey().get() : "") 
index b8e8eef..5191ff8 100644 (file)
@@ -23,6 +23,7 @@ package org.onap.aai.champcore.model;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Optional;
 import java.util.Set;
 
@@ -135,4 +136,9 @@ public final class ChampRelationshipConstraint {
                
                return false;
        }
+
+       @Override
+       public int hashCode() {
+               return Objects.hash(getType(), getConnectionConstraints(), getPropertyConstraints());
+       }
 }
index 763f649..e33af39 100644 (file)
@@ -20,6 +20,7 @@
  */
 package org.onap.aai.champcore.model;
 
+import java.util.Objects;
 import org.onap.aai.champcore.model.fluent.index.CreateRelationshipIndexable;
 import org.onap.aai.champcore.model.fluent.index.impl.CreateRelationshipIndexableImpl;
 
@@ -83,4 +84,9 @@ public final class ChampRelationshipIndex {
                
                return false;
        }
+
+       @Override
+       public int hashCode() {
+               return Objects.hash(getName(), getField().getName());
+       }
 }
index d007ee4..a58cbc6 100644 (file)
  */
 package org.onap.aai.champcore.core;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertTrue;
 
 import java.util.Collection;
 import java.util.Optional;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
-
 import org.junit.Test;
 import org.onap.aai.champcore.ChampAPI;
 import org.onap.aai.champcore.ChampGraph;
 import org.onap.aai.champcore.exceptions.ChampIndexNotExistsException;
+import org.onap.aai.champcore.model.ChampField;
+import org.onap.aai.champcore.model.ChampField.Type;
 import org.onap.aai.champcore.model.ChampObjectIndex;
+import org.onap.aai.champcore.model.ChampObjectIndex.Builder;
 
 public class ChampObjectIndexTest extends BaseChampAPITest {
        @Test
@@ -130,4 +134,32 @@ public class ChampObjectIndexTest extends BaseChampAPITest {
                assertTrue(objectIndex.getType().equals("foo"));
                assertTrue(objectIndex.getField().getName().equals("name"));
        }
+
+       @Test
+       public void verifyEqualsAndHashCodeMethods() {
+               ChampField champField1 = new ChampField.Builder("name").type(Type.STRING).build();
+               ChampField champField2 = new ChampField.Builder("differentName").type(Type.STRING).build();
+
+               ChampObjectIndex obj1 = new Builder("name", "type", champField1).build();
+               ChampObjectIndex obj2 = new Builder("name", "type", champField1).build();
+               ChampObjectIndex obj3 = new Builder("name", "type", champField1).build();
+               ChampObjectIndex obj4 = new Builder("name", "type", champField2).build();
+               ChampObjectIndex obj5 = new Builder("differentName", "type", champField1).build();
+
+               // if
+               assertEquals(obj1, obj2);
+               assertEquals(obj1.hashCode(), obj2.hashCode());
+               //and
+               assertEquals(obj1, obj3);
+               assertEquals(obj1.hashCode(), obj3.hashCode());
+               //then
+               assertEquals(obj2, obj3);
+               assertEquals(obj2.hashCode(), obj3.hashCode());
+
+               assertNotEquals(obj1, obj4);
+               assertNotEquals(obj1.hashCode(), obj4.hashCode());
+
+               assertNotEquals(obj1, obj5);
+               assertNotEquals(obj1.hashCode(), obj5.hashCode());
+       }
 }
index f174f3e..e53896d 100644 (file)
  */
 package org.onap.aai.champcore.core;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Collection;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 import org.junit.Test;
 import org.onap.aai.champcore.ChampAPI;
 import org.onap.aai.champcore.ChampGraph;
-import org.onap.aai.champcore.exceptions.*;
+import org.onap.aai.champcore.exceptions.ChampIndexNotExistsException;
+import org.onap.aai.champcore.exceptions.ChampMarshallingException;
+import org.onap.aai.champcore.exceptions.ChampObjectNotExistsException;
+import org.onap.aai.champcore.exceptions.ChampRelationshipNotExistsException;
+import org.onap.aai.champcore.exceptions.ChampSchemaViolationException;
+import org.onap.aai.champcore.exceptions.ChampTransactionException;
+import org.onap.aai.champcore.exceptions.ChampUnmarshallingException;
 import org.onap.aai.champcore.model.ChampField;
 import org.onap.aai.champcore.model.ChampRelationship;
 import org.onap.aai.champcore.model.ChampRelationshipIndex;
 
-import java.util.Collection;
-import java.util.Optional;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-
-import static org.junit.Assert.assertTrue;
-
 public class ChampRelationshipIndexTest extends BaseChampAPITest {
 
   @Test
@@ -175,4 +182,29 @@ public class ChampRelationshipIndexTest extends BaseChampAPITest {
     assertTrue(relationshipIndex.getType().equals("foo"));
     assertTrue(relationshipIndex.getField().getName().equals("name"));
   }
+
+
+      @Test
+    public void verifyEqualsAndHashCodeMethods(){
+        ChampField champField1 = new ChampField.Builder("name").build();
+        ChampField champField2 = new ChampField.Builder("differentName").build();
+        ChampRelationshipIndex obj1 = new ChampRelationshipIndex.Builder("name","type",champField1).build();
+        ChampRelationshipIndex obj2 = new ChampRelationshipIndex.Builder("name","type",champField1).build();
+        ChampRelationshipIndex obj3 = new ChampRelationshipIndex.Builder("name","type",champField1).build();
+        ChampRelationshipIndex obj4 = new ChampRelationshipIndex.Builder("name","type",champField2).build();
+
+
+        // if
+        assertEquals(obj1, obj2);
+        assertEquals(obj1.hashCode(), obj2.hashCode());
+        //and
+        assertEquals(obj1, obj3);
+        assertEquals(obj1.hashCode(), obj3.hashCode());
+        //then
+        assertEquals(obj2, obj3);
+        assertEquals(obj2.hashCode(), obj3.hashCode());
+
+        assertNotEquals(obj1, obj4);
+        assertNotEquals(obj1.hashCode(), obj4.hashCode());
+    }
 }
index 549f7f4..67dcbd1 100644 (file)
@@ -27,6 +27,7 @@ import org.onap.aai.champcore.ChampTransaction;
 import org.onap.aai.champcore.exceptions.*;
 import org.onap.aai.champcore.model.ChampObject;
 import org.onap.aai.champcore.model.ChampRelationship;
+import org.onap.aai.champcore.model.ChampRelationship.Builder;
 import org.onap.aai.champcore.model.ChampRelationship.ReservedPropertyKeys;
 import org.onap.aai.champcore.model.ChampRelationship.ReservedTypes;
 
@@ -36,6 +37,8 @@ import java.util.Map;
 import java.util.Optional;
 import java.util.stream.Collectors;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertTrue;
 
 public class ChampRelationshipTest extends BaseChampAPITest {
@@ -277,6 +280,30 @@ public class ChampRelationshipTest extends BaseChampAPITest {
       assertTrue(ChampRelationship.ReservedTypes.valueOf(type.name()) == type);
     }
   }
+
+  @Test
+  public void verifyEqualsAndHashCodeMethods() {
+    ChampObject source = new ChampObject.Builder("type").build();
+    ChampObject target = new ChampObject.Builder("type").build();
+    ChampRelationship champRelationship = new Builder(source, target, "type").key("someKey").build();
+    ChampRelationship obj1 = new Builder(champRelationship).key("someKey").build();
+    ChampRelationship obj2 = new Builder(champRelationship).key("someKey").build();
+    ChampRelationship obj3 = new Builder(champRelationship).key("someKey").build();
+    ChampRelationship obj4 = new Builder(champRelationship).key("differentKey").build();
+
+    // if
+    assertEquals(obj1, obj2);
+    assertEquals(obj1.hashCode(), obj2.hashCode());
+    //and
+    assertEquals(obj1, obj3);
+    assertEquals(obj1.hashCode(), obj3.hashCode());
+    //then
+    assertEquals(obj2, obj3);
+    assertEquals(obj2.hashCode(), obj3.hashCode());
+
+    assertNotEquals(obj1, obj4);
+    assertNotEquals(obj1.hashCode(), obj4.hashCode());
+  }
   
   public static ChampTransaction getTransaction() {
     return new TestTransaction();
diff --git a/champ-lib/champ-core/src/test/java/org/onap/aai/champcore/model/ChampObjectConstraintTest.java b/champ-lib/champ-core/src/test/java/org/onap/aai/champcore/model/ChampObjectConstraintTest.java
new file mode 100644 (file)
index 0000000..c77214c
--- /dev/null
@@ -0,0 +1,53 @@
+/**
+ * ============LICENSE_START==========================================
+ * org.onap.aai
+ * ===================================================================
+ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2018 Nokia 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.aai.champcore.model;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+
+import org.junit.Test;
+import org.onap.aai.champcore.model.ChampObjectConstraint.Builder;
+
+
+public class ChampObjectConstraintTest {
+
+    @Test
+    public void verifyEqualsAndHashCodeMethods(){
+        ChampObjectConstraint obj1 = new Builder("type").build();
+        ChampObjectConstraint obj2 = new Builder("type").build();
+        ChampObjectConstraint obj3 = new Builder("type").build();
+        ChampObjectConstraint obj4 = new Builder("differentType").build();
+
+        // if
+        assertEquals(obj1, obj2);
+        assertEquals(obj1.hashCode(), obj2.hashCode());
+        //and
+        assertEquals(obj1, obj3);
+        assertEquals(obj1.hashCode(), obj3.hashCode());
+        //then
+        assertEquals(obj2, obj3);
+        assertEquals(obj2.hashCode(), obj3.hashCode());
+
+        assertNotEquals(obj1, obj4);
+        assertNotEquals(obj1.hashCode(), obj4.hashCode());
+    }
+
+}
\ No newline at end of file
diff --git a/champ-lib/champ-core/src/test/java/org/onap/aai/champcore/model/ChampRelationshipConstraintTest.java b/champ-lib/champ-core/src/test/java/org/onap/aai/champcore/model/ChampRelationshipConstraintTest.java
new file mode 100644 (file)
index 0000000..f9bfa3c
--- /dev/null
@@ -0,0 +1,57 @@
+/**
+ * ============LICENSE_START==========================================
+ * org.onap.aai
+ * ===================================================================
+ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2018 Nokia 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.aai.champcore.model;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+
+import java.util.HashSet;
+import org.junit.Test;
+
+
+
+public class ChampRelationshipConstraintTest {
+
+    @Test
+    public void verifyEqualsAndHashCodeMethods(){
+        ChampRelationshipConstraint obj1 = new ChampRelationshipConstraint.Builder("type")
+            .connectionConstraints(new HashSet<>()).propertyConstraints(new HashSet<>()).build();
+        ChampRelationshipConstraint obj2 = new ChampRelationshipConstraint.Builder("type")
+            .connectionConstraints(new HashSet<>()).propertyConstraints(new HashSet<>()).build();
+        ChampRelationshipConstraint obj3 = new ChampRelationshipConstraint.Builder("type")
+            .connectionConstraints(new HashSet<>()).propertyConstraints(new HashSet<>()).build();
+        ChampRelationshipConstraint obj4 = new ChampRelationshipConstraint.Builder("differentType")
+            .connectionConstraints(new HashSet<>()).propertyConstraints(new HashSet<>()).build();
+
+        // if
+        assertEquals(obj1, obj2);
+        assertEquals(obj1.hashCode(), obj2.hashCode());
+        //and
+        assertEquals(obj1, obj3);
+        assertEquals(obj1.hashCode(), obj3.hashCode());
+        //then
+        assertEquals(obj2, obj3);
+        assertEquals(obj2.hashCode(), obj3.hashCode());
+
+        assertNotEquals(obj1, obj4);
+        assertNotEquals(obj1.hashCode(), obj4.hashCode());
+    }
+}
\ No newline at end of file