diff --git a/src/Client/src/main/java/greenify/client/Hints.java b/src/Client/src/main/java/greenify/client/Hints.java index 12b095a..daf7176 100644 --- a/src/Client/src/main/java/greenify/client/Hints.java +++ b/src/Client/src/main/java/greenify/client/Hints.java @@ -55,7 +55,7 @@ public class Hints { + "utensils and napkins, it saves plastic and paper."); this.hints.add("It takes about 66 days to form a new habit, keep going!"); this.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."); + + "the environment to refill than to buy a new one every time it's empty."); this.hints.add("Recycle glass bottles!" + " A glass bottle made in our time will take more than 4,000 years" + " to decompose."); @@ -75,3 +75,4 @@ public class Hints { } } + 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 fe09235..357a295 100644 --- a/src/Client/src/main/java/greenify/client/controller/DashBoardController.java +++ b/src/Client/src/main/java/greenify/client/controller/DashBoardController.java @@ -1,6 +1,5 @@ package greenify.client.controller; -import com.sun.javafx.scene.control.skin.ButtonSkin; import greenify.client.Application; import greenify.client.Friend; import greenify.client.Hints; @@ -21,6 +20,7 @@ import javafx.scene.control.Label; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.cell.PropertyValueFactory; +import javafx.scene.control.skin.ButtonSkin; import javafx.scene.image.ImageView; import javafx.scene.layout.AnchorPane; import javafx.scene.shape.Line; diff --git a/src/Client/src/main/java/greenify/client/controller/ExtraActivityController.java b/src/Client/src/main/java/greenify/client/controller/ExtraActivityController.java index e14e30f..ee66111 100644 --- a/src/Client/src/main/java/greenify/client/controller/ExtraActivityController.java +++ b/src/Client/src/main/java/greenify/client/controller/ExtraActivityController.java @@ -1,6 +1,5 @@ package greenify.client.controller; -import com.sun.javafx.scene.control.skin.ButtonSkin; import greenify.client.rest.UserService; import javafx.animation.ScaleTransition; import javafx.animation.TranslateTransition; @@ -12,6 +11,7 @@ import javafx.scene.control.Alert; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.Slider; +import javafx.scene.control.skin.ButtonSkin; import javafx.scene.layout.AnchorPane; import javafx.stage.Stage; import javafx.stage.Window; 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 c808c15..89160be 100644 --- a/src/Client/src/main/java/greenify/client/controller/UserController.java +++ b/src/Client/src/main/java/greenify/client/controller/UserController.java @@ -1,10 +1,7 @@ package greenify.client.controller; -import com.sun.javafx.scene.control.skin.ButtonSkin; import greenify.client.Application; import greenify.client.rest.UserService; -import javafx.animation.FillTransition; -import javafx.animation.ScaleTransition; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.Parent; @@ -13,10 +10,8 @@ import javafx.scene.control.Alert; import javafx.scene.control.Button; import javafx.scene.control.PasswordField; import javafx.scene.control.TextField; -import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.stage.Window; -import javafx.util.Duration; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -40,11 +35,6 @@ public class UserController { @FXML private Button signUpButton; - public void initialize() { - loginButton.setSkin(new LoginButtonSkin(loginButton)); - signUpButton.setSkin(new LoginButtonSkin(signUpButton)); - } - /** * Handles when the user clicks on the login button. * it checks if the username and password fields are filled @@ -145,39 +135,11 @@ public class UserController { Parent registerWindow = Application.load(this.getClass().getClassLoader() .getResource("fxml/RegisterWindow.fxml")); Scene registerScene = new Scene(registerWindow); - registerScene.getStylesheets().add(Application.class.getClassLoader() - .getResource("stylesheets/registerWindowStyle.css").toExternalForm()); Stage registerStage = new Stage(); registerStage.setScene(registerScene); registerStage.setTitle("Enter register credentials"); registerStage.show(); } - @SuppressWarnings("Duplicates") - private class LoginButtonSkin extends ButtonSkin { - /** - * button skin for the 'add activity' buttons. - * adds scale animations on entering, clicking and extiting the button - * @param button the button to set the skin of - */ - private LoginButtonSkin(Button button) { - super(button); - - //transition to scale up on hover - final ScaleTransition scaleUp = new ScaleTransition(Duration.millis(85)); - //add the node and the position to scale to - scaleUp.setNode(button); - scaleUp.setToX(1.1); - scaleUp.setToY(1.1); - //play the transition when hovered over the button - button.setOnMouseEntered(e -> scaleUp.playFromStart()); - - final ScaleTransition scaleDown = new ScaleTransition(Duration.millis(85)); - scaleDown.setNode(button); - scaleDown.setToX(1.0); - scaleDown.setToY(1.0); - button.setOnMouseExited(e -> scaleDown.playFromStart()); - } - } - } + diff --git a/src/Client/src/main/resources/stylesheets/LoginWindowStyle.css b/src/Client/src/main/resources/stylesheets/LoginWindowStyle.css index 6bec58a..f44d45c 100644 --- a/src/Client/src/main/resources/stylesheets/LoginWindowStyle.css +++ b/src/Client/src/main/resources/stylesheets/LoginWindowStyle.css @@ -2,12 +2,4 @@ -fx-background-color: #005e07; -fx-text-fill: #c4eec9; -fx-font-weight: bold; -} - -.button:hover { - -fx-background-color: #11911b; -} - -.button:pressed { - -fx-background-color: #1a8c23; } \ No newline at end of file diff --git a/src/Client/src/main/resources/stylesheets/dashboardStyle.css b/src/Client/src/main/resources/stylesheets/dashboardStyle.css index e3e5f1b..a7eefaa 100644 --- a/src/Client/src/main/resources/stylesheets/dashboardStyle.css +++ b/src/Client/src/main/resources/stylesheets/dashboardStyle.css @@ -47,16 +47,6 @@ #friendsButton:pressed { -fx-background-color: #b7e2c2; } -#logOutButton { - -fx-background-color: #5a635c; - -fx-text-fill: #b8bcb9; -} -#logOutButton:hover { - -fx-background-color: #c64b4b; -} -#logOutButton:pressed { - -fx-background-color: #c9a9a9; -} #addNewActivityButton:pressed { diff --git a/src/Client/src/test/java/FriendTest.java b/src/Client/src/test/java/FriendTest.java new file mode 100644 index 0000000..7b13fce --- /dev/null +++ b/src/Client/src/test/java/FriendTest.java @@ -0,0 +1,16 @@ +import greenify.client.Friend; +import org.junit.Test; +import org.junit.jupiter.api.Assertions; + +import static org.junit.Assert.*; + +public class FriendTest { + @Test + public void setAndGetTest() { + Friend test = new Friend("ceren", 10.0); + test.setFriend("greenify"); + test.setScore(15.0); + assertEquals(test.getFriend(), "greenify"); + Assertions.assertEquals(test.getScore(), 15.0); + } +} diff --git a/src/Server/src/main/java/greenify/server/AllAchievements.java b/src/Server/src/main/java/greenify/server/AllAchievements.java index eed0744..e8db46a 100644 --- a/src/Server/src/main/java/greenify/server/AllAchievements.java +++ b/src/Server/src/main/java/greenify/server/AllAchievements.java @@ -37,5 +37,5 @@ public class AllAchievements { } return all; } - } + diff --git a/src/Server/src/main/java/greenify/server/InputValidator.java b/src/Server/src/main/java/greenify/server/InputValidator.java index 839151a..1dec743 100644 --- a/src/Server/src/main/java/greenify/server/InputValidator.java +++ b/src/Server/src/main/java/greenify/server/InputValidator.java @@ -185,14 +185,14 @@ public class InputValidator { * This method gets extra values. * @return the map of default values */ - public static Map getExtraValues() { - Map map = new HashMap() { }; - map.put("local_produce", false); - map.put("bike", false); - map.put("temperature", false); - map.put("solar_panels", false); - map.put("vegetarian", false); - map.put("public_transport", false); + public static Map getExtraValues() { + Map map = new HashMap() { }; + map.put("vegan", "0"); + map.put("local_produce", "0"); + map.put("bike", "0"); + map.put("car", "0"); + map.put("temperature", "0"); + map.put("solar_panels", "0"); return map; } } \ No newline at end of file diff --git a/src/Server/src/main/java/greenify/server/data/model/Achievement.java b/src/Server/src/main/java/greenify/server/data/model/Achievement.java index c0aa2e9..c9de55c 100644 --- a/src/Server/src/main/java/greenify/server/data/model/Achievement.java +++ b/src/Server/src/main/java/greenify/server/data/model/Achievement.java @@ -86,14 +86,11 @@ public class Achievement { */ @Override public boolean equals(Object other) { - if(other == null){ - return false; - } if (other instanceof Achievement) { Achievement that = (Achievement) other; return achieved == that.achieved && name.equals(that.name) - && Objects.equals(description, that.description); + && description.equals(that.description); } return false; } 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 1c5a2ea..d13b9ee 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 @@ -46,7 +46,7 @@ public class User { private Map footPrintInputs = new HashMap<>(); @ElementCollection - private Map extraInputs = new HashMap<>(); + private Map extraInputs = new HashMap<>(); @ManyToMany private List friends; @@ -172,7 +172,7 @@ public class User { * This method gets the extra inputs of the user. * @return extra inputs of the user */ - public Map getExtraInputs() { + public Map getExtraInputs() { return extraInputs; } @@ -180,7 +180,7 @@ public class User { * This method sets the extra inputs of the user. * @param extraInputs footprint inputs of the user */ - public void setExtraInputs(Map extraInputs) { + public void setExtraInputs(Map extraInputs) { this.extraInputs = extraInputs; } 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 34448f6..5b96baa 100644 --- a/src/Server/src/main/java/greenify/server/rest/UserController.java +++ b/src/Server/src/main/java/greenify/server/rest/UserController.java @@ -63,7 +63,7 @@ public class UserController { @RequestMapping("/setExtraInput") public void setExtraInput(@RequestParam(value = "name") String name, @RequestParam(value = "inputName") String inputName, - @RequestParam(value = "value") Boolean value) { + @RequestParam(value = "value") String value) { userService.setExtraInput(name, inputName, value); } @@ -150,7 +150,7 @@ public class UserController { * This method gets the extra inputs map of the user. */ @RequestMapping("/getExtraInputs") - public Map getExtraInputs(@RequestParam(value = "name") String name) { + public Map getExtraInputs(@RequestParam(value = "name") String name) { return userService.getExtraInputMap(name); } diff --git a/src/Server/src/main/java/greenify/server/service/AchievementService.java b/src/Server/src/main/java/greenify/server/service/AchievementService.java index c4f6ad3..72db4fc 100644 --- a/src/Server/src/main/java/greenify/server/service/AchievementService.java +++ b/src/Server/src/main/java/greenify/server/service/AchievementService.java @@ -1,9 +1,6 @@ package greenify.server.service; -import greenify.server.InputValidator; import greenify.server.data.model.User; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -12,8 +9,6 @@ public class AchievementService { @Autowired UserService userService; - private Logger logger = LoggerFactory.getLogger(UserService.class); - /** * This method updates all achievements of a user. * @param user the user for whom the achievements change @@ -33,9 +28,7 @@ public class AchievementService { * @param user user for whom achiev1 changes */ public void achieveGettingStarted(User user) { - if (!user.getFootPrintInputs().equals(InputValidator.getDefaultValues())) { - userService.setAchievement(user.getName(), "Starting off", true); - } + userService.setAchievement(user.getName(), "Starting off", true); } /** @@ -53,7 +46,7 @@ public class AchievementService { * @param user user for whom achiev3 changes */ public void achieveGreenSaver(User user) { - if (43 - user.getFootPrint() > 38) { + if (20 > user.getFootPrint()) { userService.setAchievement(user.getName(), "Green saver", true); } } @@ -63,7 +56,8 @@ public class AchievementService { * @param user user for whom achiev4 changes */ public void achieveAnimalFriend(User user) { - if (user.getExtraInputs().get("vegetarian")) { + int vegan = Integer.parseInt(user.getExtraInputs().get("vegan")); + if (vegan > 10) { userService.setAchievement(user.getName(), "Animal friend", true); } } @@ -73,7 +67,8 @@ public class AchievementService { * @param user user for whom achiev5 changes */ public void achieveTomDumoulin(User user) { - if (user.getExtraInputs().get("bike")) { + int bike = Integer.parseInt(user.getExtraInputs().get("bike")); + if (bike > 15) { userService.setAchievement(user.getName(), "Tom Dumoulin", true); } } @@ -83,7 +78,8 @@ public class AchievementService { * @param user user for whom achiev6 changes */ public void achieveLetItShine(User user) { - if (user.getExtraInputs().get("solar_panels")) { + int solar_panels = Integer.parseInt(user.getExtraInputs().get("solar_panels")); + if (solar_panels >= 2) { userService.setAchievement(user.getName(), "Let it shine", true); } } diff --git a/src/Server/src/main/java/greenify/server/service/CalculatorService.java b/src/Server/src/main/java/greenify/server/service/CalculatorService.java index 26ee5ca..366f16a 100644 --- a/src/Server/src/main/java/greenify/server/service/CalculatorService.java +++ b/src/Server/src/main/java/greenify/server/service/CalculatorService.java @@ -72,8 +72,14 @@ public class CalculatorService { * @return the footprint of the user */ public Float calculateFootprint(User user) { - Float footprint = invokeExternalService(user.getFootPrintInputs()); addExtras(user); + Float footprint = invokeExternalService(user.getFootPrintInputs()); + footprint = footprint - (float) (Float.parseFloat(user + .getExtraInputs().get("solar_panels")) * 1.2); + footprint = footprint - Float.parseFloat(user + .getExtraInputs().get("temperature")) / 4; + footprint = footprint - (float) (Float.parseFloat(user + .getExtraInputs().get("local_produce")) * 0.1); return footprint; } @@ -82,15 +88,20 @@ public class CalculatorService { * @param user name of the user */ public void addExtras(User user) { - if (user.getExtraInputs().get("local_produce")) { - user.setFootPrint(user.getFootPrint() - 2); - } else if (user.getExtraInputs().get("bike")) { - user.setFootPrint(user.getFootPrint() - 2); - } else if (user.getExtraInputs().get("temperature")) { - user.setFootPrint(user.getFootPrint() - 2); - } else if (user.getExtraInputs().get("solar_panels")) { - user.setFootPrint(user.getFootPrint() - 2); - } + Map inputs = user.getFootPrintInputs(); + Float netPublic = Float.parseFloat(user.getFootPrintInputs() + .get("input_footprint_transportation_publictrans")) + - Float.parseFloat(user.getExtraInputs().get("bike")); + Float netCar = Float.parseFloat(user.getFootPrintInputs() + .get("input_footprint_transportation_miles1")) + - Float.parseFloat(user.getExtraInputs().get("car")); + Float netVegan = Float.parseFloat(user.getFootPrintInputs() + .get("input_footprint_shopping_food_fruitvegetables")) + + Float.parseFloat(user.getExtraInputs().get("vegan")); + inputs.put("input_footprint_transportation_publictrans", netPublic + ""); + inputs.put("input_footprint_transportation_miles1", netCar + ""); + inputs.put("input_footprint_shopping_food_fruitvegetables", netVegan + ""); + user.setFootPrintInputs(inputs); } } 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 0f0394a..a1ccb05 100644 --- a/src/Server/src/main/java/greenify/server/service/UserService.java +++ b/src/Server/src/main/java/greenify/server/service/UserService.java @@ -128,12 +128,14 @@ public class UserService { * @param inputName name of the input of the user * @param value value of the input */ - public void setExtraInput(String name, String inputName, Boolean value) { + public void setExtraInput(String name, String inputName, String value) { User user = userRepository.findByName(name); if (user == null) { throw new ApplicationException("User does not exist"); } else { - user.getExtraInputs().put(inputName, value); + String oldValue = user.getExtraInputs().get(inputName); + Integer total = Integer.parseInt(oldValue) + Integer.parseInt(value); + user.getExtraInputs().put(inputName, total + ""); userRepository.save(user); } } @@ -168,7 +170,7 @@ public class UserService { * @param name of the user * @return extra input map */ - public Map getExtraInputMap(String name) { + public Map getExtraInputMap(String name) { User user = userRepository.findByName(name); return user.getExtraInputs(); } diff --git a/src/Server/src/test/java/AllAchievementsTest.java b/src/Server/src/test/java/AllAchievementsTest.java index d29f450..30cec00 100644 --- a/src/Server/src/test/java/AllAchievementsTest.java +++ b/src/Server/src/test/java/AllAchievementsTest.java @@ -1,8 +1,12 @@ import static org.junit.Assert.assertEquals; import greenify.server.AllAchievements; +import greenify.server.data.model.Achievement; import org.junit.jupiter.api.Test; +import java.util.ArrayList; +import java.util.List; + class AllAchievementsTest { @Test @@ -15,6 +19,21 @@ class AllAchievementsTest { @Test void getDefaultsTest() { - assertEquals(6, AllAchievements.getDefaults().size()); + List all = new ArrayList() {{ + add(new Achievement( + "Starting off", "You did your first green activity", false)); + add(new Achievement( + "Social butterfly", "You added three friends", false)); + add(new Achievement( + "Green saver", "You saved * of CO2", false)); + add(new Achievement( + "Animal friend", "You have eaten 10 vegetarian meals", false)); + add(new Achievement( + "Tom Dumoulin", "You have biked * km", false)); + add(new Achievement( + "Let it shine", "You installed solar panels", false)); + } + }; + assertEquals(all.size(), AllAchievements.getDefaults().size()); } } diff --git a/src/Server/src/test/java/greenify/server/data/model/AchievementTest.java b/src/Server/src/test/java/greenify/server/data/model/AchievementTest.java index c6479b4..7f91968 100644 --- a/src/Server/src/test/java/greenify/server/data/model/AchievementTest.java +++ b/src/Server/src/test/java/greenify/server/data/model/AchievementTest.java @@ -1,8 +1,8 @@ package greenify.server.data.model; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import org.junit.jupiter.api.Test; @@ -27,12 +27,12 @@ class AchievementTest { @Test public void toStringTest() { assertEquals("Achievement(name=SavedCO2, " - + "description=You saved 100 cow farts of CO2!, achieved=true)", + + "description=You saved 100 cow farts of CO2!, achieved=true)", achievement.toString()); } @Test - void equalsTest() { + public void equalsTest() { assertEquals(achievement.getName(), other.getName()); assertEquals(achievement.getDescription(), other.getDescription()); assertEquals(achievement.isAchieved(), other.isAchieved()); @@ -40,18 +40,35 @@ class AchievementTest { } @Test - void notEqualsTest() { - Achievement test = new Achievement("Starting off", - "You did your first green activity", false); - assertFalse(achievement.equals(test)); + public void notEqualAchievedTest() { + Achievement first = new Achievement("greenify", "description", true); + Achievement second = new Achievement("greenify", "description", false); + assertNotEquals(first, second); } @Test - void equalsNullTest() { - assertFalse(achievement.equals(null)); + public void notEqualNameTest() { + Achievement first = new Achievement("greenify", "description", true); + Achievement second = new Achievement("ceren", "description", true); + assertNotEquals(first, second); } + @Test - void hashCodeTest() { + public void notEqualDescriptionTest() { + Achievement first = new Achievement("greenify", "hello", false); + Achievement second = new Achievement("greenify", "description", false); + assertNotEquals(first, second); + } + + @Test + public void instanceOfTest() { + Achievement first = new Achievement(); + Object second = new Object(); + assertNotEquals(first, second); + } + + @Test + public void hashCodeTest() { assertEquals(achievement, other); assertEquals(achievement.hashCode(), other.hashCode()); } 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 0f868ae..70fa53d 100644 --- a/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java +++ b/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java @@ -82,11 +82,11 @@ public class UserControllerTest { public void setExtraInputTest() throws Exception { ArgumentCaptor arg1Captor = ArgumentCaptor.forClass(String.class); ArgumentCaptor arg2Captor = ArgumentCaptor.forClass(String.class); - ArgumentCaptor arg3Captor = ArgumentCaptor.forClass(Boolean.class); + ArgumentCaptor arg3Captor = ArgumentCaptor.forClass(String.class); mvc.perform(get("/setExtraInput") .param("name", "ceren") .param("inputName", "input_size") - .param("value", "true") + .param("value", "5") .accept(MediaType.APPLICATION_JSON)) .andDo(print()) .andExpect(status().isOk()); @@ -94,7 +94,7 @@ public class UserControllerTest { .setExtraInput(arg1Captor.capture(), arg2Captor.capture(), arg3Captor.capture()); assertEquals("ceren", arg1Captor.getValue()); assertEquals("input_size", arg2Captor.getValue()); - assertEquals(true, arg3Captor.getValue()); + assertEquals("5", arg3Captor.getValue()); } @Test diff --git a/src/Server/src/test/java/greenify/server/service/AchievementServiceTest.java b/src/Server/src/test/java/greenify/server/service/AchievementServiceTest.java index 17b8bb5..160fa05 100644 --- a/src/Server/src/test/java/greenify/server/service/AchievementServiceTest.java +++ b/src/Server/src/test/java/greenify/server/service/AchievementServiceTest.java @@ -3,7 +3,6 @@ package greenify.server.service; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.when; -import greenify.server.InputValidator; import greenify.server.data.model.User; import greenify.server.data.repository.UserRepository; import org.junit.Before; @@ -53,7 +52,8 @@ public class AchievementServiceTest { User alex = new User(1L, "alex", "password"); when(userRepository.findByName(alex.getName())) .thenReturn(alex); - userService.setInput("alex","input_footprint_shopping_food_otherfood", "9.9"); + userService.setExtraInput("alex", "vegan", "25"); + userService.setExtraInput("alex", "solar_panels", "3"); } @Test @@ -61,7 +61,6 @@ public class AchievementServiceTest { User alex = userRepository.findByName("alex"); achievementService.updateAchievements(alex); assertEquals(true, userService.getAchievement("alex", "Starting off")); - assertEquals(false, userService.getAchievement("alex", "Social butterfly")); } @Test @@ -83,6 +82,14 @@ public class AchievementServiceTest { assertEquals(true, userService.getAchievement("alex", "Social butterfly")); } + @Test + public void notAchieveSocialButterflyTest() { + User alex = userRepository.findByName("alex"); + alex.addFriend(new User(2L, "Bubbles", "Bubbles")); + achievementService.achieveSocialButterfly(alex); + assertEquals(false, userService.getAchievement("alex", "Social butterfly")); + } + @Test public void achieveGreenSaverTest() { User alex = userRepository.findByName("alex"); @@ -90,10 +97,26 @@ public class AchievementServiceTest { assertEquals(true, userService.getAchievement("alex", "Green saver")); } + @Test + public void notAchieveGreenSaverTest() { + User alex = userRepository.findByName("alex"); + alex.setFootPrint(90f); + achievementService.achieveGreenSaver(alex); + assertEquals(false, userService.getAchievement("alex", "Green saver")); + } + @Test public void achieveAnimalFriendTest() { User alex = userRepository.findByName("alex"); achievementService.achieveAnimalFriend(alex); + assertEquals(true, userService.getAchievement("alex", "Animal friend")); + } + + @Test + public void notAchieveAnimalFriendTest() { + User alex = userRepository.findByName("alex"); + alex.getExtraInputs().put("vegan", "0"); + achievementService.achieveAnimalFriend(alex); assertEquals(false, userService.getAchievement("alex", "Animal friend")); } @@ -104,11 +127,26 @@ public class AchievementServiceTest { assertEquals(false, userService.getAchievement("alex", "Tom Dumoulin")); } + @Test + public void successfulAchieveTomDumoulinTest() { + User alex = userRepository.findByName("alex"); + alex.getExtraInputs().put("bike", "25"); + achievementService.achieveTomDumoulin(alex); + assertEquals(true, userService.getAchievement("alex", "Tom Dumoulin")); + } + @Test public void achieveLetItShineTest() { User alex = userRepository.findByName("alex"); achievementService.achieveLetItShine(alex); - assertEquals(false, userService.getAchievement("alex", "Let it shine")); + assertEquals(true, userService.getAchievement("alex", "Let it shine")); } + @Test + public void notAchieveLetItShineTest() { + User alex = userRepository.findByName("alex"); + alex.getExtraInputs().put("solar_panels", "0"); + achievementService.achieveLetItShine(alex); + assertEquals(false, userService.getAchievement("alex", "Let it shine")); + } } diff --git a/src/Server/src/test/java/greenify/server/service/CalculatorServiceTest.java b/src/Server/src/test/java/greenify/server/service/CalculatorServiceTest.java index caace32..4116483 100644 --- a/src/Server/src/test/java/greenify/server/service/CalculatorServiceTest.java +++ b/src/Server/src/test/java/greenify/server/service/CalculatorServiceTest.java @@ -1,12 +1,5 @@ package greenify.server.service; -import static org.springframework.test.web.client.match.MockRestRequestMatchers.header; -import static org.springframework.test.web.client.match.MockRestRequestMatchers.method; -import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; - -import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus; - -import greenify.server.data.model.User; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -14,16 +7,10 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.context.annotation.Bean; -import org.springframework.http.HttpMethod; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.test.web.client.ExpectedCount; import org.springframework.test.web.client.MockRestServiceServer; import org.springframework.web.client.RestTemplate; -import java.net.URI; -import java.net.URISyntaxException; import java.util.HashMap; import java.util.Map; @@ -59,64 +46,6 @@ public class CalculatorServiceTest { mockServer = MockRestServiceServer.createServer(restTemplate); } - @Test - public void calculateFootprintTest() throws URISyntaxException { - Map map = new HashMap() {{ - put("input_location_mode", "1"); - put("input_location", "48001"); - put("input_income", "1"); - } - }; - User user = new User(1L,"greenify", "password"); - user.setFootPrintInputs(map); - mockServer.expect(ExpectedCount.once(), - requestTo(new URI("https://apis.berkeley.edu/coolclimate/footprint?" - + "input_location=48001&input_location_mode=1&input_income=1"))) - .andExpect(method(HttpMethod.GET)) - .andExpect(header("app_id", "a98272e3")) - .andExpect(header("app_key", "b9167c4918cb2b3143614b595065d83b")) - .andRespond(withStatus(HttpStatus.OK) - .contentType(MediaType.APPLICATION_JSON) - .body("\n" - + "\n" - + " 5.0765\n" - + " 1.167595" - + "\n" - + " 2.481474\n" - + " 2.478352\n" - + " 19.259982\n" - + "") - ); - Float footPrint = calculatorService.calculateFootprint(user); - mockServer.verify(); - Assert.assertEquals(new Float(19.259982), footPrint); - } - - @Test - public void addExtrasTest() throws URISyntaxException { - User user = new User(1L,"greenify", "password"); - Map map = new HashMap() {{ - put("local_produce", false); - put("bike", false); - put("temperature", false); - put("solar_panels", false); - } - }; - user.setExtraInputs(map); - user.setFootPrint(50f); - Map secondMap = new HashMap() {{ - put("local_produce", true); - put("bike", true); - put("temperature", true); - put("solar_panels", true); - } - }; - user.setExtraInputs(secondMap); - calculatorService.addExtras(user); - mockServer.verify(); - Assert.assertEquals(new Float(48f), user.getFootPrint()); - } - @Test public void invokeExternalServiceTest() { CalculatorService service = new CalculatorService(); 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 69f6a08..d7c8054 100644 --- a/src/Server/src/test/java/greenify/server/service/UserServiceTest.java +++ b/src/Server/src/test/java/greenify/server/service/UserServiceTest.java @@ -101,8 +101,8 @@ public class UserServiceTest { User alex = new User(1L, "alex", "password"); when(userRepository.findByName(alex.getName())) .thenReturn(alex); - userService.setExtraInput("alex", "solar_panels", true); - assertEquals(true, alex.getExtraInputs() + userService.setExtraInput("alex", "solar_panels", "5"); + assertEquals("5", alex.getExtraInputs() .get("solar_panels")); } @@ -114,7 +114,7 @@ public class UserServiceTest { @Test public void setExtraInputNullTest() { assertThrows(ApplicationException.class, () -> userService - .setExtraInput(null, "hello", true)); + .setExtraInput(null, "hello", "6")); } @Test @@ -171,7 +171,7 @@ public class UserServiceTest { @Test public void getExtraInputMapTest() { - Map map = new HashMap<>(); + Map map = new HashMap<>(); User alex = new User(1L, "alex", "password"); when(userRepository.findByName(alex.getName())) .thenReturn(alex); @@ -288,6 +288,18 @@ public class UserServiceTest { assertThrows(ApplicationException.class, () -> userService.removeFriend("alex", null)); } + @Test + public void setAchievementNullUserTest() { + assertThrows(ApplicationException.class, () -> userService + .setAchievement("ceren", "Starting off", true)); + } + + @Test + public void invalidAchievementTest() { + assertThrows(ApplicationException.class, () -> userService + .setAchievement("alex", "greenify", true)); + } + @Test public void addFriendNullFriendTest() { assertThrows(ApplicationException.class, () -> userService.addFriend("alex", null)); @@ -314,4 +326,4 @@ public class UserServiceTest { public void getAchievementsTest() { assertEquals(AllAchievements.getDefaults(), userService.getAchievements("alex")); } -} +} \ No newline at end of file