Merge branch 'Test/achievement' into 'master'

Test/achievement

See merge request cse1105/2018-2019/oopp-group-43/template!66
This commit is contained in:
Ceren Ugurlu
2019-04-06 10:40:14 +00:00
5 changed files with 50 additions and 24 deletions

View File

@@ -86,6 +86,9 @@ 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

View File

@@ -13,9 +13,6 @@ public class AchievementService {
@Autowired
UserService userService;
@Autowired
UserRepository userRepository;
private Logger logger = LoggerFactory.getLogger(UserService.class);
/**
@@ -24,17 +21,28 @@ public class AchievementService {
*/
public void updateAchievements(User user) {
achieveGettingStarted(user);
userRepository.save(user);
achieveSocialButterfly(user);
}
/**
* This method changes achiev1 when this is the case.
* This method makes sure the user gets an achievement
* upon calculating their footprint for the first time.
* @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);
}
}
/**
* This method makes sure the user gets an achievement
* when they have added three friends.
* @param user user for whom achiev2 changes
*/
public void achieveSocialButterfly(User user) {
if (user.getFriends().size() == 3) {
userService.setAchievement(user.getName(), "Social butterfly", true);
}
}

View File

@@ -114,8 +114,8 @@ public class UserService {
if (InputValidator.isValidItem(inputName)
&& InputValidator.isValidItemValue(inputName, value)) {
user.getFootPrintInputs().put(inputName, value);
userRepository.save(user);
achievementService.updateAchievements(user);
userRepository.save(user);
} else {
throw new ApplicationException("Invalid input");
}
@@ -242,14 +242,14 @@ public class UserService {
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");
}
}
if (!AllAchievements.isValidAchievement(achievement)) {
throw new ApplicationException("Invalid achievement");
}
Map<String, Boolean> temp = user.getAchievements();
temp.put(achievement, achieved);
user.setAchievements(temp);
userRepository.save(user);
}
/**

View File

@@ -1,6 +1,7 @@
package greenify.server.data.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.jupiter.api.Test;
@@ -38,6 +39,17 @@ class AchievementTest {
assertEquals(achievement, other);
}
@Test
void notEqualsTest() {
Achievement test = new Achievement("Starting off",
"You did your first green activity", false);
assertFalse(achievement.equals(test));
}
@Test
void equalsNullTest() {
assertFalse(achievement.equals(null));
}
@Test
void hashCodeTest() {
assertEquals(achievement, other);

View File

@@ -3,6 +3,7 @@ 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;
@@ -24,6 +25,14 @@ public class AchievementServiceTest {
}
}
@TestConfiguration
static class AchievementServiceConfiguration {
@Bean
public AchievementService achievementService() {
return new AchievementService();
}
}
@Autowired
private UserService userService;
@@ -33,7 +42,7 @@ public class AchievementServiceTest {
@MockBean
private CalculatorService calculatorService;
@MockBean
@Autowired
private AchievementService achievementService;
/**
@@ -44,28 +53,22 @@ public class AchievementServiceTest {
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);
userService.setInput("alex","input_footprint_shopping_food_otherfood", "9.9");
}
@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"));
assertEquals(false, userService.getAchievement("alex", "Social butterfly"));
}
}