Skip to content

Commit

Permalink
improve testing coverage, revert merge tests
Browse files Browse the repository at this point in the history
  • Loading branch information
julian-christl committed Oct 27, 2023
1 parent 4c15d72 commit 7dec68e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import java.net.URLConnection;
import java.nio.file.Path;
import java.time.Duration;
import java.util.*;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.awaitility.Awaitility.await;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.argThat;
import static org.mockito.Mockito.mockStatic;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.io.File;
Expand Down Expand Up @@ -3247,6 +3250,11 @@ private Course createCourseWithCourseImageAndReturn() throws Exception {
var result = request.getMvc().perform(buildCreateCourse(course, "testIcon")).andExpect(status().isCreated()).andReturn();
course = objectMapper.readValue(result.getResponse().getContentAsString(), Course.class);

assertThat(course.getCourseIcon()).as("Course icon got stored").isNotNull();
var imgResult = request.getMvc().perform(get(course.getCourseIcon())).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_OCTET_STREAM))
.andReturn();
assertThat(imgResult.getResponse().getContentAsByteArray()).isNotEmpty();

var createdCourse = courseRepo.findByIdElseThrow(course.getId());
assertThat(createdCourse.getCourseIcon()).as("Course icon got stored").isNotNull();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package de.tum.in.www1.artemis.exercise.fileuploadexercise;

import static org.assertj.core.api.Assertions.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.net.URI;
import java.nio.file.Files;
Expand All @@ -14,8 +17,10 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
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.util.LinkedMultiValueMap;

import de.tum.in.www1.artemis.AbstractSpringIntegrationIndependentTest;
Expand Down Expand Up @@ -173,6 +178,10 @@ else if (filename.contains("\\")) {
var fileBytes = Files.readAllBytes(actualFilePath);
assertThat(fileBytes.length > 0).as("Stored file has content").isTrue();
checkDetailsHidden(returnedSubmission, true);

MvcResult file = request.getMvc().perform(get(returnedSubmission.getFilePath())).andExpect(status().isOk()).andExpect(content().contentType(MediaType.IMAGE_PNG))
.andReturn();
assertThat(file.getResponse().getContentAsByteArray()).isEqualTo(validFile.getBytes());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.byLessThan;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.time.ZonedDateTime;
Expand Down Expand Up @@ -149,13 +151,15 @@ void init() {
void testCreateQuizExercise(QuizMode quizMode) throws Exception {
QuizExercise quizExercise = createQuizOnServer(ZonedDateTime.now().plusHours(5), null, quizMode);
createdQuizAssert(quizExercise);
checkCreatedFiles(quizExercise);
}

@Test
@WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
void testCreateQuizExerciseForExam() throws Exception {
QuizExercise quizExercise = createQuizOnServerForExam();
createdQuizAssert(quizExercise);
checkCreatedFiles(quizExercise);
}

@Test
Expand Down Expand Up @@ -253,6 +257,28 @@ private QuizExercise updateQuizExerciseWithFiles(QuizExercise quizExercise, List
return null;
}

private void checkCreatedFiles(QuizExercise quizExercise) throws Exception {
List<DragAndDropQuestion> questions = quizExercise.getQuizQuestions().stream().filter(q -> q instanceof DragAndDropQuestion).map(q -> (DragAndDropQuestion) q).toList();
for (DragAndDropQuestion question : questions) {
if (question.getBackgroundFilePath() == null) {
continue;
}
checkCreatedFile(question.getBackgroundFilePath());
for (DragItem dragItem : question.getDragItems()) {
if (dragItem.getPictureFilePath() == null) {
continue;
}
checkCreatedFile(dragItem.getPictureFilePath());
}
}
}

private void checkCreatedFile(String path) throws Exception {
MvcResult result = request.getMvc().perform(get(path)).andExpect(status().isOk()).andExpect(content().contentType(MediaType.APPLICATION_OCTET_STREAM)).andReturn();
byte[] image = result.getResponse().getContentAsByteArray();
assertThat(image).isNotEmpty();
}

@Test
@WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR")
void createQuizExercise_setNeitherCourseAndExerciseGroup_badRequest() throws Exception {
Expand Down Expand Up @@ -1152,7 +1178,6 @@ void testUpdateQuizExerciseWithNotificationText() throws Exception {
void importQuizExerciseToSameCourse() throws Exception {
ZonedDateTime now = ZonedDateTime.now();
QuizExercise quizExercise = quizExerciseUtilService.createQuiz(now.plusHours(2), null, QuizMode.SYNCHRONIZED);
courseUtilService.enableMessagingForCourse(quizExercise.getCourseViaExerciseGroupOrCourseMember());
quizExerciseService.handleDndQuizFileCreation(quizExercise,
List.of(new MockMultipartFile("files", "dragItemImage2.png", MediaType.IMAGE_PNG_VALUE, "dragItemImage".getBytes()),
new MockMultipartFile("files", "dragItemImage4.png", MediaType.IMAGE_PNG_VALUE, "dragItemImage".getBytes())));
Expand All @@ -1162,7 +1187,6 @@ void importQuizExerciseToSameCourse() throws Exception {
assertThat(changedQuiz).isNotNull();
changedQuiz.setTitle("New title");
changedQuiz.setReleaseDate(now);
changedQuiz.setChannelName("testchannel-quiz");

QuizExercise importedExercise = importQuizExerciseWithFiles(changedQuiz, changedQuiz.getId(), List.of(), HttpStatus.CREATED);

Expand All @@ -1173,10 +1197,6 @@ void importQuizExerciseToSameCourse() throws Exception {
.isEqualTo(quizExercise.getCourseViaExerciseGroupOrCourseMember().getId());
assertThat(importedExercise.getCourseViaExerciseGroupOrCourseMember()).isEqualTo(quizExercise.getCourseViaExerciseGroupOrCourseMember());
assertThat(importedExercise.getQuizQuestions()).as("Imported exercise has same number of questions").hasSameSizeAs(quizExercise.getQuizQuestions());

Channel channelDB = channelRepository.findChannelByExerciseId(importedExercise.getId());
assertThat(channelDB).isNotNull();
assertThat(channelDB.getName()).isEqualTo("testchannel-quiz");
}

/**
Expand All @@ -1192,14 +1212,11 @@ void importQuizExerciseFromCourseToCourse() throws Exception {
new MockMultipartFile("files", "dragItemImage4.png", MediaType.IMAGE_PNG_VALUE, "dragItemImage".getBytes())));
quizExerciseService.save(quizExercise);

courseUtilService.enableMessagingForCourse(quizExercise.getCourseViaExerciseGroupOrCourseMember());
quizExercise.setChannelName("testchannel-quiz" + UUID.randomUUID().toString().substring(0, 8));
Course course = courseUtilService.addEmptyCourse();
quizExercise.setCourse(course);

QuizExercise importedExercise = importQuizExerciseWithFiles(quizExercise, quizExercise.getId(), List.of(), HttpStatus.CREATED);
assertThat(importedExercise.getCourseViaExerciseGroupOrCourseMember()).as("Quiz was imported for different course")
.isEqualTo(quizExercise.getCourseViaExerciseGroupOrCourseMember());
Channel channelDB = channelRepository.findChannelByExerciseId(importedExercise.getId());
assertThat(channelDB).isNotNull();
assertThat(importedExercise.getCourseViaExerciseGroupOrCourseMember()).as("Quiz was imported for different course").isEqualTo(course);
}

/**
Expand All @@ -1219,8 +1236,6 @@ void importQuizExerciseFromCourseToExam() throws Exception {

QuizExercise importedExercise = importQuizExerciseWithFiles(quizExercise, quizExercise.getId(), List.of(), HttpStatus.CREATED);
assertThat(importedExercise.getExerciseGroup()).as("Quiz was imported for different exercise group").isEqualTo(exerciseGroup);
Channel channelDB = channelRepository.findChannelByExerciseId(importedExercise.getId());
assertThat(channelDB).isNull();
}

/**
Expand Down Expand Up @@ -1253,7 +1268,6 @@ void importQuizExerciseFromExamToCourse() throws Exception {
new MockMultipartFile("files", "dragItemImage4.png", MediaType.IMAGE_PNG_VALUE, "dragItemImage".getBytes())));
quizExerciseService.save(quizExercise);
quizExercise.setCourse(course);
quizExercise.setChannelName("testchannel-quiz");

QuizExercise importedExercise = importQuizExerciseWithFiles(quizExercise, quizExercise.getId(), List.of(), HttpStatus.CREATED);
assertThat(importedExercise.getCourseViaExerciseGroupOrCourseMember()).isEqualTo(course);
Expand Down Expand Up @@ -1320,7 +1334,6 @@ void testImportQuizExercise_team_modeChange() throws Exception {

Course course = courseUtilService.addEmptyCourse();
changedQuiz.setCourse(course);
changedQuiz.setChannelName("testchannel-quiz");
quizExerciseUtilService.setupTeamQuizExercise(changedQuiz, 1, 10);

changedQuiz = importQuizExerciseWithFiles(changedQuiz, quizExercise.getId(), List.of(), HttpStatus.CREATED);
Expand Down Expand Up @@ -1367,7 +1380,6 @@ void testImportQuizExercise_individual_modeChange() throws Exception {
changedQuiz.setMode(ExerciseMode.INDIVIDUAL);
Course course = courseUtilService.addEmptyCourse();
changedQuiz.setCourse(course);
changedQuiz.setChannelName("testchannel-quiz");

changedQuiz = importQuizExerciseWithFiles(changedQuiz, quizExercise.getId(), List.of(), HttpStatus.CREATED);

Expand Down Expand Up @@ -1396,7 +1408,6 @@ void testImportQuizExerciseChangeQuizMode() throws Exception {
QuizExercise changedQuiz = quizExerciseRepository.findOneWithQuestionsAndStatistics(quizExercise.getId());
assertThat(changedQuiz).isNotNull();
changedQuiz.setQuizMode(QuizMode.INDIVIDUAL);
changedQuiz.setChannelName("testchannel-quiz");

QuizExercise importedExercise = importQuizExerciseWithFiles(changedQuiz, quizExercise.getId(), List.of(), HttpStatus.CREATED);

Expand Down Expand Up @@ -1439,7 +1450,6 @@ void testMultipleChoiceQuizExplanationLength_Valid() throws Exception {

MultipleChoiceQuestion question = (MultipleChoiceQuestion) quizExercise.getQuizQuestions().get(0);
question.setExplanation("0".repeat(validityThreshold));
quizExercise.setChannelName("testchannel-quiz");

createQuizExerciseWithFiles(quizExercise, HttpStatus.CREATED, true);
}
Expand Down Expand Up @@ -1470,7 +1480,6 @@ void testMultipleChoiceQuizOptionExplanationLength_Valid() throws Exception {

MultipleChoiceQuestion question = (MultipleChoiceQuestion) quizExercise.getQuizQuestions().get(0);
question.getAnswerOptions().get(0).setExplanation("0".repeat(validityThreshold));
quizExercise.setChannelName("testchannel-quiz");

createQuizExerciseWithFiles(quizExercise, HttpStatus.CREATED, true);
}
Expand Down Expand Up @@ -1581,21 +1590,13 @@ void testFilterForCourseDashboard_QuizSubmissionButNoParticipation() {
}

private QuizExercise createQuizOnServer(ZonedDateTime releaseDate, ZonedDateTime dueDate, QuizMode quizMode) throws Exception {
return createQuizOnServer(releaseDate, dueDate, quizMode, "exercise-new-quiz");
}

private QuizExercise createQuizOnServer(ZonedDateTime releaseDate, ZonedDateTime dueDate, QuizMode quizMode, String channelName) throws Exception {
QuizExercise quizExercise = quizExerciseUtilService.createQuiz(releaseDate, dueDate, quizMode);
quizExercise.setDuration(3600);
quizExercise.setChannelName(channelName);
courseUtilService.enableMessagingForCourse(quizExercise.getCourseViaExerciseGroupOrCourseMember());

QuizExercise quizExerciseServer = createQuizExerciseWithFiles(quizExercise, HttpStatus.CREATED, true);
QuizExercise quizExerciseDatabase = quizExerciseRepository.findOneWithQuestionsAndStatistics(quizExerciseServer.getId());
assertThat(quizExerciseServer).isNotNull();
assertThat(quizExerciseDatabase).isNotNull();
Channel channelDB = channelRepository.findChannelByExerciseId(quizExerciseDatabase.getId());
assertThat(channelDB).isNotNull();

checkQuizExercises(quizExercise, quizExerciseServer);
checkQuizExercises(quizExercise, quizExerciseDatabase);
Expand Down Expand Up @@ -1630,9 +1631,6 @@ private QuizExercise createQuizOnServerForExam() throws Exception {
assertThat(quizExerciseServer).isNotNull();
assertThat(quizExerciseDatabase).isNotNull();

Channel channelDB = channelRepository.findChannelByExerciseId(quizExercise.getId());
assertThat(channelDB).isNull();

checkQuizExercises(quizExercise, quizExerciseServer);
checkQuizExercises(quizExercise, quizExerciseDatabase);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -12,6 +15,7 @@
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.util.LinkedMultiValueMap;

import de.tum.in.www1.artemis.AbstractSpringIntegrationIndependentTest;
Expand Down Expand Up @@ -71,6 +75,10 @@ void initTestCase() {
void createAttachment() throws Exception {
Attachment actualAttachment = request.postWithMultipartFile("/api/attachments", attachment, "attachment",
new MockMultipartFile("file", "test.txt", MediaType.TEXT_PLAIN_VALUE, "testContent".getBytes()), Attachment.class, HttpStatus.CREATED);
assertThat(actualAttachment.getLink()).isNotNull();
MvcResult file = request.getMvc().perform(get(actualAttachment.getLink())).andExpect(status().isOk()).andExpect(content().contentType(MediaType.TEXT_PLAIN_VALUE))
.andReturn();
assertThat(file.getResponse().getContentAsByteArray()).isNotEmpty();
var expectedAttachment = attachmentRepository.findById(actualAttachment.getId()).orElseThrow();
assertThat(actualAttachment).isEqualTo(expectedAttachment);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,10 @@ public <T> T get(String path, HttpStatus expectedStatus, Class<T> responseType,
return get(path, expectedStatus, responseType, params, new HttpHeaders());
}

public File getFile(String path, HttpStatus expectedStatus) throws Exception {

Check warning on line 561 in src/test/java/de/tum/in/www1/artemis/util/RequestUtilService.java

View check run for this annotation

Teamscale / teamscale-findings

src/test/java/de/tum/in/www1/artemis/util/RequestUtilService.java#L561

[New] In this file 61 interface comments are missing. Consider adding explanatory comments or restricting the visibility. View in Teamscale: https://teamscale.io/activity.html#details/GitHub-ls1intum-Artemis?t=chore%2Fremove-deprecated-file-upload%3A1698433784000
return getFile(path, expectedStatus, new LinkedMultiValueMap<>());
}

public File getFile(String path, HttpStatus expectedStatus, MultiValueMap<String, String> params) throws Exception {
return getFile(path, expectedStatus, params, null);
}
Expand Down

0 comments on commit 7dec68e

Please sign in to comment.