From 05646bf8fa91b40c8a1649fcff3abda90d2abfeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kaan=20=C3=87ayl=C4=B1?= <38523756+kaancayli@users.noreply.github.com> Date: Tue, 3 Dec 2024 10:50:52 +0100 Subject: [PATCH] Development: Fix exercise deletion with existing Iris messages (#9875) --- .../changelog/20241125000900_changelog.xml | 66 ++++++++++++++ .../resources/config/liquibase/master.xml | 1 + ...risExerciseChatSessionIntegrationTest.java | 91 +++++++++++++++++++ 3 files changed, 158 insertions(+) create mode 100644 src/main/resources/config/liquibase/changelog/20241125000900_changelog.xml diff --git a/src/main/resources/config/liquibase/changelog/20241125000900_changelog.xml b/src/main/resources/config/liquibase/changelog/20241125000900_changelog.xml new file mode 100644 index 000000000000..dc9ee6d41854 --- /dev/null +++ b/src/main/resources/config/liquibase/changelog/20241125000900_changelog.xml @@ -0,0 +1,66 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/config/liquibase/master.xml b/src/main/resources/config/liquibase/master.xml index d331337ceef4..e8f58b18d024 100644 --- a/src/main/resources/config/liquibase/master.xml +++ b/src/main/resources/config/liquibase/master.xml @@ -36,6 +36,7 @@ + diff --git a/src/test/java/de/tum/cit/aet/artemis/iris/IrisExerciseChatSessionIntegrationTest.java b/src/test/java/de/tum/cit/aet/artemis/iris/IrisExerciseChatSessionIntegrationTest.java index aeaa3fee33bc..5a979a2114dc 100644 --- a/src/test/java/de/tum/cit/aet/artemis/iris/IrisExerciseChatSessionIntegrationTest.java +++ b/src/test/java/de/tum/cit/aet/artemis/iris/IrisExerciseChatSessionIntegrationTest.java @@ -3,6 +3,8 @@ import static org.assertj.core.api.Assertions.assertThat; import java.util.List; +import java.util.Map; +import java.util.concurrent.ThreadLocalRandom; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -10,11 +12,21 @@ import org.springframework.http.HttpStatus; import org.springframework.security.test.context.support.WithMockUser; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; + import de.tum.cit.aet.artemis.core.domain.Course; +import de.tum.cit.aet.artemis.iris.domain.message.IrisJsonMessageContent; +import de.tum.cit.aet.artemis.iris.domain.message.IrisMessage; +import de.tum.cit.aet.artemis.iris.domain.message.IrisMessageContent; +import de.tum.cit.aet.artemis.iris.domain.message.IrisMessageSender; +import de.tum.cit.aet.artemis.iris.domain.message.IrisTextMessageContent; import de.tum.cit.aet.artemis.iris.domain.session.IrisExerciseChatSession; import de.tum.cit.aet.artemis.iris.domain.session.IrisSession; import de.tum.cit.aet.artemis.iris.dto.IrisStatusDTO; import de.tum.cit.aet.artemis.iris.repository.IrisExerciseChatSessionRepository; +import de.tum.cit.aet.artemis.iris.repository.IrisMessageRepository; +import de.tum.cit.aet.artemis.iris.service.IrisMessageService; import de.tum.cit.aet.artemis.programming.domain.ProgrammingExercise; class IrisExerciseChatSessionIntegrationTest extends AbstractIrisIntegrationTest { @@ -24,6 +36,12 @@ class IrisExerciseChatSessionIntegrationTest extends AbstractIrisIntegrationTest @Autowired private IrisExerciseChatSessionRepository irisExerciseChatSessionRepository; + @Autowired + private IrisMessageService irisMessageService; + + @Autowired + private IrisMessageRepository irisMessageRepository; + private ProgrammingExercise exercise; @BeforeEach @@ -37,6 +55,41 @@ void initTestCase() { activateIrisFor(exercise); } + private IrisExerciseChatSession createSessionForUser(String userLogin) { + var user = userUtilService.getUserByLogin(TEST_PREFIX + userLogin); + return irisExerciseChatSessionRepository.save(new IrisExerciseChatSession(exercise, user)); + } + + private IrisMessage createDefaultMockTextMessage(IrisSession irisSession) { + var messageToSend = irisSession.newMessage(); + messageToSend.addContent(createMockTextContent(), createMockTextContent(), createMockTextContent()); + return messageToSend; + } + + private IrisMessageContent createMockTextContent() { + String[] adjectives = { "happy", "sad", "angry", "funny", "silly", "crazy", "beautiful", "smart" }; + String[] nouns = { "dog", "cat", "house", "car", "book", "computer", "phone", "shoe" }; + + var rdm = ThreadLocalRandom.current(); + String randomAdjective = adjectives[rdm.nextInt(adjectives.length)]; + String randomNoun = nouns[rdm.nextInt(nouns.length)]; + + var text = "The " + randomAdjective + " " + randomNoun + " jumped over the lazy dog."; + return new IrisTextMessageContent(text); + } + + private IrisMessage createDefaultMockJsonMessage(IrisSession irisSession) { + var messageToSend = irisSession.newMessage(); + messageToSend.addContent(createMockJsonContent(), createMockJsonContent(), createMockJsonContent()); + return messageToSend; + } + + private IrisJsonMessageContent createMockJsonContent() { + var jsonMap = Map.of("key1", "value1", "key2", "value2", "key3", "value3"); + JsonNode jsonNode = new ObjectMapper().valueToTree(jsonMap); + return new IrisJsonMessageContent(jsonNode); + } + @Test @WithMockUser(username = TEST_PREFIX + "student1", roles = "USER") void createSession() throws Exception { @@ -101,6 +154,44 @@ void testDeleteExerciseWithIrisSession() throws Exception { assertThat(irisExerciseChatSessionRepository.findById(irisSession.getId())).isEmpty(); } + @Test + @WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR") + void testDeleteExerciseWithIrisMessagesWithTextMessageContent() throws Exception { + var irisSession = createSessionForUser("instructor1"); + + // Create and some messages to the session + irisMessageService.saveMessage(createDefaultMockTextMessage(irisSession), irisSession, IrisMessageSender.USER); + irisMessageService.saveMessage(createDefaultMockTextMessage(irisSession), irisSession, IrisMessageSender.LLM); + irisMessageService.saveMessage(createDefaultMockTextMessage(irisSession), irisSession, IrisMessageSender.USER); + irisMessageService.saveMessage(createDefaultMockTextMessage(irisSession), irisSession, IrisMessageSender.LLM); + + assertThat(irisExerciseChatSessionRepository.findByIdElseThrow(irisSession.getId())).isNotNull(); + // Set the URL request parameters to prevent an internal server error which is irrelevant for this test + var url = "/api/programming-exercises/" + exercise.getId() + "?deleteStudentReposBuildPlans=false&deleteBaseReposBuildPlans=false"; + request.delete(url, HttpStatus.OK); + assertThat(irisExerciseChatSessionRepository.findById(irisSession.getId())).isEmpty(); + assertThat(irisMessageRepository.findAllBySessionId(irisSession.getId())).isEmpty(); + } + + @Test + @WithMockUser(username = TEST_PREFIX + "instructor1", roles = "INSTRUCTOR") + void testDeleteExerciseWithIrisMessagesWithJsonMessageContent() throws Exception { + var irisSession = createSessionForUser("instructor1"); + + // Create and some messages to the session + irisMessageService.saveMessage(createDefaultMockJsonMessage(irisSession), irisSession, IrisMessageSender.USER); + irisMessageService.saveMessage(createDefaultMockJsonMessage(irisSession), irisSession, IrisMessageSender.LLM); + irisMessageService.saveMessage(createDefaultMockJsonMessage(irisSession), irisSession, IrisMessageSender.USER); + irisMessageService.saveMessage(createDefaultMockJsonMessage(irisSession), irisSession, IrisMessageSender.LLM); + + assertThat(irisExerciseChatSessionRepository.findByIdElseThrow(irisSession.getId())).isNotNull(); + // Set the URL request parameters to prevent an internal server error which is irrelevant for this test + var url = "/api/programming-exercises/" + exercise.getId() + "?deleteStudentReposBuildPlans=false&deleteBaseReposBuildPlans=false"; + request.delete(url, HttpStatus.OK); + assertThat(irisExerciseChatSessionRepository.findById(irisSession.getId())).isEmpty(); + assertThat(irisMessageRepository.findAllBySessionId(irisSession.getId())).isEmpty(); + } + private static String exerciseChatUrl(long sessionId) { return "/api/iris/exercise-chat/" + sessionId + "/sessions"; }