Add new tests

This commit is contained in:
cugurlu
2019-03-31 00:51:55 +01:00
parent cf1704e872
commit b4a00fade2
3 changed files with 64 additions and 20 deletions

View File

@@ -9,7 +9,7 @@ import greenify.common.ApplicationException;
import org.junit.Test; import org.junit.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.List;
public class UserTest { public class UserTest {
@@ -113,18 +113,21 @@ public class UserTest {
}); });
} }
@Test @Test
public void friendsToStringTest() { public void addTwiceTest() {
User first = new User(1L, "greenify", "password"); User test = new User(1L, "greenify", "password");
User second = new User(1L, "merel", "password"); List<User> friendList = new ArrayList<>();
first.addFriend(second); friendList.add(test);
assertEquals(first.friendsToString(), "friends=[{name=merel, footprint=0.0}]"); User user = new User(1L, "green", "pass");
user.setFriends(friendList);
assertThrows(ApplicationException.class, () -> {
user.addFriend(test);
});
} }
@Test @Test
public void setFriendTest() { public void setFriendTest() {
Collection<User> friends = new ArrayList<>(); List<User> friends = new ArrayList<User>();
User first = new User(1L, "greenify", "password"); User first = new User(1L, "greenify", "password");
User second = new User(1L, "merel", "password"); User second = new User(1L, "merel", "password");
friends.add(second); friends.add(second);

View File

@@ -94,6 +94,14 @@ public class UserControllerTest {
assertEquals("merel", arg2Captor.getValue()); assertEquals("merel", arg2Captor.getValue());
} }
@Test
public void getAllUsersTest() throws Exception {
mvc.perform(get("/getAllUsers")
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk());
}
@Test @Test
public void getInputTest() throws Exception { public void getInputTest() throws Exception {
ArgumentCaptor<String> arg1Captor = ArgumentCaptor.forClass(String.class); ArgumentCaptor<String> arg1Captor = ArgumentCaptor.forClass(String.class);

View File

@@ -8,8 +8,10 @@ import greenify.common.ApplicationException;
import greenify.common.UserDto; import greenify.common.UserDto;
import greenify.server.data.model.User; import greenify.server.data.model.User;
import greenify.server.data.repository.UserRepository; import greenify.server.data.repository.UserRepository;
import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration; 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 org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
public class UserServiceTest { public class UserServiceTest {
@@ -118,7 +121,8 @@ public class UserServiceTest {
when(calculatorService.calculateFootprint(alex)) when(calculatorService.calculateFootprint(alex))
.thenReturn(15f); .thenReturn(15f);
userService.setInput("alex", "input_footprint_shopping_food_dairy_default", "6.5"); 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 @Test
@@ -139,18 +143,51 @@ public class UserServiceTest {
@Test @Test
public void getFootprintTest() { 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"); User alex = new User(1L, "alex", "password");
when(userRepository.findByName(alex.getName())) when(userRepository.findByName(alex.getName()))
.thenReturn(alex); .thenReturn(alex);
when(calculatorService.calculateFootprint(alex)) when(calculatorService.calculateFootprint(alex))
.thenReturn(15f); .thenReturn(15f);
userService.setInput("alex", "input_footprint_shopping_food_dairy_default", "6.5"); userService.saveFootprint("alex");
assertEquals(15f, userService.getFootprint("alex"), 0.0); 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<String> friendList = new ArrayList<>();
friendList.add("lola");
assertEquals(friendList, userService.getFriends("alex"));
} }
@Test @Test
public void getAllUserTest() { public void getAllUserTest() {
assertEquals(userRepository.findAll(), userService.getAllUsers()); User alex = new User(1L, "alex", "password");
User lola = new User(2L, "lola", "pass");
Iterable<User> users = new ArrayList<>();
((ArrayList<User>) users).add(alex);
((ArrayList<User>) users).add(lola);
when(userRepository.findAll())
.thenReturn(users);
List<String> userList = new ArrayList<>();
userList.add("alex");
userList.add("lola");
assertEquals(userList, userService.getAllUsers());
} }
@Test @Test
@@ -170,16 +207,12 @@ public class UserServiceTest {
} }
@Test @Test
public void addFriendsExceptionTest() { public void addFriendNullFriendTest() {
assertThrows(ApplicationException.class, () -> userService.addFriend("greenify", null)); assertThrows(ApplicationException.class, () -> userService.addFriend("alex", null));
} }
@Test @Test
public void leaderboardTest() { public void addFriendNullUserTest() {
User alex = userRepository.findByName("alex"); assertThrows(ApplicationException.class, () -> userService.addFriend(null, "password"));
User lola = userRepository.findByName("lola");
userService.addFriend("alex", "lola");
assertEquals(userService.getLeaderboard("alex"), "friends=[{name=lola, footprint=0.0}]");
} }
} }