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

Development: Refactor ExamIntegrationTest #7112

Merged
merged 18 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
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
2,023 changes: 27 additions & 1,996 deletions src/test/java/de/tum/in/www1/artemis/exam/ExamIntegrationTest.java

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

281 changes: 281 additions & 0 deletions src/test/java/de/tum/in/www1/artemis/exam/ExamStartTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
package de.tum.in.www1.artemis.exam;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;

import org.eclipse.jgit.api.errors.GitAPIException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.test.context.support.WithMockUser;

import de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest;
import de.tum.in.www1.artemis.course.CourseUtilService;
import de.tum.in.www1.artemis.domain.*;
import de.tum.in.www1.artemis.domain.enumeration.DiagramType;
import de.tum.in.www1.artemis.domain.exam.*;
import de.tum.in.www1.artemis.domain.modeling.ModelingExercise;
import de.tum.in.www1.artemis.domain.modeling.ModelingSubmission;
import de.tum.in.www1.artemis.domain.participation.Participation;
import de.tum.in.www1.artemis.domain.participation.ProgrammingExerciseParticipation;
import de.tum.in.www1.artemis.domain.participation.ProgrammingExerciseStudentParticipation;
import de.tum.in.www1.artemis.exercise.modelingexercise.ModelingExerciseFactory;
import de.tum.in.www1.artemis.exercise.programmingexercise.ProgrammingExerciseFactory;
import de.tum.in.www1.artemis.exercise.programmingexercise.ProgrammingExerciseTestService;
import de.tum.in.www1.artemis.exercise.programmingexercise.ProgrammingExerciseUtilService;
import de.tum.in.www1.artemis.exercise.textexercise.TextExerciseFactory;
import de.tum.in.www1.artemis.participation.ParticipationUtilService;
import de.tum.in.www1.artemis.repository.*;
import de.tum.in.www1.artemis.service.connectors.vcs.VersionControlRepositoryPermission;
import de.tum.in.www1.artemis.service.scheduled.ParticipantScoreScheduleService;
import de.tum.in.www1.artemis.user.UserUtilService;
import de.tum.in.www1.artemis.util.ExamPrepareExercisesTestUtil;

// TODO IMPORTANT test more complex exam configurations (mixed exercise type, more variants and more registered students)
class ExamStartTest extends AbstractSpringIntegrationBambooBitbucketJiraTest {

private static final String TEST_PREFIX = "examstarttest";

@Autowired
private ExerciseRepository exerciseRepo;

@Autowired
private ExamRepository examRepository;

@Autowired
private ExerciseGroupRepository exerciseGroupRepository;

@Autowired
private StudentExamRepository studentExamRepository;

@Autowired
private ParticipationTestRepository participationTestRepository;

@Autowired
private ProgrammingExerciseTestService programmingExerciseTestService;

@Autowired
private UserUtilService userUtilService;

@Autowired
private CourseUtilService courseUtilService;

@Autowired
private ExamUtilService examUtilService;

@Autowired
private ProgrammingExerciseUtilService programmingExerciseUtilService;

@Autowired
private ParticipationUtilService participationUtilService;

private Course course1;

private Exam exam;

private static final int NUMBER_OF_STUDENTS = 2;

private Set<User> registeredUsers;

private final List<StudentExam> createdStudentExams = new ArrayList<>();

@BeforeEach
void initTestCase() throws GitAPIException {
userUtilService.addUsers(TEST_PREFIX, NUMBER_OF_STUDENTS, 0, 0, 1);

course1 = courseUtilService.addEmptyCourse();
exam = examUtilService.addExamWithExerciseGroup(course1, true);
bitbucketRequestMockProvider.enableMockingOfRequests();

ParticipantScoreScheduleService.DEFAULT_WAITING_TIME_FOR_SCHEDULED_TASKS = 200;
participantScoreScheduleService.activate();

doNothing().when(gitService).combineAllCommitsOfRepositoryIntoOne(any());

// registering users
User student1 = userUtilService.getUserByLogin(TEST_PREFIX + "student1");
User student2 = userUtilService.getUserByLogin(TEST_PREFIX + "student2");
registeredUsers = Set.of(student1, student2);
exam.setExamUsers(Set.of(new ExamUser()));
// setting dates
exam.setStartDate(ZonedDateTime.now().plusHours(2));
exam.setEndDate(ZonedDateTime.now().plusHours(3));
exam.setVisibleDate(ZonedDateTime.now().plusHours(1));
}

@AfterEach
void tearDown() throws Exception {
bitbucketRequestMockProvider.reset();
bambooRequestMockProvider.reset();
if (programmingExerciseTestService.exerciseRepo != null) {
programmingExerciseTestService.tearDown();
}

// Cleanup of Bidirectional Relationships
for (StudentExam studentExam : createdStudentExams) {
exam.removeStudentExam(studentExam);
}
examRepository.save(exam);
}

@Test
@WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
void testStartExercisesWithTextExercise() throws Exception {
// creating exercise
ExerciseGroup exerciseGroup = exam.getExerciseGroups().get(0);

TextExercise textExercise = TextExerciseFactory.generateTextExerciseForExam(exerciseGroup);
exerciseGroup.addExercise(textExercise);
exerciseGroupRepository.save(exerciseGroup);
textExercise = exerciseRepo.save(textExercise);

createStudentExams(textExercise);

List<Participation> studentParticipations = invokePrepareExerciseStart();

for (Participation participation : studentParticipations) {
assertThat(participation.getExercise()).isEqualTo(textExercise);
assertThat(participation.getExercise().getCourseViaExerciseGroupOrCourseMember()).isNotNull();
assertThat(participation.getExercise().getExerciseGroup()).isEqualTo(exam.getExerciseGroups().get(0));
assertThat(participation.getSubmissions()).hasSize(1);
var textSubmission = (TextSubmission) participation.getSubmissions().iterator().next();
assertThat(textSubmission.getText()).isNull();
}
}

@Test
@WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
void testStartExercisesWithModelingExercise() throws Exception {
// creating exercise
ModelingExercise modelingExercise = ModelingExerciseFactory.generateModelingExerciseForExam(DiagramType.ClassDiagram, exam.getExerciseGroups().get(0));
exam.getExerciseGroups().get(0).addExercise(modelingExercise);
exerciseGroupRepository.save(exam.getExerciseGroups().get(0));
modelingExercise = exerciseRepo.save(modelingExercise);

createStudentExams(modelingExercise);

List<Participation> studentParticipations = invokePrepareExerciseStart();

for (Participation participation : studentParticipations) {
assertThat(participation.getExercise()).isEqualTo(modelingExercise);
assertThat(participation.getExercise().getCourseViaExerciseGroupOrCourseMember()).isNotNull();
assertThat(participation.getExercise().getExerciseGroup()).isEqualTo(exam.getExerciseGroups().get(0));
assertThat(participation.getSubmissions()).hasSize(1);
var modelingSubmission = (ModelingSubmission) participation.getSubmissions().iterator().next();
assertThat(modelingSubmission.getModel()).isNull();
assertThat(modelingSubmission.getExplanationText()).isNull();
}
}

@Test
@WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
void testStartExerciseWithProgrammingExercise() throws Exception {
bitbucketRequestMockProvider.enableMockingOfRequests(true);
bambooRequestMockProvider.enableMockingOfRequests(true);

ProgrammingExercise programmingExercise = createProgrammingExercise();

participationUtilService.mockCreationOfExerciseParticipation(programmingExercise, versionControlService, continuousIntegrationService);

createStudentExams(programmingExercise);

var studentParticipations = invokePrepareExerciseStart();

for (Participation participation : studentParticipations) {
assertThat(participation.getExercise()).isEqualTo(programmingExercise);
assertThat(participation.getExercise().getCourseViaExerciseGroupOrCourseMember()).isNotNull();
assertThat(participation.getExercise().getExerciseGroup()).isEqualTo(exam.getExerciseGroups().get(0));
// No initial submissions should be created for programming exercises
assertThat(participation.getSubmissions()).isEmpty();
assertThat(((ProgrammingExerciseParticipation) participation).isLocked()).isTrue();
verify(versionControlService, never()).configureRepository(eq(programmingExercise), (ProgrammingExerciseStudentParticipation) eq(participation), eq(true));
}
}

private static class ExamStartDateSource implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(Arguments.of(ZonedDateTime.now().minusHours(1)), // after exam start
Arguments.arguments(ZonedDateTime.now().plusMinutes(3)) // before exam start but after pe unlock date
);
}
}

@ParameterizedTest(name = "{displayName} [{index}]")
@ArgumentsSource(ExamStartDateSource.class)
@WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
void testStartExerciseWithProgrammingExercise_participationUnlocked(ZonedDateTime startDate) throws Exception {
exam.setVisibleDate(ZonedDateTime.now().minusHours(2));
exam.setStartDate(startDate);
examRepository.save(exam);

bitbucketRequestMockProvider.enableMockingOfRequests(true);
bambooRequestMockProvider.enableMockingOfRequests(true);

ProgrammingExercise programmingExercise = createProgrammingExercise();

participationUtilService.mockCreationOfExerciseParticipation(programmingExercise, versionControlService, continuousIntegrationService);

createStudentExams(programmingExercise);

var studentParticipations = invokePrepareExerciseStart();

for (Participation participation : studentParticipations) {
assertThat(participation.getExercise()).isEqualTo(programmingExercise);
assertThat(participation.getExercise().getCourseViaExerciseGroupOrCourseMember()).isNotNull();
assertThat(participation.getExercise().getExerciseGroup()).isEqualTo(exam.getExerciseGroups().get(0));
// No initial submissions should be created for programming exercises
assertThat(participation.getSubmissions()).isEmpty();
ProgrammingExerciseStudentParticipation studentParticipation = (ProgrammingExerciseStudentParticipation) participation;
// The participation should not get locked if it gets created after the exam already started
assertThat(studentParticipation.isLocked()).isFalse();
verify(versionControlService).addMemberToRepository(studentParticipation.getVcsRepositoryUrl(), studentParticipation.getStudent().orElseThrow(),
VersionControlRepositoryPermission.REPO_WRITE);
}
}

private void createStudentExams(Exercise exercise) {
// creating student exams
for (User user : registeredUsers) {
StudentExam studentExam = new StudentExam();
studentExam.addExercise(exercise);
studentExam.setUser(user);
exam.addStudentExam(studentExam);
createdStudentExams.add(studentExamRepository.save(studentExam));
}

exam = examRepository.save(exam);
}

private ProgrammingExercise createProgrammingExercise() {
ProgrammingExercise programmingExercise = ProgrammingExerciseFactory.generateProgrammingExerciseForExam(exam.getExerciseGroups().get(0));
programmingExercise = exerciseRepo.save(programmingExercise);
programmingExercise = programmingExerciseUtilService.addTemplateParticipationForProgrammingExercise(programmingExercise);
exam.getExerciseGroups().get(0).addExercise(programmingExercise);
exerciseGroupRepository.save(exam.getExerciseGroups().get(0));
return programmingExercise;
}

private List<Participation> invokePrepareExerciseStart() throws Exception {
// invoke start exercises
int noGeneratedParticipations = ExamPrepareExercisesTestUtil.prepareExerciseStart(request, exam, course1);
verify(gitService, times(examUtilService.getNumberOfProgrammingExercises(exam.getId()))).combineAllCommitsOfRepositoryIntoOne(any());
assertThat(noGeneratedParticipations).isEqualTo(exam.getStudentExams().size());
return participationTestRepository.findByExercise_ExerciseGroup_Exam_Id(exam.getId());
}

}
19 changes: 19 additions & 0 deletions src/test/java/de/tum/in/www1/artemis/exam/ExamUtilService.java
Original file line number Diff line number Diff line change
Expand Up @@ -765,4 +765,23 @@ public StudentExam addExercisesWithParticipationsAndSubmissionsToStudentExam(Exa

return studentExamRepository.save(studentExam);
}

/**
* gets the number of programming exercises in the exam
*
* @param examId id of the exam to be searched for programming exercises
* @return number of programming exercises in the exams
*/
public int getNumberOfProgrammingExercises(Long examId) {
Exam exam = examRepository.findWithExerciseGroupsAndExercisesByIdOrElseThrow(examId);
int count = 0;
for (var exerciseGroup : exam.getExerciseGroups()) {
for (var exercise : exerciseGroup.getExercises()) {
if (exercise instanceof ProgrammingExercise) {
count++;
}
}
}
return count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import static org.mockito.Mockito.*;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -27,6 +25,7 @@
import de.tum.in.www1.artemis.domain.exam.ExerciseGroup;
import de.tum.in.www1.artemis.exercise.programmingexercise.ProgrammingExerciseFactory;
import de.tum.in.www1.artemis.exercise.textexercise.TextExerciseFactory;
import de.tum.in.www1.artemis.exercise.textexercise.TextExerciseUtilService;
import de.tum.in.www1.artemis.repository.ExamRepository;
import de.tum.in.www1.artemis.repository.ExerciseRepository;
import de.tum.in.www1.artemis.repository.TextExerciseRepository;
Expand Down Expand Up @@ -55,6 +54,9 @@ class ExerciseGroupIntegrationTest extends AbstractSpringIntegrationBambooBitbuc
@Autowired
private ExamUtilService examUtilService;

@Autowired
private TextExerciseUtilService textExerciseUtilService;

private Course course1;

private Exam exam1;
Expand Down Expand Up @@ -276,4 +278,53 @@ void importExerciseGroup_preCheckFailed() throws Exception {
request.postListWithResponseBody("/api/courses/" + course1.getId() + "/exams/" + exam.getId() + "/import-exercise-group", List.of(programmingGroup), ExerciseGroup.class,
HttpStatus.BAD_REQUEST);
}

@Test
@WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
void testUpdateOrderOfExerciseGroups() throws Exception {
Exam exam = ExamFactory.generateExam(course1);
ExerciseGroup exerciseGroup1 = ExamFactory.generateExerciseGroupWithTitle(true, exam, "first");
ExerciseGroup exerciseGroup2 = ExamFactory.generateExerciseGroupWithTitle(true, exam, "second");
ExerciseGroup exerciseGroup3 = ExamFactory.generateExerciseGroupWithTitle(true, exam, "third");
examRepository.save(exam);

TextExercise exercise1_1 = textExerciseUtilService.createTextExerciseForExam(exerciseGroup1);
TextExercise exercise1_2 = textExerciseUtilService.createTextExerciseForExam(exerciseGroup1);
TextExercise exercise2_1 = textExerciseUtilService.createTextExerciseForExam(exerciseGroup2);
TextExercise exercise3_1 = textExerciseUtilService.createTextExerciseForExam(exerciseGroup3);
TextExercise exercise3_2 = textExerciseUtilService.createTextExerciseForExam(exerciseGroup3);
TextExercise exercise3_3 = textExerciseUtilService.createTextExerciseForExam(exerciseGroup3);

List<ExerciseGroup> orderedExerciseGroups = new ArrayList<>(List.of(exerciseGroup2, exerciseGroup3, exerciseGroup1));
// Should save new order
request.put("/api/courses/" + course1.getId() + "/exams/" + exam.getId() + "/exercise-groups-order", orderedExerciseGroups, HttpStatus.OK);
verify(examAccessService).checkCourseAndExamAccessForEditorElseThrow(course1.getId(), exam.getId());

List<ExerciseGroup> savedExerciseGroups = examRepository.findWithExerciseGroupsById(exam.getId()).orElseThrow().getExerciseGroups();
assertThat(savedExerciseGroups.get(0).getTitle()).isEqualTo("second");
assertThat(savedExerciseGroups.get(1).getTitle()).isEqualTo("third");
assertThat(savedExerciseGroups.get(2).getTitle()).isEqualTo("first");

// Exercises should be preserved
Exam savedExam = examRepository.findWithExerciseGroupsAndExercisesById(exam.getId()).orElseThrow();
ExerciseGroup savedExerciseGroup1 = savedExam.getExerciseGroups().get(2);
ExerciseGroup savedExerciseGroup2 = savedExam.getExerciseGroups().get(0);
ExerciseGroup savedExerciseGroup3 = savedExam.getExerciseGroups().get(1);
assertThat(savedExerciseGroup1.getExercises()).containsExactlyInAnyOrder(exercise1_1, exercise1_2);
assertThat(savedExerciseGroup2.getExercises()).containsExactlyInAnyOrder(exercise2_1);
assertThat(savedExerciseGroup3.getExercises()).containsExactlyInAnyOrder(exercise3_1, exercise3_2, exercise3_3);

// Should fail with too many exercise groups
orderedExerciseGroups.add(exerciseGroup1);
request.put("/api/courses/" + course1.getId() + "/exams/" + exam.getId() + "/exercise-groups-order", orderedExerciseGroups, HttpStatus.BAD_REQUEST);

// Should fail with too few exercise groups
orderedExerciseGroups.remove(3);
orderedExerciseGroups.remove(2);
request.put("/api/courses/" + course1.getId() + "/exams/" + exam.getId() + "/exercise-groups-order", orderedExerciseGroups, HttpStatus.BAD_REQUEST);

// Should fail with different exercise group
orderedExerciseGroups = Arrays.asList(exerciseGroup2, exerciseGroup3, ExamFactory.generateExerciseGroup(true, exam));
request.put("/api/courses/" + course1.getId() + "/exams/" + exam.getId() + "/exercise-groups-order", orderedExerciseGroups, HttpStatus.BAD_REQUEST);
}
}
Loading