Added tests and method to make sure you cannot add yourself as a friend

This commit is contained in:
Merel Steenbergen
2019-03-26 10:56:45 +01:00
parent 7a4a031b1d
commit cc9f8196bc
2 changed files with 28 additions and 2 deletions

View File

@@ -2,8 +2,10 @@ package greenify.server.data.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import greenify.common.ApplicationException;
import org.junit.Test;
import java.util.ArrayList;
@@ -90,7 +92,7 @@ public class UserTest {
}
@Test
public void addFriend(){
public void addFriendTest(){
User first = new User(1L, "greenify", "password");
User second = new User(1L, "merel", "password");
assertEquals(first.getFriends(), second.getFriends());
@@ -100,5 +102,13 @@ public class UserTest {
test.add(second);
assertEquals(first.getFriends(), test);
}
@Test
public void addYourselfTest(){
User test = new User(1L, "greenify", "password");
assertEquals(test.getFriends(), new ArrayList<User>());
assertThrows(ApplicationException.class, () -> {
test.addFriend(test);
}); }
}

View File

@@ -18,6 +18,8 @@ import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
@RunWith(SpringRunner.class)
public class UserServiceTest {
@TestConfiguration
@@ -45,7 +47,9 @@ public class UserServiceTest {
User alex = new User(1L, "alex", "password");
when(userRepository.findByName(alex.getName()))
.thenReturn(alex);
}
User lola = new User(2L, "lola", "password");
when(userRepository.findByName(lola.getName()))
.thenReturn(lola); }
@Test
public void validLoginTest() {
@@ -157,4 +161,16 @@ public class UserServiceTest {
userService.loginUser(null, null);
});
}
@Test
public void addFriendTest() {
User alex = userRepository.findByName("alex");
User lola = userRepository.findByName("lola");
assertEquals(lola.getFriends(), alex.getFriends());
userService.addFriend("alex", "lola");
ArrayList<User> test = new ArrayList<User>();
test.add(lola);
assertEquals(alex.getFriends(), test);
}
}