BAEL-7142, finilize the pr, refresh test cases
This commit is contained in:
parent
2d0c011456
commit
8492c4dffa
|
@ -1,3 +1,3 @@
|
||||||
package com.baeldung.spring.ai.dto;
|
package com.baeldung.spring.ai.dto;
|
||||||
|
|
||||||
public record PoetryDto (String title, String poetry){}
|
public record PoetryDto (String title, String poetry, String genre, String theme){}
|
||||||
|
|
|
@ -14,7 +14,9 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||||
@Service
|
@Service
|
||||||
public class PoetryServiceImpl implements PoetryService {
|
public class PoetryServiceImpl implements PoetryService {
|
||||||
|
|
||||||
public static final String WRITE_ME_HAIKU_ABOUT_CAT = "Write me Haiku about cat, haiku should contain word cat";
|
public static final String WRITE_ME_HAIKU_ABOUT_CAT = """
|
||||||
|
Write me Haiku about cat,
|
||||||
|
haiku should start with the word cat obligatory""";
|
||||||
private final AiClient aiClient;
|
private final AiClient aiClient;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
|
|
|
@ -7,13 +7,21 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@RestControllerAdvice
|
@RestControllerAdvice
|
||||||
public class ExceptionTranslator extends ResponseEntityExceptionHandler {
|
public class ExceptionTranslator extends ResponseEntityExceptionHandler {
|
||||||
|
|
||||||
|
public static final String OPEN_AI_CLIENT_RAISED_EXCEPTION = "Open AI client raised exception";
|
||||||
|
|
||||||
@ExceptionHandler(OpenAiHttpException.class)
|
@ExceptionHandler(OpenAiHttpException.class)
|
||||||
ProblemDetail handleOpenAiHttpException(OpenAiHttpException ex){
|
ProblemDetail handleOpenAiHttpException(OpenAiHttpException ex) {
|
||||||
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, ex.getMessage());
|
HttpStatus status = Optional
|
||||||
problemDetail.setTitle("Open AI client raised exception");
|
.ofNullable(HttpStatus.resolve(ex.statusCode))
|
||||||
|
.orElse(HttpStatus.BAD_REQUEST);
|
||||||
|
|
||||||
|
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(status, ex.getMessage());
|
||||||
|
problemDetail.setTitle(OPEN_AI_CLIENT_RAISED_EXCEPTION);
|
||||||
return problemDetail;
|
return problemDetail;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
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.DynamicPropertyRegistry;
|
||||||
|
import org.springframework.test.context.DynamicPropertySource;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
|
import static java.lang.String.format;
|
||||||
|
import static org.hamcrest.Matchers.containsStringIgnoringCase;
|
||||||
|
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 ExceptionTranslatorIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ObjectMapper objectMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private AiClient aiClient;
|
||||||
|
|
||||||
|
@DynamicPropertySource
|
||||||
|
static void setupTestcontainerProperties(DynamicPropertyRegistry registry) {
|
||||||
|
registry.add("spring.ai.openai.api-key", () -> "incorrect_token");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenGetCatHaiku_whenCallingAiClientWithIncorrectToken_thenUnauthorized() throws Exception {
|
||||||
|
mockMvc.perform(get("/ai/cathaiku"))
|
||||||
|
.andExpect(status().isUnauthorized())
|
||||||
|
.andExpect(jsonPath("$.title").value(ExceptionTranslator.OPEN_AI_CLIENT_RAISED_EXCEPTION));
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,9 +11,8 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.*;
|
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;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||||
|
|
||||||
@AutoConfigureMockMvc
|
@AutoConfigureMockMvc
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
|
@ -30,9 +29,21 @@ public class PoetryControllerIntegrationTest {
|
||||||
private AiClient aiClient;
|
private AiClient aiClient;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getCatHaikuTest() throws Exception {
|
public void givenGetCatHaiku_whenCallingAiClient_thenCorrect() throws Exception {
|
||||||
mockMvc.perform(get("/ai/cathaiku"))
|
mockMvc.perform(get("/ai/cathaiku"))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(content().string(containsStringIgnoringCase("cat")));
|
.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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue