BAEL-7142, add integration test for haikucat endpoint
This commit is contained in:
parent
684c91239d
commit
490612f9c7
|
@ -9,9 +9,9 @@
|
|||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<artifactId>parent-boot-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
<relativePath>../parent-boot-3</relativePath>
|
||||
</parent>
|
||||
|
||||
<repositories>
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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")));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
spring:
|
||||
ai:
|
||||
openai.api-key: ${OPEN_AI_KEY}
|
Loading…
Reference in New Issue