diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/classroom/application/ClassroomService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/classroom/application/ClassroomService.kt index b86e1f45..15f0bbc0 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/classroom/application/ClassroomService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/classroom/application/ClassroomService.kt @@ -4,9 +4,7 @@ import edu.ntnu.idi.idatt.backend.classroom.domain.Classroom import edu.ntnu.idi.idatt.backend.classroom.domain.ClassroomPupilStatus import edu.ntnu.idi.idatt.backend.classroom.domain.ClassroomStatus import edu.ntnu.idi.idatt.backend.classroom.infrastructure.ClassroomRepository -import edu.ntnu.idi.idatt.backend.game.gameProgress.domain.PupilGameProgress -import edu.ntnu.idi.idatt.backend.game.gameProgress.domain.PupilGameProgressId -import edu.ntnu.idi.idatt.backend.game.gameProgress.infrastructure.PupilGameProgressRepository +import edu.ntnu.idi.idatt.backend.game.gameProgress.application.PupilGameProgressService import edu.ntnu.idi.idatt.backend.iam.application.ClassroomUserResolver import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver import edu.ntnu.idi.idatt.backend.iam.infrastructure.UserRepository @@ -25,8 +23,9 @@ import java.util.* * @property classroomRepository repository for classroom access. * @property currentUserResolver shared resolver for the authenticated user and role guard. * @property userRepository repository for user access. - * @property classroomUserResolver repository for classroom access. - * @property pupilGameProgressRepository pupil repository for game progress. + * @property classroomUserResolver resolver for classroom access control. + * @property pupilGameProgressService service for classroom-scoped game progress entries. + * @property taskRepository repository used to count classroom tasks. * @property domainEventPublisher publisher for application events. */ @Service @@ -35,7 +34,7 @@ class ClassroomService( private val currentUserResolver: CurrentUserResolver, private val userRepository: UserRepository, private val classroomUserResolver: ClassroomUserResolver, - private val pupilGameProgressRepository: PupilGameProgressRepository, + private val pupilGameProgressService: PupilGameProgressService, private val taskRepository: TaskRepository, private val domainEventPublisher: DomainEventPublisher, ) { @@ -124,15 +123,9 @@ class ClassroomService( classroom.addPupilMembership(user) classroomRepository.save(classroom) - pupilGameProgressRepository.save( - PupilGameProgress( - id = PupilGameProgressId( - pupilUserId = userId, - classroomId = classroom.id, - ), - pupil = user, - classroom = classroom, - ), + pupilGameProgressService.createPupilGameProgressEntry( + pupilId = userId, + classroomId = requireNotNull(classroom.id) { "Classroom is missing an id" }, ) domainEventPublisher.publish(PupilJoinedClassroomEvent(pupil = user, classroom = classroom)) diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/gameProgress/application/PupilGameProgressService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/gameProgress/application/PupilGameProgressService.kt new file mode 100644 index 00000000..0349c946 --- /dev/null +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/gameProgress/application/PupilGameProgressService.kt @@ -0,0 +1,138 @@ +package edu.ntnu.idi.idatt.backend.game.gameProgress.application + +import edu.ntnu.idi.idatt.backend.classroom.infrastructure.ClassroomRepository +import edu.ntnu.idi.idatt.backend.game.gameProgress.domain.PupilGameProgress +import edu.ntnu.idi.idatt.backend.game.gameProgress.domain.PupilGameProgressId +import edu.ntnu.idi.idatt.backend.game.gameProgress.infrastructure.PupilGameProgressRepository +import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver +import edu.ntnu.idi.idatt.backend.iam.infrastructure.UserRepository +import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser +import org.springframework.http.HttpStatus +import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional +import org.springframework.web.server.ResponseStatusException +import java.time.Instant + +/** + * Application service for classroom-scoped pupil game progress. + * + * The service centralizes reads and writes for the aggregated progress row tied + * to one pupil and one classroom. It is used by XP awarding, classroom join + * flows, and profile/notebook lookups that need progress-derived values. + * + * @property pupilGameProgressRepository repository for persisted pupil game-progress rows + * @property userRepository repository used to resolve pupils when creating new progress rows + * @property classroomRepository repository used to resolve classrooms when creating new progress rows + * @property currentUserResolver resolver used to derive the active pupil and classroom from the authenticated user + */ +@Service +class PupilGameProgressService( + private val pupilGameProgressRepository: PupilGameProgressRepository, + private val userRepository: UserRepository, + private val classroomRepository: ClassroomRepository, + private val currentUserResolver: CurrentUserResolver, +) { + companion object { + private const val XP_PER_LEVEL = 150 + } + + /** + * Updates the authenticated pupil's classroom-scoped XP total and derived level. + * + * @param currentUser authenticated pupil whose progress should be updated + * @param totalXp recalculated classroom-scoped total XP + * + * If no progress row exists for the active classroom, the update is skipped. + */ + @Transactional + fun updateUserXpAndLevel(currentUser: CurrentUser, totalXp: Int) { + val pupil = currentUserResolver.requirePupil(currentUser.subject) + val pupilId = requireNotNull(pupil.id) { "Pupil is missing an id" } + val classroom = currentUserResolver.getClassroomByUser(currentUser) + val classroomId = requireNotNull(classroom.id) { "Classroom is missing an id" } + val pupilGameProgress = pupilGameProgressRepository.findByPupilIdAndClassroomId(pupilId, classroomId) + ?: return + + val currentLevel = calculateLevelFromXp(totalXp) + + pupilGameProgress.updateProgress( + totalScore = totalXp, + totalXp = totalXp, + currentLevel = currentLevel, + completedStopsCount = pupilGameProgress.completedStopsCount, + completedTasksCount = pupilGameProgress.completedTasksCount, + finalBossCompleted = pupilGameProgress.finalBossCompleted, + lastActivityAt = Instant.now(), + ) + + pupilGameProgressRepository.save(pupilGameProgress) + } + + @Transactional(readOnly = true) + fun getUserCompletedStopsCount(currentUser: CurrentUser): Int { + val pupil = currentUserResolver.requirePupil(currentUser.subject) + val pupilId = requireNotNull(pupil.id) { "Authenticated pupil is missing an id" } + + val classroom = currentUserResolver.getClassroomByUser(currentUser) + val classroomId = requireNotNull(classroom.id) { "Active classroom is missing an id" } + + val gameProgress = pupilGameProgressRepository.findByPupilIdAndClassroomId(pupilId, classroomId) + val completedStopsCount = gameProgress?.completedStopsCount ?: 0 + + return completedStopsCount + } + + /** + * Returns the persisted progress row for one pupil in one classroom. + * + * @param pupilId persisted pupil user id + * @param classroomId persisted classroom id + * @return classroom-scoped progress row, or `null` when none exists + */ + @Transactional(readOnly = true) + fun getPupilProgress(pupilId: Long, classroomId: Long): PupilGameProgress? = + pupilGameProgressRepository.findByPupilIdAndClassroomId(pupilId, classroomId) + + /** + * Creates an empty classroom-scoped progress row for a pupil if it does not already exist. + * + * @param pupilId persisted pupil user id + * @param classroomId persisted classroom id + * @throws ResponseStatusException when the pupil or classroom does not exist + */ + @Transactional + fun createPupilGameProgressEntry(pupilId: Long, classroomId: Long) { + if (pupilGameProgressRepository.findByPupilIdAndClassroomId(pupilId, classroomId) != null) { + return + } + + val pupil = userRepository.findById(pupilId).orElseThrow { + ResponseStatusException(HttpStatus.NOT_FOUND, "Pupil not found") + } + val classroom = classroomRepository.findById(classroomId).orElseThrow { + ResponseStatusException(HttpStatus.NOT_FOUND, "Classroom not found") + } + + pupilGameProgressRepository.save( + PupilGameProgress( + id = PupilGameProgressId( + pupilUserId = pupilId, + classroomId = requireNotNull(classroom.id) { "Classroom is missing an id" }, + ), + pupil = pupil, + classroom = classroom, + ), + ) + } + + /** + * Maps accumulated XP to a 1-based level. + * + * Each full [XP_PER_LEVEL] XP increases the pupil's level by one. + * + * @param totalXp classroom-scoped accumulated XP + * @return derived 1-based level. + */ + private fun calculateLevelFromXp(totalXp: Int): Int = + (totalXp / XP_PER_LEVEL) + 1 +} diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/tasks/domain/TaskType.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/tasks/domain/TaskType.kt index 449f6873..96820b67 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/tasks/domain/TaskType.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/game/tasks/domain/TaskType.kt @@ -4,6 +4,7 @@ package edu.ntnu.idi.idatt.backend.game.tasks.domain * Coarse content categories for authored tasks. */ enum class TaskType { + EMAIL_REVIEW, NEWS_COMPARISON, IMAGE_ASSESSMENT, IMAGE_SELECT, diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt index 6c53dc07..bbcae205 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/notebook/application/NotebookService.kt @@ -1,6 +1,6 @@ package edu.ntnu.idi.idatt.backend.notebook.application -import edu.ntnu.idi.idatt.backend.game.gameProgress.infrastructure.PupilGameProgressRepository +import edu.ntnu.idi.idatt.backend.game.gameProgress.application.PupilGameProgressService import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.MapStopsRepository import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser @@ -20,13 +20,15 @@ import org.springframework.web.server.ResponseStatusException * @property notebookEntryRepository repository used to load and persist notebook entries * @property currentUserResolver resolver used to enforce that only pupil users access notebook entries * @property mapStopsRepository repository used to validate referenced map stops during creation + * @property pupilGameProgressService service used to resolve notebook visibility from pupil progress + * @property xpService service used to award XP for notebook activity */ @Service class NotebookService( private val notebookEntryRepository: NotebookEntryRepository, private val currentUserResolver: CurrentUserResolver, private val mapStopsRepository: MapStopsRepository, - private val pupilGameProgressRepository: PupilGameProgressRepository, + private val pupilGameProgressService: PupilGameProgressService, private val xpService: XpService, ) { /** @@ -43,8 +45,7 @@ class NotebookService( val classroom = currentUserResolver.getClassroomByUser(currentUser) val classroomId = requireNotNull(classroom.id) { "Active classroom is missing an id" } - val gameProgress = pupilGameProgressRepository.findByPupilIdAndClassroomId(pupilId, classroomId) - val completedStopsCount = gameProgress?.completedStopsCount ?: 0 + val completedStopsCount = pupilGameProgressService.getUserCompletedStopsCount(currentUser) return notebookEntryRepository.findAllVisibleToPupil(pupilId, classroomId, completedStopsCount) } @@ -58,7 +59,6 @@ class NotebookService( @Transactional fun createNotebookEntry(command: CreateNotebookEntryCommand): NotebookEntry { val pupil = currentUserResolver.requirePupil(command.currentUser.subject) - val pupilId = requireNotNull(pupil.id) { "Authenticated pupil is missing an id" } val classroom = currentUserResolver.getClassroomByUser(command.currentUser) if (command.sourceType == NotebookSourceType.GAME) { diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/profile/application/CurrentUserProfileService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/profile/application/CurrentUserProfileService.kt index eac625c6..0462f90f 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/profile/application/CurrentUserProfileService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/users/profile/application/CurrentUserProfileService.kt @@ -1,6 +1,7 @@ package edu.ntnu.idi.idatt.backend.users.profile.application -import edu.ntnu.idi.idatt.backend.game.gameProgress.infrastructure.PupilGameProgressRepository +import edu.ntnu.idi.idatt.backend.game.gameProgress.application.PupilGameProgressService +import edu.ntnu.idi.idatt.backend.game.gameProgress.domain.PupilGameProgress import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver import edu.ntnu.idi.idatt.backend.iam.domain.UserRole import edu.ntnu.idi.idatt.backend.users.profile.domain.PupilProfile @@ -16,14 +17,14 @@ import org.springframework.web.server.ResponseStatusException * * @property currentUserResolver resolver to authenticate users * @property pupilProfileRepository repository used to load pupil profile data - * @property pupilGameProgressRepository repository used to load classroom-scoped game progress + * @property pupilGameProgressService service used to load classroom-scoped game progress * @property teacherProfileRepository repository used to load the teacher profile data */ @Service class CurrentUserProfileService( private val currentUserResolver: CurrentUserResolver, private val pupilProfileRepository: PupilProfileRepository, - private val pupilGameProgressRepository: PupilGameProgressRepository, + private val pupilGameProgressService: PupilGameProgressService, private val teacherProfileRepository: TeacherProfileRepository, ) { /** @@ -120,8 +121,15 @@ class CurrentUserProfileService( ResponseStatusException(HttpStatus.NOT_FOUND, "Pupil profile was not found") } - private fun getCurrentClassroomProgress(userId: Long, classroomId: Long?) = - classroomId?.let { pupilGameProgressRepository.findByPupilIdAndClassroomId(userId, it) } + /** + * Loads the classroom-scoped progress row for the supplied pupil if a current classroom is set. + * + * @param userId persisted pupil user id + * @param classroomId current classroom id from the pupil profile, or `null` + * @return classroom-scoped progress row, or `null` when no classroom or progress row exists + */ + private fun getCurrentClassroomProgress(userId: Long, classroomId: Long?): PupilGameProgress? = + classroomId?.let { pupilGameProgressService.getPupilProgress(userId, it) } } /** @@ -145,6 +153,11 @@ data class CurrentUserProfile( /** * Internal pupil profile projection. + * + * @property displayName pupil-facing display name + * @property currentClassroomId active classroom id, or `null` when the pupil has none selected + * @property totalXp classroom-scoped accumulated XP for the active classroom + * @property currentLevel classroom-scoped level derived from the active classroom XP total */ data class PupilProfileDetails( val displayName: String, @@ -155,6 +168,9 @@ data class PupilProfileDetails( /** * Internal teacher profile projection reserved for future support. + * + * @property teacherName teacher full name shown in the product + * @property schoolName school name associated with the teacher */ data class TeacherProfileDetails( val teacherName: String, diff --git a/src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/application/XpService.kt b/src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/application/XpService.kt index be545d8d..595c98e7 100644 --- a/src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/application/XpService.kt +++ b/src/main/kotlin/edu/ntnu/idi/idatt/backend/xp/application/XpService.kt @@ -1,6 +1,6 @@ package edu.ntnu.idi.idatt.backend.xp.application -import edu.ntnu.idi.idatt.backend.game.gameProgress.infrastructure.PupilGameProgressRepository +import edu.ntnu.idi.idatt.backend.game.gameProgress.application.PupilGameProgressService import edu.ntnu.idi.idatt.backend.xp.domain.PupilXpEvent import edu.ntnu.idi.idatt.backend.xp.domain.PupilXpEventId import edu.ntnu.idi.idatt.backend.xp.domain.XpEventCode @@ -8,11 +8,9 @@ import edu.ntnu.idi.idatt.backend.xp.infrastructure.PupilXpEventRepository import edu.ntnu.idi.idatt.backend.xp.infrastructure.XpEventRepository import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver import edu.ntnu.idi.idatt.backend.iam.security.CurrentUser -import org.springframework.http.HttpStatus import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional import org.springframework.web.server.ResponseStatusException -import java.time.Instant /** * Application service for awarding XP events to pupils. @@ -23,19 +21,16 @@ import java.time.Instant * * @property pupilXpEventRepository repository for awarded pupil XP rows * @property xpEventRepository repository for XP event definitions - * @property pupilGameProgressRepository repository for classroom-scoped pupil game progress + * @property pupilGameProgressService service for classroom-scoped pupil game progress * @property currentUserResolver resolver for the authenticated pupil user */ @Service class XpService( private val pupilXpEventRepository: PupilXpEventRepository, private val xpEventRepository: XpEventRepository, - private val pupilGameProgressRepository: PupilGameProgressRepository, + private val pupilGameProgressService: PupilGameProgressService, private val currentUserResolver: CurrentUserResolver, ) { - companion object { - private const val XP_PER_LEVEL = 150 - } /** * Awards one XP event to the authenticated pupil. @@ -84,22 +79,20 @@ class XpService( * classroom progress record. * * @param currentUser authenticated pupil whose progress should be recalculated - * @throws ResponseStatusException when no game progress row exists for the active classroom + * If no classroom context is available for the user, the progress update is skipped. + * + * @param currentUser authenticated pupil whose progress should be recalculated */ @Transactional fun updatePupilGameProgressLevel(currentUser: CurrentUser) { val pupil = currentUserResolver.requirePupil(currentUser.subject) val pupilId = requireNotNull(pupil.id) { "Pupil is missing an id" } - val classroom = try { + try { currentUserResolver.getClassroomByUser(currentUser) } catch (_: ResponseStatusException) { return } - val classroomId = requireNotNull(classroom.id) { "Classroom is missing an id" } - - val pupilGameProgress = pupilGameProgressRepository.findByPupilIdAndClassroomId(pupilId, classroomId) - ?: return val pupilXpEvents = pupilXpEventRepository.findAllByIdPupilUserId(pupilId) val totalXp = pupilXpEvents.sumOf { pupilXpEvent -> @@ -108,29 +101,12 @@ class XpService( } xpEvent.xpScore * pupilXpEvent.completionCount } - val currentLevel = calculateLevelFromXp(totalXp) - pupilGameProgress.updateProgress( - totalScore = totalXp, - totalXp = totalXp, - currentLevel = currentLevel, - completedStopsCount = pupilGameProgress.completedStopsCount, - completedTasksCount = pupilGameProgress.completedTasksCount, - finalBossCompleted = pupilGameProgress.finalBossCompleted, - lastActivityAt = Instant.now(), - ) - pupilGameProgressRepository.save(pupilGameProgress) + try { + pupilGameProgressService.updateUserXpAndLevel(currentUser, totalXp) + } catch (_: ResponseStatusException) { + return + } } - /** - * Maps accumulated XP to a 1-based level. - * - * Each full [XP_PER_LEVEL] XP increases the pupil's level by one, while - * pupils with no XP remain at level 1. - * - * @param totalXp classroom-scoped accumulated XP. - * @return derived 1-based level. - */ - private fun calculateLevelFromXp(totalXp: Int): Int = - (totalXp / XP_PER_LEVEL) + 1 } diff --git a/src/test/kotlin/edu/ntnu/idi/idatt/backend/classroom/ClassroomServiceTest.kt b/src/test/kotlin/edu/ntnu/idi/idatt/backend/classroom/ClassroomServiceTest.kt index 997b0565..253a3d52 100644 --- a/src/test/kotlin/edu/ntnu/idi/idatt/backend/classroom/ClassroomServiceTest.kt +++ b/src/test/kotlin/edu/ntnu/idi/idatt/backend/classroom/ClassroomServiceTest.kt @@ -10,8 +10,7 @@ import edu.ntnu.idi.idatt.backend.classroom.domain.ClassroomPupilMembershipId import edu.ntnu.idi.idatt.backend.classroom.domain.ClassroomPupilStatus import edu.ntnu.idi.idatt.backend.classroom.domain.ClassroomStatus import edu.ntnu.idi.idatt.backend.classroom.infrastructure.ClassroomRepository -import edu.ntnu.idi.idatt.backend.game.gameProgress.domain.PupilGameProgress -import edu.ntnu.idi.idatt.backend.game.gameProgress.infrastructure.PupilGameProgressRepository +import edu.ntnu.idi.idatt.backend.game.gameProgress.application.PupilGameProgressService import edu.ntnu.idi.idatt.backend.iam.application.ClassroomUserResolver import edu.ntnu.idi.idatt.backend.game.tasks.infrastructure.TaskRepository import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver @@ -25,19 +24,19 @@ import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import org.mockito.ArgumentMatchers.any +import org.mockito.ArgumentMatchers.anyLong import org.mockito.Mockito.mock import org.mockito.Mockito.never import org.mockito.Mockito.verify import org.mockito.Mockito.`when` import org.springframework.http.HttpStatus import org.springframework.web.server.ResponseStatusException -import org.mockito.Mockito.any class ClassroomServiceTest { private lateinit var classroomRepository: ClassroomRepository private lateinit var currentUserResolver: CurrentUserResolver private lateinit var classroomUserResolver: ClassroomUserResolver - private lateinit var pupilGameProgressRepository: PupilGameProgressRepository + private lateinit var pupilGameProgressService: PupilGameProgressService private lateinit var taskRepository: TaskRepository private lateinit var classroomService: ClassroomService private lateinit var userRepository: UserRepository @@ -49,7 +48,7 @@ class ClassroomServiceTest { currentUserResolver = mock(CurrentUserResolver::class.java) userRepository = mock(UserRepository::class.java) classroomUserResolver = mock(ClassroomUserResolver::class.java) - pupilGameProgressRepository = mock(PupilGameProgressRepository::class.java) + pupilGameProgressService = mock(PupilGameProgressService::class.java) taskRepository = mock(TaskRepository::class.java) eventPublisher = mock(DomainEventPublisher::class.java) classroomService = ClassroomService( @@ -57,7 +56,7 @@ class ClassroomServiceTest { currentUserResolver, userRepository, classroomUserResolver, - pupilGameProgressRepository, + pupilGameProgressService, taskRepository, eventPublisher, ) @@ -108,7 +107,7 @@ class ClassroomServiceTest { assertEquals("class", result.classroomTitle) assertEquals("PENDING", result.membershipStatus) verify(classroomRepository).save(classroom) - verify(pupilGameProgressRepository).save(any(PupilGameProgress::class.java)) + verify(pupilGameProgressService).createPupilGameProgressEntry(3L, 1L) verify(classroomRepository).findByJoinCode(classroomCode) } @@ -128,7 +127,7 @@ class ClassroomServiceTest { assertEquals(HttpStatus.CONFLICT, exception.statusCode) verify(classroomRepository, never()).save(classroom) - verify(pupilGameProgressRepository, never()).save(any(PupilGameProgress::class.java)) + verify(pupilGameProgressService, never()).createPupilGameProgressEntry(anyLong(), anyLong()) } @Test @@ -144,7 +143,7 @@ class ClassroomServiceTest { classroomService.joinClassroom(command) } assertEquals(HttpStatus.NOT_FOUND, exception.statusCode) - verify(pupilGameProgressRepository, never()).save(any(PupilGameProgress::class.java)) + verify(pupilGameProgressService, never()).createPupilGameProgressEntry(anyLong(), anyLong()) } @Test @@ -170,7 +169,7 @@ class ClassroomServiceTest { } assertEquals(HttpStatus.CONFLICT, exception.statusCode) - verify(pupilGameProgressRepository, never()).save(any(PupilGameProgress::class.java)) + verify(pupilGameProgressService, never()).createPupilGameProgressEntry(anyLong(), anyLong()) } @Test @@ -191,7 +190,7 @@ class ClassroomServiceTest { assertEquals(HttpStatus.FORBIDDEN, exception.statusCode) verify(classroomRepository, never()).save(classroom) - verify(pupilGameProgressRepository, never()).save(any(PupilGameProgress::class.java)) + verify(pupilGameProgressService, never()).createPupilGameProgressEntry(anyLong(), anyLong()) } @Test @@ -212,7 +211,7 @@ class ClassroomServiceTest { assertEquals(HttpStatus.FORBIDDEN, exception.statusCode) verify(classroomRepository, never()).save(classroom) - verify(pupilGameProgressRepository, never()).save(any(PupilGameProgress::class.java)) + verify(pupilGameProgressService, never()).createPupilGameProgressEntry(anyLong(), anyLong()) } @Test @@ -227,7 +226,7 @@ class ClassroomServiceTest { } assertEquals(HttpStatus.UNAUTHORIZED, exception.statusCode) - verify(pupilGameProgressRepository, never()).save(any(PupilGameProgress::class.java)) + verify(pupilGameProgressService, never()).createPupilGameProgressEntry(anyLong(), anyLong()) } @Test diff --git a/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookServiceTests.kt b/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookServiceTests.kt index 215e3c24..fbd731de 100644 --- a/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookServiceTests.kt +++ b/src/test/kotlin/edu/ntnu/idi/idatt/backend/notebook/NotebookServiceTests.kt @@ -1,7 +1,7 @@ package edu.ntnu.idi.idatt.backend.notebook import edu.ntnu.idi.idatt.backend.classroom.domain.Classroom -import edu.ntnu.idi.idatt.backend.game.gameProgress.infrastructure.PupilGameProgressRepository +import edu.ntnu.idi.idatt.backend.game.gameProgress.application.PupilGameProgressService import edu.ntnu.idi.idatt.backend.game.mapstops.domain.MapStop import edu.ntnu.idi.idatt.backend.game.mapstops.infrastructure.MapStopsRepository import edu.ntnu.idi.idatt.backend.xp.application.XpService @@ -33,13 +33,13 @@ class NotebookServiceTests { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) - val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val pupilGameProgressService = Mockito.mock(PupilGameProgressService::class.java) val xpService = Mockito.mock(XpService::class.java) val service = NotebookService( notebookEntryRepository, currentUserResolver, mapStopsRepository, - pupilGameProgressRepository, + pupilGameProgressService, xpService, ) val currentUser = currentUser("42") @@ -49,7 +49,7 @@ class NotebookServiceTests { Mockito.`when`(currentUserResolver.requirePupil("42")).thenReturn(pupil) Mockito.`when`(currentUserResolver.getClassroomByUser(currentUser)).thenReturn(classroom) - Mockito.`when`(pupilGameProgressRepository.findByPupilIdAndClassroomId(42L, 7L)).thenReturn(null) + Mockito.`when`(pupilGameProgressService.getUserCompletedStopsCount(currentUser)).thenReturn(0) Mockito.`when`(notebookEntryRepository.findAllVisibleToPupil(42L, 7L, 0)).thenReturn(entries) val result = service.getAllNotebookEntries(currentUser) @@ -62,13 +62,13 @@ class NotebookServiceTests { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) - val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val pupilGameProgressService = Mockito.mock(PupilGameProgressService::class.java) val xpService = Mockito.mock(XpService::class.java) val service = NotebookService( notebookEntryRepository, currentUserResolver, mapStopsRepository, - pupilGameProgressRepository, + pupilGameProgressService, xpService, ) val currentUser = currentUser("42") @@ -109,13 +109,13 @@ class NotebookServiceTests { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) - val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val pupilGameProgressService = Mockito.mock(PupilGameProgressService::class.java) val xpService = Mockito.mock(XpService::class.java) val service = NotebookService( notebookEntryRepository, currentUserResolver, mapStopsRepository, - pupilGameProgressRepository, + pupilGameProgressService, xpService, ) val currentUser = currentUser("42") @@ -145,13 +145,13 @@ class NotebookServiceTests { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) - val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val pupilGameProgressService = Mockito.mock(PupilGameProgressService::class.java) val xpService = Mockito.mock(XpService::class.java) val service = NotebookService( notebookEntryRepository, currentUserResolver, mapStopsRepository, - pupilGameProgressRepository, + pupilGameProgressService, xpService, ) val currentUser = currentUser("42") @@ -181,13 +181,13 @@ class NotebookServiceTests { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) - val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val pupilGameProgressService = Mockito.mock(PupilGameProgressService::class.java) val xpService = Mockito.mock(XpService::class.java) val service = NotebookService( notebookEntryRepository, currentUserResolver, mapStopsRepository, - pupilGameProgressRepository, + pupilGameProgressService, xpService, ) val currentUser = currentUser("42") @@ -218,13 +218,13 @@ class NotebookServiceTests { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) - val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val pupilGameProgressService = Mockito.mock(PupilGameProgressService::class.java) val xpService = Mockito.mock(XpService::class.java) val service = NotebookService( notebookEntryRepository, currentUserResolver, mapStopsRepository, - pupilGameProgressRepository, + pupilGameProgressService, xpService, ) val currentUser = currentUser("42") @@ -256,13 +256,13 @@ class NotebookServiceTests { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) - val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val pupilGameProgressService = Mockito.mock(PupilGameProgressService::class.java) val xpService = Mockito.mock(XpService::class.java) val service = NotebookService( notebookEntryRepository, currentUserResolver, mapStopsRepository, - pupilGameProgressRepository, + pupilGameProgressService, xpService, ) val currentUser = currentUser("42") @@ -290,13 +290,13 @@ class NotebookServiceTests { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) - val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val pupilGameProgressService = Mockito.mock(PupilGameProgressService::class.java) val xpService = Mockito.mock(XpService::class.java) val service = NotebookService( notebookEntryRepository, currentUserResolver, mapStopsRepository, - pupilGameProgressRepository, + pupilGameProgressService, xpService, ) val currentUser = currentUser("42") @@ -320,13 +320,13 @@ class NotebookServiceTests { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val notebookEntryRepository = Mockito.mock(NotebookEntryRepository::class.java) val mapStopsRepository = Mockito.mock(MapStopsRepository::class.java) - val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val pupilGameProgressService = Mockito.mock(PupilGameProgressService::class.java) val xpService = Mockito.mock(XpService::class.java) val service = NotebookService( notebookEntryRepository, currentUserResolver, mapStopsRepository, - pupilGameProgressRepository, + pupilGameProgressService, xpService, ) val currentUser = currentUser("42") diff --git a/src/test/kotlin/edu/ntnu/idi/idatt/backend/users/profile/CurrentUserProfileServiceTests.kt b/src/test/kotlin/edu/ntnu/idi/idatt/backend/users/profile/CurrentUserProfileServiceTests.kt index 0be7d098..dae2b0d7 100644 --- a/src/test/kotlin/edu/ntnu/idi/idatt/backend/users/profile/CurrentUserProfileServiceTests.kt +++ b/src/test/kotlin/edu/ntnu/idi/idatt/backend/users/profile/CurrentUserProfileServiceTests.kt @@ -1,7 +1,7 @@ package edu.ntnu.idi.idatt.backend.users.profile import edu.ntnu.idi.idatt.backend.game.gameProgress.domain.PupilGameProgress -import edu.ntnu.idi.idatt.backend.game.gameProgress.infrastructure.PupilGameProgressRepository +import edu.ntnu.idi.idatt.backend.game.gameProgress.application.PupilGameProgressService import edu.ntnu.idi.idatt.backend.iam.application.CurrentUserResolver import edu.ntnu.idi.idatt.backend.iam.domain.User import edu.ntnu.idi.idatt.backend.iam.domain.UserRole @@ -25,12 +25,12 @@ class CurrentUserProfileServiceTests { fun `get current user profile returns pupil profile for pupil user`() { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val pupilProfileRepository = Mockito.mock(PupilProfileRepository::class.java) - val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val pupilGameProgressService = Mockito.mock(PupilGameProgressService::class.java) val teacherProfileRepository = Mockito.mock(TeacherProfileRepository::class.java) val service = CurrentUserProfileService( currentUserResolver, pupilProfileRepository, - pupilGameProgressRepository, + pupilGameProgressService, teacherProfileRepository, ) val user = pupilUser(42L) @@ -44,7 +44,7 @@ class CurrentUserProfileServiceTests { Mockito.`when`(currentUserResolver.requireUser("42")).thenReturn(user) Mockito.`when`(pupilProfileRepository.findById(42L)).thenReturn(Optional.of(pupilProfile)) - Mockito.`when`(pupilGameProgressRepository.findByPupilIdAndClassroomId(42L, 101L)).thenReturn( + Mockito.`when`(pupilGameProgressService.getPupilProgress(42L, 101L)).thenReturn( PupilGameProgress(totalXp = 480, currentLevel = 4), ) @@ -65,12 +65,12 @@ class CurrentUserProfileServiceTests { fun `get current user profile returns default progress when pupil has no current classroom`() { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val pupilProfileRepository = Mockito.mock(PupilProfileRepository::class.java) - val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val pupilGameProgressService = Mockito.mock(PupilGameProgressService::class.java) val teacherProfileRepository = Mockito.mock(TeacherProfileRepository::class.java) val service = CurrentUserProfileService( currentUserResolver, pupilProfileRepository, - pupilGameProgressRepository, + pupilGameProgressService, teacherProfileRepository, ) val user = pupilUser(42L) @@ -89,19 +89,19 @@ class CurrentUserProfileServiceTests { assertEquals(0, resolvedProfile.pupilProfile?.totalXp) assertEquals(1, resolvedProfile.pupilProfile?.currentLevel) - Mockito.verifyNoInteractions(pupilGameProgressRepository) + Mockito.verifyNoInteractions(pupilGameProgressService) } @Test fun `get current user profile returns default progress when classroom progress row is missing`() { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val pupilProfileRepository = Mockito.mock(PupilProfileRepository::class.java) - val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val pupilGameProgressService = Mockito.mock(PupilGameProgressService::class.java) val teacherProfileRepository = Mockito.mock(TeacherProfileRepository::class.java) val service = CurrentUserProfileService( currentUserResolver, pupilProfileRepository, - pupilGameProgressRepository, + pupilGameProgressService, teacherProfileRepository, ) val user = pupilUser(42L) @@ -115,7 +115,7 @@ class CurrentUserProfileServiceTests { Mockito.`when`(currentUserResolver.requireUser("42")).thenReturn(user) Mockito.`when`(pupilProfileRepository.findById(42L)).thenReturn(Optional.of(pupilProfile)) - Mockito.`when`(pupilGameProgressRepository.findByPupilIdAndClassroomId(42L, 101L)).thenReturn(null) + Mockito.`when`(pupilGameProgressService.getPupilProgress(42L, 101L)).thenReturn(null) val resolvedProfile = service.getCurrentUserProfile("42") @@ -127,12 +127,12 @@ class CurrentUserProfileServiceTests { fun `get current user profile returns teacher shell for teacher user`() { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val pupilProfileRepository = Mockito.mock(PupilProfileRepository::class.java) - val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val pupilGameProgressService = Mockito.mock(PupilGameProgressService::class.java) val teacherProfileRepository = Mockito.mock(TeacherProfileRepository::class.java) val service = CurrentUserProfileService( currentUserResolver, pupilProfileRepository, - pupilGameProgressRepository, + pupilGameProgressService, teacherProfileRepository, ) val user = teacherUser(7L) @@ -162,12 +162,12 @@ class CurrentUserProfileServiceTests { fun `get current user profile rejects invalid authenticated subject`() { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val pupilProfileRepository = Mockito.mock(PupilProfileRepository::class.java) - val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val pupilGameProgressService = Mockito.mock(PupilGameProgressService::class.java) val teacherProfileRepository = Mockito.mock(TeacherProfileRepository::class.java) val service = CurrentUserProfileService( currentUserResolver, pupilProfileRepository, - pupilGameProgressRepository, + pupilGameProgressService, teacherProfileRepository, ) val exception = ResponseStatusException(HttpStatus.UNAUTHORIZED, "Invalid authenticated user subject") @@ -185,12 +185,12 @@ class CurrentUserProfileServiceTests { fun `get current user profile rejects unknown authenticated user`() { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val pupilProfileRepository = Mockito.mock(PupilProfileRepository::class.java) - val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val pupilGameProgressService = Mockito.mock(PupilGameProgressService::class.java) val teacherProfileRepository = Mockito.mock(TeacherProfileRepository::class.java) val service = CurrentUserProfileService( currentUserResolver, pupilProfileRepository, - pupilGameProgressRepository, + pupilGameProgressService, teacherProfileRepository, ) val exception = ResponseStatusException(HttpStatus.UNAUTHORIZED, "Authenticated user was not found") @@ -208,12 +208,12 @@ class CurrentUserProfileServiceTests { fun `get current user profile rejects pupil without profile row`() { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val pupilProfileRepository = Mockito.mock(PupilProfileRepository::class.java) - val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val pupilGameProgressService = Mockito.mock(PupilGameProgressService::class.java) val teacherProfileRepository = Mockito.mock(TeacherProfileRepository::class.java) val service = CurrentUserProfileService( currentUserResolver, pupilProfileRepository, - pupilGameProgressRepository, + pupilGameProgressService, teacherProfileRepository, ) val user = pupilUser(42L) @@ -232,12 +232,12 @@ class CurrentUserProfileServiceTests { fun `get current user profile rejects teacher without profile row`() { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val pupilProfileRepository = Mockito.mock(PupilProfileRepository::class.java) - val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val pupilGameProgressService = Mockito.mock(PupilGameProgressService::class.java) val teacherProfileRepository = Mockito.mock(TeacherProfileRepository::class.java) val service = CurrentUserProfileService( currentUserResolver, pupilProfileRepository, - pupilGameProgressRepository, + pupilGameProgressService, teacherProfileRepository, ) val user = teacherUser(7L) @@ -256,12 +256,12 @@ class CurrentUserProfileServiceTests { fun `update current user display name persists submitted value for pupil`() { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val pupilProfileRepository = Mockito.mock(PupilProfileRepository::class.java) - val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val pupilGameProgressService = Mockito.mock(PupilGameProgressService::class.java) val teacherProfileRepository = Mockito.mock(TeacherProfileRepository::class.java) val service = CurrentUserProfileService( currentUserResolver, pupilProfileRepository, - pupilGameProgressRepository, + pupilGameProgressService, teacherProfileRepository, ) val user = pupilUser(42L) @@ -278,7 +278,7 @@ class CurrentUserProfileServiceTests { Mockito.`when`(pupilProfileRepository.save(Mockito.any(PupilProfile::class.java))).thenAnswer { invocation -> invocation.arguments[0] as PupilProfile } - Mockito.`when`(pupilGameProgressRepository.findByPupilIdAndClassroomId(42L, 101L)).thenReturn( + Mockito.`when`(pupilGameProgressService.getPupilProgress(42L, 101L)).thenReturn( PupilGameProgress(totalXp = 480, currentLevel = 4), ) @@ -298,12 +298,12 @@ class CurrentUserProfileServiceTests { fun `update current user display name rejects teacher user`() { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val pupilProfileRepository = Mockito.mock(PupilProfileRepository::class.java) - val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val pupilGameProgressService = Mockito.mock(PupilGameProgressService::class.java) val teacherProfileRepository = Mockito.mock(TeacherProfileRepository::class.java) val service = CurrentUserProfileService( currentUserResolver, pupilProfileRepository, - pupilGameProgressRepository, + pupilGameProgressService, teacherProfileRepository, ) val exception = ResponseStatusException(HttpStatus.FORBIDDEN, "Only pupils are allowed") @@ -324,12 +324,12 @@ class CurrentUserProfileServiceTests { fun `update current user display name rejects pupil without profile row`() { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val pupilProfileRepository = Mockito.mock(PupilProfileRepository::class.java) - val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val pupilGameProgressService = Mockito.mock(PupilGameProgressService::class.java) val teacherProfileRepository = Mockito.mock(TeacherProfileRepository::class.java) val service = CurrentUserProfileService( currentUserResolver, pupilProfileRepository, - pupilGameProgressRepository, + pupilGameProgressService, teacherProfileRepository, ) val user = pupilUser(42L) diff --git a/src/test/kotlin/edu/ntnu/idi/idatt/backend/xp/XpServiceTests.kt b/src/test/kotlin/edu/ntnu/idi/idatt/backend/xp/XpServiceTests.kt index 50ccc63b..5faff877 100644 --- a/src/test/kotlin/edu/ntnu/idi/idatt/backend/xp/XpServiceTests.kt +++ b/src/test/kotlin/edu/ntnu/idi/idatt/backend/xp/XpServiceTests.kt @@ -1,9 +1,7 @@ package edu.ntnu.idi.idatt.backend.xp import edu.ntnu.idi.idatt.backend.classroom.domain.Classroom -import edu.ntnu.idi.idatt.backend.game.gameProgress.domain.PupilGameProgress -import edu.ntnu.idi.idatt.backend.game.gameProgress.domain.PupilGameProgressId -import edu.ntnu.idi.idatt.backend.game.gameProgress.infrastructure.PupilGameProgressRepository +import edu.ntnu.idi.idatt.backend.game.gameProgress.application.PupilGameProgressService import edu.ntnu.idi.idatt.backend.xp.application.XpService import edu.ntnu.idi.idatt.backend.xp.domain.PupilXpEvent import edu.ntnu.idi.idatt.backend.xp.domain.PupilXpEventId @@ -28,21 +26,19 @@ class XpServiceTests { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val pupilXpEventRepository = Mockito.mock(PupilXpEventRepository::class.java) val xpEventRepository = Mockito.mock(XpEventRepository::class.java) - val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val pupilGameProgressService = Mockito.mock(PupilGameProgressService::class.java) val service = XpService( pupilXpEventRepository, xpEventRepository, - pupilGameProgressRepository, + pupilGameProgressService, currentUserResolver, ) val currentUser = currentUser() val pupil = pupil() - val classroom = classroom() val xpEvent = xpEvent(id = 10L, xpScore = 75, repeatable = false) - val progress = gameProgress(pupil, classroom) Mockito.`when`(currentUserResolver.requirePupil(currentUser.subject)).thenReturn(pupil) - Mockito.`when`(currentUserResolver.getClassroomByUser(currentUser)).thenReturn(classroom) + Mockito.`when`(currentUserResolver.getClassroomByUser(currentUser)).thenReturn(classroom()) Mockito.`when`(xpEventRepository.findByCode(XpEventCode.STOP_COMPLETED)).thenReturn(xpEvent) Mockito.`when`(pupilXpEventRepository.findByIdPupilUserIdAndIdXpEventId(1L, 10L)).thenReturn(null) Mockito.`when`(pupilXpEventRepository.findAllByIdPupilUserId(1L)).thenReturn( @@ -55,7 +51,6 @@ class XpServiceTests { ), ), ) - Mockito.`when`(pupilGameProgressRepository.findByPupilIdAndClassroomId(1L, 5L)).thenReturn(progress) service.awardXpEvent(currentUser, XpEventCode.STOP_COMPLETED) @@ -68,9 +63,7 @@ class XpServiceTests { assertSame(xpEvent, savedAward.xpEvent) assertEquals(1, savedAward.completionCount) - assertEquals(75, progress.totalXp) - assertEquals(1, progress.currentLevel) - Mockito.verify(pupilGameProgressRepository).save(progress) + Mockito.verify(pupilGameProgressService).updateUserXpAndLevel(currentUser, 75) } @Test @@ -78,11 +71,11 @@ class XpServiceTests { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val pupilXpEventRepository = Mockito.mock(PupilXpEventRepository::class.java) val xpEventRepository = Mockito.mock(XpEventRepository::class.java) - val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val pupilGameProgressService = Mockito.mock(PupilGameProgressService::class.java) val service = XpService( pupilXpEventRepository, xpEventRepository, - pupilGameProgressRepository, + pupilGameProgressService, currentUserResolver, ) val currentUser = currentUser() @@ -101,7 +94,7 @@ class XpServiceTests { service.awardXpEvent(currentUser, XpEventCode.STOP_COMPLETED) Mockito.verify(pupilXpEventRepository, Mockito.never()).save(Mockito.any(PupilXpEvent::class.java)) - Mockito.verifyNoInteractions(pupilGameProgressRepository) + Mockito.verifyNoInteractions(pupilGameProgressService) } @Test @@ -109,16 +102,15 @@ class XpServiceTests { val currentUserResolver = Mockito.mock(CurrentUserResolver::class.java) val pupilXpEventRepository = Mockito.mock(PupilXpEventRepository::class.java) val xpEventRepository = Mockito.mock(XpEventRepository::class.java) - val pupilGameProgressRepository = Mockito.mock(PupilGameProgressRepository::class.java) + val pupilGameProgressService = Mockito.mock(PupilGameProgressService::class.java) val service = XpService( pupilXpEventRepository, xpEventRepository, - pupilGameProgressRepository, + pupilGameProgressService, currentUserResolver, ) val currentUser = currentUser() val pupil = pupil() - val classroom = classroom() val xpEvent = xpEvent(id = 10L, xpScore = 80, repeatable = true) val existingAward = PupilXpEvent( id = PupilXpEventId(pupilUserId = 1L, xpEventId = 10L), @@ -126,27 +118,18 @@ class XpServiceTests { xpEvent = xpEvent, completionCount = 1, ) - val progress = gameProgress( - pupil = pupil, - classroom = classroom, - totalXp = 80, - currentLevel = 1, - ) Mockito.`when`(currentUserResolver.requirePupil(currentUser.subject)).thenReturn(pupil) - Mockito.`when`(currentUserResolver.getClassroomByUser(currentUser)).thenReturn(classroom) + Mockito.`when`(currentUserResolver.getClassroomByUser(currentUser)).thenReturn(classroom()) Mockito.`when`(xpEventRepository.findByCode(XpEventCode.STOP_COMPLETED)).thenReturn(xpEvent) Mockito.`when`(pupilXpEventRepository.findByIdPupilUserIdAndIdXpEventId(1L, 10L)).thenReturn(existingAward) Mockito.`when`(pupilXpEventRepository.findAllByIdPupilUserId(1L)).thenReturn(listOf(existingAward)) - Mockito.`when`(pupilGameProgressRepository.findByPupilIdAndClassroomId(1L, 5L)).thenReturn(progress) service.awardXpEvent(currentUser, XpEventCode.STOP_COMPLETED) assertEquals(2, existingAward.completionCount) Mockito.verify(pupilXpEventRepository).save(existingAward) - assertEquals(160, progress.totalXp) - assertEquals(2, progress.currentLevel) - Mockito.verify(pupilGameProgressRepository).save(progress) + Mockito.verify(pupilGameProgressService).updateUserXpAndLevel(currentUser, 160) } private fun currentUser(): CurrentUser = @@ -182,21 +165,4 @@ class XpServiceTests { repeatable = repeatable, ) - private fun gameProgress( - pupil: User, - classroom: Classroom, - totalXp: Int = 0, - currentLevel: Int = 1, - ): PupilGameProgress = - PupilGameProgress( - id = PupilGameProgressId( - pupilUserId = requireNotNull(pupil.id), - classroomId = classroom.id, - ), - pupil = pupil, - classroom = classroom, - totalScore = 0, - totalXp = totalXp, - currentLevel = currentLevel, - ) }