From 490612f9c7fe7db5bfb02165589848d3a6a68e0c Mon Sep 17 00:00:00 2001 From: Ekatereana Date: Sun, 26 Nov 2023 22:48:58 +0200 Subject: [PATCH] BAEL-7142, add integration test for haikucat endpoint --- spring-ai/pom.xml | 4 +- .../spring/ai/web/ExceptionTranslator.java | 19 ++++++++++ .../spring/ai/web/PoetryController.java | 2 +- .../web/PoetryControllerIntegrationTest.java | 38 +++++++++++++++++++ spring-ai/src/test/resources/application.yml | 3 ++ 5 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 spring-ai/src/main/java/com/baeldung/spring/ai/web/ExceptionTranslator.java create mode 100644 spring-ai/src/test/java/com/baeldung/spring/ai/web/PoetryControllerIntegrationTest.java create mode 100644 spring-ai/src/test/resources/application.yml diff --git a/spring-ai/pom.xml b/spring-ai/pom.xml index 52eba30508..572941358b 100644 --- a/spring-ai/pom.xml +++ b/spring-ai/pom.xml @@ -9,9 +9,9 @@ com.baeldung - parent-boot-2 + parent-boot-3 0.0.1-SNAPSHOT - ../parent-boot-2 + ../parent-boot-3 diff --git a/spring-ai/src/main/java/com/baeldung/spring/ai/web/ExceptionTranslator.java b/spring-ai/src/main/java/com/baeldung/spring/ai/web/ExceptionTranslator.java new file mode 100644 index 0000000000..98c6a35e94 --- /dev/null +++ b/spring-ai/src/main/java/com/baeldung/spring/ai/web/ExceptionTranslator.java @@ -0,0 +1,19 @@ +package com.baeldung.spring.ai.web; + +import com.theokanning.openai.OpenAiHttpException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ProblemDetail; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; + +@RestControllerAdvice +public class ExceptionTranslator extends ResponseEntityExceptionHandler { + + @ExceptionHandler(OpenAiHttpException.class) + ProblemDetail handleOpenAiHttpException(OpenAiHttpException ex){ + ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, ex.getMessage()); + problemDetail.setTitle("Open AI client raised exception"); + return problemDetail; + } +} diff --git a/spring-ai/src/main/java/com/baeldung/spring/ai/web/PoetryController.java b/spring-ai/src/main/java/com/baeldung/spring/ai/web/PoetryController.java index b6e510c65b..7c612e5fee 100644 --- a/spring-ai/src/main/java/com/baeldung/spring/ai/web/PoetryController.java +++ b/spring-ai/src/main/java/com/baeldung/spring/ai/web/PoetryController.java @@ -16,7 +16,7 @@ import org.springframework.web.bind.annotation.RestController; @RequestMapping("ai") public class PoetryController { - public static final String WRITE_ME_HAIKU_ABOUT_CAT = "Write me Haiku about cat"; + public static final String WRITE_ME_HAIKU_ABOUT_CAT = "Write me Haiku about cat, haiku should contain word cat"; private final AiClient aiClient; @Autowired diff --git a/spring-ai/src/test/java/com/baeldung/spring/ai/web/PoetryControllerIntegrationTest.java b/spring-ai/src/test/java/com/baeldung/spring/ai/web/PoetryControllerIntegrationTest.java new file mode 100644 index 0000000000..782eb6aa86 --- /dev/null +++ b/spring-ai/src/test/java/com/baeldung/spring/ai/web/PoetryControllerIntegrationTest.java @@ -0,0 +1,38 @@ +package com.baeldung.spring.ai.web; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.ai.client.AiClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; + +import static org.hamcrest.Matchers.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; + +@AutoConfigureMockMvc +@RunWith(SpringRunner.class) +@SpringBootTest +public class PoetryControllerIntegrationTest { + + @Autowired + private MockMvc mockMvc; + + @Autowired + private ObjectMapper objectMapper; + + @Autowired + private AiClient aiClient; + + @Test + public void getCatHaikuTest() throws Exception { + mockMvc.perform(get("/ai/cathaiku")) + .andExpect(status().isOk()) + .andExpect(content().string(containsStringIgnoringCase("cat"))); + } +} diff --git a/spring-ai/src/test/resources/application.yml b/spring-ai/src/test/resources/application.yml new file mode 100644 index 0000000000..dfc8294e45 --- /dev/null +++ b/spring-ai/src/test/resources/application.yml @@ -0,0 +1,3 @@ +spring: + ai: + openai.api-key: ${OPEN_AI_KEY} \ No newline at end of file