UPDATE:: added achievements to user class

This commit is contained in:
mlwauben
2019-03-30 09:25:12 +01:00
parent 3d61cd061a
commit f5d9bd5010
2 changed files with 43 additions and 10 deletions

View File

@@ -1,12 +1,14 @@
package greenify.server.data.model;
import greenify.common.ApplicationException;
import greenify.server.AllAchievements;
import greenify.server.InputValidator;
import lombok.Data;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -47,6 +49,9 @@ public class User {
@JoinColumn
private Collection<User> friends;
@ElementCollection
private List<Achievement> achievements;
public User() {}
/**
@@ -60,7 +65,8 @@ public class User {
this.name = name;
this.password = password;
this.setFootPrintInputs(InputValidator.getDefaultValues());
this.friends = new ArrayList<User>();
this.friends = new ArrayList<>();
this.setAchievements(AllAchievements.getDefaults());
}
/**
@@ -160,7 +166,7 @@ public class User {
}
/**
* Adds a friend to the friendslist of the user.
* Adds a friend to the friends list of the user.
* @param user the friend you want to add.
*/
public void addFriend(User user) {
@@ -172,6 +178,22 @@ public class User {
}
}
/**
* This method sets the achievements of the user.
* @param achievements achievements of the user
*/
public void setAchievements(List<Achievement> achievements) {
this.achievements = achievements;
}
/**
* This method gets the achievements of the user.
* @return achievements of the user
*/
public List<Achievement> getAchievements() {
return this.achievements;
}
/**
* This method gets a human readable (JSON) object.
* @return the JSON form of the object.
@@ -183,8 +205,8 @@ public class User {
}
/**
* 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.
* Returns the name and score of the friends in JSON. Needed for the leader board.
* @return a JSON object of the friend list with only names and scores.
*/
public String friendsToString() {
String result = "friends=[";

View File

@@ -1,11 +1,11 @@
package greenify.server.data.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.assertThrows;
import greenify.common.ApplicationException;
import greenify.server.AllAchievements;
import org.junit.Test;
import java.util.ArrayList;
@@ -99,7 +99,7 @@ public class UserTest {
User second = new User(1L, "merel", "password");
assertEquals(first.getFriends(), second.getFriends());
first.addFriend(second);
ArrayList<User> test = new ArrayList<User>();
ArrayList<User> test = new ArrayList<>();
test.add(second);
assertEquals(first.getFriends(), test);
}
@@ -108,9 +108,7 @@ public class UserTest {
public void addYourselfTest() {
User test = new User(1L, "greenify", "password");
assertEquals(test.getFriends(), new ArrayList<User>());
assertThrows(ApplicationException.class, () -> {
test.addFriend(test);
});
assertThrows(ApplicationException.class, () -> test.addFriend(test));
}
@@ -131,4 +129,17 @@ public class UserTest {
first.setFriends(friends);
assertEquals(friends, first.getFriends());
}
@Test
public void getAchievementsTest() {
User user = new User(1L, "greenify", "password");
assertEquals(user.getAchievements(), AllAchievements.getDefaults());
}
@Test
public void setAchievementsTest() {
User user = new User(1L, "greenify", "password");
user.setAchievements(null);
assertNull(user.getAchievements());
}
}