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)
-
+# 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.
-
-
-### 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)
-
+
## Personal development plan
-
+
### 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)
-
-
+
## Personal Development Plan:
-- This is my core quadrant: 
+- 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)
-
+
## Personal Development Plan
-
+
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.
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/java/greenify/client/controller/DashBoardController.java b/src/Client/src/main/java/greenify/client/controller/DashBoardController.java
index 80feab6..ff9a565 100644
--- a/src/Client/src/main/java/greenify/client/controller/DashBoardController.java
+++ b/src/Client/src/main/java/greenify/client/controller/DashBoardController.java
@@ -28,12 +28,17 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import java.io.IOException;
+import java.util.List;
/**
* Class that controls the dashboard fxml file (the GUI Screen).
*/
@Controller
public class DashBoardController {
+ public static ObservableList data = FXCollections.observableArrayList();
+ public ObservableList friendLeaderData = FXCollections.observableArrayList();
+ public ObservableList globalLeaderData = FXCollections.observableArrayList();
+
@Autowired
UserService userService;
@@ -76,6 +81,18 @@ public class DashBoardController {
@FXML
private TableColumn scoreColumn;
@FXML
+ private TableView globalLeaderboard;
+ @FXML
+ private TableColumn globalUser;
+ @FXML
+ private TableColumn globalScore;
+ @FXML
+ private TableView friendLeaderboard;
+ @FXML
+ private TableColumn friendUser;
+ @FXML
+ private TableColumn friendScore;
+ @FXML
private PieChart pieChart;
@FXML
private Label usernameLabel;
@@ -83,7 +100,7 @@ public class DashBoardController {
/**
* Loads the the necessary things before anything else.
*/
- public void initialize() {
+ public void initialize() throws InterruptedException {
//sets the text of the 'welcome back' text to include the username
welcomebacktext.setText("Welcome back, " + userService.currentUser.getName() + "!");
//adds the slide transition to the menu bar
@@ -95,8 +112,11 @@ public class DashBoardController {
friendsButton.setSkin(new MyButtonSkin(friendsButton));
friendsColumn.setCellValueFactory(new PropertyValueFactory<>("Friend"));
scoreColumn.setCellValueFactory(new PropertyValueFactory<>("Score"));
- friendsTable.setItems(FriendController.getData());
- if(pieChart != null) {
+ globalUser.setCellValueFactory(new PropertyValueFactory<>("Friend"));
+ globalScore.setCellValueFactory(new PropertyValueFactory<>("Score"));
+ friendUser.setCellValueFactory(new PropertyValueFactory<>("Friend"));
+ friendScore.setCellValueFactory(new PropertyValueFactory<>("Score"));
+ if (pieChart != null) {
ObservableList pieChartData =
FXCollections.observableArrayList(
new PieChart.Data("Vegan Meal", 100),
@@ -110,6 +130,37 @@ public class DashBoardController {
pieChart.setMaxSize(1000, 1000);
pieChart.setData(pieChartData);
}
+ List friendList = userService.getFriendNames(userService.currentUser.getName());
+ for (int i = 0; i < friendList.size(); i++) {
+ Friend friend = new Friend(friendList.get(i),
+ userService.getFootprint(friendList.get(i)));
+ data.add(friend);
+ }
+ friendsTable.setItems(data);
+ updateLeaderboard();
+ }
+
+ /**
+ * Sorts the scores of users.
+ * @param users the list of users
+ */
+ public void sortScores(List users) throws InterruptedException {
+ for (int i = 0; i < users.size(); i++) {
+ for (int j = 0; j < users.size(); j++) {
+ Float firstScore = userService.getFootprint(users.get(i));
+ Float secondScore = userService.getFootprint(users.get(j));
+ if (i > j && firstScore < secondScore) {
+ String temp = users.get(i);
+ users.set(i, users.get(j));
+ users.set(j, temp);
+ }
+ if (i < j && firstScore > secondScore) {
+ String temp = users.get(i);
+ users.set(i, users.get(j));
+ users.set(j, temp);
+ }
+ }
+ }
}
/**
@@ -117,7 +168,6 @@ public class DashBoardController {
* @param node the node on which the transition needs to act
*/
public void addFadeTransition(Node node) {
-
fadeTrans = new FadeTransition(Duration.millis(400), node);
fadeTrans.setFromValue(0);
fadeTrans.setToValue(1.0);
@@ -129,14 +179,14 @@ public class DashBoardController {
* Displays the dashboard pane.
* @param event the event (clicking the button)
*/
- public void displayDashboard(ActionEvent event) {
+ public void displayDashboard(ActionEvent event) throws InterruptedException {
addFadeTransition(dashboardPane);
System.out.println("display dashboard");
dashboardPane.setVisible(true);
userPane.setVisible(false);
activitiesPane.setVisible(false);
friendsPane.setVisible(false);
-
+ updateLeaderboard();
}
/**
@@ -194,7 +244,7 @@ public class DashBoardController {
* Opens the calculator.
* @throws IOException if the Application doesn't load.
*/
- public void openCalculator() throws IOException {
+ public void openCalculator() throws IOException, InterruptedException {
Parent calc = Application.load(this.getClass().getClassLoader()
.getResource("fxml/calculator.fxml"));
Scene scene = new Scene(calc);
@@ -207,6 +257,10 @@ public class DashBoardController {
calcStage.show();
}
+ /**
+ * method opend addFriend scene.
+ * @throws IOException when file is not found
+ */
public void openAddFriend() throws IOException {
Parent calc = Application.load(this.getClass().getClassLoader()
.getResource("fxml/AddFriend.fxml"));
@@ -217,6 +271,37 @@ public class DashBoardController {
calcStage.show();
}
+ /**
+ * Leaderboard is updaating.
+ * @throws InterruptedException throws exception
+ */
+ public void updateLeaderboard() throws InterruptedException {
+ friendLeaderboard.getItems().clear();
+ globalLeaderboard.getItems().clear();
+ //global leaderboard
+ globalLeaderData.removeAll();
+ List userList = userService.getAllUsers();
+ sortScores(userList);
+ for (int j = 0; j < userList.size(); j++) {
+ Friend user = new Friend(userList.get(j), userService.getFootprint(userList.get(j)));
+ globalLeaderData.add(user);
+ }
+ globalLeaderboard.setItems(globalLeaderData);
+ // friend leaderboard
+ friendLeaderData.removeAll();
+ String name = userService.currentUser.getName();
+ List friendList = userService.getFriendNames(name);
+ friendList.add(name);
+ sortScores(friendList);
+ for (int i = 0; i < friendList.size(); i++) {
+ Friend friend = new Friend(friendList.get(i),
+ userService.getFootprint(friendList.get(i)));
+ friendLeaderData.add(friend);
+ }
+ friendLeaderboard.setItems(friendLeaderData);
+ }
+
+
//class for the animations on the navigation buttons
public class MyButtonSkin extends ButtonSkin {
/**
diff --git a/src/Client/src/main/java/greenify/client/controller/FriendController.java b/src/Client/src/main/java/greenify/client/controller/FriendController.java
index e364f4f..25992cb 100644
--- a/src/Client/src/main/java/greenify/client/controller/FriendController.java
+++ b/src/Client/src/main/java/greenify/client/controller/FriendController.java
@@ -2,11 +2,11 @@ package greenify.client.controller;
import greenify.client.Friend;
import greenify.client.rest.UserService;
-import javafx.collections.FXCollections;
-import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
-import javafx.scene.control.*;
+import javafx.scene.control.Alert;
+import javafx.scene.control.Button;
+import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.stage.Window;
import org.springframework.beans.factory.annotation.Autowired;
@@ -17,7 +17,8 @@ public class FriendController {
@Autowired
UserService userService;
- public static ObservableList data = FXCollections.observableArrayList();
+ @Autowired
+ DashBoardController controller;
@FXML
private Button addButton;
@@ -29,7 +30,7 @@ public class FriendController {
* @param event the click of the sign up button
*/
@FXML
- public void addFriend(ActionEvent event) {
+ public void addFriend(ActionEvent event) throws InterruptedException {
//set the window to the current window (for displaying the alerts)
Window owner = addButton.getScene().getWindow();
//check if the username field is empty
@@ -38,19 +39,28 @@ public class FriendController {
UserController.AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Username Error!",
"Please enter a username!");
return;
+ } else if (userNameText.getText().equals(userService.currentUser.getName())) {
+ UserController.AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Error!",
+ "Cannot add yourself as a friend!");
+ return;
+ } else if (userService.getFriendNames(userService.currentUser.getName())
+ .contains(userNameText.getText())) {
+ UserController.AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Error!",
+ "Cannot add a friend twice!");
+ return;
+ } else if (!userService.getAllUsers().contains(userNameText.getText())) {
+ UserController.AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Error!",
+ "The user does not exist!");
+ return;
}
//add friend to the current user
userService.addFriend(userService.currentUser.getName(), userNameText.getText());
Friend friend = new Friend(userNameText.getText(),
userService.getFootprint(userNameText.getText()));
- data.add(friend);
+ DashBoardController.data.add(friend);
//close the register window after the user has entered all the credentials
Stage current = (Stage) owner;
current.close();
+ controller.updateLeaderboard();
}
-
- public static ObservableList getData() {
- return data;
- }
-
}
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 cd2d035..f07534f 100644
--- a/src/Client/src/main/java/greenify/client/rest/UserService.java
+++ b/src/Client/src/main/java/greenify/client/rest/UserService.java
@@ -12,6 +12,8 @@ import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
+import java.util.List;
+
@Service
public class UserService {
public UserDto currentUser;
@@ -117,6 +119,42 @@ public class UserService {
return result;
}
+ /**
+ * Saves the footprint of the user.
+ * @param name name of the user
+ * @return returns the footprint score
+ */
+ @SuppressWarnings("Duplicates")
+ public Float saveFootprint(String name) {
+ HttpHeaders headers = new HttpHeaders();
+ headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
+ UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/saveFootprint")
+ .queryParam("name", name);
+ new HttpEntity<>(headers);
+ System.out.println(builder.build().encode().toUri());
+ Float result = this.restTemplate.getForObject(builder
+ .build().encode().toUri(), Float.class);
+ return result;
+ }
+
+ /**
+ * Gets the friend list of the user.
+ * @param name name of the user
+ * @return returns the friend list
+ */
+ @SuppressWarnings("Duplicates")
+ public List getFriendNames(String name) {
+ HttpHeaders headers = new HttpHeaders();
+ headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
+ UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/getFriends")
+ .queryParam("name", name);
+ new HttpEntity<>(headers);
+ System.out.println(builder.build().encode().toUri());
+ List result = this.restTemplate.getForObject(builder
+ .build().encode().toUri(), List.class);
+ return result;
+ }
+
/**
* Adds a friend to the user.
* @param name the username of the current user.
@@ -134,4 +172,19 @@ public class UserService {
ResponseEntity authenticateResponse = this.restTemplate.getForEntity(builder.build()
.encode().toUri(), String.class);
}
+
+ /**
+ * Gets the list of all users.
+ */
+ @SuppressWarnings("Duplicates")
+ public List getAllUsers() {
+ HttpHeaders headers = new HttpHeaders();
+ headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
+ UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/getAllUsers");
+ HttpEntity> entity = new HttpEntity<>(headers);
+ System.out.println(builder.build().encode().toUri());
+ List result = this.restTemplate.getForObject(builder
+ .build().encode().toUri(), List.class);
+ return result;
+ }
}
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 @@
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
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");
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
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;
}
}
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"));
}
}