Skip to content

Commit

Permalink
Fix existing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesStoehr committed Aug 12, 2024
1 parent c8ef79a commit 07e3972
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public interface FileUploadExerciseRepository extends ArtemisJpaRepository<FileU
@EntityGraph(type = LOAD, attributePaths = { "teamAssignmentConfig", "categories", "competencies" })
Optional<FileUploadExercise> findWithEagerTeamAssignmentConfigAndCategoriesAndCompetenciesById(Long exerciseId);

Optional<FileUploadExercise> findByTitleAndCourseId(@Param("title") String title, @Param("courseId") long courseId);
Optional<FileUploadExercise> findByTitleAndCourseId(String title, long courseId);

@NotNull
default FileUploadExercise findWithEagerCompetenciesByIdElseThrow(Long exerciseId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public interface LectureRepository extends ArtemisJpaRepository<Lecture, Long> {
""")
Optional<Lecture> findByIdWithLectureUnitsAndSlidesAndAttachments(@Param("lectureId") long lectureId);

Optional<Lecture> findByTitleAndCourseId(@Param("title") String title, @Param("courseId") Long courseId);
Optional<Lecture> findByTitleAndCourseId(String title, long courseId);

@SuppressWarnings("PMD.MethodNamingConventions")
Page<Lecture> findByTitleIgnoreCaseContainingOrCourse_TitleIgnoreCaseContaining(String partialTitle, String partialCourseTitle, Pageable pageable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public interface ModelingExerciseRepository extends ArtemisJpaRepository<Modelin
@EntityGraph(type = LOAD, attributePaths = { "studentParticipations", "studentParticipations.submissions", "studentParticipations.submissions.results" })
Optional<ModelingExercise> findWithStudentParticipationsSubmissionsResultsById(Long exerciseId);

Optional<ModelingExercise> findByTitleAndCourseId(@Param("title") String title, @Param("courseId") long courseId);
Optional<ModelingExercise> findByTitleAndCourseId(String title, long courseId);

@NotNull
default ModelingExercise findWithEagerExampleSubmissionsAndCompetenciesByIdElseThrow(long exerciseId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,9 @@ SELECT COUNT (DISTINCT p)
""")
Optional<ProgrammingExercise> findByIdWithGradingCriteria(@Param("exerciseId") long exerciseId);

Optional<ProgrammingExercise> findByTitleAndCourseId(@Param("title") String title, @Param("courseId") long courseId);
Optional<ProgrammingExercise> findByTitleAndCourseId(String title, long courseId);

Optional<ProgrammingExercise> findByShortNameAndCourseId(@Param("shortName") String shortName, @Param("courseId") long courseId);
Optional<ProgrammingExercise> findByShortNameAndCourseId(String shortName, long courseId);

default ProgrammingExercise findByIdWithGradingCriteriaElseThrow(long exerciseId) {
return getValueElseThrow(findByIdWithGradingCriteria(exerciseId), exerciseId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public interface QuizExerciseRepository extends ArtemisJpaRepository<QuizExercis
@EntityGraph(type = LOAD, attributePaths = { "quizBatches" })
Optional<QuizExercise> findWithEagerBatchesById(Long quizExerciseId);

Optional<QuizExercise> findByTitleAndCourseId(@Param("title") String title, @Param("courseId") long courseId);
Optional<QuizExercise> findByTitleAndCourseId(String title, long courseId);

@NotNull
default QuizExercise findWithEagerBatchesByIdOrElseThrow(Long quizExerciseId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public interface TextExerciseRepository extends ArtemisJpaRepository<TextExercis
@EntityGraph(type = LOAD, attributePaths = { "gradingCriteria" })
Optional<TextExercise> findWithGradingCriteriaById(long exerciseId);

Optional<TextExercise> findByTitleAndCourseId(@Param("title") String title, @Param("courseId") long courseId);
Optional<TextExercise> findByTitleAndCourseId(String title, long courseId);

@NotNull
default TextExercise findWithGradingCriteriaByIdElseThrow(long exerciseId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ public LearningObjectImportService(ExerciseRepository exerciseRepository, Progra
this.courseCompetencyRepository = courseCompetencyRepository;
}

/**
* Imports the related learning objects from the source course competencies into the course to import into and links them to the imported competencies.
*
* @param sourceCourseCompetencies The source course competencies to import from.
* @param idToImportedCompetency A map from the source competency IDs to the imported competencies.
* @param courseToImportInto The course to import the learning objects into.
* @param importOptions The import options.
*/
public void importRelatedLearningObjects(Set<CourseCompetency> sourceCourseCompetencies, Map<Long, CompetencyWithTailRelationDTO> idToImportedCompetency,
Course courseToImportInto, CompetencyImportOptionsDTO importOptions) {
Set<CourseCompetency> importedCourseCompetencies = idToImportedCompetency.values().stream().map(CompetencyWithTailRelationDTO::competency).collect(Collectors.toSet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import static org.mockito.Mockito.verify;

import java.time.ZonedDateTime;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -43,6 +43,7 @@
import de.tum.in.www1.artemis.repository.LectureUnitRepository;
import de.tum.in.www1.artemis.repository.PrerequisiteRepository;
import de.tum.in.www1.artemis.repository.TextUnitRepository;
import de.tum.in.www1.artemis.web.rest.dto.competency.CompetencyImportOptionsDTO;
import de.tum.in.www1.artemis.web.rest.dto.competency.CompetencyImportResponseDTO;
import de.tum.in.www1.artemis.web.rest.dto.competency.CompetencyWithTailRelationDTO;

Expand Down Expand Up @@ -386,11 +387,12 @@ void shouldReturnForbiddenForInstructorOfOtherCourseForCreate(CourseCompetency n
createCall(course.getId(), newCompetency, HttpStatus.FORBIDDEN);
}

abstract CourseCompetency importCall(long courseId, long competencyId, HttpStatus expectedStatus) throws Exception;
abstract CourseCompetency importCall(long courseId, CompetencyImportOptionsDTO importOptions, HttpStatus expectedStatus) throws Exception;

// Test
void shouldImportCompetency() throws Exception {
CourseCompetency importedCompetency = importCall(course2.getId(), competency.getId(), HttpStatus.CREATED);
CompetencyImportOptionsDTO importOptions = new CompetencyImportOptionsDTO(Set.of(competency.getId()), Optional.empty(), false, false, false, Optional.empty(), false);
CourseCompetency importedCompetency = importCall(course2.getId(), importOptions, HttpStatus.CREATED);

assertThat(courseCompetencyRepository.findById(importedCompetency.getId())).isNotEmpty();
assertThat(importedCompetency.getTitle()).isEqualTo(competency.getTitle());
Expand All @@ -405,7 +407,9 @@ void shouldImportCompetency() throws Exception {

// Test
void shouldReturnForbiddenForInstructorOfOtherCourseForImport() throws Exception {
importCall(course.getId(), 42, HttpStatus.FORBIDDEN);
CompetencyImportOptionsDTO importOptions = new CompetencyImportOptionsDTO(Set.of(42L), Optional.empty(), false, false, false, Optional.empty(), false);

importCall(course.getId(), importOptions, HttpStatus.FORBIDDEN);
}

abstract List<? extends CourseCompetency> createBulkCall(long courseId, List<? extends CourseCompetency> competencies, HttpStatus expectedStatus) throws Exception;
Expand Down Expand Up @@ -451,21 +455,23 @@ void shouldReturnForbiddenForInstructorOfOtherCourseForCreateBulk() throws Excep
createBulkCall(course.getId(), List.of(), HttpStatus.FORBIDDEN);
}

abstract List<CompetencyWithTailRelationDTO> importAllCall(long courseId, long sourceCourseId, boolean importRelations, HttpStatus expectedStatus) throws Exception;
abstract List<CompetencyWithTailRelationDTO> importAllCall(long sourceCourseId, CompetencyImportOptionsDTO importOptions, HttpStatus expectedStatus) throws Exception;

// Test
void shouldImportAllCompetencies(Function<Course, CourseCompetency> createCourseCompetencyForCourse) throws Exception {
var course3 = courseUtilService.createCourse();

var competencyDTOList = importAllCall(course.getId(), course3.getId(), false, HttpStatus.CREATED);
CompetencyImportOptionsDTO importOptions = new CompetencyImportOptionsDTO(Set.of(), Optional.of(course3.getId()), false, false, false, Optional.empty(), false);
var competencyDTOList = importAllCall(course.getId(), importOptions, HttpStatus.CREATED);

assertThat(competencyDTOList).isEmpty();

CourseCompetency head = createCourseCompetencyForCourse.apply(course3);
CourseCompetency tail = createCourseCompetencyForCourse.apply(course3);
createRelation(tail, head, RelationType.ASSUMES);

competencyDTOList = importAllCall(course.getId(), course3.getId(), true, HttpStatus.CREATED);
importOptions = new CompetencyImportOptionsDTO(Set.of(), Optional.of(course3.getId()), true, false, false, Optional.empty(), false);
competencyDTOList = importAllCall(course.getId(), importOptions, HttpStatus.CREATED);

assertThat(competencyDTOList).hasSize(2);
// assert that only one of the DTOs has the relation connected
Expand All @@ -476,7 +482,8 @@ void shouldImportAllCompetencies(Function<Course, CourseCompetency> createCourse
assertThat(competencyDTOList.get(1).tailRelations()).isNull();
}

competencyDTOList = importAllCall(course.getId(), course3.getId(), false, HttpStatus.CREATED);
importOptions = new CompetencyImportOptionsDTO(Set.of(), Optional.of(course3.getId()), false, false, false, Optional.empty(), false);
competencyDTOList = importAllCall(course.getId(), importOptions, HttpStatus.CREATED);
assertThat(competencyDTOList).hasSize(2);
// relations should be empty when not importing them
assertThat(competencyDTOList.getFirst().tailRelations()).isNull();
Expand All @@ -485,12 +492,14 @@ void shouldImportAllCompetencies(Function<Course, CourseCompetency> createCourse

// Test
void shouldReturnForbiddenForInstructorNotInCourse() throws Exception {
importAllCall(course.getId(), course2.getId(), false, HttpStatus.FORBIDDEN);
CompetencyImportOptionsDTO importOptions = new CompetencyImportOptionsDTO(Set.of(), Optional.of(course2.getId()), false, false, false, Optional.empty(), false);
importAllCall(course.getId(), importOptions, HttpStatus.FORBIDDEN);
}

// Test
void shouldReturnBadRequestForImportFromSameCourse() throws Exception {
importAllCall(course.getId(), course.getId(), false, HttpStatus.BAD_REQUEST);
CompetencyImportOptionsDTO importOptions = new CompetencyImportOptionsDTO(Set.of(), Optional.of(course.getId()), false, false, false, Optional.empty(), false);
importAllCall(course.getId(), importOptions, HttpStatus.BAD_REQUEST);
}

abstract List<CompetencyImportResponseDTO> importStandardizedCall(long courseId, List<Long> idList, HttpStatus expectedStatus) throws Exception;
Expand Down Expand Up @@ -526,24 +535,23 @@ void shouldReturnNotFoundForNotExistingIds() throws Exception {
importStandardizedCall(course.getId(), idList, HttpStatus.NOT_FOUND);
}

abstract List<CompetencyWithTailRelationDTO> importBulkCall(long courseId, Set<Long> competencyIds, boolean importRelations, HttpStatus expectedStatus) throws Exception;
abstract List<CompetencyWithTailRelationDTO> importBulkCall(long courseId, CompetencyImportOptionsDTO importOptions, HttpStatus expectedStatus) throws Exception;

// Test
void shouldReturnForbiddenForInstructorOfOtherCourseForBulkImport() throws Exception {
importBulkCall(course.getId(), Set.of(), false, HttpStatus.FORBIDDEN);
CompetencyImportOptionsDTO importOptions = new CompetencyImportOptionsDTO(Set.of(), Optional.empty(), false, false, false, Optional.empty(), false);
importBulkCall(course.getId(), importOptions, HttpStatus.FORBIDDEN);
}

// Test
void shouldImportCompetencies(Function<Course, CourseCompetency> createCourseCompetencyForCourse) throws Exception {
var competencyDTOList = importBulkCall(course.getId(), Collections.emptySet(), false, HttpStatus.CREATED);
assertThat(competencyDTOList).isEmpty();

CourseCompetency head = createCourseCompetencyForCourse.apply(course2);
CourseCompetency tail = createCourseCompetencyForCourse.apply(course2);
createRelation(tail, head, RelationType.ASSUMES);
Set<Long> competencyIds = Set.of(head.getId(), tail.getId());

competencyDTOList = importBulkCall(course.getId(), competencyIds, true, HttpStatus.CREATED);
var importOptions = new CompetencyImportOptionsDTO(competencyIds, Optional.empty(), true, false, false, Optional.empty(), false);
var competencyDTOList = importBulkCall(course.getId(), importOptions, HttpStatus.CREATED);

assertThat(competencyDTOList).hasSize(2);
// competency 2 should be the tail of one relation
Expand All @@ -556,7 +564,8 @@ void shouldImportCompetencies(Function<Course, CourseCompetency> createCourseCom
assertThat(competencyDTOList.get(1).tailRelations()).hasSize(1);
}

competencyDTOList = importBulkCall(course.getId(), competencyIds, false, HttpStatus.CREATED);
importOptions = new CompetencyImportOptionsDTO(competencyIds, Optional.empty(), false, false, false, Optional.empty(), false);
competencyDTOList = importBulkCall(course.getId(), importOptions, HttpStatus.CREATED);
assertThat(competencyDTOList).hasSize(2);
// relations should be empty when not importing them
assertThat(competencyDTOList.getFirst().tailRelations()).isNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
Expand All @@ -16,6 +15,7 @@
import de.tum.in.www1.artemis.domain.competency.Competency;
import de.tum.in.www1.artemis.domain.competency.CourseCompetency;
import de.tum.in.www1.artemis.domain.enumeration.IncludedInOverallScore;
import de.tum.in.www1.artemis.web.rest.dto.competency.CompetencyImportOptionsDTO;
import de.tum.in.www1.artemis.web.rest.dto.competency.CompetencyImportResponseDTO;
import de.tum.in.www1.artemis.web.rest.dto.competency.CompetencyWithTailRelationDTO;

Expand All @@ -35,7 +35,8 @@ void setupTestScenario() {
class PreAuthorize {

private void testAllPreAuthorizeEditor() throws Exception {
request.post("/api/courses/" + course.getId() + "/competencies/import/bulk", Collections.emptyList(), HttpStatus.FORBIDDEN);
request.post("/api/courses/" + course.getId() + "/competencies/import/bulk", new CompetencyImportOptionsDTO(null, null, false, false, false, null, false),
HttpStatus.FORBIDDEN);
request.post("/api/courses/" + course.getId() + "/competencies/import-standardized", Collections.emptyList(), HttpStatus.FORBIDDEN);
}

Expand All @@ -45,8 +46,10 @@ private void testAllPreAuthorizeInstructor() throws Exception {
request.delete("/api/courses/" + course.getId() + "/competencies/" + competency.getId(), HttpStatus.FORBIDDEN);
request.post("/api/courses/" + course.getId() + "/competencies/bulk", Collections.emptyList(), HttpStatus.FORBIDDEN);
// import
request.post("/api/courses/" + course.getId() + "/competencies/import-all/1", null, HttpStatus.FORBIDDEN);
request.post("/api/courses/" + course.getId() + "/competencies/import", competency.getId(), HttpStatus.FORBIDDEN);
request.post("/api/courses/" + course.getId() + "/competencies/import-all", new CompetencyImportOptionsDTO(null, null, false, false, false, null, false),
HttpStatus.FORBIDDEN);
request.post("/api/courses/" + course.getId() + "/competencies/import", new CompetencyImportOptionsDTO(null, null, false, false, false, null, false),
HttpStatus.FORBIDDEN);
}

@Test
Expand Down Expand Up @@ -213,8 +216,8 @@ void shouldReturnForbiddenForInstructorOfOtherCourseForCreate() throws Exception
super.shouldReturnForbiddenForInstructorOfOtherCourseForCreate(new Competency());
}

CourseCompetency importCall(long courseId, long competencyId, HttpStatus expectedStatus) throws Exception {
return request.postWithResponseBody("/api/courses/" + courseId + "/competencies/import", competencyId, Competency.class, expectedStatus);
CourseCompetency importCall(long courseId, CompetencyImportOptionsDTO importOptions, HttpStatus expectedStatus) throws Exception {
return request.postWithResponseBody("/api/courses/" + courseId + "/competencies/import", importOptions, Competency.class, expectedStatus);
}

@Test
Expand Down Expand Up @@ -263,9 +266,8 @@ void shouldReturnForbiddenForInstructorOfOtherCourseForCreateBulk() throws Excep
super.shouldReturnForbiddenForInstructorOfOtherCourseForCreateBulk();
}

List<CompetencyWithTailRelationDTO> importAllCall(long courseId, long sourceCourseId, boolean importRelations, HttpStatus expectedStatus) throws Exception {
return request.postListWithResponseBody("/api/courses/" + courseId + "/competencies/import-all/" + sourceCourseId + (importRelations ? "?importRelations=true" : ""), null,
CompetencyWithTailRelationDTO.class, expectedStatus);
List<CompetencyWithTailRelationDTO> importAllCall(long courseId, CompetencyImportOptionsDTO importOptions, HttpStatus expectedStatus) throws Exception {
return request.postListWithResponseBody("/api/courses/" + courseId + "/competencies/import-all", importOptions, CompetencyWithTailRelationDTO.class, expectedStatus);
}

@Test
Expand Down Expand Up @@ -302,9 +304,8 @@ void shouldReturnNotFoundForNotExistingIds() throws Exception {
super.shouldReturnNotFoundForNotExistingIds();
}

List<CompetencyWithTailRelationDTO> importBulkCall(long courseId, Set<Long> competencyIds, boolean importRelations, HttpStatus expectedStatus) throws Exception {
return request.postListWithResponseBody("/api/courses/" + courseId + "/competencies/import/bulk" + (importRelations ? "?importRelations=true" : ""), competencyIds,
CompetencyWithTailRelationDTO.class, expectedStatus);
List<CompetencyWithTailRelationDTO> importBulkCall(long courseId, CompetencyImportOptionsDTO importOptions, HttpStatus expectedStatus) throws Exception {
return request.postListWithResponseBody("/api/courses/" + courseId + "/competencies/import/bulk", importOptions, CompetencyWithTailRelationDTO.class, expectedStatus);
}

@Test
Expand Down
Loading

0 comments on commit 07e3972

Please sign in to comment.