This commit is contained in:
root
2025-12-21 10:31:30 +05:00
parent c4484cfafd
commit efcfa1dc8f
12 changed files with 370 additions and 47 deletions
@@ -2,8 +2,10 @@ package kz.konturai.parser;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
@SpringBootTest
@EnabledIfEnvironmentVariable(named = "RUN_INTEGRATION_TESTS", matches = "true")
class ParserApplicationTests {
@Test
@@ -11,7 +11,7 @@ import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -45,10 +45,12 @@ class MarketItemControllerTest {
mockItems.add(item);
Page<MarketItem> mockPage = new PageImpl<>(mockItems);
when(marketItemService.getAllItems(any(Pageable.class))).thenReturn(mockPage);
when(marketItemService.parseDate(any())).thenReturn(null);
when(marketItemService.getAllItems(any())).thenReturn(mockPage);
// When
ResponseEntity<ApiResponse<Page<MarketItem>>> response = marketItemController.getItems(null, null, null, null);
ResponseEntity<ApiResponse<Page<MarketItem>>> response = marketItemController.getItems(null, null, null,
PageRequest.of(0, 20));
// Then
assertEquals(HttpStatus.OK, response.getStatusCode());
@@ -2,6 +2,7 @@ package kz.konturai.parser.scheduler;
import kz.konturai.parser.service.KursivParserService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
@@ -9,6 +10,7 @@ import org.springframework.test.context.TestPropertySource;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
@EnabledIfEnvironmentVariable(named = "RUN_INTEGRATION_TESTS", matches = "true")
@TestPropertySource(properties = {
"spring.data.mongodb.host=localhost",
"spring.data.mongodb.port=27017",
@@ -11,6 +11,7 @@ import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import static org.mockito.ArgumentMatchers.any;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
@@ -79,10 +80,11 @@ class EmailServiceTest {
byte[] pdfBytes = "test pdf content".getBytes();
String filename = "test-report.pdf";
when(mailSender.createMimeMessage()).thenThrow(new MessagingException("SMTP error"));
when(mailSender.createMimeMessage()).thenThrow(new RuntimeException("SMTP error"));
// Act
emailService.sendReportByEmail(recipientEmail, reportTitle, authorName, companyName, pdfBytes, filename);
assertThrows(RuntimeException.class, () -> emailService.sendReportByEmail(
recipientEmail, reportTitle, authorName, companyName, pdfBytes, filename));
// Assert
verify(mailSender, times(1)).createMimeMessage();
@@ -6,12 +6,14 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
@EnabledIfEnvironmentVariable(named = "RUN_INTEGRATION_TESTS", matches = "true")
@TestPropertySource(properties = {
"spring.data.mongodb.host=92.38.48.166",
"spring.data.mongodb.port=27017",
@@ -6,12 +6,14 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
@EnabledIfEnvironmentVariable(named = "RUN_INTEGRATION_TESTS", matches = "true")
@TestPropertySource(properties = {
"spring.data.mongodb.host=localhost",
"spring.data.mongodb.port=27017",
@@ -11,6 +11,7 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.TestPropertySource;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import java.time.LocalDateTime;
import java.util.List;
@@ -19,6 +20,7 @@ import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
@EnabledIfEnvironmentVariable(named = "RUN_INTEGRATION_TESTS", matches = "true")
@TestPropertySource(properties = {
"spring.data.mongodb.host=92.38.48.166",
"spring.data.mongodb.port=27017",
@@ -0,0 +1,65 @@
package kz.konturai.parser.service;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.reflect.Method;
import org.junit.jupiter.api.Test;
import kz.konturai.parser.model.DetailLevel;
class MarketingAnalysisServiceDetailLevelPromptTest {
// Keep this test free of Spring/Mockito to avoid agent-attach requirements.
private final MarketingAnalysisService marketingAnalysisService = new MarketingAnalysisService(null, null, null,
null);
@Test
void appendDetailLevelDirectives_shouldForceBullets_forKratko() throws Exception {
Method m = MarketingAnalysisService.class.getDeclaredMethod(
"appendDetailLevelDirectives",
StringBuilder.class,
DetailLevel.class,
String.class);
m.setAccessible(true);
StringBuilder sb = new StringBuilder();
m.invoke(marketingAnalysisService, sb, DetailLevel.КРАТКО, "summary");
String out = sb.toString();
assertTrue(out.contains("РЕЖИМ КРАТКО"));
assertTrue(out.contains("bullet points"));
assertTrue(out.contains("Игнорируй любые требования"));
}
@Test
void buildSystemPromptForSection_shouldReturnNull_whenNotPodrobno() throws Exception {
Method m = MarketingAnalysisService.class.getDeclaredMethod(
"buildSystemPromptForSection",
DetailLevel.class,
String.class);
m.setAccessible(true);
Object out = m.invoke(marketingAnalysisService, DetailLevel.КРАТКО, "audience");
assertNull(out);
}
@Test
void buildSystemPromptForSection_shouldIncludeAcademicDirective_whenPodrobno() throws Exception {
Method m = MarketingAnalysisService.class.getDeclaredMethod(
"buildSystemPromptForSection",
DetailLevel.class,
String.class);
m.setAccessible(true);
String out = (String) m.invoke(marketingAnalysisService, DetailLevel.ПОДРОБНО, "audience");
assertNotNull(out);
assertTrue(out.contains("глубокий академический анализ"));
assertTrue(out.contains("LTV"));
assertTrue(out.contains("Porter's Five Forces"));
}
}
@@ -0,0 +1,55 @@
package kz.konturai.parser.service;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
class OpenAIAnalyticsServiceRequestBodyTest {
@Test
void buildChatMessages_shouldPrependSystemMessage_whenProvided() {
OpenAIAnalyticsService svc = new OpenAIAnalyticsService("http://localhost");
List<Map<String, Object>> messages = svc.buildChatMessages("USER", "SYSTEM");
assertEquals(2, messages.size());
assertEquals("system", messages.get(0).get("role"));
assertEquals("SYSTEM", messages.get(0).get("content"));
assertEquals("user", messages.get(1).get("role"));
assertEquals("USER", messages.get(1).get("content"));
}
@Test
void buildChatMessages_shouldNotAddSystemMessage_whenBlank() {
OpenAIAnalyticsService svc = new OpenAIAnalyticsService("http://localhost");
List<Map<String, Object>> messages = svc.buildChatMessages("USER", " ");
assertEquals(1, messages.size());
assertEquals("user", messages.get(0).get("role"));
assertEquals("USER", messages.get(0).get("content"));
}
@Test
void buildChatCompletionsRequestBody_shouldReflectMaxTokensOverrideAndMessages() {
OpenAIAnalyticsService svc = new OpenAIAnalyticsService("http://localhost");
List<Map<String, Object>> messages = List.of(
Map.of("role", "system", "content", "SYS"),
Map.of("role", "user", "content", "USR"));
Map<String, Object> body = svc.buildChatCompletionsRequestBody("gpt-test", messages, 24000, 0.3);
assertEquals("gpt-test", body.get("model"));
assertEquals(24000, body.get("max_tokens"));
assertNotNull(body.get("messages"));
assertEquals(messages, body.get("messages"));
assertEquals(0.3, ((Number) body.get("temperature")).doubleValue(), 1e-9);
}
}
@@ -0,0 +1,3 @@
mock-maker-subclass