From 4cea0f9285cc11ac9cfd1ace9e10544d7080745e Mon Sep 17 00:00:00 2001 From: entholzer Date: Fri, 3 Jan 2025 16:27:09 +0100 Subject: [PATCH] fixed architecture tests --- .../GroupNotificationServiceTest.java | 16 ++--- .../NotificationResourceIntegrationTest.java | 60 +++++++++---------- .../NotificationScheduleServiceTest.java | 10 ++-- .../ConversationNotificationServiceTest.java | 6 +- .../NotificationTestRepository.java | 4 ++ .../core/user/util/UserTestService.java | 4 +- 6 files changed, 53 insertions(+), 47 deletions(-) diff --git a/src/test/java/de/tum/cit/aet/artemis/communication/notification/GroupNotificationServiceTest.java b/src/test/java/de/tum/cit/aet/artemis/communication/notification/GroupNotificationServiceTest.java index 4ccf0116d463..2f109cb2d32e 100644 --- a/src/test/java/de/tum/cit/aet/artemis/communication/notification/GroupNotificationServiceTest.java +++ b/src/test/java/de/tum/cit/aet/artemis/communication/notification/GroupNotificationServiceTest.java @@ -48,11 +48,11 @@ import de.tum.cit.aet.artemis.communication.domain.Post; import de.tum.cit.aet.artemis.communication.domain.conversation.Channel; import de.tum.cit.aet.artemis.communication.domain.notification.Notification; -import de.tum.cit.aet.artemis.communication.repository.NotificationRepository; import de.tum.cit.aet.artemis.communication.repository.NotificationSettingRepository; import de.tum.cit.aet.artemis.communication.service.notifications.GroupNotificationScheduleService; import de.tum.cit.aet.artemis.core.domain.Course; import de.tum.cit.aet.artemis.core.domain.User; +import de.tum.cit.aet.artemis.core.test_repository.NotificationTestRepository; import de.tum.cit.aet.artemis.core.test_repository.UserTestRepository; import de.tum.cit.aet.artemis.core.user.util.UserUtilService; import de.tum.cit.aet.artemis.core.util.CourseUtilService; @@ -76,7 +76,7 @@ class GroupNotificationServiceTest extends AbstractSpringIntegrationIndependentT private static final String TEST_PREFIX = "groupnotificationservice"; @Autowired - private NotificationRepository notificationRepository; + private NotificationTestRepository notificationTestRepository; @Autowired private NotificationSettingRepository notificationSettingRepository; @@ -231,12 +231,12 @@ void setUp() { userUtilService.changeUser(TEST_PREFIX + "instructor1"); // store the current notification count to let tests work even if notifications are created in other tests - notificationCountBeforeTest = notificationRepository.findAll().size(); + notificationCountBeforeTest = notificationTestRepository.findAll().size(); } @AfterEach void tearDown() { - notificationRepository.deleteAllInBatch(); + notificationTestRepository.deleteAllInBatch(); } /** @@ -262,10 +262,10 @@ private Notification verifyRepositoryCallWithCorrectNotificationAndReturnNotific */ private Notification verifyRepositoryCallWithCorrectNotificationAndReturnNotificationAtIndex(int numberOfGroupsAndCalls, String expectedNotificationTitle, int index) { await().untilAsserted( - () -> assertThat(notificationRepository.findAll()).as("The number of created notifications should be the same as the number of notified groups/authorities") + () -> assertThat(notificationTestRepository.findAll()).as("The number of created notifications should be the same as the number of notified groups/authorities") .hasSize(numberOfGroupsAndCalls + notificationCountBeforeTest)); - List capturedNotifications = notificationRepository.findAll(); + List capturedNotifications = notificationTestRepository.findAll(); Notification lastCapturedNotification = capturedNotifications.get(capturedNotifications.size() - 1); assertThat(lastCapturedNotification.getTitle()).as("The title of the captured notification should be equal to the expected one").isEqualTo(expectedNotificationTitle); @@ -440,10 +440,10 @@ private void verifyPush(Notification notification, Set users, Object notif */ @Test void testNotifyStudentGroupAboutAttachmentChange_futureReleaseDate() { - var countBefore = notificationRepository.count(); + var countBefore = notificationTestRepository.count(); attachment.setReleaseDate(FUTURE_TIME); groupNotificationService.notifyStudentGroupAboutAttachmentChange(attachment, NOTIFICATION_TEXT); - var countAfter = notificationRepository.count(); + var countAfter = notificationTestRepository.count(); assertThat(countAfter).as("No notification should be created/saved").isEqualTo(countBefore); } diff --git a/src/test/java/de/tum/cit/aet/artemis/communication/notification/NotificationResourceIntegrationTest.java b/src/test/java/de/tum/cit/aet/artemis/communication/notification/NotificationResourceIntegrationTest.java index 07c00a1b9a3a..d01fd96749fd 100644 --- a/src/test/java/de/tum/cit/aet/artemis/communication/notification/NotificationResourceIntegrationTest.java +++ b/src/test/java/de/tum/cit/aet/artemis/communication/notification/NotificationResourceIntegrationTest.java @@ -20,17 +20,17 @@ import de.tum.cit.aet.artemis.communication.domain.notification.NotificationConstants; import de.tum.cit.aet.artemis.communication.domain.notification.SingleUserNotification; import de.tum.cit.aet.artemis.communication.notification.util.NotificationFactory; -import de.tum.cit.aet.artemis.communication.repository.NotificationRepository; import de.tum.cit.aet.artemis.communication.repository.NotificationSettingRepository; import de.tum.cit.aet.artemis.core.domain.Course; import de.tum.cit.aet.artemis.core.domain.User; +import de.tum.cit.aet.artemis.core.test_repository.NotificationTestRepository; import de.tum.cit.aet.artemis.shared.base.AbstractSpringIntegrationIndependentTest; import de.tum.cit.aet.artemis.text.util.TextExerciseUtilService; class NotificationResourceIntegrationTest extends AbstractSpringIntegrationIndependentTest { @Autowired - private NotificationRepository notificationRepository; + private NotificationTestRepository notificationTestRepository; @Autowired private NotificationSettingRepository notificationSettingRepository; @@ -49,7 +49,7 @@ void initTestCase() { userUtilService.addUsers(TEST_PREFIX, 2, 1, 1, 1); course1 = textExerciseUtilService.addCourseWithOneReleasedTextExercise(); course2 = textExerciseUtilService.addCourseWithOneReleasedTextExercise(); - notificationRepository.deleteAll(); + notificationTestRepository.deleteAll(); User student1 = userUtilService.getUserByLogin(TEST_PREFIX + "student1"); student1.setLastNotificationRead(ZonedDateTime.now().minusDays(1)); @@ -58,7 +58,7 @@ void initTestCase() { @AfterEach void tearDown() { - notificationRepository.deleteAll(); + notificationTestRepository.deleteAll(); } @Test @@ -66,9 +66,9 @@ void tearDown() { void testGetNotifications_recipientEvaluation() throws Exception { User recipient = userTestRepository.getUser(); SingleUserNotification notification1 = NotificationFactory.generateSingleUserNotification(ZonedDateTime.now(), recipient); - notificationRepository.save(notification1); + notificationTestRepository.save(notification1); SingleUserNotification notification2 = NotificationFactory.generateSingleUserNotification(ZonedDateTime.now(), userUtilService.getUserByLogin(TEST_PREFIX + "student2")); - notificationRepository.save(notification2); + notificationTestRepository.save(notification2); List notifications = request.getList("/api/notifications", HttpStatus.OK, Notification.class); assertThat(notifications).as("Notification with recipient equal to current user is returned").contains(notification1); @@ -81,11 +81,11 @@ void testGetNotifications_courseEvaluation() throws Exception { // student1 is member of `testgroup` and `tumuser` per default // the studentGroupName of course1 is `tumuser` per default GroupNotification notification1 = NotificationFactory.generateGroupNotification(ZonedDateTime.now(), course1, GroupNotificationType.STUDENT); - notificationRepository.save(notification1); + notificationTestRepository.save(notification1); course2.setStudentGroupName("some-group"); courseRepository.save(course2); GroupNotification notification2 = NotificationFactory.generateGroupNotification(ZonedDateTime.now(), course2, GroupNotificationType.STUDENT); - notificationRepository.save(notification2); + notificationTestRepository.save(notification2); List notifications = request.getList("/api/notifications", HttpStatus.OK, Notification.class); assertThat(notifications).as("Notification with course the current user belongs to is returned").contains(notification1); @@ -96,13 +96,13 @@ void testGetNotifications_courseEvaluation() throws Exception { @WithMockUser(username = TEST_PREFIX + "student1", roles = "USER") void testGetNotifications_groupNotificationTypeEvaluation_asStudent() throws Exception { GroupNotification notificationStudent = NotificationFactory.generateGroupNotification(ZonedDateTime.now(), course1, GroupNotificationType.STUDENT); - notificationRepository.save(notificationStudent); + notificationTestRepository.save(notificationStudent); GroupNotification notificationTutor = NotificationFactory.generateGroupNotification(ZonedDateTime.now(), course1, GroupNotificationType.TA); - notificationRepository.save(notificationTutor); + notificationTestRepository.save(notificationTutor); GroupNotification notificationEditor = NotificationFactory.generateGroupNotification(ZonedDateTime.now(), course1, GroupNotificationType.EDITOR); - notificationRepository.save(notificationEditor); + notificationTestRepository.save(notificationEditor); GroupNotification notificationInstructor = NotificationFactory.generateGroupNotification(ZonedDateTime.now(), course1, GroupNotificationType.INSTRUCTOR); - notificationRepository.save(notificationInstructor); + notificationTestRepository.save(notificationInstructor); List notifications = request.getList("/api/notifications", HttpStatus.OK, Notification.class); assertThat(notifications).as("Notification with type student is returned").contains(notificationStudent); @@ -115,13 +115,13 @@ void testGetNotifications_groupNotificationTypeEvaluation_asStudent() throws Exc @WithMockUser(username = TEST_PREFIX + "tutor1", roles = "TA") void testGetNotifications_groupNotificationTypeEvaluation_asTutor() throws Exception { GroupNotification notificationStudent = NotificationFactory.generateGroupNotification(ZonedDateTime.now(), course1, GroupNotificationType.STUDENT); - notificationRepository.save(notificationStudent); + notificationTestRepository.save(notificationStudent); GroupNotification notificationTutor = NotificationFactory.generateGroupNotification(ZonedDateTime.now(), course1, GroupNotificationType.TA); - notificationRepository.save(notificationTutor); + notificationTestRepository.save(notificationTutor); GroupNotification notificationEditor = NotificationFactory.generateGroupNotification(ZonedDateTime.now(), course1, GroupNotificationType.EDITOR); - notificationRepository.save(notificationEditor); + notificationTestRepository.save(notificationEditor); GroupNotification notificationInstructor = NotificationFactory.generateGroupNotification(ZonedDateTime.now(), course1, GroupNotificationType.INSTRUCTOR); - notificationRepository.save(notificationInstructor); + notificationTestRepository.save(notificationInstructor); List notifications = request.getList("/api/notifications", HttpStatus.OK, Notification.class); assertThat(notifications).as("Notification with type student is not returned").doesNotContain(notificationStudent); @@ -134,13 +134,13 @@ void testGetNotifications_groupNotificationTypeEvaluation_asTutor() throws Excep @WithMockUser(username = TEST_PREFIX + "editor1", roles = "EDITOR") void testGetNotifications_groupNotificationTypeEvaluation_asEditor() throws Exception { GroupNotification notificationStudent = NotificationFactory.generateGroupNotification(ZonedDateTime.now(), course1, GroupNotificationType.STUDENT); - notificationRepository.save(notificationStudent); + notificationTestRepository.save(notificationStudent); GroupNotification notificationTutor = NotificationFactory.generateGroupNotification(ZonedDateTime.now(), course1, GroupNotificationType.TA); - notificationRepository.save(notificationTutor); + notificationTestRepository.save(notificationTutor); GroupNotification notificationEditor = NotificationFactory.generateGroupNotification(ZonedDateTime.now(), course1, GroupNotificationType.EDITOR); - notificationRepository.save(notificationEditor); + notificationTestRepository.save(notificationEditor); GroupNotification notificationInstructor = NotificationFactory.generateGroupNotification(ZonedDateTime.now(), course1, GroupNotificationType.INSTRUCTOR); - notificationRepository.save(notificationInstructor); + notificationTestRepository.save(notificationInstructor); List notifications = request.getList("/api/notifications", HttpStatus.OK, Notification.class); assertThat(notifications).as("Notification with type student is not returned").doesNotContain(notificationStudent); @@ -153,13 +153,13 @@ void testGetNotifications_groupNotificationTypeEvaluation_asEditor() throws Exce @WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR") void testGetNotifications_groupNotificationTypeEvaluation_asInstructor() throws Exception { GroupNotification notificationStudent = NotificationFactory.generateGroupNotification(ZonedDateTime.now(), course1, GroupNotificationType.STUDENT); - notificationRepository.save(notificationStudent); + notificationTestRepository.save(notificationStudent); GroupNotification notificationTutor = NotificationFactory.generateGroupNotification(ZonedDateTime.now(), course1, GroupNotificationType.TA); - notificationRepository.save(notificationTutor); + notificationTestRepository.save(notificationTutor); GroupNotification notificationEditor = NotificationFactory.generateGroupNotification(ZonedDateTime.now(), course1, GroupNotificationType.EDITOR); - notificationRepository.save(notificationEditor); + notificationTestRepository.save(notificationEditor); GroupNotification notificationInstructor = NotificationFactory.generateGroupNotification(ZonedDateTime.now(), course1, GroupNotificationType.INSTRUCTOR); - notificationRepository.save(notificationInstructor); + notificationTestRepository.save(notificationInstructor); List notifications = request.getList("/api/notifications", HttpStatus.OK, Notification.class); assertThat(notifications).as("Notification with type student is not returned").doesNotContain(notificationStudent); @@ -184,11 +184,11 @@ void testGetAllNotificationsForCurrentUserFilteredBySettings() throws Exception GroupNotification allowedNotification = NotificationFactory.generateGroupNotification(ZonedDateTime.now(), course1, GroupNotificationType.STUDENT); allowedNotification.setTitle(NotificationConstants.findCorrespondingNotificationTitle(allowedType)); - notificationRepository.save(allowedNotification); + notificationTestRepository.save(allowedNotification); GroupNotification blockedNotification = NotificationFactory.generateGroupNotification(ZonedDateTime.now(), course1, GroupNotificationType.STUDENT); blockedNotification.setTitle(NotificationConstants.findCorrespondingNotificationTitle(blockedType)); - notificationRepository.save(blockedNotification); + notificationTestRepository.save(blockedNotification); List notifications = request.getList("/api/notifications", HttpStatus.OK, Notification.class); @@ -207,10 +207,10 @@ void testGetAllNotificationsForCurrentUser_hideUntilDeactivated() throws Excepti userTestRepository.save(student1); GroupNotification futureNotification = NotificationFactory.generateGroupNotification(timeNow.plusHours(1), course1, GroupNotificationType.STUDENT); - notificationRepository.save(futureNotification); + notificationTestRepository.save(futureNotification); GroupNotification pastNotification = NotificationFactory.generateGroupNotification(timeNow.minusHours(1), course1, GroupNotificationType.STUDENT); - notificationRepository.save(pastNotification); + notificationTestRepository.save(pastNotification); List notifications = request.getList("/api/notifications", HttpStatus.OK, Notification.class); @@ -228,10 +228,10 @@ void testGetAllNotificationsForCurrentUser_hideUntilActivated() throws Exception userTestRepository.save(student1); GroupNotification futureNotification = NotificationFactory.generateGroupNotification(timeNow.plusHours(1), course1, GroupNotificationType.STUDENT); - notificationRepository.save(futureNotification); + notificationTestRepository.save(futureNotification); GroupNotification pastNotification = NotificationFactory.generateGroupNotification(timeNow.minusHours(1), course1, GroupNotificationType.STUDENT); - notificationRepository.save(pastNotification); + notificationTestRepository.save(pastNotification); List notifications = request.getList("/api/notifications", HttpStatus.OK, Notification.class); diff --git a/src/test/java/de/tum/cit/aet/artemis/communication/notification/NotificationScheduleServiceTest.java b/src/test/java/de/tum/cit/aet/artemis/communication/notification/NotificationScheduleServiceTest.java index c49163742ba4..a7506c413281 100644 --- a/src/test/java/de/tum/cit/aet/artemis/communication/notification/NotificationScheduleServiceTest.java +++ b/src/test/java/de/tum/cit/aet/artemis/communication/notification/NotificationScheduleServiceTest.java @@ -22,11 +22,11 @@ import de.tum.cit.aet.artemis.assessment.domain.Result; import de.tum.cit.aet.artemis.assessment.test_repository.ResultTestRepository; import de.tum.cit.aet.artemis.communication.domain.NotificationSetting; -import de.tum.cit.aet.artemis.communication.repository.NotificationRepository; import de.tum.cit.aet.artemis.communication.repository.NotificationSettingRepository; import de.tum.cit.aet.artemis.core.domain.Course; import de.tum.cit.aet.artemis.core.domain.User; import de.tum.cit.aet.artemis.core.service.messaging.InstanceMessageReceiveService; +import de.tum.cit.aet.artemis.core.test_repository.NotificationTestRepository; import de.tum.cit.aet.artemis.core.user.util.UserUtilService; import de.tum.cit.aet.artemis.core.util.CourseUtilService; import de.tum.cit.aet.artemis.exercise.domain.Exercise; @@ -43,7 +43,7 @@ class NotificationScheduleServiceTest extends AbstractSpringIntegrationLocalCILo private InstanceMessageReceiveService instanceMessageReceiveService; @Autowired - private NotificationRepository notificationRepository; + private NotificationTestRepository notificationTestRepository; @Autowired private NotificationSettingRepository notificationSettingRepository; @@ -80,7 +80,7 @@ void init() { exercise.setMaxPoints(5.0); exerciseRepository.saveAndFlush(exercise); - sizeBefore = notificationRepository.count(); + sizeBefore = notificationTestRepository.count(); } @Test @@ -91,7 +91,7 @@ void shouldCreateNotificationAndEmailAtReleaseDate() { exerciseRepository.saveAndFlush(exercise); instanceMessageReceiveService.processScheduleExerciseReleasedNotification(exercise.getId()); - await().until(() -> notificationRepository.count() > sizeBefore); + await().until(() -> notificationTestRepository.count() > sizeBefore); verify(groupNotificationService, timeout(TIMEOUT_MS)).notifyAllGroupsAboutReleasedExercise(exercise); verify(mailService, timeout(TIMEOUT_MS).atLeastOnce()).sendNotification(any(), anySet(), any()); } @@ -114,7 +114,7 @@ void shouldCreateNotificationAndEmailAtAssessmentDueDate() { exerciseRepository.saveAndFlush(exercise); instanceMessageReceiveService.processScheduleAssessedExerciseSubmittedNotification(exercise.getId()); - await().until(() -> notificationRepository.count() > sizeBefore); + await().until(() -> notificationTestRepository.count() > sizeBefore); verify(singleUserNotificationService, timeout(TIMEOUT_MS)).notifyUsersAboutAssessedExerciseSubmission(exercise); verify(javaMailSender, timeout(TIMEOUT_MS)).send(any(MimeMessage.class)); } diff --git a/src/test/java/de/tum/cit/aet/artemis/communication/notifications/service/ConversationNotificationServiceTest.java b/src/test/java/de/tum/cit/aet/artemis/communication/notifications/service/ConversationNotificationServiceTest.java index 11911a26af6e..cf646f594ef0 100644 --- a/src/test/java/de/tum/cit/aet/artemis/communication/notifications/service/ConversationNotificationServiceTest.java +++ b/src/test/java/de/tum/cit/aet/artemis/communication/notifications/service/ConversationNotificationServiceTest.java @@ -20,7 +20,6 @@ import de.tum.cit.aet.artemis.communication.domain.notification.ConversationNotification; import de.tum.cit.aet.artemis.communication.domain.notification.Notification; import de.tum.cit.aet.artemis.communication.repository.ConversationMessageRepository; -import de.tum.cit.aet.artemis.communication.repository.NotificationRepository; import de.tum.cit.aet.artemis.communication.repository.conversation.ConversationNotificationRepository; import de.tum.cit.aet.artemis.communication.service.notifications.ConversationNotificationService; import de.tum.cit.aet.artemis.communication.test_repository.ConversationParticipantTestRepository; @@ -28,6 +27,7 @@ import de.tum.cit.aet.artemis.core.domain.Course; import de.tum.cit.aet.artemis.core.domain.DomainObject; import de.tum.cit.aet.artemis.core.domain.User; +import de.tum.cit.aet.artemis.core.test_repository.NotificationTestRepository; import de.tum.cit.aet.artemis.core.test_repository.UserTestRepository; import de.tum.cit.aet.artemis.core.user.util.UserUtilService; import de.tum.cit.aet.artemis.core.util.CourseUtilService; @@ -62,7 +62,7 @@ class ConversationNotificationServiceTest extends AbstractSpringIntegrationIndep private CourseUtilService courseUtilService; @Autowired - private NotificationRepository notificationRepository; + private NotificationTestRepository notificationTestRepository; private OneToOneChat oneToOneChat; @@ -115,7 +115,7 @@ void createNotificationForNewMessageInConversation() { conversationNotificationService.notifyAboutNewMessage(post, notification, Set.of(user2)); verifyRepositoryCallWithCorrectNotification(NEW_MESSAGE_TITLE); - Notification sentNotification = notificationRepository.findAll().stream().max(Comparator.comparing(DomainObject::getId)).orElseThrow(); + Notification sentNotification = notificationTestRepository.findAll().stream().max(Comparator.comparing(DomainObject::getId)).orElseThrow(); verify(generalInstantNotificationService).sendNotification(sentNotification, Set.of(user2), post); diff --git a/src/test/java/de/tum/cit/aet/artemis/core/test_repository/NotificationTestRepository.java b/src/test/java/de/tum/cit/aet/artemis/core/test_repository/NotificationTestRepository.java index 89354e5fae86..5ac06e0cc6c9 100644 --- a/src/test/java/de/tum/cit/aet/artemis/core/test_repository/NotificationTestRepository.java +++ b/src/test/java/de/tum/cit/aet/artemis/core/test_repository/NotificationTestRepository.java @@ -2,11 +2,15 @@ import java.util.List; +import org.springframework.context.annotation.Primary; import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Repository; import de.tum.cit.aet.artemis.communication.domain.notification.Notification; import de.tum.cit.aet.artemis.communication.repository.NotificationRepository; +@Repository +@Primary public interface NotificationTestRepository extends NotificationRepository { @Query(""" diff --git a/src/test/java/de/tum/cit/aet/artemis/core/user/util/UserTestService.java b/src/test/java/de/tum/cit/aet/artemis/core/user/util/UserTestService.java index 1702f990a1d7..67ce0cc894a8 100644 --- a/src/test/java/de/tum/cit/aet/artemis/core/user/util/UserTestService.java +++ b/src/test/java/de/tum/cit/aet/artemis/core/user/util/UserTestService.java @@ -160,7 +160,9 @@ public void setup(String testPrefix, MockDelegate mockDelegate) throws Exception } public void tearDown() throws IOException { - notificationTestRepository.deleteAllInBatch(notificationTestRepository.findAllByRecipientId(student.getId())); + if (student.getId() != null) { + notificationTestRepository.deleteAllInBatch(notificationTestRepository.findAllByRecipientId(student.getId())); + } userTestRepository.deleteAll(userTestRepository.searchAllByLoginOrName(Pageable.unpaged(), TEST_PREFIX)); }