From cea68e93c9418ccdc68114584127aad9c5a0193c Mon Sep 17 00:00:00 2001 From: Merel Steenbergen Date: Thu, 14 Mar 2019 14:04:04 +0100 Subject: [PATCH 01/14] Add outline of activity class, has bugs --- .../java/greenify/common/ActivityDTO.java | 34 ++++++ .../greenify/server/data/model/Activity.java | 102 ++++++++++++++++++ .../java/greenify/server/data/model/User.java | 1 - 3 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 src/Common/src/main/java/greenify/common/ActivityDTO.java create mode 100644 src/Server/src/main/java/greenify/server/data/model/Activity.java diff --git a/src/Common/src/main/java/greenify/common/ActivityDTO.java b/src/Common/src/main/java/greenify/common/ActivityDTO.java new file mode 100644 index 0000000..6ed2fbc --- /dev/null +++ b/src/Common/src/main/java/greenify/common/ActivityDTO.java @@ -0,0 +1,34 @@ +package greenify.common; + +public class ActivityDTO { + private Long id; + private String name; + private String description; + private int score; + + public ActivityDTO() { + } + + public ActivityDTO(Long id, String name, String description, int score) { + this.id = id; + this.name = name; + this.description=description; + this.score= score; + } + + public String getName() { + return name; + } + + public Long getId() { + return id; + } + + public String getDescription() { + return description; + } + + public int getScore() { + return score; + } +} diff --git a/src/Server/src/main/java/greenify/server/data/model/Activity.java b/src/Server/src/main/java/greenify/server/data/model/Activity.java new file mode 100644 index 0000000..99635e4 --- /dev/null +++ b/src/Server/src/main/java/greenify/server/data/model/Activity.java @@ -0,0 +1,102 @@ +package greenify.server.data.model; + +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import java.util.Scanner; + +@Entity +@Data +//@AllArgsConstructor +public abstract class Activity { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + Long id; + String name; + String description; + int score; + + /** + * makes an activity object. + * @param id the id of the activity. + * @param name the name of the feature. + * @param description the description of the feature. + * @param score the amount of points a user gets for doing this activity. + */ + public void User(Long id, String name, String description, int score) { + this.id = id; + this.name = name; + this.description = setDes(description); + this.score = score; + } + + /** + * gets the id. + * @return the id + */ + public Long getId() { + return id; + } + + /** + * gets the name. + * @return the name + */ + public String getName() { + return name; + } + + /** + * gets the password. + * @return the password + */ + public String getDescription() { + return description; + } + + public void setId(Long id) { + this.id = id; + } + + /** + * sets the name. + * @param name the you want to assign to this.name. + */ + public void setName(String name) { + this.name = name; + } + + /** + * sets the description. + * @param description the description to be set. + */ + public String setDes(String description){ + Scanner scan = new Scanner(System.in); + System.out.println("Please enter the description"); + description = scan.nextLine(); + scan.close(); + return description; + } + + /** + * sets the score you get for an activity. + * @param score the you want to assign to the activity. + */ + public void setScore(int score) { + this.score = score; + } + + /** + * Returns a human readable object. It's in JSON. + * @return the JSON form of the object. + */ + @Override + public String toString() { + return "Activity(id=" + this.id + ", name=" + this.name + ", description=" + this.description + ", score=" + this.score + ")"; + } +} \ 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 5d3057a..38b6187 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 @@ -11,7 +11,6 @@ import javax.persistence.Id; @Entity @Data //@AllArgsConstructor -@NoArgsConstructor public class User { @Id From cca49bc9d6b21871fb19ef184d42bf86aa09ccdd Mon Sep 17 00:00:00 2001 From: Merel Steenbergen Date: Thu, 14 Mar 2019 14:31:38 +0100 Subject: [PATCH 02/14] Fix bugs. Feature abstract class added --- src/Server/src/main/java/greenify/server/data/model/User.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 38b6187..6e703d1 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 @@ -11,6 +11,7 @@ import javax.persistence.Id; @Entity @Data //@AllArgsConstructor +@NoArgsConstructor public class User { @Id @@ -72,4 +73,4 @@ public class User { return "User(id=" + this.id + ", name=" + this.name + ", password=" + this.password + ")"; } -} \ No newline at end of file +} From 8bbe3f0fe950188a3393f6b261ece72f7481c722 Mon Sep 17 00:00:00 2001 From: Merel Steenbergen Date: Thu, 14 Mar 2019 15:08:49 +0100 Subject: [PATCH 03/14] Some more work done on the feature object --- .../greenify/client/rest/ActivityService.java | 25 ++++++++++++++++++ .../java/greenify/common/ActivityDTO.java | 2 +- .../server/rest/ActivityController.java | 26 +++++++++++++++++++ .../server/service/ActivityService.java | 6 +++++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/Client/src/main/java/greenify/client/rest/ActivityService.java create mode 100644 src/Server/src/main/java/greenify/server/rest/ActivityController.java create mode 100644 src/Server/src/main/java/greenify/server/service/ActivityService.java diff --git a/src/Client/src/main/java/greenify/client/rest/ActivityService.java b/src/Client/src/main/java/greenify/client/rest/ActivityService.java new file mode 100644 index 0000000..fe69b90 --- /dev/null +++ b/src/Client/src/main/java/greenify/client/rest/ActivityService.java @@ -0,0 +1,25 @@ +package greenify.client.rest; + +import greenify.common.ActivityDTO; +import greenify.common.ApplicationException; +import greenify.common.UserDTO; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; + +public class ActivityService { + + Logger logger = LoggerFactory.getLogger(greenify.server.service.ActivityService.class); + @Autowired + UserRepository userRepository; + + /** + * Adds the activity to the history of the user and adds the score. + * @param name the name of the activity + * @return a activityDTO of activity. + */ + public ActivityDTO addActivity(String name) { + + } + +} diff --git a/src/Common/src/main/java/greenify/common/ActivityDTO.java b/src/Common/src/main/java/greenify/common/ActivityDTO.java index 6ed2fbc..a564f1e 100644 --- a/src/Common/src/main/java/greenify/common/ActivityDTO.java +++ b/src/Common/src/main/java/greenify/common/ActivityDTO.java @@ -12,7 +12,7 @@ public class ActivityDTO { public ActivityDTO(Long id, String name, String description, int score) { this.id = id; this.name = name; - this.description=description; + this.description = description; this.score= score; } diff --git a/src/Server/src/main/java/greenify/server/rest/ActivityController.java b/src/Server/src/main/java/greenify/server/rest/ActivityController.java new file mode 100644 index 0000000..e2f9f2d --- /dev/null +++ b/src/Server/src/main/java/greenify/server/rest/ActivityController.java @@ -0,0 +1,26 @@ +package greenify.server.rest; + +import greenify.common.UserDTO; +import greenify.server.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class ActivityController { + @Autowired + UserService userService; + + @RequestMapping("/addActivity") + 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); + } +} \ No newline at end of file diff --git a/src/Server/src/main/java/greenify/server/service/ActivityService.java b/src/Server/src/main/java/greenify/server/service/ActivityService.java new file mode 100644 index 0000000..a9c2bbc --- /dev/null +++ b/src/Server/src/main/java/greenify/server/service/ActivityService.java @@ -0,0 +1,6 @@ +package greenify.server.service; + +public class ActivityService { + + +} From 34d098f1889ba2d983ef46b2213fcbae3c9b2331 Mon Sep 17 00:00:00 2001 From: Merel Steenbergen Date: Sun, 17 Mar 2019 16:57:09 +0100 Subject: [PATCH 04/14] Finish activity class and add tests --- .../greenify/client/rest/ActivityService.java | 25 ----- .../greenify/server/data/model/Activity.java | 68 ++++++++---- .../java/greenify/server/data/model/User.java | 100 ++++++++++++++---- .../server/service/ActivityService.java | 6 -- .../greenify/server/service/UserService.java | 2 +- src/Server/src/test/java/ActivityTest.java | 48 +++++++++ src/Server/src/test/java/UserTest.java | 33 ++++-- 7 files changed, 203 insertions(+), 79 deletions(-) delete mode 100644 src/Client/src/main/java/greenify/client/rest/ActivityService.java delete mode 100644 src/Server/src/main/java/greenify/server/service/ActivityService.java create mode 100644 src/Server/src/test/java/ActivityTest.java diff --git a/src/Client/src/main/java/greenify/client/rest/ActivityService.java b/src/Client/src/main/java/greenify/client/rest/ActivityService.java deleted file mode 100644 index fe69b90..0000000 --- a/src/Client/src/main/java/greenify/client/rest/ActivityService.java +++ /dev/null @@ -1,25 +0,0 @@ -package greenify.client.rest; - -import greenify.common.ActivityDTO; -import greenify.common.ApplicationException; -import greenify.common.UserDTO; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; - -public class ActivityService { - - Logger logger = LoggerFactory.getLogger(greenify.server.service.ActivityService.class); - @Autowired - UserRepository userRepository; - - /** - * Adds the activity to the history of the user and adds the score. - * @param name the name of the activity - * @return a activityDTO of activity. - */ - public ActivityDTO addActivity(String name) { - - } - -} diff --git a/src/Server/src/main/java/greenify/server/data/model/Activity.java b/src/Server/src/main/java/greenify/server/data/model/Activity.java index 99635e4..01a2e02 100644 --- a/src/Server/src/main/java/greenify/server/data/model/Activity.java +++ b/src/Server/src/main/java/greenify/server/data/model/Activity.java @@ -3,20 +3,20 @@ package greenify.server.data.model; import lombok.Data; import lombok.NoArgsConstructor; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; +import javax.accessibility.AccessibleValue; +import javax.persistence.*; +import java.util.Objects; import java.util.Scanner; @Entity @Data +@Table(name = "activities") //@AllArgsConstructor -public abstract class Activity { +public class Activity { @Id @GeneratedValue(strategy = GenerationType.AUTO) - Long id; + long id; String name; String description; int score; @@ -28,10 +28,10 @@ public abstract class Activity { * @param description the description of the feature. * @param score the amount of points a user gets for doing this activity. */ - public void User(Long id, String name, String description, int score) { + public Activity(long id, String name, String description, int score) { this.id = id; this.name = name; - this.description = setDes(description); + this.description = description; this.score = score; } @@ -39,7 +39,7 @@ public abstract class Activity { * gets the id. * @return the id */ - public Long getId() { + public long getId() { return id; } @@ -52,14 +52,27 @@ public abstract class Activity { } /** - * gets the password. - * @return the password + * gets the description. + * @return the description */ public String getDescription() { return description; } - public void setId(Long id) { + /** + * gets the score. + * @return the score + */ + public int getScore() { + return score; + } + + + /** + * sets the id. + * @param id the you want to assign to this.id. + */ + public void setId(long id) { this.id = id; } @@ -75,12 +88,8 @@ public abstract class Activity { * sets the description. * @param description the description to be set. */ - public String setDes(String description){ - Scanner scan = new Scanner(System.in); - System.out.println("Please enter the description"); - description = scan.nextLine(); - scan.close(); - return description; + public void setDescription(String description){ + this.description = description; } /** @@ -91,6 +100,7 @@ public abstract class Activity { this.score = score; } + /** * Returns a human readable object. It's in JSON. * @return the JSON form of the object. @@ -99,4 +109,26 @@ public abstract class Activity { public String toString() { return "Activity(id=" + this.id + ", name=" + this.name + ", description=" + this.description + ", score=" + this.score + ")"; } + + @Override + public boolean equals(Object other) { + if(other instanceof Activity){ + Activity that = (Activity)other; + if(that.id != this.id) + return false; + if(!that.name.equals(this.name)) + return false; + if(!that.description.equals(this.description)) + return false; + if(that.score != this.score) + return false; + return true; + } + return false; + } + + @Override + public int hashCode() { + return Objects.hash(id, name, description, score); + } } \ 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 6e703d1..d588eb1 100644 --- a/src/Server/src/main/java/greenify/server/data/model/User.java +++ b/src/Server/src/main/java/greenify/server/data/model/User.java @@ -1,35 +1,44 @@ package greenify.server.data.model; import lombok.Data; -import lombok.NoArgsConstructor; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; +import javax.persistence.*; +import javax.validation.constraints.NotNull; +import java.util.Objects; +@EnableAutoConfiguration @Entity @Data -//@AllArgsConstructor -@NoArgsConstructor +@Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) - Long id; - String name; - String password; + private Long id; + + @NotNull + private String name; + + @NotNull + private String password; + + private int veganMeal; + + public User() {} /** * makes a user object. * @param id the id of the user. * @param name the supplied username * @param password the supplied password + * @param veganMeal the supplied number of vegan meal */ - public User(Long id, String name, String password) { + public User(Long id, String name, String password, int veganMeal) { this.id = id; this.name = name; this.password = password; + this.veganMeal = veganMeal; } /** @@ -40,6 +49,8 @@ public class User { return id; } + public void setId(Long id) { this.id = id; } + /** * gets the name. * @return the name @@ -48,6 +59,8 @@ public class User { return name; } + public void setName(String name) { this.name = name; } + /** * gets the password. * @return the password @@ -56,21 +69,68 @@ public class User { return password; } - public void setId(Long id) { - this.id = id; + public void setPassword(String password) { this.password = password; } + + /** + * gets the number of vegan meal. + * @return the veganMeal + */ + public int getVeganMeal() { + return veganMeal; } - public void setName(String name) { - this.name = name; - } - - public void setPassword(String password) { - this.password = password; + public void setVeganMeal(int veganMeal) { this.veganMeal = veganMeal; } + + /** + * checks if two users are equal. + * @param other the object to compare the user with + * @return a boolean value of true if the user is equal to the object + */ + @Override + public boolean equals(Object other) { + + if (this == other) { + return true; + } + if (other == null) { + return false; + } + if (getClass() != other.getClass()) { + return false; + } + if (other instanceof User) { + User that = (User) other; + return this.getName().equals(that.getName()) + && this.getId().equals(that.getId()) + && this.getPassword().equals(that.getPassword()) + && this.getVeganMeal() == that.getVeganMeal(); + } + return false; } + /** + * creates a string of the user object. + * in the form of: User(id=, name=, password=, veganMeal=) + * @return a string of the user object + */ @Override public String toString() { + return "User(id=" + + this.id + + ", name=" + + this.name + + ", password=" + + this.password + + ", veganMeal=" + + this.veganMeal + ")"; + } - return "User(id=" + this.id + ", name=" + this.name + ", password=" + this.password + ")"; + /** + * hashes the User object. + * @return a hashcode for the user object + */ + @Override + public int hashCode() { + return Objects.hash(id, name, password, veganMeal); } } diff --git a/src/Server/src/main/java/greenify/server/service/ActivityService.java b/src/Server/src/main/java/greenify/server/service/ActivityService.java deleted file mode 100644 index a9c2bbc..0000000 --- a/src/Server/src/main/java/greenify/server/service/ActivityService.java +++ /dev/null @@ -1,6 +0,0 @@ -package greenify.server.service; - -public class ActivityService { - - -} 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 4756726..a844a1d 100644 --- a/src/Server/src/main/java/greenify/server/service/UserService.java +++ b/src/Server/src/main/java/greenify/server/service/UserService.java @@ -26,7 +26,7 @@ public class UserService { if (user != null) { throw new ApplicationException("User already exists"); } else { - user = userRepository.save(new User(null, name, password)); + user = userRepository.save(new User(null, name, password, 0)); } logger.info("Created user id=" + user.getId() + ", name=" + user.getName()); return new UserDTO(user.getId(), user.getName()); diff --git a/src/Server/src/test/java/ActivityTest.java b/src/Server/src/test/java/ActivityTest.java new file mode 100644 index 0000000..7a491e2 --- /dev/null +++ b/src/Server/src/test/java/ActivityTest.java @@ -0,0 +1,48 @@ +import greenify.server.data.model.Activity; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class ActivityTest { + @Test + public void setAndGetTest() { + Activity activity = new Activity(1, "Vegetarian", "Eating", 100); + Activity testActivity = new Activity(0, null, null, 0); + testActivity.setId(1); + testActivity.setName("Vegetarian"); + testActivity.setDescription("Eating"); + testActivity.setScore(100); + assertEquals(activity.getId(), 1); + assertEquals(activity.getName(), "Vegetarian"); + assertEquals(activity.getDescription(), "Eating"); + assertEquals(activity.getScore(), 100); + assertEquals(activity, testActivity); + } + + @Test + public void toStringTest() { + Activity activity = new Activity(1, "Solar panels", "Installed", 10000); + assertEquals("Activity(id=1, name=Solar panels, description=Installed, score=10000)", activity.toString()); + } + + @Test + public void equalsTest() { + Activity first = new Activity(1, "Solar panels", "Installed", 10000); + Activity second = new Activity(1, "Solar panels", "Installed", 10000); + assertEquals(first.getId(), second.getId()); + assertEquals(first.getName(), second.getName()); + assertEquals(first.getDescription(), second.getDescription()); + assertEquals(first.getScore(), second.getScore()); + assertTrue(first.equals(second)); + } + + @Test + public void hashCodeTest() { + Activity first = new Activity(1, "Solar panels", "Installed", 10000); + Activity second = new Activity(1, "Solar panels", "Installed", 10000); + assertEquals(first, second); + assertEquals(first.hashCode(), second.hashCode()); +// assertTrue(first.hashCode() == second.hashCode()); + } +} diff --git a/src/Server/src/test/java/UserTest.java b/src/Server/src/test/java/UserTest.java index ae359c9..3e557ca 100644 --- a/src/Server/src/test/java/UserTest.java +++ b/src/Server/src/test/java/UserTest.java @@ -6,36 +6,51 @@ import static org.junit.jupiter.api.Assertions.assertTrue; public class UserTest { @Test public void setAndGetTest() { - User user = new User(1L, "greenify", "password"); - User testUser = new User(null, null, null); + User user = new User(1L, "greenify", "password", 3); + User testUser = new User(); testUser.setId(1L); testUser.setName("greenify"); testUser.setPassword("password"); + testUser.setVeganMeal(3); assertTrue(user.getId().equals(1L)); assertEquals(user.getName(), "greenify"); assertEquals(user.getPassword(), "password"); - assertEquals(user, testUser); + assertEquals(user.getVeganMeal(), 3); + + } + + @Test + public void equalsMethodTest() { + User user = new User(1L, "greenify", "password", 3); + User testUser = new User(); + testUser.setId(1L); + testUser.setName("greenify"); + testUser.setPassword("password"); + testUser.setVeganMeal(3); + assertTrue(user.equals(testUser)); } @Test public void toStringTest() { - User user = new User(1L, "greenify", "password"); - assertEquals("User(id=1, name=greenify, password=password)", user.toString()); + User user = new User(1L, "greenify", "password", 3); + System.out.println("String is " + user.toString()); + assertTrue("User(id=1, name=greenify, password=password, veganMeal=3)".equals(user.toString())); } @Test public void equalsTest() { - User first = new User(1L, "greenify", "password"); - User second = new User(1L, "greenify", "password"); + 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"); - User second = new User(1L, "greenify", "password"); + 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()); } From f5fa03b1042cd1b3a57659ae05edc019740f11ca Mon Sep 17 00:00:00 2001 From: cugurlu Date: Sun, 17 Mar 2019 17:06:50 +0100 Subject: [PATCH 05/14] Add checkstyle plugin --- CSE1105.checkstyle.xml | 188 ----------------------------- build.gradle | 48 -------- quality/checkstyle/checkstyle.xml | 188 +++++++++++++++++++++++++++++ quality/checkstyle/suppression.xml | 3 + src/Client/build.gradle | 39 +++--- src/Common/build.gradle | 17 +++ src/Server/build.gradle | 17 +++ 7 files changed, 242 insertions(+), 258 deletions(-) delete mode 100644 CSE1105.checkstyle.xml create mode 100644 quality/checkstyle/checkstyle.xml create mode 100644 quality/checkstyle/suppression.xml 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 e91d569..2b6ed72 100644 --- a/build.gradle +++ b/build.gradle @@ -16,44 +16,16 @@ 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.server.Application' } -tasks.withType(Checkstyle) { - reports { - html.destination rootProject.file("build/reports/checkstyle.html") - } -} - repositories { mavenCentral() } -allprojects { - task hello { - doLast { task -> - println "I'm $task.project.name" - } - } -} - -////client bootjar -//bootJar { -// baseName = 'gs-consuming-rest' -// version = '0.1.0' -//} -// -////server bootjar -//bootJar { -// baseName = 'gs-rest-service' -// version = '0.1.0' -//} - bootJar { enabled = false } @@ -65,10 +37,6 @@ jar { sourceCompatibility = 1.8 targetCompatibility = 1.8 -test { - useJUnitPlatform() -} - dependencies { compile("org.springframework.boot:spring-boot-starter") compile("org.springframework:spring-web") @@ -90,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/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/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/Common/build.gradle b/src/Common/build.gradle index a903381..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( diff --git a/src/Server/build.gradle b/src/Server/build.gradle index 4d1b88a..6968ab5 100644 --- a/src/Server/build.gradle +++ b/src/Server/build.gradle @@ -13,6 +13,7 @@ apply plugin: 'idea' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' apply plugin: 'jacoco' +apply plugin: 'checkstyle' bootJar { baseName = 'gs-rest-service' @@ -31,6 +32,22 @@ 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') From 07c76dd3850ee9d9efb18aa00f38f32fe49880d1 Mon Sep 17 00:00:00 2001 From: cugurlu Date: Sun, 17 Mar 2019 17:08:46 +0100 Subject: [PATCH 06/14] Fix checkstyle errors --- src/.idea/checkstyle-idea.xml | 2 + src/.idea/workspace.xml | 732 +++++++++++------- .../java/greenify/client/Application.java | 16 +- .../controller/DashBoardController.java | 25 +- .../controller/RegisterWindowController.java | 2 +- .../client/controller/UserController.java | 28 +- .../greenify/client/rest/UserService.java | 23 +- src/Client/src/test/java/UserServiceTest.java | 25 +- .../main/java/greenify/common/UserDTO.java | 6 +- src/Common/src/test/java/UserDTOTest.java | 12 +- .../java/greenify/server/data/model/User.java | 74 +- .../data/repository/UserRepository.java | 3 +- .../greenify/server/rest/UserController.java | 28 +- .../greenify/server/service/UserService.java | 14 +- src/Server/src/test/java/ApplicationTest.java | 3 - .../greenify/server/data/model/UserTest.java | 6 +- .../server/rest/UserControllerTest.java | 10 +- .../server/service/UserServiceTest.java | 30 +- 18 files changed, 577 insertions(+), 462 deletions(-) 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 @@ @@ -306,8 +418,8 @@ - - + + - + - - - true - - - - - - - - true - - - - - - + + + + true + + + + + + + + + + + true + + @@ -419,11 +531,11 @@ - - - - - + + + + + @@ -441,35 +553,35 @@ - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + @@ -484,17 +596,17 @@ - - + + - + - - + + @@ -503,7 +615,7 @@ - + @@ -513,7 +625,7 @@ - + @@ -553,9 +665,6 @@ - - - @@ -594,25 +703,8 @@ - - - - - - - - - - - - - - - - - - + @@ -634,9 +726,6 @@ - - - @@ -660,53 +749,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -722,18 +764,6 @@ - - - - - - - - - - - - @@ -741,16 +771,6 @@ - - - - - - - - - - @@ -758,43 +778,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -826,36 +809,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -866,24 +819,211 @@ - + - - + + + + + + + + + + + + + + + + + + + - - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Client/src/main/java/greenify/client/Application.java b/src/Client/src/main/java/greenify/client/Application.java index 2ce787e..77ae0c0 100644 --- a/src/Client/src/main/java/greenify/client/Application.java +++ b/src/Client/src/main/java/greenify/client/Application.java @@ -10,6 +10,8 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; +import java.io.IOException; + @SpringBootApplication public class Application extends javafx.application.Application { private static ConfigurableApplicationContext springContext; @@ -19,16 +21,16 @@ public class Application extends javafx.application.Application { launch(args); } - public static Parent load(java.net.URL url) { + /** + * 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); - try { - return loader.load(); - } catch (Exception e) { - e.printStackTrace(); - } - return null; + return loader.load(); } @Override 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 b44eff2..ff2cde2 100644 --- a/src/Client/src/main/java/greenify/client/controller/DashBoardController.java +++ b/src/Client/src/main/java/greenify/client/controller/DashBoardController.java @@ -17,17 +17,17 @@ public class DashBoardController { private int count = 0; @FXML - public AnchorPane menuBar; - public AnchorPane dashboardPane; - public AnchorPane userPane; - public AnchorPane activitiesPane; - public Label welcomebacktext; - public Button dashboardButton; - public Button activitiesButton; - public Button userButton; - public Button veganMealButton; - public Label counter; - public Label scoreField; + 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. @@ -70,7 +70,8 @@ public class DashBoardController { count++; counter.setText("Count: " + count); System.out.println(userService); - userService.addVeganMeal(userService.currentUser.getId(), userService.currentUser.getName()); + 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 index a0e6a8b..df99252 100644 --- a/src/Client/src/main/java/greenify/client/controller/RegisterWindowController.java +++ b/src/Client/src/main/java/greenify/client/controller/RegisterWindowController.java @@ -33,7 +33,7 @@ public class RegisterWindowController { */ @FXML public void handleSignUpButton(ActionEvent event) { - //set the window to the current window (for displaying the alerts) + //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()) { 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 2668d93..bec5ade 100644 --- a/src/Client/src/main/java/greenify/client/controller/UserController.java +++ b/src/Client/src/main/java/greenify/client/controller/UserController.java @@ -4,7 +4,6 @@ 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,15 +23,9 @@ public class UserController { UserService userService; @FXML - public TextField usernameField; - - @FXML + private TextField usernameField; private PasswordField passwordField; - - @FXML private Button loginButton; - - @FXML private Button signupButton; @FXML @@ -65,9 +58,11 @@ public class UserController { * @author sem */ public void openDashboard() throws IOException { - Parent dash = Application.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()); + scene.getStylesheets().add(getClass().getClassLoader() + .getResource("stylesheets/dashboardStyle.css").toExternalForm()); Stage appStage = new Stage(); appStage.setScene(scene); appStage.show(); @@ -94,13 +89,16 @@ public class UserController { } } - public void handleRegisterButtonAction(ActionEvent event) throws Exception{ - //load the fxml file - Parent registerWindow = Application.load (this.getClass().getClassLoader().getResource("fxml/RegisterWindow.fxml")); - //make the window use the scene + /** + * 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(); - //open the window 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 d1822f0..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,11 +1,12 @@ 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; @@ -15,7 +16,7 @@ public class UserService { @Autowired RestTemplate restTemplate; - public UserDTO currentUser; + public UserDto currentUser; @Bean RestTemplate restTemplate(RestTemplateBuilder builder) { @@ -28,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") @@ -36,7 +37,8 @@ public class UserService { .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); + UserDto result = this.restTemplate.getForObject(builder.build() + .encode().toUri(), UserDto.class); this.currentUser = result; return result; } @@ -47,7 +49,7 @@ public class UserService { * @param password the password of the user * @return a userDTO */ - public UserDTO loginUser(String name, String password) { + 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") @@ -55,7 +57,8 @@ public class UserService { .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); + UserDto result = this.restTemplate.getForObject(builder.build() + .encode().toUri(), UserDto.class); this.currentUser = result; return result; } @@ -66,7 +69,7 @@ public class UserService { * @param name the username of the user * @return a userDTO */ - public UserDTO addVeganMeal(Long id, String name) { + 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") @@ -74,6 +77,6 @@ public class UserService { .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); + return this.restTemplate.getForObject(builder.build().encode().toUri(), UserDto.class); } } diff --git a/src/Client/src/test/java/UserServiceTest.java b/src/Client/src/test/java/UserServiceTest.java index 1c4a7ef..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; @@ -22,32 +25,32 @@ public class UserServiceTest { @Test public void userRegisterTest() throws Exception { - UserDTO testUser = new UserDTO(1L, "Eric"); + 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"); + UserDto testUser = new UserDto(1L, "Eric"); Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/loginUser?name=Eric&password=password"), - UserDTO.class)) + UserDto.class)) .thenReturn(testUser); - UserDTO user = userService.loginUser("Eric", "password"); + UserDto user = userService.loginUser("Eric", "password"); Assert.assertEquals(testUser, user); } @Test public void addVeganMealTest() throws Exception { - UserDTO testUser = new UserDTO(1L, "Eric"); + UserDto testUser = new UserDto(1L, "Eric"); Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/addVeganMeal?id=1&name=Eric"), - UserDTO.class)) + UserDto.class)) .thenReturn(testUser); - UserDTO user = userService.addVeganMeal(1L, "Eric"); + UserDto user = userService.addVeganMeal(1L, "Eric"); Assert.assertEquals(testUser, user); } } diff --git a/src/Common/src/main/java/greenify/common/UserDTO.java b/src/Common/src/main/java/greenify/common/UserDTO.java index 8ff9e56..09e77f4 100644 --- a/src/Common/src/main/java/greenify/common/UserDTO.java +++ b/src/Common/src/main/java/greenify/common/UserDTO.java @@ -4,15 +4,15 @@ package greenify.common; // 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 { +public class UserDto { private Long id; private String name; - public UserDTO() { + public UserDto() { } - public UserDTO(Long id, String name) { + public UserDto(Long id, String name) { this.id = id; this.name = name; } diff --git a/src/Common/src/test/java/UserDTOTest.java b/src/Common/src/test/java/UserDTOTest.java index 33a01f6..604f486 100644 --- a/src/Common/src/test/java/UserDTOTest.java +++ b/src/Common/src/test/java/UserDTOTest.java @@ -1,14 +1,14 @@ -import greenify.common.UserDTO; +import greenify.common.UserDto; import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -public class UserDTOTest { +public class UserDtoTest { @Test public void setAndGetTest() { - UserDTO user = new UserDTO(1L, "greenify"); - UserDTO testUser = new UserDTO(); + UserDto user = new UserDto(1L, "greenify"); + UserDto testUser = new UserDto(); testUser.setId(1L); testUser.setName("greenify"); assertTrue(user.getId() == 1L); @@ -17,8 +17,8 @@ public class UserDTOTest { @Test public void equalsTest() { - UserDTO first = new UserDTO(1L, "greenify"); - UserDTO second = new UserDTO(1L, "greenify"); + UserDto first = new UserDto(1L, "greenify"); + UserDto second = new UserDto(1L, "greenify"); assertEquals(first.getId(), second.getId()); assertEquals(first.getName(), second.getName()); } diff --git a/src/Server/src/main/java/greenify/server/data/model/User.java b/src/Server/src/main/java/greenify/server/data/model/User.java index d588eb1..8ce92ea 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 @@ -3,9 +3,12 @@ package greenify.server.data.model; import lombok.Data; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import javax.persistence.*; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; import javax.validation.constraints.NotNull; -import java.util.Objects; @EnableAutoConfiguration @Entity @@ -49,7 +52,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 +64,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. @@ -69,7 +76,9 @@ public class User { return password; } - public void setPassword(String password) { this.password = password; } + public void setPassword(String password) { + this.password = password; + } /** * gets the number of vegan meal. @@ -79,58 +88,7 @@ public class User { return veganMeal; } - public void setVeganMeal(int veganMeal) { this.veganMeal = veganMeal; } - - /** - * checks if two users are equal. - * @param other the object to compare the user with - * @return a boolean value of true if the user is equal to the object - */ - @Override - public boolean equals(Object other) { - - if (this == other) { - return true; - } - if (other == null) { - return false; - } - if (getClass() != other.getClass()) { - return false; - } - if (other instanceof User) { - User that = (User) other; - return this.getName().equals(that.getName()) - && this.getId().equals(that.getId()) - && this.getPassword().equals(that.getPassword()) - && this.getVeganMeal() == that.getVeganMeal(); - } - return false; - } - - /** - * creates a string of the user object. - * in the form of: User(id=, name=, password=, veganMeal=) - * @return a string of the user object - */ - @Override - public String toString() { - return "User(id=" - + this.id - + ", name=" - + this.name - + ", password=" - + this.password - + ", veganMeal=" - + this.veganMeal + ")"; - } - - /** - * hashes the User object. - * @return a hashcode for the user object - */ - @Override - public int hashCode() { - return Objects.hash(id, name, password, veganMeal); + public void setVeganMeal(int veganMeal) { + this.veganMeal = veganMeal; } } 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 5e7d511..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,10 +1,11 @@ package greenify.server.data.repository; -import org.springframework.data.repository.CrudRepository; import greenify.server.data.model.User; +import org.springframework.data.repository.CrudRepository; public interface UserRepository extends CrudRepository { User findByName(String name); + T save(T user); } diff --git a/src/Server/src/main/java/greenify/server/rest/UserController.java b/src/Server/src/main/java/greenify/server/rest/UserController.java index 452689f..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,11 +1,15 @@ 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.*; +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 public class UserController { @@ -17,27 +21,27 @@ public class UserController { 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("/loginUser") - public UserDTO loginUser(@RequestParam(value = "name") String name, - @RequestParam(value = "password") String password) { + public UserDto loginUser(@RequestParam(value = "name") String name, + @RequestParam(value = "password") String password) { return userService.loginUser(name, password); } - @GetMapping(path="/all") - public @ResponseBody - Iterable getAllUsers() { - // This returns a JSON or XML with the users - return userRepository.findAll(); - } - @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 68d257a..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,7 +21,7 @@ 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) { user = userRepository.save(new User(null, name, password, 0)); @@ -29,7 +29,7 @@ public class UserService { 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 loginUser(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,14 +47,13 @@ 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 - * @return a userDTO of the user added vegan meal */ public void addVeganMeal(Long id, String name) { User user = userRepository.findByName(name); @@ -62,7 +61,8 @@ public class UserService { count++; user.setVeganMeal(count); userRepository.save(user); - logger.info("Added vegan meal to user(id=" + user.getId() + ", name=" + user.getName() + ")"); + logger.info("Added vegan meal to user(id=" + user.getId() + + ", name=" + user.getName() + ")"); } } diff --git a/src/Server/src/test/java/ApplicationTest.java b/src/Server/src/test/java/ApplicationTest.java index eb54359..4e28ace 100644 --- a/src/Server/src/test/java/ApplicationTest.java +++ b/src/Server/src/test/java/ApplicationTest.java @@ -1,9 +1,6 @@ -import greenify.server.Application; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.SpringBootConfiguration; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) 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 efe7549..fbc1838 100644 --- a/src/Server/src/test/java/greenify/server/data/model/UserTest.java +++ b/src/Server/src/test/java/greenify/server/data/model/UserTest.java @@ -1,19 +1,19 @@ package greenify.server.data.model; -import greenify.server.data.model.User; -import org.junit.Test; 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 user = new User(1L, "greenify", "password", 3); 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"); 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 abb31d7..a723114 100644 --- a/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java +++ b/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java @@ -1,6 +1,6 @@ //package greenify.server.rest; // -//import greenify.common.UserDTO; +//import greenify.common.UserDto; //import greenify.server.data.model.User; //import greenify.server.service.UserService; //import org.junit.Test; @@ -40,16 +40,18 @@ // @Test // public void getVehicleWhenRequestingTextShouldReturnMakeAndModel() throws Exception { // given(this.userService.loginUser("name", "password")) -// .willReturn(new UserDTO(1L, "name")); +// .willReturn(new UserDto(1L, "name")); // this.mvc.perform(get("/loginUser").accept(MediaType.APPLICATION_JSON)) -// .andExpect(status().isOk()).andExpect(content().json("name=name, password=password")); +// .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"); +// UserDto user = userService.loginUser("alex", "password"); // given(userService.loginUser("alex", "password")).willReturn(user); // mvc.perform(get("/loginUser") // .contentType(MediaType.ALL)) 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 467f675..9ef7169 100644 --- a/src/Server/src/test/java/greenify/server/service/UserServiceTest.java +++ b/src/Server/src/test/java/greenify/server/service/UserServiceTest.java @@ -1,7 +1,11 @@ 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.common.UserDto; import greenify.server.data.model.User; import greenify.server.data.repository.UserRepository; import org.junit.Before; @@ -12,9 +16,6 @@ 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; -import static org.junit.Assert.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.Mockito.when; @RunWith(SpringRunner.class) public class UserServiceTest { @@ -32,6 +33,9 @@ public class UserServiceTest { @MockBean private UserRepository userRepository; + /** + * setUp method for test. + */ @Before public void setUp() { User alex = new User(1L, "alex", "password", 0); @@ -43,18 +47,18 @@ public class UserServiceTest { public void validLoginTest() { String name = "alex"; String password = "password"; - UserDTO found = userService.loginUser(name, 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 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() { From 0d0b78f6d0b3b2621858e30b36f6b7f1b4c10624 Mon Sep 17 00:00:00 2001 From: Ceren Ugurlu Date: Sun, 17 Mar 2019 16:13:05 +0000 Subject: [PATCH 07/14] Update Application.java --- .../src/main/java/greenify/client/Application.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Client/src/main/java/greenify/client/Application.java b/src/Client/src/main/java/greenify/client/Application.java index 77ae0c0..bd03e7c 100644 --- a/src/Client/src/main/java/greenify/client/Application.java +++ b/src/Client/src/main/java/greenify/client/Application.java @@ -26,11 +26,16 @@ public class Application extends javafx.application.Application { * @param url which is being loaded. * @return parent object. */ - public static Parent load(java.net.URL url) throws IOException { + public static Parent load(java.net.URL url) { FXMLLoader loader = new FXMLLoader(); loader.setControllerFactory(springContext::getBean); loader.setLocation(url); - return loader.load(); + try { + return loader.load(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; } @Override From 72c8a213a15b2e61ef05066c77e74afb0fbc656a Mon Sep 17 00:00:00 2001 From: Merel Steenbergen Date: Thu, 14 Mar 2019 14:04:04 +0100 Subject: [PATCH 08/14] Add outline of activity class, has bugs --- .../java/greenify/common/ActivityDTO.java | 34 ++++++ .../greenify/server/data/model/Activity.java | 102 ++++++++++++++++++ .../java/greenify/server/data/model/User.java | 1 - 3 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 src/Common/src/main/java/greenify/common/ActivityDTO.java create mode 100644 src/Server/src/main/java/greenify/server/data/model/Activity.java diff --git a/src/Common/src/main/java/greenify/common/ActivityDTO.java b/src/Common/src/main/java/greenify/common/ActivityDTO.java new file mode 100644 index 0000000..6ed2fbc --- /dev/null +++ b/src/Common/src/main/java/greenify/common/ActivityDTO.java @@ -0,0 +1,34 @@ +package greenify.common; + +public class ActivityDTO { + private Long id; + private String name; + private String description; + private int score; + + public ActivityDTO() { + } + + public ActivityDTO(Long id, String name, String description, int score) { + this.id = id; + this.name = name; + this.description=description; + this.score= score; + } + + public String getName() { + return name; + } + + public Long getId() { + return id; + } + + public String getDescription() { + return description; + } + + public int getScore() { + return score; + } +} diff --git a/src/Server/src/main/java/greenify/server/data/model/Activity.java b/src/Server/src/main/java/greenify/server/data/model/Activity.java new file mode 100644 index 0000000..99635e4 --- /dev/null +++ b/src/Server/src/main/java/greenify/server/data/model/Activity.java @@ -0,0 +1,102 @@ +package greenify.server.data.model; + +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import java.util.Scanner; + +@Entity +@Data +//@AllArgsConstructor +public abstract class Activity { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + Long id; + String name; + String description; + int score; + + /** + * makes an activity object. + * @param id the id of the activity. + * @param name the name of the feature. + * @param description the description of the feature. + * @param score the amount of points a user gets for doing this activity. + */ + public void User(Long id, String name, String description, int score) { + this.id = id; + this.name = name; + this.description = setDes(description); + this.score = score; + } + + /** + * gets the id. + * @return the id + */ + public Long getId() { + return id; + } + + /** + * gets the name. + * @return the name + */ + public String getName() { + return name; + } + + /** + * gets the password. + * @return the password + */ + public String getDescription() { + return description; + } + + public void setId(Long id) { + this.id = id; + } + + /** + * sets the name. + * @param name the you want to assign to this.name. + */ + public void setName(String name) { + this.name = name; + } + + /** + * sets the description. + * @param description the description to be set. + */ + public String setDes(String description){ + Scanner scan = new Scanner(System.in); + System.out.println("Please enter the description"); + description = scan.nextLine(); + scan.close(); + return description; + } + + /** + * sets the score you get for an activity. + * @param score the you want to assign to the activity. + */ + public void setScore(int score) { + this.score = score; + } + + /** + * Returns a human readable object. It's in JSON. + * @return the JSON form of the object. + */ + @Override + public String toString() { + return "Activity(id=" + this.id + ", name=" + this.name + ", description=" + this.description + ", score=" + this.score + ")"; + } +} \ 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 5d3057a..38b6187 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 @@ -11,7 +11,6 @@ import javax.persistence.Id; @Entity @Data //@AllArgsConstructor -@NoArgsConstructor public class User { @Id From 82d3f08e2d05cd48b358acb48508ce7e1fe6ddd5 Mon Sep 17 00:00:00 2001 From: Merel Steenbergen Date: Thu, 14 Mar 2019 14:31:38 +0100 Subject: [PATCH 09/14] Fix bugs. Feature abstract class added --- src/Server/src/main/java/greenify/server/data/model/User.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 38b6187..6e703d1 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 @@ -11,6 +11,7 @@ import javax.persistence.Id; @Entity @Data //@AllArgsConstructor +@NoArgsConstructor public class User { @Id @@ -72,4 +73,4 @@ public class User { return "User(id=" + this.id + ", name=" + this.name + ", password=" + this.password + ")"; } -} \ No newline at end of file +} From 454a0ec1a360cb16cbea884fe041f6b3b2b65d78 Mon Sep 17 00:00:00 2001 From: Merel Steenbergen Date: Thu, 14 Mar 2019 15:08:49 +0100 Subject: [PATCH 10/14] Some more work done on the feature object --- .../greenify/client/rest/ActivityService.java | 25 ++++++++++++++++++ .../java/greenify/common/ActivityDTO.java | 2 +- .../server/rest/ActivityController.java | 26 +++++++++++++++++++ .../server/service/ActivityService.java | 6 +++++ 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/Client/src/main/java/greenify/client/rest/ActivityService.java create mode 100644 src/Server/src/main/java/greenify/server/rest/ActivityController.java create mode 100644 src/Server/src/main/java/greenify/server/service/ActivityService.java diff --git a/src/Client/src/main/java/greenify/client/rest/ActivityService.java b/src/Client/src/main/java/greenify/client/rest/ActivityService.java new file mode 100644 index 0000000..fe69b90 --- /dev/null +++ b/src/Client/src/main/java/greenify/client/rest/ActivityService.java @@ -0,0 +1,25 @@ +package greenify.client.rest; + +import greenify.common.ActivityDTO; +import greenify.common.ApplicationException; +import greenify.common.UserDTO; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; + +public class ActivityService { + + Logger logger = LoggerFactory.getLogger(greenify.server.service.ActivityService.class); + @Autowired + UserRepository userRepository; + + /** + * Adds the activity to the history of the user and adds the score. + * @param name the name of the activity + * @return a activityDTO of activity. + */ + public ActivityDTO addActivity(String name) { + + } + +} diff --git a/src/Common/src/main/java/greenify/common/ActivityDTO.java b/src/Common/src/main/java/greenify/common/ActivityDTO.java index 6ed2fbc..a564f1e 100644 --- a/src/Common/src/main/java/greenify/common/ActivityDTO.java +++ b/src/Common/src/main/java/greenify/common/ActivityDTO.java @@ -12,7 +12,7 @@ public class ActivityDTO { public ActivityDTO(Long id, String name, String description, int score) { this.id = id; this.name = name; - this.description=description; + this.description = description; this.score= score; } diff --git a/src/Server/src/main/java/greenify/server/rest/ActivityController.java b/src/Server/src/main/java/greenify/server/rest/ActivityController.java new file mode 100644 index 0000000..e2f9f2d --- /dev/null +++ b/src/Server/src/main/java/greenify/server/rest/ActivityController.java @@ -0,0 +1,26 @@ +package greenify.server.rest; + +import greenify.common.UserDTO; +import greenify.server.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class ActivityController { + @Autowired + UserService userService; + + @RequestMapping("/addActivity") + 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); + } +} \ No newline at end of file diff --git a/src/Server/src/main/java/greenify/server/service/ActivityService.java b/src/Server/src/main/java/greenify/server/service/ActivityService.java new file mode 100644 index 0000000..a9c2bbc --- /dev/null +++ b/src/Server/src/main/java/greenify/server/service/ActivityService.java @@ -0,0 +1,6 @@ +package greenify.server.service; + +public class ActivityService { + + +} From a80aaf3a59f5781eaabc8c3392a3a97106950276 Mon Sep 17 00:00:00 2001 From: Merel Steenbergen Date: Sun, 17 Mar 2019 16:57:09 +0100 Subject: [PATCH 11/14] Finish activity class and add tests --- .../greenify/client/rest/ActivityService.java | 25 ----- .../greenify/server/data/model/Activity.java | 68 ++++++++---- .../java/greenify/server/data/model/User.java | 100 ++++++++++++++---- .../server/service/ActivityService.java | 6 -- .../greenify/server/service/UserService.java | 2 +- src/Server/src/test/java/ActivityTest.java | 48 +++++++++ src/Server/src/test/java/UserTest.java | 33 ++++-- 7 files changed, 203 insertions(+), 79 deletions(-) delete mode 100644 src/Client/src/main/java/greenify/client/rest/ActivityService.java delete mode 100644 src/Server/src/main/java/greenify/server/service/ActivityService.java create mode 100644 src/Server/src/test/java/ActivityTest.java diff --git a/src/Client/src/main/java/greenify/client/rest/ActivityService.java b/src/Client/src/main/java/greenify/client/rest/ActivityService.java deleted file mode 100644 index fe69b90..0000000 --- a/src/Client/src/main/java/greenify/client/rest/ActivityService.java +++ /dev/null @@ -1,25 +0,0 @@ -package greenify.client.rest; - -import greenify.common.ActivityDTO; -import greenify.common.ApplicationException; -import greenify.common.UserDTO; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; - -public class ActivityService { - - Logger logger = LoggerFactory.getLogger(greenify.server.service.ActivityService.class); - @Autowired - UserRepository userRepository; - - /** - * Adds the activity to the history of the user and adds the score. - * @param name the name of the activity - * @return a activityDTO of activity. - */ - public ActivityDTO addActivity(String name) { - - } - -} diff --git a/src/Server/src/main/java/greenify/server/data/model/Activity.java b/src/Server/src/main/java/greenify/server/data/model/Activity.java index 99635e4..01a2e02 100644 --- a/src/Server/src/main/java/greenify/server/data/model/Activity.java +++ b/src/Server/src/main/java/greenify/server/data/model/Activity.java @@ -3,20 +3,20 @@ package greenify.server.data.model; import lombok.Data; import lombok.NoArgsConstructor; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; +import javax.accessibility.AccessibleValue; +import javax.persistence.*; +import java.util.Objects; import java.util.Scanner; @Entity @Data +@Table(name = "activities") //@AllArgsConstructor -public abstract class Activity { +public class Activity { @Id @GeneratedValue(strategy = GenerationType.AUTO) - Long id; + long id; String name; String description; int score; @@ -28,10 +28,10 @@ public abstract class Activity { * @param description the description of the feature. * @param score the amount of points a user gets for doing this activity. */ - public void User(Long id, String name, String description, int score) { + public Activity(long id, String name, String description, int score) { this.id = id; this.name = name; - this.description = setDes(description); + this.description = description; this.score = score; } @@ -39,7 +39,7 @@ public abstract class Activity { * gets the id. * @return the id */ - public Long getId() { + public long getId() { return id; } @@ -52,14 +52,27 @@ public abstract class Activity { } /** - * gets the password. - * @return the password + * gets the description. + * @return the description */ public String getDescription() { return description; } - public void setId(Long id) { + /** + * gets the score. + * @return the score + */ + public int getScore() { + return score; + } + + + /** + * sets the id. + * @param id the you want to assign to this.id. + */ + public void setId(long id) { this.id = id; } @@ -75,12 +88,8 @@ public abstract class Activity { * sets the description. * @param description the description to be set. */ - public String setDes(String description){ - Scanner scan = new Scanner(System.in); - System.out.println("Please enter the description"); - description = scan.nextLine(); - scan.close(); - return description; + public void setDescription(String description){ + this.description = description; } /** @@ -91,6 +100,7 @@ public abstract class Activity { this.score = score; } + /** * Returns a human readable object. It's in JSON. * @return the JSON form of the object. @@ -99,4 +109,26 @@ public abstract class Activity { public String toString() { return "Activity(id=" + this.id + ", name=" + this.name + ", description=" + this.description + ", score=" + this.score + ")"; } + + @Override + public boolean equals(Object other) { + if(other instanceof Activity){ + Activity that = (Activity)other; + if(that.id != this.id) + return false; + if(!that.name.equals(this.name)) + return false; + if(!that.description.equals(this.description)) + return false; + if(that.score != this.score) + return false; + return true; + } + return false; + } + + @Override + public int hashCode() { + return Objects.hash(id, name, description, score); + } } \ 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 6e703d1..d588eb1 100644 --- a/src/Server/src/main/java/greenify/server/data/model/User.java +++ b/src/Server/src/main/java/greenify/server/data/model/User.java @@ -1,35 +1,44 @@ package greenify.server.data.model; import lombok.Data; -import lombok.NoArgsConstructor; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; +import javax.persistence.*; +import javax.validation.constraints.NotNull; +import java.util.Objects; +@EnableAutoConfiguration @Entity @Data -//@AllArgsConstructor -@NoArgsConstructor +@Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) - Long id; - String name; - String password; + private Long id; + + @NotNull + private String name; + + @NotNull + private String password; + + private int veganMeal; + + public User() {} /** * makes a user object. * @param id the id of the user. * @param name the supplied username * @param password the supplied password + * @param veganMeal the supplied number of vegan meal */ - public User(Long id, String name, String password) { + public User(Long id, String name, String password, int veganMeal) { this.id = id; this.name = name; this.password = password; + this.veganMeal = veganMeal; } /** @@ -40,6 +49,8 @@ public class User { return id; } + public void setId(Long id) { this.id = id; } + /** * gets the name. * @return the name @@ -48,6 +59,8 @@ public class User { return name; } + public void setName(String name) { this.name = name; } + /** * gets the password. * @return the password @@ -56,21 +69,68 @@ public class User { return password; } - public void setId(Long id) { - this.id = id; + public void setPassword(String password) { this.password = password; } + + /** + * gets the number of vegan meal. + * @return the veganMeal + */ + public int getVeganMeal() { + return veganMeal; } - public void setName(String name) { - this.name = name; - } - - public void setPassword(String password) { - this.password = password; + public void setVeganMeal(int veganMeal) { this.veganMeal = veganMeal; } + + /** + * checks if two users are equal. + * @param other the object to compare the user with + * @return a boolean value of true if the user is equal to the object + */ + @Override + public boolean equals(Object other) { + + if (this == other) { + return true; + } + if (other == null) { + return false; + } + if (getClass() != other.getClass()) { + return false; + } + if (other instanceof User) { + User that = (User) other; + return this.getName().equals(that.getName()) + && this.getId().equals(that.getId()) + && this.getPassword().equals(that.getPassword()) + && this.getVeganMeal() == that.getVeganMeal(); + } + return false; } + /** + * creates a string of the user object. + * in the form of: User(id=, name=, password=, veganMeal=) + * @return a string of the user object + */ @Override public String toString() { + return "User(id=" + + this.id + + ", name=" + + this.name + + ", password=" + + this.password + + ", veganMeal=" + + this.veganMeal + ")"; + } - return "User(id=" + this.id + ", name=" + this.name + ", password=" + this.password + ")"; + /** + * hashes the User object. + * @return a hashcode for the user object + */ + @Override + public int hashCode() { + return Objects.hash(id, name, password, veganMeal); } } diff --git a/src/Server/src/main/java/greenify/server/service/ActivityService.java b/src/Server/src/main/java/greenify/server/service/ActivityService.java deleted file mode 100644 index a9c2bbc..0000000 --- a/src/Server/src/main/java/greenify/server/service/ActivityService.java +++ /dev/null @@ -1,6 +0,0 @@ -package greenify.server.service; - -public class ActivityService { - - -} 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 4756726..a844a1d 100644 --- a/src/Server/src/main/java/greenify/server/service/UserService.java +++ b/src/Server/src/main/java/greenify/server/service/UserService.java @@ -26,7 +26,7 @@ public class UserService { if (user != null) { throw new ApplicationException("User already exists"); } else { - user = userRepository.save(new User(null, name, password)); + user = userRepository.save(new User(null, name, password, 0)); } logger.info("Created user id=" + user.getId() + ", name=" + user.getName()); return new UserDTO(user.getId(), user.getName()); diff --git a/src/Server/src/test/java/ActivityTest.java b/src/Server/src/test/java/ActivityTest.java new file mode 100644 index 0000000..7a491e2 --- /dev/null +++ b/src/Server/src/test/java/ActivityTest.java @@ -0,0 +1,48 @@ +import greenify.server.data.model.Activity; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class ActivityTest { + @Test + public void setAndGetTest() { + Activity activity = new Activity(1, "Vegetarian", "Eating", 100); + Activity testActivity = new Activity(0, null, null, 0); + testActivity.setId(1); + testActivity.setName("Vegetarian"); + testActivity.setDescription("Eating"); + testActivity.setScore(100); + assertEquals(activity.getId(), 1); + assertEquals(activity.getName(), "Vegetarian"); + assertEquals(activity.getDescription(), "Eating"); + assertEquals(activity.getScore(), 100); + assertEquals(activity, testActivity); + } + + @Test + public void toStringTest() { + Activity activity = new Activity(1, "Solar panels", "Installed", 10000); + assertEquals("Activity(id=1, name=Solar panels, description=Installed, score=10000)", activity.toString()); + } + + @Test + public void equalsTest() { + Activity first = new Activity(1, "Solar panels", "Installed", 10000); + Activity second = new Activity(1, "Solar panels", "Installed", 10000); + assertEquals(first.getId(), second.getId()); + assertEquals(first.getName(), second.getName()); + assertEquals(first.getDescription(), second.getDescription()); + assertEquals(first.getScore(), second.getScore()); + assertTrue(first.equals(second)); + } + + @Test + public void hashCodeTest() { + Activity first = new Activity(1, "Solar panels", "Installed", 10000); + Activity second = new Activity(1, "Solar panels", "Installed", 10000); + assertEquals(first, second); + assertEquals(first.hashCode(), second.hashCode()); +// assertTrue(first.hashCode() == second.hashCode()); + } +} diff --git a/src/Server/src/test/java/UserTest.java b/src/Server/src/test/java/UserTest.java index ae359c9..3e557ca 100644 --- a/src/Server/src/test/java/UserTest.java +++ b/src/Server/src/test/java/UserTest.java @@ -6,36 +6,51 @@ import static org.junit.jupiter.api.Assertions.assertTrue; public class UserTest { @Test public void setAndGetTest() { - User user = new User(1L, "greenify", "password"); - User testUser = new User(null, null, null); + User user = new User(1L, "greenify", "password", 3); + User testUser = new User(); testUser.setId(1L); testUser.setName("greenify"); testUser.setPassword("password"); + testUser.setVeganMeal(3); assertTrue(user.getId().equals(1L)); assertEquals(user.getName(), "greenify"); assertEquals(user.getPassword(), "password"); - assertEquals(user, testUser); + assertEquals(user.getVeganMeal(), 3); + + } + + @Test + public void equalsMethodTest() { + User user = new User(1L, "greenify", "password", 3); + User testUser = new User(); + testUser.setId(1L); + testUser.setName("greenify"); + testUser.setPassword("password"); + testUser.setVeganMeal(3); + assertTrue(user.equals(testUser)); } @Test public void toStringTest() { - User user = new User(1L, "greenify", "password"); - assertEquals("User(id=1, name=greenify, password=password)", user.toString()); + User user = new User(1L, "greenify", "password", 3); + System.out.println("String is " + user.toString()); + assertTrue("User(id=1, name=greenify, password=password, veganMeal=3)".equals(user.toString())); } @Test public void equalsTest() { - User first = new User(1L, "greenify", "password"); - User second = new User(1L, "greenify", "password"); + 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"); - User second = new User(1L, "greenify", "password"); + 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()); } From 20b6e89781cd611da3044e42d5222a3f8deb53d0 Mon Sep 17 00:00:00 2001 From: cugurlu Date: Sun, 17 Mar 2019 17:27:29 +0100 Subject: [PATCH 12/14] Fix checkstyle errors --- .../src/main/java/greenify/client/Application.java | 9 ++------- src/Common/src/main/java/greenify/common/UserDTO.java | 1 - src/Common/src/test/java/ErrorResponseTest.java | 7 ++++--- src/Common/src/test/java/UserDTOTest.java | 6 +++--- 4 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/Client/src/main/java/greenify/client/Application.java b/src/Client/src/main/java/greenify/client/Application.java index bd03e7c..77ae0c0 100644 --- a/src/Client/src/main/java/greenify/client/Application.java +++ b/src/Client/src/main/java/greenify/client/Application.java @@ -26,16 +26,11 @@ public class Application extends javafx.application.Application { * @param url which is being loaded. * @return parent object. */ - public static Parent load(java.net.URL url) { + public static Parent load(java.net.URL url) throws IOException { FXMLLoader loader = new FXMLLoader(); loader.setControllerFactory(springContext::getBean); loader.setLocation(url); - try { - return loader.load(); - } catch (Exception e) { - e.printStackTrace(); - } - return null; + return loader.load(); } @Override diff --git a/src/Common/src/main/java/greenify/common/UserDTO.java b/src/Common/src/main/java/greenify/common/UserDTO.java index 09e77f4..8c67cd2 100644 --- a/src/Common/src/main/java/greenify/common/UserDTO.java +++ b/src/Common/src/main/java/greenify/common/UserDTO.java @@ -5,7 +5,6 @@ package greenify.common; // 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; diff --git a/src/Common/src/test/java/ErrorResponseTest.java b/src/Common/src/test/java/ErrorResponseTest.java index d996b40..350e71f 100644 --- a/src/Common/src/test/java/ErrorResponseTest.java +++ b/src/Common/src/test/java/ErrorResponseTest.java @@ -1,9 +1,10 @@ -import greenify.common.ErrorResponse; -import org.junit.Test; - 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() { diff --git a/src/Common/src/test/java/UserDTOTest.java b/src/Common/src/test/java/UserDTOTest.java index 604f486..1d1d75b 100644 --- a/src/Common/src/test/java/UserDTOTest.java +++ b/src/Common/src/test/java/UserDTOTest.java @@ -1,9 +1,9 @@ -import greenify.common.UserDto; -import org.junit.Test; - 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() { From 561cca7f5ae1a9d25d7fe7bca9bc8bb6cf312744 Mon Sep 17 00:00:00 2001 From: Ceren Ugurlu Date: Sun, 17 Mar 2019 16:29:56 +0000 Subject: [PATCH 13/14] Update UserDTO.java --- .../src/main/java/greenify/common/{UserDTO.java => UserDto.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/Common/src/main/java/greenify/common/{UserDTO.java => UserDto.java} (100%) diff --git a/src/Common/src/main/java/greenify/common/UserDTO.java b/src/Common/src/main/java/greenify/common/UserDto.java similarity index 100% rename from src/Common/src/main/java/greenify/common/UserDTO.java rename to src/Common/src/main/java/greenify/common/UserDto.java From 5b13f26deed00474e4245fdf8ac3b8dc3254e770 Mon Sep 17 00:00:00 2001 From: Ceren Ugurlu Date: Sun, 17 Mar 2019 16:30:41 +0000 Subject: [PATCH 14/14] Update UserDTOTest.java --- src/Common/src/test/java/{UserDTOTest.java => UserDtoTest.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/Common/src/test/java/{UserDTOTest.java => UserDtoTest.java} (100%) diff --git a/src/Common/src/test/java/UserDTOTest.java b/src/Common/src/test/java/UserDtoTest.java similarity index 100% rename from src/Common/src/test/java/UserDTOTest.java rename to src/Common/src/test/java/UserDtoTest.java