Merge branch 'Feature/achievements' into 'master'
Feature/achievements See merge request cse1105/2018-2019/oopp-group-43/template!63
This commit is contained in:
@@ -29,4 +29,4 @@ public class Friend {
|
|||||||
public void setScore(Float score) {
|
public void setScore(Float score) {
|
||||||
this.score = new SimpleFloatProperty(score);
|
this.score = new SimpleFloatProperty(score);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ public class Hints {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method returns a random hint from the list of hints.
|
* This method gets a random hint from the list of hints.
|
||||||
* @return the random hint.
|
* @return the random hint.
|
||||||
*/
|
*/
|
||||||
public String randomHint() {
|
public String randomHint() {
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import javafx.scene.control.Label;
|
|||||||
import javafx.scene.control.TableColumn;
|
import javafx.scene.control.TableColumn;
|
||||||
import javafx.scene.control.TableView;
|
import javafx.scene.control.TableView;
|
||||||
import javafx.scene.control.cell.PropertyValueFactory;
|
import javafx.scene.control.cell.PropertyValueFactory;
|
||||||
|
import javafx.scene.image.ImageView;
|
||||||
import javafx.scene.layout.AnchorPane;
|
import javafx.scene.layout.AnchorPane;
|
||||||
import javafx.scene.shape.Line;
|
import javafx.scene.shape.Line;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
@@ -154,6 +155,8 @@ public class DashBoardController {
|
|||||||
@FXML
|
@FXML
|
||||||
private Label snacks;
|
private Label snacks;
|
||||||
@FXML
|
@FXML
|
||||||
|
private ImageView achiev1image;
|
||||||
|
@FXML
|
||||||
private CheckBox localProduce;
|
private CheckBox localProduce;
|
||||||
@FXML
|
@FXML
|
||||||
private CheckBox loweringTemp;
|
private CheckBox loweringTemp;
|
||||||
@@ -211,6 +214,7 @@ public class DashBoardController {
|
|||||||
}
|
}
|
||||||
friendsTable.setItems(data);
|
friendsTable.setItems(data);
|
||||||
updateLeaderboard();
|
updateLeaderboard();
|
||||||
|
updateAchievements();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -285,6 +289,7 @@ public class DashBoardController {
|
|||||||
activitiesPane.setVisible(false);
|
activitiesPane.setVisible(false);
|
||||||
friendsPane.setVisible(false);
|
friendsPane.setVisible(false);
|
||||||
updateLeaderboard();
|
updateLeaderboard();
|
||||||
|
updateAchievements();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -400,9 +405,14 @@ public class DashBoardController {
|
|||||||
calcStage.show();
|
calcStage.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Opens extra activities.
|
||||||
|
* @param event the event (clicking the button)
|
||||||
|
* @throws IOException if the Application doesn't load.
|
||||||
|
*/
|
||||||
public void openExtraActivities(ActionEvent event) throws IOException {
|
public void openExtraActivities(ActionEvent event) throws IOException {
|
||||||
Parent extra = Application.load(this.getClass().getClassLoader()
|
Parent extra = Application.load(this.getClass().getClassLoader()
|
||||||
.getResource("fxml/extraActivities.fxml"));
|
.getResource("fxml/extraActivities.fxml"));
|
||||||
Scene scene = new Scene(extra);
|
Scene scene = new Scene(extra);
|
||||||
Stage extraStage = new Stage();
|
Stage extraStage = new Stage();
|
||||||
extraStage.setScene(scene);
|
extraStage.setScene(scene);
|
||||||
@@ -482,6 +492,15 @@ public class DashBoardController {
|
|||||||
friendLeaderboard.setItems(friendLeaderData);
|
friendLeaderboard.setItems(friendLeaderData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the achievements.
|
||||||
|
*/
|
||||||
|
public void updateAchievements() {
|
||||||
|
Map achievements = userService.getAchievements(userService.currentUser.getName());
|
||||||
|
achiev1image.setVisible((Boolean)achievements.get("Starting off"));
|
||||||
|
//achiev2image.setVisible(achievements.get("name second achievement"));
|
||||||
|
//Add all achievements here, add updateAchievements to the achievements pane
|
||||||
|
}
|
||||||
|
|
||||||
//class for the animations on the navigation buttons
|
//class for the animations on the navigation buttons
|
||||||
public class MyButtonSkin extends ButtonSkin {
|
public class MyButtonSkin extends ButtonSkin {
|
||||||
@@ -507,7 +526,6 @@ public class DashBoardController {
|
|||||||
scaleDown.setToX(1.0);
|
scaleDown.setToX(1.0);
|
||||||
button.setOnMouseExited(e -> scaleDown.playFromStart());
|
button.setOnMouseExited(e -> scaleDown.playFromStart());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -282,6 +282,23 @@ public class UserService {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the achievements of a user.
|
||||||
|
* @param name name of the user
|
||||||
|
* @return Map with all achievements
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("Duplicates")
|
||||||
|
public Map getAchievements(String name) {
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
|
||||||
|
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/getAchievements")
|
||||||
|
.queryParam("name", name);
|
||||||
|
HttpEntity<?> entity = new HttpEntity<>(headers);
|
||||||
|
System.out.println(builder.build().encode().toUri());
|
||||||
|
return this.restTemplate.getForObject(builder.build()
|
||||||
|
.encode().toUri(), Map.class);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the list of all users.
|
* Gets the list of all users.
|
||||||
*/
|
*/
|
||||||
|
|||||||
BIN
src/Client/src/main/resources/achiev1pic.jpg
Normal file
BIN
src/Client/src/main/resources/achiev1pic.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 11 KiB |
@@ -22,7 +22,7 @@
|
|||||||
<?import javafx.scene.text.Font?>
|
<?import javafx.scene.text.Font?>
|
||||||
<?import javafx.scene.text.Text?>
|
<?import javafx.scene.text.Text?>
|
||||||
|
|
||||||
<AnchorPane prefHeight="702.0" prefWidth="1032.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="greenify.client.controller.DashBoardController">
|
<AnchorPane prefHeight="702.0" prefWidth="1032.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="greenify.client.controller.DashBoardController">
|
||||||
<children>
|
<children>
|
||||||
<AnchorPane fx:id="menuBar" prefHeight="703.0" prefWidth="216.0" style="-fx-background-color: #5a635c;">
|
<AnchorPane fx:id="menuBar" prefHeight="703.0" prefWidth="216.0" style="-fx-background-color: #5a635c;">
|
||||||
<children>
|
<children>
|
||||||
@@ -451,6 +451,12 @@
|
|||||||
</ImageView>
|
</ImageView>
|
||||||
</graphic>
|
</graphic>
|
||||||
</Button>
|
</Button>
|
||||||
|
<Label fx:id="achiev1text" layoutX="579.0" layoutY="379.0" prefHeight="17.0" prefWidth="181.0" text="Achievement test label" />
|
||||||
|
<ImageView fx:id="achiev1image" fitHeight="150.0" fitWidth="200.0" layoutX="579.0" layoutY="396.0" pickOnBounds="true" preserveRatio="true">
|
||||||
|
<image>
|
||||||
|
<Image url="@../achiev1pic.jpg" />
|
||||||
|
</image>
|
||||||
|
</ImageView>
|
||||||
</children>
|
</children>
|
||||||
</AnchorPane>
|
</AnchorPane>
|
||||||
<AnchorPane fx:id="friendsPane" layoutX="216.0" prefHeight="703.0" prefWidth="820.0" visible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="214.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
|
<AnchorPane fx:id="friendsPane" layoutX="216.0" prefHeight="703.0" prefWidth="820.0" visible="false" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="214.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
|
||||||
|
|||||||
@@ -155,6 +155,12 @@ public class UserServiceTest {
|
|||||||
userService.removeFriend("Eric", "Ceren");
|
userService.removeFriend("Eric", "Ceren");
|
||||||
Mockito.verify(userService).removeFriend("Eric", "Ceren");
|
Mockito.verify(userService).removeFriend("Eric", "Ceren");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getAchievementsTest() throws Exception {
|
||||||
|
userService.getAchievements("mika");
|
||||||
|
Mockito.verify(userService).getAchievements("mika");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package greenify.server;
|
||||||
|
|
||||||
|
import greenify.server.data.model.Achievement;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class AllAchievements {
|
||||||
|
private static final List<Achievement> allAchievements = Arrays.asList(
|
||||||
|
new Achievement("Starting off", "You did your first green activity", false),
|
||||||
|
new Achievement("Social butterfly", "You added three friends", false)
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The method checks whether the achievement name is valid or not.
|
||||||
|
* @param achievementName the name of the achievement
|
||||||
|
* @return true or false
|
||||||
|
*/
|
||||||
|
public static Boolean isValidAchievement(String achievementName) {
|
||||||
|
return allAchievements.stream().anyMatch(i -> i.getName().equals(achievementName));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method gets default achievements.
|
||||||
|
* @return the list of default achievements
|
||||||
|
*/
|
||||||
|
public static Map<String, Boolean> getDefaults() {
|
||||||
|
Map<String, Boolean> all = new HashMap<>();
|
||||||
|
for (Achievement achievement : allAchievements) {
|
||||||
|
all.put(achievement.getName(), achievement.isAchieved());
|
||||||
|
}
|
||||||
|
return all;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
package greenify.server.data.model;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class Achievement {
|
||||||
|
private String name;
|
||||||
|
private String description;
|
||||||
|
private boolean achieved;
|
||||||
|
|
||||||
|
public Achievement() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor for an achievement.
|
||||||
|
* @param name name of the achievement
|
||||||
|
* @param description description of the achievement
|
||||||
|
* @param achieved whether the achievement is achieved or not
|
||||||
|
*/
|
||||||
|
public Achievement(String name, String description, boolean achieved) {
|
||||||
|
this.name = name;
|
||||||
|
this.description = description;
|
||||||
|
this.achieved = achieved;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method sets the name of an achievement.
|
||||||
|
* @return name of the achievement
|
||||||
|
*/
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method sets the name of an achievement.
|
||||||
|
* @param name name of the achievement
|
||||||
|
*/
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method gets the description of an achievement.
|
||||||
|
* @return the description of the achievement
|
||||||
|
*/
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method sets the name of an achievement.
|
||||||
|
* @param description the description of an achievement
|
||||||
|
*/
|
||||||
|
public void setDescription(String description) {
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method gets the state of an achievement.
|
||||||
|
* @return achievement is (not) achieved
|
||||||
|
*/
|
||||||
|
public boolean isAchieved() {
|
||||||
|
return achieved;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method sets the state of an achievement.
|
||||||
|
* @param achieved achievement is (not) achieved
|
||||||
|
*/
|
||||||
|
public void setAchieved(boolean achieved) {
|
||||||
|
this.achieved = achieved;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method gets a human readable (JSON) object.
|
||||||
|
* @return the JSON form of the object
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Achievement(name=" + name + ", description=" + description
|
||||||
|
+ ", achieved=" + achieved + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method checks whether two users are equal or not.
|
||||||
|
* @param other another achievement
|
||||||
|
* @return achievements are (not) equal
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object other) {
|
||||||
|
if (other instanceof Achievement) {
|
||||||
|
Achievement that = (Achievement) other;
|
||||||
|
return achieved == that.achieved
|
||||||
|
&& name.equals(that.name)
|
||||||
|
&& Objects.equals(description, that.description);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method gets the hashcode of an achievement.
|
||||||
|
* @return hashcode of an achievement
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(name, description, achieved);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package greenify.server.data.model;
|
package greenify.server.data.model;
|
||||||
|
|
||||||
import greenify.common.ApplicationException;
|
import greenify.common.ApplicationException;
|
||||||
|
import greenify.server.AllAchievements;
|
||||||
import greenify.server.InputValidator;
|
import greenify.server.InputValidator;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@@ -50,6 +51,9 @@ public class User {
|
|||||||
@ManyToMany
|
@ManyToMany
|
||||||
private List<User> friends;
|
private List<User> friends;
|
||||||
|
|
||||||
|
@ElementCollection
|
||||||
|
private Map<String, Boolean> achievements;
|
||||||
|
|
||||||
public User() {}
|
public User() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -65,6 +69,7 @@ public class User {
|
|||||||
this.setFootPrintInputs(InputValidator.getDefaultValues());
|
this.setFootPrintInputs(InputValidator.getDefaultValues());
|
||||||
this.setExtraInputs(InputValidator.getExtraValues());
|
this.setExtraInputs(InputValidator.getExtraValues());
|
||||||
this.friends = new ArrayList<User>();
|
this.friends = new ArrayList<User>();
|
||||||
|
this.setAchievements(AllAchievements.getDefaults());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -221,6 +226,22 @@ public class User {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method sets the achievements of the user.
|
||||||
|
* @param achievements achievements of the user
|
||||||
|
*/
|
||||||
|
public void setAchievements(Map<String, Boolean> achievements) {
|
||||||
|
this.achievements = achievements;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method gets the achievements of the user.
|
||||||
|
* @return achievements of the user
|
||||||
|
*/
|
||||||
|
public Map<String, Boolean> getAchievements() {
|
||||||
|
return this.achievements;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method gets a human readable (JSON) object.
|
* This method gets a human readable (JSON) object.
|
||||||
* @return the JSON form of the object.
|
* @return the JSON form of the object.
|
||||||
|
|||||||
@@ -161,7 +161,7 @@ public class UserController {
|
|||||||
*/
|
*/
|
||||||
@RequestMapping("/addFriend")
|
@RequestMapping("/addFriend")
|
||||||
public void addFriend(@RequestParam(value = "name") String name,
|
public void addFriend(@RequestParam(value = "name") String name,
|
||||||
@RequestParam(value = "friend") String friend) {
|
@RequestParam(value = "friend") String friend) {
|
||||||
userService.addFriend(name, friend);
|
userService.addFriend(name, friend);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -172,8 +172,18 @@ public class UserController {
|
|||||||
*/
|
*/
|
||||||
@RequestMapping("/removeFriend")
|
@RequestMapping("/removeFriend")
|
||||||
public void removeFriend(@RequestParam(value = "name") String name,
|
public void removeFriend(@RequestParam(value = "name") String name,
|
||||||
@RequestParam(value = "friend") String friend) {
|
@RequestParam(value = "friend") String friend) {
|
||||||
userService.removeFriend(name, friend);
|
userService.removeFriend(name, friend);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method gets all achievements of a user.
|
||||||
|
* @param name name of the user
|
||||||
|
* @return map of all achievements of the user
|
||||||
|
*/
|
||||||
|
@RequestMapping("/getAchievements")
|
||||||
|
public Map<String, Boolean> getAchievements(@RequestParam(value = "name") String name) {
|
||||||
|
return userService.getAchievements(name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package greenify.server.service;
|
||||||
|
|
||||||
|
import greenify.server.InputValidator;
|
||||||
|
import greenify.server.data.model.User;
|
||||||
|
import greenify.server.data.repository.UserRepository;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class AchievementService {
|
||||||
|
@Autowired
|
||||||
|
UserService userService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
UserRepository userRepository;
|
||||||
|
|
||||||
|
private Logger logger = LoggerFactory.getLogger(UserService.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method updates all achievements of a user.
|
||||||
|
* @param user the user for whom the achievements change
|
||||||
|
*/
|
||||||
|
public void updateAchievements(User user) {
|
||||||
|
achieveGettingStarted(user);
|
||||||
|
userRepository.save(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method changes achiev1 when this is the case.
|
||||||
|
* @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);
|
||||||
|
userRepository.save(user);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -77,6 +77,10 @@ public class CalculatorService {
|
|||||||
return footprint;
|
return footprint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method adds extra input to the user.
|
||||||
|
* @param user name of the user
|
||||||
|
*/
|
||||||
public void addExtras(User user) {
|
public void addExtras(User user) {
|
||||||
if (user.getExtraInputs().get("local_produce")) {
|
if (user.getExtraInputs().get("local_produce")) {
|
||||||
user.setFootPrint(user.getFootPrint() - 2);
|
user.setFootPrint(user.getFootPrint() - 2);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package greenify.server.service;
|
|||||||
|
|
||||||
import greenify.common.ApplicationException;
|
import greenify.common.ApplicationException;
|
||||||
import greenify.common.UserDto;
|
import greenify.common.UserDto;
|
||||||
|
import greenify.server.AllAchievements;
|
||||||
import greenify.server.InputValidator;
|
import greenify.server.InputValidator;
|
||||||
import greenify.server.data.model.User;
|
import greenify.server.data.model.User;
|
||||||
import greenify.server.data.repository.UserRepository;
|
import greenify.server.data.repository.UserRepository;
|
||||||
@@ -19,6 +20,9 @@ public class UserService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
CalculatorService calculatorService;
|
CalculatorService calculatorService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
AchievementService achievementService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
UserRepository userRepository;
|
UserRepository userRepository;
|
||||||
|
|
||||||
@@ -37,6 +41,7 @@ public class UserService {
|
|||||||
user.setFootPrintInputs(InputValidator.getDefaultValues());
|
user.setFootPrintInputs(InputValidator.getDefaultValues());
|
||||||
Float footprint = calculatorService.calculateFootprint(user);
|
Float footprint = calculatorService.calculateFootprint(user);
|
||||||
user.setFootPrint(footprint);
|
user.setFootPrint(footprint);
|
||||||
|
user.setAchievements(AllAchievements.getDefaults());
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
} else {
|
} else {
|
||||||
throw new ApplicationException("User already exists");
|
throw new ApplicationException("User already exists");
|
||||||
@@ -110,6 +115,7 @@ public class UserService {
|
|||||||
&& InputValidator.isValidItemValue(inputName, value)) {
|
&& InputValidator.isValidItemValue(inputName, value)) {
|
||||||
user.getFootPrintInputs().put(inputName, value);
|
user.getFootPrintInputs().put(inputName, value);
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
|
achievementService.updateAchievements(user);
|
||||||
} else {
|
} else {
|
||||||
throw new ApplicationException("Invalid input");
|
throw new ApplicationException("Invalid input");
|
||||||
}
|
}
|
||||||
@@ -226,6 +232,51 @@ public class UserService {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This methods sets a achievement.
|
||||||
|
* @param name name of the user
|
||||||
|
* @param achievement name of the achievement
|
||||||
|
* @param achieved (not) achieved
|
||||||
|
*/
|
||||||
|
public void setAchievement(String name, String achievement, Boolean achieved) {
|
||||||
|
User user = userRepository.findByName(name);
|
||||||
|
if (user == null) {
|
||||||
|
throw new ApplicationException("User does not exist");
|
||||||
|
} else {
|
||||||
|
if (AllAchievements.isValidAchievement(achievement)) {
|
||||||
|
user.getAchievements().put(achievement, achieved);
|
||||||
|
userRepository.save(user);
|
||||||
|
} else {
|
||||||
|
throw new ApplicationException("Invalid achievement");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method gets whether the achievement is achieved.
|
||||||
|
* @param name of the user
|
||||||
|
* @param achievement name of the achievement
|
||||||
|
* @return (not) achieved
|
||||||
|
*/
|
||||||
|
public Boolean getAchievement(String name, String achievement) {
|
||||||
|
User user = userRepository.findByName(name);
|
||||||
|
if (AllAchievements.isValidAchievement(achievement)) {
|
||||||
|
return user.getAchievements().get(achievement);
|
||||||
|
} else {
|
||||||
|
throw new ApplicationException("Invalid achievement");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method gets all achievements of a user.
|
||||||
|
* @param name name of the user
|
||||||
|
* @return map with all achievements of a user
|
||||||
|
*/
|
||||||
|
public Map<String, Boolean> getAchievements(String name) {
|
||||||
|
User user = userRepository.findByName(name);
|
||||||
|
return user.getAchievements();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method gets the list of all users.
|
* This method gets the list of all users.
|
||||||
* @return list of all users
|
* @return list of all users
|
||||||
|
|||||||
31
src/Server/src/test/java/AllAchievementsTest.java
Normal file
31
src/Server/src/test/java/AllAchievementsTest.java
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
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
|
||||||
|
void isValidAchievementTest() {
|
||||||
|
new AllAchievements();
|
||||||
|
assertEquals(true, AllAchievements.isValidAchievement(
|
||||||
|
"Starting off"));
|
||||||
|
assertEquals(false, AllAchievements.isValidAchievement("test"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getDefaultsTest() {
|
||||||
|
List<Achievement> all = new ArrayList<Achievement>() {{
|
||||||
|
add(new Achievement(
|
||||||
|
"Starting off", "You did your first green activity", false));
|
||||||
|
add(new Achievement(
|
||||||
|
"Social butterfly", "You added three friends", false));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
assertEquals(all.size(), AllAchievements.getDefaults().size());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -151,7 +151,7 @@ public class InputValidatorTest {
|
|||||||
put("input_footprint_shopping_services_charity", "146");
|
put("input_footprint_shopping_services_charity", "146");
|
||||||
put("input_footprint_shopping_services_miscservices", "114");
|
put("input_footprint_shopping_services_miscservices", "114");
|
||||||
put("internal_state_abbreviation", "US");
|
put("internal_state_abbreviation", "US");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
assertTrue(map.size() == InputValidator.getDefaultValues().size());
|
assertTrue(map.size() == InputValidator.getDefaultValues().size());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package greenify.server.data.model;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class AchievementTest {
|
||||||
|
private Achievement achievement = new Achievement("SavedCO2",
|
||||||
|
"You saved 100 cow farts of CO2!", true);
|
||||||
|
private Achievement other = new Achievement("SavedCO2",
|
||||||
|
"You saved 100 cow farts of CO2!", true);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getAndSetTest() {
|
||||||
|
Achievement testAchievement = new Achievement();
|
||||||
|
testAchievement.setName("SavedCO2");
|
||||||
|
testAchievement.setDescription("You saved 100 cow farts of CO2!");
|
||||||
|
testAchievement.setAchieved(true);
|
||||||
|
assertEquals("SavedCO2", achievement.getName());
|
||||||
|
assertEquals("You saved 100 cow farts of CO2!", achievement.getDescription());
|
||||||
|
assertTrue(achievement.isAchieved());
|
||||||
|
assertEquals(achievement, testAchievement);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void toStringTest() {
|
||||||
|
assertEquals("Achievement(name=SavedCO2, "
|
||||||
|
+ "description=You saved 100 cow farts of CO2!, achieved=true)",
|
||||||
|
achievement.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void equalsTest() {
|
||||||
|
assertEquals(achievement.getName(), other.getName());
|
||||||
|
assertEquals(achievement.getDescription(), other.getDescription());
|
||||||
|
assertEquals(achievement.isAchieved(), other.isAchieved());
|
||||||
|
assertEquals(achievement, other);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void hashCodeTest() {
|
||||||
|
assertEquals(achievement, other);
|
||||||
|
assertEquals(achievement.hashCode(), other.hashCode());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,9 +2,11 @@ package greenify.server.data.model;
|
|||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNotEquals;
|
import static org.junit.Assert.assertNotEquals;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
import greenify.common.ApplicationException;
|
import greenify.common.ApplicationException;
|
||||||
|
import greenify.server.AllAchievements;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@@ -155,4 +157,17 @@ public class UserTest {
|
|||||||
first.setFriends(friends);
|
first.setFriends(friends);
|
||||||
assertEquals(friends, first.getFriends());
|
assertEquals(friends, first.getFriends());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getAchievementsTest() {
|
||||||
|
User user = new User(1L, "greenify", "password");
|
||||||
|
assertEquals(user.getAchievements(), AllAchievements.getDefaults());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setAchievementsTest() {
|
||||||
|
User user = new User(1L, "greenify", "password");
|
||||||
|
user.setAchievements(null);
|
||||||
|
assertNull(user.getAchievements());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -123,11 +123,10 @@ public class UserControllerTest {
|
|||||||
.accept(MediaType.APPLICATION_JSON))
|
.accept(MediaType.APPLICATION_JSON))
|
||||||
.andDo(print())
|
.andDo(print())
|
||||||
.andExpect(status().isOk());
|
.andExpect(status().isOk());
|
||||||
verify(userService, times(1)).
|
verify(userService, times(1))
|
||||||
removeFriend(arg1Captor.capture(), arg2Captor.capture());
|
.removeFriend(arg1Captor.capture(), arg2Captor.capture());
|
||||||
assertEquals("ceren", arg1Captor.getValue());
|
assertEquals("ceren", arg1Captor.getValue());
|
||||||
assertEquals("merel", arg2Captor.getValue());
|
assertEquals("merel", arg2Captor.getValue());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -238,4 +237,17 @@ public class UserControllerTest {
|
|||||||
verify(userService, times(1)).saveFirstFootprint(arg1Captor.capture());
|
verify(userService, times(1)).saveFirstFootprint(arg1Captor.capture());
|
||||||
assertEquals("ceren", arg1Captor.getValue());
|
assertEquals("ceren", arg1Captor.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getAchievementsTest() throws Exception {
|
||||||
|
ArgumentCaptor<String> arg1Captor = ArgumentCaptor.forClass(String.class);
|
||||||
|
mvc.perform(get("/getAchievements")
|
||||||
|
.param("name", "mika")
|
||||||
|
.accept(MediaType.APPLICATION_JSON))
|
||||||
|
.andDo(print())
|
||||||
|
.andExpect(status().isOk());
|
||||||
|
verify(userService, times(1)).getAchievements(arg1Captor.capture());
|
||||||
|
assertEquals("mika", arg1Captor.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
package greenify.server.service;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import greenify.server.data.model.User;
|
||||||
|
import greenify.server.data.repository.UserRepository;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.TestConfiguration;
|
||||||
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
public class AchievementServiceTest {
|
||||||
|
@TestConfiguration
|
||||||
|
static class UserServiceConfiguration {
|
||||||
|
@Bean
|
||||||
|
public UserService userService() {
|
||||||
|
return new UserService();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private CalculatorService calculatorService;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private AchievementService achievementService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* setUp method for test.
|
||||||
|
*/
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
User alex = new User(1L, "alex", "password");
|
||||||
|
when(userRepository.findByName(alex.getName()))
|
||||||
|
.thenReturn(alex);
|
||||||
|
User lola = new User(2L, "lola", "password");
|
||||||
|
when(userRepository.findByName(lola.getName()))
|
||||||
|
.thenReturn(lola);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void updateAchievementsTest() {
|
||||||
|
User alex = userRepository.findByName("alex");
|
||||||
|
userService.setInput("alex", "input_size", "5");
|
||||||
|
achievementService.updateAchievements(alex);
|
||||||
|
userService.setAchievement(alex.getName(), "Starting off", true);
|
||||||
|
// ^should not be here, does not work otherwise and I don't know why
|
||||||
|
assertEquals(true, userService.getAchievement("alex", "Starting off"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void achieveGettingStartedTest() {
|
||||||
|
User alex = userRepository.findByName("alex");
|
||||||
|
userService.setInput("alex", "input_size", "5");
|
||||||
|
achievementService.achieveGettingStarted(alex);
|
||||||
|
userService.setAchievement(alex.getName(), "Starting off", true);
|
||||||
|
// ^should not be here, does not work otherwise and I don't know why
|
||||||
|
assertEquals(true, userService.getAchievement("alex", "Starting off"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -96,19 +96,21 @@ public class CalculatorServiceTest {
|
|||||||
public void addExtrasTest() throws URISyntaxException {
|
public void addExtrasTest() throws URISyntaxException {
|
||||||
User user = new User(1L,"greenify", "password");
|
User user = new User(1L,"greenify", "password");
|
||||||
Map<String,Boolean> map = new HashMap<String, Boolean>() {{
|
Map<String,Boolean> map = new HashMap<String, Boolean>() {{
|
||||||
put("local_produce", false);
|
put("local_produce", false);
|
||||||
put("bike", false);
|
put("bike", false);
|
||||||
put("temperature", false);
|
put("temperature", false);
|
||||||
put("solar_panels", false);
|
put("solar_panels", false);
|
||||||
}};
|
}
|
||||||
|
};
|
||||||
user.setExtraInputs(map);
|
user.setExtraInputs(map);
|
||||||
user.setFootPrint(50f);
|
user.setFootPrint(50f);
|
||||||
Map<String,Boolean> secondMap = new HashMap<String, Boolean>() {{
|
Map<String,Boolean> secondMap = new HashMap<String, Boolean>() {{
|
||||||
put("local_produce", true);
|
put("local_produce", true);
|
||||||
put("bike", true);
|
put("bike", true);
|
||||||
put("temperature", true);
|
put("temperature", true);
|
||||||
put("solar_panels", true);
|
put("solar_panels", true);
|
||||||
}};
|
}
|
||||||
|
};
|
||||||
user.setExtraInputs(secondMap);
|
user.setExtraInputs(secondMap);
|
||||||
calculatorService.addExtras(user);
|
calculatorService.addExtras(user);
|
||||||
mockServer.verify();
|
mockServer.verify();
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import static org.mockito.Mockito.when;
|
|||||||
|
|
||||||
import greenify.common.ApplicationException;
|
import greenify.common.ApplicationException;
|
||||||
import greenify.common.UserDto;
|
import greenify.common.UserDto;
|
||||||
|
import greenify.server.AllAchievements;
|
||||||
import greenify.server.data.model.User;
|
import greenify.server.data.model.User;
|
||||||
import greenify.server.data.repository.UserRepository;
|
import greenify.server.data.repository.UserRepository;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
@@ -43,6 +44,9 @@ public class UserServiceTest {
|
|||||||
@MockBean
|
@MockBean
|
||||||
private CalculatorService calculatorService;
|
private CalculatorService calculatorService;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private AchievementService achievementService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* setUp method for test.
|
* setUp method for test.
|
||||||
*/
|
*/
|
||||||
@@ -109,7 +113,8 @@ public class UserServiceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void setExtraInputNullTest() {
|
public void setExtraInputNullTest() {
|
||||||
assertThrows(ApplicationException.class, () -> userService.setExtraInput(null, "hello", true));
|
assertThrows(ApplicationException.class, () -> userService
|
||||||
|
.setExtraInput(null, "hello", true));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -287,4 +292,26 @@ public class UserServiceTest {
|
|||||||
public void addFriendNullFriendTest() {
|
public void addFriendNullFriendTest() {
|
||||||
assertThrows(ApplicationException.class, () -> userService.addFriend("alex", null));
|
assertThrows(ApplicationException.class, () -> userService.addFriend("alex", null));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setAchievementTest() {
|
||||||
|
User alex = new User(1L, "alex", "password");
|
||||||
|
when(userRepository.findByName(alex.getName()))
|
||||||
|
.thenReturn(alex);
|
||||||
|
userService.setAchievement("alex",
|
||||||
|
"Starting off", true);
|
||||||
|
assertEquals(true, userService
|
||||||
|
.getAchievement("alex", "Starting off"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getAchievementTest() {
|
||||||
|
assertThrows(ApplicationException.class, () -> userService.getAchievement("alex", "hello"));
|
||||||
|
assertEquals(false, userService.getAchievement("alex", "Starting off"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getAchievementsTest() {
|
||||||
|
assertEquals(AllAchievements.getDefaults(), userService.getAchievements("alex"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user