diff --git a/.gitignore b/.gitignore
index 850b757..f909a04 100644
--- a/.gitignore
+++ b/.gitignore
@@ -19,7 +19,7 @@
# Edit at https://www.gitignore.io/?templates=java,maven,eclipse,intellij,visualstudiocode
### our project ###
-# src/**/.idea/
+src/**/.idea/
# src/**/.gradle/
diff --git a/CSE1105.checkstyle.xml b/CSE1105.checkstyle.xml
deleted file mode 100644
index d0fe4b0..0000000
--- a/CSE1105.checkstyle.xml
+++ /dev/null
@@ -1,188 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/build.gradle b/build.gradle
index 420b1e4..2b6ed72 100644
--- a/build.gradle
+++ b/build.gradle
@@ -16,45 +16,27 @@ apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
-apply plugin: 'jacoco'
-apply plugin: 'checkstyle'
apply plugin: 'application'
application {
- mainClassName = 'greenify.client.Application'
-}
-
-//apply plugin: 'org.openjfx.javafxplugin'
-
-tasks.withType(Checkstyle) {
- reports {
- html.destination rootProject.file("build/reports/checkstyle.html")
- }
+ mainClassName = 'greenify.server.Application'
}
repositories {
mavenCentral()
}
-//client bootjar
bootJar {
- baseName = 'gs-consuming-rest'
- version = '0.1.0'
+ enabled = false
}
-//server bootjar
-bootJar {
- baseName = 'gs-rest-service'
- version = '0.1.0'
+jar {
+ enabled = true
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
-test {
- useJUnitPlatform()
-}
-
dependencies {
compile("org.springframework.boot:spring-boot-starter")
compile("org.springframework:spring-web")
@@ -76,22 +58,6 @@ dependencies {
)
}
-jacoco {
- toolVersion = "0.8.3"
- reportsDir = file("$buildDir/customJacocoReportDir")
-}
-
-jacocoTestReport {
- reports {
- xml.enabled false
- csv.enabled false
- html.destination file("${buildDir}/jacocoHtml")
- }
-}
-
-task application(type: JavaExec, dependsOn: classes) {
- main = 'Cient.Application'
-}
diff --git a/doc/reports/sprint-reviews/20190225_sprint_review.md b/doc/reports/sprint-reviews/20190225_sprint_review.md
deleted file mode 100644
index 6adcb33..0000000
--- a/doc/reports/sprint-reviews/20190225_sprint_review.md
+++ /dev/null
@@ -1,12 +0,0 @@
-
-# Sprint Review
-
-## Main problems Encountered
-
-We have no problems so far. Everyone arrives on time and does the tasks assigned to them.
-
-## Adjustments from previous sprints
-Since this is the first review, there are no previous sprints yet
-
-## Adjustments for next sprint
- - Keep up the good work!
diff --git a/doc/reports/sprint-reviews/20190311_sprint_review.md b/doc/reports/sprint-reviews/20190311_sprint_review.md
deleted file mode 100644
index d4d255b..0000000
--- a/doc/reports/sprint-reviews/20190311_sprint_review.md
+++ /dev/null
@@ -1,14 +0,0 @@
-
-# Sprint Review
-
-## Main problems Encountered
-
-### Problem 1: Kristin left the group
-Kristin does not continue CSE and left our group. We'll work with five people from now on.
-
-## Adjustments from previous sprints
-We created the backlog before the sprint, which helps making clear what we're working on.
-
-## Adjustments for next sprint
- - Group made out of five
- - Backlogs ready on time
diff --git a/doc/sprints/sprint1/20190225_scrumboard.png b/doc/sprints/sprint1/20190403_scrumboard.png
similarity index 100%
rename from doc/sprints/sprint1/20190225_scrumboard.png
rename to doc/sprints/sprint1/20190403_scrumboard.png
diff --git a/doc/sprints/sprint2/20190311_scrumboard.png b/doc/sprints/sprint2/20191003_scrumboard.png
similarity index 100%
rename from doc/sprints/sprint2/20190311_scrumboard.png
rename to doc/sprints/sprint2/20191003_scrumboard.png
diff --git a/quality/checkstyle/checkstyle.xml b/quality/checkstyle/checkstyle.xml
new file mode 100644
index 0000000..905d05c
--- /dev/null
+++ b/quality/checkstyle/checkstyle.xml
@@ -0,0 +1,188 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/quality/checkstyle/suppression.xml b/quality/checkstyle/suppression.xml
new file mode 100644
index 0000000..32fd9c0
--- /dev/null
+++ b/quality/checkstyle/suppression.xml
@@ -0,0 +1,3 @@
+
+
+
\ No newline at end of file
diff --git a/src/.idea/checkstyle-idea.xml b/src/.idea/checkstyle-idea.xml
index ef7efd6..f51119c 100644
--- a/src/.idea/checkstyle-idea.xml
+++ b/src/.idea/checkstyle-idea.xml
@@ -3,10 +3,12 @@
+
+
+ add a v
+ add
+ sig
+ regis
+ greenify
+ userC
+
+
+
+
+
@@ -230,29 +277,111 @@
-
-
-
-
+
+
+
+
-
+
-
-
-
-
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -261,16 +390,21 @@
-
+
+
-
+
+
+
+
+
@@ -284,8 +418,8 @@
-
-
+
+
@@ -296,7 +430,7 @@
-
+
@@ -304,7 +438,7 @@
true
-
+
@@ -315,7 +449,7 @@
-
+
@@ -323,10 +457,10 @@
true
-
+
-
+
@@ -334,7 +468,7 @@
-
+
@@ -342,13 +476,43 @@
true
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ true
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ true
+
@@ -365,22 +529,13 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
@@ -398,35 +553,35 @@
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
-
-
+
+
@@ -441,68 +596,68 @@
-
-
+
+
-
-
+
+
-
-
+
+
-
-
-
-
+
+
+
+
-
+
-
-
-
-
+
+
+
+
-
+
-
+
-
+
-
-
-
-
+
+
+
+
-
-
+
+
-
-
-
-
+
+
+
+
@@ -510,127 +665,294 @@
-
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
-
-
+
+
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
@@ -639,15 +961,59 @@
-
-
+
+
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -656,48 +1022,8 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
diff --git a/src/Client/build.gradle b/src/Client/build.gradle
index 9b01f2b..61f1dea 100644
--- a/src/Client/build.gradle
+++ b/src/Client/build.gradle
@@ -13,32 +13,27 @@ apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'jacoco'
-//apply plugin: 'checkstyle'
-//
-//checkstyle {
-// version = '7.8.1'
-// config = 'checkstyle/checkstyle.xml' as File
-//}
-//
-//checkstyleMain {
-// source ='src/main/java'
-//}
-//
-//checkstyleTest {
-// source ='src/test/java'
-//}
-//
-//tasks.withType(Checkstyle) {
-// reports {
-// xml.enabled false
-// html.enabled true
-// html.stylesheet resources.text.fromFile('config/xsl/checkstyle-custom.xsl')
-// }
-//}
+apply plugin: 'checkstyle'
sourceCompatibility = 1.8
targetCompatibility = 1.8
+def configDir = "${project.rootDir}/quality"
+
+checkstyle {
+ toolVersion '7.8.1'
+ configFile file("$configDir/checkstyle/checkstyle.xml")
+ configProperties.checkstyleSuppressionsPath = file("$configDir/checkstyle/suppressions.xml").absolutePath
+}
+
+checkstyleMain {
+ source ='src/main/java'
+}
+
+checkstyleTest {
+ source ='src/test/java'
+}
+
test {
useJUnitPlatform()
}
diff --git a/src/Client/src/main/java/greenify/client/Application.java b/src/Client/src/main/java/greenify/client/Application.java
index dca6ad1..77ae0c0 100644
--- a/src/Client/src/main/java/greenify/client/Application.java
+++ b/src/Client/src/main/java/greenify/client/Application.java
@@ -10,51 +10,39 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
+import java.io.IOException;
+
@SpringBootApplication
public class Application extends javafx.application.Application {
- private ConfigurableApplicationContext springContext;
- private Parent rootNode;
- private FXMLLoader fxmlLoader;
+ private static ConfigurableApplicationContext springContext;
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) {
launch(args);
}
- // @Bean
- // public RestTemplate restTemplate(RestTemplateBuilder builder) {
- // return builder.build();
- // }
+ /**
+ * This method takes an url and return a parent.
+ * @param url which is being loaded.
+ * @return parent object.
+ */
+ public static Parent load(java.net.URL url) throws IOException {
+ FXMLLoader loader = new FXMLLoader();
+ loader.setControllerFactory(springContext::getBean);
+ loader.setLocation(url);
+ return loader.load();
+ }
@Override
public void init() throws Exception {
springContext = SpringApplication.run(Application.class);
- fxmlLoader = new FXMLLoader();
- fxmlLoader.setControllerFactory(springContext::getBean);
}
@Override
public void start(Stage primaryStage) throws Exception {
- fxmlLoader.setLocation(this.getClass().getClassLoader().getResource("fxml/sample.fxml"));
-
- // fxmlLoader.setLocation(
- // this.getClass().getClassLoader().getResource("fxml/dashboard.fxml")
- // );
-
-
- rootNode = fxmlLoader.load();
-
- // rootNode = FXMLLoader.load(
- // this.getClass().getClassLoader().getResource("fxml/sample.fxml")
- // );
-
- primaryStage.setTitle("GoGreen");
+ Parent rootNode = load(this.getClass().getClassLoader().getResource("fxml/sample.fxml"));
+ primaryStage.setTitle("Greenify");
Scene scene = new Scene(rootNode);
-
- // scene.getStylesheets().add(
- // getClass().getResource("stylesheets/dashboardStyle.css").toExternalForm()
- // );
-
primaryStage.setScene(scene);
primaryStage.show();
}
@@ -63,14 +51,4 @@ public class Application extends javafx.application.Application {
public void stop() {
springContext.stop();
}
-
- // @Bean
- // public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
- // return args -> {
- // User user = restTemplate.getForObject(
- // "http://localhost:8080/user", User.class);
- // log.info(user.toString());
- //
- // };
- // }
}
\ 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 75c4dd5..ff2cde2 100644
--- a/src/Client/src/main/java/greenify/client/controller/DashBoardController.java
+++ b/src/Client/src/main/java/greenify/client/controller/DashBoardController.java
@@ -5,32 +5,29 @@ import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
-import javafx.scene.effect.DropShadow;
import javafx.scene.layout.AnchorPane;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+@Controller
public class DashBoardController {
@Autowired
UserService userService;
- @FXML
- public AnchorPane menuBar;
- public AnchorPane dashboardPane;
- public AnchorPane userPane;
- public AnchorPane activitiesPane;
- public Label welcomebacktext;
- // public Button addActivityButton;
- // public ComboBox addActivity;
+ private int count = 0;
@FXML
- public Label dashboardText;
- public Label activitiesText;
- public Label userText;
- public Button dashboardButton;
- public Button activitiesButton;
- public Button userButton;
-
- DropShadow shadow = new DropShadow();
+ private AnchorPane menuBar;
+ private AnchorPane dashboardPane;
+ private AnchorPane userPane;
+ private AnchorPane activitiesPane;
+ private Label welcomebacktext;
+ private Button dashboardButton;
+ private Button activitiesButton;
+ private Button userButton;
+ private Button veganMealButton;
+ private Label counter;
+ private Label scoreField;
/**
* displays the dashboard pane.
@@ -64,13 +61,17 @@ public class DashBoardController {
userPane.setVisible(true);
activitiesPane.setVisible(false);
}
- // public void addShadow(MouseEvent event) {
- // userButton.setEffect(shadow);
- // }
- //
- // public void removeShadow(MouseEvent event) {
- // userButton.setEffect(null);
- //
- // }
+ /**
+ * adds a vegetarian meal.
+ * @param event the event (clicking the button)
+ */
+ public void addVeganMeal(ActionEvent event) {
+ count++;
+ counter.setText("Count: " + count);
+ System.out.println(userService);
+ userService.addVeganMeal(userService.currentUser.getId(),
+ userService.currentUser.getName());
+ System.out.println("Vegetarian meal is added");
+ }
}
diff --git a/src/Client/src/main/java/greenify/client/controller/RegisterWindowController.java b/src/Client/src/main/java/greenify/client/controller/RegisterWindowController.java
new file mode 100644
index 0000000..df99252
--- /dev/null
+++ b/src/Client/src/main/java/greenify/client/controller/RegisterWindowController.java
@@ -0,0 +1,68 @@
+package greenify.client.controller;
+
+import greenify.client.rest.UserService;
+import javafx.event.ActionEvent;
+import javafx.fxml.FXML;
+import javafx.scene.control.Alert;
+import javafx.scene.control.Button;
+import javafx.scene.control.PasswordField;
+import javafx.scene.control.TextField;
+import javafx.stage.Stage;
+import javafx.stage.Window;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+
+@Controller
+public class RegisterWindowController {
+
+ @Autowired
+ UserService userService;
+
+ @FXML
+ private TextField userNameText;
+ @FXML
+ private PasswordField passwordField;
+ @FXML
+ private PasswordField passwordField2;
+ @FXML
+ private Button signupButton;
+
+ /**
+ * signs the user up.
+ * @param event the click of the signup button
+ */
+ @FXML
+ public void handleSignUpButton(ActionEvent event) {
+ //set the window to the current window (for displaying the alerts)
+ Window owner = signupButton.getScene().getWindow();
+ //check if the username field is empty
+ if (userNameText.getText().isEmpty()) {
+ //if so, display an alert
+ UserController.AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Username Error!",
+ "Please enter a username!");
+ return;
+ }
+
+ //check if the password field is empty
+ if (passwordField.getText().isEmpty()) {
+ //if so, display an alert
+ UserController.AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Password Error!",
+ "Please enter a password!");
+ return;
+ }
+
+ //check if the two password fields are equal
+ if (!passwordField.getText().equals(passwordField2.getText())) {
+ //if not, display an alert
+ UserController.AlertHelper.showAlert(Alert.AlertType.ERROR, owner, "Password Error!",
+ "Please make sure the passwords entered are the same!");
+ return;
+ }
+
+ userService.registerUser(userNameText.getText(), passwordField.getText());
+
+ //close the register window after the user has entered all the credentials
+ Stage current = (Stage) owner;
+ current.close();
+ }
+}
diff --git a/src/Client/src/main/java/greenify/client/controller/UserController.java b/src/Client/src/main/java/greenify/client/controller/UserController.java
index 3c58473..bec5ade 100644
--- a/src/Client/src/main/java/greenify/client/controller/UserController.java
+++ b/src/Client/src/main/java/greenify/client/controller/UserController.java
@@ -1,9 +1,9 @@
package greenify.client.controller;
+import greenify.client.Application;
import greenify.client.rest.UserService;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
-import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
@@ -24,29 +24,10 @@ public class UserController {
@FXML
private TextField usernameField;
-
- @FXML
private PasswordField passwordField;
-
- @FXML
private Button loginButton;
-
- @FXML
private Button signupButton;
-
- // @Value("${my.url}")
- // private String myUrl;
-
- // @FXML
- // private void initialize(ActionEvent event) throws IOException {
- // Parent parent = FXMLLoader.load(getClass().getResource("sample.fxml"));
- // Scene scene = new Scene(parent);
- // Stage app_stage = (Stage)((Node) event.getSource()).getScene().getWindow();
- // app_stage.setScene(scene);
- // app_stage.show();
- // }
-
@FXML
protected void handleLoginButtonAction(ActionEvent event) throws IOException {
Window owner = loginButton.getScene().getWindow();
@@ -55,7 +36,6 @@ public class UserController {
"Please enter your username");
return;
} else {
- // newUser.setUsername(usernameField.getText());
System.out.println("Username is " + usernameField.getText());
}
if (passwordField.getText().isEmpty()) {
@@ -63,22 +43,11 @@ public class UserController {
"Please enter a password");
return;
} else {
- // newUser.setPassword(passwordField.getText());
System.out.println("Password is " + passwordField.getText());
}
-
- userService.registerUser(usernameField.getText(), passwordField.getText());
-
- // load the dashboard stage
- // Parent parent = FXMLLoader.load(
- // this.getClass().getClassLoader().getResource("/fxml/dashboard.fxml")
- // );
- //
- // Scene scene = new Scene(parent);
- // Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
- // app_stage.setScene(scene);
- // app_stage.setFullScreen(true);
- // app_stage.show();
+ userService.loginUser(usernameField.getText(), passwordField.getText());
+ Stage current = (Stage) owner;
+ current.close();
openDashboard();
}
@@ -89,17 +58,16 @@ public class UserController {
* @author sem
*/
public void openDashboard() throws IOException {
- Parent dash = FXMLLoader.load(
- this.getClass().getClassLoader().getResource("fxml/Dashboard.fxml")
- );
+ Parent dash = Application.load(this.getClass().getClassLoader()
+ .getResource("fxml/dashboard.fxml"));
Scene scene = new Scene(dash);
+ scene.getStylesheets().add(getClass().getClassLoader()
+ .getResource("stylesheets/dashboardStyle.css").toExternalForm());
Stage appStage = new Stage();
appStage.setScene(scene);
- // app_stage.setFullScreen(true);
appStage.show();
}
-
public static class AlertHelper {
/**
* alerts for the login screen.
@@ -121,4 +89,19 @@ public class UserController {
}
}
+ /**
+ * The method handles register button.
+ * @param event User clicks to the button
+ * @throws Exception when the file couldn't find
+ */
+ public void handleRegisterButtonAction(ActionEvent event) throws Exception {
+ Parent registerWindow = Application.load(this.getClass().getClassLoader()
+ .getResource("fxml/RegisterWindow.fxml"));
+ Scene registerScene = new Scene(registerWindow);
+ Stage registerStage = new Stage();
+ registerStage.setScene(registerScene);
+ registerStage.setTitle("Enter register credentials");
+ registerStage.show();
+ }
+
}
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 1423f9c..fda9524 100644
--- a/src/Client/src/main/java/greenify/client/rest/UserService.java
+++ b/src/Client/src/main/java/greenify/client/rest/UserService.java
@@ -1,21 +1,25 @@
package greenify.client.rest;
-import greenify.common.UserDTO;
+import greenify.common.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
-import org.springframework.http.*;
-import org.springframework.stereotype.Component;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
-@Component
+@Service
public class UserService {
@Autowired
RestTemplate restTemplate;
+ public UserDto currentUser;
+
@Bean
- public RestTemplate restTemplate(RestTemplateBuilder builder) {
+ RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@@ -25,7 +29,7 @@ public class UserService {
* @param password the password of the user
* @return a userDTO
*/
- public UserDTO registerUser(String name, String password) {
+ public UserDto registerUser(String name, String password) {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/registerUser")
@@ -33,6 +37,46 @@ public class UserService {
.queryParam("password", password);
HttpEntity> entity = new HttpEntity<>(headers);
System.out.println(builder.build().encode().toUri());
- return this.restTemplate.getForObject(builder.build().encode().toUri(), UserDTO.class);
+ UserDto result = this.restTemplate.getForObject(builder.build()
+ .encode().toUri(), UserDto.class);
+ this.currentUser = result;
+ return result;
+ }
+
+ /**
+ * sign ins the user.
+ * @param name the username of the user
+ * @param password the password of the user
+ * @return a userDTO
+ */
+ public UserDto loginUser(String name, String password) {
+ HttpHeaders headers = new HttpHeaders();
+ headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
+ UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/loginUser")
+ .queryParam("name", name)
+ .queryParam("password", password);
+ HttpEntity> entity = new HttpEntity<>(headers);
+ System.out.println(builder.build().encode().toUri());
+ UserDto result = this.restTemplate.getForObject(builder.build()
+ .encode().toUri(), UserDto.class);
+ this.currentUser = result;
+ return result;
+ }
+
+ /**
+ * a user adds vegan meal.
+ * @param id the id of the user
+ * @param name the username of the user
+ * @return a userDTO
+ */
+ public UserDto addVeganMeal(Long id, String name) {
+ HttpHeaders headers = new HttpHeaders();
+ headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
+ UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/addVeganMeal")
+ .queryParam("id", id)
+ .queryParam("name", name);
+ HttpEntity> entity = new HttpEntity<>(headers);
+ System.out.println(builder.build().encode().toUri());
+ return this.restTemplate.getForObject(builder.build().encode().toUri(), UserDto.class);
}
}
diff --git a/src/Client/src/main/resources/addActivity1.png b/src/Client/src/main/resources/addActivity1.png
new file mode 100644
index 0000000..85af03c
Binary files /dev/null and b/src/Client/src/main/resources/addActivity1.png differ
diff --git a/src/Client/src/main/resources/addActivity2.png b/src/Client/src/main/resources/addActivity2.png
new file mode 100644
index 0000000..48d85d9
Binary files /dev/null and b/src/Client/src/main/resources/addActivity2.png differ
diff --git a/src/Client/src/main/resources/application.properties b/src/Client/src/main/resources/application.properties
index e69de29..ff55ac5 100644
--- a/src/Client/src/main/resources/application.properties
+++ b/src/Client/src/main/resources/application.properties
@@ -0,0 +1 @@
+logging.level.org.springframework.beans.factory=DEBUG
\ No newline at end of file
diff --git a/src/Client/src/main/resources/fxml/RegisterWindow.fxml b/src/Client/src/main/resources/fxml/RegisterWindow.fxml
new file mode 100644
index 0000000..4b3a472
--- /dev/null
+++ b/src/Client/src/main/resources/fxml/RegisterWindow.fxml
@@ -0,0 +1,44 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/Client/src/main/resources/fxml/dashboard.fxml b/src/Client/src/main/resources/fxml/dashboard.fxml
index 317b873..1627774 100644
--- a/src/Client/src/main/resources/fxml/dashboard.fxml
+++ b/src/Client/src/main/resources/fxml/dashboard.fxml
@@ -1,38 +1,37 @@
-
-
-
-
-
+
+
-
-
-
+
+
+
-
+
-
@@ -41,6 +40,16 @@
+
+
+
+
+
+
+
+
+
+
@@ -49,6 +58,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -58,18 +81,18 @@
-
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
diff --git a/src/Client/src/main/resources/fxml/sample.fxml b/src/Client/src/main/resources/fxml/sample.fxml
index 27dd987..ea80989 100644
--- a/src/Client/src/main/resources/fxml/sample.fxml
+++ b/src/Client/src/main/resources/fxml/sample.fxml
@@ -1,18 +1,13 @@
-
-
-
-
+
-
-
-
-
+
+
-
+
@@ -21,19 +16,19 @@
-
-
+
+
-
+
-
+
-
+
diff --git a/src/Client/src/main/resources/registerBackground.png b/src/Client/src/main/resources/registerBackground.png
new file mode 100644
index 0000000..b98cca1
Binary files /dev/null and b/src/Client/src/main/resources/registerBackground.png differ
diff --git a/src/Client/src/main/resources/stylesheets/Big Designer.ttf b/src/Client/src/main/resources/stylesheets/Big Designer.ttf
new file mode 100644
index 0000000..d833857
Binary files /dev/null and b/src/Client/src/main/resources/stylesheets/Big Designer.ttf differ
diff --git a/src/Client/src/main/resources/stylesheets/DesignioRegular.otf b/src/Client/src/main/resources/stylesheets/DesignioRegular.otf
new file mode 100644
index 0000000..4668fea
Binary files /dev/null and b/src/Client/src/main/resources/stylesheets/DesignioRegular.otf differ
diff --git a/src/Client/src/main/resources/stylesheets/dashboardStyle.css b/src/Client/src/main/resources/stylesheets/dashboardStyle.css
index e10deb1..72cc665 100644
--- a/src/Client/src/main/resources/stylesheets/dashboardStyle.css
+++ b/src/Client/src/main/resources/stylesheets/dashboardStyle.css
@@ -1,5 +1,31 @@
-.button {
- -fx-border-width: 0px 0px 1px 0px;
- -fx-border-color: #f9f9f9;
- -fx-border-radius: 0%;
-}
\ No newline at end of file
+@font-face {
+ font-family: 'Designio Regular ';
+ src: url('stylesheets/DesignioRegular.otf');
+}
+
+.root {
+ -fx-background-color: #f9fffb;
+ -fx-font-family: "Big Designer";
+}
+
+#dashboardButton {
+ -fx-background-color: #5a635c;
+
+}
+#dashboardButton:pressed {
+ -fx-background-color: #b7e2c2;
+}
+
+#userButton {
+ -fx-background-color: #5a635c;
+}
+#userButton:pressed {
+ -fx-background-color: #b7e2c2;
+}
+
+#activitiesButton {
+ -fx-background-color: #5a635c;
+}
+#activitiesButton:pressed {
+ -fx-background-color: #b7e2c2;
+}
diff --git a/src/Client/src/test/java/ApplicationTest.java b/src/Client/src/test/java/ApplicationTest.java
index 32c7f26..e69de29 100644
--- a/src/Client/src/test/java/ApplicationTest.java
+++ b/src/Client/src/test/java/ApplicationTest.java
@@ -1,16 +0,0 @@
-import greenify.client.Application;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.springframework.test.context.junit4.SpringRunner;
-
-@RunWith(SpringRunner.class)
-public class ApplicationTest {
- @Test
- public void applicationContextLoaded() {
- }
-
- @Test
- public void applicationContextTest() {
- Application.main(new String[] {});
- }
-}
\ No newline at end of file
diff --git a/src/Client/src/test/java/UserServiceTest.java b/src/Client/src/test/java/UserServiceTest.java
index 46ba840..034d840 100644
--- a/src/Client/src/test/java/UserServiceTest.java
+++ b/src/Client/src/test/java/UserServiceTest.java
@@ -1,9 +1,12 @@
import greenify.client.rest.UserService;
-import greenify.common.UserDTO;
+import greenify.common.UserDto;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.mockito.*;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -21,13 +24,33 @@ public class UserServiceTest {
UserService userService;
@Test
- public void mocking() throws Exception {
- UserDTO testUser = new UserDTO(1L, "Eric");
+ public void userRegisterTest() throws Exception {
+ UserDto testUser = new UserDto(1L, "Eric");
Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/registerUser?name=Eric&password=password"),
- UserDTO.class))
+ UserDto.class))
.thenReturn(testUser);
- UserDTO user = userService.registerUser("Eric", "password");
+ UserDto user = userService.registerUser("Eric", "password");
+ Assert.assertEquals(testUser, user);
+ }
+
+ @Test
+ public void userLoginTest() throws Exception {
+ UserDto testUser = new UserDto(1L, "Eric");
+ Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/loginUser?name=Eric&password=password"),
+ UserDto.class))
+ .thenReturn(testUser);
+ UserDto user = userService.loginUser("Eric", "password");
+ Assert.assertEquals(testUser, user);
+ }
+
+ @Test
+ public void addVeganMealTest() throws Exception {
+ UserDto testUser = new UserDto(1L, "Eric");
+ Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/addVeganMeal?id=1&name=Eric"),
+ UserDto.class))
+ .thenReturn(testUser);
+ UserDto user = userService.addVeganMeal(1L, "Eric");
Assert.assertEquals(testUser, user);
}
}
diff --git a/src/Common/build.gradle b/src/Common/build.gradle
index 9f4c6c0..f43cd70 100644
--- a/src/Common/build.gradle
+++ b/src/Common/build.gradle
@@ -11,6 +11,7 @@ apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'jacoco'
+apply plugin: 'checkstyle'
sourceCompatibility = 1.8
targetCompatibility = 1.8
@@ -19,6 +20,22 @@ repositories {
mavenCentral()
}
+def configDir = "${project.rootDir}/quality"
+
+checkstyle {
+ toolVersion '7.8.1'
+ configFile file("$configDir/checkstyle/checkstyle.xml")
+ configProperties.checkstyleSuppressionsPath = file("$configDir/checkstyle/suppressions.xml").absolutePath
+}
+
+checkstyleMain {
+ source ='src/main/java'
+}
+
+checkstyleTest {
+ source ='src/test/java'
+}
+
dependencies {
testCompile("junit:junit")
testCompile(
@@ -42,4 +59,4 @@ jacocoTestReport {
csv.enabled false
html.destination file("${buildDir}/jacocoHtml")
}
-}
\ No newline at end of file
+}
diff --git a/src/Common/src/main/java/greenify/common/ErrorResponse.java b/src/Common/src/main/java/greenify/common/ErrorResponse.java
index 648fc99..4acb747 100644
--- a/src/Common/src/main/java/greenify/common/ErrorResponse.java
+++ b/src/Common/src/main/java/greenify/common/ErrorResponse.java
@@ -7,6 +7,8 @@ public class ErrorResponse {
this.message = message;
}
+ public ErrorResponse() { }
+
public String getMessage() {
return message;
}
@@ -14,4 +16,4 @@ public class ErrorResponse {
public void setMessage(String message) {
this.message = message;
}
-}
+}
\ No newline at end of file
diff --git a/src/Common/src/main/java/greenify/common/UserDTO.java b/src/Common/src/main/java/greenify/common/UserDTO.java
deleted file mode 100644
index ba07182..0000000
--- a/src/Common/src/main/java/greenify/common/UserDTO.java
+++ /dev/null
@@ -1,22 +0,0 @@
-package greenify.common;
-
-public class UserDTO {
- private Long id;
- private String name;
-
- public UserDTO() {
- }
-
- public UserDTO(Long id, String name) {
- this.id = id;
- this.name = name;
- }
-
- public String getName() {
- return name;
- }
-
- public Long getId() {
- return id;
- }
-}
\ No newline at end of file
diff --git a/src/Common/src/main/java/greenify/common/UserDto.java b/src/Common/src/main/java/greenify/common/UserDto.java
new file mode 100644
index 0000000..8c67cd2
--- /dev/null
+++ b/src/Common/src/main/java/greenify/common/UserDto.java
@@ -0,0 +1,34 @@
+package greenify.common;
+
+// DTO stands for Data Transfer Object.
+// is an object that carries data between processes.
+// The motivation for its use is that communication between processes is usually done
+// resorting to remote interfaces (e.g., web services), where each call is an expensive operation.
+public class UserDto {
+ private Long id;
+ private String name;
+
+ public UserDto() {
+ }
+
+ public UserDto(Long id, String name) {
+ this.id = id;
+ this.name = name;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+}
\ No newline at end of file
diff --git a/src/Common/src/test/java/ErrorResponseTest.java b/src/Common/src/test/java/ErrorResponseTest.java
new file mode 100644
index 0000000..350e71f
--- /dev/null
+++ b/src/Common/src/test/java/ErrorResponseTest.java
@@ -0,0 +1,24 @@
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import greenify.common.ErrorResponse;
+
+import org.junit.Test;
+
+public class ErrorResponseTest {
+ @Test
+ public void setAndGetTest() {
+ ErrorResponse response = new ErrorResponse("New error");
+ ErrorResponse testResponse = new ErrorResponse();
+ testResponse.setMessage("New error");
+ assertTrue(response.getMessage().equals("New error"));
+ }
+
+ @Test
+ public void equalsTest() {
+ ErrorResponse first = new ErrorResponse("New error");
+ ErrorResponse second = new ErrorResponse("New error");
+ assertEquals(first.getMessage(), second.getMessage());
+ assertTrue(first.getMessage().equals(second.getMessage()));
+ }
+}
\ No newline at end of file
diff --git a/src/Common/src/test/java/UserDtoTest.java b/src/Common/src/test/java/UserDtoTest.java
new file mode 100644
index 0000000..1d1d75b
--- /dev/null
+++ b/src/Common/src/test/java/UserDtoTest.java
@@ -0,0 +1,25 @@
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import greenify.common.UserDto;
+import org.junit.Test;
+
+public class UserDtoTest {
+ @Test
+ public void setAndGetTest() {
+ UserDto user = new UserDto(1L, "greenify");
+ UserDto testUser = new UserDto();
+ testUser.setId(1L);
+ testUser.setName("greenify");
+ assertTrue(user.getId() == 1L);
+ assertEquals(user.getName(), "greenify");
+ }
+
+ @Test
+ public void equalsTest() {
+ UserDto first = new UserDto(1L, "greenify");
+ UserDto second = new UserDto(1L, "greenify");
+ assertEquals(first.getId(), second.getId());
+ assertEquals(first.getName(), second.getName());
+ }
+}
\ No newline at end of file
diff --git a/src/Server/build.gradle b/src/Server/build.gradle
index 50aeedd..6968ab5 100644
--- a/src/Server/build.gradle
+++ b/src/Server/build.gradle
@@ -15,13 +15,6 @@ apply plugin: 'io.spring.dependency-management'
apply plugin: 'jacoco'
apply plugin: 'checkstyle'
-
-tasks.withType(Checkstyle) {
- reports {
- html.destination rootProject.file("build/reports/checkstyle.html")
- }
-}
-
bootJar {
baseName = 'gs-rest-service'
version = '0.1.0'
@@ -39,14 +32,33 @@ repositories {
sourceCompatibility = 1.8
targetCompatibility = 1.8
+def configDir = "${project.rootDir}/quality"
+
+checkstyle {
+ toolVersion '7.8.1'
+ configFile file("$configDir/checkstyle/checkstyle.xml")
+ configProperties.checkstyleSuppressionsPath = file("$configDir/checkstyle/suppressions.xml").absolutePath
+}
+
+checkstyleMain {
+ source ='src/main/java'
+}
+
+checkstyleTest {
+ source ='src/test/java'
+}
+
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile('org.springframework.boot:spring-boot-starter-test')
+ testCompile('org.springframework.boot:spring-boot-test')
+ compile('org.springframework.boot:spring-boot-devtools')
compile project(path: ':src:Common')
compileOnly 'org.projectlombok:lombok:1.18.6'
annotationProcessor 'org.projectlombok:lombok:1.18.6'
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("com.h2database:h2")
+ compile("org.springframework.boot:spring-boot-starter-actuator")
testCompile(
'junit:junit:4.12',
'org.junit.jupiter:junit-jupiter-api:5.4.0'
@@ -55,6 +67,7 @@ dependencies {
'org.junit.jupiter:junit-jupiter-engine:5.4.0',
'org.junit.vintage:junit-vintage-engine:5.4.0'
)
+ compile 'javax.xml.bind:jaxb-api:2.3.0'
}
jacoco {
@@ -70,8 +83,3 @@ jacocoTestReport {
}
}
-checkstyle {
- toolVersion = "7.6.1"
-}
-
-
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 d588eb1..fd9daae 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
@@ -49,7 +49,9 @@ public class User {
return id;
}
- public void setId(Long id) { this.id = id; }
+ public void setId(Long id) {
+ this.id = id;
+ }
/**
* gets the name.
@@ -59,7 +61,9 @@ public class User {
return name;
}
- public void setName(String name) { this.name = name; }
+ public void setName(String name) {
+ this.name = name;
+ }
/**
* gets the password.
diff --git a/src/Server/src/main/java/greenify/server/data/repository/UserJpaRepository.java b/src/Server/src/main/java/greenify/server/data/repository/UserJpaRepository.java
deleted file mode 100644
index e31ad11..0000000
--- a/src/Server/src/main/java/greenify/server/data/repository/UserJpaRepository.java
+++ /dev/null
@@ -1,8 +0,0 @@
-package greenify.server.data.repository;
-
-import greenify.server.data.model.User;
-import org.springframework.data.jpa.repository.JpaRepository;
-
-public interface UserJpaRepository extends UserRepository, JpaRepository {
-
-}
\ No newline at end of file
diff --git a/src/Server/src/main/java/greenify/server/data/repository/UserRepository.java b/src/Server/src/main/java/greenify/server/data/repository/UserRepository.java
index e98f956..1c745f8 100644
--- a/src/Server/src/main/java/greenify/server/data/repository/UserRepository.java
+++ b/src/Server/src/main/java/greenify/server/data/repository/UserRepository.java
@@ -1,8 +1,11 @@
package greenify.server.data.repository;
import greenify.server.data.model.User;
+import org.springframework.data.repository.CrudRepository;
-public interface UserRepository {
+public interface UserRepository extends CrudRepository {
User findByName(String name);
+
T save(T user);
}
+
diff --git a/src/Server/src/main/java/greenify/server/rest/MainController.java b/src/Server/src/main/java/greenify/server/rest/MainController.java
new file mode 100644
index 0000000..b985819
--- /dev/null
+++ b/src/Server/src/main/java/greenify/server/rest/MainController.java
@@ -0,0 +1,38 @@
+package greenify.server.rest;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+import greenify.server.data.model.User;
+import greenify.server.data.repository.UserRepository;
+
+@Controller // This means that this class is a Controller
+@RequestMapping(path="/demo") // This means URL's start with /demo (after Application path)
+public class MainController {
+ @Autowired // This means to get the bean called userRepository
+ // Which is auto-generated by Spring, we will use it to handle the data
+ private UserRepository userRepository;
+
+ @GetMapping(path="/add") // Map ONLY GET Requests
+ public @ResponseBody String addNewUser (@RequestParam String name
+ , @RequestParam String password) {
+ // @ResponseBody means the returned String is the response, not a view name
+ // @RequestParam means it is a parameter from the GET or POST request
+
+ User n = new User();
+ n.setName(name);
+ n.setPassword(password);
+ userRepository.save(n);
+ return "Saved";
+ }
+
+ @GetMapping(path="/all")
+ public @ResponseBody Iterable getAllUsers() {
+ // This returns a JSON or XML with the users
+ return userRepository.findAll();
+ }
+}
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 c80288a..5918c63 100644
--- a/src/Server/src/main/java/greenify/server/rest/UserController.java
+++ b/src/Server/src/main/java/greenify/server/rest/UserController.java
@@ -1,10 +1,14 @@
package greenify.server.rest;
-import greenify.common.UserDTO;
+import greenify.common.UserDto;
+import greenify.server.data.model.User;
+import greenify.server.data.repository.UserRepository;
import greenify.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
@@ -12,15 +16,32 @@ public class UserController {
@Autowired
UserService userService;
+ @Autowired // This means to get the bean called userRepository
+ // Which is auto-generated by Spring, we will use it to handle the data
+ UserRepository userRepository;
+
@RequestMapping("/registerUser")
- public UserDTO registerUser(@RequestParam(value = "name") String name,
+ public UserDto registerUser(@RequestParam(value = "name") String name,
@RequestParam(value = "password") String password) {
return userService.registerUser(name, password);
}
- @RequestMapping("/login")
- public UserDTO login(@RequestParam(value = "name") String name,
- @RequestParam(value = "password") String password) {
- return userService.login(name, password);
+ @RequestMapping("/loginUser")
+ public UserDto loginUser(@RequestParam(value = "name") String name,
+ @RequestParam(value = "password") String password) {
+ return userService.loginUser(name, password);
+ }
+
+ @RequestMapping("/addVeganMeal")
+ public void addVeganMeal(@RequestParam(value = "id") Long id,
+ @RequestParam(value = "name") String name) {
+ userService.addVeganMeal(id, name);
+ }
+
+ @GetMapping(path = "/all")
+ @ResponseBody
+ public Iterable getAllUsers() {
+ // This returns a JSON or XML with the users
+ return userRepository.findAll();
}
}
\ No newline at end of file
diff --git a/src/Server/src/main/java/greenify/server/service/UserService.java b/src/Server/src/main/java/greenify/server/service/UserService.java
index a844a1d..0d06526 100644
--- a/src/Server/src/main/java/greenify/server/service/UserService.java
+++ b/src/Server/src/main/java/greenify/server/service/UserService.java
@@ -1,7 +1,7 @@
package greenify.server.service;
import greenify.common.ApplicationException;
-import greenify.common.UserDTO;
+import greenify.common.UserDto;
import greenify.server.data.model.User;
import greenify.server.data.repository.UserRepository;
import org.slf4j.Logger;
@@ -21,15 +21,15 @@ public class UserService {
* @param password the password of the user
* @return a userDTO of the registered user
*/
- public UserDTO registerUser(String name, String password) {
+ public UserDto registerUser(String name, String password) {
User user = userRepository.findByName(name);
- if (user != null) {
- throw new ApplicationException("User already exists");
- } else {
+ if (user == null) {
user = userRepository.save(new User(null, name, password, 0));
+ } else {
+ throw new ApplicationException("User already exists");
}
logger.info("Created user id=" + user.getId() + ", name=" + user.getName());
- return new UserDTO(user.getId(), user.getName());
+ return new UserDto(user.getId(), user.getName());
}
/**
@@ -38,7 +38,7 @@ public class UserService {
* @param password the password of the user
* @return a userDTO of the logged in user
*/
- public UserDTO login(String name, String password) {
+ public UserDto loginUser(String name, String password) {
User user = userRepository.findByName(name);
if (user == null) {
throw new ApplicationException("User does not exist");
@@ -47,6 +47,22 @@ public class UserService {
throw new ApplicationException("Wrong password");
}
}
- return new UserDTO(user.getId(), user.getName());
+ return new UserDto(user.getId(), user.getName());
+ }
+
+ /**
+ * add vegan meal to the user.
+ * @param id the id of the user
+ * @param name the name of the user
+ */
+ public void addVeganMeal(Long id, String name) {
+ User user = userRepository.findByName(name);
+ int count = user.getVeganMeal();
+ count++;
+ user.setVeganMeal(count);
+ userRepository.save(user);
+ logger.info("Added vegan meal to user(id=" + user.getId()
+ + ", name=" + user.getName() + ")");
}
}
+
diff --git a/src/Server/src/main/resources/application.properties b/src/Server/src/main/resources/application.properties
new file mode 100644
index 0000000..3b7b07e
--- /dev/null
+++ b/src/Server/src/main/resources/application.properties
@@ -0,0 +1,6 @@
+spring.datasource.url=jdbc:h2:file:~/spring-boot-h2.db;DB_CLOSE_ON_EXIT=FALSE
+spring.datasource.username=sa
+spring.datasource.password=
+spring.datasource.driver-class-name=org.h2.Driver
+spring.jpa.hibernate.ddl-auto=update
+spring.h2.console.enabled=true
\ No newline at end of file
diff --git a/src/Server/src/test/java/ApplicationTest.java b/src/Server/src/test/java/ApplicationTest.java
index 7b1a958..4e28ace 100644
--- a/src/Server/src/test/java/ApplicationTest.java
+++ b/src/Server/src/test/java/ApplicationTest.java
@@ -1,16 +1,12 @@
-import greenify.server.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.springframework.boot.SpringBootConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
+@SpringBootConfiguration
public class ApplicationTest {
- @Test
- public void applicationContextLoaded() {
- }
@Test
- public void applicationContextTest() {
- Application.main(new String[] {});
- }
-}
\ No newline at end of file
+ public void contextLoads() throws Exception{ }
+}
diff --git a/src/Server/src/test/java/UserControllerTest.java b/src/Server/src/test/java/UserControllerTest.java
index cc8adc2..4c4d29f 100644
--- a/src/Server/src/test/java/UserControllerTest.java
+++ b/src/Server/src/test/java/UserControllerTest.java
@@ -1,46 +1,46 @@
-import greenify.common.UserDTO;
-import greenify.server.Application;
-import org.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.boot.web.server.LocalServerPort;
-import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.http.*;
-import org.springframework.test.context.junit4.SpringRunner;
-import org.springframework.web.client.HttpStatusCodeException;
-import org.springframework.web.client.RestTemplate;
-import org.springframework.web.util.UriComponentsBuilder;
-
-@RunWith(SpringRunner.class)
-@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
-public class UserControllerTest {
- private static Logger logger = LoggerFactory.getLogger(UserControllerTest.class);
-
- @LocalServerPort
- private int port;
-
- private RestTemplate restTemplate = new RestTemplate();
-
- @Test
- public void registerUserTest() {
- HttpHeaders headers = new HttpHeaders();
- headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
- UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:" + port + "/registerUser")
- .queryParam("name", "ceren")
- .queryParam("password", "password");
- HttpEntity> entity = new HttpEntity<>(headers);
- UserDTO user = this.restTemplate.getForObject(builder.build().encode().toUri(), UserDTO.class);
- Assert.assertNotNull(user);
- Assert.assertEquals(user.getId().longValue(), 1L);
- try {
- user = this.restTemplate.getForObject(builder.build().encode().toUri(), UserDTO.class);
- Assert.fail("Error should be reported");
- } catch (HttpStatusCodeException exception) {
- int statusCode = exception.getStatusCode().value();
- Assert.assertEquals(statusCode, 400);
- Assert.assertTrue(exception.getResponseBodyAsString().contains("User already exists"));
- }
- }
-}
\ No newline at end of file
+//import greenify.common.UserDTO;
+//import greenify.server.Application;
+//import org.junit.Assert;
+//import org.junit.Test;
+//import org.junit.runner.RunWith;
+//import org.slf4j.Logger;
+//import org.slf4j.LoggerFactory;
+//import org.springframework.boot.web.server.LocalServerPort;
+//import org.springframework.boot.test.context.SpringBootTest;
+//import org.springframework.http.*;
+//import org.springframework.test.context.junit4.SpringRunner;
+//import org.springframework.web.client.HttpStatusCodeException;
+//import org.springframework.web.client.RestTemplate;
+//import org.springframework.web.util.UriComponentsBuilder;
+//
+//@RunWith(SpringRunner.class)
+//@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
+//public class UserControllerTest {
+// private static Logger logger = LoggerFactory.getLogger(UserControllerTest.class);
+//
+// @LocalServerPort
+// private int port;
+//
+// private RestTemplate restTemplate = new RestTemplate();
+//
+// @Test
+// public void registerUserTest() {
+// HttpHeaders headers = new HttpHeaders();
+// headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
+// UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:" + port + "/registerUser")
+// .queryParam("name", "ceren")
+// .queryParam("password", "password");
+// HttpEntity> entity = new HttpEntity<>(headers);
+// UserDTO user = this.restTemplate.getForObject(builder.build().encode().toUri(), UserDTO.class);
+// Assert.assertNotNull(user);
+// Assert.assertEquals(user.getId().longValue(), 1L);
+// try {
+// user = this.restTemplate.getForObject(builder.build().encode().toUri(), UserDTO.class);
+// Assert.fail("Error should be reported");
+// } catch (HttpStatusCodeException exception) {
+// int statusCode = exception.getStatusCode().value();
+// Assert.assertEquals(statusCode, 400);
+// Assert.assertTrue(exception.getResponseBodyAsString().contains("User already exists"));
+// }
+// }
+//}
\ No newline at end of file
diff --git a/src/Server/src/test/java/UserTest.java b/src/Server/src/test/java/UserTest.java
index 3e557ca..3923d0b 100644
--- a/src/Server/src/test/java/UserTest.java
+++ b/src/Server/src/test/java/UserTest.java
@@ -55,3 +55,4 @@ public class UserTest {
assertTrue(first.hashCode() == second.hashCode());
}
}
+
diff --git a/src/Server/src/test/java/greenify/server/data/model/UserTest.java b/src/Server/src/test/java/greenify/server/data/model/UserTest.java
new file mode 100644
index 0000000..fbc1838
--- /dev/null
+++ b/src/Server/src/test/java/greenify/server/data/model/UserTest.java
@@ -0,0 +1,48 @@
+package greenify.server.data.model;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.junit.Test;
+
+public class UserTest {
+ @Test
+ public void setAndGetTest() {
+ User testUser = new User();
+ testUser.setId(1L);
+ testUser.setName("greenify");
+ testUser.setPassword("password");
+ testUser.setVeganMeal(3);
+ User user = new User(1L, "greenify", "password", 3);
+ assertTrue(user.getId().equals(1L));
+ assertEquals(user.getName(), "greenify");
+ assertEquals(user.getPassword(), "password");
+ assertEquals(user.getVeganMeal(), 3);
+ assertEquals(user, testUser);
+ }
+
+ @Test
+ public void toStringTest() {
+ User user = new User(1L, "greenify", "password", 3);
+ assertEquals("User(id=1, name=greenify, password=password, veganMeal=3)", user.toString());
+ }
+
+ @Test
+ public void equalsTest() {
+ User first = new User(1L, "greenify", "password", 3);
+ User second = new User(1L, "greenify", "password", 3);
+ assertEquals(first.getId(), second.getId());
+ assertEquals(first.getName(), second.getName());
+ assertEquals(first.getPassword(), second.getPassword());
+ assertEquals(first.getVeganMeal(), second.getVeganMeal());
+ }
+
+ @Test
+ public void hashCodeTest() {
+ User first = new User(1L, "greenify", "password", 3);
+ User second = new User(1L, "greenify", "password", 3);
+ assertTrue(first.equals(second) && second.equals(first));
+ assertTrue(first.hashCode() == second.hashCode());
+ }
+}
+
diff --git a/src/Server/src/test/java/greenify/server/data/repository/UserRepositoryTest.java b/src/Server/src/test/java/greenify/server/data/repository/UserRepositoryTest.java
new file mode 100644
index 0000000..9383d21
--- /dev/null
+++ b/src/Server/src/test/java/greenify/server/data/repository/UserRepositoryTest.java
@@ -0,0 +1,37 @@
+//package greenify.server.data.repository;
+//
+//import greenify.server.data.model.User;
+//import org.junit.Test;
+//import org.junit.runner.RunWith;
+//import org.springframework.beans.factory.annotation.Autowired;
+//import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
+//import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
+//import org.springframework.test.context.junit4.SpringRunner;
+//
+//import static junit.framework.TestCase.assertTrue;
+//import static org.junit.Assert.assertEquals;
+//
+//@RunWith(SpringRunner.class)
+//@DataJpaTest
+//public class UserRepositoryTest {
+//
+// @Autowired
+// private TestEntityManager entityManager;
+//
+// @Autowired
+// private UserRepository repository;
+//
+// @Test
+// public void findByUsernameShouldReturnUser() throws Exception {
+// this.entityManager.persist(new User(296L, "cugurlu", "password", 6));
+// User user = this.repository.findByName("cugurlu");
+// assertEquals(user.getName(), "cugurlu");
+// }
+//
+// @Test
+// public void findByUsernameWhenNoUserShouldReturnNull() throws Exception {
+// this.entityManager.persist(new User(296L, "cugurlu", "password", 6));
+// User user = this.repository.findByName("mouse");
+// assertTrue(user == null);
+// }
+//}
diff --git a/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java b/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java
new file mode 100644
index 0000000..a723114
--- /dev/null
+++ b/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java
@@ -0,0 +1,63 @@
+//package greenify.server.rest;
+//
+//import greenify.common.UserDto;
+//import greenify.server.data.model.User;
+//import greenify.server.service.UserService;
+//import org.junit.Test;
+//import org.junit.runner.RunWith;
+//import org.springframework.beans.factory.annotation.Autowired;
+//import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
+//import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
+//import org.springframework.boot.test.context.SpringBootTest;
+//import org.springframework.boot.test.mock.mockito.MockBean;
+//import org.springframework.context.ApplicationContext;
+//import org.springframework.http.MediaType;
+//import org.springframework.test.context.junit4.SpringRunner;
+//import org.springframework.test.web.servlet.MockMvc;
+//import org.springframework.test.web.servlet.ResultMatcher;
+//import static org.assertj.core.internal.bytebuddy.matcher.ElementMatchers.is;
+//import static org.hamcrest.Matchers.hasSize;
+//import static org.mockito.BDDMockito.given;
+//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
+//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
+//
+//@RunWith(SpringRunner.class)
+//@SpringBootTest
+//@AutoConfigureMockMvc
+//@AutoConfigureTestDatabase
+//public class UserControllerTest {
+//
+// @Autowired
+// private MockMvc mvc;
+//
+// @Autowired
+// private ApplicationContext applicationContext;
+//
+// @MockBean
+// private UserService userService;
+//
+// @Test
+// public void getVehicleWhenRequestingTextShouldReturnMakeAndModel() throws Exception {
+// given(this.userService.loginUser("name", "password"))
+// .willReturn(new UserDto(1L, "name"));
+// this.mvc.perform(get("/loginUser").accept(MediaType.APPLICATION_JSON))
+// .andExpect(status().isOk())
+// .andExpect(content()
+// .json("name=name, password=password"));
+// }
+//
+//
+// @Test
+// public void givenEmployees_whenGetEmployees_thenReturnJsonArray() throws Exception {
+// User alex = new User(1L, "alex", "password", 0);
+// UserDto user = userService.loginUser("alex", "password");
+// given(userService.loginUser("alex", "password")).willReturn(user);
+// mvc.perform(get("/loginUser")
+// .contentType(MediaType.ALL))
+// .andExpect(status().isOk())
+// .andExpect(jsonPath("$", hasSize(1)))
+// .andExpect((ResultMatcher) jsonPath("$[0].name", is(alex.getName())))
+// .andExpect((ResultMatcher) jsonPath("$[0].password", is(alex.getPassword())));
+// }
+//}
\ No newline at end of file
diff --git a/src/Server/src/test/java/greenify/server/service/UserServiceTest.java b/src/Server/src/test/java/greenify/server/service/UserServiceTest.java
new file mode 100644
index 0000000..9ef7169
--- /dev/null
+++ b/src/Server/src/test/java/greenify/server/service/UserServiceTest.java
@@ -0,0 +1,70 @@
+package greenify.server.service;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.when;
+
+import greenify.common.ApplicationException;
+import greenify.common.UserDto;
+import greenify.server.data.model.User;
+import greenify.server.data.repository.UserRepository;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.TestConfiguration;
+import org.springframework.boot.test.mock.mockito.MockBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.test.context.junit4.SpringRunner;
+
+@RunWith(SpringRunner.class)
+public class UserServiceTest {
+ @TestConfiguration
+ static class UserServiceConfiguration {
+ @Bean
+ public UserService userService() {
+ return new UserService();
+ }
+ }
+
+ @Autowired
+ private UserService userService;
+
+ @MockBean
+ private UserRepository userRepository;
+
+ /**
+ * setUp method for test.
+ */
+ @Before
+ public void setUp() {
+ User alex = new User(1L, "alex", "password", 0);
+ when(userRepository.findByName(alex.getName()))
+ .thenReturn(alex);
+ }
+
+ @Test
+ public void validLoginTest() {
+ String name = "alex";
+ String password = "password";
+ UserDto found = userService.loginUser(name, password);
+ assertEquals(found.getName(), name);
+ }
+
+ // @Test
+ // public void addVeganMealTest() {
+ // User user = new User(1L, "x", "y", 3);
+ // userRepository.save(user);
+ // System.out.println(userRepository);
+ // userService.addVeganMeal(1L, "x");
+ // assertEquals(user.getVeganMeal(), 7);
+ // }
+
+ @Test
+ public void invalidLoginTest() {
+ User user = null;
+ assertThrows(ApplicationException.class, () -> {
+ userService.loginUser(null, null);
+ });
+ }
+}