Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quiz exercises: Fix LTI Moodle quiz participation results reflection #7129

Merged
merged 53 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
46fa274
fix public endpoints
basak-akan Jul 30, 2023
af9d73b
fix tests
basak-akan Jul 30, 2023
1cf38f1
Merge branch 'develop' into bugfix/fix-lti-programming-exercise
basak-akan Aug 5, 2023
d518fdd
add lti new result service for programming submission resource
basak-akan Aug 8, 2023
06c488d
Merge branch 'develop' into bugfix/fix-lti-programming-exercise
basak-akan Aug 8, 2023
f7f3fb4
Merge branch 'develop' into bugfix/fix-lti-programming-exercise
basak-akan Aug 22, 2023
ebf7d7b
fix new user creation
basak-akan Aug 22, 2023
1e00dff
fix new user creation for optinal vcs and ci, fix server tests
basak-akan Aug 23, 2023
cef319c
fix teamscale-findings
basak-akan Aug 24, 2023
e3f0c1e
fix LtiIntegrationTest
basak-akan Aug 26, 2023
4931096
Merge branch 'develop' into bugfix/fix-lti-programming-exercise
basak-akan Aug 26, 2023
012a138
adapt lti integration tests since LtiUserId already removed
basak-akan Aug 27, 2023
86f12b1
fix StudentParticipation for onNewResult
basak-akan Aug 28, 2023
b80c77f
fix teamscale-findings
basak-akan Aug 28, 2023
fef35d5
Merge branch 'develop' into bugfix/fix-lti-programming-exercise
basak-akan Aug 28, 2023
5548ed7
Merge branch 'develop' into bugfix/fix-lti-programming-exercise
basak-akan Aug 30, 2023
56d6cde
Merge branch 'bugfix/fix-lti-programming-exercise' of https://github.…
basak-akan Aug 30, 2023
21b6b8e
Merge branch 'develop' into bugfix/fix-lti-programming-exercise
basak-akan Aug 30, 2023
f5c2c87
Merge branch 'develop' into bugfix/fix-lti-programming-exercise
basak-akan Aug 31, 2023
125a283
add ltiNewResultService for quiz schedule service and statistic service
basak-akan Aug 31, 2023
7797128
Merge branch 'bugfix/fix-lti-programming-exercise' into bugfix/fix-lt…
basak-akan Aug 31, 2023
5643094
Merge branch 'develop' into bugfix/fix-lti-programming-exercise
basak-akan Sep 4, 2023
42b7038
Merge branch 'bugfix/fix-lti-programming-exercise' into bugfix/fix-lt…
basak-akan Sep 4, 2023
e53ced1
add tests
basak-akan Sep 6, 2023
6566526
Merge branch 'develop' into bugfix/fix-lti-programming-exercise
basak-akan Sep 6, 2023
218100f
Merge branch 'bugfix/fix-lti-programming-exercise' into bugfix/fix-lt…
basak-akan Sep 6, 2023
8acb255
remove public from test class name
basak-akan Sep 6, 2023
ff78f36
fix teamscale-findings
basak-akan Sep 6, 2023
42fd97b
fix teamscale-findings
basak-akan Sep 7, 2023
e97c3c3
merge develop
basak-akan Sep 9, 2023
6f3a313
Merge branch 'bugfix/fix-lti-programming-exercise' into bugfix/fix-lt…
basak-akan Sep 9, 2023
d538147
Merge branch 'develop' into bugfix/fix-lti-programming-exercise
basak-akan Sep 17, 2023
4cfa868
Merge branch 'bugfix/fix-lti-programming-exercise' into bugfix/fix-lt…
basak-akan Sep 17, 2023
bb39301
Merge branch 'develop' into bugfix/fix-lti-programming-exercise
basak-akan Sep 18, 2023
59cf689
Merge branch 'bugfix/fix-lti-programming-exercise' into bugfix/fix-lt…
basak-akan Sep 18, 2023
ad3031f
Merge branch 'develop' into bugfix/fix-lti-programming-exercise
basak-akan Sep 18, 2023
6a62017
Merge branch 'bugfix/fix-lti-programming-exercise' into bugfix/fix-lt…
basak-akan Sep 18, 2023
2055105
Merge branch 'develop' into bugfix/fix-lti-quiz-exercise-grade
basak-akan Sep 24, 2023
a0753ed
remove comment
basak-akan Sep 24, 2023
26588e9
revert unnecessary changes from teamscalefindings
basak-akan Sep 24, 2023
bfd2079
remove remaining unused imports
basak-akan Sep 24, 2023
d19378d
try fix server tests
basak-akan Sep 25, 2023
f29e3e4
Merge branch 'develop' into bugfix/fix-lti-quiz-exercise-grade
basak-akan Sep 30, 2023
d989d07
adapt lti profile changes
basak-akan Sep 30, 2023
3404441
Merge branch 'develop' into bugfix/fix-lti-quiz-exercise-grade
basak-akan Oct 12, 2023
5534dd0
Merge branch 'develop' into bugfix/fix-lti-quiz-exercise-grade
basak-akan Oct 17, 2023
da4d70e
Merge branch 'develop' into bugfix/fix-lti-quiz-exercise-grade
basak-akan Oct 25, 2023
c55b2b6
Merge branch 'develop' into bugfix/fix-lti-quiz-exercise-grade
basak-akan Oct 29, 2023
80a513f
Merge branch 'develop' into bugfix/fix-lti-quiz-exercise-grade
basak-akan Nov 1, 2023
978feaf
fix test
basak-akan Nov 2, 2023
85704ee
Merge branch 'develop' into bugfix/fix-lti-quiz-exercise-grade
basak-akan Nov 7, 2023
146e465
Merge branch 'develop' into bugfix/fix-lti-quiz-exercise-grade
basak-akan Nov 8, 2023
8706913
Merge branch 'develop' into bugfix/fix-lti-quiz-exercise-grade
basak-akan Nov 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import org.slf4j.Logger;
Expand All @@ -10,8 +11,10 @@

import de.tum.in.www1.artemis.domain.Result;
import de.tum.in.www1.artemis.domain.participation.Participation;
import de.tum.in.www1.artemis.domain.participation.StudentParticipation;
import de.tum.in.www1.artemis.domain.quiz.*;
import de.tum.in.www1.artemis.repository.*;
import de.tum.in.www1.artemis.service.connectors.lti.LtiNewResultService;

@Service
public class QuizStatisticService {
Expand All @@ -30,15 +33,18 @@

private final WebsocketMessagingService websocketMessagingService;

private final Optional<LtiNewResultService> ltiNewResultService;

public QuizStatisticService(StudentParticipationRepository studentParticipationRepository, ResultRepository resultRepository,
WebsocketMessagingService websocketMessagingService, QuizPointStatisticRepository quizPointStatisticRepository,
QuizQuestionStatisticRepository quizQuestionStatisticRepository, QuizSubmissionRepository quizSubmissionRepository) {
QuizQuestionStatisticRepository quizQuestionStatisticRepository, QuizSubmissionRepository quizSubmissionRepository, Optional<LtiNewResultService> ltiNewResultService) {

Check warning on line 40 in src/main/java/de/tum/in/www1/artemis/service/QuizStatisticService.java

View check run for this annotation

Teamscale / teamscale-findings

src/main/java/de/tum/in/www1/artemis/service/QuizStatisticService.java#L38-L40

Interface comment missing https://teamscale.io/findings.html#details/GitHub-ls1intum-Artemis?t=bugfix%2Ffix-lti-quiz-exercise-grade%3AHEAD&id=5A4A3708E64AD45920EF71B22D6D68B3
this.studentParticipationRepository = studentParticipationRepository;
this.resultRepository = resultRepository;
this.quizPointStatisticRepository = quizPointStatisticRepository;
this.quizQuestionStatisticRepository = quizQuestionStatisticRepository;
this.websocketMessagingService = websocketMessagingService;
this.quizSubmissionRepository = quizSubmissionRepository;
this.ltiNewResultService = ltiNewResultService;
}

/**
Expand Down Expand Up @@ -93,6 +99,10 @@
var latestUnratedSubmission = quizSubmissionRepository.findWithEagerSubmittedAnswersById(latestUnratedResult.getSubmission().getId());
quizExercise.addResultToAllStatistics(latestUnratedResult, latestUnratedSubmission);
}

if (ltiNewResultService.isPresent()) {
ltiNewResultService.get().onNewResult((StudentParticipation) participation);
}
}

// save changed Statistics
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package de.tum.in.www1.artemis.service.scheduled.cache.quiz;

Check warning on line 1 in src/main/java/de/tum/in/www1/artemis/service/scheduled/cache/quiz/QuizScheduleService.java

View check run for this annotation

Teamscale / teamscale-findings

src/main/java/de/tum/in/www1/artemis/service/scheduled/cache/quiz/QuizScheduleService.java#L1

Violation of file size threshold (source lines of code) of 300: 444 https://teamscale.io/findings.html#details/GitHub-ls1intum-Artemis?t=bugfix%2Ffix-lti-quiz-exercise-grade%3AHEAD&id=D8F19B815F28344E647EAEB1B99A4FFD

import static de.tum.in.www1.artemis.service.util.TimeLogUtil.formatDurationFrom;

Expand Down Expand Up @@ -43,6 +43,7 @@
import de.tum.in.www1.artemis.service.QuizMessagingService;
import de.tum.in.www1.artemis.service.QuizStatisticService;
import de.tum.in.www1.artemis.service.WebsocketMessagingService;
import de.tum.in.www1.artemis.service.connectors.lti.LtiNewResultService;
import de.tum.in.www1.artemis.service.scheduled.cache.Cache;

@Service
Expand Down Expand Up @@ -72,9 +73,11 @@

private final QuizExerciseRepository quizExerciseRepository;

private final Optional<LtiNewResultService> ltiNewResultService;

public QuizScheduleService(WebsocketMessagingService websocketMessagingService, StudentParticipationRepository studentParticipationRepository, UserRepository userRepository,

Check warning on line 78 in src/main/java/de/tum/in/www1/artemis/service/scheduled/cache/quiz/QuizScheduleService.java

View check run for this annotation

Teamscale / teamscale-findings

src/main/java/de/tum/in/www1/artemis/service/scheduled/cache/quiz/QuizScheduleService.java#L78

Method `QuizScheduleService` has 9 parameters but no more than 7 parameters are allowed https://teamscale.io/findings.html#details/GitHub-ls1intum-Artemis?t=bugfix%2Ffix-lti-quiz-exercise-grade%3AHEAD&id=F438363628CB2CD4C681E7198875E8CD
QuizSubmissionRepository quizSubmissionRepository, HazelcastInstance hazelcastInstance, QuizExerciseRepository quizExerciseRepository,
QuizMessagingService quizMessagingService, QuizStatisticService quizStatisticService) {
QuizMessagingService quizMessagingService, QuizStatisticService quizStatisticService, Optional<LtiNewResultService> ltiNewResultService) {

Check warning on line 80 in src/main/java/de/tum/in/www1/artemis/service/scheduled/cache/quiz/QuizScheduleService.java

View check run for this annotation

Teamscale / teamscale-findings

src/main/java/de/tum/in/www1/artemis/service/scheduled/cache/quiz/QuizScheduleService.java#L78-L80

In this file 5 interface comments are missing. Consider adding explanatory comments or restricting the visibility. View in Teamscale: Line 50: https://teamscale.io/findings.html#details/GitHub-ls1intum-Artemis?t=bugfix%2Ffix-lti-quiz-exercise-grade%3AHEAD&id=ABE75AC01B38BC85019188CF9247A2CC Line 78: https://teamscale.io/findings.html#details/GitHub-ls1intum-Artemis?t=bugfix%2Ffix-lti-quiz-exercise-grade%3AHEAD&id=B1C5B6FBB23E2E2FFE94017E37520EC4 Line 106: https://teamscale.io/findings.html#details/GitHub-ls1intum-Artemis?t=bugfix%2Ffix-lti-quiz-exercise-grade%3AHEAD&id=2EFC7DBFB235614A0CA7FCFF8832CA18 Line 537: https://teamscale.io/findings.html#details/GitHub-ls1intum-Artemis?t=bugfix%2Ffix-lti-quiz-exercise-grade%3AHEAD&id=948A2DCC2814D7C17D6AFFE60964C80D Line 542: https://teamscale.io/findings.html#details/GitHub-ls1intum-Artemis?t=bugfix%2Ffix-lti-quiz-exercise-grade%3AHEAD&id=DD17A01ABF97372DD6708595499CB49E
this.websocketMessagingService = websocketMessagingService;
this.studentParticipationRepository = studentParticipationRepository;
this.userRepository = userRepository;
Expand All @@ -85,6 +88,7 @@
this.scheduledProcessQuizSubmissions = hazelcastInstance.getCPSubsystem().getAtomicReference(HAZELCAST_PROCESS_CACHE_HANDLER);
this.threadPoolTaskScheduler = hazelcastInstance.getScheduledExecutorService(Constants.HAZELCAST_QUIZ_SCHEDULER);
this.quizCache = new QuizCache(hazelcastInstance);
this.ltiNewResultService = ltiNewResultService;
}

/**
Expand Down Expand Up @@ -491,8 +495,11 @@
log.error("Participation is missing student (or student is missing username): {}", participation);
}
else {
sendQuizResultToUser(quizExerciseId, participation);
cachedQuiz.getParticipations().remove(entry.getKey());
if(ltiNewResultService.isPresent()) {
ltiNewResultService.get().onNewResult(participation);

Check failure on line 499 in src/main/java/de/tum/in/www1/artemis/service/scheduled/cache/quiz/QuizScheduleService.java

View check run for this annotation

Teamscale / teamscale-findings

src/main/java/de/tum/in/www1/artemis/service/scheduled/cache/quiz/QuizScheduleService.java#L499

This method is quite long [0] and deeply nested in multiple places [1, 2, 3] which can make it hard to understand and maintain. Consider extracting helper methods or reducing the nesting by using early breaks or returns. [0] https://teamscale.io/findings.html#details/GitHub-ls1intum-Artemis?t=bugfix%2Ffix-lti-quiz-exercise-grade%3AHEAD&id=9BA8E6181164F05B506F74DF838D3D36 [1] https://teamscale.io/findings.html#details/GitHub-ls1intum-Artemis?t=bugfix%2Ffix-lti-quiz-exercise-grade%3AHEAD&id=FB68DA24199511CF6508589F4EE67DAB [2] https://teamscale.io/findings.html#details/GitHub-ls1intum-Artemis?t=bugfix%2Ffix-lti-quiz-exercise-grade%3AHEAD&id=277ED87B7B040A122D609A26244504E4 [3] https://teamscale.io/findings.html#details/GitHub-ls1intum-Artemis?t=bugfix%2Ffix-lti-quiz-exercise-grade%3AHEAD&id=E7820B821E9E0D4CBE178784CC849B3C
}
sendQuizResultToUser(quizExerciseId, participation);
cachedQuiz.getParticipations().remove(entry.getKey());
}
});
if (!finishedParticipations.isEmpty()) {
Expand Down
219 changes: 219 additions & 0 deletions src/test/java/de/tum/in/www1/artemis/LtiQuizIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
package de.tum.in.www1.artemis;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.time.ZonedDateTime;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Isolated;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import com.fasterxml.jackson.databind.ObjectMapper;

import de.tum.in.www1.artemis.course.CourseUtilService;
import de.tum.in.www1.artemis.domain.Course;
import de.tum.in.www1.artemis.domain.enumeration.AssessmentType;
import de.tum.in.www1.artemis.domain.enumeration.QuizMode;
import de.tum.in.www1.artemis.domain.quiz.*;

Check warning on line 35 in src/test/java/de/tum/in/www1/artemis/LtiQuizIntegrationTest.java

View check run for this annotation

Teamscale / teamscale-findings

src/test/java/de/tum/in/www1/artemis/LtiQuizIntegrationTest.java#L35

Star import of `de.tum.in.www1.artemis.domain.quiz.*` should not be used https://teamscale.io/findings.html#details/GitHub-ls1intum-Artemis?t=bugfix%2Ffix-lti-quiz-exercise-grade%3AHEAD&id=3DD759F7F60A532C2FB436C4E8CF42FE
import de.tum.in.www1.artemis.exercise.quizexercise.QuizExerciseFactory;
import de.tum.in.www1.artemis.participation.ParticipationUtilService;
import de.tum.in.www1.artemis.repository.CourseRepository;
import de.tum.in.www1.artemis.repository.ExerciseRepository;
import de.tum.in.www1.artemis.repository.QuizExerciseRepository;
import de.tum.in.www1.artemis.repository.SubmissionRepository;
import de.tum.in.www1.artemis.service.QuizExerciseService;
import de.tum.in.www1.artemis.user.UserUtilService;
import de.tum.in.www1.artemis.web.websocket.QuizSubmissionWebsocketService;

@Isolated
class LtiQuizIntegrationTest extends AbstractSpringIntegrationBambooBitbucketJiraTest {

private static final String TEST_PREFIX = "ltiquizsubmissiontest";

@Autowired
private CourseRepository courseRepository;

@Autowired
private ExerciseRepository exerciseRepository;

@Autowired
private QuizExerciseService quizExerciseService;

@Autowired
private QuizExerciseRepository quizExerciseRepository;

@Autowired
private QuizSubmissionWebsocketService quizSubmissionWebsocketService;

@Autowired
private SubmissionRepository submissionRepository;

@Autowired
private CourseUtilService courseUtilService;

@Autowired
private UserUtilService userUtilService;

@Autowired
private ParticipationUtilService participationUtilService;

@Autowired
private ObjectMapper objectMapper;

@BeforeEach
void init() {
// do not use the schedule service based on a time interval in the tests, because this would result in flaky tests that run much slower
quizScheduleService.stopSchedule();
arrangeLtiServiceMocks();
}

@AfterEach
protected void resetSpyBeans() {
super.resetSpyBeans();
}

@ParameterizedTest(name = "{displayName} [{index}] {argumentsWithNames}")
@ValueSource(booleans = { true, false })
@WithMockUser(username = TEST_PREFIX + "student1", roles = "USER")
void testLtiServicesAreCalledUponQuizSubmission(boolean isSubmitted) {

QuizExercise quizExercise = createSimpleQuizExercise(ZonedDateTime.now().minusMinutes(1), 240);
quizExercise = quizExerciseService.save(quizExercise);

QuizSubmission quizSubmission = new QuizSubmission();
for (var question : quizExercise.getQuizQuestions()) {
quizSubmission.addSubmittedAnswers(QuizExerciseFactory.generateSubmittedAnswerForQuizWithCorrectAndFalseAnswers(question));
}

userUtilService.addUsers(TEST_PREFIX, 1, 0, 0, 1);
quizSubmission.submitted(isSubmitted);
quizSubmissionWebsocketService.saveSubmission(quizExercise.getId(), quizSubmission, () -> TEST_PREFIX + "student1");

assertThat(submissionRepository.countByExerciseIdSubmitted(quizExercise.getId())).isZero();
quizScheduleService.processCachedQuizSubmissions();

verifyNoInteractions(lti10Service);
verifyNoInteractions(lti13Service);

// End the quiz right now
quizExercise = quizExerciseRepository.findOneWithQuestionsAndStatistics(quizExercise.getId());
assertThat(quizExercise).isNotNull();
quizExercise.setDueDate(ZonedDateTime.now());
exerciseRepository.saveAndFlush(quizExercise);

quizScheduleService.processCachedQuizSubmissions();

verify(lti10Service).onNewResult(any());
verify(lti13Service).onNewResult(any());

}

@Test
@WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
void testLtiReevaluateStatistics() throws Exception {

QuizExercise quizExercise = createQuizExercise(ZonedDateTime.now().plusHours(5));
quizExercise.setReleaseDate(ZonedDateTime.now().minusHours(5));
quizExercise.setDueDate(ZonedDateTime.now().minusHours(2));

var now = ZonedDateTime.now();

// generate submissions for each student
int numberOfParticipants = 10;
userUtilService.addStudents(TEST_PREFIX, 2, 14);

for (int i = 1; i <= numberOfParticipants; i++) {
QuizSubmission quizSubmission = QuizExerciseFactory.generateSubmissionForThreeQuestions(quizExercise, i, true, now.minusHours(3));
participationUtilService.addSubmission(quizExercise, quizSubmission, TEST_PREFIX + "student" + i);
participationUtilService.addResultToSubmission(quizSubmission, AssessmentType.AUTOMATIC, null, quizExercise.getScoreForSubmission(quizSubmission), true);
}

// calculate statistics
QuizExercise quizExerciseWithRecalculatedStatistics = request.get("/api/quiz-exercises/" + quizExercise.getId() + "/recalculate-statistics", HttpStatus.OK,
QuizExercise.class);

assertThat(quizExerciseWithRecalculatedStatistics.getQuizPointStatistic().getPointCounters()).hasSize(10);
assertThat(quizExerciseWithRecalculatedStatistics.getQuizPointStatistic().getParticipantsRated()).isEqualTo(numberOfParticipants);

Check warning on line 154 in src/test/java/de/tum/in/www1/artemis/LtiQuizIntegrationTest.java

View check run for this annotation

Teamscale / teamscale-findings

src/test/java/de/tum/in/www1/artemis/LtiQuizIntegrationTest.java#L137-L154

Clone with 2 instances of length 10 https://teamscale.io/findings.html#details/GitHub-ls1intum-Artemis?t=bugfix%2Ffix-lti-quiz-exercise-grade%3AHEAD&id=C38512AE7A0A1CE79EA8C2E795664FD8

verify(lti10Service, times(10)).onNewResult(any());
verify(lti13Service, times(10)).onNewResult(any());

}

private QuizExercise createSimpleQuizExercise(ZonedDateTime releaseDate, int duration) {
Course course = courseUtilService.createCourse();
course.setOnlineCourse(true);
courseRepository.save(course);

QuizExercise quizExercise = QuizExerciseFactory.createQuiz(course, releaseDate, null, QuizMode.SYNCHRONIZED);
quizExercise.duration(duration);
return quizExercise;
}

private void arrangeLtiServiceMocks() {
doNothing().when(lti10Service).onNewResult(any());
doNothing().when(lti13Service).onNewResult(any());
}

private QuizExercise createQuizExercise(ZonedDateTime releaseDate) throws Exception {
QuizExercise quizExercise = createSimpleQuizExercise(releaseDate, 3600);

QuizExercise quizExerciseServer = createQuizExerciseWithFiles(quizExercise, HttpStatus.CREATED);
QuizExercise quizExerciseDatabase = quizExerciseRepository.findOneWithQuestionsAndStatistics(quizExerciseServer.getId());
assertThat(quizExerciseServer).isNotNull();
assertThat(quizExerciseDatabase).isNotNull();

return quizExerciseDatabase;
}

private QuizExercise createQuizExerciseWithFiles(QuizExercise quizExercise, HttpStatus expectedStatus) throws Exception {
var builder = MockMvcRequestBuilders.multipart(HttpMethod.POST, "/api/quiz-exercises");
addFilesToBuilderAndModifyExercise(builder, quizExercise);
builder.file(new MockMultipartFile("exercise", "", MediaType.APPLICATION_JSON_VALUE, objectMapper.writeValueAsBytes(quizExercise)))
.contentType(MediaType.MULTIPART_FORM_DATA);
MvcResult result = request.getMvc().perform(builder).andExpect(status().is(expectedStatus.value())).andReturn();
request.restoreSecurityContext();
if (HttpStatus.valueOf(result.getResponse().getStatus()).is2xxSuccessful()) {
assertThat(result.getResponse().getContentAsString()).isNotBlank();
return objectMapper.readValue(result.getResponse().getContentAsString(), QuizExercise.class);
}
return null;
}

private void addFilesToBuilderAndModifyExercise(MockMultipartHttpServletRequestBuilder builder, QuizExercise quizExercise) {
int index = 0;
for (var question : quizExercise.getQuizQuestions()) {
if (question instanceof DragAndDropQuestion dragAndDropQuestion) {
String backgroundFileName = "backgroundImage" + index++ + ".jpg";
dragAndDropQuestion.setBackgroundFilePath(backgroundFileName);
builder.file(new MockMultipartFile("files", backgroundFileName, MediaType.IMAGE_JPEG_VALUE, "backgroundImage".getBytes()));

for (var dragItem : dragAndDropQuestion.getDragItems()) {
if (dragItem.getPictureFilePath() != null) {
String filename = "dragItemImage" + index++ + ".png";
dragItem.setPictureFilePath(filename);
builder.file(new MockMultipartFile("files", filename, MediaType.IMAGE_PNG_VALUE, "dragItemImage".getBytes()));

Check warning on line 213 in src/test/java/de/tum/in/www1/artemis/LtiQuizIntegrationTest.java

View check run for this annotation

Teamscale / teamscale-findings

src/test/java/de/tum/in/www1/artemis/LtiQuizIntegrationTest.java#L211-L213

This method is slightly nested [0]. Consider extracting helper methods or reducing the nesting by using early breaks or returns. [0] https://teamscale.io/findings.html#details/GitHub-ls1intum-Artemis?t=bugfix%2Ffix-lti-quiz-exercise-grade%3AHEAD&id=E312162CAF205F4BB5B051BB5F01993A
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import de.tum.in.www1.artemis.service.*;
import de.tum.in.www1.artemis.service.connectors.GitService;
import de.tum.in.www1.artemis.service.connectors.lti.Lti10Service;
import de.tum.in.www1.artemis.service.connectors.lti.Lti13Service;
import de.tum.in.www1.artemis.service.exam.ExamAccessService;
import de.tum.in.www1.artemis.service.messaging.InstanceMessageSendService;
import de.tum.in.www1.artemis.service.notifications.*;
Expand Down Expand Up @@ -52,6 +53,9 @@ public abstract class AbstractArtemisIntegrationTest implements MockDelegate {
@SpyBean
protected Lti10Service lti10Service;

@SpyBean
protected Lti13Service lti13Service;

@SpyBean
protected GitService gitService;

Expand Down
Loading