Added testing for the friend list and equals in User class in Server

This commit is contained in:
Merel Steenbergen
2019-03-21 16:09:23 +01:00
parent b3a0ddaa68
commit fb69ec280d
2 changed files with 48 additions and 1 deletions

View File

@@ -1,5 +1,7 @@
package greenify.server.data.model;
import greenify.common.ApplicationException;
import greenify.server.Application;
import lombok.Data;
import java.util.ArrayList;
@@ -101,8 +103,13 @@ public class User {
}
public void addFriend(User user){
if(!user.equals(this)) {
friends.add(user);
}
else {
throw new ApplicationException("Cannnot add yourself as a friend");
}
}
/**
* Returns a human readable object. It's in JSON.

View File

@@ -1,11 +1,18 @@
package greenify.server.data.model;
import static greenify.server.data.model.User.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import greenify.common.ApplicationException;
import greenify.server.Application;
import org.junit.Test;
import java.util.ArrayList;
public class UserTest {
@Test
public void setAndGetTest() {
@@ -28,6 +35,13 @@ public class UserTest {
assertEquals("User(id=1, name=greenify, password=password, veganMeal=3)", user.toString());
}
@Test
public void equalsNullTest(){
User first = new User(1L, "greenify", "password", 0);
User second = null;
assertNotEquals(first, second);
}
@Test
public void equalsTest() {
User first = new User(1L, "greenify", "password", 3);
@@ -46,6 +60,12 @@ public class UserTest {
assertFalse(first.equals(second));
}
@Test
public void sameEqualsTest(){
User user = new User(6l, "Merel", "password", 0);
assertEquals(user, user);
}
@Test
public void instanceOfTest() {
User first = new User();
@@ -60,5 +80,25 @@ public class UserTest {
assertEquals(first, second);
assertEquals(first.hashCode(), second.hashCode());
}
@Test
public void addFriendTest(){
User user = new User(1l, "user", "friends", 0);
User friend = new User(2l, "friend", "friends", 0);
assertEquals(user.getFriends(), new ArrayList<User>());
user.addFriend(friend);
ArrayList<User> list = new ArrayList<User>();
list.add(friend);
assertEquals(user.getFriends(), list);
}
@Test
public void addYourselfTest(){
User user = new User(1l, "user", "friends", 0);
assertThrows(ApplicationException.class, () -> {
user.addFriend(user);
});
assertEquals(user.getFriends(), new ArrayList<User>());
}
}