122 lines
5.1 KiB
Java
122 lines
5.1 KiB
Java
package SuMoC.backend.user.service;
|
|
|
|
import SuMoC.backend.gamification.Timeframe;
|
|
import SuMoC.backend.user.dtos.StatScope;
|
|
import SuMoC.backend.user.models.User;
|
|
import SuMoC.backend.user.repository.UserRepository;
|
|
import SuMoC.backend.user.stats.models.StatsList;
|
|
import SuMoC.backend.user.stats.StatsService;
|
|
import jakarta.transaction.Transactional;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.security.test.context.support.WithMockUser;
|
|
import org.springframework.test.context.ActiveProfiles;
|
|
import org.springframework.web.server.ResponseStatusException;
|
|
|
|
import static SuMoC.backend.Factories.UserTestFactory.uniqueUser;
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
@SpringBootTest
|
|
@ActiveProfiles("test")
|
|
@Transactional
|
|
@WithMockUser(username = "test1@test.de", roles = {"USER"})
|
|
public class StatsServiceTest {
|
|
|
|
@Autowired private StatsService statsService;
|
|
@Autowired private UserRepository userRepository;
|
|
|
|
@BeforeEach
|
|
void setup() {
|
|
userRepository.save(uniqueUser(1));
|
|
}
|
|
|
|
private StatsList currentUserStats() {
|
|
return userRepository.findByEmail("test1@test.de").orElseThrow().getStats();
|
|
}
|
|
|
|
|
|
@Test
|
|
void testAddStatsToCurrentUser() {
|
|
// exception
|
|
ResponseStatusException ex = assertThrows(ResponseStatusException.class,
|
|
() -> statsService.addStatsToCurrentUser("NON_EXISTING", 100, StatScope.TOTAL, false, false));
|
|
assertEquals(HttpStatus.BAD_REQUEST, ex.getStatusCode());
|
|
|
|
// scope = null
|
|
statsService.addStatsToCurrentUser("XP", 10, null, false, false);
|
|
assertEquals(10, currentUserStats().getStatValue(Timeframe.WEEKLY, "XP"));
|
|
assertEquals(10, currentUserStats().getStatValue(Timeframe.MONTHLY, "XP"));
|
|
assertEquals(10, currentUserStats().getStatValue(Timeframe.TOTAL, "XP"));
|
|
|
|
// scope = ALL
|
|
statsService.addStatsToCurrentUser("XP", 10, StatScope.ALL, false, false);
|
|
assertEquals(20, currentUserStats().getStatValue(Timeframe.WEEKLY, "XP"));
|
|
assertEquals(20, currentUserStats().getStatValue(Timeframe.MONTHLY, "XP"));
|
|
assertEquals(20, currentUserStats().getStatValue(Timeframe.TOTAL, "XP"));
|
|
|
|
// scope = TOTAL
|
|
statsService.addStatsToCurrentUser("KM_CYCLED", 30, StatScope.TOTAL, false, false);
|
|
assertEquals(30, currentUserStats().getStatValue(Timeframe.TOTAL, "KM_CYCLED"));
|
|
assertEquals(0, currentUserStats().getStatValue(Timeframe.WEEKLY, "KM_CYCLED"));
|
|
|
|
// scope = MONTHLY
|
|
statsService.addStatsToCurrentUser("CO2_SAVED", 20, StatScope.MONTHLY, false, false);
|
|
assertEquals(20, currentUserStats().getStatValue(Timeframe.MONTHLY, "CO2_SAVED"));
|
|
assertEquals(0, currentUserStats().getStatValue(Timeframe.WEEKLY, "CO2_SAVED"));
|
|
|
|
// scope = WEEKLY
|
|
statsService.addStatsToCurrentUser("KM_WALKED", 10, StatScope.WEEKLY, false, false);
|
|
assertEquals(10, currentUserStats().getStatValue(Timeframe.WEEKLY, "KM_WALKED"));
|
|
assertEquals(0, currentUserStats().getStatValue(Timeframe.MONTHLY, "KM_WALKED"));
|
|
|
|
// awardBadges / awardChallenges Flags
|
|
assertDoesNotThrow(() -> statsService.addStatsToCurrentUser("XP", 1, StatScope.ALL, true, false));
|
|
assertDoesNotThrow(() -> statsService.addStatsToCurrentUser("XP", 1, StatScope.ALL, false, true));
|
|
assertDoesNotThrow(() -> statsService.addStatsToCurrentUser("XP", 1, StatScope.ALL, true, true));
|
|
}
|
|
|
|
|
|
@Test
|
|
void testGetStats() {
|
|
statsService.addStatsToCurrentUser("XP", 77, StatScope.TOTAL, false, false);
|
|
StatsList stats = statsService.getStats();
|
|
assertNotNull(stats);
|
|
assertEquals(77, stats.getStatValue(Timeframe.TOTAL, "XP"));
|
|
}
|
|
|
|
|
|
@Test
|
|
void testResetWeeklyStats() {
|
|
User user2 = userRepository.save(uniqueUser(2));
|
|
statsService.addStatsToCurrentUser("XP", 100, StatScope.ALL, false, false);
|
|
user2.getStats().addToAllStats("XP", 100);
|
|
userRepository.save(user2);
|
|
|
|
statsService.resetWeeklyStats();
|
|
|
|
// WEEKLY = 0, keep MONTHLY & TOTAL
|
|
assertEquals(0, currentUserStats().getStatValue(Timeframe.WEEKLY, "XP"));
|
|
assertEquals(100, currentUserStats().getStatValue(Timeframe.MONTHLY, "XP"));
|
|
assertEquals(100, currentUserStats().getStatValue(Timeframe.TOTAL, "XP"));
|
|
|
|
userRepository.findAll().forEach(u ->
|
|
assertEquals(0, u.getStats().getStatValue(Timeframe.WEEKLY, "XP")));
|
|
}
|
|
|
|
|
|
@Test
|
|
void testResetMonthlyStats() {
|
|
statsService.addStatsToCurrentUser("CO2_SAVED", 200, StatScope.ALL, false, false);
|
|
|
|
statsService.resetMonthlyStats();
|
|
|
|
// MONTHLY = 0, keep WEEKLY & TOTAL
|
|
assertEquals(0, currentUserStats().getStatValue(Timeframe.MONTHLY, "CO2_SAVED"));
|
|
assertEquals(200, currentUserStats().getStatValue(Timeframe.WEEKLY, "CO2_SAVED"));
|
|
assertEquals(200, currentUserStats().getStatValue(Timeframe.TOTAL, "CO2_SAVED"));
|
|
}
|
|
}
|