From 52291ac10fd460c0803dae8bedd8d0fa91ad456b Mon Sep 17 00:00:00 2001 From: Merel Steenbergen Date: Mon, 1 Apr 2019 16:36:12 +0200 Subject: [PATCH 1/7] ADD:: Friend remove methods and tests in server.User --- build.gradle | 6 +++--- .../java/greenify/server/data/model/User.java | 13 ++++++++++++ .../greenify/server/data/model/UserTest.java | 21 +++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index 2b6ed72..98f5661 100644 --- a/build.gradle +++ b/build.gradle @@ -18,9 +18,9 @@ apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' apply plugin: 'application' -application { - mainClassName = 'greenify.server.Application' -} +//application { +// mainClassName = 'greenify.server.Application' +//} repositories { mavenCentral() 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 9f1860b..9227fde 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 @@ -208,6 +208,19 @@ public class User { } } + /** + * Removes a friend from the friendslist of the user. + * @param user the friend you want to remove. + */ + public void removeFriend(User user) { + if (!friends.contains(user)) { + throw new ApplicationException("This user is not your friend!"); + } else { + friends.remove(user); + System.out.print("Friend removed"); + } + } + /** * This method gets a human readable (JSON) object. * @return the JSON form of the object. 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 f95b2c4..dcfd702 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 @@ -125,6 +125,27 @@ public class UserTest { }); } + @Test + public void removeFriendValidTest() { + User test = new User(1L, "greenify", "password"); + List friendList = new ArrayList<>(); + friendList.add(test); + User user = new User(1L, "green", "pass"); + user.setFriends(friendList); + assertEquals(user.getFriends(), friendList); + user.removeFriend(test); + assertEquals(user.getFriends(), new ArrayList()); + } + + @Test + public void removeFriendInvalidTest() { + User user = new User(1L, "greenify", "password"); + User test = new User(2L, "user", "pass"); + assertThrows(ApplicationException.class, () -> { + user.removeFriend(test); + }); + } + @Test public void setFriendTest() { List friends = new ArrayList(); From 6d37b99c14f0d5d73f293842da147127a91797b6 Mon Sep 17 00:00:00 2001 From: Merel Steenbergen Date: Mon, 1 Apr 2019 16:43:10 +0200 Subject: [PATCH 2/7] ADD:: RemoveFriend method and tests in Server.UserService --- .../greenify/server/service/UserService.java | 20 ++++++++++++++++-- .../server/service/UserServiceTest.java | 21 +++++++++++++++---- 2 files changed, 35 insertions(+), 6 deletions(-) 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 f4cea39..685d618 100644 --- a/src/Server/src/main/java/greenify/server/service/UserService.java +++ b/src/Server/src/main/java/greenify/server/service/UserService.java @@ -64,21 +64,37 @@ public class UserService { } /** -<<<<<<< HEAD * Adds a friend to the friendlist of the user. * @param name the username of the user * @param friend the name of the friend you want to add. + * @throws ApplicationException if the user is not in the database. */ public void addFriend(String name, String friend) { User user = userRepository.findByName(name); User add = userRepository.findByName(friend); - if (user == null || add == null ) { + if (add == null ) { throw new ApplicationException("User does not exist"); } user.addFriend(add); userRepository.save(user); } + /** + * Removes a friend from the friendlist of the user. + * @param name the username of the user + * @param friend the name of the friend you want to remove. + * @throws ApplicationException if the user is not in the database. + */ + public void removeFriend(String name, String friend) { + User user = userRepository.findByName(name); + User remove = userRepository.findByName(friend); + if (remove == null ) { + throw new ApplicationException("User does not exist"); + } + user.removeFriend(remove); + userRepository.save(user); + } + /** * This method sets input for a user. * @param name name of the user 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 80631e0..61dc5c5 100644 --- a/src/Server/src/test/java/greenify/server/service/UserServiceTest.java +++ b/src/Server/src/test/java/greenify/server/service/UserServiceTest.java @@ -266,12 +266,25 @@ public class UserServiceTest { } @Test - public void addFriendNullFriendTest() { - assertThrows(ApplicationException.class, () -> userService.addFriend("alex", null)); + public void removeFriendTest() { + User alex = userRepository.findByName("alex"); + User lola = userRepository.findByName("lola"); + assertEquals(lola.getFriends(), alex.getFriends()); + userService.addFriend("alex", "lola"); + ArrayList test = new ArrayList(); + test.add(lola); + assertEquals(alex.getFriends(), test); + userService.removeFriend("alex", "lola"); + assertEquals(lola.getFriends(), alex.getFriends()); } @Test - public void addFriendNullUserTest() { - assertThrows(ApplicationException.class, () -> userService.addFriend(null, "password")); + public void removeFriendNullTest() { + assertThrows(ApplicationException.class, () -> userService.removeFriend("alex", null)); + } + + @Test + public void addFriendNullFriendTest() { + assertThrows(ApplicationException.class, () -> userService.addFriend("alex", null)); } } From 12b05f3b732a8a24e267f7db308cf12da412bb70 Mon Sep 17 00:00:00 2001 From: Merel Steenbergen Date: Mon, 1 Apr 2019 16:46:01 +0200 Subject: [PATCH 3/7] ADD:: RemoveFriend method and test in client.userService --- .../java/greenify/client/rest/UserService.java | 18 ++++++++++++++++++ src/Client/src/test/java/UserServiceTest.java | 8 ++++++++ 2 files changed, 26 insertions(+) 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 9252cc8..e0bd827 100644 --- a/src/Client/src/main/java/greenify/client/rest/UserService.java +++ b/src/Client/src/main/java/greenify/client/rest/UserService.java @@ -230,6 +230,24 @@ public class UserService { .encode().toUri(), String.class); } + /** + * Removes a friend from the friendslist of the user. + * @param name the username of the current user. + * @param friend the username of the friend you want to remove. + */ + @SuppressWarnings("Duplicates") + public void removeFriend(String name, String friend) { + HttpHeaders headers = new HttpHeaders(); + headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/removeFriend") + .queryParam("name", name) + .queryParam("friend",friend); + HttpEntity entity = new HttpEntity<>(headers); + System.out.println(builder.build().encode().toUri()); + ResponseEntity authenticateResponse = this.restTemplate.getForEntity(builder.build() + .encode().toUri(), String.class); + } + /** * Gets the footprint inputs of the user. * @param name the username of the current user. diff --git a/src/Client/src/test/java/UserServiceTest.java b/src/Client/src/test/java/UserServiceTest.java index 812bc3e..b7d1fa6 100644 --- a/src/Client/src/test/java/UserServiceTest.java +++ b/src/Client/src/test/java/UserServiceTest.java @@ -147,6 +147,14 @@ public class UserServiceTest { userService.addFriend("Eric", "Ceren"); Mockito.verify(userService).addFriend("Eric", "Ceren"); } + + @Test + public void removeFriendTest() throws Exception { + userService.addFriend("Eric", "Ceren"); + Mockito.verify(userService).addFriend("Eric", "Ceren"); + userService.removeFriend("Eric", "Ceren"); + Mockito.verify(userService).removeFriend("Eric", "Ceren"); + } } From e48264a667a8dd445b3c504e642ce9645bd2bf0c Mon Sep 17 00:00:00 2001 From: Merel Steenbergen Date: Mon, 1 Apr 2019 16:53:16 +0200 Subject: [PATCH 4/7] EDIT:: Uncomment build.gradle mainClassName --- build.gradle | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index 98f5661..2b6ed72 100644 --- a/build.gradle +++ b/build.gradle @@ -18,9 +18,9 @@ apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' apply plugin: 'application' -//application { -// mainClassName = 'greenify.server.Application' -//} +application { + mainClassName = 'greenify.server.Application' +} repositories { mavenCentral() From 7b6457b2865fb6f3930589ffa02a9e971f6cea7d Mon Sep 17 00:00:00 2001 From: Merel Steenbergen Date: Wed, 3 Apr 2019 14:19:30 +0200 Subject: [PATCH 5/7] ADD::RemoveFriend method and test in server.userController --- ...ft.pdf => 20190401_draft_final_report.pdf} | Bin .../greenify/server/rest/UserController.java | 16 ++++++++-- .../server/rest/UserControllerTest.java | 28 ++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) rename doc/reports/final-report/{Final report - draft.pdf => 20190401_draft_final_report.pdf} (100%) diff --git a/doc/reports/final-report/Final report - draft.pdf b/doc/reports/final-report/20190401_draft_final_report.pdf similarity index 100% rename from doc/reports/final-report/Final report - draft.pdf rename to doc/reports/final-report/20190401_draft_final_report.pdf 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 e0e28e8..6d282e9 100644 --- a/src/Server/src/main/java/greenify/server/rest/UserController.java +++ b/src/Server/src/main/java/greenify/server/rest/UserController.java @@ -155,13 +155,25 @@ public class UserController { } /** - * This method adds friend for a user. + * This method adds a friend to a user. * @param name name of the user - * + * @param friend the name of the user you want to add as a friend. */ @RequestMapping("/addFriend") public void addFriend(@RequestParam(value = "name") String name, @RequestParam(value = "friend") String friend) { userService.addFriend(name, friend); } + + /** + * This method removes a friend from a user. + * @param name name of the user + * @param friend name of the friend you want to remove + */ + @RequestMapping("/removeFriend") + public void removeFriend(@RequestParam(value = "name") String name, + @RequestParam(value = "friend") String friend) { + userService.removeFriend(name, friend); + } } + 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 1866605..4d5d459 100644 --- a/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java +++ b/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java @@ -113,6 +113,34 @@ public class UserControllerTest { assertEquals("merel", arg2Captor.getValue()); } + @Test + public void removeFriendTest() throws Exception { + ArgumentCaptor arg1Captor = ArgumentCaptor.forClass(String.class); + ArgumentCaptor arg2Captor = ArgumentCaptor.forClass(String.class); + mvc.perform(get("/addFriend") + .param("name", "ceren") + .param("friend", "merel") + .accept(MediaType.APPLICATION_JSON)) + .andDo(print()) + .andExpect(status().isOk()); + verify(userService, times(1)) + .addFriend(arg1Captor.capture(), arg2Captor.capture()); + assertEquals("ceren", arg1Captor.getValue()); + assertEquals("merel", arg2Captor.getValue()); + + mvc.perform(get("/removeFriend") + .param("name", "ceren") + .param("friend", "merel") + .accept(MediaType.APPLICATION_JSON)) + .andDo(print()) + .andExpect(status().isOk()); + verify(userService, times(1)). + removeFriend(arg1Captor.capture(), arg2Captor.capture()); + assertEquals("ceren", arg1Captor.getValue()); + assertEquals("merel", arg2Captor.getValue()); + + } + @Test public void getInputMapTest() throws Exception { ArgumentCaptor arg1Captor = ArgumentCaptor.forClass(String.class); From bb6d8870364daa996dcbdb1ad6da949ad28fe6ff Mon Sep 17 00:00:00 2001 From: Merel Steenbergen Date: Wed, 3 Apr 2019 15:33:26 +0200 Subject: [PATCH 6/7] Added class Hints, including tests --- .../src/main/java/greenify/client/Hints.java | 68 +++++++++++++++++++ .../controller/CalculatorController.java | 5 +- .../controller/DashBoardController.java | 1 + src/Client/src/test/java/HintsTest.java | 29 ++++++++ 4 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 src/Client/src/main/java/greenify/client/Hints.java create mode 100644 src/Client/src/test/java/HintsTest.java diff --git a/src/Client/src/main/java/greenify/client/Hints.java b/src/Client/src/main/java/greenify/client/Hints.java new file mode 100644 index 0000000..34aa3fb --- /dev/null +++ b/src/Client/src/main/java/greenify/client/Hints.java @@ -0,0 +1,68 @@ +package greenify.client; + +import java.util.ArrayList; +import java.util.Random; + +public class Hints { + public ArrayList hints; + + public Hints() { + this.hints = new ArrayList(); + initHints(); + } + + /** + * This method adds all the Strings to the arraylist. + */ + private void initHints() { + hints.add("Buying local produce will not only decrease your carbon " + + "footprint, but also help your local economy: A win-win!"); + hints.add("Did you know that a gas oven only uses 6% of its energy " + + "to cook? And an electric oven is not much better at 12%."); + hints.add("70% of the deforestation of the Amazon is to provide land for cattle ranches."); + hints.add("Research shows that reducing meat consumption can increase" + + " your life span by 3.6 years"); + hints.add("Vegetarians have a lower risk of getting heart disease, high blood pressure, " + + "diabetes and cancer than meat eaters."); + hints.add("Did you know? The carbon footprint of a vegetarian diet is about half " + + "that of a meat-lover’s diet!"); + hints.add("Cycling is good for the environment AND for your body, " + + "so why not grab your bike instead of your car?"); + hints.add("If we could capture all of the sun’s energy shining on the Earth for just one " + + "hour, we could power the entire world for one year!"); + hints.add("27,000 trees are cut down each day so we can have toilet paper."); + hints.add("A glass bottle made in our time will take more than 4,000 years to decompose."); + hints.add("Don't forget to turn off the lights and heating in rooms" + + " you're not using at the moment!"); + hints.add("Did you know that about 4.5% of the Dutch population does not eat meat"); + hints.add("Reuse your bags when you go grocery shopping. You will save plastic bags."); + hints.add("An estimated 250 million trees can be save each year " + + "if every published newspaper is recycled."); + hints.add("About 88,000 jobs were created in 2015 through the wind power sector."); + hints.add("You can use LED lights in your home to safe energy!"); + hints.add("If you isolate your home well, it will be warmer " + + "and you'll save energy as well!"); + hints.add("Do you have leftovers? Donate them to food kitchens. This way you won't waste " + + "food and help people at the same time."); + hints.add("A lot of coffee places give you a discount if you bring your own cup. " + + "Get rid of those disposable cups!"); + hints.add("When shopping, look for products with minimal to no packaging, " + + "or at least packaging made from recycled items. "); + hints.add("If you order food, you can ask the restaurant to not include " + + "utensils and napkins, it saves plastic and paper."); + hints.add("It takes about 66 days to form a new habit, keep going!"); + hints.add("Get yourself a nice reusable water bottle! It's cheaper and better for " + + "the environment to refill than to buy a new one every time it's empty."); + } + + /** + * This method returns a random hint from the list of hints. + * @return the random hint. + */ + public String randomHint() { + Random rand = new Random(); + int index = rand.nextInt(hints.size()); + return hints.get(index); + } + +} diff --git a/src/Client/src/main/java/greenify/client/controller/CalculatorController.java b/src/Client/src/main/java/greenify/client/controller/CalculatorController.java index 5098636..a44e0ef 100644 --- a/src/Client/src/main/java/greenify/client/controller/CalculatorController.java +++ b/src/Client/src/main/java/greenify/client/controller/CalculatorController.java @@ -9,9 +9,7 @@ import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.fxml.FXML; -import javafx.fxml.FXMLLoader; import javafx.scene.Node; -import javafx.scene.Parent; import javafx.scene.control.Button; import javafx.scene.control.CheckBox; import javafx.scene.control.Label; @@ -338,7 +336,8 @@ public class CalculatorController { @SuppressWarnings("Duplicates") public void displayExtra(ActionEvent event) throws IOException { - // Parent extra = FXMLLoader.load(getClass().getClassLoader().getResource("fxml/extraActivities.fxml")); + // Parent extra = FXMLLoader.load(getClass().getClassLoader() + // .getResource("fxml/extraActivities.fxml")); // extraPane.getChildren().setAll(extra); getStartedPane.setVisible(false); travelPane.setVisible(false); 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 259ed42..b43433d 100644 --- a/src/Client/src/main/java/greenify/client/controller/DashBoardController.java +++ b/src/Client/src/main/java/greenify/client/controller/DashBoardController.java @@ -495,4 +495,5 @@ public class DashBoardController { } } + } \ No newline at end of file diff --git a/src/Client/src/test/java/HintsTest.java b/src/Client/src/test/java/HintsTest.java new file mode 100644 index 0000000..c0e2442 --- /dev/null +++ b/src/Client/src/test/java/HintsTest.java @@ -0,0 +1,29 @@ +import static junit.framework.TestCase.assertTrue; +import static org.junit.Assert.assertFalse; + +import greenify.client.Hints; +import org.junit.Test; + +public class HintsTest { + @Test + public void initHintsTest() { + Hints test = new Hints(); + assertFalse(test.hints.isEmpty()); + } + + @Test + public void hintsContainsTest() { + Hints test = new Hints(); + assertTrue(test.hints.contains("27,000 trees are cut down " + + "each day so we can have toilet paper.")); + } + + @Test + public void randomHintTest() { + Hints test = new Hints(); + String random = test.randomHint(); + assertTrue(test.hints.contains(random)); + } +} + + From 01e03782c2b73eb9331143195322aaa95951864a Mon Sep 17 00:00:00 2001 From: Merel Steenbergen Date: Wed, 3 Apr 2019 13:40:53 +0000 Subject: [PATCH 7/7] Remove addFriend part from removeFriendTest from UserControllerTest.java --- .../java/greenify/server/rest/UserControllerTest.java | 11 ----------- 1 file changed, 11 deletions(-) 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 4d5d459..2a16852 100644 --- a/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java +++ b/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java @@ -117,17 +117,6 @@ public class UserControllerTest { public void removeFriendTest() throws Exception { ArgumentCaptor arg1Captor = ArgumentCaptor.forClass(String.class); ArgumentCaptor arg2Captor = ArgumentCaptor.forClass(String.class); - mvc.perform(get("/addFriend") - .param("name", "ceren") - .param("friend", "merel") - .accept(MediaType.APPLICATION_JSON)) - .andDo(print()) - .andExpect(status().isOk()); - verify(userService, times(1)) - .addFriend(arg1Captor.capture(), arg2Captor.capture()); - assertEquals("ceren", arg1Captor.getValue()); - assertEquals("merel", arg2Captor.getValue()); - mvc.perform(get("/removeFriend") .param("name", "ceren") .param("friend", "merel")