badge and stat tests + jacoco

This commit is contained in:
2026-02-27 19:23:19 +01:00
parent 2b267400c4
commit 2db8c13f8f
10 changed files with 465 additions and 111 deletions
@@ -0,0 +1,121 @@
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.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"));
}
}