diff --git a/src/main/java/de/tum/cit/aet/artemis/assessment/repository/LongFeedbackTextRepository.java b/src/main/java/de/tum/cit/aet/artemis/assessment/repository/LongFeedbackTextRepository.java index 4e0c502a75db..f3a8909a6978 100644 --- a/src/main/java/de/tum/cit/aet/artemis/assessment/repository/LongFeedbackTextRepository.java +++ b/src/main/java/de/tum/cit/aet/artemis/assessment/repository/LongFeedbackTextRepository.java @@ -52,6 +52,10 @@ public interface LongFeedbackTextRepository extends ArtemisJpaRepository feedbackIds); + @Modifying + @Transactional + void deleteByFeedbackId(final Long feedbackId); + default LongFeedbackText findByFeedbackIdWithFeedbackAndResultAndParticipationElseThrow(final Long feedbackId) { return getValueElseThrow(findWithFeedbackAndResultAndParticipationByFeedbackId(feedbackId), feedbackId); } diff --git a/src/main/java/de/tum/cit/aet/artemis/assessment/service/ResultService.java b/src/main/java/de/tum/cit/aet/artemis/assessment/service/ResultService.java index 92b1f6e19030..7c91008842c4 100644 --- a/src/main/java/de/tum/cit/aet/artemis/assessment/service/ResultService.java +++ b/src/main/java/de/tum/cit/aet/artemis/assessment/service/ResultService.java @@ -522,23 +522,26 @@ private void handleFeedbackPersistence(Feedback feedback, Result result, Map feedbacks = new ArrayList<>(); var manualLongFeedback = new Feedback().credits(1.00).type(FeedbackType.MANUAL_UNREFERENCED); manualLongFeedback.setDetailText("abc".repeat(5000)); @@ -553,6 +554,54 @@ void updateManualProgrammingExerciseResult_addFeedbackAfterManualLongFeedback() assertThat(savedAutomaticLongFeedback.getDetailText()).isEqualTo(manualLongFeedback.getDetailText()); } + @ParameterizedTest + @ValueSource(booleans = { true, false }) + @WithMockUser(username = TEST_PREFIX + "tutor1", roles = "TA") + void shouldKeepExistingLongFeedbackWhenSavingAnAssessment(boolean submit) throws Exception { + var manualLongFeedback = new Feedback().credits(0.0); + var longText = "abc".repeat(5000); + manualLongFeedback.setDetailText(longText); + var result = new Result().feedbacks(List.of(manualLongFeedback)).score(0.0); + result = resultRepository.save(result); + + LinkedMultiValueMap params = new LinkedMultiValueMap<>(); + params.add("submit", String.valueOf(submit)); + result = request.putWithResponseBodyAndParams("/api/participations/" + programmingExerciseStudentParticipation.getId() + "/manual-results", result, Result.class, + HttpStatus.OK, params); + + var longFeedbackText = longFeedbackTextRepository.findByFeedbackId(result.getFeedbacks().getFirst().getId()); + assertThat(longFeedbackText).isPresent(); + assertThat(longFeedbackText.get().getText()).isEqualTo(longText); + } + + @ParameterizedTest + @ValueSource(booleans = { true, false }) + @WithMockUser(username = TEST_PREFIX + "tutor1", roles = "TA") + void shouldUpdateUnreferencedLongFeedbackWhenSavingAnAssessment(boolean submit) throws Exception { + var manualLongFeedback = new Feedback().credits(0.0).type(FeedbackType.MANUAL_UNREFERENCED); + var longText = "abc".repeat(5000); + manualLongFeedback.setDetailText(longText); + var result = new Result().feedbacks(List.of(manualLongFeedback)).score(0.0); + result = resultRepository.save(result); + + var newLongText = "def".repeat(5000); + manualLongFeedback = result.getFeedbacks().getFirst(); + + // The actual complete longtext is still stored in the detailText field when the result is sent from the client + var detailText = Feedback.class.getDeclaredField("detailText"); + detailText.setAccessible(true); + detailText.set(manualLongFeedback, newLongText); + + LinkedMultiValueMap params = new LinkedMultiValueMap<>(); + params.add("submit", String.valueOf(submit)); + result = request.putWithResponseBodyAndParams("/api/participations/" + programmingExerciseStudentParticipation.getId() + "/manual-results", result, Result.class, + HttpStatus.OK, params); + + var longFeedbackText = longFeedbackTextRepository.findByFeedbackId(result.getFeedbacks().getFirst().getId()); + assertThat(longFeedbackText).isPresent(); + assertThat(longFeedbackText.get().getText()).isEqualTo(newLongText); + } + @Test @WithMockUser(username = TEST_PREFIX + "tutor1", roles = "TA") void updateManualProgrammingExerciseResult_addFeedbackAfterAutomaticLongFeedback() throws Exception {