diff --git a/src/test/java/de/tum/in/www1/artemis/exam/QuizPoolIntegrationTest.java b/src/test/java/de/tum/in/www1/artemis/exam/QuizPoolIntegrationTest.java index 9616695f0892..77cfc534b8f8 100644 --- a/src/test/java/de/tum/in/www1/artemis/exam/QuizPoolIntegrationTest.java +++ b/src/test/java/de/tum/in/www1/artemis/exam/QuizPoolIntegrationTest.java @@ -17,14 +17,8 @@ import de.tum.in.www1.artemis.domain.Course; import de.tum.in.www1.artemis.domain.User; import de.tum.in.www1.artemis.domain.exam.Exam; -import de.tum.in.www1.artemis.domain.quiz.DragAndDropQuestion; -import de.tum.in.www1.artemis.domain.quiz.MultipleChoiceQuestion; -import de.tum.in.www1.artemis.domain.quiz.QuizGroup; -import de.tum.in.www1.artemis.domain.quiz.QuizPool; -import de.tum.in.www1.artemis.domain.quiz.QuizQuestion; -import de.tum.in.www1.artemis.domain.quiz.ShortAnswerQuestion; +import de.tum.in.www1.artemis.domain.quiz.*; import de.tum.in.www1.artemis.exercise.quizexercise.QuizExerciseFactory; -import de.tum.in.www1.artemis.exercise.quizexercise.QuizExerciseUtilService; import de.tum.in.www1.artemis.service.QuizPoolService; import de.tum.in.www1.artemis.user.UserUtilService; import de.tum.in.www1.artemis.util.RequestUtilService; @@ -42,9 +36,6 @@ class QuizPoolIntegrationTest extends AbstractSpringIntegrationBambooBitbucketJi @Autowired private ExamUtilService examUtilService; - @Autowired - private QuizExerciseUtilService quizExerciseUtilService; - @Autowired private UserUtilService userUtilService; @@ -91,10 +82,10 @@ void testCreateQuizPoolSuccessful() throws Exception { void testUpdateQuizPoolSuccessful() throws Exception { QuizPool quizPool = createQuizPool(); - QuizGroup quizGroup3 = quizExerciseUtilService.createQuizGroup("Exception Handling"); - QuizQuestion saQuizQuestion1 = quizExerciseUtilService.createShortAnswerQuestionWithTitleAndGroup("SA 1", quizGroup2); - QuizQuestion saQuizQuestion2 = quizExerciseUtilService.createShortAnswerQuestionWithTitleAndGroup("SA 2", quizGroup3); - QuizQuestion saQuizQuestion3 = quizExerciseUtilService.createShortAnswerQuestionWithTitleAndGroup("SA 3", null); + QuizGroup quizGroup3 = QuizExerciseFactory.createQuizGroup("Exception Handling"); + QuizQuestion saQuizQuestion1 = QuizExerciseFactory.createShortAnswerQuestionWithTitleAndGroup("SA 1", quizGroup2); + QuizQuestion saQuizQuestion2 = QuizExerciseFactory.createShortAnswerQuestionWithTitleAndGroup("SA 2", quizGroup3); + QuizQuestion saQuizQuestion3 = QuizExerciseFactory.createShortAnswerQuestionWithTitleAndGroup("SA 3", null); quizPool.setQuizGroups(List.of(quizPool.getQuizGroups().get(0), quizPool.getQuizGroups().get(2), quizGroup3)); quizPool.setQuizQuestions(List.of(quizPool.getQuizQuestions().get(0), quizPool.getQuizQuestions().get(1), quizPool.getQuizQuestions().get(2), saQuizQuestion1, saQuizQuestion2, saQuizQuestion3)); @@ -165,11 +156,11 @@ void testUpdateQuizPoolNotFoundExam() throws Exception { @Test @WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR") void testGetQuizPoolSuccessful() throws Exception { - QuizGroup quizGroup0 = quizExerciseUtilService.createQuizGroup("Encapsulation"); - QuizGroup quizGroup1 = quizExerciseUtilService.createQuizGroup("Inheritance"); - QuizQuestion mcQuizQuestion = quizExerciseUtilService.createMultipleChoiceQuestionWithTitleAndGroup("MC", quizGroup0); - QuizQuestion dndQuizQuestion = quizExerciseUtilService.createDragAndDropQuestionWithTitleAndGroup("DND", quizGroup1); - QuizQuestion saQuizQuestion = quizExerciseUtilService.createShortAnswerQuestionWithTitleAndGroup("SA", null); + QuizGroup quizGroup0 = QuizExerciseFactory.createQuizGroup("Encapsulation"); + QuizGroup quizGroup1 = QuizExerciseFactory.createQuizGroup("Inheritance"); + QuizQuestion mcQuizQuestion = QuizExerciseFactory.createMultipleChoiceQuestionWithTitleAndGroup("MC", quizGroup0); + QuizQuestion dndQuizQuestion = QuizExerciseFactory.createDragAndDropQuestionWithTitleAndGroup("DND", quizGroup1); + QuizQuestion saQuizQuestion = QuizExerciseFactory.createShortAnswerQuestionWithTitleAndGroup("SA", null); quizPool.setQuizGroups(List.of(quizGroup0, quizGroup1)); quizPool.setQuizQuestions(List.of(mcQuizQuestion, dndQuizQuestion, saQuizQuestion)); QuizPool savedQuizPool = quizPoolService.update(exam.getId(), quizPool); @@ -190,14 +181,14 @@ void testGetQuizPoolNotFoundExam() throws Exception { } private QuizPool createQuizPool() throws Exception { - quizGroup0 = quizExerciseUtilService.createQuizGroup("Encapsulation"); - quizGroup1 = quizExerciseUtilService.createQuizGroup("Inheritance"); - quizGroup2 = quizExerciseUtilService.createQuizGroup("Polymorphism"); - QuizQuestion mcQuizQuestion0 = quizExerciseUtilService.createMultipleChoiceQuestionWithTitleAndGroup("MC 0", quizGroup0); - QuizQuestion mcQuizQuestion1 = quizExerciseUtilService.createMultipleChoiceQuestionWithTitleAndGroup("MC 1", quizGroup0); - QuizQuestion dndQuizQuestion0 = quizExerciseUtilService.createDragAndDropQuestionWithTitleAndGroup("DND 0", quizGroup1); - QuizQuestion dndQuizQuestion1 = quizExerciseUtilService.createDragAndDropQuestionWithTitleAndGroup("DND 1", quizGroup2); - QuizQuestion saQuizQuestion0 = quizExerciseUtilService.createShortAnswerQuestionWithTitleAndGroup("SA 0", null); + quizGroup0 = QuizExerciseFactory.createQuizGroup("Encapsulation"); + quizGroup1 = QuizExerciseFactory.createQuizGroup("Inheritance"); + quizGroup2 = QuizExerciseFactory.createQuizGroup("Polymorphism"); + QuizQuestion mcQuizQuestion0 = QuizExerciseFactory.createMultipleChoiceQuestionWithTitleAndGroup("MC 0", quizGroup0); + QuizQuestion mcQuizQuestion1 = QuizExerciseFactory.createMultipleChoiceQuestionWithTitleAndGroup("MC 1", quizGroup0); + QuizQuestion dndQuizQuestion0 = QuizExerciseFactory.createDragAndDropQuestionWithTitleAndGroup("DND 0", quizGroup1); + QuizQuestion dndQuizQuestion1 = QuizExerciseFactory.createDragAndDropQuestionWithTitleAndGroup("DND 1", quizGroup2); + QuizQuestion saQuizQuestion0 = QuizExerciseFactory.createShortAnswerQuestionWithTitleAndGroup("SA 0", null); quizPool.setQuizGroups(List.of(quizGroup0, quizGroup1, quizGroup2)); quizPool.setQuizQuestions(List.of(mcQuizQuestion0, mcQuizQuestion1, dndQuizQuestion0, dndQuizQuestion1, saQuizQuestion0)); diff --git a/src/test/java/de/tum/in/www1/artemis/exercise/modelingexercise/ModelingExerciseUtilService.java b/src/test/java/de/tum/in/www1/artemis/exercise/modelingexercise/ModelingExerciseUtilService.java index f79432fba120..a42148dbb0de 100644 --- a/src/test/java/de/tum/in/www1/artemis/exercise/modelingexercise/ModelingExerciseUtilService.java +++ b/src/test/java/de/tum/in/www1/artemis/exercise/modelingexercise/ModelingExerciseUtilService.java @@ -318,7 +318,6 @@ public Submission addModelingSubmissionWithFinishedResultAndAssessor(ModelingExe return participationUtilService.addSubmissionWithFinishedResultsWithAssessor(participation, submission, assessorLogin); } - /** * Creates and saves a ModelingSubmission from a file. * diff --git a/src/test/java/de/tum/in/www1/artemis/exercise/quizexercise/QuizExerciseFactory.java b/src/test/java/de/tum/in/www1/artemis/exercise/quizexercise/QuizExerciseFactory.java index e35dcc77a67d..65bf421a80e3 100644 --- a/src/test/java/de/tum/in/www1/artemis/exercise/quizexercise/QuizExerciseFactory.java +++ b/src/test/java/de/tum/in/www1/artemis/exercise/quizexercise/QuizExerciseFactory.java @@ -28,28 +28,47 @@ */ public class QuizExerciseFactory { + /** + * Creates a quiz exercise with the given dates and adds it to the course. + * The quiz consist of one multiple choice, one drag and drop, and one short answer question. + * + * @param course The course the quiz should be added to. + * @param releaseDate The release date of the quiz. + * @param dueDate The due date of the quiz. + * @param quizMode The quiz mode used. SYNCHRONIZED, BATCHED, or INDIVIDUAL. + * @return The created quiz. + */ @NotNull public static QuizExercise createQuiz(Course course, ZonedDateTime releaseDate, ZonedDateTime dueDate, QuizMode quizMode) { QuizExercise quizExercise = generateQuizExercise(releaseDate, dueDate, quizMode, course); - initializeQuizExercise(quizExercise); + addQuestionsToQuizExercise(quizExercise); return quizExercise; } + /** + * Creates a quiz exercise for an exam with the given dates and adds it to the course. + * The quiz consist of one multiple choice, one drag and drop, and one short answer question. + * + * @param exerciseGroup Exercise group of an exam to which the quiz should be added. + * @return The created quiz. + */ @NotNull public static QuizExercise createQuizForExam(ExerciseGroup exerciseGroup) { QuizExercise quizExercise = generateQuizExerciseForExam(exerciseGroup); - initializeQuizExercise(quizExercise); + addQuestionsToQuizExercise(quizExercise); return quizExercise; } /** - * initializes a quiz with all different types of questions + * Adds different types of questions to the given quiz. + * One multiple choice, one drag and drop, and one short answer question is added to the quiz. + * The grading instructions are set to null. * - * @param quizExercise to be initialized + * @param quizExercise The quiz to which questions should be added. */ - public static void initializeQuizExercise(QuizExercise quizExercise) { + public static void addQuestionsToQuizExercise(QuizExercise quizExercise) { quizExercise.addQuestions(createMultipleChoiceQuestion()); quizExercise.addQuestions(createDragAndDropQuestion()); quizExercise.addQuestions(createShortAnswerQuestion()); @@ -57,6 +76,12 @@ public static void initializeQuizExercise(QuizExercise quizExercise) { quizExercise.setGradingInstructions(null); } + /** + * Creates a short answer question with score 2 and 2 different spot-solution mappings. + * The scoring type of the question is proportional without penalty. + * + * @return The created short answer question. + */ @NotNull public static ShortAnswerQuestion createShortAnswerQuestion() { ShortAnswerQuestion sa = (ShortAnswerQuestion) new ShortAnswerQuestion().title("SA").score(2).text("This is a long answer text"); @@ -106,6 +131,12 @@ public static ShortAnswerQuestion createShortAnswerQuestion() { return sa; } + /** + * Creates a drag and drop question with score 3 and 3 different location-drag item mappings. + * The scoring type of the question is proportional with penalty. + * + * @return The created drag and drop question. + */ @NotNull public static DragAndDropQuestion createDragAndDropQuestion() { DragAndDropQuestion dnd = (DragAndDropQuestion) new DragAndDropQuestion().title("DnD").score(3).text("Q2"); @@ -168,10 +199,21 @@ public static DragAndDropQuestion createDragAndDropQuestion() { return dnd; } + /** + * Generates a temporary id for a quiz exercise using ThreadLocalRandom. + * + * @return The generated temporary id. + */ public static Long generateTempId() { return ThreadLocalRandom.current().nextLong(Long.MAX_VALUE); } + /** + * Creates a multiple choice question with score 4 and 2 different answer options. One answer is correct and the other one incorrect. + * The scoring type of the question is all or nothing. + * + * @return The created multiple choice question. + */ @NotNull public static MultipleChoiceQuestion createMultipleChoiceQuestion() { MultipleChoiceQuestion mc = (MultipleChoiceQuestion) new MultipleChoiceQuestion().title("MC").score(4).text("Q1"); @@ -184,6 +226,13 @@ public static MultipleChoiceQuestion createMultipleChoiceQuestion() { return mc; } + /** + * Generates a quiz batch for a quiz exercise using the given start time. + * + * @param quizExercise The quiz to which a batch should be added. + * @param startTime The start time of the batch. + * @return The created quiz batch. + */ public static QuizBatch generateQuizBatch(QuizExercise quizExercise, ZonedDateTime startTime) { var quizBatch = new QuizBatch(); quizBatch.setQuizExercise(quizExercise); @@ -191,10 +240,29 @@ public static QuizBatch generateQuizBatch(QuizExercise quizExercise, ZonedDateTi return quizBatch; } + /** + * Generates a quiz exercise without an assessment due date and adds it to the given course. + * + * @param releaseDate The release date of the quiz. + * @param dueDate The due date of the quiz. + * @param quizMode The quiz mode of the quiz. SYNCHRONIZED, BATCHED, or INDIVIDUAL. + * @param course The course to which the quiz should be added to. + * @return The created quiz exercise. + */ public static QuizExercise generateQuizExercise(ZonedDateTime releaseDate, ZonedDateTime dueDate, QuizMode quizMode, Course course) { return generateQuizExercise(releaseDate, dueDate, null, quizMode, course); } + /** + * Generates a quiz exercise using the passed dates and adds it to the given course. + * + * @param releaseDate The release date of the quiz. + * @param dueDate The due date of the quiz. + * @param assessmentDueDate The assessment due date of the quiz. + * @param quizMode The quiz mode of the quiz. SYNCHRONIZED, BATCHED, or INDIVIDUAL. + * @param course The course to which the quiz should be added to. + * @return The created quiz exercise. + */ public static QuizExercise generateQuizExercise(ZonedDateTime releaseDate, ZonedDateTime dueDate, ZonedDateTime assessmentDueDate, QuizMode quizMode, Course course) { QuizExercise quizExercise = (QuizExercise) ExerciseFactory.populateExercise(new QuizExercise(), releaseDate, dueDate, assessmentDueDate, course); quizExercise.setTitle("new quiz"); @@ -214,11 +282,12 @@ public static QuizExercise generateQuizExercise(ZonedDateTime releaseDate, Zoned } /** - * Generates a submitted answer for a given question. + * Generates a submitted answer for the given quiz question of any type. + * The correctness of the answer depends on the passed correct value. * - * @param question given question, the answer is for - * @param correct boolean whether the answer should be correct or not - * @return created SubmittedAnswer + * @param question The quiz question to which a submitted answer should be added. + * @param correct True, if the answer should be correct. + * @return The generated submitted answer. */ public static SubmittedAnswer generateSubmittedAnswerFor(QuizQuestion question, boolean correct) { if (question instanceof MultipleChoiceQuestion) { @@ -299,6 +368,12 @@ else if (question instanceof ShortAnswerQuestion) { return null; } + /** + * Generates a quiz exercise for an exam with the duration of 10 seconds and randomized question order. + * + * @param exerciseGroup The exam exercise group to which the quiz should be added. + * @return The created exam quiz exercise. + */ public static QuizExercise generateQuizExerciseForExam(ExerciseGroup exerciseGroup) { var quizExercise = (QuizExercise) ExerciseFactory.populateExerciseForExam(new QuizExercise(), exerciseGroup); @@ -309,41 +384,56 @@ public static QuizExercise generateQuizExerciseForExam(ExerciseGroup exerciseGro quizExercise.setAllowedNumberOfAttempts(1); quizExercise.setDuration(10); quizExercise.setQuizPointStatistic(new QuizPointStatistic()); - for (var question : quizExercise.getQuizQuestions()) { - if (question instanceof DragAndDropQuestion) { - question.setQuizQuestionStatistic(new DragAndDropQuestionStatistic()); - } - else if (question instanceof MultipleChoiceQuestion) { - question.setQuizQuestionStatistic(new MultipleChoiceQuestionStatistic()); - } - else { - question.setQuizQuestionStatistic(new ShortAnswerQuestionStatistic()); - } - } quizExercise.setRandomizeQuestionOrder(true); return quizExercise; } + /** + * Generates quiz exercise for an exam with the duration of 10 seconds and randomized question order. + * + * @param exerciseGroup The exam exercise group to which the quiz exercise should be added. + * @param title The title which is used to set the quiz title together with a 3 character random UUID suffix. + * @return The generated quiz exercise. + */ public static QuizExercise generateQuizExerciseForExam(ExerciseGroup exerciseGroup, String title) { var quizExercise = generateQuizExerciseForExam(exerciseGroup); quizExercise.setTitle(title + UUID.randomUUID().toString().substring(0, 3)); return quizExercise; } - public static void initializeQuizExerciseWithAllQuestionTypes(QuizExercise quizExercise) { + /** + * Adds different types of questions to the given quiz. + * One multiple choice, one drag and drop, one short answer, and one single choice question is added to the quiz. + * The grading instructions are set to null. + * + * @param quizExercise The quiz to which questions should be added. + */ + public static void addAllQuestionTypesToQuizExercise(QuizExercise quizExercise) { quizExercise.addQuestions(createMultipleChoiceQuestionWithAllTypesOfAnswerOptions()); quizExercise.addQuestions(createDragAndDropQuestionWithAllTypesOfMappings()); quizExercise.addQuestions(createShortAnswerQuestionWithRealisticText()); quizExercise.addQuestions(createSingleChoiceQuestion()); } + /** + * Creates a short answer question with "This [-spot0] a [-spot 2] answer text" text. + * + * @return The created short answer question. + */ private static ShortAnswerQuestion createShortAnswerQuestionWithRealisticText() { var shortAnswerQuestion = createShortAnswerQuestion(); shortAnswerQuestion.setText("This [-spot0] a [-spot 2] answer text"); return shortAnswerQuestion; } + /** + * Creates a drag and drop question with score 5 and 3 different location-drag item mappings. + * Fourth drag item is invalid and fifth one has a picture file. + * The scoring type of the question is proportional with penalty. + * + * @return The created drag and drop question. + */ public static DragAndDropQuestion createDragAndDropQuestionWithAllTypesOfMappings() { DragAndDropQuestion dnd = (DragAndDropQuestion) new DragAndDropQuestion().title("DnD").score(3).text("Q2"); dnd.setScoringType(ScoringType.PROPORTIONAL_WITH_PENALTY); @@ -417,6 +507,13 @@ public static DragAndDropQuestion createDragAndDropQuestionWithAllTypesOfMapping return dnd; } + /** + * Creates a multiple choice question with score 4 and 5 different answer options. 2 answers are correct and the other 3 incorrect. + * One answer option is invalid. + * The scoring type of the question is all or nothing. + * + * @return The created multiple choice question. + */ @NotNull public static MultipleChoiceQuestion createMultipleChoiceQuestionWithAllTypesOfAnswerOptions() { MultipleChoiceQuestion mc = (MultipleChoiceQuestion) new MultipleChoiceQuestion().title("MC").score(4).text("Q1"); @@ -429,6 +526,12 @@ public static MultipleChoiceQuestion createMultipleChoiceQuestionWithAllTypesOfA return mc; } + /** + * Creates a single choice question with 2 answer options. + * The scoring type of the question is all or nothing. + * + * @return The created single choice question. + */ @NotNull public static MultipleChoiceQuestion createSingleChoiceQuestion() { var singleChoiceQuestion = createMultipleChoiceQuestion(); @@ -437,12 +540,15 @@ public static MultipleChoiceQuestion createSingleChoiceQuestion() { } /** - * Generate submissions for a student for an exercise. Results depend on the studentID. + * Generates a submission for the given quiz exercise. The quiz should have at least 3 questions! + * A submitted answer is added to each of the first 3 questions. + * The result of each answer depends on the given student id. * - * @param quizExercise QuizExercise the submissions are for (we assume 3 questions here) - * @param studentID ID of the student - * @param submitted Boolean if it is submitted or not - * @param submissionDate Submission date + * @param quizExercise The quiz to which the submission should be added. + * @param studentID The id of the student participating in the quiz. + * @param submitted True, if the submission should be submitted. + * @param submissionDate The submission date. + * @return The created quiz submission. */ public static QuizSubmission generateSubmissionForThreeQuestions(QuizExercise quizExercise, int studentID, boolean submitted, ZonedDateTime submissionDate) { QuizSubmission quizSubmission = new QuizSubmission(); @@ -458,6 +564,13 @@ public static QuizSubmission generateSubmissionForThreeQuestions(QuizExercise qu return quizSubmission; } + /** + * Generates a submitted answer for the given quiz question of any type. + * The answer is only partially correct. + * + * @param question The quiz question to which a submitted answer should be added. + * @return The generated submitted answer. + */ public static SubmittedAnswer generateSubmittedAnswerForQuizWithCorrectAndFalseAnswers(QuizQuestion question) { if (question instanceof MultipleChoiceQuestion) { var submittedAnswer = new MultipleChoiceSubmittedAnswer(); @@ -521,12 +634,13 @@ else if (question instanceof ShortAnswerQuestion) { } /** - * Generate a submission with all or none options of a MultipleChoiceQuestion selected, if there is one in the exercise + * Generate a submission with all or none options of a multiple choice questions selected. + * For other question typed, incorrect submitted answers are added. * - * @param quizExercise Exercise the submission is for - * @param submitted Boolean whether it is submitted or not - * @param submissionDate Submission date - * @param selectEverything Boolean whether every answer option should be selected or none + * @param quizExercise The quiz to which the submission should be added. + * @param submitted True, if the submission is submitted. + * @param submissionDate The date the submission is submitted. + * @param selectEverything True, if every answer option should be selected. Otherwise, none are selected. */ public static QuizSubmission generateSpecialSubmissionWithResult(QuizExercise quizExercise, boolean submitted, ZonedDateTime submissionDate, boolean selectEverything) { QuizSubmission quizSubmission = new QuizSubmission(); @@ -553,19 +667,27 @@ public static QuizSubmission generateSpecialSubmissionWithResult(QuizExercise qu return quizSubmission; } + /** + * Creates a titled quiz exercise with all types of questions for an exam. + * The quiz includes one multiple choice, one drag and drop, one short answer, and single choice question. + * + * @param exerciseGroup The exam exercise group to which the quiz exercise should be added. + * @param title The title which is used to set the quiz title together with a 3 character random UUID suffix. + * @return The created exam quiz exercise. + */ @NotNull public static QuizExercise createQuizWithAllQuestionTypesForExam(ExerciseGroup exerciseGroup, String title) { QuizExercise quizExercise = QuizExerciseFactory.generateQuizExerciseForExam(exerciseGroup, title); - initializeQuizExerciseWithAllQuestionTypes(quizExercise); + addAllQuestionTypesToQuizExercise(quizExercise); return quizExercise; } /** - * creates a quiz submissions and adds submitted answer to it + * Creates a quiz submissions and adds a submitted answer to it. * - * @param quizQuestion the quiz question we want to add the submission to - * @param correct should the generated answer be correct or not - * @return the created quiz submission + * @param quizQuestion The quiz question to which a submission should be added. + * @param correct True if, the generated answer should be correct. + * @return The created quiz submission. */ public static QuizSubmission generateQuizSubmissionWithSubmittedAnswer(QuizQuestion quizQuestion, boolean correct) { QuizSubmission quizSubmission = ParticipationFactory.generateQuizSubmission(true); @@ -575,20 +697,87 @@ public static QuizSubmission generateQuizSubmissionWithSubmittedAnswer(QuizQuest } /** - * sets the quiz question id associated to the quiz submission answer to null + * Sets quiz questions associated to quiz submission answers to null. * - * @param quizSubmission quiz submission with answer + * @param quizSubmission The quiz submission with answer. */ public static void setQuizQuestionToNull(QuizSubmission quizSubmission) { quizSubmission.getSubmittedAnswers().forEach(answer -> answer.setQuizQuestion(null)); } /** - * sets the quiz question id associated to the quiz submission answer to null + * Sets quiz question ids associated to quiz submission answers to null. * - * @param quizSubmission quiz submission with answer + * @param quizSubmission The quiz submission with answer. */ public static void setQuizQuestionsIdToNull(QuizSubmission quizSubmission) { quizSubmission.getSubmittedAnswers().forEach(answer -> answer.getQuizQuestion().setId(null)); } + + /** + * Creates a quiz group with the given name. + * + * @param name The name of the quiz group. + * @return The created quiz group. + */ + @NotNull + public static QuizGroup createQuizGroup(String name) { + QuizGroup quizGroup = new QuizGroup(); + quizGroup.setName(name); + return quizGroup; + } + + /** + * Creates a multiple choice question with the given title and quiz group. + * + * @param title The title of the quiz question. + * @param quizGroup The group of the quiz question. + * @return The created multiple choice question. + */ + @NotNull + public static MultipleChoiceQuestion createMultipleChoiceQuestionWithTitleAndGroup(String title, QuizGroup quizGroup) { + MultipleChoiceQuestion quizQuestion = QuizExerciseFactory.createMultipleChoiceQuestion(); + setQuizQuestionsTitleAndGroup(quizQuestion, title, quizGroup); + return quizQuestion; + } + + /** + * Creates a drag and drop question with the given title and quiz group. + * + * @param title The title of the quiz question. + * @param quizGroup The group of the quiz question. + * @return The created drag and drop question. + */ + @NotNull + public static DragAndDropQuestion createDragAndDropQuestionWithTitleAndGroup(String title, QuizGroup quizGroup) { + DragAndDropQuestion quizQuestion = QuizExerciseFactory.createDragAndDropQuestion(); + setQuizQuestionsTitleAndGroup(quizQuestion, title, quizGroup); + return quizQuestion; + } + + /** + * Creates a short answer question with the given title and quiz group. + * + * @param title The title of the quiz question. + * @param quizGroup The group of the quiz question. + * @return The created short answer question. + */ + @NotNull + public static ShortAnswerQuestion createShortAnswerQuestionWithTitleAndGroup(String title, QuizGroup quizGroup) { + ShortAnswerQuestion quizQuestion = QuizExerciseFactory.createShortAnswerQuestion(); + setQuizQuestionsTitleAndGroup(quizQuestion, title, quizGroup); + return quizQuestion; + } + + /** + * Sets the title and group of the quiz question. + * + * @param quizQuestion The quiz question to be updated. + * @param title The new title of the quiz question. + * @param quizGroup The new group of the quiz question. + */ + private static void setQuizQuestionsTitleAndGroup(QuizQuestion quizQuestion, String title, QuizGroup quizGroup) { + quizQuestion.setTitle(title); + quizQuestion.setQuizGroup(quizGroup); + } } diff --git a/src/test/java/de/tum/in/www1/artemis/exercise/quizexercise/QuizExerciseUtilService.java b/src/test/java/de/tum/in/www1/artemis/exercise/quizexercise/QuizExerciseUtilService.java index 27594619aae1..ba378033e3ed 100644 --- a/src/test/java/de/tum/in/www1/artemis/exercise/quizexercise/QuizExerciseUtilService.java +++ b/src/test/java/de/tum/in/www1/artemis/exercise/quizexercise/QuizExerciseUtilService.java @@ -89,10 +89,23 @@ public class QuizExerciseUtilService { @Autowired private QuizScheduleService quizScheduleService; + /** + * Creates and saves a course with one quiz exercise with the title "Title". + * The quiz is synchronized and has a duration of 120 seconds. + * + * @return The created course with the quiz. + */ public Course addCourseWithOneQuizExercise() { return addCourseWithOneQuizExercise("Title"); } + /** + * Creates and saves a course with one quiz exercise with the given title. + * The quiz is synchronized and has a duration of 120 seconds. + * + * @param title The title of the quiz exercise. + * @return The newly created course with the quiz. + */ public Course addCourseWithOneQuizExercise(String title) { Course course = CourseFactory.generateCourse(null, pastTimestamp, futureTimestamp, new HashSet<>(), "tumuser", "tutor", "editor", "instructor"); QuizExercise quizExercise = QuizExerciseFactory.createQuiz(course, futureTimestamp, futureFutureTimestamp, QuizMode.SYNCHRONIZED); @@ -107,34 +120,41 @@ public Course addCourseWithOneQuizExercise(String title) { return course; } - public QuizSubmission saveQuizSubmission(QuizExercise exercise, QuizSubmission submission, String login) { - StudentParticipation participation = participationUtilService.createAndSaveParticipationForExercise(exercise, login); + /** + * Creates and adds a student participation to the given quiz submission. The submission is then saved in the repository. + * + * @param quizExercise The quiz for which a student participation should be created. + * @param submission The submission which should be saved + * @param login The login of the user participating in the quiz. + * @return The saved submission. + */ + public QuizSubmission saveQuizSubmission(QuizExercise quizExercise, QuizSubmission submission, String login) { + StudentParticipation participation = participationUtilService.createAndSaveParticipationForExercise(quizExercise, login); participation.addSubmission(submission); submission.setParticipation(participation); - submission = quizSubmissionRepository.save(submission); - return submission; + return quizSubmissionRepository.save(submission); } /** - * important quiz fields are emptied, so it can be imported, + * Sets quiz exercise dates and course to null so the quiz can be imported. The quiz batches are set to an empty set. * - * @param quizExercise to be emptied + * @param quizExercise The quiz of which fields should be set to null. */ public void emptyOutQuizExercise(QuizExercise quizExercise) { quizExercise.setReleaseDate(null); - quizExercise.setCourse(null); quizExercise.setDueDate(null); quizExercise.setAssessmentDueDate(null); + quizExercise.setCourse(null); quizExercise.setQuizBatches(new HashSet<>()); } /** - * Creates a new quiz that gets saved in the QuizExercise repository. + * Creates and saves a new quiz exercise. * - * @param releaseDate release date of the quiz, is also used to set the start date of the course - * @param dueDate due date of the quiz, is also used to set the end date of the course - * @param quizMode SYNCHRONIZED, BATCHED or INDIVIDUAL - * @return quiz that was created + * @param releaseDate The release date of the quiz, also used to set the start date of the course. + * @param dueDate The due date of the quiz, also used to set the end date of the course. + * @param quizMode The mode of the quiz. SYNCHRONIZED, BATCHED or INDIVIDUAL. + * @return The created quiz exercise. */ public QuizExercise createAndSaveQuiz(ZonedDateTime releaseDate, ZonedDateTime dueDate, QuizMode quizMode) { QuizExercise quizExercise = createQuiz(releaseDate, dueDate, quizMode); @@ -144,31 +164,31 @@ public QuizExercise createAndSaveQuiz(ZonedDateTime releaseDate, ZonedDateTime d } /** - * Creates a new quiz + * Creates and saves a course. A new quiz exercise is created and added to the course. * - * @param releaseDate release date of the quiz, is also used to set the start date of the course - * @param dueDate due date of the quiz, is also used to set the end date of the course - * @param quizMode SYNCHRONIZED, BATCHED or INDIVIDUAL - * @return quiz that was created + * @param releaseDate The release date of the quiz, also used to set the start date of the course. + * @param dueDate The due date of the quiz, also used to set the end date of the course. + * @param quizMode The mode of the quiz. SYNCHRONIZED, BATCHED, or INDIVIDUAL. + * @return The created quiz exercise. */ public QuizExercise createQuiz(ZonedDateTime releaseDate, ZonedDateTime dueDate, QuizMode quizMode) { Course course = courseUtilService.createAndSaveCourse(null, releaseDate == null ? null : releaseDate.minusDays(1), dueDate == null ? null : dueDate.plusDays(1), Set.of()); QuizExercise quizExercise = QuizExerciseFactory.generateQuizExercise(releaseDate, dueDate, quizMode, course); - QuizExerciseFactory.initializeQuizExercise(quizExercise); + QuizExerciseFactory.addQuestionsToQuizExercise(quizExercise); return quizExercise; } /** - * Creates a team quiz exercise with a team and saves it into the repository. + * Creates and saves a team quiz exercise. * - * @param releaseDate release date of the quiz - * @param dueDate due date of the quiz - * @param quizMode SYNCHRONIZED, BATCHED or INDIVIDUAL - * @param minTeamSize minimum number of members the team is allowed to have - * @param maxTeamSize maximum number of members the team is allowed to have - * @return exercise created + * @param releaseDate The release date of the quiz. + * @param dueDate The due date of the quiz. + * @param quizMode The mode of the quiz. SYNCHRONIZED, BATCHED or INDIVIDUAL + * @param minTeamSize The minimum number of members the team is allowed to have. + * @param maxTeamSize The maximum number of members the team is allowed to have. + * @return The created quiz exercise. */ public QuizExercise createAndSaveTeamQuiz(ZonedDateTime releaseDate, ZonedDateTime dueDate, QuizMode quizMode, int minTeamSize, int maxTeamSize) { QuizExercise quizExercise = createQuiz(releaseDate, dueDate, quizMode); @@ -183,11 +203,11 @@ public QuizExercise createAndSaveTeamQuiz(ZonedDateTime releaseDate, ZonedDateTi } /** - * sets up a team quiz exercise. + * Sets up a team quiz exercise by creating the team assignment config with the passed values and setting it to the quiz. * - * @param quiz quiz exercise that should be a team exercise. - * @param minTeamSize minimum number of members the team is allowed to have - * @param maxTeamSize maximum number of members the team is allowed to have + * @param quiz The quiz which should be a team exercise. + * @param minTeamSize The minimum number of members the team is allowed to have. + * @param maxTeamSize The maximum number of members the team is allowed to have. */ public void setupTeamQuizExercise(QuizExercise quiz, int minTeamSize, int maxTeamSize) { var teamAssignmentConfig = new TeamAssignmentConfig(); @@ -199,11 +219,11 @@ public void setupTeamQuizExercise(QuizExercise quiz, int minTeamSize, int maxTea } /** - * Creates a new exam quiz that gets saved in the QuizExercise repository. + * Creates and saves a course and an exam. An exam quiz exercise is created and saved. * - * @param startDate start date of the exam, is also used to set the end date of the course the exam is in - * @param endDate end date of the exam, is also used to set the end date of the course the exam is in - * @return exam quiz that was created + * @param startDate The start date of the exam, also used to set the start date of the course the exam is in. + * @param endDate The end date of the exam, also used to set the end date of the course the exam is in. + * @return The created exam quiz exercise. */ @NotNull public QuizExercise createAndSaveExamQuiz(ZonedDateTime startDate, ZonedDateTime endDate) { @@ -214,7 +234,7 @@ public QuizExercise createAndSaveExamQuiz(ZonedDateTime startDate, ZonedDateTime examRepository.save(exam); QuizExercise quizExercise = QuizExerciseFactory.generateQuizExerciseForExam(exerciseGroup); - QuizExerciseFactory.initializeQuizExercise(quizExercise); + QuizExerciseFactory.addQuestionsToQuizExercise(quizExercise); quizExerciseRepository.save(quizExercise); @@ -222,10 +242,10 @@ public QuizExercise createAndSaveExamQuiz(ZonedDateTime startDate, ZonedDateTime } /** - * renames the quiz with the passed title, the quiz gets saved in the repository. + * Renames and saves the quiz exercise using the passed title. * - * @param quizExercise quiz to be renamed - * @param newTitle new name of the quiz + * @param quizExercise The quiz to be renamed. + * @param newTitle The new name of the quiz. */ public void renameAndSaveQuiz(QuizExercise quizExercise, String newTitle) { quizExercise.setTitle(newTitle); @@ -233,16 +253,28 @@ public void renameAndSaveQuiz(QuizExercise quizExercise, String newTitle) { } /** - * sets the quiz exercise of quiz batch and saves the batch into the repository + * Sets the quiz exercise of the quiz batch and saves the batch into the repository. * - * @param batch quiz batch that should get saved - * @param quizExercise quiz exercise to be added to the batch + * @param batch The quiz batch which should be saved. + * @param quizExercise The quiz exercise to be added to the batch. */ public void setQuizBatchExerciseAndSave(QuizBatch batch, QuizExercise quizExercise) { batch.setQuizExercise(quizExercise); quizBatchRepository.save(batch); } + /** + * Creates and saves a quiz with a multiple, single choice, short, and drag and drop question. + * An actual picture file is used as the background and a data item of the drag and drop question. + * The quiz takes 120 seconds and is synchronized. + * A participation and submission are also created for the user with the given login. An answer is submitted for each question. + * + * @param course The course of the quiz. + * @param login The login of the user participating in the quiz. + * @param dueDateInTheFuture True, if the due date of the quiz is in the future. + * @return The created quiz submission. + * @throws IOException If the background or data item file cannot be accessed. + */ public QuizSubmission addQuizExerciseToCourseWithParticipationAndSubmissionForUser(Course course, String login, boolean dueDateInTheFuture) throws IOException { QuizExercise quizExercise; if (dueDateInTheFuture) { @@ -253,8 +285,7 @@ public QuizSubmission addQuizExerciseToCourseWithParticipationAndSubmissionForUs } quizExercise.setTitle("quiz"); quizExercise.setDuration(120); - assertThat(quizExercise.getQuizQuestions()).isNotEmpty(); - assertThat(quizExercise.isValid()).isTrue(); + course.addExercises(quizExercise); StudentParticipation studentParticipation = new StudentParticipation(); studentParticipation.setExercise(quizExercise); @@ -351,48 +382,33 @@ public QuizSubmission addQuizExerciseToCourseWithParticipationAndSubmissionForUs return quizSubmission; } + /** + * Creates and saves a quiz exercise with all question types using the passed dates and adds it to the given course. + * After initialization, the quiz consists of one multiple choice, one drag and drop, one short answer, and single choice question. + * + * @param releaseDate The release date of the quiz. + * @param dueDate The due date of the quiz. + * @param assessmentDueDate The assessment due date of the quiz. + * @param quizMode The quiz mode of the quiz. + * @param course The course to which the quiz should be added to. + * @return The newly created quiz exercise. + */ public QuizExercise createAndSaveQuizWithAllQuestionTypes(Course course, ZonedDateTime releaseDate, ZonedDateTime dueDate, ZonedDateTime assessmentDueDate, QuizMode quizMode) { QuizExercise quizExercise = QuizExerciseFactory.generateQuizExercise(releaseDate, dueDate, assessmentDueDate, quizMode, course); - QuizExerciseFactory.initializeQuizExerciseWithAllQuestionTypes(quizExercise); + QuizExerciseFactory.addAllQuestionTypesToQuizExercise(quizExercise); return quizExerciseRepository.save(quizExercise); } + /** + * Joins the given quiz batch as the user with the given username. + * + * @param quizExercise The quiz of the batch which should be joined. + * @param batch The quiz batch which should be joined. + * @param username The username of the user joining the batch. + */ public void joinQuizBatch(QuizExercise quizExercise, QuizBatch batch, String username) { var user = new User(); user.setLogin(username); quizScheduleService.joinQuizBatch(quizExercise, batch, user); } - - @NotNull - public QuizGroup createQuizGroup(String name) { - QuizGroup quizGroup = new QuizGroup(); - quizGroup.setName(name); - return quizGroup; - } - - @NotNull - public MultipleChoiceQuestion createMultipleChoiceQuestionWithTitleAndGroup(String title, QuizGroup quizGroup) { - MultipleChoiceQuestion quizQuestion = QuizExerciseFactory.createMultipleChoiceQuestion(); - setQuizQuestionsTitleAndGroup(quizQuestion, title, quizGroup); - return quizQuestion; - } - - @NotNull - public DragAndDropQuestion createDragAndDropQuestionWithTitleAndGroup(String title, QuizGroup quizGroup) { - DragAndDropQuestion quizQuestion = QuizExerciseFactory.createDragAndDropQuestion(); - setQuizQuestionsTitleAndGroup(quizQuestion, title, quizGroup); - return quizQuestion; - } - - @NotNull - public ShortAnswerQuestion createShortAnswerQuestionWithTitleAndGroup(String title, QuizGroup quizGroup) { - ShortAnswerQuestion quizQuestion = QuizExerciseFactory.createShortAnswerQuestion(); - setQuizQuestionsTitleAndGroup(quizQuestion, title, quizGroup); - return quizQuestion; - } - - private void setQuizQuestionsTitleAndGroup(Q quizQuestion, String title, QuizGroup quizGroup) { - quizQuestion.setTitle(title); - quizQuestion.setQuizGroup(quizGroup); - } }