diff --git a/doc/reports/checkstyle/20190325_checkstyle.jpg b/doc/reports/checkstyle/20190325_checkstyle.jpg new file mode 100644 index 0000000..54dcac9 Binary files /dev/null and b/doc/reports/checkstyle/20190325_checkstyle.jpg differ diff --git a/doc/reports/coverage/20190325_coverage.png b/doc/reports/coverage/20190325_coverage.png new file mode 100644 index 0000000..60dc619 Binary files /dev/null and b/doc/reports/coverage/20190325_coverage.png differ diff --git a/src/Client/src/main/java/greenify/client/Application.java b/src/Client/src/main/java/greenify/client/Application.java index a8e7d4f..29afbe3 100644 --- a/src/Client/src/main/java/greenify/client/Application.java +++ b/src/Client/src/main/java/greenify/client/Application.java @@ -12,15 +12,11 @@ import org.springframework.context.ConfigurableApplicationContext; import java.io.IOException; -//springbootApplication is so Spring knows that this is a Spring application @SpringBootApplication public class Application extends javafx.application.Application { - //configurable application is for spring so it knows that it can use it private static ConfigurableApplicationContext springContext; - //logger to log all the things that happen to the console private static final Logger log = LoggerFactory.getLogger(Application.class); - //launch is to launch the GUI things public static void main(String[] args) { launch(args); } @@ -29,10 +25,8 @@ public class Application extends javafx.application.Application { * This method takes an url and return a parent. * @param url which is being loaded. * @return parent object. - * @throws IOException if it can't find an FXML file */ public static Parent load(java.net.URL url) throws IOException { - //loader to load the FXML file FXMLLoader loader = new FXMLLoader(); loader.setControllerFactory(springContext::getBean); loader.setLocation(url); @@ -41,22 +35,14 @@ public class Application extends javafx.application.Application { @Override public void init() throws Exception { - //run the application springContext = SpringApplication.run(Application.class); } @Override public void start(Stage primaryStage) throws Exception { - //load the fxml file - Parent rootNode = load(this.getClass().getClassLoader().getResource("fxml/sample.fxml")); - //set the title for the window + Parent rootNode = load(this.getClass().getClassLoader().getResource("fxml/LoginWindow.fxml")); primaryStage.setTitle("Greenify"); - //set the scene Scene scene = new Scene(rootNode); - //add the stylesheet - scene.getStylesheets() - .add(getClass().getClassLoader().getResource("stylesheets/LoginWindowStyle.css") - .toExternalForm()); primaryStage.setScene(scene); primaryStage.show(); } 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 c9429e5..81e148a 100644 --- a/src/Client/src/main/java/greenify/client/controller/DashBoardController.java +++ b/src/Client/src/main/java/greenify/client/controller/DashBoardController.java @@ -72,9 +72,6 @@ public class DashBoardController { activitiesButton.setSkin(new MyButtonSkin(activitiesButton)); userButton.setSkin(new MyButtonSkin(userButton)); friendsButton.setSkin(new MyButtonSkin(friendsButton)); - - - } /** @@ -109,11 +106,7 @@ public class DashBoardController { * @param event the event (clicking the button) */ public void displayActivities(ActionEvent event) { - - addFadeTransition(activitiesPane); - - net = userService.currentUser.getVeganMeal() + count; totalVeganMealCounter.setText("" + net); System.out.println("display activities"); dashboardPane.setVisible(false); @@ -146,23 +139,6 @@ public class DashBoardController { } - - /** - * adds a vegetarian meal. - * @param event the event (clicking the button) - */ - public void addVeganMeal(ActionEvent event) { - - count++; - net = userService.currentUser.getVeganMeal() + count; - totalVeganMealCounter.setText("" + net); - veganMealCounter.setText("" + count); - System.out.println(userService); - userService.addVeganMeal(userService.currentUser.getId(), - userService.currentUser.getName()); - System.out.println("Vegetarian meal is added"); - } - //sets the slide in transition for startup public void addSlideTransition(Node node, Line path1) { PathTransition pathTrans = new PathTransition(Duration.millis(1100), path1, node); @@ -194,4 +170,4 @@ public class DashBoardController { button.setOnMouseExited(e -> scaleDown.playFromStart()); } } -} \ No newline at end of file +} 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 abb387d..fc0f097 100644 --- a/src/Client/src/main/java/greenify/client/controller/UserController.java +++ b/src/Client/src/main/java/greenify/client/controller/UserController.java @@ -82,7 +82,7 @@ public class UserController { public void openDashboard() throws IOException { //load the fxml file Parent dash = Application.load(this.getClass().getClassLoader() - .getResource("fxml/dashboard.fxml")); + .getResource("fxml/dashboard.fxml")); Scene scene = new Scene(dash); //add the stylesheet for the CSS scene.getStylesheets().add(getClass().getClassLoader() 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 ced0ec2..c993e2d 100644 --- a/src/Client/src/main/java/greenify/client/rest/UserService.java +++ b/src/Client/src/main/java/greenify/client/rest/UserService.java @@ -80,27 +80,18 @@ public class UserService { return result; } - /** - * a user adds vegan meal. - * @param id the id of the user - * @param name the username of the user - * @return a userDTO - */ @SuppressWarnings("Duplicates") - public UserDto addVeganMeal(Long id, String name) { - //this method is almost the same as the registerUser one, but with a different link + public String updateInput(String name, String inputName, String value) { HttpHeaders headers = new HttpHeaders(); headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); - UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/addVeganMeal") - .queryParam("id", id) - .queryParam("name", name); + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/setInput") + .queryParam("name", name) + .queryParam("inputName", inputName) + .queryParam("value",value); HttpEntity entity = new HttpEntity<>(headers); 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; + String result = this.restTemplate.getForObject(builder.build() + .encode().toUri(), String.class); + return result; } } diff --git a/src/Client/src/main/resources/fxml/sample.fxml b/src/Client/src/main/resources/fxml/LoginWindow.fxml similarity index 96% rename from src/Client/src/main/resources/fxml/sample.fxml rename to src/Client/src/main/resources/fxml/LoginWindow.fxml index 96027de..03a3902 100644 --- a/src/Client/src/main/resources/fxml/sample.fxml +++ b/src/Client/src/main/resources/fxml/LoginWindow.fxml @@ -1,14 +1,10 @@ - - - - @@ -36,3 +32,4 @@ + diff --git a/src/Client/src/main/resources/fxml/RegisterWindow.fxml b/src/Client/src/main/resources/fxml/RegisterWindow.fxml index 35bff80..9daeb7f 100644 --- a/src/Client/src/main/resources/fxml/RegisterWindow.fxml +++ b/src/Client/src/main/resources/fxml/RegisterWindow.fxml @@ -1,21 +1,14 @@ - - - - - - - - + diff --git a/src/Client/src/main/resources/fxml/dashboard.fxml b/src/Client/src/main/resources/fxml/dashboard.fxml index 2f240b5..9b788d0 100644 --- a/src/Client/src/main/resources/fxml/dashboard.fxml +++ b/src/Client/src/main/resources/fxml/dashboard.fxml @@ -1,22 +1,10 @@ - - - - - - - - - - - - @@ -229,10 +217,10 @@ - - - - + + + + diff --git a/src/Client/src/test/java/UserServiceTest.java b/src/Client/src/test/java/UserServiceTest.java index ac707f5..1fd77b4 100644 --- a/src/Client/src/test/java/UserServiceTest.java +++ b/src/Client/src/test/java/UserServiceTest.java @@ -14,7 +14,6 @@ import org.springframework.web.client.RestTemplate; @RunWith(MockitoJUnitRunner.class) public class UserServiceTest { - //logger that logs everything to the console private static Logger logger = LoggerFactory.getLogger(UserServiceTest.class); @Mock @@ -26,8 +25,7 @@ public class UserServiceTest { @Test public void userRegisterTest() throws Exception { - //tests if registering works - UserDto testUser = new UserDto(1L, "Eric", 0); + UserDto testUser = new UserDto(1L, "Eric"); Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/registerUser?name=Eric&password=password"), UserDto.class)) .thenReturn(testUser); @@ -38,25 +36,13 @@ public class UserServiceTest { @Test public void userLoginTest() throws Exception { - //tests if logging in works - UserDto testUser = new UserDto(1L, "Eric", 0); + UserDto testUser = new UserDto(1L, "Eric"); Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/loginUser?name=Eric&password=password"), UserDto.class)) .thenReturn(testUser); UserDto user = userService.loginUser("Eric", "password"); Assert.assertEquals(testUser, user); } - - @Test - public void addVeganMealTest() throws Exception { - //tests if adding a vegetarian meal works - 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); - UserDto user = userService.addVeganMeal(1L, "Eric"); - Assert.assertEquals(testUser, user); - } } diff --git a/src/Common/src/main/java/greenify/common/ActivityDto.java b/src/Common/src/main/java/greenify/common/ActivityDto.java deleted file mode 100644 index 36186d3..0000000 --- a/src/Common/src/main/java/greenify/common/ActivityDto.java +++ /dev/null @@ -1,91 +0,0 @@ -package greenify.common; - -public class ActivityDto { - private Long id; - private String name; - private String description; - private int score; - - public ActivityDto() { - } - - /** - * Constructor for ActivityDto. - * @param id id of the activity - * @param name name of the activity - * @param description description of the activity - * @param score 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; - } - - //all the getters and setters of the class - - /** - * gets the name of the activity. - * @return the name of the activity - */ - public String getName() { - return name; - } - - /** - * sets the name of the activity. - * @param name the name to be set of the activity. - */ - public void setName(String name) { - this.name = name; - } - - /** - * gets the id of the activity. - * @return the id of the activity. - */ - public Long getId() { - return id; - } - - /** - * sets the id of the activity. - * @param id the id to be set of the activity. - */ - public void setId(Long id) { - this.id = id; - } - - /** - * gets the description of the activity. - * @return the description of the activity. - */ - public String getDescription() { - return description; - } - - /** - * sets the description of the activity. - * @param description the description to be set of the activity. - */ - public void setDescription(String description) { - this.description = description; - } - - /** - * gets the score of the activity. - * @return the score of the activity. - */ - public int getScore() { - return score; - } - - /** - * sets the score of the activity. - * @param score the score to be set of the activity. - */ - public void setScore(int score) { - this.score = score; - } -} diff --git a/src/Common/src/main/java/greenify/common/UserDto.java b/src/Common/src/main/java/greenify/common/UserDto.java index def3c13..f3bdf29 100644 --- a/src/Common/src/main/java/greenify/common/UserDto.java +++ b/src/Common/src/main/java/greenify/common/UserDto.java @@ -7,7 +7,6 @@ package greenify.common; public class UserDto { private Long id; private String name; - private int veganMeal; public UserDto() { } @@ -16,12 +15,10 @@ public class UserDto { * 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) { + public UserDto(Long id, String name) { this.id = id; this.name = name; - this.veganMeal = veganMeal; } public String getName() { @@ -39,12 +36,4 @@ 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/ActivityDtoTest.java b/src/Common/src/test/java/ActivityDtoTest.java deleted file mode 100644 index 942eb6d..0000000 --- a/src/Common/src/test/java/ActivityDtoTest.java +++ /dev/null @@ -1,31 +0,0 @@ -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/Common/src/test/java/UserDtoTest.java b/src/Common/src/test/java/UserDtoTest.java index 3c92691..a1892bd 100644 --- a/src/Common/src/test/java/UserDtoTest.java +++ b/src/Common/src/test/java/UserDtoTest.java @@ -10,17 +10,15 @@ public class UserDtoTest { UserDto testUser = new UserDto(); testUser.setId(1L); testUser.setName("greenify"); - testUser.setVeganMeal(0); - UserDto user = new UserDto(1L, "greenify", 0); + UserDto user = new UserDto(1L, "greenify"); assertTrue(user.getId() == 1L); assertEquals(user.getName(), "greenify"); - assertTrue(user.getVeganMeal() == 0); } @Test public void equalsTest() { - UserDto first = new UserDto(1L, "greenify", 0); - UserDto second = new UserDto(1L, "greenify", 0); + UserDto first = new UserDto(1L, "greenify"); + UserDto second = new UserDto(1L, "greenify"); assertEquals(first.getId(), second.getId()); assertEquals(first.getName(), second.getName()); } diff --git a/src/Server/src/main/java/greenify/server/InputItem.java b/src/Server/src/main/java/greenify/server/InputItem.java new file mode 100644 index 0000000..985ec2e --- /dev/null +++ b/src/Server/src/main/java/greenify/server/InputItem.java @@ -0,0 +1,59 @@ +package greenify.server; + +public class InputItem { + private String name; + private Boolean isFloat; + private String defaultValue; + private Boolean isPresentByDefault = true; + + /** + * Constructor for input items. + * @param name of the input + * @param isFloat whether it is float or not + * @param defaultValue states the value + */ + public InputItem(String name, boolean isFloat, String defaultValue) { + this.name = name; + this.isFloat = isFloat; + this.defaultValue = defaultValue; + } + + /** + * Constructor for input items. + * @param name of the input + * @param isFloat whether it is float or not + * @param defaultValue states the value + * @param isPresentByDefault for different number of cars + */ + public InputItem(String name, Boolean isFloat, String defaultValue, + Boolean isPresentByDefault) { + this.name = name; + this.isFloat = isFloat; + this.defaultValue = defaultValue; + this.isPresentByDefault = isPresentByDefault; + } + + public String getDefaultValue() { + return defaultValue; + } + + public void setDefaultValue(String defaultValue) { + this.defaultValue = defaultValue; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Boolean getFloat() { + return isFloat; + } + + public void setFloat(Boolean isFloat) { + this.isFloat = isFloat; + } +} diff --git a/src/Server/src/main/java/greenify/server/InputValidator.java b/src/Server/src/main/java/greenify/server/InputValidator.java new file mode 100644 index 0000000..a17b575 --- /dev/null +++ b/src/Server/src/main/java/greenify/server/InputValidator.java @@ -0,0 +1,87 @@ +package greenify.server; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +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_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" ) + ); + + /** + * The method checks that 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(); + } + + /** + * The method checks that 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); + } + } + if (item.getFloat()) { + try { + Float number = Float.parseFloat(value); + } catch (NumberFormatException | NullPointerException nfe) { + return false; + } + return true; + } else { + try { + Integer number = Integer.parseInt(value); + } catch (NumberFormatException | NullPointerException nfe) { + return false; + } + return true; + } + } + + /** + * getter for 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()); + } + return map; + } +} 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 deleted file mode 100644 index d73e387..0000000 --- a/src/Server/src/main/java/greenify/server/data/model/Activity.java +++ /dev/null @@ -1,130 +0,0 @@ -package greenify.server.data.model; - -import lombok.Data; - -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 - @GeneratedValue(strategy = GenerationType.AUTO) - Long id; - String name; - String description; - int score; - - /** - * makes an activity object. - * @param id the id of the activity. - * @param name the name of the feature. - * @param description the description of the feature. - * @param score the amount of points a user gets for doing this activity. - */ - public Activity(long id, String name, String description, int score) { - this.id = id; - this.name = name; - this.description = description; - this.score = score; - } - - /** - * gets the id. - * @return the id - */ - public Long getId() { - return id; - } - - /** - * gets the name. - * @return the name - */ - public String getName() { - return name; - } - - /** - * gets the description. - * @return the description - */ - public String getDescription() { - return description; - } - - /** - * gets the score. - * @return the score - */ - public int getScore() { - return score; - } - - - /** - * sets the id. - * @param id the you want to assign to this.id. - */ - public void setId(Long id) { - this.id = id; - } - - /** - * sets the name. - * @param name the you want to assign to this.name. - */ - public void setName(String name) { - this.name = name; - } - - /** - * sets the description. - * @param description the description to be set. - */ - public void setDescription(String description) { - this.description = description; - } - - /** - * sets the score you get for an activity. - * @param score the you want to assign to the activity. - */ - public void setScore(int score) { - this.score = score; - } - - - /** - * Returns a human readable object. It's in JSON. - * @return the JSON form of the object. - */ - @Override - public String toString() { - return "Activity(id=" + this.id + ", name=" + this.name + ", description=" - + this.description + ", score=" + this.score + ")"; - } - - @Override - public boolean equals(Object other) { - if (other instanceof Activity) { - Activity that = (Activity)other; - if (that.id == this.id && that.name.equals(this.name) - && that.description.equals(this.description) && that.score == this.score) { - return true; - } - } - return false; - } - - @Override - public int hashCode() { - return Objects.hash(id, name, description, score); - } -} 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 6ba6f73..39f385b 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,13 @@ package greenify.server.data.model; +import greenify.server.InputValidator; import lombok.Data; +import java.util.HashMap; +import java.util.Map; import java.util.Objects; + +import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; @@ -25,7 +30,11 @@ public class User { @NotNull private String password; - private int veganMeal; + @NotNull + private Float footPrint = 0.0f; + + @ElementCollection + private Map footPrintInputs = new HashMap<>(); public User() {} @@ -34,13 +43,12 @@ public class User { * @param id the id of the user. * @param name the supplied username * @param password the supplied password - * @param veganMeal the supplied number of vegan meal */ - public User(Long id, String name, String password, int veganMeal) { + public User(Long id, String name, String password) { this.id = id; this.name = name; this.password = password; - this.veganMeal = veganMeal; + this.setFootPrintInputs(InputValidator.getDefaultValues()); } /** @@ -80,15 +88,23 @@ public class User { } /** - * gets the number of vegan meal. - * @return the veganMeal + * gets the footPrint of user. + * @return the footPrint */ - public int getVeganMeal() { - return veganMeal; + public Float getFootPrint() { + return footPrint; } - public void setVeganMeal(int veganMeal) { - this.veganMeal = veganMeal; + public Map getFootPrintInputs() { + return footPrintInputs; + } + + public void setFootPrintInputs(Map footPrintInputs) { + this.footPrintInputs = footPrintInputs; + } + + public void setFootPrint(Float footPrint) { + this.footPrint = footPrint; } @@ -99,7 +115,7 @@ public class User { @Override public String toString() { return "User(id=" + this.id + ", name=" + this.name + ", password=" - + this.password + ", veganMeal=" + this.veganMeal + ")"; + + this.password + ")"; } @Override @@ -107,7 +123,7 @@ public class User { if (other instanceof User) { User that = (User)other; if (that.id == this.id && that.name.equals(this.name) - && that.password.equals(this.password) && that.veganMeal == this.veganMeal) { + && that.password.equals(this.password)) { return true; } } @@ -116,6 +132,6 @@ public class User { @Override public int hashCode() { - return Objects.hash(id, name, password, veganMeal); + return Objects.hash(id, name, password); } } 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 3236f39..94800d8 100644 --- a/src/Server/src/main/java/greenify/server/rest/UserController.java +++ b/src/Server/src/main/java/greenify/server/rest/UserController.java @@ -1,49 +1,33 @@ 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.*; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; -//class that controls the user with regards to the server and sending data between them -//this class kind of 'redirects' the requests from the client to the server @RestController public class UserController { @Autowired UserService userService; - //registers a user in the userService @RequestMapping("/registerUser") - //requestMapping is for the communication (GET, POST, PUT requests) - //as with Web and Database Technology public UserDto registerUser(@RequestParam(value = "name") String name, @RequestParam(value = "password") String password) { - //the requestParams are the parameters that are sent with the request - //so in this case that it wants to register with the name and password return userService.registerUser(name, password); } - //logs a user in in the userService @RequestMapping("/loginUser") public UserDto loginUser(@RequestParam(value = "name") String name, @RequestParam(value = "password") String password) { return userService.loginUser(name, password); } - - - /** - * adds a vegetarian meal to the user. - * @param id the id of the user - * @param name thr username of the user - */ - @RequestMapping("/addVeganMeal") - public void addVeganMeal(@RequestParam(value = "id") Long id, - @RequestParam(value = "name") String name) { - //here the requestParams are the id and name, because that is needed for the - //addVeganMeal method of the userService - userService.addVeganMeal(id, name); + @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/CalculatorService.java b/src/Server/src/main/java/greenify/server/service/CalculatorService.java new file mode 100644 index 0000000..7ad64b4 --- /dev/null +++ b/src/Server/src/main/java/greenify/server/service/CalculatorService.java @@ -0,0 +1,67 @@ +package greenify.server.service; + +import greenify.server.data.model.User; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponentsBuilder; + +import java.util.Map; + +@Service +public class CalculatorService { + Logger logger = LoggerFactory.getLogger(UserService.class); + + @Autowired + RestTemplate restTemplate; + + @Bean + RestTemplate restTemplate(RestTemplateBuilder builder) { + return builder.build(); + } + + protected 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" + * -H "accept: application/json" -H "app_id: a98272e3" + * -H "app_key: b9167c4918cb2b3143614b595065d83b" + */ + HttpHeaders headers = new HttpHeaders(); + headers.set("accept", MediaType.APPLICATION_JSON_VALUE); + headers.set("app_id", "a98272e3"); + headers.set("app_key", "b9167c4918cb2b3143614b595065d83b"); + HttpEntity entity = new HttpEntity<>("parameters", headers); + UriComponentsBuilder builder = + UriComponentsBuilder.fromHttpUrl("https://apis.berkeley.edu/coolclimate/footprint-sandbox"); + for (String inputId : map.keySet()) { + builder = builder.queryParam(inputId, map.get(inputId)); + } + ResponseEntity response = restTemplate + .exchange(builder.build().encode().toUri(), HttpMethod.GET, + entity, String.class); + logger.info(response.getStatusCode().toString()); + logger.info(response.getBody()); + String result = response.getBody().substring(response.getBody() + .indexOf("") + + 20, response.getBody().indexOf("")); + // to do: in not HTTP 200 or exception case throws exception + System.out.println(Float.parseFloat(result)); + return Float.parseFloat(result); + } + + public 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 7ddd5f3..61ff0d9 100644 --- a/src/Server/src/main/java/greenify/server/service/UserService.java +++ b/src/Server/src/main/java/greenify/server/service/UserService.java @@ -2,6 +2,7 @@ package greenify.server.service; import greenify.common.ApplicationException; import greenify.common.UserDto; +import greenify.server.InputValidator; import greenify.server.data.model.User; import greenify.server.data.repository.UserRepository; import org.slf4j.Logger; @@ -11,14 +12,15 @@ import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; -//userService class that gets used by the server to handle requests for users @Service public class UserService { Logger logger = LoggerFactory.getLogger(UserService.class); @Autowired UserRepository userRepository; - //userRepository to talk with the database + + @Autowired + CalculatorService calculatorService; /** * registers the user. @@ -28,17 +30,15 @@ public class UserService { */ public UserDto registerUser(String name, String password) { User user = userRepository.findByName(name); - //find the name of the user in the database if (user == null) { - user = new User(null, name, password, 0); - //if the user isn't already in the database, save it in there + user = new User(null, name, password); + user.setFootPrintInputs(InputValidator.getDefaultValues()); userRepository.save(user); } else { throw new ApplicationException("User already exists"); } logger.info("Created user id=" + user.getId() + ", name=" + user.getName()); - //return a transferable user object that has been saved - return new UserDto(user.getId(), user.getName(), user.getVeganMeal()); + return new UserDto(user.getId(), user.getName()); } /** @@ -49,34 +49,56 @@ public class UserService { */ public UserDto loginUser(String name, String password) { User user = userRepository.findByName(name); - //again find the name if (user == null) { throw new ApplicationException("User does not exist"); - //if it doesn't exist or the password is wrong, throw an exception } else { if (!user.getPassword().equals(password)) { throw new ApplicationException("Wrong password"); } } - //return a transferable user object that has been logged in - return new UserDto(user.getId(), user.getName(), user.getVeganMeal()); + return new UserDto(user.getId(), user.getName()); } /** - * add vegan meal to the user. - * @param id the id of the user - * @param name the name of the user + * The method sets input value. + * @param name of the user + * @param inputName is the name of the setting input + * @param value of the input */ - public void addVeganMeal(Long id, String name) { + public void setInput(String name, String inputName, String value) { User user = userRepository.findByName(name); - int count = user.getVeganMeal(); - //find the user and update their vegetarian meal count - count++; - user.setVeganMeal(count); - //save it to the database - userRepository.save(user); - logger.info("Added vegan meal to user(id=" + user.getId() - + ", name=" + user.getName() + ")"); + if (user == null) { + throw new ApplicationException("User does not exist"); + } else { + if (InputValidator.isValidItem(inputName) + && InputValidator.isValidItemValue(inputName, value)) { + user.getFootPrintInputs().put(inputName, value); + user.setFootPrint(calculatorService.calculateFootprint(user)); + } else { + throw new ApplicationException("Invalid input"); + } + } + } + + /** + * Gets 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) { + User user = userRepository.findByName(name); + if (InputValidator.isValidItem(inputName)) { + String item = user.getFootPrintInputs().get(inputName); + return item; + } else { + throw new ApplicationException("Invalid input"); + } + } + + public Float getFootprint(String name) { + User user = userRepository.findByName(name); + return calculatorService.calculateFootprint(user); } @GetMapping(path = "/all") diff --git a/src/Server/src/test/java/InputValidatorTest.java b/src/Server/src/test/java/InputValidatorTest.java new file mode 100644 index 0000000..e55018d --- /dev/null +++ b/src/Server/src/test/java/InputValidatorTest.java @@ -0,0 +1,59 @@ +import greenify.server.InputValidator; +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +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); + } + + @Test + public void validItemValueTest() { + Assert.assertEquals(true, InputValidator + .isValidItemValue("transportation_num_vehicles", "4")); + Assert.assertEquals(false, 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")); + } + + @Test + public void getDefaultValuesTest() { + Map map = new HashMap() {{ + put("input_size", "1"); + put("input_income", "40000"); + put("transportation_num_vehicles", "1"); + put("transportation_miles1", "16100"); + put("transportation_fuels1", "2"); + put("transportation_mpg1", null); + put("transportation_miles2", "13200"); + put("transportation_fuels2", "0"); + put("transportation_mpg2", "22"); + put("transportation_publicTrans", "436"); + put("transportation_air", "3900"); + put("housing_electricity_kwh_year", "12632"); + put("housing_cleanPercent", "0"); + put("housing_naturalGas_therms_year", "472"); + put("housing_heatingOil_gallons_year", "73"); + put("housing_square_feet", "1850"); + put("housing_water_sewage", "100"); + put("food_meat_fish_eggs", "2.4"); + put("food_grains", "4.1"); + put("food_dairy", "2.2"); + put("food_fruit_vegetables", "3.5"); + put("food_snacks_drinks", "3.4"); + put("shopping_goods", "1310"); + put("shopping_services", "2413"); + } + }; + Assert.assertEquals(InputValidator.getDefaultValues(), map); + } +} 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 deleted file mode 100644 index e33d5e4..0000000 --- a/src/Server/src/test/java/greenify/server/data/model/ActivityTest.java +++ /dev/null @@ -1,57 +0,0 @@ -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; - -public class ActivityTest { - @Test - public void setAndGetTest() { - Activity testActivity = new Activity(0, null, null, 0); - testActivity.setId(1L); - testActivity.setName("Vegetarian"); - testActivity.setDescription("Eating"); - testActivity.setScore(100); - Activity activity = new Activity(1L, "Vegetarian", "Eating", 100); - assertTrue(activity.getId().equals(1L)); - assertEquals(activity.getName(), "Vegetarian"); - assertEquals(activity.getDescription(), "Eating"); - assertEquals(activity.getScore(), 100); - assertEquals(activity, testActivity); - } - - @Test - public void toStringTest() { - Activity activity = new Activity(1, "Solar panels", "Installed", 10000); - assertEquals("Activity(id=1, name=Solar panels, " - + "description=Installed, score=10000)", activity.toString()); - } - - @Test - public void equalsTest() { - Activity first = new Activity(1, "Solar panels", "Installed", 10000); - Activity second = new Activity(1, "Solar panels", "Installed", 10000); - assertEquals(first.getId(), second.getId()); - assertEquals(first.getName(), second.getName()); - assertEquals(first.getDescription(), second.getDescription()); - assertEquals(first.getScore(), second.getScore()); - 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); - Activity second = new Activity(1, "Solar panels", "Installed", 10000); - assertEquals(first, second); - assertEquals(first.hashCode(), second.hashCode()); - } -} diff --git a/src/Server/src/test/java/greenify/server/data/model/UserTest.java b/src/Server/src/test/java/greenify/server/data/model/UserTest.java index 5283d6b..3a27fb8 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 @@ -13,36 +13,54 @@ public class UserTest { testUser.setId(1L); testUser.setName("greenify"); testUser.setPassword("password"); - testUser.setVeganMeal(3); - User user = new User(1L, "greenify", "password", 3); + User user = new User(1L, "greenify", "password"); assertTrue(user.getId().equals(1L)); assertEquals(user.getName(), "greenify"); assertEquals(user.getPassword(), "password"); - assertEquals(user.getVeganMeal(), 3); assertEquals(user, testUser); } @Test public void toStringTest() { - User user = new User(1L, "greenify", "password", 3); - assertEquals("User(id=1, name=greenify, password=password, veganMeal=3)", user.toString()); + User user = new User(1L, "greenify", "password"); + assertEquals("User(id=1, name=greenify, password=password)", user.toString()); } @Test public void equalsTest() { - User first = new User(1L, "greenify", "password", 3); - User second = new User(1L, "greenify", "password", 3); + User first = new User(1L, "greenify", "password"); + User second = new User(1L, "greenify", "password"); assertEquals(first.getId(), second.getId()); assertEquals(first.getName(), second.getName()); assertEquals(first.getPassword(), second.getPassword()); - assertEquals(first.getVeganMeal(), second.getVeganMeal()); assertTrue(first.equals(second)); } + @Test + public void equalsDifferentId() { + User first = new User(1L, "greenify", "password"); + User second = new User(2L, "greenify", "password"); + assertFalse(first.equals(second)); + } + + @Test + public void equalsDifferentName() { + User first = new User(1L, "greenify", "password"); + User second = new User(1L, "hello", "password"); + assertFalse(first.equals(second)); + } + + @Test + public void equalsDifferentPassword() { + User first = new User(1L, "greenify", "password"); + User second = new User(1L, "greenify", "hi"); + assertFalse(first.equals(second)); + } + @Test public void notEqualsTest() { - User first = new User(1L, "greenify", "password", 3); - User second = new User(1L, "greenify", "password", 7); + User first = new User(1L, "greenify", "password"); + User second = new User(2L, "greenify", "password"); assertFalse(first.equals(second)); } @@ -55,8 +73,8 @@ public class UserTest { @Test public void hashCodeTest() { - User first = new User(1L, "greenify", "password", 3); - User second = new User(1L, "greenify", "password", 3); + User first = new User(1L, "greenify", "password"); + User second = new User(1L, "greenify", "password"); 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 a9f2974..1a00660 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 @@ -17,7 +17,7 @@ public class UserRepositoryTest { @Test public void findByUsernameTest() throws Exception { - repository.save(new User(296L, "cugurlu", "password", 6)); + 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 9daa437..bb3d1fa 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", 0)); + .willReturn(new UserDto(1L, "name")); 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", 0)); + .willReturn(new UserDto(1L, "ceren")); mvc.perform(get("/loginUser") .param("name", "ceren") .param("password", "password") @@ -54,4 +54,9 @@ public class UserControllerTest { .andDo(print()) .andExpect(status().isOk()).andExpect(content().json("{'id':1,'name':'ceren'}")); } + + @Test + public void setInputTest() throws Exception { + + } } diff --git a/src/Server/src/test/java/greenify/server/service/CalculatorServiceTest.java b/src/Server/src/test/java/greenify/server/service/CalculatorServiceTest.java new file mode 100644 index 0000000..9c9926f --- /dev/null +++ b/src/Server/src/test/java/greenify/server/service/CalculatorServiceTest.java @@ -0,0 +1,114 @@ +package greenify.server.service; + +import static org.springframework.test.web.client.match.MockRestRequestMatchers.header; +import static org.springframework.test.web.client.match.MockRestRequestMatchers.method; +import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; + +import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus; + +import greenify.server.data.model.User; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.client.ExpectedCount; +import org.springframework.test.web.client.MockRestServiceServer; +import org.springframework.web.client.RestTemplate; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.HashMap; +import java.util.Map; + +@RunWith(SpringRunner.class) +public class CalculatorServiceTest { + + @TestConfiguration + static class CalculatorServiceTestConfiguration { + + RestTemplate restTemplate = new RestTemplate(); + + @Bean + public CalculatorService calculatorService() { + CalculatorService calculatorService = new CalculatorService(); + return calculatorService; + } + + @Bean + public RestTemplate restTemplate() { + return restTemplate; + } + } + + @Autowired + private CalculatorService calculatorService; + + @Autowired + private RestTemplate restTemplate; + + private MockRestServiceServer mockServer; + + @Before + public void init() { + mockServer = MockRestServiceServer.createServer(restTemplate); + } + + @Test + public void calculateFootprintTest() throws URISyntaxException { + Map map = new HashMap() {{ + put("input_location_mode", "1"); + put("input_location", "48001"); + put("input_income", "1"); + } + }; + User user = new User(1L,"greenify", "password"); + user.setFootPrintInputs(map); + mockServer.expect(ExpectedCount.once(), + //requestTo(new URI("https://apis.berkeley.edu/coolclimate/footprint-sandbox"))) + requestTo(new URI("https://apis.berkeley.edu/coolclimate/footprint-sandbox?" + + "input_location=48001&input_location_mode=1&input_income=1"))) + .andExpect(method(HttpMethod.GET)) + .andExpect(header("app_id", "a98272e3")) + .andExpect(header("app_key", "b9167c4918cb2b3143614b595065d83b")) + .andRespond(withStatus(HttpStatus.OK) + .contentType(MediaType.APPLICATION_JSON) + .body("\n" + + "\n" + + " 5.0765\n" + + " 1.167595" + + "\n" + + " 2.481474\n" + + " 2.478352\n" + + " 19.259982\n" + + "") + ); + Float footPrint = calculatorService.calculateFootprint(user); + mockServer.verify(); + Assert.assertEquals(new Float(19.259982), footPrint); + } + + @Test + public void invokeExternalServiceTest() { + CalculatorService service = new CalculatorService(); + service.restTemplate = new RestTemplate(); + Map map = new HashMap() {{ + put("input_location_mode", "1"); + put("input_location", "48001"); + put("input_income", "1"); + put("input_size", "0"); + put("input_footprint_transportation_miles1", "3"); + put("input_footprint_transportation_mpg1", "5"); + put("input_footprint_transportation_fuel1", "0"); + } + }; + Float footPrint = service.invokeExternalService(map); + Assert.assertEquals(new Float(12.743548), footPrint); + } +} 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 a1ed76c..bf06ce8 100644 --- a/src/Server/src/test/java/greenify/server/service/UserServiceTest.java +++ b/src/Server/src/test/java/greenify/server/service/UserServiceTest.java @@ -1,6 +1,7 @@ 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; @@ -11,7 +12,6 @@ import greenify.server.data.repository.UserRepository; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Mock; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.test.mock.mockito.MockBean; @@ -34,12 +34,15 @@ public class UserServiceTest { @MockBean private UserRepository userRepository; + @MockBean + private CalculatorService calculatorService; + /** * setUp method for test. */ @Before public void setUp() { - User alex = new User(1L, "alex", "password", 0); + User alex = new User(1L, "alex", "password"); when(userRepository.findByName(alex.getName())) .thenReturn(alex); } @@ -52,13 +55,96 @@ public class UserServiceTest { assertEquals(found.getName(), name); } + @Test + public void loginExceptionTest() { + assertThrows(ApplicationException.class, () -> { + userService.loginUser("alex", "greenify"); + }); + } + @Test public void userRegisterTest() { - User user = new User(1L, "name", "password", 0); + User user = new User(1L, "name", "password"); UserDto registered = userService.registerUser(user.getName(), user.getPassword()); assertEquals(registered.getName(), "name"); } + @Test + public void registerExceptionTest() { + assertThrows(ApplicationException.class, () -> { + userService.registerUser("alex", "password"); + }); + } + + @Test + public void setInputTest() { + User alex = new User(1L, "alex", "password"); + when(userRepository.findByName(alex.getName())) + .thenReturn(alex); + userService.setInput("alex", "food_grains", "6.5"); + assertEquals("6.5", alex.getFootPrintInputs().get("food_grains")); + } + + @Test + public void setInputNullTest() { + assertThrows(ApplicationException.class, () -> { + userService.setInput(null, "hello", "5.5"); + }); + } + + @Test + public void setInputApplicationTestItem() { + assertThrows(ApplicationException.class, () -> { + userService.setInput("alex", "hello", "3.5"); + }); + } + + @Test + public void setInputApplicationTestValue() { + assertThrows(ApplicationException.class, () -> { + userService.setInput("alex", "transportation_num_vehicles", "5.5"); + }); + } + + + @Test + public void setInputFootprintTest() { + User alex = new User(1L, "alex", "password"); + when(userRepository.findByName(alex.getName())) + .thenReturn(alex); + when(calculatorService.calculateFootprint(alex)) + .thenReturn(15f); + userService.setInput("alex", "food_grains", "6.5"); + assertTrue(15f == alex.getFootPrint()); + } + + @Test + public void getInputTest() { + User alex = new User(1L, "alex", "password"); + when(userRepository.findByName(alex.getName())) + .thenReturn(alex); + userService.setInput("alex", "food_grains", "6.5"); + assertEquals("6.5", userService.getInput("alex", "food_grains")); + } + + @Test + public void getInputExceptionTest() { + assertThrows(ApplicationException.class, () -> { + userService.getInput("alex", "hello"); + }); + } + + @Test + public void getFootprintTest() { + User alex = new User(1L, "alex", "password"); + when(userRepository.findByName(alex.getName())) + .thenReturn(alex); + when(calculatorService.calculateFootprint(alex)) + .thenReturn(15f); + userService.setInput("alex", "food_grains", "6.5"); + assertTrue(15f == userService.getFootprint("alex")); + } + @Test public void getAllUserTest() { assertEquals(userRepository.findAll(), userService.getAllUsers());