From 571d8e2aca1490d7ef6da858fc12263ab1238ce3 Mon Sep 17 00:00:00 2001 From: Ekatereana Date: Thu, 30 Nov 2023 16:17:45 +0200 Subject: [PATCH] BAEL-7142, fix the tests to the manual after aligment --- .../web/PoetryControllerIntegrationTest.java | 87 ------------------- .../ai/web/PoetryControllerManualTest.java | 51 +++++++++++ 2 files changed, 51 insertions(+), 87 deletions(-) delete mode 100644 spring-ai/src/test/java/com/baeldung/spring/ai/web/PoetryControllerIntegrationTest.java create mode 100644 spring-ai/src/test/java/com/baeldung/spring/ai/web/PoetryControllerManualTest.java 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 deleted file mode 100644 index b951c53f1e..0000000000 --- a/spring-ai/src/test/java/com/baeldung/spring/ai/web/PoetryControllerIntegrationTest.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.baeldung.spring.ai.web; - -import com.baeldung.spring.ai.dto.PoetryDto; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.theokanning.openai.OpenAiError; -import com.theokanning.openai.OpenAiHttpException; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.ai.client.AiClient; -import org.springframework.ai.client.AiResponse; -import org.springframework.ai.client.Generation; -import org.springframework.ai.prompt.Prompt; -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.boot.test.mock.mockito.MockBean; -import org.springframework.boot.test.mock.mockito.SpyBean; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.test.web.servlet.MockMvc; - -import java.util.List; -import java.util.Optional; - -import static org.hamcrest.Matchers.*; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.when; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; - -@AutoConfigureMockMvc -@RunWith(SpringRunner.class) -@SpringBootTest -public class PoetryControllerIntegrationTest { - - public static final String GENERATED_HAIKU = "generated haiku"; - public static final String TEST_TITLE = "test_title"; - public static final String SOME_POETRY = "some poetry"; - public static final String TEST_GENRE = "test_genre"; - public static final String TEST_THEME = "test_theme"; - @Autowired - private MockMvc mockMvc; - - @Autowired - private ObjectMapper objectMapper; - - @MockBean - private AiClient aiClient; - - @Test - public void givenGetCatHaiku_whenCallingAiClient_thenCorrect() throws Exception { - when(aiClient.generate(anyString())).thenReturn(GENERATED_HAIKU); - mockMvc.perform(get("/ai/cathaiku")) - .andExpect(status().isOk()) - .andExpect(content().string(GENERATED_HAIKU)); - } - - @Test - public void givenGetPoetryWithGenreAndTheme_whenCallingAiClient_thenCorrect() throws Exception { - PoetryDto poetryDto = new PoetryDto( - TEST_TITLE, - SOME_POETRY, - TEST_GENRE, - TEST_THEME - ); - when(aiClient.generate((Prompt) any(Prompt.class))).thenReturn( - new AiResponse(List.of(new Generation(objectMapper.writeValueAsString(poetryDto))))); - String genre = "lyric"; - String theme = "coffee"; - mockMvc.perform(get("/ai/poetry?genre={genre}&theme={theme}", genre, theme)) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.genre").value(poetryDto.genre())) - .andExpect(jsonPath("$.theme").value(poetryDto.theme())) - .andExpect(jsonPath("$.poetry").value(poetryDto.poetry())) - .andExpect(jsonPath("$.title").value(poetryDto.title())); - } - - @Test - public void givenGetCatHaiku_whenCallingAiClientWithIncorrectToken_thenUnauthorized() throws Exception { - when(aiClient.generate(anyString())).thenThrow(new OpenAiHttpException( - new OpenAiError(new OpenAiError.OpenAiErrorDetails()), - new Exception(), - 401)); - mockMvc.perform(get("/ai/cathaiku")) - .andExpect(status().isUnauthorized()) - .andExpect(jsonPath("$.title").value(ExceptionTranslator.OPEN_AI_CLIENT_RAISED_EXCEPTION)); - } -} diff --git a/spring-ai/src/test/java/com/baeldung/spring/ai/web/PoetryControllerManualTest.java b/spring-ai/src/test/java/com/baeldung/spring/ai/web/PoetryControllerManualTest.java new file mode 100644 index 0000000000..d1750bdb4a --- /dev/null +++ b/spring-ai/src/test/java/com/baeldung/spring/ai/web/PoetryControllerManualTest.java @@ -0,0 +1,51 @@ +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.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +@AutoConfigureMockMvc +@RunWith(SpringRunner.class) +@SpringBootTest +public class PoetryControllerManualTest { + + @Autowired + private MockMvc mockMvc; + + @Autowired + private ObjectMapper objectMapper; + + @Autowired + private AiClient aiClient; + + @Test + public void givenGetCatHaiku_whenCallingAiClient_thenCorrect() throws Exception { + mockMvc.perform(get("/ai/cathaiku")) + .andExpect(status().isOk()) + .andExpect(content().string(containsStringIgnoringCase("cat"))); + } + + @Test + public void givenGetPoetryWithGenreAndTheme_whenCallingAiClient_thenCorrect() throws Exception { + String genre = "lyric"; + String theme = "coffee"; + mockMvc.perform(get("/ai/poetry?genre={genre}&theme={theme}", genre, theme)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.genre").value(containsStringIgnoringCase(genre))) + .andExpect(jsonPath("$.theme").value(containsStringIgnoringCase(theme))) + .andExpect(jsonPath("$.poetry").isNotEmpty()) + .andExpect(jsonPath("$.title").exists()); + } +} + +}