test: Add OpenRouterService integration test with SpringBootTest
This commit is contained in:
@@ -3,15 +3,27 @@ package ru.cathub.telegabot.configuration;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
@ConfigurationProperties(prefix = "openrouter.api")
|
||||
@Getter
|
||||
@Setter
|
||||
public class OpenRouterConfig {
|
||||
private String key;
|
||||
private String url;
|
||||
private String model;
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package ru.cathub.telegabot.service.impl;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -20,7 +21,7 @@ import java.util.Map;
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class OpenRouterServiceImpl implements OpenRouterService {
|
||||
|
||||
|
||||
private final OpenRouterConfig config;
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package ru.cathub.telegabot.service;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.web.client.RestClientException;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import ru.cathub.telegabot.exception.OpenRouterException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@SpringBootTest
|
||||
class OpenRouterServiceIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private OpenRouterService openRouterService;
|
||||
|
||||
@MockitoBean
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@Test
|
||||
void shouldReturnValidResponse() throws OpenRouterException {
|
||||
// Mock response
|
||||
Map<String, Object> mockResponse = Map.of(
|
||||
"choices", List.of(Map.of(
|
||||
"message", Map.of("content", "Test response")
|
||||
))
|
||||
);
|
||||
|
||||
when(restTemplate.postForEntity(anyString(), any(), eq(Map.class)))
|
||||
.thenReturn(new ResponseEntity<>(mockResponse, HttpStatus.OK));
|
||||
|
||||
String response = openRouterService.getChatResponse("Test message");
|
||||
assertEquals("Test response", response);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldThrowExceptionOnApiError() {
|
||||
when(restTemplate.postForEntity(anyString(), any(), eq(Map.class)))
|
||||
.thenThrow(new RestClientException("API error"));
|
||||
|
||||
assertThrows(OpenRouterException.class,
|
||||
() -> openRouterService.getChatResponse("Test message"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
openrouter.api.url=https://test-url
|
||||
openrouter.api.model=test-model
|
||||
openrouter.api.key=test-key
|
||||
Reference in New Issue
Block a user