From ec37bdcd79365b64f4da0fd98a382f3cb1aaa461 Mon Sep 17 00:00:00 2001 From: Mika Wauben Date: Tue, 26 Mar 2019 07:05:10 +0000 Subject: [PATCH 01/11] Update build.gradle --- build.gradle | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index 2b6ed72..98f5661 100644 --- a/build.gradle +++ b/build.gradle @@ -18,9 +18,9 @@ apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' apply plugin: 'application' -application { - mainClassName = 'greenify.server.Application' -} +//application { +// mainClassName = 'greenify.server.Application' +//} repositories { mavenCentral() From f28a4ea9242ca9c3eef72bef5a638795404f9c46 Mon Sep 17 00:00:00 2001 From: mlwauben Date: Tue, 26 Mar 2019 08:25:09 +0100 Subject: [PATCH 02/11] FIX:: application classes server+client --- .../java/greenify/client/Application.java | 26 +++++++++++++++---- .../java/greenify/server/Application.java | 4 +++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/Client/src/main/java/greenify/client/Application.java b/src/Client/src/main/java/greenify/client/Application.java index 29afbe3..40c41f3 100644 --- a/src/Client/src/main/java/greenify/client/Application.java +++ b/src/Client/src/main/java/greenify/client/Application.java @@ -4,19 +4,24 @@ import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +//import org.slf4j.Logger; +//import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import java.io.IOException; + @SpringBootApplication public class Application extends javafx.application.Application { private static ConfigurableApplicationContext springContext; - private static final Logger log = LoggerFactory.getLogger(Application.class); +// private static final Logger log = LoggerFactory.getLogger(Application.class); + /** + * This (main) method starts launch. + * @param args the arguments + */ public static void main(String[] args) { launch(args); } @@ -33,11 +38,19 @@ public class Application extends javafx.application.Application { return loader.load(); } + /** + * This method initializes the application. + */ @Override - public void init() throws Exception { + public void init() { springContext = SpringApplication.run(Application.class); } + /** + * This method opens the login window. + * @param primaryStage the login window + * @throws Exception in case fxml file is not found + */ @Override public void start(Stage primaryStage) throws Exception { Parent rootNode = load(this.getClass().getClassLoader().getResource("fxml/LoginWindow.fxml")); @@ -47,8 +60,11 @@ public class Application extends javafx.application.Application { primaryStage.show(); } + /** + * This method stops the application. + */ @Override public void stop() { springContext.stop(); } -} \ No newline at end of file +} diff --git a/src/Server/src/main/java/greenify/server/Application.java b/src/Server/src/main/java/greenify/server/Application.java index 8ef14a2..fff0f2b 100644 --- a/src/Server/src/main/java/greenify/server/Application.java +++ b/src/Server/src/main/java/greenify/server/Application.java @@ -6,6 +6,10 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { + /** + * This method starts the server. + * @param args the arguments + */ public static void main(String[] args) { SpringApplication.run(Application.class, args); } From 4200ae587dbdaaf3f9b2e213dda0adac95c89eaf Mon Sep 17 00:00:00 2001 From: mlwauben Date: Tue, 26 Mar 2019 08:26:40 +0100 Subject: [PATCH 03/11] FIX:: CI --- build.gradle | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index 98f5661..2b6ed72 100644 --- a/build.gradle +++ b/build.gradle @@ -18,9 +18,9 @@ apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' apply plugin: 'application' -//application { -// mainClassName = 'greenify.server.Application' -//} +application { + mainClassName = 'greenify.server.Application' +} repositories { mavenCentral() From 0ae5e7f4ee5beed047500bea4b2fec050be617c3 Mon Sep 17 00:00:00 2001 From: mlwauben Date: Tue, 26 Mar 2019 08:46:54 +0100 Subject: [PATCH 04/11] FIX:: checkstyle 'common' --- .../greenify/common/ApplicationException.java | 4 +++ .../java/greenify/common/ErrorResponse.java | 19 ++++++++++-- .../main/java/greenify/common/UserDto.java | 30 +++++++++++++++---- .../src/test/java/ErrorResponseTest.java | 10 +++---- src/Common/src/test/java/UserDtoTest.java | 8 ++--- 5 files changed, 54 insertions(+), 17 deletions(-) diff --git a/src/Common/src/main/java/greenify/common/ApplicationException.java b/src/Common/src/main/java/greenify/common/ApplicationException.java index b9321ed..f64b9fd 100644 --- a/src/Common/src/main/java/greenify/common/ApplicationException.java +++ b/src/Common/src/main/java/greenify/common/ApplicationException.java @@ -1,6 +1,10 @@ package greenify.common; public class ApplicationException extends RuntimeException { + /** + * This method returns an application exception message. + * @param message the exception message + */ public ApplicationException(String message) { super(message); } diff --git a/src/Common/src/main/java/greenify/common/ErrorResponse.java b/src/Common/src/main/java/greenify/common/ErrorResponse.java index 4acb747..df449c6 100644 --- a/src/Common/src/main/java/greenify/common/ErrorResponse.java +++ b/src/Common/src/main/java/greenify/common/ErrorResponse.java @@ -1,19 +1,34 @@ package greenify.common; public class ErrorResponse { - String message; + private String message; + /** + * The method creates a new error response with a message. + * @param message the message you want to response with + */ public ErrorResponse(String message) { this.message = message; } + /** + * This method creates an error response without a message. + */ public ErrorResponse() { } + /** + * This method returns the message. + * @return the message + */ public String getMessage() { return message; } + /** + * This method sets a message. + * @param message the message you want set + */ public void setMessage(String message) { this.message = message; } -} \ No newline at end of file +} diff --git a/src/Common/src/main/java/greenify/common/UserDto.java b/src/Common/src/main/java/greenify/common/UserDto.java index f3bdf29..2b83014 100644 --- a/src/Common/src/main/java/greenify/common/UserDto.java +++ b/src/Common/src/main/java/greenify/common/UserDto.java @@ -1,9 +1,13 @@ +/* + DTO stands for Data Transfer Object. + is an object that carries data between processes. + The motivation for its use is that communication between processes + is usually done + resorting to remote interfaces (e.g., web services), + where each call is an expensive operation. + */ package greenify.common; -// DTO stands for Data Transfer Object. -// is an object that carries data between processes. -// The motivation for its use is that communication between processes is usually done -// resorting to remote interfaces (e.g., web services), where each call is an expensive operation. public class UserDto { private Long id; private String name; @@ -21,19 +25,35 @@ public class UserDto { this.name = name; } + /** + * Method that returns the name of a user. + * @return name + */ public String getName() { return name; } + /** + * Method that sets the name of a user. + * @param name name of a user + */ public void setName(String name) { this.name = name; } + /** + * Method that returns the ID of a user. + * @return id + */ public Long getId() { return id; } + /** + * Method that sets the ID of a user. + * @param id id of a user + */ public void setId(Long id) { this.id = id; } -} \ No newline at end of file +} diff --git a/src/Common/src/test/java/ErrorResponseTest.java b/src/Common/src/test/java/ErrorResponseTest.java index 350e71f..05892e1 100644 --- a/src/Common/src/test/java/ErrorResponseTest.java +++ b/src/Common/src/test/java/ErrorResponseTest.java @@ -1,17 +1,15 @@ import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - import greenify.common.ErrorResponse; - import org.junit.Test; public class ErrorResponseTest { + @Test public void setAndGetTest() { ErrorResponse response = new ErrorResponse("New error"); ErrorResponse testResponse = new ErrorResponse(); testResponse.setMessage("New error"); - assertTrue(response.getMessage().equals("New error")); + assertEquals("New error", response.getMessage()); } @Test @@ -19,6 +17,6 @@ public class ErrorResponseTest { ErrorResponse first = new ErrorResponse("New error"); ErrorResponse second = new ErrorResponse("New error"); assertEquals(first.getMessage(), second.getMessage()); - assertTrue(first.getMessage().equals(second.getMessage())); } -} \ 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 a1892bd..565ca0f 100644 --- a/src/Common/src/test/java/UserDtoTest.java +++ b/src/Common/src/test/java/UserDtoTest.java @@ -1,17 +1,16 @@ import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - import greenify.common.UserDto; import org.junit.Test; public class UserDtoTest { + @Test public void setAndGetTest() { UserDto testUser = new UserDto(); testUser.setId(1L); testUser.setName("greenify"); UserDto user = new UserDto(1L, "greenify"); - assertTrue(user.getId() == 1L); + assertEquals(1L, (long) user.getId()); assertEquals(user.getName(), "greenify"); } @@ -22,4 +21,5 @@ public class UserDtoTest { assertEquals(first.getId(), second.getId()); assertEquals(first.getName(), second.getName()); } -} \ No newline at end of file + +} From 72cb770861e012343db9fea50f949639b12301e1 Mon Sep 17 00:00:00 2001 From: mlwauben Date: Tue, 26 Mar 2019 09:49:27 +0100 Subject: [PATCH 05/11] FIX:: checkstyle 'server', things with user --- .../java/greenify/server/data/model/User.java | 84 +++++++++++++------ .../data/repository/UserRepository.java | 11 ++- .../greenify/server/rest/UserController.java | 21 ++++- .../greenify/server/service/UserService.java | 48 ++++++----- .../greenify/server/data/model/UserTest.java | 30 ++++--- .../data/repository/UserRepositoryTest.java | 4 +- .../server/rest/UserControllerTest.java | 5 +- .../server/service/UserServiceTest.java | 35 +++----- 8 files changed, 141 insertions(+), 97 deletions(-) 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)); } + } From d8ced45a675c4af13c16df531e0364a78349d835 Mon Sep 17 00:00:00 2001 From: mlwauben Date: Tue, 26 Mar 2019 09:59:00 +0100 Subject: [PATCH 06/11] FIX:: checkstyle server user tests --- .../src/test/java/greenify/server/data/model/UserTest.java | 4 +++- .../greenify/server/data/repository/UserRepositoryTest.java | 2 ++ .../test/java/greenify/server/rest/UserControllerTest.java | 1 + .../test/java/greenify/server/service/UserServiceTest.java | 6 ++++-- 4 files changed, 10 insertions(+), 3 deletions(-) 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 be8eb81..0be519d 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,7 +1,9 @@ package greenify.server.data.model; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + import org.junit.Test; -import static org.junit.Assert.*; public class UserTest { 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 fcdb09e..1128a95 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,9 @@ 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; 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 553c833..d0784ba 100644 --- a/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java +++ b/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java @@ -57,5 +57,6 @@ public class UserControllerTest { @Test 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 7376cd7..7994f72 100644 --- a/src/Server/src/test/java/greenify/server/service/UserServiceTest.java +++ b/src/Server/src/test/java/greenify/server/service/UserServiceTest.java @@ -68,7 +68,8 @@ public class UserServiceTest { @Test public void registerExceptionTest() { - assertThrows(ApplicationException.class, () -> userService.registerUser("alex", "password")); + assertThrows(ApplicationException.class, () -> + userService.registerUser("alex", "password")); } @Test @@ -92,7 +93,8 @@ public class UserServiceTest { @Test public void setInputApplicationTestValue() { - assertThrows(ApplicationException.class, () -> userService.setInput("tom", "transportation_num_vehicles", "5.5")); + assertThrows(ApplicationException.class, () -> + userService.setInput("tom", "transportation_num_vehicles", "5.5")); } From 9e345ee353ba2e11983c1e29591575a434933d37 Mon Sep 17 00:00:00 2001 From: mlwauben Date: Tue, 26 Mar 2019 10:55:12 +0100 Subject: [PATCH 07/11] FIX:: checkstyle fixes --- .../greenify/common/ApplicationException.java | 2 +- .../java/greenify/common/ErrorResponse.java | 2 +- .../main/java/greenify/common/UserDto.java | 1 + .../src/test/java/ErrorResponseTest.java | 1 + src/Common/src/test/java/UserDtoTest.java | 1 + .../java/greenify/server/InputValidator.java | 69 ++++++++++--------- .../java/greenify/server/data/model/User.java | 14 ++-- .../server/rest/RestExceptionHandler.java | 8 ++- .../greenify/server/rest/UserController.java | 2 +- .../server/service/CalculatorService.java | 25 +++++-- .../greenify/server/service/UserService.java | 12 ++-- src/Server/src/test/java/ApplicationTest.java | 2 +- .../src/test/java/InputValidatorTest.java | 23 ++++--- .../server/rest/UserControllerTest.java | 8 +-- .../server/service/CalculatorServiceTest.java | 3 +- 15 files changed, 96 insertions(+), 77 deletions(-) diff --git a/src/Common/src/main/java/greenify/common/ApplicationException.java b/src/Common/src/main/java/greenify/common/ApplicationException.java index f64b9fd..d748ea6 100644 --- a/src/Common/src/main/java/greenify/common/ApplicationException.java +++ b/src/Common/src/main/java/greenify/common/ApplicationException.java @@ -2,7 +2,7 @@ package greenify.common; public class ApplicationException extends RuntimeException { /** - * This method returns an application exception message. + * This method sends an application exception message. * @param message the exception message */ public ApplicationException(String message) { diff --git a/src/Common/src/main/java/greenify/common/ErrorResponse.java b/src/Common/src/main/java/greenify/common/ErrorResponse.java index df449c6..72ab1b9 100644 --- a/src/Common/src/main/java/greenify/common/ErrorResponse.java +++ b/src/Common/src/main/java/greenify/common/ErrorResponse.java @@ -17,7 +17,7 @@ public class ErrorResponse { public ErrorResponse() { } /** - * This method returns the message. + * This method gets the message. * @return the message */ public String getMessage() { diff --git a/src/Common/src/main/java/greenify/common/UserDto.java b/src/Common/src/main/java/greenify/common/UserDto.java index 2b83014..d2b9d4f 100644 --- a/src/Common/src/main/java/greenify/common/UserDto.java +++ b/src/Common/src/main/java/greenify/common/UserDto.java @@ -6,6 +6,7 @@ resorting to remote interfaces (e.g., web services), where each call is an expensive operation. */ + package greenify.common; public class UserDto { diff --git a/src/Common/src/test/java/ErrorResponseTest.java b/src/Common/src/test/java/ErrorResponseTest.java index 05892e1..9cce02b 100644 --- a/src/Common/src/test/java/ErrorResponseTest.java +++ b/src/Common/src/test/java/ErrorResponseTest.java @@ -1,4 +1,5 @@ import static org.junit.Assert.assertEquals; + import greenify.common.ErrorResponse; import org.junit.Test; diff --git a/src/Common/src/test/java/UserDtoTest.java b/src/Common/src/test/java/UserDtoTest.java index 565ca0f..90514ae 100644 --- a/src/Common/src/test/java/UserDtoTest.java +++ b/src/Common/src/test/java/UserDtoTest.java @@ -1,4 +1,5 @@ import static org.junit.Assert.assertEquals; + import greenify.common.UserDto; import org.junit.Test; diff --git a/src/Server/src/main/java/greenify/server/InputValidator.java b/src/Server/src/main/java/greenify/server/InputValidator.java index a17b575..9b3f0dc 100644 --- a/src/Server/src/main/java/greenify/server/InputValidator.java +++ b/src/Server/src/main/java/greenify/server/InputValidator.java @@ -4,68 +4,69 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; public class InputValidator { private static final List inputItems = Arrays.asList( new InputItem("input_size", false, "1"), new InputItem("input_income", false, "40000"), - new InputItem("transportation_num_vehicles", false, "1" ), + new InputItem("transportation_num_vehicles", false, "1"), new InputItem("transportation_miles1", false, "16100", false), - new InputItem("transportation_fuels1", false, "2" , false), - new InputItem("transportation_mpg1", false, null, false ), - new InputItem("transportation_miles2", false, "13200", false ), - new InputItem("transportation_fuels2", false, "0" , false), - new InputItem("transportation_mpg2", false, "22" ,false), - new InputItem("transportation_publicTrans", false, "436" ), - new InputItem("transportation_air", false, "3900" ), - new InputItem("housing_electricity_kwh_year", false, "12632" ), - new InputItem("housing_cleanPercent", false, "0" ), - new InputItem("housing_naturalGas_therms_year", false, "472" ), - new InputItem("housing_heatingOil_gallons_year", false, "73" ), - new InputItem("housing_square_feet", false, "1850" ), - new InputItem("housing_water_sewage", false, "100" ), - new InputItem("food_meat_fish_eggs", true, "2.4" ), - new InputItem("food_grains", true, "4.1" ), - new InputItem("food_dairy", true, "2.2" ), - new InputItem("food_fruit_vegetables", true, "3.5" ), - new InputItem("food_snacks_drinks", true, "3.4" ), - new InputItem("shopping_goods", false, "1310" ), - new InputItem("shopping_services", false, "2413" ) + new InputItem("transportation_fuels1", false, "2", false), + new InputItem("transportation_mpg1", false, null, false), + new InputItem("transportation_miles2", false, "13200", false), + new InputItem("transportation_fuels2", false, "0", false), + new InputItem("transportation_mpg2", false, "22", false), + new InputItem("transportation_publicTrans", false, "436"), + new InputItem("transportation_air", false, "3900"), + new InputItem("housing_electricity_kwh_year", false, "12632"), + new InputItem("housing_cleanPercent", false, "0"), + new InputItem("housing_naturalGas_therms_year", false, "472"), + new InputItem("housing_heatingOil_gallons_year", false, "73"), + new InputItem("housing_square_feet", false, "1850"), + new InputItem("housing_water_sewage", false, "100"), + new InputItem("food_meat_fish_eggs", true, "2.4"), + new InputItem("food_grains", true, "4.1"), + new InputItem("food_dairy", true, "2.2"), + new InputItem("food_fruit_vegetables", true, "3.5"), + new InputItem("food_snacks_drinks", true, "3.4"), + new InputItem("shopping_goods", false, "1310"), + new InputItem("shopping_services", false, "2413") ); /** - * The method checks that the id is valid or not. + * The method checks whether the id is valid or not. * @param inputName the name of input * @return true or false */ public static Boolean isValidItem(String inputName) { - return inputItems.stream().filter(i -> i.getName() == inputName).findAny().isPresent(); + return inputItems.stream().anyMatch(i -> i.getName().equals(inputName)); } /** - * The method checks that the item value is valid or not. + * The method checks whether the item value is valid or not. * @param inputName the name of input * @param value the value of item * @return true or false */ public static boolean isValidItemValue(String inputName, String value) { InputItem item = null; - for (int i = 0; i < inputItems.size(); i++) { - if (inputItems.get(i).getName() == inputName) { - item = inputItems.get(i); + for (InputItem inputItem : inputItems) { + if (inputItem.getName().equals(inputName)) { + item = inputItem; } } - if (item.getFloat()) { + if (Objects.requireNonNull(item).getFloat()) { try { - Float number = Float.parseFloat(value); + Float.parseFloat(value); } catch (NumberFormatException | NullPointerException nfe) { return false; } return true; } else { try { - Integer number = Integer.parseInt(value); + Integer.parseInt(value); } catch (NumberFormatException | NullPointerException nfe) { return false; } @@ -74,13 +75,13 @@ public class InputValidator { } /** - * getter for default values. + * This method gets default values. * @return the map of default values */ public static Map getDefaultValues() { - Map map = new HashMap(){}; - for (int i = 0; i < inputItems.size(); i++) { - map.put(inputItems.get(i).getName(), inputItems.get(i).getDefaultValue()); + Map map = new HashMap() { }; + for (InputItem inputItem : inputItems) { + map.put(inputItem.getName(), inputItem.getDefaultValue()); } return map; } 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 f66c691..0fb5ccf 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 @@ -52,7 +52,7 @@ public class User { } /** - * This method returns the ID of the user. + * This method gets the ID of the user. * @return the id of the user */ public Long getId() { @@ -68,7 +68,7 @@ public class User { } /** - * This method returns the name of the user. + * This method gets the name of the user. * @return the name of the user */ public String getName() { @@ -84,7 +84,7 @@ public class User { } /** - * This method returns the password of the user. + * This method gets the password of the user. * @return the password of the user */ public String getPassword() { @@ -100,7 +100,7 @@ public class User { } /** - * This method returns the footPrint of user. + * This method gets the footPrint of user. * @return the footprint of the user */ public Float getFootPrint() { @@ -116,7 +116,7 @@ public class User { } /** - * This method returns the footprint inputs of the user. + * This method gets the footprint inputs of the user. * @return footprint inputs of the user */ public Map getFootPrintInputs() { @@ -132,7 +132,7 @@ public class User { } /** - * This method returns a human readable (JSON) object. + * This method gets a human readable (JSON) object. * @return the JSON form of the object. */ @Override @@ -157,7 +157,7 @@ public class User { } /** - * This method returns the hashcode of a user. + * This method gets the hashcode of a user. * @return hashcode of a user */ @Override 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 d4657cc..ff5d444 100644 --- a/src/Server/src/main/java/greenify/server/rest/RestExceptionHandler.java +++ b/src/Server/src/main/java/greenify/server/rest/RestExceptionHandler.java @@ -7,12 +7,14 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; -//class that handles exceptions for the rest server +/** + * This class handles exceptions for the REST server. + */ @RestControllerAdvice -public class RestExceptionHandler { +class RestExceptionHandler { @ExceptionHandler(ApplicationException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) - public static ErrorResponse applicationException(ApplicationException ex) { + 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 05ed9db..2d7d3c7 100644 --- a/src/Server/src/main/java/greenify/server/rest/UserController.java +++ b/src/Server/src/main/java/greenify/server/rest/UserController.java @@ -11,7 +11,7 @@ import org.springframework.web.bind.annotation.RestController; public class UserController { @Autowired private - UserService userService; + UserService userService; /** * This method registers the user. diff --git a/src/Server/src/main/java/greenify/server/service/CalculatorService.java b/src/Server/src/main/java/greenify/server/service/CalculatorService.java index 7ad64b4..dbf9a65 100644 --- a/src/Server/src/main/java/greenify/server/service/CalculatorService.java +++ b/src/Server/src/main/java/greenify/server/service/CalculatorService.java @@ -16,21 +16,27 @@ import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; import java.util.Map; +import java.util.Objects; @Service -public class CalculatorService { - Logger logger = LoggerFactory.getLogger(UserService.class); - +class CalculatorService { @Autowired RestTemplate restTemplate; + private Logger logger = LoggerFactory.getLogger(UserService.class); + @Bean RestTemplate restTemplate(RestTemplateBuilder builder) { return builder.build(); } - protected Float invokeExternalService(Map map) { - /** + /** + * This method invokes the external service that calculates a footprint. + * @param map used variables to calculate a footprint + * @return a footprint + */ + Float invokeExternalService(Map map) { + /* * curl -X GET "https://apis.berkeley.edu/coolclimate/footprint-sandbox?input_location_mode=1 * &input_location=48001&input_income=1&input_size=0&input_footprint_transportation_miles1=3 * &input_footprint_transportation_mpg1=5&input_footprint_transportation_fuel1=0" @@ -52,7 +58,7 @@ public class CalculatorService { entity, String.class); logger.info(response.getStatusCode().toString()); logger.info(response.getBody()); - String result = response.getBody().substring(response.getBody() + String result = response.getBody().substring(Objects.requireNonNull(response.getBody()) .indexOf("") + 20, response.getBody().indexOf("")); // to do: in not HTTP 200 or exception case throws exception @@ -60,7 +66,12 @@ public class CalculatorService { return Float.parseFloat(result); } - public Float calculateFootprint(User user) { + /** + * The method calculates a users footprint. + * @param user the user + * @return the footprint of the user + */ + Float calculateFootprint(User user) { return invokeExternalService(user.getFootPrintInputs()); } } 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 66e1bde..9eb28d4 100644 --- a/src/Server/src/main/java/greenify/server/service/UserService.java +++ b/src/Server/src/main/java/greenify/server/service/UserService.java @@ -14,13 +14,13 @@ import org.springframework.web.bind.annotation.ResponseBody; @Service public class UserService { - private Logger logger = LoggerFactory.getLogger(UserService.class); + @Autowired + CalculatorService calculatorService; @Autowired UserRepository userRepository; - @Autowired - CalculatorService calculatorService; + private Logger logger = LoggerFactory.getLogger(UserService.class); /** * This method registers the user. @@ -81,7 +81,7 @@ public class UserService { } /** - * This method returns the input value of an input. + * This method gets the input value of an input. * @param name of the user * @param inputName name of the input * @return input value @@ -96,7 +96,7 @@ public class UserService { } /** - * This method returns the footprint of a user. + * This method gets the footprint of a user. * @param name name of the user * @return footprint of the user */ @@ -106,7 +106,7 @@ public class UserService { } /** - * This method returns a JSON of XML with all users. + * This method gets a JSON of XML with all users. * @return JSON/XML of all users */ @GetMapping(path = "/all") diff --git a/src/Server/src/test/java/ApplicationTest.java b/src/Server/src/test/java/ApplicationTest.java index 3e39948..caba9de 100644 --- a/src/Server/src/test/java/ApplicationTest.java +++ b/src/Server/src/test/java/ApplicationTest.java @@ -8,6 +8,6 @@ import org.springframework.test.context.junit4.SpringRunner; public class ApplicationTest { @Test - public void contextLoads() throws Exception{ } + public void contextLoads() { } } diff --git a/src/Server/src/test/java/InputValidatorTest.java b/src/Server/src/test/java/InputValidatorTest.java index e55018d..d80794a 100644 --- a/src/Server/src/test/java/InputValidatorTest.java +++ b/src/Server/src/test/java/InputValidatorTest.java @@ -1,5 +1,8 @@ +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import greenify.server.InputValidator; -import org.junit.Assert; import org.junit.Test; import java.util.HashMap; @@ -9,20 +12,20 @@ public class InputValidatorTest { @Test public void validItemIdTest() { - InputValidator inputValidator = new InputValidator(); - Assert.assertEquals(inputValidator.isValidItem("transportation_num_vehicles"), true); - Assert.assertEquals(inputValidator.isValidItem("test"), false); + new InputValidator(); + assertEquals(InputValidator.isValidItem("transportation_num_vehicles"), true); + assertEquals(InputValidator.isValidItem("test"), false); } @Test public void validItemValueTest() { - Assert.assertEquals(true, InputValidator + assertTrue(InputValidator .isValidItemValue("transportation_num_vehicles", "4")); - Assert.assertEquals(false, InputValidator + assertFalse(InputValidator .isValidItemValue("transportation_num_vehicles", "3.5")); - Assert.assertEquals(false, InputValidator.isValidItemValue( "food_grains", "hello")); - Assert.assertEquals(true, InputValidator.isValidItemValue("food_grains", "5")); - Assert.assertEquals(true, InputValidator.isValidItemValue("food_grains", "3.5")); + assertFalse(InputValidator.isValidItemValue("food_grains", "hello")); + assertTrue(InputValidator.isValidItemValue("food_grains", "5")); + assertTrue(InputValidator.isValidItemValue("food_grains", "3.5")); } @Test @@ -54,6 +57,6 @@ public class InputValidatorTest { put("shopping_services", "2413"); } }; - Assert.assertEquals(InputValidator.getDefaultValues(), map); + assertEquals(InputValidator.getDefaultValues(), map); } } 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 d0784ba..b139f95 100644 --- a/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java +++ b/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java @@ -55,8 +55,8 @@ public class UserControllerTest { .andExpect(status().isOk()).andExpect(content().json("{'id':1,'name':'ceren'}")); } - @Test - public void setInputTest() { - - } + //@Test + //public void setInputTest() { + // + //} } diff --git a/src/Server/src/test/java/greenify/server/service/CalculatorServiceTest.java b/src/Server/src/test/java/greenify/server/service/CalculatorServiceTest.java index 9c9926f..47fd3fb 100644 --- a/src/Server/src/test/java/greenify/server/service/CalculatorServiceTest.java +++ b/src/Server/src/test/java/greenify/server/service/CalculatorServiceTest.java @@ -37,8 +37,7 @@ public class CalculatorServiceTest { @Bean public CalculatorService calculatorService() { - CalculatorService calculatorService = new CalculatorService(); - return calculatorService; + return new CalculatorService(); } @Bean From bd21e30f26529347c6029625fc8f3b44b287217b Mon Sep 17 00:00:00 2001 From: mlwauben Date: Tue, 26 Mar 2019 11:00:24 +0100 Subject: [PATCH 08/11] FIX:: checkstyle 'client' tests --- src/Client/src/test/java/ApplicationTest.java | 2 +- src/Client/src/test/java/UserServiceTest.java | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Client/src/test/java/ApplicationTest.java b/src/Client/src/test/java/ApplicationTest.java index 4e28ace..01b420b 100644 --- a/src/Client/src/test/java/ApplicationTest.java +++ b/src/Client/src/test/java/ApplicationTest.java @@ -8,5 +8,5 @@ import org.springframework.test.context.junit4.SpringRunner; public class ApplicationTest { @Test - public void contextLoads() throws Exception{ } + public void contextLoads() { } } diff --git a/src/Client/src/test/java/UserServiceTest.java b/src/Client/src/test/java/UserServiceTest.java index 1fd77b4..4ac4044 100644 --- a/src/Client/src/test/java/UserServiceTest.java +++ b/src/Client/src/test/java/UserServiceTest.java @@ -8,13 +8,13 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.Spy; import org.mockito.junit.MockitoJUnitRunner; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +//import org.slf4j.Logger; +//import org.slf4j.LoggerFactory; import org.springframework.web.client.RestTemplate; @RunWith(MockitoJUnitRunner.class) public class UserServiceTest { - private static Logger logger = LoggerFactory.getLogger(UserServiceTest.class); + // private static Logger logger = LoggerFactory.getLogger(UserServiceTest.class); @Mock RestTemplate restTemplate; From 8677ac94efa23e6115f6d78520545c6a334ce420 Mon Sep 17 00:00:00 2001 From: mlwauben Date: Tue, 26 Mar 2019 11:34:53 +0100 Subject: [PATCH 09/11] FIX:: checkstyle 'client' --- .../java/greenify/client/Application.java | 5 +- .../controller/DashBoardController.java | 52 +++++++++---------- .../controller/RegisterWindowController.java | 31 +++++++---- .../client/controller/UserController.java | 31 ++++++----- .../greenify/client/rest/UserService.java | 28 +++++----- .../src/main/resources/fxml/LoginWindow.fxml | 2 +- .../main/resources/fxml/RegisterWindow.fxml | 2 +- .../src/main/resources/fxml/dashboard.fxml | 2 +- 8 files changed, 88 insertions(+), 65 deletions(-) diff --git a/src/Client/src/main/java/greenify/client/Application.java b/src/Client/src/main/java/greenify/client/Application.java index 40c41f3..127b49f 100644 --- a/src/Client/src/main/java/greenify/client/Application.java +++ b/src/Client/src/main/java/greenify/client/Application.java @@ -16,7 +16,7 @@ import java.io.IOException; @SpringBootApplication public class Application extends javafx.application.Application { private static ConfigurableApplicationContext springContext; -// private static final Logger log = LoggerFactory.getLogger(Application.class); + //private static final Logger log = LoggerFactory.getLogger(Application.class); /** * This (main) method starts launch. @@ -53,7 +53,8 @@ public class Application extends javafx.application.Application { */ @Override public void start(Stage primaryStage) throws Exception { - Parent rootNode = load(this.getClass().getClassLoader().getResource("fxml/LoginWindow.fxml")); + Parent rootNode = load(this.getClass().getClassLoader() + .getResource("fxml/LoginWindow.fxml")); primaryStage.setTitle("Greenify"); Scene scene = new Scene(rootNode); primaryStage.setScene(scene); 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 81e148a..09470d2 100644 --- a/src/Client/src/main/java/greenify/client/controller/DashBoardController.java +++ b/src/Client/src/main/java/greenify/client/controller/DashBoardController.java @@ -16,18 +16,14 @@ import javafx.util.Duration; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; -//Class that controls the dashboard fxml file (the GUI Screen) - - +/** + * Class that controls the dashboard fxml file (the GUI Screen). + */ @Controller public class DashBoardController { @Autowired UserService userService; - private FadeTransition fadeTrans; //transition for switching between the different panels - private int net; - private int count = 0; - @FXML private AnchorPane dashboardPane; @FXML @@ -36,12 +32,12 @@ public class DashBoardController { private AnchorPane activitiesPane; @FXML private AnchorPane friendsPane; - @FXML - private Label veganMealCounter; + //@FXML + //private Label veganMealCounter; @FXML private Label totalVeganMealCounter; @FXML - private Label welcomebacktext; + private Label welcomeBackText; @FXML private Button dashboardButton; @FXML @@ -54,17 +50,17 @@ public class DashBoardController { private Line pathLine; @FXML private AnchorPane menuBar; - @FXML - private Button addNewActivityButton; + //@FXML + //private Button addNewActivityButton; /** - * loads the the necessary things before anything else. + * Loads the the necessary things before anything else. */ public void initialize() { //sets the text of the 'welcome back' text to include the username - welcomebacktext.setText("Welcome back, " + userService.currentUser.getName() + "!"); + welcomeBackText.setText("Welcome back, " + userService.currentUser.getName() + "!"); //adds the slide transition to the menu bar addSlideTransition(menuBar, pathLine); //adds animations to the navigation buttons @@ -75,20 +71,22 @@ public class DashBoardController { } /** - * adds a fade transition for switching between the different panes. + * Adds a fade transition for switching between the different panes. * @param node the node on which the transition needs to act */ - public void addFadeTransition(Node node) { + private void addFadeTransition(Node node) { - fadeTrans = new FadeTransition(Duration.millis(400), node); + // FadeTransition is now a local variable + // net is now a local variable + //transition for switching between the different panels + FadeTransition fadeTrans = new FadeTransition(Duration.millis(400), node); fadeTrans.setFromValue(0); fadeTrans.setToValue(1.0); fadeTrans.play(); } - /** - * displays the dashboard pane. + * Displays the dashboard pane. * @param event the event (clicking the button) */ public void displayDashboard(ActionEvent event) { @@ -98,15 +96,15 @@ public class DashBoardController { userPane.setVisible(false); activitiesPane.setVisible(false); friendsPane.setVisible(false); - } /** - * displays the activities pane. + * Displays the activities pane. * @param event the event (clicking the button) */ public void displayActivities(ActionEvent event) { addFadeTransition(activitiesPane); + int net = 0; totalVeganMealCounter.setText("" + net); System.out.println("display activities"); dashboardPane.setVisible(false); @@ -116,7 +114,7 @@ public class DashBoardController { } /** - * displays the user profile pane. + * Displays the user profile pane. * @param event the event (clicking the button) */ public void displayUser(ActionEvent event) { @@ -126,9 +124,12 @@ public class DashBoardController { userPane.setVisible(true); activitiesPane.setVisible(false); friendsPane.setVisible(false); - } + /** + * Displays the friends pane. + * @param event the event (clicking the button) + */ public void displayFriends(ActionEvent event) { addFadeTransition(friendsPane); System.out.println("display friends"); @@ -136,11 +137,10 @@ public class DashBoardController { userPane.setVisible(false); activitiesPane.setVisible(false); friendsPane.setVisible(true); - } //sets the slide in transition for startup - public void addSlideTransition(Node node, Line path1) { + private void addSlideTransition(Node node, Line path1) { PathTransition pathTrans = new PathTransition(Duration.millis(1100), path1, node); pathTrans.play(); } @@ -153,7 +153,7 @@ public class DashBoardController { * and scales down when you stop hovering over it. * @param button the button to add the animation to */ - public MyButtonSkin(Button button) { + MyButtonSkin(Button button) { //inherit the button properties super(button); //transition to scale up on hover diff --git a/src/Client/src/main/java/greenify/client/controller/RegisterWindowController.java b/src/Client/src/main/java/greenify/client/controller/RegisterWindowController.java index c44b668..62ac1ce 100644 --- a/src/Client/src/main/java/greenify/client/controller/RegisterWindowController.java +++ b/src/Client/src/main/java/greenify/client/controller/RegisterWindowController.java @@ -9,7 +9,7 @@ import javafx.scene.control.Alert; import javafx.scene.control.Button; import javafx.scene.control.PasswordField; import javafx.scene.control.TextField; -import javafx.scene.shape.Line; +//import javafx.scene.shape.Line; import javafx.stage.Stage; import javafx.stage.Window; import javafx.util.Duration; @@ -18,7 +18,9 @@ import org.springframework.stereotype.Controller; import java.util.concurrent.TimeUnit; -//class that controls the actions for the register window +/** + * Class that controls the actions for the register window. + */ @Controller public class RegisterWindowController { @@ -32,12 +34,17 @@ public class RegisterWindowController { @FXML private PasswordField passwordField2; @FXML - private Button signupButton; - @FXML - private Line uNamePathLine; + private Button signUpButton; + //@FXML + //private Line uNamePathLine; + /** + * Initializes the animation. + * @throws InterruptedException exception when interrupted + */ public void initialize() throws InterruptedException { - // PathTransition pathTransUName = new PathTransition(Duration.millis(1100), uNamePathLine, userNameText); + // PathTransition pathTransUName = new PathTransition(Duration.millis(1100), + // uNamePathLine, userNameText); // pathTransUName.play(); addSlideAnimation(1100, userNameText, -300); addSlideAnimation(1100, passwordField, 300); @@ -46,7 +53,13 @@ public class RegisterWindowController { } - public void addSlideAnimation(int duration, Node node, int from) { + /** + * Adds the slide animation. + * @param duration the duration + * @param node the node + * @param from from where + */ + private void addSlideAnimation(int duration, Node node, int from) { TranslateTransition slideIn = new TranslateTransition(Duration.millis(duration), node); slideIn.setFromX(from); slideIn.setToX(0); @@ -54,13 +67,13 @@ public class RegisterWindowController { } /** - * signs the user up. + * Signs up the user. * @param event the click of the sign up button */ @FXML public void handleSignUpButton(ActionEvent event) { //set the window to the current window (for displaying the alerts) - Window owner = signupButton.getScene().getWindow(); + Window owner = signUpButton.getScene().getWindow(); //check if the username field is empty if (userNameText.getText().isEmpty()) { //if so, display an alert 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 fc0f097..9c3e238 100644 --- a/src/Client/src/main/java/greenify/client/controller/UserController.java +++ b/src/Client/src/main/java/greenify/client/controller/UserController.java @@ -16,8 +16,11 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import java.io.IOException; +import java.util.Objects; -//class that controls the actions for the login screen +/** + * Class that controls the actions for the login screen. + */ @Controller public class UserController { @Autowired @@ -33,10 +36,10 @@ public class UserController { private Button loginButton; @FXML - private Button signupButton; + private Button signUpButton; /** - * handles when the user clicks on the login button. + * Handles when the user clicks on the login button. * it checks if the username and password fields are filled * and gives alerts if they aren't filled in. * @param event the click of the login button @@ -75,18 +78,18 @@ public class UserController { } /** - * opens the dashboard stage. + * Opens the dashboard stage. * @throws IOException exception if fxml file can't be found * @author sem */ - public void openDashboard() throws IOException { + private void openDashboard() throws IOException { //load the fxml file Parent dash = Application.load(this.getClass().getClassLoader() .getResource("fxml/dashboard.fxml")); Scene scene = new Scene(dash); //add the stylesheet for the CSS - scene.getStylesheets().add(getClass().getClassLoader() - .getResource("stylesheets/dashboardStyle.css").toExternalForm()); + scene.getStylesheets().add(Objects.requireNonNull(getClass().getClassLoader() + .getResource("stylesheets/dashboardStyle.css")).toExternalForm()); Stage appStage = new Stage(); appStage.setScene(scene); //set the title @@ -94,8 +97,10 @@ public class UserController { appStage.show(); } - //class for showing the alerts - public static class AlertHelper { + /** + * Class for showing the alerts. + */ + static class AlertHelper { /** * alerts for the login screen. * @param alertType the type of alert @@ -103,10 +108,10 @@ public class UserController { * @param title the title given to the displayed alert * @param message the message displayed in the alert */ - public static void showAlert(Alert.AlertType alertType, - Window owner, - String title, - String message) { + static void showAlert(Alert.AlertType alertType, + Window owner, + String title, + String message) { Alert alert = new Alert(alertType); alert.setTitle(title); alert.setHeaderText(null); 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 c993e2d..c177e28 100644 --- a/src/Client/src/main/java/greenify/client/rest/UserService.java +++ b/src/Client/src/main/java/greenify/client/rest/UserService.java @@ -8,25 +8,23 @@ 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; @Service public class UserService { + public UserDto currentUser; + @Autowired RestTemplate restTemplate; - public UserDto currentUser; - @Bean RestTemplate restTemplate(RestTemplateBuilder builder) { return builder.build(); } /** - * registers the user. + * Registers the user. * @param name the username of the user * @param password the password of the user * @return a userDTO @@ -37,7 +35,7 @@ public class UserService { public UserDto registerUser(String name, String password) { //headers for http HttpHeaders headers = new HttpHeaders(); - //set the accept header in JSÖN value + //set the accept header in JSON value headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); //connect to the server with the needed url UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/registerUser") @@ -47,7 +45,7 @@ public class UserService { //getting the password from the database //create a http entity to be sent - HttpEntity entity = new HttpEntity<>(headers); + new HttpEntity<>(headers); System.out.println(builder.build().encode().toUri()); //the result to be sent is a userDto @@ -59,7 +57,7 @@ public class UserService { } /** - * sign ins the user. + * Signs in the user. * @param name the username of the user * @param password the password of the user * @return a userDTO @@ -72,7 +70,7 @@ public class UserService { UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/loginUser") .queryParam("name", name) .queryParam("password", password); - HttpEntity entity = new HttpEntity<>(headers); + new HttpEntity<>(headers); System.out.println(builder.build().encode().toUri()); UserDto result = this.restTemplate.getForObject(builder.build() .encode().toUri(), UserDto.class); @@ -80,6 +78,13 @@ public class UserService { return result; } + /** + * Updates the input of the user. + * @param name name of the user + * @param inputName name of the input + * @param value value of the input + * @return returns the result + */ @SuppressWarnings("Duplicates") public String updateInput(String name, String inputName, String value) { HttpHeaders headers = new HttpHeaders(); @@ -88,10 +93,9 @@ public class UserService { .queryParam("name", name) .queryParam("inputName", inputName) .queryParam("value",value); - HttpEntity entity = new HttpEntity<>(headers); + new HttpEntity<>(headers); System.out.println(builder.build().encode().toUri()); - String result = this.restTemplate.getForObject(builder.build() + return this.restTemplate.getForObject(builder.build() .encode().toUri(), String.class); - return result; } } diff --git a/src/Client/src/main/resources/fxml/LoginWindow.fxml b/src/Client/src/main/resources/fxml/LoginWindow.fxml index 03a3902..03b8014 100644 --- a/src/Client/src/main/resources/fxml/LoginWindow.fxml +++ b/src/Client/src/main/resources/fxml/LoginWindow.fxml @@ -17,7 +17,7 @@