diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Group.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Group.java index 346ccd7fb82b696d451775fedcb542fa0f34b758..9a87de61a0cdf093cc3043aa2b1103b7d4828f8d 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Group.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/group/Group.java @@ -82,4 +82,29 @@ public class Group { @JsonIgnoreProperties({"groups"}) private List<Achievement> achievements; + /** + * Checks if two objects are equal + * @param o the object to compare to + * @return true if the objects are equal, false otherwise + */ + @Override + public boolean equals(Object o){ + if(o == this){ + return true; + } + if(!(o instanceof Group group)){ + return false; + } + return group.getGroupId() == this.getGroupId(); + } + + /** + * Gets the hashcode of the object + * @return the hashcode of the object + */ + @Override + public int hashCode(){ + return Long.hashCode(this.getGroupId()); + } + } diff --git a/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/group/GroupControllerTest.java b/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/group/GroupControllerTest.java index 1cbf6abea1fab4e5dd2f8ebe46975661e7f6099c..36935a179af13410d05f2c60518a08b5774e9129 100644 --- a/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/group/GroupControllerTest.java +++ b/src/test/java/ntnu/idatt2016/v233/SmartMat/controller/group/GroupControllerTest.java @@ -282,7 +282,7 @@ public class GroupControllerTest { assertSame(HttpStatus.FORBIDDEN, groupResponseEntity.getStatusCode()); assertNull(groupResponseEntity.getBody()); - } - + } + } \ No newline at end of file diff --git a/src/test/java/ntnu/idatt2016/v233/SmartMat/entity/group/GroupTest.java b/src/test/java/ntnu/idatt2016/v233/SmartMat/entity/group/GroupTest.java new file mode 100644 index 0000000000000000000000000000000000000000..f74cf33e59a9fbd5087e877a1d9cfd5660250e0e --- /dev/null +++ b/src/test/java/ntnu/idatt2016/v233/SmartMat/entity/group/GroupTest.java @@ -0,0 +1,53 @@ +package ntnu.idatt2016.v233.SmartMat.entity.group; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class GroupTest { + + @Test + void testEquals() { + Group group = Group.builder() + .groupName("Test Group") + .groupId(1L) + .build(); + + Group group2 = Group.builder() + .groupId(2L) + .groupName("Test Group2") + .build(); + + + assertEquals(group, group); + assertNotEquals(group, group2); + assertNotEquals("hello", group); + } + + @Test + void testHashCode() { + Group group = Group.builder() + .groupName("Test Group") + .groupId(1L) + .build(); + + Group group2 = Group.builder() + .groupId(2L) + .groupName("Test Group2") + .build(); + + assertEquals(group.hashCode(), group.hashCode()); + assertNotEquals(group.hashCode(), group2.hashCode()); + } + + @Test + void testToString() { + Group group = Group.builder() + .groupName("Test Group") + .groupId(1L) + .build(); + + assertEquals("Group(groupId=1, level=0, points=0, groupName=Test Group, linkCode=null, open=null, shoppingList=null, user=null, fridge=null, achievements=null)", + group.toString()); + } +} \ No newline at end of file