From e12ed2b0d600926fffe657f5295e2f26344ba325 Mon Sep 17 00:00:00 2001 From: birkon <birkon@stud.ntnu.no> Date: Thu, 4 May 2023 15:27:37 +0200 Subject: [PATCH] test for equals and hash code in group entity --- .../v233/SmartMat/entity/group/Group.java | 25 +++++++++ .../controller/group/GroupControllerTest.java | 4 +- .../v233/SmartMat/entity/group/GroupTest.java | 53 +++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 src/test/java/ntnu/idatt2016/v233/SmartMat/entity/group/GroupTest.java 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 346ccd7f..9a87de61 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 1cbf6abe..36935a17 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 00000000..f74cf33e --- /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 -- GitLab