UPDATE:: achievements and tests

This commit is contained in:
mlwauben
2019-03-30 16:43:24 +01:00
parent ca11cd3c9a
commit 7656b60fcf
6 changed files with 193 additions and 0 deletions

View File

@@ -13,6 +13,15 @@ public class AllAchievements {
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

View File

@@ -0,0 +1,39 @@
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;
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);
}
}
}

View File

@@ -2,6 +2,7 @@ package greenify.server.service;
import greenify.common.ApplicationException;
import greenify.common.UserDto;
import greenify.server.AllAchievements;
import greenify.server.InputValidator;
import greenify.server.data.model.User;
import greenify.server.data.repository.UserRepository;
@@ -17,6 +18,9 @@ public class UserService {
@Autowired
CalculatorService calculatorService;
@Autowired
AchievementService achievementService;
@Autowired
UserRepository userRepository;
@@ -35,6 +39,7 @@ public class UserService {
user.setFootPrintInputs(InputValidator.getDefaultValues());
Float footprint = calculatorService.calculateFootprint(user);
user.setFootPrint(footprint);
user.setAchievements(AllAchievements.getDefaults());
userRepository.save(user);
} else {
throw new ApplicationException("User already exists");
@@ -102,6 +107,7 @@ public class UserService {
user.getFootPrintInputs().put(inputName, value);
userRepository.save(user);
user.setFootPrint(calculatorService.calculateFootprint(user));
achievementService.updateAchievements(user);
userRepository.save(user);
} else {
throw new ApplicationException("Invalid input");
@@ -136,6 +142,41 @@ public class UserService {
return user.getFootPrint();
}
/**
* 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 a JSON of XML with all users.
* @return JSON/XML of all users

View File

@@ -10,6 +10,14 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
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>() {{

View File

@@ -0,0 +1,77 @@
package greenify.server.service;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;
import greenify.common.ApplicationException;
import greenify.common.UserDto;
import greenify.server.InputValidator;
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;
import java.util.ArrayList;
@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"));
}
}

View File

@@ -38,6 +38,9 @@ public class UserServiceTest {
@MockBean
private CalculatorService calculatorService;
@MockBean
private AchievementService achievementService;
/**
* setUp method for test.
*/
@@ -180,6 +183,22 @@ public class UserServiceTest {
User lola = userRepository.findByName("lola");
userService.addFriend("alex", "lola");
assertEquals(userService.getLeaderboard("alex"), "friends=[{name=lola, footprint=0.0}]");
}
@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"));
}
}