From 1da7f62c95d05189537eff9144d691dfa4e021f7 Mon Sep 17 00:00:00 2001 From: cugurlu Date: Sun, 17 Mar 2019 18:58:56 +0100 Subject: [PATCH] Fix the branch and add a test --- .../client/controller/UserController.java | 37 +++----- .../greenify/client/rest/UserService.java | 30 ++----- .../src/main/resources/fxml/dashboard.fxml | 89 +++---------------- .../java/greenify/common/ActivityDto.java | 25 +++++- src/Common/src/test/java/ActivityDtoTest.java | 31 +++++++ .../java/greenify/server/data/model/User.java | 67 +++----------- .../data/repository/UserRepository.java | 3 - .../greenify/server/rest/UserController.java | 11 ++- .../greenify/server/service/UserService.java | 24 ++--- 9 files changed, 104 insertions(+), 213 deletions(-) create mode 100644 src/Common/src/test/java/ActivityDtoTest.java diff --git a/src/Client/src/main/java/greenify/client/controller/UserController.java b/src/Client/src/main/java/greenify/client/controller/UserController.java index 23c8a47..bec5ade 100644 --- a/src/Client/src/main/java/greenify/client/controller/UserController.java +++ b/src/Client/src/main/java/greenify/client/controller/UserController.java @@ -1,9 +1,9 @@ package greenify.client.controller; +import greenify.client.Application; import greenify.client.rest.UserService; import javafx.event.ActionEvent; import javafx.fxml.FXML; -import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.Alert; @@ -23,15 +23,9 @@ public class UserController { UserService userService; @FXML - public TextField usernameField; - - @FXML + private TextField usernameField; private PasswordField passwordField; - - @FXML private Button loginButton; - - @FXML private Button signupButton; @FXML @@ -64,12 +58,10 @@ public class UserController { * @author sem */ public void openDashboard() throws IOException { - Parent dash = FXMLLoader.load( - this.getClass().getClassLoader().getResource("fxml/dashboard.fxml") - ); + Parent dash = Application.load(this.getClass().getClassLoader() + .getResource("fxml/dashboard.fxml")); Scene scene = new Scene(dash); - scene.getStylesheets().add(getClass() - .getClassLoader() + scene.getStylesheets().add(getClass().getClassLoader() .getResource("stylesheets/dashboardStyle.css").toExternalForm()); Stage appStage = new Stage(); appStage.setScene(scene); @@ -98,21 +90,16 @@ public class UserController { } /** - * handles the click of the 'sign up' button. - * opens a new stage where the user can register. - * @param event the click of the button - * @throws Exception an exception if the fxml file is not found + * The method handles register button. + * @param event User clicks to the button + * @throws Exception when the file couldn't find */ public void handleRegisterButtonAction(ActionEvent event) throws Exception { - //load the fxml file - Parent registerWindow = FXMLLoader.load( - this.getClass().getClassLoader().getResource("fxml/RegisterWindow.fxml") - ); - //make the window use the scene - Scene registerscene = new Scene(registerWindow); + Parent registerWindow = Application.load(this.getClass().getClassLoader() + .getResource("fxml/RegisterWindow.fxml")); + Scene registerScene = new Scene(registerWindow); Stage registerStage = new Stage(); - //open the window - registerStage.setScene(registerscene); + registerStage.setScene(registerScene); registerStage.setTitle("Enter register credentials"); registerStage.show(); } diff --git a/src/Client/src/main/java/greenify/client/rest/UserService.java b/src/Client/src/main/java/greenify/client/rest/UserService.java index 85b5199..fda9524 100644 --- a/src/Client/src/main/java/greenify/client/rest/UserService.java +++ b/src/Client/src/main/java/greenify/client/rest/UserService.java @@ -13,7 +13,6 @@ import org.springframework.web.util.UriComponentsBuilder; @Service public class UserService { - @Autowired RestTemplate restTemplate; @@ -28,9 +27,8 @@ public class UserService { * registers the user. * @param name the username of the user * @param password the password of the user - * @return a built userDTO with the required information + * @return a userDTO */ - @SuppressWarnings("Duplicates") public UserDto registerUser(String name, String password) { HttpHeaders headers = new HttpHeaders(); headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); @@ -46,12 +44,11 @@ public class UserService { } /** - * logd the user in. + * sign ins the user. * @param name the username of the user * @param password the password of the user - * @return a built userDTO with the required information + * @return a userDTO */ - @SuppressWarnings("Duplicates") public UserDto loginUser(String name, String password) { HttpHeaders headers = new HttpHeaders(); headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); @@ -67,12 +64,11 @@ public class UserService { } /** - * adds a vegetarian meal to the user. + * a user adds vegan meal. * @param id the id of the user * @param name the username of the user - * @return a built userDTO with the required information + * @return a userDTO */ - @SuppressWarnings("Duplicates") public UserDto addVeganMeal(Long id, String name) { HttpHeaders headers = new HttpHeaders(); headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); @@ -83,20 +79,4 @@ public class UserService { System.out.println(builder.build().encode().toUri()); return this.restTemplate.getForObject(builder.build().encode().toUri(), UserDto.class); } - - /** - * gets the username from the user. - * @param id the id of the user - * @return a UserDTO with the required information - */ - @SuppressWarnings("Duplicates") - public UserDto getName(Long id) { - HttpHeaders headers = new HttpHeaders(); - headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); - UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/getUsername") - .queryParam("id", id); - HttpEntity entity = new HttpEntity<>(headers); - System.out.println(builder.build().encode().toUri()); - return this.restTemplate.getForObject(builder.build().encode().toUri(), UserDto.class); - } } diff --git a/src/Client/src/main/resources/fxml/dashboard.fxml b/src/Client/src/main/resources/fxml/dashboard.fxml index 900ed93..1627774 100644 --- a/src/Client/src/main/resources/fxml/dashboard.fxml +++ b/src/Client/src/main/resources/fxml/dashboard.fxml @@ -1,15 +1,11 @@ - - - - @@ -39,84 +35,21 @@ - + - - - - - - - - - - - - - - - - - - - - - - - - - + + diff --git a/src/Common/src/main/java/greenify/common/ActivityDto.java b/src/Common/src/main/java/greenify/common/ActivityDto.java index 7f89776..0096fe0 100644 --- a/src/Common/src/main/java/greenify/common/ActivityDto.java +++ b/src/Common/src/main/java/greenify/common/ActivityDto.java @@ -9,26 +9,49 @@ public class ActivityDto { public ActivityDto() { } + /** + * Constructor for ActivityDto. + * @param id of the activity + * @param name of the activity + * @param description of the activity + * @param score of the activity + */ public ActivityDto(Long id, String name, String description, int score) { this.id = id; this.name = name; this.description = description; - this.score= score; + this.score = score; } public String getName() { return name; } + public void setName(String name) { + this.name = name; + } + public Long getId() { return id; } + public void setId(Long id) { + this.id = id; + } + public String getDescription() { return description; } + public void setDescription(String description) { + this.description = description; + } + public int getScore() { return score; } + + public void setScore(int score) { + this.score = score; + } } diff --git a/src/Common/src/test/java/ActivityDtoTest.java b/src/Common/src/test/java/ActivityDtoTest.java new file mode 100644 index 0000000..942eb6d --- /dev/null +++ b/src/Common/src/test/java/ActivityDtoTest.java @@ -0,0 +1,31 @@ +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import greenify.common.ActivityDto; +import org.junit.Test; + +public class ActivityDtoTest { + @Test + public void setAndGetTest() { + ActivityDto testActivity = new ActivityDto(); + testActivity.setId(1L); + testActivity.setName("eatVeganMeal"); + testActivity.setDescription("User adds a vegan meal"); + testActivity.setScore(10); + ActivityDto activity = new ActivityDto(1L, "eatVeganMeal", "User adds a vegan meal", 10); + assertTrue(activity.getId() == 1L); + assertEquals(activity.getName(), "eatVeganMeal"); + assertEquals(activity.getDescription(), "User adds a vegan meal"); + assertEquals(activity.getScore(), 10); + } + + @Test + public void equalsTest() { + ActivityDto first = new ActivityDto(1L, "eatVeganMeal", "User adds a vegan meal", 10); + ActivityDto second = new ActivityDto(1L, "eatVeganMeal", "User adds a vegan meal", 10); + assertEquals(first.getId(), second.getId()); + assertEquals(first.getName(), second.getName()); + assertEquals(first.getDescription(), second.getDescription()); + assertEquals(first.getScore(), second.getScore()); + } +} \ 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 8f7c03b..8ce92ea 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 @@ -3,15 +3,17 @@ package greenify.server.data.model; import lombok.Data; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import javax.persistence.*; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; import javax.validation.constraints.NotNull; -import java.util.Objects; @EnableAutoConfiguration @Entity @Data @Table(name = "users") - public class User { @Id @@ -74,7 +76,9 @@ public class User { return password; } - public void setPassword(String password) { this.password = password; } + public void setPassword(String password) { + this.password = password; + } /** * gets the number of vegan meal. @@ -84,58 +88,7 @@ public class User { return veganMeal; } - public void setVeganMeal(int veganMeal) { this.veganMeal = veganMeal; } - - /** - * checks if two users are equal. - * @param other the object to compare the user with - * @return a boolean value of true if the user is equal to the object - */ - @Override - public boolean equals(Object other) { - - if (this == other) { - return true; - } - if (other == null) { - return false; - } - if (getClass() != other.getClass()) { - return false; - } - if (other instanceof User) { - User that = (User) other; - return this.getName().equals(that.getName()) - && this.getId().equals(that.getId()) - && this.getPassword().equals(that.getPassword()) - && this.getVeganMeal() == that.getVeganMeal(); - } - return false; - } - - /** - * creates a string of the user object. - * in the form of: User(id=, name=, password=, veganMeal=) - * @return a string of the user object - */ - @Override - public String toString() { - return "User(id=" - + this.id - + ", name=" - + this.name - + ", password=" - + this.password - + ", veganMeal=" - + this.veganMeal + ")"; - } - - /** - * hashes the User object. - * @return a hashcode for the user object - */ - @Override - public int hashCode() { - return Objects.hash(id, name, password, veganMeal); + public void setVeganMeal(int veganMeal) { + this.veganMeal = veganMeal; } } 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 1e8d910..1c745f8 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 @@ -4,11 +4,8 @@ import greenify.server.data.model.User; import org.springframework.data.repository.CrudRepository; public interface UserRepository extends CrudRepository { - // User findByName(@Param("name") String name); User findByName(String name); - User findById(Long id); - 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 abb3a39..5918c63 100644 --- a/src/Server/src/main/java/greenify/server/rest/UserController.java +++ b/src/Server/src/main/java/greenify/server/rest/UserController.java @@ -2,8 +2,8 @@ package greenify.server.rest; import greenify.common.UserDto; import greenify.server.data.model.User; -import greenify.server.service.UserService; import greenify.server.data.repository.UserRepository; +import greenify.server.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -16,6 +16,10 @@ 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) { @@ -34,11 +38,6 @@ public class UserController { userService.addVeganMeal(id, name); } - @GetMapping("/getUsername") - public void getUsername(@RequestParam(value = "id") Long id) { - userService.getUsername(id); - - @GetMapping(path = "/all") @ResponseBody public Iterable getAllUsers() { 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 578fc7d..0d06526 100644 --- a/src/Server/src/main/java/greenify/server/service/UserService.java +++ b/src/Server/src/main/java/greenify/server/service/UserService.java @@ -23,16 +23,15 @@ public class UserService { */ public UserDto registerUser(String name, String password) { User user = userRepository.findByName(name); - if (user != null) { - throw new ApplicationException("User already exists"); - } else { + if (user == null) { user = userRepository.save(new User(null, name, password, 0)); + } else { + throw new ApplicationException("User already exists"); } logger.info("Created user id=" + user.getId() + ", name=" + user.getName()); return new UserDto(user.getId(), user.getName()); } - /** * logs the user in. * @param name the username of the user @@ -61,20 +60,9 @@ public class UserService { int count = user.getVeganMeal(); count++; user.setVeganMeal(count); - logger.info("Added vegan meal to user(id=" - + user.getId() + ", name=" + user.getName() + ")"); - } - - /** - * gets the username of the user with the specified id. - * @param id the id of the user - * @return the username of the user - */ - public String getUsername(Long id) { - User user = userRepository.findById(id); - String name = user.getName(); - logger.info("retrieved username from user with username=" + name + ", id=" + id); - return name; + userRepository.save(user); + logger.info("Added vegan meal to user(id=" + user.getId() + + ", name=" + user.getName() + ")"); } }