diff --git a/src/Server/src/main/java/greenify/server/data/model/User.java b/src/Server/src/main/java/greenify/server/data/model/User.java index 1edcc28..fd916be 100644 --- a/src/Server/src/main/java/greenify/server/data/model/User.java +++ b/src/Server/src/main/java/greenify/server/data/model/User.java @@ -7,16 +7,11 @@ import lombok.Data; import java.util.HashMap; import java.util.Map; import java.util.Objects; +import java.util.ArrayList; +import java.util.Collection; -import javax.persistence.ElementCollection; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.Table; -import java.util.*; -import java.util.logging.Logger; import javax.persistence.*; + import javax.validation.constraints.NotNull; @Entity @@ -27,7 +22,6 @@ public class User { @Id @NotNull @GeneratedValue(strategy = GenerationType.AUTO) -// @Column(name = "id") private Long id; @NotNull @@ -114,15 +108,19 @@ public class User { this.footPrintInputs = footPrintInputs; } - public ArrayList getFriends(){ + public ArrayList getFriends() { return (ArrayList)this.friends; } - public void addFriend(User user){ - if(user.equals(this)){ + /** + * Adds a friend to the friendslist of the user. + * @param user the friend you want to add. + */ + public void addFriend(User user) { + if (user.equals(this)) { throw new ApplicationException("Cannot add yourself as a friend"); } - else{ + else { friends.add(user); System.out.print("Friend added!"); } @@ -143,12 +141,16 @@ public class User { + this.password + ")"; } + /** + * Returns the name and score of the friends in JSON. Needed for the leaderboard. + * @return a JSON object of the friendlist with only names and scores. + */ public String friendsToString(){ String result = "friends=["; - for(User u : friends){ + for (User u : friends) { result += "{name=" + u.getName() + ", footprint=" + u.getFootPrint() + "}, "; } - if(result.endsWith(", ")){ + if (result.endsWith(", ")) { return result.substring(0, result.lastIndexOf(",")) + "]"; } return result + "]"; diff --git a/src/Server/src/test/java/greenify/server/data/model/UserTest.java b/src/Server/src/test/java/greenify/server/data/model/UserTest.java index 92b8d2c..dd22b40 100644 --- a/src/Server/src/test/java/greenify/server/data/model/UserTest.java +++ b/src/Server/src/test/java/greenify/server/data/model/UserTest.java @@ -84,7 +84,7 @@ public class UserTest { } @Test - public void getFriendEmpty(){ + public void getFriendEmpty() { User first = new User(1L, "greenify", "password"); User second = new User(1L, "merel", "password"); assertEquals(first.getFriends(), second.getFriends()); @@ -92,7 +92,7 @@ public class UserTest { } @Test - public void addFriendTest(){ + public void addFriendTest() { User first = new User(1L, "greenify", "password"); User second = new User(1L, "merel", "password"); assertEquals(first.getFriends(), second.getFriends()); @@ -103,7 +103,7 @@ public class UserTest { } @Test - public void addYourselfTest(){ + public void addYourselfTest() { User test = new User(1L, "greenify", "password"); assertEquals(test.getFriends(), new ArrayList()); assertThrows(ApplicationException.class, () -> { @@ -113,7 +113,7 @@ public class UserTest { @Test - public void friendsToStringTest(){ + public void friendsToStringTest() { User first = new User(1L, "greenify", "password"); User second = new User(1L, "merel", "password"); first.addFriend(second); diff --git a/src/Server/src/test/java/greenify/server/service/UserServiceTest.java b/src/Server/src/test/java/greenify/server/service/UserServiceTest.java index ddfacb1..dc520d9 100644 --- a/src/Server/src/test/java/greenify/server/service/UserServiceTest.java +++ b/src/Server/src/test/java/greenify/server/service/UserServiceTest.java @@ -49,7 +49,8 @@ public class UserServiceTest { .thenReturn(alex); User lola = new User(2L, "lola", "password"); when(userRepository.findByName(lola.getName())) - .thenReturn(lola); } + .thenReturn(lola); + } @Test public void validLoginTest() {