diff --git a/src/test/java/de/tum/in/www1/artemis/science/ScienceUtilService.java b/src/test/java/de/tum/in/www1/artemis/science/ScienceUtilService.java index bcbdb17eafb0..d7cbc1010b9e 100644 --- a/src/test/java/de/tum/in/www1/artemis/science/ScienceUtilService.java +++ b/src/test/java/de/tum/in/www1/artemis/science/ScienceUtilService.java @@ -23,11 +23,12 @@ public class ScienceUtilService { * @param identity The login of the user associated with the event. * @param type The type of the event. * @param resourceId The id of the resource associated with the event. + * @param timestamp The timestamp of the event. */ - public ScienceEvent createScienceEvent(String identity, ScienceEventType type, Long resourceId) { + public ScienceEvent createScienceEvent(String identity, ScienceEventType type, Long resourceId, ZonedDateTime timestamp) { ScienceEvent event = new ScienceEvent(); event.setIdentity(identity); - event.setTimestamp(ZonedDateTime.now()); + event.setTimestamp(timestamp); event.setType(type); event.setResourceId(resourceId); return scienceEventRepository.save(event); @@ -39,14 +40,7 @@ public ScienceEvent createScienceEvent(String identity, ScienceEventType type, L */ public static Comparator scienceEventComparator = Comparator.comparing(ScienceEvent::getResourceId).thenComparing(ScienceEvent::getType) .thenComparing((ScienceEvent e1, ScienceEvent e2) -> { - - Duration d = Duration.between(e1.getTimestamp(), e2.getTimestamp()); - if (d.toNanos() > 500) { - return 1; - } - else if (d.toNanos() < -500) { - return -1; - } - return 0; - }); + Duration duration = Duration.between(e1.getTimestamp(), e2.getTimestamp()); + return Math.abs(duration.toNanos()) < 1e5 ? 0 : duration.isNegative() ? -1 : 1; + }).thenComparing(ScienceEvent::getIdentity); } diff --git a/src/test/java/de/tum/in/www1/artemis/service/DataExportCreationServiceTest.java b/src/test/java/de/tum/in/www1/artemis/service/DataExportCreationServiceTest.java index e21e7563d52a..98f8b97e07e3 100644 --- a/src/test/java/de/tum/in/www1/artemis/service/DataExportCreationServiceTest.java +++ b/src/test/java/de/tum/in/www1/artemis/service/DataExportCreationServiceTest.java @@ -167,7 +167,8 @@ void initTestCase() throws IOException { apollonRequestMockProvider.enableMockingOfRequests(); - // mock apollon conversion 8 times, because the last test includes 8 modeling exercises, because each test adds modeling exercises + // mock apollon conversion 8 times, because the last test includes 8 modeling + // exercises, because each test adds modeling exercises for (int i = 0; i < 8; i++) { mockApollonConversion(); } @@ -230,7 +231,6 @@ private void assertCommunicationDataCsvFile(Path courseDirPath) { /** * Asserts the content of the science events CSV file. * Allows for a 500ns difference between the timestamps due to the reimport from the csv export. - * Might cause the test to be flaky if multiple events are created overlapping and matched wrongly. * * @param extractedZipDirPath The path to the extracted zip directory * @param events The set of science events to compare with the content of the CSV file @@ -249,13 +249,13 @@ private void assertScienceEventsCSVFile(Path extractedZipDirPath, Set createScienceEvents(String userLogin) { - return Set.of(scienceUtilService.createScienceEvent(userLogin, ScienceEventType.EXERCISE__OPEN, 1L), - scienceUtilService.createScienceEvent(userLogin, ScienceEventType.LECTURE__OPEN, 2L), - scienceUtilService.createScienceEvent(userLogin, ScienceEventType.LECTURE__OPEN_UNIT, 3L)); + + ZonedDateTime timestamp = ZonedDateTime.now(); + // Rounding timestamp due to rounding during export + timestamp = timestamp.withNano(timestamp.getNano() - timestamp.getNano() % 10000); + return Set.of(scienceUtilService.createScienceEvent(userLogin, ScienceEventType.EXERCISE__OPEN, 1L, timestamp), + scienceUtilService.createScienceEvent(userLogin, ScienceEventType.LECTURE__OPEN, 2L, timestamp.plusMinutes(1)), + scienceUtilService.createScienceEvent(userLogin, ScienceEventType.LECTURE__OPEN_UNIT, 3L, timestamp.plusSeconds(30))); } @@ -635,7 +639,8 @@ void testDataExportContainsDataAboutCourseStudentUnenrolled() throws Exception { var course = prepareCourseDataForDataExportCreation(assessmentDueDateInTheFuture, courseShortName); conversationUtilService.addOneMessageForUserInCourse(TEST_PREFIX + "student1", course, "only one post"); var dataExport = initDataExport(); - // by setting the course groups to a different value we simulate unenrollment because the user is no longer part of the user group and hence, the course. + // by setting the course groups to a different value, we simulate unenrollment + // because the user is no longer part of the user group and hence, the course. courseUtilService.updateCourseGroups("abc", course, ""); dataExportCreationService.createDataExport(dataExport); var dataExportFromDb = dataExportRepository.findByIdElseThrow(dataExport.getId());