From a41b697510cac764c8b0d0daa3ce774e1dd9ee96 Mon Sep 17 00:00:00 2001 From: cugurlu Date: Sun, 31 Mar 2019 00:45:50 +0100 Subject: [PATCH 1/7] Fix calculator bugs --- .../src/main/java/greenify/client/Friend.java | 10 ++-- .../controller/CalculatorController.java | 52 +++++++++------- .../src/main/resources/fxml/calculator.fxml | 60 ++++++++----------- 3 files changed, 62 insertions(+), 60 deletions(-) diff --git a/src/Client/src/main/java/greenify/client/Friend.java b/src/Client/src/main/java/greenify/client/Friend.java index 87ec3cd..64d38bc 100644 --- a/src/Client/src/main/java/greenify/client/Friend.java +++ b/src/Client/src/main/java/greenify/client/Friend.java @@ -6,11 +6,11 @@ import javafx.beans.property.SimpleStringProperty; public class Friend { private SimpleStringProperty friend; - private SimpleFloatProperty friendScore; + private SimpleFloatProperty score; public Friend(String friend, Float friendScore) { this.friend = new SimpleStringProperty(friend); - this.friendScore = new SimpleFloatProperty(friendScore); + this.score = new SimpleFloatProperty(friendScore); } @@ -22,11 +22,11 @@ public class Friend { this.friend = new SimpleStringProperty(name); } - public Float getFriendScore() { - return friendScore.get(); + public Float getScore() { + return score.get(); } public void setScore(Float score) { - this.friendScore = new SimpleFloatProperty(score); + this.score = new SimpleFloatProperty(score); } } \ No newline at end of file diff --git a/src/Client/src/main/java/greenify/client/controller/CalculatorController.java b/src/Client/src/main/java/greenify/client/controller/CalculatorController.java index 241d1c0..b7105c3 100644 --- a/src/Client/src/main/java/greenify/client/controller/CalculatorController.java +++ b/src/Client/src/main/java/greenify/client/controller/CalculatorController.java @@ -16,6 +16,7 @@ import javafx.scene.control.ScrollPane; import javafx.scene.control.Slider; import javafx.scene.control.TextField; import javafx.scene.layout.AnchorPane; +import javafx.scene.text.Text; import javafx.stage.Stage; import javafx.stage.Window; import javafx.util.Duration; @@ -29,6 +30,9 @@ public class CalculatorController { @Autowired UserService userService; + @Autowired + DashBoardController controller; + //navigation panes @FXML private AnchorPane getStartedPane; @@ -119,6 +123,10 @@ public class CalculatorController { //shopping pane @FXML + private Text goodsField; + @FXML + private Text servicesField; + @FXML private Slider goodsSlider; @FXML private Label goodsLabel; @@ -303,8 +311,7 @@ public class CalculatorController { * The method saves the calculation. * @param event user clicks to button */ - public void saveCalc(ActionEvent event) { - Window owner = saveButton.getScene().getWindow(); + public void saveCalc(ActionEvent event) throws InterruptedException { getStartedPane.setVisible(false); travelPane.setVisible(false); homePane.setVisible(false); @@ -336,7 +343,7 @@ public class CalculatorController { if (!carTravelGasolineLabel.getText().replace(" km/L", "").equals("0")) { userService.updateInput(userService.currentUser.getName(), "input_footprint_transportation_mpg1", - carTravelGasolineLabel.getText()); + carTravelGasolineLabel.getText().replace(" km/L", "")); } if (!carTravelDieselField.getText().equals("0")) { userService.updateInput(userService.currentUser.getName(), @@ -346,36 +353,36 @@ public class CalculatorController { if (!carTravelDieselLabel.getText().replace(" km/L", "").equals("0")) { userService.updateInput(userService.currentUser.getName(), "input_footprint_transportation_mpg2", - carTravelDieselLabel.getText()); + carTravelDieselLabel.getText().replace(" km/L", "")); } if (!carTravelElectricField.getText().equals("0")) { userService.updateInput(userService.currentUser.getName(), "input_footprint_transportation_miles3", - peopleInHouseHoldLabel.getText()); + carTravelElectricField.getText()); } if (!carTravelElectricLabel.getText().replace(" km/Le", "").equals("0")) { userService.updateInput(userService.currentUser.getName(), "input_footprint_transportation_mpg3", - carTravelElectricLabel.getText()); + carTravelElectricLabel.getText().replace(" km/Le", "")); } if (!electricityField.getText().equals("0")) { userService.updateInput(userService.currentUser.getName(), - "input_footprint_electricity_dollars", + "input_footprint_housing_electricity_dollars", electricityField.getText()); } if (!cleanEnergyPurchasedLabel.getText().replace(" %", "").equals("0")) { userService.updateInput(userService.currentUser.getName(), "input_footprint_housing_gco2_per_kwh", - cleanEnergyPurchasedLabel.getText()); + cleanEnergyPurchasedLabel.getText().replace(" %", "")); } if (!naturalGasField.getText().equals("0")) { userService.updateInput(userService.currentUser.getName(), - "input_footprint_naturalgas_dollars", + "input_footprint_housing_naturalgas_dollars", naturalGasField.getText()); } if (!heatingOilField.getText().equals("0")) { userService.updateInput(userService.currentUser.getName(), - "input_footprint_heatingoil_dollars", + "input_footprint_housing_heatingoil_dollars", heatingOilField.getText()); } if (!livingSpaceField.getText().equals("0")) { @@ -386,44 +393,49 @@ public class CalculatorController { if (!waterUsageLabel.getText().replace("% of similar households", "").equals("0")) { userService.updateInput(userService.currentUser.getName(), "input_footprint_housing_watersewage", - waterUsageLabel.getText()); + waterUsageLabel.getText().replace("% of similar households", "")); } if (!meatFishEggsLabel.getText().replace(" daily servings per person", "").equals("2.6")) { userService.updateInput(userService.currentUser.getName(), "input_footprint_shopping_food_meatfisheggs", - meatFishEggsLabel.getText()); + meatFishEggsLabel.getText().replace(" daily servings per person", "")); } - if (!grainsBakedGoodsLabel.getText().replace(" daily servings per person", "").equals("4.4")) { + if (!grainsBakedGoodsLabel.getText() + .replace(" daily servings per person", "").equals("4.4")) { userService.updateInput(userService.currentUser.getName(), "input_footprint_shopping_food_cereals", - grainsBakedGoodsLabel.getText()); + grainsBakedGoodsLabel.getText().replace(" daily servings per person", "")); } if (!dairyLabel.getText().replace(" daily servings per person", "").equals("2.4")) { userService.updateInput(userService.currentUser.getName(), "input_footprint_shopping_food_dairy", - dairyLabel.getText()); + dairyLabel.getText().replace(" daily servings per person", "")); } - if (!fruitsVegetablesLabel.getText().replace(" daily servings per person", "").equals("3.9")) { + if (!fruitsVegetablesLabel.getText() + .replace(" daily servings per person", "").equals("3.9")) { userService.updateInput(userService.currentUser.getName(), "input_footprint_shopping_food_fruitvegetables", - fruitsVegetablesLabel.getText()); + fruitsVegetablesLabel.getText().replace(" daily servings per person", "")); } if (!snacksDrinksLabel.getText().replace(" daily servings per person", "").equals("3.7")) { userService.updateInput(userService.currentUser.getName(), "input_footprint_shopping_food_otherfood", - snacksDrinksLabel.getText()); + snacksDrinksLabel.getText().replace(" daily servings per person", "")); } if (!goodsLabel.getText().replace(" € / month", "").equals("1520")) { userService.updateInput(userService.currentUser.getName(), "input_footprint_shopping_goods_total", - snacksDrinksLabel.getText()); + goodsLabel.getText().replace("€ / month", "")); } if (!servicesLabel.getText().replace(" € / month", "").equals("3428")) { userService.updateInput(userService.currentUser.getName(), "input_footprint_shopping_services_total", - snacksDrinksLabel.getText()); + servicesLabel.getText().replace("€ / month", "")); } + Float footprint = userService.saveFootprint(userService.currentUser.getName()); + Window owner = saveButton.getScene().getWindow(); Stage current = (Stage) owner; current.close(); + controller.updateLeaderboard(); } } \ No newline at end of file diff --git a/src/Client/src/main/resources/fxml/calculator.fxml b/src/Client/src/main/resources/fxml/calculator.fxml index 412d1fc..8543d3f 100644 --- a/src/Client/src/main/resources/fxml/calculator.fxml +++ b/src/Client/src/main/resources/fxml/calculator.fxml @@ -1,12 +1,5 @@ - - - - - - - @@ -50,9 +43,9 @@ + from https://www.flaticon.com/ + flaticon is licenced by http://creativecommons.org/licenses/by/3.0/ Creative Commons BY 3.0 + --> @@ -108,7 +101,7 @@ - @@ -146,9 +139,8 @@ + + + + + + + + + + + + + + From cc236c46b653899f18ca8fced8ec35dab6392e94 Mon Sep 17 00:00:00 2001 From: cugurlu Date: Sun, 31 Mar 2019 00:49:16 +0100 Subject: [PATCH 3/7] Add tests for userService --- src/Client/src/test/java/UserServiceTest.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/Client/src/test/java/UserServiceTest.java b/src/Client/src/test/java/UserServiceTest.java index 39478b4..d46c4f2 100644 --- a/src/Client/src/test/java/UserServiceTest.java +++ b/src/Client/src/test/java/UserServiceTest.java @@ -10,6 +10,9 @@ import org.mockito.Spy; import org.mockito.junit.MockitoJUnitRunner; import org.springframework.web.client.RestTemplate; +import java.util.ArrayList; +import java.util.List; + @RunWith(MockitoJUnitRunner.class) public class UserServiceTest { @@ -51,6 +54,38 @@ public class UserServiceTest { Assert.assertEquals(estimate, result); } + @Test + public void saveFootprint() throws Exception { + Float estimate = new Float(5); + Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/saveFootprint?name=Eric"), + Float.class)) + .thenReturn(estimate); + Float result = userService.saveFootprint("Eric"); + Assert.assertEquals(estimate, result); + } + + @Test + public void getFriendNamesTest() throws Exception { + List estimate = new ArrayList(); + estimate.add("alex"); + Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/getFriends?name=Eric"), + List.class)) + .thenReturn(estimate); + List result = userService.getFriendNames("Eric"); + Assert.assertEquals(estimate, result); + } + + @Test + public void getAllUsers() throws Exception { + List estimate = new ArrayList(); + estimate.add("alex"); + Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/getAllUsers"), + List.class)) + .thenReturn(estimate); + List result = userService.getAllUsers(); + Assert.assertEquals(estimate, result); + } + @Test public void setInputTest() throws Exception { userService.updateInput("Eric", "input_size", "5"); From 383d505f52a711afa154804d3c820cdcec55d2ee Mon Sep 17 00:00:00 2001 From: cugurlu Date: Sun, 31 Mar 2019 00:50:21 +0100 Subject: [PATCH 4/7] Fix checkstyle errors and delete unused methods for high coverage --- .../java/greenify/server/InputValidator.java | 241 +++++++++--------- .../java/greenify/server/data/model/User.java | 39 +-- 2 files changed, 130 insertions(+), 150 deletions(-) diff --git a/src/Server/src/main/java/greenify/server/InputValidator.java b/src/Server/src/main/java/greenify/server/InputValidator.java index b7dc6a1..be03097 100644 --- a/src/Server/src/main/java/greenify/server/InputValidator.java +++ b/src/Server/src/main/java/greenify/server/InputValidator.java @@ -4,127 +4,130 @@ 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_location", false, "Chicago"), - new InputItem("input_location_mode", false, "1"), - new InputItem("input_size", false, "3"), - new InputItem("input_income", false, "3000"), - new InputItem("input_population", false, "1"), - new InputItem("input_changed", false, "0"), - new InputItem("input_footprint_household_adults", false, "0"), - new InputItem("input_footprint_household_children", false, "0"), - new InputItem("input_footprint_transportation_num_vehicles", false, "1"), - new InputItem("input_footprint_transportation_miles1", false, "16100", false), - new InputItem("input_footprint_transportation_mpg1", false, "22", false), - new InputItem("input_footprint_transportation_fuel1", false, "0", false), - new InputItem("input_footprint_transportation_miles2", false, "0", false), - new InputItem("input_footprint_transportation_fuel2", false, "0", false), - new InputItem("input_footprint_transportation_mpg2", false, "0", false), - new InputItem("input_footprint_transportation_miles3", false, "0", false), - new InputItem("input_footprint_transportation_fuel3", false, "0", false), - new InputItem("input_footprint_transportation_mpg3", false, "0", false), - new InputItem("input_footprint_transportation_miles4", false, "0", false), - new InputItem("input_footprint_transportation_fuel4", false, "0", false), - new InputItem("input_footprint_transportation_mpg4", false, "0", false), - new InputItem("input_footprint_transportation_miles5", false, "0", false), - new InputItem("input_footprint_transportation_fuel5", false, "0", false), - new InputItem("input_footprint_transportation_mpg5", false, "0", false), - new InputItem("input_footprint_transportation_miles6", false, "0", false), - new InputItem("input_footprint_transportation_fuel6", false, "0", false), - new InputItem("input_footprint_transportation_mpg6", false, "0", false), - new InputItem("input_footprint_transportation_miles7", false, "0", false), - new InputItem("input_footprint_transportation_fuel7", false, "0", false), - new InputItem("input_footprint_transportation_mpg7", false, "0", false), - new InputItem("input_footprint_transportation_miles8", false, "0", false), - new InputItem("input_footprint_transportation_fuel8", false, "0", false), - new InputItem("input_footprint_transportation_mpg8", false, "0", false), - new InputItem("input_footprint_transportation_miles9", false, "0", false), - new InputItem("input_footprint_transportation_fuel9", false, "0", false), - new InputItem("input_footprint_transportation_mpg9", false, "0", false), - new InputItem("input_footprint_transportation_miles10", false, "0", false), - new InputItem("input_footprint_transportation_fuel10", false, "0", false), - new InputItem("input_footprint_transportation_mpg10", false, "0", false), - new InputItem("input_footprint_transportation_groundtype", false, "0"), - new InputItem("input_footprint_transportation_publictrans", false, "436"), - new InputItem("input_footprint_transportation_bus", false, "174"), - new InputItem("input_footprint_transportation_transit", false, "131"), - new InputItem("input_footprint_transportation_commuter", false, "87"), - new InputItem("input_footprint_transportation_intercity", false, "44"), - new InputItem("input_footprint_transportation_airtype", false, "0"), - new InputItem("input_footprint_transportation_airtotal", false, "6"), - new InputItem("input_footprint_transportation_airshort", false, "3"), - new InputItem("input_footprint_transportation_airmedium", false, "3"), - new InputItem("input_footprint_transportation_airlong", false, "0"), - new InputItem("input_footprint_transportation_airextended", false, "0"), - new InputItem("input_footprint_housing_cdd", false, "40000"), - new InputItem("input_footprint_housing_hdd", false, "40000"), - new InputItem("input_footprint_housing_electricity_type", false, "0"), - new InputItem("input_footprint_housing_electricity_dollars", false, "1200"), - new InputItem("input_footprint_housing_electricity_kwh", false, "12632"), - new InputItem("input_footprint_housing_cleanpercent", false, "0"), - new InputItem("input_footprint_housing_naturalgas_type", false, "0"), - new InputItem("input_footprint_housing_naturalgas_dollars", false, "600"), - new InputItem("input_footprint_housing_naturalgas_therms", false, "472"), - new InputItem("input_footprint_housing_naturalgas_cuft", false, "472444"), - new InputItem("input_footprint_housing_heatingoil_type", false, "0"), - new InputItem("input_footprint_housing_heatingoil_dollars", false, "220"), - new InputItem("input_footprint_housing_heatingoil_gallons", false, "73"), - new InputItem("input_footprint_housing_heatingoil_dollars_per_gallon", false, "40000"), - new InputItem("input_footprint_housing_squarefeet", false, "1850"), - new InputItem("input_footprint_housing_watersewage", false, "100"), - new InputItem("input_footprint_housing_gco2_per_kwh", false, "0"), - new InputItem("input_footprint_shopping_food_meatfisheggs_default", true, "2.4"), - new InputItem("input_footprint_shopping_food_meat_beefpork_default", true, "1.1"), - new InputItem("input_footprint_shopping_food_meat_poultry_default", true, "0.7"), - new InputItem("input_footprint_shopping_food_meat_fish_default", true, "0.3"), - new InputItem("input_footprint_shopping_food_meat_other_default", true, "0.3"), - new InputItem("input_footprint_shopping_food_fruitvegetables_default", true, "3.5"), - new InputItem("input_footprint_shopping_food_dairy_default", true, "2.2"), - new InputItem("input_footprint_shopping_food_cereals_default", true, "4.1"), - new InputItem("input_footprint_shopping_food_otherfood_default", true, "3.4"), - new InputItem("input_footprint_shopping_food_meattype", true, "0"), - new InputItem("input_footprint_shopping_food_meatfisheggs", true, "2.4"), - new InputItem("input_footprint_shopping_food_meat_beefpork", true, "2.4"), - new InputItem("input_footprint_shopping_food_meat_poultry", true, "2.4"), - new InputItem("input_footprint_shopping_food_meat_fish", true, "2.4"), - new InputItem("input_footprint_shopping_food_meat_other", true, "2.4"), - new InputItem("input_footprint_shopping_food_cereals", true, "4.1"), - new InputItem("input_footprint_shopping_food_dairy", true, "2.2"), - new InputItem("input_footprint_shopping_food_fruitvegetables", true, "3.5"), - new InputItem("input_footprint_shopping_food_otherfood", true, "3.4"), - new InputItem("input_footprint_shopping_goods_default_furnitureappliances", false, "1310"), - new InputItem("input_footprint_shopping_goods_default_clothing", false, "1310"), - new InputItem("input_footprint_shopping_goods_default_other_entertainment", false, "1310"), - new InputItem("input_footprint_shopping_goods_default_other_office", false, "1310"), - new InputItem("input_footprint_shopping_goods_default_other_personalcare", false, "1310"), - new InputItem("input_footprint_shopping_goods_default_other_autoparts", false, "1310"), - new InputItem("input_footprint_shopping_goods_default_other_medical", false, "1310"), - new InputItem("input_footprint_shopping_goods_type", false, "1310"), - new InputItem("input_footprint_shopping_goods_total", false, "1310"), - new InputItem("input_footprint_shopping_goods_furnitureappliances", false, "362"), - new InputItem("input_footprint_shopping_goods_clothing", false, "391"), - new InputItem("input_footprint_shopping_goods_other_type", false, "0"), - new InputItem("input_footprint_shopping_goods_other_total", false, "1311"), - new InputItem("input_footprint_shopping_goods_other_entertainment", false, "200"), - new InputItem("input_footprint_shopping_goods_other_office", false, "38"), - new InputItem("input_footprint_shopping_goods_other_personalcare", false, "103"), - new InputItem("input_footprint_shopping_goods_other_autoparts", false, "45"), - new InputItem("input_footprint_shopping_goods_other_medical", false, "172"), - new InputItem("input_footprint_shopping_services_type", false, "0"), - new InputItem("input_footprint_shopping_services_total", false, "2413"), - new InputItem("input_footprint_shopping_services_healthcare", false, "841"), - new InputItem("input_footprint_shopping_services_education", false, "122"), - new InputItem("input_footprint_shopping_services_communications", false, "163"), - new InputItem("input_footprint_shopping_services_vehicleservices", false, "180"), - new InputItem("input_footprint_shopping_services_finance", false, "566"), - new InputItem("input_footprint_shopping_services_household", false, "28"), - new InputItem("input_footprint_shopping_services_charity", false, "146"), - new InputItem("input_footprint_shopping_services_miscservices", false, "114"), - new InputItem("internal_state_abbreviation", false, "US") + new InputItem("input_location", false, "Chicago"), + new InputItem("input_location_mode", false, "1"), + new InputItem("input_size", false, "3"), + new InputItem("input_income", false, "3000"), + new InputItem("input_population", false, "1"), + new InputItem("input_changed", false, "0"), + new InputItem("input_footprint_household_adults", false, "0"), + new InputItem("input_footprint_household_children", false, "0"), + new InputItem("input_footprint_transportation_num_vehicles", false, "10"), + new InputItem("input_footprint_transportation_miles1", false, "16100", false), + new InputItem("input_footprint_transportation_mpg1", false, "22", false), + new InputItem("input_footprint_transportation_fuel1", false, "0", false), + new InputItem("input_footprint_transportation_miles2", false, "0", false), + new InputItem("input_footprint_transportation_fuel2", false, "0", false), + new InputItem("input_footprint_transportation_mpg2", false, "0", false), + new InputItem("input_footprint_transportation_miles3", false, "0", false), + new InputItem("input_footprint_transportation_fuel3", false, "0", false), + new InputItem("input_footprint_transportation_mpg3", false, "0", false), + new InputItem("input_footprint_transportation_miles4", false, "0", false), + new InputItem("input_footprint_transportation_fuel4", false, "0", false), + new InputItem("input_footprint_transportation_mpg4", false, "0", false), + new InputItem("input_footprint_transportation_miles5", false, "0", false), + new InputItem("input_footprint_transportation_fuel5", false, "0", false), + new InputItem("input_footprint_transportation_mpg5", false, "0", false), + new InputItem("input_footprint_transportation_miles6", false, "0", false), + new InputItem("input_footprint_transportation_fuel6", false, "0", false), + new InputItem("input_footprint_transportation_mpg6", false, "0", false), + new InputItem("input_footprint_transportation_miles7", false, "0", false), + new InputItem("input_footprint_transportation_fuel7", false, "0", false), + new InputItem("input_footprint_transportation_mpg7", false, "0", false), + new InputItem("input_footprint_transportation_miles8", false, "0", false), + new InputItem("input_footprint_transportation_fuel8", false, "0", false), + new InputItem("input_footprint_transportation_mpg8", false, "0", false), + new InputItem("input_footprint_transportation_miles9", false, "0", false), + new InputItem("input_footprint_transportation_fuel9", false, "0", false), + new InputItem("input_footprint_transportation_mpg9", false, "0", false), + new InputItem("input_footprint_transportation_miles10", false, "0", false), + new InputItem("input_footprint_transportation_fuel10", false, "0", false), + new InputItem("input_footprint_transportation_mpg10", false, "0", false), + new InputItem("input_footprint_transportation_groundtype", false, "0"), + new InputItem("input_footprint_transportation_publictrans", false, "436"), + new InputItem("input_footprint_transportation_bus", false, "174"), + new InputItem("input_footprint_transportation_transit", false, "131"), + new InputItem("input_footprint_transportation_commuter", false, "87"), + new InputItem("input_footprint_transportation_intercity", false, "44"), + new InputItem("input_footprint_transportation_airtype", false, "0"), + new InputItem("input_footprint_transportation_airtotal", false, "6"), + new InputItem("input_footprint_transportation_airshort", false, "3"), + new InputItem("input_footprint_transportation_airmedium", false, "3"), + new InputItem("input_footprint_transportation_airlong", false, "0"), + new InputItem("input_footprint_transportation_airextended", false, "0"), + new InputItem("input_footprint_housing_cdd", false, "40000"), + new InputItem("input_footprint_housing_hdd", false, "40000"), + new InputItem("input_footprint_housing_electricity_type", false, "0"), + new InputItem("input_footprint_housing_electricity_dollars", false, "1200"), + new InputItem("input_footprint_housing_electricity_kwh", false, "12632"), + new InputItem("input_footprint_housing_cleanpercent", false, "0"), + new InputItem("input_footprint_housing_naturalgas_type", false, "0"), + new InputItem("input_footprint_housing_naturalgas_dollars", false, "600"), + new InputItem("input_footprint_housing_naturalgas_therms", false, "472"), + new InputItem("input_footprint_housing_naturalgas_cuft", false, "472444"), + new InputItem("input_footprint_housing_heatingoil_type", false, "0"), + new InputItem("input_footprint_housing_heatingoil_dollars", false, "220"), + new InputItem("input_footprint_housing_heatingoil_gallons", false, "73"), + new InputItem("input_footprint_housing_heatingoil_dollars_per_gallon", false, "40000"), + new InputItem("input_footprint_housing_squarefeet", false, "1850"), + new InputItem("input_footprint_housing_watersewage", false, "100"), + new InputItem("input_footprint_housing_gco2_per_kwh", false, "0"), + new InputItem("input_footprint_shopping_food_meatfisheggs_default", true, "2.4"), + new InputItem("input_footprint_shopping_food_meat_beefpork_default", true, "1.1"), + new InputItem("input_footprint_shopping_food_meat_poultry_default", true, "0.7"), + new InputItem("input_footprint_shopping_food_meat_fish_default", true, "0.3"), + new InputItem("input_footprint_shopping_food_meat_other_default", true, "0.3"), + new InputItem("input_footprint_shopping_food_fruitvegetables_default", true, "3.5"), + new InputItem("input_footprint_shopping_food_dairy_default", true, "2.2"), + new InputItem("input_footprint_shopping_food_cereals_default", true, "4.1"), + new InputItem("input_footprint_shopping_food_otherfood_default", true, "3.4"), + new InputItem("input_footprint_shopping_food_meattype", true, "0"), + new InputItem("input_footprint_shopping_food_meatfisheggs", true, "2.4"), + new InputItem("input_footprint_shopping_food_meat_beefpork", true, "2.4"), + new InputItem("input_footprint_shopping_food_meat_poultry", true, "2.4"), + new InputItem("input_footprint_shopping_food_meat_fish", true, "2.4"), + new InputItem("input_footprint_shopping_food_meat_other", true, "2.4"), + new InputItem("input_footprint_shopping_food_cereals", true, "4.1"), + new InputItem("input_footprint_shopping_food_dairy", true, "2.2"), + new InputItem("input_footprint_shopping_food_fruitvegetables", true, "3.5"), + new InputItem("input_footprint_shopping_food_otherfood", true, "3.4"), + new InputItem("input_footprint_shopping_goods_default_furnitureappliances", + false, "1310"), + new InputItem("input_footprint_shopping_goods_default_clothing", false, "1310"), + new InputItem("input_footprint_shopping_goods_default_other_entertainment", + false, "1310"), + new InputItem("input_footprint_shopping_goods_default_other_office", false, "1310"), + new InputItem( + "input_footprint_shopping_goods_default_other_personalcare", false, "1310"), + new InputItem("input_footprint_shopping_goods_default_other_autoparts", + false, "1310"), + new InputItem("input_footprint_shopping_goods_default_other_medical", false, "1310"), + new InputItem("input_footprint_shopping_goods_type", false, "1310"), + new InputItem("input_footprint_shopping_goods_total", false, "1310"), + new InputItem("input_footprint_shopping_goods_furnitureappliances", false, "362"), + new InputItem("input_footprint_shopping_goods_clothing", false, "391"), + new InputItem("input_footprint_shopping_goods_other_type", false, "0"), + new InputItem("input_footprint_shopping_goods_other_total", false, "1311"), + new InputItem("input_footprint_shopping_goods_other_entertainment", false, "200"), + new InputItem("input_footprint_shopping_goods_other_office", false, "38"), + new InputItem("input_footprint_shopping_goods_other_personalcare", false, "103"), + new InputItem("input_footprint_shopping_goods_other_autoparts", false, "45"), + new InputItem("input_footprint_shopping_goods_other_medical", false, "172"), + new InputItem("input_footprint_shopping_services_type", false, "0"), + new InputItem("input_footprint_shopping_services_total", false, "2413"), + new InputItem("input_footprint_shopping_services_healthcare", false, "841"), + new InputItem("input_footprint_shopping_services_education", false, "122"), + new InputItem("input_footprint_shopping_services_communications", false, "163"), + new InputItem("input_footprint_shopping_services_vehicleservices", false, "180"), + new InputItem("input_footprint_shopping_services_finance", false, "566"), + new InputItem("input_footprint_shopping_services_household", false, "28"), + new InputItem("input_footprint_shopping_services_charity", false, "146"), + new InputItem("input_footprint_shopping_services_miscservices", false, "114"), + new InputItem("internal_state_abbreviation", false, "US") ); /** @@ -149,7 +152,7 @@ public class InputValidator { item = inputItem; } } - if (Objects.requireNonNull(item).getFloat()) { + if (item.getFloat()) { try { Float.parseFloat(value); } catch (NumberFormatException | NullPointerException nfe) { @@ -177,4 +180,4 @@ public class InputValidator { } return map; } -} +} \ No newline at end of file diff --git a/src/Server/src/main/java/greenify/server/data/model/User.java b/src/Server/src/main/java/greenify/server/data/model/User.java index 4f85d38..3252ee7 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 @@ -5,20 +5,13 @@ import greenify.server.InputValidator; import lombok.Data; import java.util.ArrayList; -import java.util.Collection; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Objects; -import javax.persistence.ElementCollection; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToMany; +import javax.persistence.*; -import javax.persistence.Table; import javax.validation.constraints.NotNull; @Entity @@ -28,7 +21,7 @@ public class User { @Id @NotNull - @GeneratedValue(strategy = GenerationType.AUTO) + @GeneratedValue(strategy = GenerationType.TABLE) private Long id; @NotNull @@ -44,8 +37,7 @@ public class User { private Map footPrintInputs = new HashMap<>(); @ManyToMany - @JoinColumn - private Collection friends; + private List friends; public User() {} @@ -147,15 +139,15 @@ public class User { * This method gets the friends of the user. * @return friends list of the user */ - public ArrayList getFriends() { - return (ArrayList)this.friends; + public List getFriends() { + return this.friends; } /** * This method sets the friend list of the user. * @param friends friend list of the user */ - public void setFriends(Collection friends) { + public void setFriends(List friends) { this.friends = friends; } @@ -164,7 +156,7 @@ public class User { * @param user the friend you want to add. */ public void addFriend(User user) { - if (user.equals(this)) { + if (user.equals(this) || friends.contains(user)) { throw new ApplicationException("Cannot add yourself as a friend"); } else { friends.add(user); @@ -182,21 +174,6 @@ public class User { + this.password + ")"; } - /** - * Returns the name and score of the friends in JSON. Needed for the leaderboard. - * @return a JSON object of the friendlist with only names and scores. - */ - public String friendsToString() { - String result = "friends=["; - for (User u : friends) { - result += "{name=" + u.getName() + ", footprint=" + u.getFootPrint() + "}, "; - } - if (result.endsWith(", ")) { - return result.substring(0, result.lastIndexOf(",")) + "]"; - } - return result + "]"; - } - /** This method checks whether two users are equal or not. * * @param other an other user * @return users are (not) equal From cf1704e87274c4d9dcd28245707e5519fdd163ad Mon Sep 17 00:00:00 2001 From: cugurlu Date: Sun, 31 Mar 2019 00:51:34 +0100 Subject: [PATCH 5/7] Connect server and client for getAllUsers, saveFootprint and getFootprint methods --- .../greenify/server/rest/UserController.java | 32 +++++++++- .../greenify/server/service/UserService.java | 64 ++++++++++++------- 2 files changed, 72 insertions(+), 24 deletions(-) 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 8646eb7..0dab152 100644 --- a/src/Server/src/main/java/greenify/server/rest/UserController.java +++ b/src/Server/src/main/java/greenify/server/rest/UserController.java @@ -7,6 +7,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import java.util.List; + @RestController public class UserController { @Autowired @@ -47,7 +49,6 @@ public class UserController { public void setInput(@RequestParam(value = "name") String name, @RequestParam(value = "inputName") String inputName, @RequestParam(value = "value") String value) { - System.out.println("Here is server controller"); userService.setInput(name, inputName, value); } @@ -72,6 +73,35 @@ public class UserController { return footprint; } + /** + * This method saves footprint for a user. + * @param name name of the user + */ + @RequestMapping("/saveFootprint") + public Float saveFootprint(@RequestParam(value = "name") String name) { + Float footprint = userService.saveFootprint(name); + return footprint; + } + + /** + * This method gets friend list for a user. + * @param name name of the user + */ + @RequestMapping("/getFriends") + public List getFriendNames(@RequestParam(value = "name") String name) { + List friends = userService.getFriends(name); + return friends; + } + + /** + * This method gets the list of all users. + */ + @RequestMapping("/getAllUsers") + public List getAllUsers() { + List users = userService.getAllUsers(); + return users; + } + /** * This method adds friend for a user. * @param name name of the user 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 6d84a81..9b9c3fb 100644 --- a/src/Server/src/main/java/greenify/server/service/UserService.java +++ b/src/Server/src/main/java/greenify/server/service/UserService.java @@ -9,8 +9,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.ResponseBody; + +import java.util.ArrayList; +import java.util.List; @Service public class UserService { @@ -70,20 +71,11 @@ public class UserService { public void addFriend(String name, String friend) { User user = userRepository.findByName(name); User add = userRepository.findByName(friend); - if (add == null) { + if (user == null || add == null ) { throw new ApplicationException("User does not exist"); } user.addFriend(add); - } - - /** - * Returns the friendlist of the user in JSON. - * @param name the username of the user - * @return a userDTO of the logged in user - */ - public String getLeaderboard(String name) { - User user = userRepository.findByName(name); - return user.friendsToString(); + userRepository.save(user); } /** @@ -101,8 +93,6 @@ public class UserService { && InputValidator.isValidItemValue(inputName, value)) { user.getFootPrintInputs().put(inputName, value); userRepository.save(user); - user.setFootPrint(calculatorService.calculateFootprint(user)); - userRepository.save(user); } else { throw new ApplicationException("Invalid input"); } @@ -125,11 +115,11 @@ public class UserService { } /** - * This method gets the footprint of a user. + * This method saves the footprint of a user. * @param name name of the user * @return footprint of the user */ - public Float getFootprint(String name) { + public Float saveFootprint(String name) { User user = userRepository.findByName(name); user.setFootPrint(calculatorService.calculateFootprint(user)); userRepository.save(user); @@ -137,12 +127,40 @@ public class UserService { } /** - * This method gets a JSON of XML with all users. - * @return JSON/XML of all users + * This method gets the footprint of a user. + * @param name name of the user + * @return footprint of the user */ - @GetMapping(path = "/all") - @ResponseBody - public Iterable getAllUsers() { - return userRepository.findAll(); + public Float getFootprint(String name) { + User user = userRepository.findByName(name); + return user.getFootPrint(); + } + + /** + * This method gets the friends of a user. + * @param name name of the user + * @return list of the friends + */ + public List getFriends(String name) { + List result = new ArrayList<>(); + User user = userRepository.findByName(name); + List friends = user.getFriends(); + for (User person : friends) { + result.add(person.getName()); + } + return result; + } + + /** + * This method gets the list of all users. + * @return list of all users + */ + public List getAllUsers() { + List result = new ArrayList<>(); + Iterable allUsers = userRepository.findAll(); + for (User person : allUsers) { + result.add(person.getName()); + } + return result; } } From b4a00fade28e0d6f72c48009cbb8904fb59f1f97 Mon Sep 17 00:00:00 2001 From: cugurlu Date: Sun, 31 Mar 2019 00:51:55 +0100 Subject: [PATCH 6/7] Add new tests --- .../greenify/server/data/model/UserTest.java | 19 ++++--- .../server/rest/UserControllerTest.java | 8 +++ .../server/service/UserServiceTest.java | 57 +++++++++++++++---- 3 files changed, 64 insertions(+), 20 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 b8bbabc..f95b2c4 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 @@ -9,7 +9,7 @@ import greenify.common.ApplicationException; import org.junit.Test; import java.util.ArrayList; -import java.util.Collection; +import java.util.List; public class UserTest { @@ -113,18 +113,21 @@ public class UserTest { }); } - @Test - public void friendsToStringTest() { - User first = new User(1L, "greenify", "password"); - User second = new User(1L, "merel", "password"); - first.addFriend(second); - assertEquals(first.friendsToString(), "friends=[{name=merel, footprint=0.0}]"); + public void addTwiceTest() { + User test = new User(1L, "greenify", "password"); + List friendList = new ArrayList<>(); + friendList.add(test); + User user = new User(1L, "green", "pass"); + user.setFriends(friendList); + assertThrows(ApplicationException.class, () -> { + user.addFriend(test); + }); } @Test public void setFriendTest() { - Collection friends = new ArrayList<>(); + List friends = new ArrayList(); User first = new User(1L, "greenify", "password"); User second = new User(1L, "merel", "password"); friends.add(second); 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 4b6c70e..101bf6f 100644 --- a/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java +++ b/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java @@ -94,6 +94,14 @@ public class UserControllerTest { assertEquals("merel", arg2Captor.getValue()); } + @Test + public void getAllUsersTest() throws Exception { + mvc.perform(get("/getAllUsers") + .accept(MediaType.APPLICATION_JSON)) + .andDo(print()) + .andExpect(status().isOk()); + } + @Test public void getInputTest() throws Exception { ArgumentCaptor arg1Captor = ArgumentCaptor.forClass(String.class); 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 a782034..89a7d61 100644 --- a/src/Server/src/test/java/greenify/server/service/UserServiceTest.java +++ b/src/Server/src/test/java/greenify/server/service/UserServiceTest.java @@ -8,8 +8,10 @@ import greenify.common.ApplicationException; import greenify.common.UserDto; import greenify.server.data.model.User; import greenify.server.data.repository.UserRepository; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.junit.jupiter.api.Assertions; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.TestConfiguration; @@ -18,6 +20,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.test.context.junit4.SpringRunner; import java.util.ArrayList; +import java.util.List; @RunWith(SpringRunner.class) public class UserServiceTest { @@ -118,7 +121,8 @@ public class UserServiceTest { when(calculatorService.calculateFootprint(alex)) .thenReturn(15f); userService.setInput("alex", "input_footprint_shopping_food_dairy_default", "6.5"); - assertEquals(15f, alex.getFootPrint(), 0.0); + Assert.assertTrue(alex.getFootPrintInputs() + .get("input_footprint_shopping_food_dairy_default").equals("6.5")); } @Test @@ -139,18 +143,51 @@ public class UserServiceTest { @Test public void getFootprintTest() { + User alex = new User(1L, "alex", "password"); + alex.setFootPrint(15F); + when(userRepository.findByName(alex.getName())) + .thenReturn(alex); + Assertions.assertEquals(15F, userService.getFootprint("alex")); + } + + @Test + public void saveFootprintTest() { User alex = new User(1L, "alex", "password"); when(userRepository.findByName(alex.getName())) .thenReturn(alex); when(calculatorService.calculateFootprint(alex)) .thenReturn(15f); - userService.setInput("alex", "input_footprint_shopping_food_dairy_default", "6.5"); - assertEquals(15f, userService.getFootprint("alex"), 0.0); + userService.saveFootprint("alex"); + Assertions.assertEquals(15f, userService.saveFootprint("alex")); + } + + @Test + public void getFriendsTest() { + User alex = new User(1L, "alex", "password"); + User lola = new User(2L, "lola", "pass"); + when(userRepository.findByName(alex.getName())) + .thenReturn(alex); + when(userRepository.findByName(lola.getName())) + .thenReturn(lola); + alex.addFriend(lola); + List friendList = new ArrayList<>(); + friendList.add("lola"); + assertEquals(friendList, userService.getFriends("alex")); } @Test public void getAllUserTest() { - assertEquals(userRepository.findAll(), userService.getAllUsers()); + User alex = new User(1L, "alex", "password"); + User lola = new User(2L, "lola", "pass"); + Iterable users = new ArrayList<>(); + ((ArrayList) users).add(alex); + ((ArrayList) users).add(lola); + when(userRepository.findAll()) + .thenReturn(users); + List userList = new ArrayList<>(); + userList.add("alex"); + userList.add("lola"); + assertEquals(userList, userService.getAllUsers()); } @Test @@ -170,16 +207,12 @@ public class UserServiceTest { } @Test - public void addFriendsExceptionTest() { - assertThrows(ApplicationException.class, () -> userService.addFriend("greenify", null)); + public void addFriendNullFriendTest() { + assertThrows(ApplicationException.class, () -> userService.addFriend("alex", null)); } @Test - public void leaderboardTest() { - User alex = userRepository.findByName("alex"); - User lola = userRepository.findByName("lola"); - userService.addFriend("alex", "lola"); - assertEquals(userService.getLeaderboard("alex"), "friends=[{name=lola, footprint=0.0}]"); - + public void addFriendNullUserTest() { + assertThrows(ApplicationException.class, () -> userService.addFriend(null, "password")); } } From 5449c664bdf21c056be77de9c9d2d77ff5d49917 Mon Sep 17 00:00:00 2001 From: cugurlu Date: Sun, 31 Mar 2019 00:54:50 +0100 Subject: [PATCH 7/7] Update README.md --- README.md | 45 ++++++++++++++++++--------------------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 87cc6a7..052cc92 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,5 @@ -## Getting your weekly reports - -**Jacoco**: -Run `maven install` ([Intellij](https://www.jetbrains.com/help/idea/2016.3/getting-started-with-maven.html#execute_maven_goal)/[Eclipse](http://imgur.com/a/6q7pV)) - -**Checkstyle**: -Run `maven site` - -# Merel Steenbergen (masteenbergen) -![Picture:](https://gitlab.ewi.tudelft.nl/cse1105/2018-2019/oopp-group-43/template/blob/master/Pictures%20Group%20Members/Merel_Steenbergen.jpg) +# Merel Steenbergen (masteenbergen) + ## Personal Development Plan @@ -19,26 +11,26 @@ everyone is keeping on track and if not, why is someone slacking? What are two of your weaker points? 1. I’m a bit of a control freak. Sometimes I find it hard to let other people do things and to mind my own business. 2. I’m competitive. This can be a good thing, because I’m very passionate and really give it 100% if I like something, but it can also lead to hurt feelings when I’m not paying attention. - -![Core quadrant:](https://gitlab.ewi.tudelft.nl/cse1105/2018-2019/oopp-group-43/template/blob/master/Pictures%20Group%20Members/Core_quadrant_Merel.png) -### G - Goal + + +### G - Goal I want to improve on letting other people handle things that I actually want to do myself. I also want to improve my programming and am aiming to not make it a competition, but to really make it a group project. This is important, because you almost never work alone in software developing, which is what I want to do after the studies. -### R - Reality (Your current situation) +### R - Reality (Your current situation) I’ve already been working on my teamwork skills since high school. I had a subject that had only projects. I’ve also worked with Scrum before, which really helps you focus on your development without letting your project out of sight. I’m better at working together and listening to tips than I was before. -### O - Options (Look at possible options) +### O - Options (Look at possible options) Actively watching myself and letting my teammates know that they need to be clear with me if they don’t like something I say or do are the best ways for me to improve. I would rather improve by myself, but for my goals it is better to let my team guide me. It’s about teamwork after all. -### W - Will (Motivation, plan of action) +### W - Will (Motivation, plan of action) I’m just going to keep working on myself and hope my teammates will work with me. It also helps to work in different teams to get a much more diverse experience. Every team is different, so I’m just going to wait what this team will bring me and how I will have to adjust myself to make the team efficient. # Sem van der Hoeven (4896726, semvanderhoeve) -![Picture location](https://gitlab.ewi.tudelft.nl/cse1105/2018-2019/oopp-group-43/template/blob/master/doc/Pictures%20Group%20Members/sem_van_der_hoeven.jpg) + ## Personal development plan -![Core quadrant location](https://gitlab.ewi.tudelft.nl/cse1105/2018-2019/oopp-group-43/template/blob/master/doc/Pictures%20Group%20Members/sem_van_der_hoeven_core_quadrant.png) + ### G - Goal My goal with this project is most of all to be able to efficiently work with a group of people. This is important to me because I have not worked on a (development) project with a (big) group of people before. I believe I have succeeded in this goal when our project is finished and we have got a passing grade. @@ -58,12 +50,11 @@ action plan: ⋅⋅* deliver things I need to do on time # Ceren Ugurlu (4851609, cugurlu) - -![My Picture](https://gitlab.ewi.tudelft.nl/cse1105/2018-2019/oopp-group-43/template/blob/master/Pictures%20Group%20Members/ceren_ugurlu.jpeg) + ## Personal Development Plan: -- This is my core quadrant: ![My Core Quadrant](https://gitlab.ewi.tudelft.nl/cse1105/2018-2019/oopp-group-43/template/blob/master/Pictures%20Group%20Members/Ceren_Ugurlu_Core_Quadrant.png) +- This is my core quadrant: My strong points: - I am an ambitious person in order to achieve my goal I can study a lot @@ -98,10 +89,10 @@ Action Plan: - While everyone is working on their part, to inform each other about the progress is very important because some parts can be connected. When two people's parts are connected they should act according to that otherwise it can lead problems or a non-compiling code) # Mika Wauben (4834739, mlwauben) -![My picture](https://gitlab.ewi.tudelft.nl/cse1105/2018-2019/oopp-group-43/template/blob/master/Pictures%20Group%20Members/mlwaubenpic.jpg) + ## Personal Development Plan -![My core quadrant](https://gitlab.ewi.tudelft.nl/cse1105/2018-2019/oopp-group-43/template/blob/master/Pictures%20Group%20Members/mlwaubenCoreQuadrant.jpg) + My strong points: 1. I am observing and think before I speak. @@ -111,14 +102,14 @@ Action Plan: 1. I like to do things alone. I rather figure something out myself instead of asking for help, even if this takes way too long. 2. I tend to stay quiet if I'm not absolutely sure my contribution is right or useful. -### G - Goal +### G - Goal My goal during this course is getting more experienced in working with things like github, libraries, scrumboard, etc. and asking for help if I need it, since being stuck on something isn’t going to help my groupmates. -### R - Reality +### R - Reality Every minute that we’re working on the project, I’m also actively working on my goal. -### O - Options +### O - Options By working on the project with the chosen programs, I’ll get more experienced in using them. Using those programs for other projects as well will only improve my knowledge. By being active in the groupchat and during the meetings, asking stuff will be easier. -### W - Will +### W - Will I will reach my goal by working actively together with my teammates using lots of the useful programs given.