From a2e432d4533a52cc8aaaeeef3c3707084a428808 Mon Sep 17 00:00:00 2001 From: cugurlu Date: Mon, 18 Mar 2019 12:14:30 +0100 Subject: [PATCH 1/3] Add totalVeganMeal property and fix checkstyle errors --- .../controller/DashBoardController.java | 24 ++++++---- .../client/controller/UserController.java | 6 +++ .../greenify/client/rest/UserService.java | 7 +++ .../src/main/resources/fxml/dashboard.fxml | 47 ++++++++++--------- src/Client/src/test/java/UserServiceTest.java | 6 +-- .../main/java/greenify/common/UserDto.java | 18 ++++++- src/Common/src/test/java/UserDtoTest.java | 8 ++-- .../greenify/server/rest/UserController.java | 4 -- .../greenify/server/service/UserService.java | 4 +- .../server/rest/UserControllerTest.java | 4 +- .../server/service/UserServiceTest.java | 19 -------- 11 files changed, 81 insertions(+), 66 deletions(-) diff --git a/src/Client/src/main/java/greenify/client/controller/DashBoardController.java b/src/Client/src/main/java/greenify/client/controller/DashBoardController.java index ff2cde2..a69a392 100644 --- a/src/Client/src/main/java/greenify/client/controller/DashBoardController.java +++ b/src/Client/src/main/java/greenify/client/controller/DashBoardController.java @@ -3,8 +3,7 @@ package greenify.client.controller; import greenify.client.rest.UserService; import javafx.event.ActionEvent; import javafx.fxml.FXML; -import javafx.scene.control.Button; -import javafx.scene.control.Label; +import javafx.scene.control.TextField; import javafx.scene.layout.AnchorPane; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -17,17 +16,19 @@ public class DashBoardController { private int count = 0; @FXML - private AnchorPane menuBar; private AnchorPane dashboardPane; + + @FXML private AnchorPane userPane; + + @FXML private AnchorPane activitiesPane; - private Label welcomebacktext; - private Button dashboardButton; - private Button activitiesButton; - private Button userButton; - private Button veganMealButton; - private Label counter; - private Label scoreField; + + @FXML + private TextField counter; + + @FXML + private TextField totalCount; /** * displays the dashboard pane. @@ -45,6 +46,7 @@ public class DashBoardController { * @param event the event (clicking the button) */ public void displayActivities(ActionEvent event) { + totalCount.setText("Total Count: " + userService.currentUser.getVeganMeal()); System.out.println("display activities"); dashboardPane.setVisible(false); userPane.setVisible(false); @@ -68,6 +70,8 @@ public class DashBoardController { */ public void addVeganMeal(ActionEvent event) { count++; + int net = userService.currentUser.getVeganMeal() + count; + totalCount.setText("Total Count: " + net); counter.setText("Count: " + count); System.out.println(userService); userService.addVeganMeal(userService.currentUser.getId(), 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 bec5ade..04fd9fe 100644 --- a/src/Client/src/main/java/greenify/client/controller/UserController.java +++ b/src/Client/src/main/java/greenify/client/controller/UserController.java @@ -24,8 +24,14 @@ public class UserController { @FXML private TextField usernameField; + + @FXML private PasswordField passwordField; + + @FXML private Button loginButton; + + @FXML private Button signupButton; @FXML 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 fda9524..27392f4 100644 --- a/src/Client/src/main/java/greenify/client/rest/UserService.java +++ b/src/Client/src/main/java/greenify/client/rest/UserService.java @@ -8,6 +8,8 @@ import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; @@ -79,4 +81,9 @@ public class UserService { System.out.println(builder.build().encode().toUri()); return this.restTemplate.getForObject(builder.build().encode().toUri(), UserDto.class); } + + @RequestMapping("/userData") + public int getVeganData(@RequestParam(value = "veganMeal") int veganMeal) { + return veganMeal; + } } diff --git a/src/Client/src/main/resources/fxml/dashboard.fxml b/src/Client/src/main/resources/fxml/dashboard.fxml index 1627774..42ae999 100644 --- a/src/Client/src/main/resources/fxml/dashboard.fxml +++ b/src/Client/src/main/resources/fxml/dashboard.fxml @@ -1,13 +1,34 @@ - + + + - + + + - - + + + + + + + + + + + + + + + - - - - - - - - - - diff --git a/src/Client/src/test/java/UserServiceTest.java b/src/Client/src/test/java/UserServiceTest.java index 034d840..bee3858 100644 --- a/src/Client/src/test/java/UserServiceTest.java +++ b/src/Client/src/test/java/UserServiceTest.java @@ -25,7 +25,7 @@ public class UserServiceTest { @Test public void userRegisterTest() throws Exception { - UserDto testUser = new UserDto(1L, "Eric"); + UserDto testUser = new UserDto(1L, "Eric", 0); Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/registerUser?name=Eric&password=password"), UserDto.class)) .thenReturn(testUser); @@ -36,7 +36,7 @@ public class UserServiceTest { @Test public void userLoginTest() throws Exception { - UserDto testUser = new UserDto(1L, "Eric"); + UserDto testUser = new UserDto(1L, "Eric", 0); Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/loginUser?name=Eric&password=password"), UserDto.class)) .thenReturn(testUser); @@ -46,7 +46,7 @@ public class UserServiceTest { @Test public void addVeganMealTest() throws Exception { - UserDto testUser = new UserDto(1L, "Eric"); + UserDto testUser = new UserDto(1L, "Eric", 0); Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/addVeganMeal?id=1&name=Eric"), UserDto.class)) .thenReturn(testUser); diff --git a/src/Common/src/main/java/greenify/common/UserDto.java b/src/Common/src/main/java/greenify/common/UserDto.java index 8c67cd2..def3c13 100644 --- a/src/Common/src/main/java/greenify/common/UserDto.java +++ b/src/Common/src/main/java/greenify/common/UserDto.java @@ -7,13 +7,21 @@ package greenify.common; public class UserDto { private Long id; private String name; + private int veganMeal; public UserDto() { } - public UserDto(Long id, String name) { + /** + * The constructor method of UserDto. + * @param id of the user + * @param name of the user + * @param veganMeal the number of vegetarian meals eaten + */ + public UserDto(Long id, String name, int veganMeal) { this.id = id; this.name = name; + this.veganMeal = veganMeal; } public String getName() { @@ -31,4 +39,12 @@ public class UserDto { public void setId(Long id) { this.id = id; } + + public int getVeganMeal() { + return veganMeal; + } + + public void setVeganMeal(int veganMeal) { + this.veganMeal = veganMeal; + } } \ No newline at end of file diff --git a/src/Common/src/test/java/UserDtoTest.java b/src/Common/src/test/java/UserDtoTest.java index 1d1d75b..3c92691 100644 --- a/src/Common/src/test/java/UserDtoTest.java +++ b/src/Common/src/test/java/UserDtoTest.java @@ -7,18 +7,20 @@ import org.junit.Test; public class UserDtoTest { @Test public void setAndGetTest() { - UserDto user = new UserDto(1L, "greenify"); UserDto testUser = new UserDto(); testUser.setId(1L); testUser.setName("greenify"); + testUser.setVeganMeal(0); + UserDto user = new UserDto(1L, "greenify", 0); assertTrue(user.getId() == 1L); assertEquals(user.getName(), "greenify"); + assertTrue(user.getVeganMeal() == 0); } @Test public void equalsTest() { - UserDto first = new UserDto(1L, "greenify"); - UserDto second = new UserDto(1L, "greenify"); + UserDto first = new UserDto(1L, "greenify", 0); + UserDto second = new UserDto(1L, "greenify", 0); assertEquals(first.getId(), second.getId()); assertEquals(first.getName(), second.getName()); } 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 b3efc56..52f62b9 100644 --- a/src/Server/src/main/java/greenify/server/rest/UserController.java +++ b/src/Server/src/main/java/greenify/server/rest/UserController.java @@ -1,14 +1,10 @@ package greenify.server.rest; import greenify.common.UserDto; -import greenify.server.data.model.User; -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; import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController 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 94b3d1c..94ab431 100644 --- a/src/Server/src/main/java/greenify/server/service/UserService.java +++ b/src/Server/src/main/java/greenify/server/service/UserService.java @@ -32,7 +32,7 @@ public class UserService { throw new ApplicationException("User already exists"); } logger.info("Created user id=" + user.getId() + ", name=" + user.getName()); - return new UserDto(user.getId(), user.getName()); + return new UserDto(user.getId(), user.getName(), user.getVeganMeal()); } /** @@ -50,7 +50,7 @@ public class UserService { throw new ApplicationException("Wrong password"); } } - return new UserDto(user.getId(), user.getName()); + return new UserDto(user.getId(), user.getName(), user.getVeganMeal()); } /** 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 3309583..9daa437 100644 --- a/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java +++ b/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java @@ -34,7 +34,7 @@ public class UserControllerTest { @Test public void registerUserTest() throws Exception { given(this.userService.registerUser("name", "password")) - .willReturn(new UserDto(1L, "name")); + .willReturn(new UserDto(1L, "name", 0)); mvc.perform(get("/registerUser") .param("name", "name") .param("password", "password") @@ -46,7 +46,7 @@ public class UserControllerTest { @Test public void loginUserTest() throws Exception { given(this.userService.loginUser("ceren", "password")) - .willReturn(new UserDto(1L, "ceren")); + .willReturn(new UserDto(1L, "ceren", 0)); mvc.perform(get("/loginUser") .param("name", "ceren") .param("password", "password") 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 ba52f6e..afd5550 100644 --- a/src/Server/src/test/java/greenify/server/service/UserServiceTest.java +++ b/src/Server/src/test/java/greenify/server/service/UserServiceTest.java @@ -56,25 +56,6 @@ public class UserServiceTest { assertEquals(userRepository.findAll(), userService.getAllUsers()); } - // @Test - // 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; From f76a6827f7e698c66da6378fe912fd91ee1774cf Mon Sep 17 00:00:00 2001 From: cugurlu Date: Mon, 18 Mar 2019 12:15:00 +0100 Subject: [PATCH 2/3] Fix tests --- .../greenify/server/data/model/Activity.java | 24 ++++++------- .../java/greenify/server/data/model/User.java | 34 ++++++++++++------- .../server/data/model/ActivityTest.java | 8 +++++ .../greenify/server/data/model/UserTest.java | 9 ++++- 4 files changed, 48 insertions(+), 27 deletions(-) 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 397c126..d73e387 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 @@ -1,17 +1,17 @@ package greenify.server.data.model; import lombok.Data; -import javax.persistence.*; + import java.util.Objects; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.Table; @Entity @Data @Table(name = "activities") - public class Activity { @Id @@ -88,7 +88,7 @@ public class Activity { * sets the description. * @param description the description to be set. */ - public void setDescription(String description){ + public void setDescription(String description) { this.description = description; } @@ -107,22 +107,18 @@ public class Activity { */ @Override public String toString() { - return "Activity(id=" + this.id + ", name=" + this.name + ", description=" + this.description + ", score=" + this.score + ")"; + return "Activity(id=" + this.id + ", name=" + this.name + ", description=" + + this.description + ", score=" + this.score + ")"; } @Override public boolean equals(Object other) { - if(other instanceof Activity){ + if (other instanceof Activity) { Activity that = (Activity)other; - if(that.id != this.id) - return false; - if(!that.name.equals(this.name)) - return false; - if(!that.description.equals(this.description)) - return false; - if(that.score != this.score) - return false; - return true; + if (that.id == this.id && that.name.equals(this.name) + && that.description.equals(this.description) && that.score == this.score) { + return true; + } } return false; } 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 c7a82d0..6ba6f73 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 @@ -1,8 +1,8 @@ package greenify.server.data.model; import lombok.Data; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import java.util.Objects; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; @@ -10,7 +10,6 @@ import javax.persistence.Id; import javax.persistence.Table; import javax.validation.constraints.NotNull; -@EnableAutoConfiguration @Entity @Data @Table(name = "users") @@ -92,20 +91,31 @@ public class User { this.veganMeal = veganMeal; } + + /** + * Returns a human readable object. It's in JSON. + * @return the JSON form of the object. + */ + @Override + public String toString() { + return "User(id=" + this.id + ", name=" + this.name + ", password=" + + this.password + ", veganMeal=" + this.veganMeal + ")"; + } + @Override public boolean equals(Object other) { - if(other instanceof User){ + 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; + if (that.id == this.id && that.name.equals(this.name) + && that.password.equals(this.password) && that.veganMeal == this.veganMeal) { + return true; + } } return false; } + + @Override + public int hashCode() { + return Objects.hash(id, name, password, veganMeal); + } } 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 cb88d1b..e33d5e4 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,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; @@ -39,6 +40,13 @@ public class ActivityTest { assertTrue(first.equals(second)); } + @Test + public void notEqualsTest() { + Activity first = new Activity(1, "Solar panels", "Installed", 10000); + Activity second = new Activity(2, "Solar panels", "Installed", 10000); + assertFalse(first.equals(second)); + } + @Test public void hashCodeTest() { Activity first = new Activity(1, "Solar panels", "Installed", 10000); 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 59e8ac0..5283d6b 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 @@ -39,6 +39,13 @@ public class UserTest { assertTrue(first.equals(second)); } + @Test + public void notEqualsTest() { + User first = new User(1L, "greenify", "password", 3); + User second = new User(1L, "greenify", "password", 7); + assertFalse(first.equals(second)); + } + @Test public void instanceOfTest() { User first = new User(); @@ -50,8 +57,8 @@ public class UserTest { public void hashCodeTest() { User first = new User(1L, "greenify", "password", 3); User second = new User(1L, "greenify", "password", 3); - assertTrue(first.equals(second) && second.equals(first)); assertEquals(first, second); + assertEquals(first.hashCode(), second.hashCode()); } } From 584e5bf90c898d5dcd62377d240ed4b8eb0554b5 Mon Sep 17 00:00:00 2001 From: cugurlu Date: Mon, 18 Mar 2019 13:06:04 +0100 Subject: [PATCH 3/3] Update GUI --- .../controller/DashBoardController.java | 16 +- .../src/main/resources/fxml/dashboard.fxml | 139 +++++++++++++++--- 2 files changed, 130 insertions(+), 25 deletions(-) diff --git a/src/Client/src/main/java/greenify/client/controller/DashBoardController.java b/src/Client/src/main/java/greenify/client/controller/DashBoardController.java index a69a392..42db72f 100644 --- a/src/Client/src/main/java/greenify/client/controller/DashBoardController.java +++ b/src/Client/src/main/java/greenify/client/controller/DashBoardController.java @@ -3,7 +3,7 @@ package greenify.client.controller; import greenify.client.rest.UserService; import javafx.event.ActionEvent; import javafx.fxml.FXML; -import javafx.scene.control.TextField; +import javafx.scene.control.Label; import javafx.scene.layout.AnchorPane; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -25,10 +25,10 @@ public class DashBoardController { private AnchorPane activitiesPane; @FXML - private TextField counter; + private Label veganMealCounter; @FXML - private TextField totalCount; + private Label totalVeganMealCounter; /** * displays the dashboard pane. @@ -46,7 +46,7 @@ public class DashBoardController { * @param event the event (clicking the button) */ public void displayActivities(ActionEvent event) { - totalCount.setText("Total Count: " + userService.currentUser.getVeganMeal()); + totalVeganMealCounter.setText("" + userService.currentUser.getVeganMeal()); System.out.println("display activities"); dashboardPane.setVisible(false); userPane.setVisible(false); @@ -71,11 +71,11 @@ public class DashBoardController { public void addVeganMeal(ActionEvent event) { count++; int net = userService.currentUser.getVeganMeal() + count; - totalCount.setText("Total Count: " + net); - counter.setText("Count: " + count); + totalVeganMealCounter.setText("" + net); + veganMealCounter.setText("" + count); System.out.println(userService); userService.addVeganMeal(userService.currentUser.getId(), - userService.currentUser.getName()); + userService.currentUser.getName()); System.out.println("Vegetarian meal is added"); } -} +} \ No newline at end of file diff --git a/src/Client/src/main/resources/fxml/dashboard.fxml b/src/Client/src/main/resources/fxml/dashboard.fxml index 42ae999..c463ad5 100644 --- a/src/Client/src/main/resources/fxml/dashboard.fxml +++ b/src/Client/src/main/resources/fxml/dashboard.fxml @@ -2,11 +2,11 @@ - + @@ -14,24 +14,9 @@ - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +