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 39f385b..f66c691 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 @@ -34,12 +34,12 @@ public class User { private Float footPrint = 0.0f; @ElementCollection - private Map footPrintInputs = new HashMap<>(); + private Map footPrintInputs = new HashMap<>(); - public User() {} + User() { } /** - * makes a user object. + * This method makes a user object. * @param id the id of the user. * @param name the supplied username * @param password the supplied password @@ -52,64 +52,87 @@ public class User { } /** - * gets the id. - * @return the id + * This method returns the ID of the user. + * @return the id of the user */ public Long getId() { return id; } - public void setId(Long id) { + /** + * This method sets the ID of the user. + * @param id the id of the user + */ + void setId(Long id) { this.id = id; } /** - * gets the name. - * @return the name + * This method returns the name of the user. + * @return the name of the user */ public String getName() { return name; } + /** + * This method sets the name of the user. + * @param name the name of the user + */ public void setName(String name) { this.name = name; } /** - * gets the password. - * @return the password + * This method returns the password of the user. + * @return the password of the user */ public String getPassword() { return password; } - public void setPassword(String password) { + /** + * This method sets the password of the user. + * @param password the password of the user + */ + void setPassword(String password) { this.password = password; } /** - * gets the footPrint of user. - * @return the footPrint + * This method returns the footPrint of user. + * @return the footprint of the user */ public Float getFootPrint() { return footPrint; } - public Map getFootPrintInputs() { - return footPrintInputs; - } - - public void setFootPrintInputs(Map footPrintInputs) { - this.footPrintInputs = footPrintInputs; - } - + /** + * This method sets the footprint of a user. + * @param footPrint footprint of a user + */ public void setFootPrint(Float footPrint) { this.footPrint = footPrint; } + /** + * This method returns the footprint inputs of the user. + * @return footprint inputs of the user + */ + public Map getFootPrintInputs() { + return footPrintInputs; + } /** - * Returns a human readable object. It's in JSON. + * This method sets the footprint inputs of the user. + * @param footPrintInputs footprint inputs of the user + */ + public void setFootPrintInputs(Map footPrintInputs) { + this.footPrintInputs = footPrintInputs; + } + + /** + * This method returns a human readable (JSON) object. * @return the JSON form of the object. */ @Override @@ -118,18 +141,25 @@ public class User { + this.password + ")"; } + /** + * This method checks whether two users are equal or not. + * @param other an other user + * @return users are (not) equal + */ @Override public boolean equals(Object other) { if (other instanceof User) { - User that = (User)other; - if (that.id == this.id && that.name.equals(this.name) - && that.password.equals(this.password)) { - return true; - } + User that = (User) other; + return that.id.equals(this.id) && that.name.equals(this.name) + && that.password.equals(this.password); } return false; } + /** + * This method returns the hashcode of a user. + * @return hashcode of a user + */ @Override public int hashCode() { return Objects.hash(id, name, password); diff --git a/src/Server/src/main/java/greenify/server/data/repository/UserRepository.java b/src/Server/src/main/java/greenify/server/data/repository/UserRepository.java index 182bda1..baf9b4c 100644 --- a/src/Server/src/main/java/greenify/server/data/repository/UserRepository.java +++ b/src/Server/src/main/java/greenify/server/data/repository/UserRepository.java @@ -3,10 +3,17 @@ package greenify.server.data.repository; import greenify.server.data.model.User; import org.springframework.data.repository.CrudRepository; -//userRepository that saves all the user and talks to the database +/** + * UserRepository that saves all the users and talks to the database. + */ public interface UserRepository extends CrudRepository { User findByName(String name); + /** + * This method saves a user in the database. + * @param user the user you want saved + * @param always a user + * @return the user + */ T save(T user); } - 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 94800d8..05ed9db 100644 --- a/src/Server/src/main/java/greenify/server/rest/UserController.java +++ b/src/Server/src/main/java/greenify/server/rest/UserController.java @@ -10,24 +10,43 @@ import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @Autowired + private UserService userService; + /** + * This method registers the user. + * @param name name of the user + * @param password password of the user + * @return the userDto of the user + */ @RequestMapping("/registerUser") public UserDto registerUser(@RequestParam(value = "name") String name, @RequestParam(value = "password") String password) { return userService.registerUser(name, password); } + /** + * This method logs in the user. + * @param name name of the user + * @param password password of the user + * @return the userDto of the user + */ @RequestMapping("/loginUser") public UserDto loginUser(@RequestParam(value = "name") String name, @RequestParam(value = "password") String password) { return userService.loginUser(name, password); } + /** + * This method sets input for a user. + * @param name name of the user + * @param inputName name of the input of the user + * @param value value of the input + */ @RequestMapping("/setInput") public void setInput(@RequestParam(value = "name") String name, @RequestParam(value = "inputName") String inputName, @RequestParam(value = "value") String value) { userService.setInput(name, inputName, value); } -} \ 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 61ff0d9..66e1bde 100644 --- a/src/Server/src/main/java/greenify/server/service/UserService.java +++ b/src/Server/src/main/java/greenify/server/service/UserService.java @@ -14,7 +14,7 @@ import org.springframework.web.bind.annotation.ResponseBody; @Service public class UserService { - Logger logger = LoggerFactory.getLogger(UserService.class); + private Logger logger = LoggerFactory.getLogger(UserService.class); @Autowired UserRepository userRepository; @@ -23,10 +23,10 @@ public class UserService { CalculatorService calculatorService; /** - * registers the user. - * @param name the username of the user - * @param password the password of the user - * @return a userDTO of the registered user + * This method registers the user. + * @param name name of the user + * @param password password of the user + * @return the userDto of the user */ public UserDto registerUser(String name, String password) { User user = userRepository.findByName(name); @@ -42,10 +42,10 @@ public class UserService { } /** - * logs the user in. - * @param name the username of the user - * @param password the password of the user - * @return a userDTO of the logged in user + * This method logs in the user. + * @param name name of the user + * @param password password of the user + * @return the userDto of the user */ public UserDto loginUser(String name, String password) { User user = userRepository.findByName(name); @@ -60,10 +60,10 @@ public class UserService { } /** - * The method sets input value. - * @param name of the user - * @param inputName is the name of the setting input - * @param value of the input + * This method sets input for a user. + * @param name name of the user + * @param inputName name of the input of the user + * @param value value of the input */ public void setInput(String name, String inputName, String value) { User user = userRepository.findByName(name); @@ -81,31 +81,37 @@ public class UserService { } /** - * Gets the input value of an input. + * This method returns the input value of an input. * @param name of the user * @param inputName name of the input * @return input value */ - public String getInput(String name, String inputName) { + String getInput(String name, String inputName) { User user = userRepository.findByName(name); if (InputValidator.isValidItem(inputName)) { - String item = user.getFootPrintInputs().get(inputName); - return item; + return user.getFootPrintInputs().get(inputName); } else { throw new ApplicationException("Invalid input"); } } - public Float getFootprint(String name) { + /** + * This method returns the footprint of a user. + * @param name name of the user + * @return footprint of the user + */ + Float getFootprint(String name) { User user = userRepository.findByName(name); return calculatorService.calculateFootprint(user); } + /** + * This method returns a JSON of XML with all users. + * @return JSON/XML of all users + */ @GetMapping(path = "/all") @ResponseBody - public Iterable getAllUsers() { - // This returns a JSON or XML with the users + Iterable getAllUsers() { return userRepository.findAll(); } } - 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 3a27fb8..be8eb81 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,12 +1,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.assertTrue; - import org.junit.Test; +import static org.junit.Assert.*; public class UserTest { + @Test public void setAndGetTest() { User testUser = new User(); @@ -14,7 +12,7 @@ public class UserTest { testUser.setName("greenify"); testUser.setPassword("password"); User user = new User(1L, "greenify", "password"); - assertTrue(user.getId().equals(1L)); + assertEquals(1L, (long) user.getId()); assertEquals(user.getName(), "greenify"); assertEquals(user.getPassword(), "password"); assertEquals(user, testUser); @@ -28,47 +26,47 @@ public class UserTest { @Test public void equalsTest() { - User first = new User(1L, "greenify", "password"); - User second = new User(1L, "greenify", "password"); + User first = new User(2L, "greenify", "12345"); + User second = new User(2L, "greenify", "12345"); assertEquals(first.getId(), second.getId()); assertEquals(first.getName(), second.getName()); assertEquals(first.getPassword(), second.getPassword()); - assertTrue(first.equals(second)); + assertEquals(first, second); } @Test public void equalsDifferentId() { User first = new User(1L, "greenify", "password"); User second = new User(2L, "greenify", "password"); - assertFalse(first.equals(second)); + assertNotEquals(first, second); } @Test public void equalsDifferentName() { - User first = new User(1L, "greenify", "password"); - User second = new User(1L, "hello", "password"); - assertFalse(first.equals(second)); + User first = new User(5L, "greenify", "password"); + User second = new User(5L, "hello", "password"); + assertNotEquals(first, second); } @Test public void equalsDifferentPassword() { User first = new User(1L, "greenify", "password"); User second = new User(1L, "greenify", "hi"); - assertFalse(first.equals(second)); + assertNotEquals(first, second); } @Test public void notEqualsTest() { User first = new User(1L, "greenify", "password"); User second = new User(2L, "greenify", "password"); - assertFalse(first.equals(second)); + assertNotEquals(first, second); } @Test public void instanceOfTest() { User first = new User(); Object second = new Object(); - assertFalse(first.equals(second)); + assertNotEquals(first, second); } @Test @@ -78,5 +76,5 @@ public class UserTest { assertEquals(first, second); assertEquals(first.hashCode(), second.hashCode()); } -} +} 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 1a00660..fcdb09e 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,7 +1,6 @@ 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; @@ -16,9 +15,10 @@ public class UserRepositoryTest { private UserRepository repository; @Test - public void findByUsernameTest() throws Exception { + public void findByUsernameTest() { repository.save(new User(296L, "cugurlu", "password")); User user = this.repository.findByName("cugurlu"); assertEquals(user.getName(), "cugurlu"); } + } 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 bb3d1fa..553c833 100644 --- a/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java +++ b/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java @@ -56,7 +56,6 @@ public class UserControllerTest { } @Test - public void setInputTest() throws Exception { - - } + public void setInputTest() { + } } 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 bf06ce8..7376cd7 100644 --- a/src/Server/src/test/java/greenify/server/service/UserServiceTest.java +++ b/src/Server/src/test/java/greenify/server/service/UserServiceTest.java @@ -1,7 +1,6 @@ package greenify.server.service; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.when; @@ -57,9 +56,7 @@ public class UserServiceTest { @Test public void loginExceptionTest() { - assertThrows(ApplicationException.class, () -> { - userService.loginUser("alex", "greenify"); - }); + assertThrows(ApplicationException.class, () -> userService.loginUser("alex", "greenify")); } @Test @@ -71,9 +68,7 @@ public class UserServiceTest { @Test public void registerExceptionTest() { - assertThrows(ApplicationException.class, () -> { - userService.registerUser("alex", "password"); - }); + assertThrows(ApplicationException.class, () -> userService.registerUser("alex", "password")); } @Test @@ -87,23 +82,17 @@ public class UserServiceTest { @Test public void setInputNullTest() { - assertThrows(ApplicationException.class, () -> { - userService.setInput(null, "hello", "5.5"); - }); + assertThrows(ApplicationException.class, () -> userService.setInput(null, "hello", "5.5")); } @Test public void setInputApplicationTestItem() { - assertThrows(ApplicationException.class, () -> { - userService.setInput("alex", "hello", "3.5"); - }); + assertThrows(ApplicationException.class, () -> userService.setInput("tom", "hello", "3.5")); } @Test public void setInputApplicationTestValue() { - assertThrows(ApplicationException.class, () -> { - userService.setInput("alex", "transportation_num_vehicles", "5.5"); - }); + assertThrows(ApplicationException.class, () -> userService.setInput("tom", "transportation_num_vehicles", "5.5")); } @@ -115,7 +104,7 @@ public class UserServiceTest { when(calculatorService.calculateFootprint(alex)) .thenReturn(15f); userService.setInput("alex", "food_grains", "6.5"); - assertTrue(15f == alex.getFootPrint()); + assertEquals(15f, alex.getFootPrint(), 0.0); } @Test @@ -129,9 +118,7 @@ public class UserServiceTest { @Test public void getInputExceptionTest() { - assertThrows(ApplicationException.class, () -> { - userService.getInput("alex", "hello"); - }); + assertThrows(ApplicationException.class, () -> userService.getInput("alex", "hello")); } @Test @@ -142,7 +129,7 @@ public class UserServiceTest { when(calculatorService.calculateFootprint(alex)) .thenReturn(15f); userService.setInput("alex", "food_grains", "6.5"); - assertTrue(15f == userService.getFootprint("alex")); + assertEquals(15f, userService.getFootprint("alex"), 0.0); } @Test @@ -152,9 +139,7 @@ public class UserServiceTest { @Test public void invalidLoginTest() { - User user = null; - assertThrows(ApplicationException.class, () -> { - userService.loginUser(null, null); - }); + assertThrows(ApplicationException.class, () -> userService.loginUser(null, null)); } + }