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 b8bbabc..f95b2c4 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 @@ -9,7 +9,7 @@ import greenify.common.ApplicationException; import org.junit.Test; import java.util.ArrayList; -import java.util.Collection; +import java.util.List; public class UserTest { @@ -113,18 +113,21 @@ public class UserTest { }); } - @Test - public void friendsToStringTest() { - User first = new User(1L, "greenify", "password"); - User second = new User(1L, "merel", "password"); - first.addFriend(second); - assertEquals(first.friendsToString(), "friends=[{name=merel, footprint=0.0}]"); + public void addTwiceTest() { + User test = new User(1L, "greenify", "password"); + List friendList = new ArrayList<>(); + friendList.add(test); + User user = new User(1L, "green", "pass"); + user.setFriends(friendList); + assertThrows(ApplicationException.class, () -> { + user.addFriend(test); + }); } @Test public void setFriendTest() { - Collection friends = new ArrayList<>(); + List friends = new ArrayList(); User first = new User(1L, "greenify", "password"); User second = new User(1L, "merel", "password"); friends.add(second); diff --git a/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java b/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java index 4b6c70e..101bf6f 100644 --- a/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java +++ b/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java @@ -94,6 +94,14 @@ public class UserControllerTest { assertEquals("merel", arg2Captor.getValue()); } + @Test + public void getAllUsersTest() throws Exception { + mvc.perform(get("/getAllUsers") + .accept(MediaType.APPLICATION_JSON)) + .andDo(print()) + .andExpect(status().isOk()); + } + @Test public void getInputTest() throws Exception { ArgumentCaptor arg1Captor = ArgumentCaptor.forClass(String.class); 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 a782034..89a7d61 100644 --- a/src/Server/src/test/java/greenify/server/service/UserServiceTest.java +++ b/src/Server/src/test/java/greenify/server/service/UserServiceTest.java @@ -8,8 +8,10 @@ import greenify.common.ApplicationException; import greenify.common.UserDto; import greenify.server.data.model.User; import greenify.server.data.repository.UserRepository; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.junit.jupiter.api.Assertions; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.TestConfiguration; @@ -18,6 +20,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.test.context.junit4.SpringRunner; import java.util.ArrayList; +import java.util.List; @RunWith(SpringRunner.class) public class UserServiceTest { @@ -118,7 +121,8 @@ public class UserServiceTest { when(calculatorService.calculateFootprint(alex)) .thenReturn(15f); userService.setInput("alex", "input_footprint_shopping_food_dairy_default", "6.5"); - assertEquals(15f, alex.getFootPrint(), 0.0); + Assert.assertTrue(alex.getFootPrintInputs() + .get("input_footprint_shopping_food_dairy_default").equals("6.5")); } @Test @@ -139,18 +143,51 @@ public class UserServiceTest { @Test public void getFootprintTest() { + User alex = new User(1L, "alex", "password"); + alex.setFootPrint(15F); + when(userRepository.findByName(alex.getName())) + .thenReturn(alex); + Assertions.assertEquals(15F, userService.getFootprint("alex")); + } + + @Test + public void saveFootprintTest() { User alex = new User(1L, "alex", "password"); when(userRepository.findByName(alex.getName())) .thenReturn(alex); when(calculatorService.calculateFootprint(alex)) .thenReturn(15f); - userService.setInput("alex", "input_footprint_shopping_food_dairy_default", "6.5"); - assertEquals(15f, userService.getFootprint("alex"), 0.0); + userService.saveFootprint("alex"); + Assertions.assertEquals(15f, userService.saveFootprint("alex")); + } + + @Test + public void getFriendsTest() { + User alex = new User(1L, "alex", "password"); + User lola = new User(2L, "lola", "pass"); + when(userRepository.findByName(alex.getName())) + .thenReturn(alex); + when(userRepository.findByName(lola.getName())) + .thenReturn(lola); + alex.addFriend(lola); + List friendList = new ArrayList<>(); + friendList.add("lola"); + assertEquals(friendList, userService.getFriends("alex")); } @Test public void getAllUserTest() { - assertEquals(userRepository.findAll(), userService.getAllUsers()); + User alex = new User(1L, "alex", "password"); + User lola = new User(2L, "lola", "pass"); + Iterable users = new ArrayList<>(); + ((ArrayList) users).add(alex); + ((ArrayList) users).add(lola); + when(userRepository.findAll()) + .thenReturn(users); + List userList = new ArrayList<>(); + userList.add("alex"); + userList.add("lola"); + assertEquals(userList, userService.getAllUsers()); } @Test @@ -170,16 +207,12 @@ public class UserServiceTest { } @Test - public void addFriendsExceptionTest() { - assertThrows(ApplicationException.class, () -> userService.addFriend("greenify", null)); + public void addFriendNullFriendTest() { + assertThrows(ApplicationException.class, () -> userService.addFriend("alex", null)); } @Test - public void leaderboardTest() { - User alex = userRepository.findByName("alex"); - User lola = userRepository.findByName("lola"); - userService.addFriend("alex", "lola"); - assertEquals(userService.getLeaderboard("alex"), "friends=[{name=lola, footprint=0.0}]"); - + public void addFriendNullUserTest() { + assertThrows(ApplicationException.class, () -> userService.addFriend(null, "password")); } }