From 4db400a308cca6a9e9667606f2b4a1e331b115e0 Mon Sep 17 00:00:00 2001 From: Timor Morrien Date: Thu, 31 Aug 2023 00:57:33 +0200 Subject: [PATCH 1/8] Fix server tests --- .../service/iris/IrisWebsocketService.java | 25 +++++++++++++++++++ .../web/rest/iris/IrisSettingsResource.java | 1 + .../iris/AbstractIrisIntegrationTest.java | 18 ++++++------- .../iris/IrisMessageIntegrationTest.java | 14 +++++++++-- .../www1/artemis/iris/IrisWebsocketTest.java | 3 ++- 5 files changed, 48 insertions(+), 13 deletions(-) diff --git a/src/main/java/de/tum/in/www1/artemis/service/iris/IrisWebsocketService.java b/src/main/java/de/tum/in/www1/artemis/service/iris/IrisWebsocketService.java index 21cd7e431377..13ded18a2503 100644 --- a/src/main/java/de/tum/in/www1/artemis/service/iris/IrisWebsocketService.java +++ b/src/main/java/de/tum/in/www1/artemis/service/iris/IrisWebsocketService.java @@ -2,6 +2,7 @@ import java.util.Collections; import java.util.Map; +import java.util.Objects; import org.springframework.stereotype.Service; @@ -110,5 +111,29 @@ public Map getTranslationParams() { public enum IrisWebsocketMessageType { MESSAGE, ERROR } + + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + if (other == null || getClass() != other.getClass()) { + return false; + } + IrisWebsocketDTO that = (IrisWebsocketDTO) other; + return type == that.type && Objects.equals(message, that.message) && Objects.equals(errorMessage, that.errorMessage) + && Objects.equals(errorTranslationKey, that.errorTranslationKey) && Objects.equals(translationParams, that.translationParams); + } + + @Override + public int hashCode() { + return Objects.hash(type, message, errorMessage, errorTranslationKey, translationParams); + } + + @Override + public String toString() { + return "IrisWebsocketDTO{" + "type=" + type + ", message=" + message + ", errorMessage='" + errorMessage + '\'' + ", errorTranslationKey='" + errorTranslationKey + '\'' + + ", translationParams=" + translationParams + '}'; + } } } diff --git a/src/main/java/de/tum/in/www1/artemis/web/rest/iris/IrisSettingsResource.java b/src/main/java/de/tum/in/www1/artemis/web/rest/iris/IrisSettingsResource.java index a4162e26f0a2..f597baa640c0 100644 --- a/src/main/java/de/tum/in/www1/artemis/web/rest/iris/IrisSettingsResource.java +++ b/src/main/java/de/tum/in/www1/artemis/web/rest/iris/IrisSettingsResource.java @@ -128,6 +128,7 @@ public ResponseEntity getProgrammingExerciseSettings(@PathVariable */ @PutMapping("iris/global-iris-settings") @EnforceAdmin + @ManualConfig public ResponseEntity updateGlobalSettings(@RequestBody IrisSettings settings) { var updatedSettings = irisSettingsService.saveGlobalIrisSettings(settings); return ResponseEntity.ok(updatedSettings); diff --git a/src/test/java/de/tum/in/www1/artemis/iris/AbstractIrisIntegrationTest.java b/src/test/java/de/tum/in/www1/artemis/iris/AbstractIrisIntegrationTest.java index 462c76961173..9b5fc3945b31 100644 --- a/src/test/java/de/tum/in/www1/artemis/iris/AbstractIrisIntegrationTest.java +++ b/src/test/java/de/tum/in/www1/artemis/iris/AbstractIrisIntegrationTest.java @@ -5,6 +5,7 @@ import static tech.jhipster.config.JHipsterConstants.SPRING_PROFILE_TEST; import java.util.Objects; +import java.util.stream.Collectors; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -18,6 +19,7 @@ import de.tum.in.www1.artemis.domain.Course; import de.tum.in.www1.artemis.domain.ProgrammingExercise; import de.tum.in.www1.artemis.domain.iris.IrisMessage; +import de.tum.in.www1.artemis.domain.iris.IrisMessageContent; import de.tum.in.www1.artemis.domain.iris.IrisTemplate; import de.tum.in.www1.artemis.exercise.ExerciseUtilService; import de.tum.in.www1.artemis.exercise.programmingexercise.ProgrammingExerciseUtilService; @@ -100,11 +102,6 @@ protected IrisTemplate createDummyTemplate() { return template; } - protected void verifyNoMessageWasSentOverWebsocket() throws InterruptedException { - Thread.sleep(1000); - verifyNoInteractions(websocketMessagingService); - } - /** * Wait for the iris message to be processed by Iris, the LLM mock and the websocket service. * @@ -124,8 +121,8 @@ protected void waitForIrisMessageToBeProcessed() throws InterruptedException { protected void verifyMessageWasSentOverWebsocket(String user, Long sessionId, String message) { verify(websocketMessagingService, times(1)).sendMessageToUser(eq(user), eq("/topic/iris/sessions/" + sessionId), ArgumentMatchers.argThat(object -> object instanceof IrisWebsocketService.IrisWebsocketDTO websocketDTO - && websocketDTO.getType() == IrisWebsocketService.IrisWebsocketDTO.IrisWebsocketMessageType.MESSAGE && websocketDTO.getMessage().getContent().size() == 1 - && Objects.equals(websocketDTO.getMessage().getContent().get(0).getTextContent(), message))); + && websocketDTO.getType() == IrisWebsocketService.IrisWebsocketDTO.IrisWebsocketMessageType.MESSAGE + && Objects.equals(websocketDTO.getMessage().getContent().stream().map(IrisMessageContent::getTextContent).collect(Collectors.joining("\n")), message))); } /** @@ -138,8 +135,9 @@ protected void verifyMessageWasSentOverWebsocket(String user, Long sessionId, St protected void verifyMessageWasSentOverWebsocket(String user, Long sessionId, IrisMessage message) { verify(websocketMessagingService, times(1)).sendMessageToUser(eq(user), eq("/topic/iris/sessions/" + sessionId), ArgumentMatchers.argThat(object -> object instanceof IrisWebsocketService.IrisWebsocketDTO websocketDTO - && websocketDTO.getType() == IrisWebsocketService.IrisWebsocketDTO.IrisWebsocketMessageType.MESSAGE && websocketDTO.getMessage().getContent().size() == 1 - && Objects.equals(websocketDTO.getMessage(), message))); + && websocketDTO.getType() == IrisWebsocketService.IrisWebsocketDTO.IrisWebsocketMessageType.MESSAGE + && Objects.equals(websocketDTO.getMessage().getContent().stream().map(IrisMessageContent::getTextContent).toList(), + message.getContent().stream().map(IrisMessageContent::getTextContent).toList()))); } /** @@ -175,7 +173,7 @@ protected void verifyNothingElseWasSentOverWebsocket(String user, Long sessionId } /** - * Verify that an error was sent through the websocket. + * Verify that no error was sent through the websocket. * * @param user the user * @param sessionId the session id diff --git a/src/test/java/de/tum/in/www1/artemis/iris/IrisMessageIntegrationTest.java b/src/test/java/de/tum/in/www1/artemis/iris/IrisMessageIntegrationTest.java index 036b8dc4bbde..058cc075a126 100644 --- a/src/test/java/de/tum/in/www1/artemis/iris/IrisMessageIntegrationTest.java +++ b/src/test/java/de/tum/in/www1/artemis/iris/IrisMessageIntegrationTest.java @@ -1,6 +1,7 @@ package de.tum.in.www1.artemis.iris; import static org.assertj.core.api.Assertions.assertThat; +import static org.awaitility.Awaitility.await; import java.time.ZonedDateTime; import java.util.List; @@ -84,8 +85,11 @@ void sendOneMessage() throws Exception { .isEqualTo(messageToSend.getContent().stream().map(IrisMessageContent::getTextContent).toList()); var irisSessionFromDb = irisSessionRepository.findByIdWithMessages(irisSession.getId()); assertThat(irisSessionFromDb.getMessages()).hasSize(1).isEqualTo(List.of(irisMessage)); + await().until(() -> irisSessionRepository.findByIdWithMessages(irisSession.getId()).getMessages().size() == 2); + verifyMessageWasSentOverWebsocket(TEST_PREFIX + "student1", irisSession.getId(), messageToSend); verifyMessageWasSentOverWebsocket(TEST_PREFIX + "student1", irisSession.getId(), "Hello World"); + verifyNothingElseWasSentOverWebsocket(TEST_PREFIX + "student1", irisSession.getId()); } @Test @@ -268,7 +272,10 @@ void sendOneMessageBadRequest() throws Exception { request.postWithResponseBody("/api/iris/sessions/" + irisSession.getId() + "/messages", messageToSend, IrisMessage.class, HttpStatus.CREATED); - verifyNoMessageWasSentOverWebsocket(); + waitForIrisMessageToBeProcessed(); + verifyMessageWasSentOverWebsocket(TEST_PREFIX + "student13", irisSession.getId(), messageToSend); + verifyErrorWasSentOverWebsocket(TEST_PREFIX + "student13", irisSession.getId()); + verifyNothingElseWasSentOverWebsocket(TEST_PREFIX + "student13", irisSession.getId()); } @Test @@ -288,7 +295,10 @@ void sendOneMessageEmptyBody() throws Exception { request.postWithResponseBody("/api/iris/sessions/" + irisSession.getId() + "/messages", messageToSend, IrisMessage.class, HttpStatus.CREATED); - verifyNoMessageWasSentOverWebsocket(); + waitForIrisMessageToBeProcessed(); + verifyMessageWasSentOverWebsocket(TEST_PREFIX + "student14", irisSession.getId(), messageToSend); + verifyErrorWasSentOverWebsocket(TEST_PREFIX + "student14", irisSession.getId()); + verifyNothingElseWasSentOverWebsocket(TEST_PREFIX + "student14", irisSession.getId()); } private IrisMessageContent createMockContent(IrisMessage message) { diff --git a/src/test/java/de/tum/in/www1/artemis/iris/IrisWebsocketTest.java b/src/test/java/de/tum/in/www1/artemis/iris/IrisWebsocketTest.java index 08e621cca2b1..2bb97ffeba0d 100644 --- a/src/test/java/de/tum/in/www1/artemis/iris/IrisWebsocketTest.java +++ b/src/test/java/de/tum/in/www1/artemis/iris/IrisWebsocketTest.java @@ -56,7 +56,8 @@ void sendMessage() { message.setContent(List.of(createMockContent(message), createMockContent(message))); message.setMessageDifferentiator(101010); irisWebsocketService.sendMessage(message); - verify(websocketMessagingService, times(1)).sendMessageToUser(eq(TEST_PREFIX + "student1"), eq("/topic/iris/sessions/" + irisSession.getId()), eq(message)); + verify(websocketMessagingService, times(1)).sendMessageToUser(eq(TEST_PREFIX + "student1"), eq("/topic/iris/sessions/" + irisSession.getId()), + eq(new IrisWebsocketService.IrisWebsocketDTO(message))); } private IrisMessageContent createMockContent(IrisMessage message) { From 707fd24a1f0251d5bca81db1f3ece078cd68246b Mon Sep 17 00:00:00 2001 From: Timor Morrien Date: Thu, 31 Aug 2023 01:04:16 +0200 Subject: [PATCH 2/8] Create AdminIrisSettingsResource --- .../admin/iris/AdminIrisSettingsResource.java | 38 +++++++++++++++++++ .../web/rest/iris/IrisSettingsResource.java | 14 ------- .../settings/shared/iris-settings.service.ts | 2 +- 3 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 src/main/java/de/tum/in/www1/artemis/web/rest/admin/iris/AdminIrisSettingsResource.java diff --git a/src/main/java/de/tum/in/www1/artemis/web/rest/admin/iris/AdminIrisSettingsResource.java b/src/main/java/de/tum/in/www1/artemis/web/rest/admin/iris/AdminIrisSettingsResource.java new file mode 100644 index 000000000000..19048d1ef407 --- /dev/null +++ b/src/main/java/de/tum/in/www1/artemis/web/rest/admin/iris/AdminIrisSettingsResource.java @@ -0,0 +1,38 @@ +package de.tum.in.www1.artemis.web.rest.admin.iris; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import de.tum.in.www1.artemis.domain.iris.settings.IrisSettings; +import de.tum.in.www1.artemis.security.annotations.EnforceAdmin; +import de.tum.in.www1.artemis.service.iris.IrisSettingsService; + +/** + * REST controller for managing {@link IrisSettings}. + */ +@RestController +@RequestMapping("api/admin/") +public class AdminIrisSettingsResource { + + private final IrisSettingsService irisSettingsService; + + public AdminIrisSettingsResource(IrisSettingsService irisSettingsService) { + this.irisSettingsService = irisSettingsService; + } + + /** + * PUT iris/global-iris-settings: Update the global iris settings. + * + * @param settings the settings to update + * @return the {@link ResponseEntity} with status {@code 200 (Ok)} and with body the updated settings. + */ + @PutMapping("iris/global-iris-settings") + @EnforceAdmin + public ResponseEntity updateGlobalSettings(@RequestBody IrisSettings settings) { + var updatedSettings = irisSettingsService.saveGlobalIrisSettings(settings); + return ResponseEntity.ok(updatedSettings); + } +} diff --git a/src/main/java/de/tum/in/www1/artemis/web/rest/iris/IrisSettingsResource.java b/src/main/java/de/tum/in/www1/artemis/web/rest/iris/IrisSettingsResource.java index f597baa640c0..f5692f388a3c 100644 --- a/src/main/java/de/tum/in/www1/artemis/web/rest/iris/IrisSettingsResource.java +++ b/src/main/java/de/tum/in/www1/artemis/web/rest/iris/IrisSettingsResource.java @@ -120,20 +120,6 @@ public ResponseEntity getProgrammingExerciseSettings(@PathVariable return ResponseEntity.ok(combinedIrisSettings); } - /** - * PUT iris/global-iris-settings: Update the global iris settings. - * - * @param settings the settings to update - * @return the {@link ResponseEntity} with status {@code 200 (Ok)} and with body the updated settings. - */ - @PutMapping("iris/global-iris-settings") - @EnforceAdmin - @ManualConfig - public ResponseEntity updateGlobalSettings(@RequestBody IrisSettings settings) { - var updatedSettings = irisSettingsService.saveGlobalIrisSettings(settings); - return ResponseEntity.ok(updatedSettings); - } - /** * PUT courses/{courseId}/raw-iris-settings: Update the raw iris settings for the course. * diff --git a/src/main/webapp/app/iris/settings/shared/iris-settings.service.ts b/src/main/webapp/app/iris/settings/shared/iris-settings.service.ts index f85240cef829..40c775b2e5f5 100644 --- a/src/main/webapp/app/iris/settings/shared/iris-settings.service.ts +++ b/src/main/webapp/app/iris/settings/shared/iris-settings.service.ts @@ -70,7 +70,7 @@ export class IrisSettingsService { * @param settings the settings to set */ setGlobalSettings(settings: IrisSettings): Observable { - return this.http.put(`${this.resourceUrl}/iris/global-iris-settings`, settings, { observe: 'response' }); + return this.http.put(`${this.resourceUrl}/admin/iris/global-iris-settings`, settings, { observe: 'response' }); } /** From ef539caff3f45bda205a4b67399cceac0fadbd0c Mon Sep 17 00:00:00 2001 From: Timor Morrien Date: Thu, 31 Aug 2023 01:06:53 +0200 Subject: [PATCH 3/8] Fix NotNull --- .../www1/artemis/config/auth/IrisAuthorizationInterceptor.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tum/in/www1/artemis/config/auth/IrisAuthorizationInterceptor.java b/src/main/java/de/tum/in/www1/artemis/config/auth/IrisAuthorizationInterceptor.java index 4b6759db352d..0d018263546d 100644 --- a/src/main/java/de/tum/in/www1/artemis/config/auth/IrisAuthorizationInterceptor.java +++ b/src/main/java/de/tum/in/www1/artemis/config/auth/IrisAuthorizationInterceptor.java @@ -2,7 +2,8 @@ import java.io.IOException; -import org.jetbrains.annotations.NotNull; +import javax.validation.constraints.NotNull; + import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Profile; import org.springframework.http.HttpHeaders; From 5d81616cb95685a68ac712420c822d23b4db6f71 Mon Sep 17 00:00:00 2001 From: Timor Morrien Date: Thu, 31 Aug 2023 17:59:07 +0200 Subject: [PATCH 4/8] Increase coverage --- .../iris/IrisMessageContentRepository.java | 12 ---- .../connectors/iris/IrisConnectorService.java | 16 +++-- .../connector/IrisRequestMockProvider.java | 36 +++++++++-- .../iris/IrisConnectorServiceTest.java | 63 +++++++++++++++++++ .../iris/IrisHestiaIntegrationTest.java | 4 +- .../iris/IrisMessageIntegrationTest.java | 6 +- 6 files changed, 108 insertions(+), 29 deletions(-) create mode 100644 src/test/java/de/tum/in/www1/artemis/iris/IrisConnectorServiceTest.java diff --git a/src/main/java/de/tum/in/www1/artemis/repository/iris/IrisMessageContentRepository.java b/src/main/java/de/tum/in/www1/artemis/repository/iris/IrisMessageContentRepository.java index 26cce35e5c3d..8ce7d931c953 100644 --- a/src/main/java/de/tum/in/www1/artemis/repository/iris/IrisMessageContentRepository.java +++ b/src/main/java/de/tum/in/www1/artemis/repository/iris/IrisMessageContentRepository.java @@ -1,23 +1,11 @@ package de.tum.in.www1.artemis.repository.iris; -import java.util.List; - -import javax.validation.constraints.NotNull; - import org.springframework.data.jpa.repository.JpaRepository; import de.tum.in.www1.artemis.domain.iris.IrisMessageContent; -import de.tum.in.www1.artemis.web.rest.errors.EntityNotFoundException; /** * Spring Data repository for the IrisMessageContent entity. */ public interface IrisMessageContentRepository extends JpaRepository { - - List findAllByMessageId(Long messageId); - - @NotNull - default IrisMessageContent findByIdElseThrow(long messageContentId) throws EntityNotFoundException { - return findById(messageContentId).orElseThrow(() -> new EntityNotFoundException("Iris Message Content", messageContentId)); - } } diff --git a/src/main/java/de/tum/in/www1/artemis/service/connectors/iris/IrisConnectorService.java b/src/main/java/de/tum/in/www1/artemis/service/connectors/iris/IrisConnectorService.java index 595c2101d660..c13551e658c4 100644 --- a/src/main/java/de/tum/in/www1/artemis/service/connectors/iris/IrisConnectorService.java +++ b/src/main/java/de/tum/in/www1/artemis/service/connectors/iris/IrisConnectorService.java @@ -11,8 +11,7 @@ import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; -import org.springframework.web.client.HttpClientErrorException; -import org.springframework.web.client.RestTemplate; +import org.springframework.web.client.*; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; @@ -67,11 +66,16 @@ public CompletableFuture sendRequest(IrisTemplate templa * @return A list of available Models as IrisModelDTO */ public List getOfferedModels() throws IrisConnectorException { - var response = restTemplate.getForEntity(irisUrl + "/api/v1/models", JsonNode.class); - if (!response.getStatusCode().is2xxSuccessful() || !response.hasBody()) { + try { + var response = restTemplate.getForEntity(irisUrl + "/api/v1/models", JsonNode.class); + if (!response.getStatusCode().is2xxSuccessful() || !response.hasBody()) { + throw new IrisConnectorException("Could not fetch offered models"); + } + return Arrays.asList((IrisModelDTO[]) parseResponse(response.getBody(), IrisModelDTO.class.arrayType())); + } + catch (HttpStatusCodeException e) { throw new IrisConnectorException("Could not fetch offered models"); } - return Arrays.asList((IrisModelDTO[]) parseResponse(response.getBody(), IrisModelDTO.class.arrayType())); } private CompletableFuture sendRequest(IrisRequestDTO request) { @@ -83,7 +87,7 @@ private CompletableFuture sendRequest(IrisRequestDTO req } return CompletableFuture.completedFuture(parseResponse(response.getBody(), IrisMessageResponseDTO.class)); } - catch (HttpClientErrorException e) { + catch (HttpStatusCodeException e) { switch (e.getStatusCode()) { case BAD_REQUEST -> { var badRequestDTO = parseResponse(objectMapper.readTree(e.getResponseBodyAsString()).get("detail"), IrisErrorResponseDTO.class); diff --git a/src/test/java/de/tum/in/www1/artemis/connector/IrisRequestMockProvider.java b/src/test/java/de/tum/in/www1/artemis/connector/IrisRequestMockProvider.java index f9e6cfd7a642..3958aeb97e9a 100644 --- a/src/test/java/de/tum/in/www1/artemis/connector/IrisRequestMockProvider.java +++ b/src/test/java/de/tum/in/www1/artemis/connector/IrisRequestMockProvider.java @@ -7,6 +7,7 @@ import java.net.URL; import java.time.ZonedDateTime; import java.util.Collections; +import java.util.Map; import org.mockito.MockitoAnnotations; import org.springframework.beans.factory.annotation.Autowired; @@ -26,7 +27,9 @@ import de.tum.in.www1.artemis.domain.iris.IrisMessage; import de.tum.in.www1.artemis.domain.iris.IrisMessageContent; import de.tum.in.www1.artemis.domain.iris.IrisMessageSender; +import de.tum.in.www1.artemis.service.connectors.iris.dto.IrisErrorResponseDTO; import de.tum.in.www1.artemis.service.connectors.iris.dto.IrisMessageResponseDTO; +import de.tum.in.www1.artemis.service.connectors.iris.dto.IrisModelDTO; @Component @Profile("iris") @@ -37,7 +40,10 @@ public class IrisRequestMockProvider { private MockRestServiceServer mockServer; @Value("${artemis.iris.url}/api/v1/messages") - private URL apiURL; + private URL messagesApiURL; + + @Value("${artemis.iris.url}/api/v1/models") + private URL modelsApiURL; @Autowired private ObjectMapper mapper; @@ -66,9 +72,9 @@ public void reset() throws Exception { /** * Mocks response call for the pyris call */ - public void mockResponse(String responseMessage) throws JsonProcessingException { + public void mockMessageResponse(String responseMessage) throws JsonProcessingException { if (responseMessage == null) { - mockServer.expect(ExpectedCount.once(), requestTo(apiURL.toString())).andExpect(method(HttpMethod.POST)).andRespond(withSuccess()); + mockServer.expect(ExpectedCount.once(), requestTo(messagesApiURL.toString())).andExpect(method(HttpMethod.POST)).andRespond(withSuccess()); return; } var irisMessage = new IrisMessage(); @@ -81,10 +87,28 @@ public void mockResponse(String responseMessage) throws JsonProcessingException var response = new IrisMessageResponseDTO(null, irisMessage); var json = mapper.writeValueAsString(response); - mockServer.expect(ExpectedCount.once(), requestTo(apiURL.toString())).andExpect(method(HttpMethod.POST)).andRespond(withSuccess(json, MediaType.APPLICATION_JSON)); + mockServer.expect(ExpectedCount.once(), requestTo(messagesApiURL.toString())).andExpect(method(HttpMethod.POST)).andRespond(withSuccess(json, MediaType.APPLICATION_JSON)); + } + + public void mockMessageError() throws JsonProcessingException { + mockMessageError(500); + } + + public void mockMessageError(int status) throws JsonProcessingException { + var errorResponseDTO = new IrisErrorResponseDTO("Test error"); + var json = Map.of("detail", errorResponseDTO); + mockServer.expect(ExpectedCount.once(), requestTo(messagesApiURL.toString())).andExpect(method(HttpMethod.POST)) + .andRespond(withRawStatus(status).body(mapper.writeValueAsString(json))); + } + + public void mockModelsResponse() throws JsonProcessingException { + var irisModelDTO = new IrisModelDTO("TEST_MODEL", "Test model", "Test description"); + var irisModelDTOArray = new IrisModelDTO[] { irisModelDTO }; + mockServer.expect(ExpectedCount.once(), requestTo(modelsApiURL.toString())).andExpect(method(HttpMethod.GET)) + .andRespond(withSuccess(mapper.writeValueAsString(irisModelDTOArray), MediaType.APPLICATION_JSON)); } - public void mockError() { - mockServer.expect(ExpectedCount.once(), requestTo(apiURL.toString())).andExpect(method(HttpMethod.POST)).andRespond(withBadRequest()); + public void mockModelsError() throws JsonProcessingException { + mockServer.expect(ExpectedCount.once(), requestTo(modelsApiURL.toString())).andExpect(method(HttpMethod.GET)).andRespond(withRawStatus(418)); } } diff --git a/src/test/java/de/tum/in/www1/artemis/iris/IrisConnectorServiceTest.java b/src/test/java/de/tum/in/www1/artemis/iris/IrisConnectorServiceTest.java new file mode 100644 index 000000000000..9b48a95973f4 --- /dev/null +++ b/src/test/java/de/tum/in/www1/artemis/iris/IrisConnectorServiceTest.java @@ -0,0 +1,63 @@ +package de.tum.in.www1.artemis.iris; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.Collections; +import java.util.stream.Stream; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.springframework.beans.factory.annotation.Autowired; + +import de.tum.in.www1.artemis.domain.iris.IrisTemplate; +import de.tum.in.www1.artemis.service.connectors.iris.IrisConnectorException; +import de.tum.in.www1.artemis.service.connectors.iris.IrisConnectorService; +import de.tum.in.www1.artemis.service.iris.exception.IrisForbiddenException; +import de.tum.in.www1.artemis.service.iris.exception.IrisInternalPyrisErrorException; +import de.tum.in.www1.artemis.service.iris.exception.IrisInvalidTemplateException; +import de.tum.in.www1.artemis.service.iris.exception.IrisModelNotAvailableException; + +public class IrisConnectorServiceTest extends AbstractIrisIntegrationTest { + + @Autowired + private IrisConnectorService irisConnectorService; + + private static Stream irisExceptions() { + return Stream.of(Arguments.of(400, IrisInvalidTemplateException.class), Arguments.of(401, IrisForbiddenException.class), Arguments.of(403, IrisForbiddenException.class), + Arguments.of(404, IrisModelNotAvailableException.class), Arguments.of(500, IrisInternalPyrisErrorException.class), + // Test default case + Arguments.of(418, IrisInternalPyrisErrorException.class)); + } + + @ParameterizedTest + @MethodSource("irisExceptions") + void testException(int httpStatus, Class exceptionClass) throws Exception { + var template = new IrisTemplate("Dummy"); + + irisRequestMockProvider.mockMessageError(httpStatus); + + irisConnectorService.sendRequest(template, "TEST_MODEL", Collections.emptyMap()).handle((response, throwable) -> { + assertThat(throwable.getCause()).isNotNull().isInstanceOf(exceptionClass); + return null; + }).get(); + } + + @Test + void testOfferedModels() throws Exception { + irisRequestMockProvider.mockModelsResponse(); + + var offeredModels = irisConnectorService.getOfferedModels(); + assertThat(offeredModels).hasSize(1); + assertThat(offeredModels.get(0).id()).isEqualTo("TEST_MODEL"); + } + + @Test + void testOfferedModelsError() throws Exception { + irisRequestMockProvider.mockModelsError(); + + assertThatThrownBy(() -> irisConnectorService.getOfferedModels()).isInstanceOf(IrisConnectorException.class); + } +} diff --git a/src/test/java/de/tum/in/www1/artemis/iris/IrisHestiaIntegrationTest.java b/src/test/java/de/tum/in/www1/artemis/iris/IrisHestiaIntegrationTest.java index 0e32ac96d711..bc72a7aa3e31 100644 --- a/src/test/java/de/tum/in/www1/artemis/iris/IrisHestiaIntegrationTest.java +++ b/src/test/java/de/tum/in/www1/artemis/iris/IrisHestiaIntegrationTest.java @@ -39,8 +39,8 @@ void initTestCase() { void updateSolutionEntriesOnSaving() throws Exception { addCodeHints(); - irisRequestMockProvider.mockResponse("Hello World Content"); - irisRequestMockProvider.mockResponse("Hello World Description"); + irisRequestMockProvider.mockMessageResponse("Hello World Content"); + irisRequestMockProvider.mockMessageResponse("Hello World Description"); var updatedCodeHint = request.postWithResponseBody("/api/programming-exercises/" + exercise.getId() + "/code-hints/" + codeHint.getId() + "/generate-description", null, CodeHint.class, HttpStatus.OK); diff --git a/src/test/java/de/tum/in/www1/artemis/iris/IrisMessageIntegrationTest.java b/src/test/java/de/tum/in/www1/artemis/iris/IrisMessageIntegrationTest.java index 058cc075a126..fafc286487b9 100644 --- a/src/test/java/de/tum/in/www1/artemis/iris/IrisMessageIntegrationTest.java +++ b/src/test/java/de/tum/in/www1/artemis/iris/IrisMessageIntegrationTest.java @@ -70,7 +70,7 @@ void sendOneMessage() throws Exception { messageToSend.setMessageDifferentiator(1453); messageToSend.setContent(List.of(createMockContent(messageToSend), createMockContent(messageToSend), createMockContent(messageToSend))); - irisRequestMockProvider.mockResponse("Hello World"); + irisRequestMockProvider.mockMessageResponse("Hello World"); var savedExercise = irisUtilTestService.setupTemplate(exercise, new LocalRepository("main")); var exerciseParticipation = participationUtilService.addStudentParticipationForProgrammingExercise(savedExercise, TEST_PREFIX + "student1"); irisUtilTestService.setupStudentParticipation(exerciseParticipation, new LocalRepository("main")); @@ -264,7 +264,7 @@ void sendOneMessageBadRequest() throws Exception { messageToSend.setSentAt(ZonedDateTime.now()); messageToSend.setContent(List.of(createMockContent(messageToSend), createMockContent(messageToSend), createMockContent(messageToSend))); - irisRequestMockProvider.mockError(); + irisRequestMockProvider.mockMessageError(); var savedExercise = irisUtilTestService.setupTemplate(exercise, new LocalRepository("main")); var exerciseParticipation = participationUtilService.addStudentParticipationForProgrammingExercise(savedExercise, TEST_PREFIX + "student13"); irisUtilTestService.setupStudentParticipation(exerciseParticipation, new LocalRepository("main")); @@ -287,7 +287,7 @@ void sendOneMessageEmptyBody() throws Exception { messageToSend.setSentAt(ZonedDateTime.now()); messageToSend.setContent(List.of(createMockContent(messageToSend), createMockContent(messageToSend), createMockContent(messageToSend))); - irisRequestMockProvider.mockResponse(null); + irisRequestMockProvider.mockMessageResponse(null); var savedExercise = irisUtilTestService.setupTemplate(exercise, new LocalRepository("main")); var exerciseParticipation = participationUtilService.addStudentParticipationForProgrammingExercise(savedExercise, TEST_PREFIX + "student14"); irisUtilTestService.setupStudentParticipation(exerciseParticipation, new LocalRepository("main")); From a88dcc75d61493113934d1f7f7c0c4550036b835 Mon Sep 17 00:00:00 2001 From: Timor Morrien Date: Thu, 31 Aug 2023 22:13:28 +0200 Subject: [PATCH 5/8] Fix Test class --- .../de/tum/in/www1/artemis/iris/IrisConnectorServiceTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/de/tum/in/www1/artemis/iris/IrisConnectorServiceTest.java b/src/test/java/de/tum/in/www1/artemis/iris/IrisConnectorServiceTest.java index 9b48a95973f4..376ba69e1a3a 100644 --- a/src/test/java/de/tum/in/www1/artemis/iris/IrisConnectorServiceTest.java +++ b/src/test/java/de/tum/in/www1/artemis/iris/IrisConnectorServiceTest.java @@ -20,7 +20,7 @@ import de.tum.in.www1.artemis.service.iris.exception.IrisInvalidTemplateException; import de.tum.in.www1.artemis.service.iris.exception.IrisModelNotAvailableException; -public class IrisConnectorServiceTest extends AbstractIrisIntegrationTest { +class IrisConnectorServiceTest extends AbstractIrisIntegrationTest { @Autowired private IrisConnectorService irisConnectorService; From 3b21c39ac447762613e7769628f1231b73626c2f Mon Sep 17 00:00:00 2001 From: Timor Morrien Date: Fri, 1 Sep 2023 09:52:59 +0200 Subject: [PATCH 6/8] Increase number of allowed server starts --- supporting_scripts/extract_number_of_server_starts.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/supporting_scripts/extract_number_of_server_starts.sh b/supporting_scripts/extract_number_of_server_starts.sh index cc87238d4776..3677527b6f20 100644 --- a/supporting_scripts/extract_number_of_server_starts.sh +++ b/supporting_scripts/extract_number_of_server_starts.sh @@ -9,8 +9,8 @@ then exit 1 fi -if [[ $numberOfStarts -gt 4 ]] +if [[ $numberOfStarts -gt 5 ]] then - echo "The number of Server Starts should not be greater than 4!" + echo "The number of Server Starts should not be greater than 5!" exit 1 fi From 733a812d0ebfa1d9fd3db729cc7048a6cf5c845a Mon Sep 17 00:00:00 2001 From: Timor Morrien Date: Mon, 4 Sep 2023 12:18:12 +0200 Subject: [PATCH 7/8] Try removing additional server start --- .../AbstractSpringIntegrationBambooBitbucketJiraTest.java | 2 +- .../tum/in/www1/artemis/iris/AbstractIrisIntegrationTest.java | 3 --- supporting_scripts/extract_number_of_server_starts.sh | 4 ++-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/test/java/de/tum/in/www1/artemis/AbstractSpringIntegrationBambooBitbucketJiraTest.java b/src/test/java/de/tum/in/www1/artemis/AbstractSpringIntegrationBambooBitbucketJiraTest.java index d2d8649f796e..61c98de28ac8 100644 --- a/src/test/java/de/tum/in/www1/artemis/AbstractSpringIntegrationBambooBitbucketJiraTest.java +++ b/src/test/java/de/tum/in/www1/artemis/AbstractSpringIntegrationBambooBitbucketJiraTest.java @@ -61,7 +61,7 @@ @ExtendWith(SpringExtension.class) @AutoConfigureEmbeddedDatabase // NOTE: we use a common set of active profiles to reduce the number of application launches during testing. This significantly saves time and memory! -@ActiveProfiles({ SPRING_PROFILE_TEST, "artemis", "bamboo", "bitbucket", "jira", "ldap", "scheduling", "athene", "apollon" }) +@ActiveProfiles({ SPRING_PROFILE_TEST, "artemis", "bamboo", "bitbucket", "jira", "ldap", "scheduling", "athene", "apollon", "iris" }) public abstract class AbstractSpringIntegrationBambooBitbucketJiraTest extends AbstractArtemisIntegrationTest { @SpyBean diff --git a/src/test/java/de/tum/in/www1/artemis/iris/AbstractIrisIntegrationTest.java b/src/test/java/de/tum/in/www1/artemis/iris/AbstractIrisIntegrationTest.java index 9b5fc3945b31..9fceb36ed9b5 100644 --- a/src/test/java/de/tum/in/www1/artemis/iris/AbstractIrisIntegrationTest.java +++ b/src/test/java/de/tum/in/www1/artemis/iris/AbstractIrisIntegrationTest.java @@ -2,7 +2,6 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.*; -import static tech.jhipster.config.JHipsterConstants.SPRING_PROFILE_TEST; import java.util.Objects; import java.util.stream.Collectors; @@ -12,7 +11,6 @@ import org.mockito.ArgumentMatchers; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.test.context.ActiveProfiles; import de.tum.in.www1.artemis.AbstractSpringIntegrationBambooBitbucketJiraTest; import de.tum.in.www1.artemis.connector.IrisRequestMockProvider; @@ -30,7 +28,6 @@ import de.tum.in.www1.artemis.service.iris.IrisWebsocketService; import de.tum.in.www1.artemis.user.UserUtilService; -@ActiveProfiles({ SPRING_PROFILE_TEST, "artemis", "bamboo", "bitbucket", "jira", "ldap", "scheduling", "athene", "apollon", "iris" }) public class AbstractIrisIntegrationTest extends AbstractSpringIntegrationBambooBitbucketJiraTest { @Autowired diff --git a/supporting_scripts/extract_number_of_server_starts.sh b/supporting_scripts/extract_number_of_server_starts.sh index 3677527b6f20..cc87238d4776 100644 --- a/supporting_scripts/extract_number_of_server_starts.sh +++ b/supporting_scripts/extract_number_of_server_starts.sh @@ -9,8 +9,8 @@ then exit 1 fi -if [[ $numberOfStarts -gt 5 ]] +if [[ $numberOfStarts -gt 4 ]] then - echo "The number of Server Starts should not be greater than 5!" + echo "The number of Server Starts should not be greater than 4!" exit 1 fi From 66ae9ce1711fa149f85f65255f81bf311e5ff1bf Mon Sep 17 00:00:00 2001 From: Timor Morrien Date: Thu, 14 Sep 2023 23:15:11 +0200 Subject: [PATCH 8/8] Implement feedback --- .../iris/AbstractIrisIntegrationTest.java | 4 +- .../iris/IrisMessageIntegrationTest.java | 125 ++++++++---------- 2 files changed, 54 insertions(+), 75 deletions(-) diff --git a/src/test/java/de/tum/in/www1/artemis/iris/AbstractIrisIntegrationTest.java b/src/test/java/de/tum/in/www1/artemis/iris/AbstractIrisIntegrationTest.java index 9fceb36ed9b5..d6893a13f9f8 100644 --- a/src/test/java/de/tum/in/www1/artemis/iris/AbstractIrisIntegrationTest.java +++ b/src/test/java/de/tum/in/www1/artemis/iris/AbstractIrisIntegrationTest.java @@ -28,7 +28,7 @@ import de.tum.in.www1.artemis.service.iris.IrisWebsocketService; import de.tum.in.www1.artemis.user.UserUtilService; -public class AbstractIrisIntegrationTest extends AbstractSpringIntegrationBambooBitbucketJiraTest { +public abstract class AbstractIrisIntegrationTest extends AbstractSpringIntegrationBambooBitbucketJiraTest { @Autowired protected CourseRepository courseRepository; @@ -105,7 +105,7 @@ protected IrisTemplate createDummyTemplate() { * @throws InterruptedException if the thread is interrupted */ protected void waitForIrisMessageToBeProcessed() throws InterruptedException { - Thread.sleep(500); + Thread.sleep(100); } /** diff --git a/src/test/java/de/tum/in/www1/artemis/iris/IrisMessageIntegrationTest.java b/src/test/java/de/tum/in/www1/artemis/iris/IrisMessageIntegrationTest.java index fafc286487b9..01d63fa45229 100644 --- a/src/test/java/de/tum/in/www1/artemis/iris/IrisMessageIntegrationTest.java +++ b/src/test/java/de/tum/in/www1/artemis/iris/IrisMessageIntegrationTest.java @@ -18,6 +18,7 @@ import de.tum.in.www1.artemis.domain.iris.IrisMessage; import de.tum.in.www1.artemis.domain.iris.IrisMessageContent; import de.tum.in.www1.artemis.domain.iris.IrisMessageSender; +import de.tum.in.www1.artemis.domain.iris.session.IrisSession; import de.tum.in.www1.artemis.participation.ParticipationUtilService; import de.tum.in.www1.artemis.repository.iris.IrisMessageRepository; import de.tum.in.www1.artemis.repository.iris.IrisSessionRepository; @@ -52,7 +53,7 @@ class IrisMessageIntegrationTest extends AbstractIrisIntegrationTest { @BeforeEach void initTestCase() { - userUtilService.addUsers(TEST_PREFIX, 14, 0, 0, 0); + userUtilService.addUsers(TEST_PREFIX, 2, 0, 0, 0); final Course course = programmingExerciseUtilService.addCourseWithOneProgrammingExerciseAndTestCases(); exercise = exerciseUtilService.getFirstExerciseWithType(course, ProgrammingExercise.class); @@ -64,11 +65,8 @@ void initTestCase() { @WithMockUser(username = TEST_PREFIX + "student1", roles = "USER") void sendOneMessage() throws Exception { var irisSession = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student1")); - var messageToSend = new IrisMessage(); - messageToSend.setSession(irisSession); - messageToSend.setSentAt(ZonedDateTime.now()); + var messageToSend = createDefaultMockMessage(irisSession); messageToSend.setMessageDifferentiator(1453); - messageToSend.setContent(List.of(createMockContent(messageToSend), createMockContent(messageToSend), createMockContent(messageToSend))); irisRequestMockProvider.mockMessageResponse("Hello World"); var savedExercise = irisUtilTestService.setupTemplate(exercise, new LocalRepository("main")); @@ -93,21 +91,18 @@ void sendOneMessage() throws Exception { } @Test - @WithMockUser(username = TEST_PREFIX + "student2", roles = "USER") + @WithMockUser(username = TEST_PREFIX + "student1", roles = "USER") void sendOneMessageToWrongSession() throws Exception { - var irisSession1 = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student2")); - var irisSession2 = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student3")); - var messageToSend = new IrisMessage(); - messageToSend.setSession(irisSession2); - messageToSend.setSentAt(ZonedDateTime.now()); - messageToSend.setContent(List.of(createMockContent(messageToSend), createMockContent(messageToSend), createMockContent(messageToSend))); + var irisSession1 = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student1")); + var irisSession2 = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student2")); + IrisMessage messageToSend = createDefaultMockMessage(irisSession2); request.postWithResponseBody("/api/iris/sessions/" + irisSession2.getId() + "/messages", messageToSend, IrisMessage.class, HttpStatus.FORBIDDEN); } @Test - @WithMockUser(username = TEST_PREFIX + "student4", roles = "USER") + @WithMockUser(username = TEST_PREFIX + "student1", roles = "USER") void sendMessageWithoutContent() throws Exception { - var irisSession = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student4")); + var irisSession = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student1")); var messageToSend = new IrisMessage(); messageToSend.setSession(irisSession); messageToSend.setSentAt(ZonedDateTime.now()); @@ -115,13 +110,10 @@ void sendMessageWithoutContent() throws Exception { } @Test - @WithMockUser(username = TEST_PREFIX + "student5", roles = "USER") + @WithMockUser(username = TEST_PREFIX + "student1", roles = "USER") void sendTwoMessages() throws Exception { - var irisSession = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student5")); - var messageToSend1 = new IrisMessage(); - messageToSend1.setSession(irisSession); - messageToSend1.setSentAt(ZonedDateTime.now()); - messageToSend1.setContent(List.of(createMockContent(messageToSend1), createMockContent(messageToSend1), createMockContent(messageToSend1))); + var irisSession = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student1")); + IrisMessage messageToSend1 = createDefaultMockMessage(irisSession); var savedExercise = irisUtilTestService.setupTemplate(exercise, new LocalRepository("main")); var exerciseParticipation = participationUtilService.addStudentParticipationForProgrammingExercise(savedExercise, TEST_PREFIX + "student1"); @@ -137,10 +129,7 @@ void sendTwoMessages() throws Exception { var irisSessionFromDb = irisSessionRepository.findByIdWithMessages(irisSession.getId()); assertThat(irisSessionFromDb.getMessages()).hasSize(1).isEqualTo(List.of(irisMessage1)); - var messageToSend2 = new IrisMessage(); - messageToSend2.setSession(irisSession); - messageToSend2.setSentAt(ZonedDateTime.now()); - messageToSend2.setContent(List.of(createMockContent(messageToSend2), createMockContent(messageToSend2), createMockContent(messageToSend2))); + IrisMessage messageToSend2 = createDefaultMockMessage(irisSession); var irisMessage2 = request.postWithResponseBody("/api/iris/sessions/" + irisSession.getId() + "/messages", messageToSend2, IrisMessage.class, HttpStatus.CREATED); assertThat(irisMessage2.getSender()).isEqualTo(IrisMessageSender.USER); assertThat(irisMessage2.getHelpful()).isNull(); @@ -152,25 +141,13 @@ void sendTwoMessages() throws Exception { } @Test - @WithMockUser(username = TEST_PREFIX + "student6", roles = "USER") + @WithMockUser(username = TEST_PREFIX + "student1", roles = "USER") void getMessages() throws Exception { - var irisSession = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student6")); - var message1 = new IrisMessage(); - message1.setSession(irisSession); - message1.setSentAt(ZonedDateTime.now()); - message1.setContent(List.of(createMockContent(message1), createMockContent(message1), createMockContent(message1))); - var message2 = new IrisMessage(); - message2.setSession(irisSession); - message2.setSentAt(ZonedDateTime.now()); - message2.setContent(List.of(createMockContent(message2), createMockContent(message2), createMockContent(message2))); - var message3 = new IrisMessage(); - message3.setSession(irisSession); - message3.setSentAt(ZonedDateTime.now()); - message3.setContent(List.of(createMockContent(message3), createMockContent(message3), createMockContent(message3))); - var message4 = new IrisMessage(); - message4.setSession(irisSession); - message4.setSentAt(ZonedDateTime.now()); - message4.setContent(List.of(createMockContent(message4), createMockContent(message4), createMockContent(message4))); + var irisSession = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student1")); + IrisMessage message1 = createDefaultMockMessage(irisSession); + IrisMessage message2 = createDefaultMockMessage(irisSession); + IrisMessage message3 = createDefaultMockMessage(irisSession); + IrisMessage message4 = createDefaultMockMessage(irisSession); irisMessageService.saveMessage(message1, irisSession, IrisMessageSender.ARTEMIS); message2 = irisMessageService.saveMessage(message2, irisSession, IrisMessageSender.LLM); @@ -187,9 +164,9 @@ void getMessages() throws Exception { } @Test - @WithMockUser(username = TEST_PREFIX + "student7", roles = "USER") + @WithMockUser(username = TEST_PREFIX + "student1", roles = "USER") void rateMessageHelpfulTrue() throws Exception { - var irisSession = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student7")); + var irisSession = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student1")); var message = new IrisMessage(); message.setSession(irisSession); message.setSentAt(ZonedDateTime.now()); @@ -201,9 +178,9 @@ void rateMessageHelpfulTrue() throws Exception { } @Test - @WithMockUser(username = TEST_PREFIX + "student8", roles = "USER") + @WithMockUser(username = TEST_PREFIX + "student1", roles = "USER") void rateMessageHelpfulFalse() throws Exception { - var irisSession = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student8")); + var irisSession = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student1")); var message = new IrisMessage(); message.setSession(irisSession); message.setSentAt(ZonedDateTime.now()); @@ -215,9 +192,9 @@ void rateMessageHelpfulFalse() throws Exception { } @Test - @WithMockUser(username = TEST_PREFIX + "student9", roles = "USER") + @WithMockUser(username = TEST_PREFIX + "student1", roles = "USER") void rateMessageHelpfulNull() throws Exception { - var irisSession = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student9")); + var irisSession = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student1")); var message = new IrisMessage(); message.setSession(irisSession); message.setSentAt(ZonedDateTime.now()); @@ -229,9 +206,9 @@ void rateMessageHelpfulNull() throws Exception { } @Test - @WithMockUser(username = TEST_PREFIX + "student10", roles = "USER") + @WithMockUser(username = TEST_PREFIX + "student1", roles = "USER") void rateMessageWrongSender() throws Exception { - var irisSession = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student10")); + var irisSession = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student1")); var message = new IrisMessage(); message.setSession(irisSession); message.setSentAt(ZonedDateTime.now()); @@ -242,10 +219,10 @@ void rateMessageWrongSender() throws Exception { } @Test - @WithMockUser(username = TEST_PREFIX + "student11", roles = "USER") + @WithMockUser(username = TEST_PREFIX + "student1", roles = "USER") void rateMessageWrongSession() throws Exception { - var irisSession1 = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student11")); - var irisSession2 = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student12")); + var irisSession1 = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student1")); + var irisSession2 = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student2")); var message = new IrisMessage(); message.setSession(irisSession1); message.setSentAt(ZonedDateTime.now()); @@ -256,49 +233,51 @@ void rateMessageWrongSession() throws Exception { } @Test - @WithMockUser(username = TEST_PREFIX + "student13", roles = "USER") + @WithMockUser(username = TEST_PREFIX + "student1", roles = "USER") void sendOneMessageBadRequest() throws Exception { - var irisSession = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student13")); - var messageToSend = new IrisMessage(); - messageToSend.setSession(irisSession); - messageToSend.setSentAt(ZonedDateTime.now()); - messageToSend.setContent(List.of(createMockContent(messageToSend), createMockContent(messageToSend), createMockContent(messageToSend))); + var irisSession = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student1")); + IrisMessage messageToSend = createDefaultMockMessage(irisSession); irisRequestMockProvider.mockMessageError(); var savedExercise = irisUtilTestService.setupTemplate(exercise, new LocalRepository("main")); - var exerciseParticipation = participationUtilService.addStudentParticipationForProgrammingExercise(savedExercise, TEST_PREFIX + "student13"); + var exerciseParticipation = participationUtilService.addStudentParticipationForProgrammingExercise(savedExercise, TEST_PREFIX + "student1"); irisUtilTestService.setupStudentParticipation(exerciseParticipation, new LocalRepository("main")); activateIrisFor(savedExercise); request.postWithResponseBody("/api/iris/sessions/" + irisSession.getId() + "/messages", messageToSend, IrisMessage.class, HttpStatus.CREATED); waitForIrisMessageToBeProcessed(); - verifyMessageWasSentOverWebsocket(TEST_PREFIX + "student13", irisSession.getId(), messageToSend); - verifyErrorWasSentOverWebsocket(TEST_PREFIX + "student13", irisSession.getId()); - verifyNothingElseWasSentOverWebsocket(TEST_PREFIX + "student13", irisSession.getId()); + verifyMessageWasSentOverWebsocket(TEST_PREFIX + "student1", irisSession.getId(), messageToSend); + verifyErrorWasSentOverWebsocket(TEST_PREFIX + "student1", irisSession.getId()); + verifyNothingElseWasSentOverWebsocket(TEST_PREFIX + "student1", irisSession.getId()); } @Test - @WithMockUser(username = TEST_PREFIX + "student14", roles = "USER") + @WithMockUser(username = TEST_PREFIX + "student1", roles = "USER") void sendOneMessageEmptyBody() throws Exception { - var irisSession = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student14")); - var messageToSend = new IrisMessage(); - messageToSend.setSession(irisSession); - messageToSend.setSentAt(ZonedDateTime.now()); - messageToSend.setContent(List.of(createMockContent(messageToSend), createMockContent(messageToSend), createMockContent(messageToSend))); + var irisSession = irisSessionService.createChatSessionForProgrammingExercise(exercise, userUtilService.getUserByLogin(TEST_PREFIX + "student1")); + IrisMessage messageToSend = createDefaultMockMessage(irisSession); irisRequestMockProvider.mockMessageResponse(null); var savedExercise = irisUtilTestService.setupTemplate(exercise, new LocalRepository("main")); - var exerciseParticipation = participationUtilService.addStudentParticipationForProgrammingExercise(savedExercise, TEST_PREFIX + "student14"); + var exerciseParticipation = participationUtilService.addStudentParticipationForProgrammingExercise(savedExercise, TEST_PREFIX + "student1"); irisUtilTestService.setupStudentParticipation(exerciseParticipation, new LocalRepository("main")); activateIrisFor(savedExercise); request.postWithResponseBody("/api/iris/sessions/" + irisSession.getId() + "/messages", messageToSend, IrisMessage.class, HttpStatus.CREATED); waitForIrisMessageToBeProcessed(); - verifyMessageWasSentOverWebsocket(TEST_PREFIX + "student14", irisSession.getId(), messageToSend); - verifyErrorWasSentOverWebsocket(TEST_PREFIX + "student14", irisSession.getId()); - verifyNothingElseWasSentOverWebsocket(TEST_PREFIX + "student14", irisSession.getId()); + verifyMessageWasSentOverWebsocket(TEST_PREFIX + "student1", irisSession.getId(), messageToSend); + verifyErrorWasSentOverWebsocket(TEST_PREFIX + "student1", irisSession.getId()); + verifyNothingElseWasSentOverWebsocket(TEST_PREFIX + "student1", irisSession.getId()); + } + + private IrisMessage createDefaultMockMessage(IrisSession irisSession) { + var messageToSend = new IrisMessage(); + messageToSend.setSession(irisSession); + messageToSend.setSentAt(ZonedDateTime.now()); + messageToSend.setContent(List.of(createMockContent(messageToSend), createMockContent(messageToSend), createMockContent(messageToSend))); + return messageToSend; } private IrisMessageContent createMockContent(IrisMessage message) {