diff --git a/src/Client/src/test/java/ApplicationTest.java b/src/Client/src/test/java/ApplicationTest.java index e69de29..4e28ace 100644 --- a/src/Client/src/test/java/ApplicationTest.java +++ b/src/Client/src/test/java/ApplicationTest.java @@ -0,0 +1,12 @@ +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.SpringBootConfiguration; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootConfiguration +public class ApplicationTest { + + @Test + public void contextLoads() throws Exception{ } +} diff --git a/src/Server/src/main/java/greenify/server/data/model/Activity.java b/src/Server/src/main/java/greenify/server/data/model/Activity.java index d15eedf..397c126 100644 --- a/src/Server/src/main/java/greenify/server/data/model/Activity.java +++ b/src/Server/src/main/java/greenify/server/data/model/Activity.java @@ -131,4 +131,4 @@ public class Activity { public int hashCode() { return Objects.hash(id, name, description, score); } -} \ No newline at end of file +} diff --git a/src/Server/src/main/java/greenify/server/data/model/User.java b/src/Server/src/main/java/greenify/server/data/model/User.java index 8ce92ea..c7a82d0 100644 --- a/src/Server/src/main/java/greenify/server/data/model/User.java +++ b/src/Server/src/main/java/greenify/server/data/model/User.java @@ -91,4 +91,21 @@ public class User { public void setVeganMeal(int veganMeal) { this.veganMeal = veganMeal; } + + @Override + public boolean equals(Object other) { + if(other instanceof User){ + User that = (User)other; + if(that.id != this.id) + return false; + if(!that.name.equals(this.name)) + return false; + if(!that.password.equals(this.password)) + return false; + if(that.veganMeal != this.veganMeal) + return false; + return true; + } + return false; + } } diff --git a/src/Server/src/main/java/greenify/server/rest/RestExceptionHandler.java b/src/Server/src/main/java/greenify/server/rest/RestExceptionHandler.java index 0dfd57c..76e5b81 100644 --- a/src/Server/src/main/java/greenify/server/rest/RestExceptionHandler.java +++ b/src/Server/src/main/java/greenify/server/rest/RestExceptionHandler.java @@ -11,7 +11,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice; public class RestExceptionHandler { @ExceptionHandler(ApplicationException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) - public ErrorResponse applicationException(ApplicationException ex) { + public static ErrorResponse applicationException(ApplicationException ex) { return new ErrorResponse(ex.getMessage()); } } diff --git a/src/Server/src/main/java/greenify/server/rest/UserController.java b/src/Server/src/main/java/greenify/server/rest/UserController.java index 5918c63..b3efc56 100644 --- a/src/Server/src/main/java/greenify/server/rest/UserController.java +++ b/src/Server/src/main/java/greenify/server/rest/UserController.java @@ -16,10 +16,6 @@ public class UserController { @Autowired UserService userService; - @Autowired // This means to get the bean called userRepository - // Which is auto-generated by Spring, we will use it to handle the data - UserRepository userRepository; - @RequestMapping("/registerUser") public UserDto registerUser(@RequestParam(value = "name") String name, @RequestParam(value = "password") String password) { @@ -37,11 +33,4 @@ public class UserController { @RequestParam(value = "name") String name) { userService.addVeganMeal(id, name); } - - @GetMapping(path = "/all") - @ResponseBody - public Iterable getAllUsers() { - // This returns a JSON or XML with the users - return userRepository.findAll(); - } } \ No newline at end of file diff --git a/src/Server/src/main/java/greenify/server/service/UserService.java b/src/Server/src/main/java/greenify/server/service/UserService.java index 0d06526..94b3d1c 100644 --- a/src/Server/src/main/java/greenify/server/service/UserService.java +++ b/src/Server/src/main/java/greenify/server/service/UserService.java @@ -8,10 +8,13 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ResponseBody; @Service public class UserService { Logger logger = LoggerFactory.getLogger(UserService.class); + @Autowired UserRepository userRepository; @@ -64,5 +67,12 @@ public class UserService { logger.info("Added vegan meal to user(id=" + user.getId() + ", name=" + user.getName() + ")"); } + + @GetMapping(path = "/all") + @ResponseBody + public Iterable getAllUsers() { + // This returns a JSON or XML with the users + return userRepository.findAll(); + } } diff --git a/src/Server/src/test/java/greenify/server/data/model/ActivityTest.java b/src/Server/src/test/java/greenify/server/data/model/ActivityTest.java index 28e39b9..cb88d1b 100644 --- a/src/Server/src/test/java/greenify/server/data/model/ActivityTest.java +++ b/src/Server/src/test/java/greenify/server/data/model/ActivityTest.java @@ -1,18 +1,19 @@ -import greenify.server.data.model.Activity; -import org.junit.Test; +package greenify.server.data.model; import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.Test; + public class ActivityTest { @Test public void setAndGetTest() { - Activity activity = new Activity(1L, "Vegetarian", "Eating", 100); Activity testActivity = new Activity(0, null, null, 0); testActivity.setId(1L); testActivity.setName("Vegetarian"); testActivity.setDescription("Eating"); testActivity.setScore(100); + Activity activity = new Activity(1L, "Vegetarian", "Eating", 100); assertTrue(activity.getId().equals(1L)); assertEquals(activity.getName(), "Vegetarian"); assertEquals(activity.getDescription(), "Eating"); @@ -23,7 +24,8 @@ public class ActivityTest { @Test public void toStringTest() { Activity activity = new Activity(1, "Solar panels", "Installed", 10000); - assertEquals("Activity(id=1, name=Solar panels, description=Installed, score=10000)", activity.toString()); + assertEquals("Activity(id=1, name=Solar panels, " + + "description=Installed, score=10000)", activity.toString()); } @Test @@ -43,6 +45,5 @@ public class ActivityTest { Activity second = new Activity(1, "Solar panels", "Installed", 10000); assertEquals(first, second); assertEquals(first.hashCode(), second.hashCode()); -// assertTrue(first.hashCode() == second.hashCode()); } } 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 fbc1838..59e8ac0 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 @@ -1,6 +1,7 @@ package greenify.server.data.model; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.Test; @@ -35,6 +36,14 @@ public class UserTest { assertEquals(first.getName(), second.getName()); assertEquals(first.getPassword(), second.getPassword()); assertEquals(first.getVeganMeal(), second.getVeganMeal()); + assertTrue(first.equals(second)); + } + + @Test + public void instanceOfTest() { + User first = new User(); + Object second = new Object(); + assertFalse(first.equals(second)); } @Test @@ -42,7 +51,7 @@ public class UserTest { User first = new User(1L, "greenify", "password", 3); User second = new User(1L, "greenify", "password", 3); assertTrue(first.equals(second) && second.equals(first)); - assertTrue(first.hashCode() == second.hashCode()); + assertEquals(first, second); } } diff --git a/src/Server/src/test/java/greenify/server/data/repository/UserRepositoryTest.java b/src/Server/src/test/java/greenify/server/data/repository/UserRepositoryTest.java index 9383d21..a9f2974 100644 --- a/src/Server/src/test/java/greenify/server/data/repository/UserRepositoryTest.java +++ b/src/Server/src/test/java/greenify/server/data/repository/UserRepositoryTest.java @@ -1,37 +1,24 @@ -//package greenify.server.data.repository; -// -//import greenify.server.data.model.User; -//import org.junit.Test; -//import org.junit.runner.RunWith; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; -//import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; -//import org.springframework.test.context.junit4.SpringRunner; -// -//import static junit.framework.TestCase.assertTrue; -//import static org.junit.Assert.assertEquals; -// -//@RunWith(SpringRunner.class) -//@DataJpaTest -//public class UserRepositoryTest { -// -// @Autowired -// private TestEntityManager entityManager; -// -// @Autowired -// private UserRepository repository; -// -// @Test -// public void findByUsernameShouldReturnUser() throws Exception { -// this.entityManager.persist(new User(296L, "cugurlu", "password", 6)); -// User user = this.repository.findByName("cugurlu"); -// assertEquals(user.getName(), "cugurlu"); -// } -// -// @Test -// public void findByUsernameWhenNoUserShouldReturnNull() throws Exception { -// this.entityManager.persist(new User(296L, "cugurlu", "password", 6)); -// User user = this.repository.findByName("mouse"); -// assertTrue(user == null); -// } -//} +package greenify.server.data.repository; + +import static org.junit.Assert.assertEquals; + +import greenify.server.data.model.User; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@DataJpaTest +public class UserRepositoryTest { + @Autowired + private UserRepository repository; + + @Test + public void findByUsernameTest() throws Exception { + repository.save(new User(296L, "cugurlu", "password", 6)); + User user = this.repository.findByName("cugurlu"); + assertEquals(user.getName(), "cugurlu"); + } +} diff --git a/src/Server/src/test/java/greenify/server/rest/RestExceptionHandlerTest.java b/src/Server/src/test/java/greenify/server/rest/RestExceptionHandlerTest.java new file mode 100644 index 0000000..688c531 --- /dev/null +++ b/src/Server/src/test/java/greenify/server/rest/RestExceptionHandlerTest.java @@ -0,0 +1,17 @@ +package greenify.server.rest; + +import static org.junit.Assert.assertEquals; + +import greenify.common.ApplicationException; +import greenify.common.ErrorResponse; +import org.junit.Test; + +public class RestExceptionHandlerTest { + @Test + public void test() { + ApplicationException ex = new ApplicationException("testing"); + ErrorResponse response = new ErrorResponse("testing"); + assertEquals(RestExceptionHandler.applicationException(ex) + .getMessage(), response.getMessage()); + } +} \ No newline at end of file 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 a723114..3309583 100644 --- a/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java +++ b/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java @@ -1,63 +1,57 @@ -//package greenify.server.rest; -// -//import greenify.common.UserDto; -//import greenify.server.data.model.User; -//import greenify.server.service.UserService; -//import org.junit.Test; -//import org.junit.runner.RunWith; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; -//import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -//import org.springframework.boot.test.context.SpringBootTest; -//import org.springframework.boot.test.mock.mockito.MockBean; -//import org.springframework.context.ApplicationContext; -//import org.springframework.http.MediaType; -//import org.springframework.test.context.junit4.SpringRunner; -//import org.springframework.test.web.servlet.MockMvc; -//import org.springframework.test.web.servlet.ResultMatcher; -//import static org.assertj.core.internal.bytebuddy.matcher.ElementMatchers.is; -//import static org.hamcrest.Matchers.hasSize; -//import static org.mockito.BDDMockito.given; -//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; -//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; -// -//@RunWith(SpringRunner.class) -//@SpringBootTest -//@AutoConfigureMockMvc -//@AutoConfigureTestDatabase -//public class UserControllerTest { -// -// @Autowired -// private MockMvc mvc; -// -// @Autowired -// private ApplicationContext applicationContext; -// -// @MockBean -// private UserService userService; -// -// @Test -// public void getVehicleWhenRequestingTextShouldReturnMakeAndModel() throws Exception { -// given(this.userService.loginUser("name", "password")) -// .willReturn(new UserDto(1L, "name")); -// this.mvc.perform(get("/loginUser").accept(MediaType.APPLICATION_JSON)) -// .andExpect(status().isOk()) -// .andExpect(content() -// .json("name=name, password=password")); -// } -// -// -// @Test -// public void givenEmployees_whenGetEmployees_thenReturnJsonArray() throws Exception { -// User alex = new User(1L, "alex", "password", 0); -// UserDto user = userService.loginUser("alex", "password"); -// given(userService.loginUser("alex", "password")).willReturn(user); -// mvc.perform(get("/loginUser") -// .contentType(MediaType.ALL)) -// .andExpect(status().isOk()) -// .andExpect(jsonPath("$", hasSize(1))) -// .andExpect((ResultMatcher) jsonPath("$[0].name", is(alex.getName()))) -// .andExpect((ResultMatcher) jsonPath("$[0].password", is(alex.getPassword()))); -// } -//} \ No newline at end of file +package greenify.server.rest; + +import static org.mockito.BDDMockito.given; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import greenify.common.UserDto; +import greenify.server.service.UserService; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.ApplicationContext; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; + +@RunWith(SpringRunner.class) +@WebMvcTest(UserController.class) +public class UserControllerTest { + + @Autowired + private MockMvc mvc; + + @Autowired + private ApplicationContext applicationContext; + + @MockBean + private UserService userService; + + @Test + public void registerUserTest() throws Exception { + given(this.userService.registerUser("name", "password")) + .willReturn(new UserDto(1L, "name")); + mvc.perform(get("/registerUser") + .param("name", "name") + .param("password", "password") + .accept(MediaType.APPLICATION_JSON)) + .andDo(print()) + .andExpect(status().isOk()).andExpect(content().json("{'id':1,'name':'name'}")); + } + + @Test + public void loginUserTest() throws Exception { + given(this.userService.loginUser("ceren", "password")) + .willReturn(new UserDto(1L, "ceren")); + mvc.perform(get("/loginUser") + .param("name", "ceren") + .param("password", "password") + .accept(MediaType.APPLICATION_JSON)) + .andDo(print()) + .andExpect(status().isOk()).andExpect(content().json("{'id':1,'name':'ceren'}")); + } +} 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 9ef7169..ba52f6e 100644 --- a/src/Server/src/test/java/greenify/server/service/UserServiceTest.java +++ b/src/Server/src/test/java/greenify/server/service/UserServiceTest.java @@ -51,15 +51,30 @@ public class UserServiceTest { assertEquals(found.getName(), name); } + @Test + public void getAllUserTest() { + assertEquals(userRepository.findAll(), userService.getAllUsers()); + } + // @Test - // public void addVeganMealTest() { - // User user = new User(1L, "x", "y", 3); - // userRepository.save(user); - // System.out.println(userRepository); - // userService.addVeganMeal(1L, "x"); - // assertEquals(user.getVeganMeal(), 7); + // public void validRegisterTest() { + // String name = "new"; + // String password = "user"; + // if(userRepository.findByName(name) == null) { + // UserDto found = userService.registerUser(name, password); + // assertEquals(found.getName(), name); + // } // } + // @Test + // public void addVeganMealTest() { + // User user = new User(1L, "name", "password", 0); + // User user = userRepository.findByName("x"); + // int veganMeal = user.getVeganMeal(); + // userService.addVeganMeal(user.getId(), user.getName()); + // assertEquals(user.getVeganMeal(), veganMeal++); + // } + @Test public void invalidLoginTest() { User user = null;