From 66282ab2d9aeb3b27fea934a411ac1fe3e3f6173 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20St=C3=B6hr?= Date: Tue, 1 Oct 2024 14:54:33 +0200 Subject: [PATCH 1/2] Change to input field and validate --- .../artemis/atlas/web/CompetencyResource.java | 35 +++++++++++++------ .../atlas/web/PrerequisiteResource.java | 35 +++++++++++++------ ...mmon-course-competency-form.component.html | 2 +- 3 files changed, 51 insertions(+), 21 deletions(-) diff --git a/src/main/java/de/tum/cit/aet/artemis/atlas/web/CompetencyResource.java b/src/main/java/de/tum/cit/aet/artemis/atlas/web/CompetencyResource.java index aa240c5f9a42..f70f110f1767 100644 --- a/src/main/java/de/tum/cit/aet/artemis/atlas/web/CompetencyResource.java +++ b/src/main/java/de/tum/cit/aet/artemis/atlas/web/CompetencyResource.java @@ -8,7 +8,6 @@ import java.util.Set; import jakarta.validation.constraints.NotNull; -import jakarta.ws.rs.BadRequestException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -136,9 +135,8 @@ public ResponseEntity getCompetency(@PathVariable long competencyId, @EnforceAtLeastInstructorInCourse public ResponseEntity createCompetency(@PathVariable long courseId, @RequestBody Competency competency) throws URISyntaxException { log.debug("REST request to create Competency : {}", competency); - if (competency.getId() != null || competency.getTitle() == null || competency.getTitle().trim().isEmpty()) { - throw new BadRequestException(); - } + checkCompetencyAttributesForCreation(competency); + var course = courseRepository.findWithEagerCompetenciesAndPrerequisitesByIdElseThrow(courseId); final var persistedCompetency = competencyService.createCourseCompetency(competency, course); @@ -159,9 +157,7 @@ public ResponseEntity createCompetency(@PathVariable long courseId, public ResponseEntity> createCompetencies(@PathVariable Long courseId, @RequestBody List competencies) throws URISyntaxException { log.debug("REST request to create Competencies : {}", competencies); for (Competency competency : competencies) { - if (competency.getId() != null || competency.getTitle() == null || competency.getTitle().trim().isEmpty()) { - throw new BadRequestException(); - } + checkCompetencyAttributesForCreation(competency); } var course = courseRepository.findWithEagerCompetenciesAndPrerequisitesByIdElseThrow(courseId); @@ -300,9 +296,8 @@ public ResponseEntity> importStandardizedCompe @EnforceAtLeastInstructorInCourse public ResponseEntity updateCompetency(@PathVariable long courseId, @RequestBody Competency competency) { log.debug("REST request to update Competency : {}", competency); - if (competency.getId() == null) { - throw new BadRequestException(); - } + checkCompetencyAttributesForUpdate(competency); + var course = courseRepository.findByIdElseThrow(courseId); var existingCompetency = competencyRepository.findByIdWithLectureUnitsElseThrow(competency.getId()); checkCourseForCompetency(course, existingCompetency); @@ -334,6 +329,26 @@ public ResponseEntity deleteCompetency(@PathVariable long competencyId, @P return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(applicationName, true, ENTITY_NAME, competency.getTitle())).build(); } + private void checkCompetencyAttributesForCreation(Competency competency) { + if (competency.getId() != null) { + throw new BadRequestAlertException("A new competency should not have an id", ENTITY_NAME, "existingCompetencyId"); + } + checkCompetencyAttributes(competency); + } + + private void checkCompetencyAttributesForUpdate(Competency competency) { + if (competency.getId() == null) { + throw new BadRequestAlertException("An updated competency should have an id", ENTITY_NAME, "missingCompetencyId"); + } + checkCompetencyAttributes(competency); + } + + private void checkCompetencyAttributes(Competency competency) { + if (competency.getTitle() == null || competency.getTitle().trim().isEmpty() || competency.getMasteryThreshold() < 1 || competency.getMasteryThreshold() > 100) { + throw new BadRequestAlertException("The attributes of the competency are invalid!", ENTITY_NAME, "invalidPrerequisiteAttributes"); + } + } + /** * Checks if the competency matches the course. * diff --git a/src/main/java/de/tum/cit/aet/artemis/atlas/web/PrerequisiteResource.java b/src/main/java/de/tum/cit/aet/artemis/atlas/web/PrerequisiteResource.java index 7cd32bdd957a..a8edbc4e128e 100644 --- a/src/main/java/de/tum/cit/aet/artemis/atlas/web/PrerequisiteResource.java +++ b/src/main/java/de/tum/cit/aet/artemis/atlas/web/PrerequisiteResource.java @@ -8,7 +8,6 @@ import java.util.Set; import jakarta.validation.constraints.NotNull; -import jakarta.ws.rs.BadRequestException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -139,9 +138,8 @@ public ResponseEntity getPrerequisite(@PathVariable long prerequis @EnforceAtLeastInstructorInCourse public ResponseEntity createPrerequisite(@PathVariable long courseId, @RequestBody Prerequisite prerequisite) throws URISyntaxException { log.debug("REST request to create Prerequisite : {}", prerequisite); - if (prerequisite.getId() != null || prerequisite.getTitle() == null || prerequisite.getTitle().trim().isEmpty()) { - throw new BadRequestException(); - } + checkPrerequisitesAttributesForCreation(prerequisite); + var course = courseRepository.findWithEagerCompetenciesAndPrerequisitesByIdElseThrow(courseId); final var persistedPrerequisite = prerequisiteService.createCourseCompetency(prerequisite, course); @@ -162,9 +160,7 @@ public ResponseEntity createPrerequisite(@PathVariable long course public ResponseEntity> createPrerequisite(@PathVariable Long courseId, @RequestBody List prerequisites) throws URISyntaxException { log.debug("REST request to create Prerequisites : {}", prerequisites); for (Prerequisite prerequisite : prerequisites) { - if (prerequisite.getId() != null || prerequisite.getTitle() == null || prerequisite.getTitle().trim().isEmpty()) { - throw new BadRequestException(); - } + checkPrerequisitesAttributesForCreation(prerequisite); } var course = courseRepository.findWithEagerCompetenciesAndPrerequisitesByIdElseThrow(courseId); @@ -303,9 +299,8 @@ public ResponseEntity> importStandardizedPrere @EnforceAtLeastInstructorInCourse public ResponseEntity updatePrerequisite(@PathVariable long courseId, @RequestBody Prerequisite prerequisite) { log.debug("REST request to update Prerequisite : {}", prerequisite); - if (prerequisite.getId() == null) { - throw new BadRequestException(); - } + checkPrerequisitesAttributesForUpdate(prerequisite); + var course = courseRepository.findByIdElseThrow(courseId); var existingPrerequisite = prerequisiteRepository.findByIdWithLectureUnitsElseThrow(prerequisite.getId()); checkCourseForPrerequisite(course, existingPrerequisite); @@ -337,6 +332,26 @@ public ResponseEntity deletePrerequisite(@PathVariable long prerequisiteId return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(applicationName, true, ENTITY_NAME, prerequisite.getTitle())).build(); } + private void checkPrerequisitesAttributesForCreation(Prerequisite prerequisite) { + if (prerequisite.getId() != null) { + throw new BadRequestAlertException("A new prerequiste should not have an id", ENTITY_NAME, "existingPrerequisiteId"); + } + checkPrerequisitesAttributes(prerequisite); + } + + private void checkPrerequisitesAttributesForUpdate(Prerequisite prerequisite) { + if (prerequisite.getId() == null) { + throw new BadRequestAlertException("An updated prerequiste should have an id", ENTITY_NAME, "missingPrerequisiteId"); + } + checkPrerequisitesAttributes(prerequisite); + } + + private void checkPrerequisitesAttributes(Prerequisite prerequisite) { + if (prerequisite.getTitle() == null || prerequisite.getTitle().trim().isEmpty() || prerequisite.getMasteryThreshold() < 1 || prerequisite.getMasteryThreshold() > 100) { + throw new BadRequestAlertException("The attributes of the competency are invalid!", ENTITY_NAME, "invalidPrerequisiteAttributes"); + } + } + /** * Checks if the prerequisite matches the course. * diff --git a/src/main/webapp/app/course/competencies/forms/common-course-competency-form.component.html b/src/main/webapp/app/course/competencies/forms/common-course-competency-form.component.html index c27066b26924..77a39956b91c 100644 --- a/src/main/webapp/app/course/competencies/forms/common-course-competency-form.component.html +++ b/src/main/webapp/app/course/competencies/forms/common-course-competency-form.component.html @@ -73,7 +73,7 @@ ({{ 'artemisApp.courseCompetency.create.averageMastery' | artemisTranslate }}: {{ averageStudentScore }}%) } - + }
From 0ab585dba92a06f49aaf613a9279924a2a721d7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20St=C3=B6hr?= Date: Tue, 1 Oct 2024 15:27:07 +0200 Subject: [PATCH 2/2] Fix tests --- .../AbstractCompetencyPrerequisiteIntegrationTest.java | 4 ++++ .../artemis/atlas/competency/util/CompetencyUtilService.java | 1 + .../atlas/competency/util/PrerequisiteUtilService.java | 1 + .../atlas/learningpath/LearningPathIntegrationTest.java | 1 + 4 files changed, 7 insertions(+) diff --git a/src/test/java/de/tum/cit/aet/artemis/atlas/competency/AbstractCompetencyPrerequisiteIntegrationTest.java b/src/test/java/de/tum/cit/aet/artemis/atlas/competency/AbstractCompetencyPrerequisiteIntegrationTest.java index 899bcaa60ec7..53ce6c1bcfcf 100644 --- a/src/test/java/de/tum/cit/aet/artemis/atlas/competency/AbstractCompetencyPrerequisiteIntegrationTest.java +++ b/src/test/java/de/tum/cit/aet/artemis/atlas/competency/AbstractCompetencyPrerequisiteIntegrationTest.java @@ -333,6 +333,7 @@ void shouldUpdateCompetencyToOptionalWhenSettingOptional(CourseCompetency newCom newCompetency.setTitle("Title"); newCompetency.setDescription("Description"); newCompetency.setCourse(course); + newCompetency.setMasteryThreshold(42); newCompetency = courseCompetencyRepository.save(newCompetency); TextExercise exercise = TextExerciseFactory.generateTextExercise(ZonedDateTime.now(), ZonedDateTime.now(), ZonedDateTime.now(), course); @@ -355,6 +356,7 @@ void shouldCreateValidCompetency(CourseCompetency newCompetency) throws Exceptio newCompetency.setTitle("FreshlyCreatedCompetency"); newCompetency.setDescription("This is an example of a freshly created competency"); newCompetency.setCourse(course); + newCompetency.setMasteryThreshold(42); List allLectureUnits = lectureUnitRepository.findAll(); Set connectedLectureUnits = new HashSet<>(allLectureUnits); newCompetency.setLectureUnits(connectedLectureUnits); @@ -419,10 +421,12 @@ void shouldCreateCompetencies(CourseCompetency competency1, CourseCompetency com competency1.setDescription("This is an example competency"); competency1.setTaxonomy(CompetencyTaxonomy.UNDERSTAND); competency1.setCourse(course); + competency1.setMasteryThreshold(42); competency2.setTitle("Competency2"); competency2.setDescription("This is another example competency"); competency2.setTaxonomy(CompetencyTaxonomy.REMEMBER); competency2.setCourse(course); + competency2.setMasteryThreshold(84); var competenciesToCreate = List.of(competency1, competency2); diff --git a/src/test/java/de/tum/cit/aet/artemis/atlas/competency/util/CompetencyUtilService.java b/src/test/java/de/tum/cit/aet/artemis/atlas/competency/util/CompetencyUtilService.java index cd5121642ce2..eb9fad1e4d3e 100644 --- a/src/test/java/de/tum/cit/aet/artemis/atlas/competency/util/CompetencyUtilService.java +++ b/src/test/java/de/tum/cit/aet/artemis/atlas/competency/util/CompetencyUtilService.java @@ -56,6 +56,7 @@ private Competency createCompetency(Course course, String suffix) { competency.setDescription("Magna pars studiorum, prodita quaerimus."); competency.setTaxonomy(CompetencyTaxonomy.UNDERSTAND); competency.setCourse(course); + competency.setMasteryThreshold(42); return competencyRepo.save(competency); } diff --git a/src/test/java/de/tum/cit/aet/artemis/atlas/competency/util/PrerequisiteUtilService.java b/src/test/java/de/tum/cit/aet/artemis/atlas/competency/util/PrerequisiteUtilService.java index e23ce52e6579..edf6e9378286 100644 --- a/src/test/java/de/tum/cit/aet/artemis/atlas/competency/util/PrerequisiteUtilService.java +++ b/src/test/java/de/tum/cit/aet/artemis/atlas/competency/util/PrerequisiteUtilService.java @@ -30,6 +30,7 @@ public Prerequisite createPrerequisite(Course course) { prerequisite.setTitle("Example Prerequisite"); prerequisite.setDescription("Magna pars studiorum, prodita quaerimus."); prerequisite.setCourse(course); + prerequisite.setMasteryThreshold(42); return prerequisiteRepository.save(prerequisite); } diff --git a/src/test/java/de/tum/cit/aet/artemis/atlas/learningpath/LearningPathIntegrationTest.java b/src/test/java/de/tum/cit/aet/artemis/atlas/learningpath/LearningPathIntegrationTest.java index fb0bf6dcb286..9749d0ffa384 100644 --- a/src/test/java/de/tum/cit/aet/artemis/atlas/learningpath/LearningPathIntegrationTest.java +++ b/src/test/java/de/tum/cit/aet/artemis/atlas/learningpath/LearningPathIntegrationTest.java @@ -193,6 +193,7 @@ private Competency createCompetencyRESTCall() throws Exception { competencyToCreate.setTitle("CompetencyToCreateTitle"); competencyToCreate.setCourse(course); competencyToCreate.setLectureUnits(Set.of(textUnit)); + competencyToCreate.setMasteryThreshold(42); return request.postWithResponseBody("/api/courses/" + course.getId() + "/competencies", competencyToCreate, Competency.class, HttpStatus.CREATED); }