.
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
package kz.konturai.parser.controller;
|
||||
|
||||
import kz.konturai.parser.dto.ApiResponse;
|
||||
import kz.konturai.parser.dto.HealthCheckDto;
|
||||
import kz.konturai.parser.service.MarketItemService;
|
||||
import kz.konturai.parser.service.ParserManagerService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class HealthCheckControllerTest {
|
||||
|
||||
@Mock
|
||||
private MarketItemService marketItemService;
|
||||
|
||||
@Mock
|
||||
private ParserManagerService parserManagerService;
|
||||
|
||||
@InjectMocks
|
||||
private HealthCheckController healthCheckController;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
// Setup common test data
|
||||
}
|
||||
|
||||
@Test
|
||||
void testHealthCheck_Success() {
|
||||
// When
|
||||
ResponseEntity<ApiResponse<HealthCheckDto>> response = healthCheckController.healthCheck();
|
||||
|
||||
// Then
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertNotNull(response.getBody());
|
||||
assertTrue(response.getBody().isSuccess());
|
||||
assertEquals("UP", response.getBody().getData().getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckMongoConnection_Success() {
|
||||
// Given
|
||||
when(marketItemService.getTotalItemsCount()).thenReturn(100L);
|
||||
|
||||
// When
|
||||
ResponseEntity<ApiResponse<HealthCheckDto>> response = healthCheckController.checkMongoConnection();
|
||||
|
||||
// Then
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertNotNull(response.getBody());
|
||||
assertTrue(response.getBody().isSuccess());
|
||||
assertEquals("UP", response.getBody().getData().getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckMongoConnection_Error() {
|
||||
// Given
|
||||
when(marketItemService.getTotalItemsCount()).thenThrow(new RuntimeException("Connection failed"));
|
||||
|
||||
// When
|
||||
ResponseEntity<ApiResponse<HealthCheckDto>> response = healthCheckController.checkMongoConnection();
|
||||
|
||||
// Then
|
||||
assertEquals(HttpStatus.SERVICE_UNAVAILABLE, response.getStatusCode());
|
||||
assertNotNull(response.getBody());
|
||||
assertFalse(response.getBody().isSuccess());
|
||||
assertEquals("DOWN", response.getBody().getData().getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetSchedulerInfo_Success() {
|
||||
// Given
|
||||
List<String> mockParsers = List.of("kursiv", "kapital", "lsm");
|
||||
when(parserManagerService.getAvailableParsers()).thenReturn(mockParsers);
|
||||
when(parserManagerService.getParserCount()).thenReturn(3);
|
||||
|
||||
// When
|
||||
ResponseEntity<ApiResponse<Map<String, Object>>> response = healthCheckController.getSchedulerInfo();
|
||||
|
||||
// Then
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertNotNull(response.getBody());
|
||||
assertTrue(response.getBody().isSuccess());
|
||||
assertTrue(response.getBody().getData().containsKey("schedulerEnabled"));
|
||||
assertTrue(response.getBody().getData().containsKey("activeParsers"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetSystemInfo_Success() {
|
||||
// Given
|
||||
List<String> mockParsers = List.of("kursiv", "kapital");
|
||||
when(parserManagerService.getAvailableParsers()).thenReturn(mockParsers);
|
||||
when(parserManagerService.getParserCount()).thenReturn(2);
|
||||
when(marketItemService.getTotalItemsCount()).thenReturn(50L);
|
||||
when(marketItemService.getSourceStatistics()).thenReturn(Map.of("test", 25L));
|
||||
|
||||
// When
|
||||
ResponseEntity<ApiResponse<Map<String, Object>>> response = healthCheckController.getSystemInfo();
|
||||
|
||||
// Then
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertNotNull(response.getBody());
|
||||
assertTrue(response.getBody().isSuccess());
|
||||
assertTrue(response.getBody().getData().containsKey("parsers"));
|
||||
assertTrue(response.getBody().getData().containsKey("data"));
|
||||
assertTrue(response.getBody().getData().containsKey("system"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckParsersHealth_Success() {
|
||||
// Given
|
||||
List<String> mockParsers = List.of("kursiv", "kapital", "lsm");
|
||||
when(parserManagerService.getAvailableParsers()).thenReturn(mockParsers);
|
||||
|
||||
// When
|
||||
ResponseEntity<ApiResponse<Map<String, Object>>> response = healthCheckController.checkParsersHealth();
|
||||
|
||||
// Then
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertNotNull(response.getBody());
|
||||
assertTrue(response.getBody().isSuccess());
|
||||
assertEquals("UP", response.getBody().getData().get("status"));
|
||||
assertEquals(3, response.getBody().getData().get("totalParsers"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package kz.konturai.parser.controller;
|
||||
|
||||
import kz.konturai.parser.dto.ApiResponse;
|
||||
import kz.konturai.parser.model.MarketItem;
|
||||
import kz.konturai.parser.service.MarketItemService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
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.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class MarketItemControllerTest {
|
||||
|
||||
@Mock
|
||||
private MarketItemService marketItemService;
|
||||
|
||||
@InjectMocks
|
||||
private MarketItemController marketItemController;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
// Setup common test data
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetItems_Success() {
|
||||
// Given
|
||||
List<MarketItem> mockItems = new ArrayList<>();
|
||||
MarketItem item = new MarketItem();
|
||||
item.setTitle("Test News");
|
||||
mockItems.add(item);
|
||||
|
||||
Page<MarketItem> mockPage = new PageImpl<>(mockItems);
|
||||
when(marketItemService.getAllItems(any(Pageable.class))).thenReturn(mockPage);
|
||||
|
||||
// When
|
||||
ResponseEntity<ApiResponse<Page<MarketItem>>> response = marketItemController.getItems(null, null, null, null);
|
||||
|
||||
// Then
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertNotNull(response.getBody());
|
||||
assertTrue(response.getBody().isSuccess());
|
||||
assertEquals(1, response.getBody().getData().getContent().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetStats_Success() {
|
||||
// Given
|
||||
when(marketItemService.getTotalItemsCount()).thenReturn(100L);
|
||||
when(marketItemService.getSourceStatistics()).thenReturn(java.util.Map.of("test", 50L));
|
||||
|
||||
// When
|
||||
ResponseEntity<ApiResponse<kz.konturai.parser.dto.ParserStatsDto>> response = marketItemController.getStats();
|
||||
|
||||
// Then
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertNotNull(response.getBody());
|
||||
assertTrue(response.getBody().isSuccess());
|
||||
assertEquals(100L, response.getBody().getData().getTotalItems());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetSources_Success() {
|
||||
// Given
|
||||
List<String> mockSources = List.of("kursiv", "kapital", "lsm");
|
||||
when(marketItemService.getAllSourceNames()).thenReturn(mockSources);
|
||||
|
||||
// When
|
||||
ResponseEntity<ApiResponse<List<String>>> response = marketItemController.getSources();
|
||||
|
||||
// Then
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertNotNull(response.getBody());
|
||||
assertTrue(response.getBody().isSuccess());
|
||||
assertEquals(3, response.getBody().getData().size());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package kz.konturai.parser.controller;
|
||||
|
||||
import kz.konturai.parser.dto.ApiResponse;
|
||||
import kz.konturai.parser.dto.ParserResultDto;
|
||||
import kz.konturai.parser.model.MarketItem;
|
||||
import kz.konturai.parser.service.MarketItemService;
|
||||
import kz.konturai.parser.service.ParserManagerService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class ParserAdminControllerTest {
|
||||
|
||||
@Mock
|
||||
private ParserManagerService parserManagerService;
|
||||
|
||||
@Mock
|
||||
private MarketItemService marketItemService;
|
||||
|
||||
@InjectMocks
|
||||
private ParserAdminController parserAdminController;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
// Setup common test data
|
||||
}
|
||||
|
||||
@Test
|
||||
void testParseSource_Success() {
|
||||
// Given
|
||||
String sourceName = "kursiv";
|
||||
List<MarketItem> mockItems = new ArrayList<>();
|
||||
MarketItem item = new MarketItem();
|
||||
item.setTitle("Test News");
|
||||
mockItems.add(item);
|
||||
|
||||
when(parserManagerService.hasParser(sourceName)).thenReturn(true);
|
||||
when(parserManagerService.runParser(sourceName)).thenReturn(mockItems);
|
||||
when(marketItemService.getTotalItemsCount()).thenReturn(100L);
|
||||
|
||||
// When
|
||||
ResponseEntity<ApiResponse<ParserResultDto>> response = parserAdminController.parseSource(sourceName);
|
||||
|
||||
// Then
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertNotNull(response.getBody());
|
||||
assertTrue(response.getBody().isSuccess());
|
||||
assertEquals(sourceName, response.getBody().getData().getSource());
|
||||
assertEquals(1, response.getBody().getData().getProcessedItems());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testParseSource_ParserNotFound() {
|
||||
// Given
|
||||
String sourceName = "nonexistent";
|
||||
when(parserManagerService.hasParser(sourceName)).thenReturn(false);
|
||||
|
||||
// When
|
||||
ResponseEntity<ApiResponse<ParserResultDto>> response = parserAdminController.parseSource(sourceName);
|
||||
|
||||
// Then
|
||||
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
|
||||
assertNotNull(response.getBody());
|
||||
assertFalse(response.getBody().isSuccess());
|
||||
assertTrue(response.getBody().getMessage().contains("Парсер не найден"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testParseAll_Success() {
|
||||
// Given
|
||||
List<ParserResultDto> mockResults = new ArrayList<>();
|
||||
ParserResultDto result = new ParserResultDto("kursiv", 5, 100, "completed");
|
||||
mockResults.add(result);
|
||||
|
||||
when(parserManagerService.runAllParsers()).thenReturn(CompletableFuture.completedFuture(mockResults));
|
||||
|
||||
// When
|
||||
ResponseEntity<ApiResponse<List<ParserResultDto>>> response = parserAdminController.parseAll();
|
||||
|
||||
// Then
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertNotNull(response.getBody());
|
||||
assertTrue(response.getBody().isSuccess());
|
||||
assertEquals(1, response.getBody().getData().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAvailableParsers_Success() {
|
||||
// Given
|
||||
List<String> mockParsers = List.of("kursiv", "kapital", "lsm");
|
||||
when(parserManagerService.getAvailableParsers()).thenReturn(mockParsers);
|
||||
|
||||
// When
|
||||
ResponseEntity<ApiResponse<List<String>>> response = parserAdminController.getAvailableParsers();
|
||||
|
||||
// Then
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertNotNull(response.getBody());
|
||||
assertTrue(response.getBody().isSuccess());
|
||||
assertEquals(3, response.getBody().getData().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckParserExists_Success() {
|
||||
// Given
|
||||
String sourceName = "kursiv";
|
||||
when(parserManagerService.hasParser(sourceName)).thenReturn(true);
|
||||
|
||||
// When
|
||||
ResponseEntity<ApiResponse<Boolean>> response = parserAdminController.checkParserExists(sourceName);
|
||||
|
||||
// Then
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertNotNull(response.getBody());
|
||||
assertTrue(response.getBody().isSuccess());
|
||||
assertTrue(response.getBody().getData());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package kz.konturai.parser.service;
|
||||
|
||||
import kz.konturai.parser.model.MarketItem;
|
||||
import kz.konturai.parser.repository.MarketItemRepository;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class LsmParserServiceTest {
|
||||
|
||||
@Mock
|
||||
private MarketItemRepository marketItemRepository;
|
||||
|
||||
@InjectMocks
|
||||
private LsmParserService lsmParserService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
ReflectionTestUtils.setField(lsmParserService, "rssFeedUrl", "https://lsm.kz/rss");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetLsmItemsCount() {
|
||||
// Given
|
||||
when(marketItemRepository.countBySourceName("LSM.kz")).thenReturn(5L);
|
||||
|
||||
// When
|
||||
long count = lsmParserService.getLsmItemsCount();
|
||||
|
||||
// Then
|
||||
assertEquals(5L, count);
|
||||
verify(marketItemRepository).countBySourceName("LSM.kz");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllLsmItems() {
|
||||
// Given
|
||||
List<MarketItem> mockItems = new ArrayList<>();
|
||||
MarketItem item1 = new MarketItem();
|
||||
item1.setSourceName("LSM.kz");
|
||||
item1.setTitle("Test News 1");
|
||||
mockItems.add(item1);
|
||||
|
||||
when(marketItemRepository.findBySourceName("LSM.kz")).thenReturn(mockItems);
|
||||
|
||||
// When
|
||||
List<MarketItem> result = lsmParserService.getAllLsmItems();
|
||||
|
||||
// Then
|
||||
assertEquals(1, result.size());
|
||||
assertEquals("LSM.kz", result.get(0).getSourceName());
|
||||
verify(marketItemRepository).findBySourceName("LSM.kz");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testParseAndSaveRssFeed_WhenFeedIsNull() {
|
||||
// When
|
||||
List<MarketItem> result = lsmParserService.parseAndSaveRssFeed();
|
||||
|
||||
// Then
|
||||
assertNotNull(result);
|
||||
assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGenerateHash() throws Exception {
|
||||
// Given
|
||||
String url = "https://example.com/news/1";
|
||||
String title = "Test News";
|
||||
|
||||
// When
|
||||
String hash1 = (String) ReflectionTestUtils.invokeMethod(lsmParserService, "generateHash", url, title);
|
||||
String hash2 = (String) ReflectionTestUtils.invokeMethod(lsmParserService, "generateHash", url, title);
|
||||
|
||||
// Then
|
||||
assertNotNull(hash1);
|
||||
assertNotNull(hash2);
|
||||
assertEquals(hash1, hash2); // Same input should produce same hash
|
||||
assertNotEquals(hash1,
|
||||
(String) ReflectionTestUtils.invokeMethod(lsmParserService, "generateHash", url, "Different Title"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package kz.konturai.parser.service;
|
||||
|
||||
import kz.konturai.parser.model.MarketItem;
|
||||
import kz.konturai.parser.repository.MarketItemRepository;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class RbcParserServiceTest {
|
||||
|
||||
@Mock
|
||||
private MarketItemRepository marketItemRepository;
|
||||
|
||||
@InjectMocks
|
||||
private RbcParserService rbcParserService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
ReflectionTestUtils.setField(rbcParserService, "rssFeedUrl",
|
||||
"https://static.feed.rbc.ru/rbc/logical/footer/news.rss");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetRbcItemsCount() {
|
||||
// Given
|
||||
when(marketItemRepository.countBySourceName("РБК")).thenReturn(10L);
|
||||
|
||||
// When
|
||||
long count = rbcParserService.getRbcItemsCount();
|
||||
|
||||
// Then
|
||||
assertEquals(10L, count);
|
||||
verify(marketItemRepository).countBySourceName("РБК");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllRbcItems() {
|
||||
// Given
|
||||
List<MarketItem> mockItems = new ArrayList<>();
|
||||
MarketItem item1 = new MarketItem();
|
||||
item1.setSourceName("РБК");
|
||||
item1.setTitle("Test News 1");
|
||||
mockItems.add(item1);
|
||||
|
||||
when(marketItemRepository.findBySourceName("РБК")).thenReturn(mockItems);
|
||||
|
||||
// When
|
||||
List<MarketItem> result = rbcParserService.getAllRbcItems();
|
||||
|
||||
// Then
|
||||
assertEquals(1, result.size());
|
||||
assertEquals("РБК", result.get(0).getSourceName());
|
||||
verify(marketItemRepository).findBySourceName("РБК");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testParseAndSaveRssFeed_WhenFeedIsNull() {
|
||||
// When
|
||||
List<MarketItem> result = rbcParserService.parseAndSaveRssFeed();
|
||||
|
||||
// Then
|
||||
assertNotNull(result);
|
||||
assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGenerateHash() throws Exception {
|
||||
// Given
|
||||
String url = "https://example.com/news/1";
|
||||
String title = "Test News";
|
||||
|
||||
// When
|
||||
String hash1 = (String) ReflectionTestUtils.invokeMethod(rbcParserService, "generateHash", url, title);
|
||||
String hash2 = (String) ReflectionTestUtils.invokeMethod(rbcParserService, "generateHash", url, title);
|
||||
|
||||
// Then
|
||||
assertNotNull(hash1);
|
||||
assertNotNull(hash2);
|
||||
assertEquals(hash1, hash2); // Same input should produce same hash
|
||||
assertNotEquals(hash1,
|
||||
(String) ReflectionTestUtils.invokeMethod(rbcParserService, "generateHash", url, "Different Title"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package kz.konturai.parser.service;
|
||||
|
||||
import kz.konturai.parser.model.MarketItem;
|
||||
import kz.konturai.parser.repository.MarketItemRepository;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class VedomostiParserServiceTest {
|
||||
|
||||
@Mock
|
||||
private MarketItemRepository marketItemRepository;
|
||||
|
||||
@InjectMocks
|
||||
private VedomostiParserService vedomostiParserService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
ReflectionTestUtils.setField(vedomostiParserService, "rssFeedUrl",
|
||||
"https://www.vedomosti.ru/rss/rubric/technology/internet");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetVedomostiItemsCount() {
|
||||
// Given
|
||||
when(marketItemRepository.countBySourceName("Ведомости")).thenReturn(15L);
|
||||
|
||||
// When
|
||||
long count = vedomostiParserService.getVedomostiItemsCount();
|
||||
|
||||
// Then
|
||||
assertEquals(15L, count);
|
||||
verify(marketItemRepository).countBySourceName("Ведомости");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllVedomostiItems() {
|
||||
// Given
|
||||
List<MarketItem> mockItems = new ArrayList<>();
|
||||
MarketItem item1 = new MarketItem();
|
||||
item1.setSourceName("Ведомости");
|
||||
item1.setTitle("Test News 1");
|
||||
mockItems.add(item1);
|
||||
|
||||
when(marketItemRepository.findBySourceName("Ведомости")).thenReturn(mockItems);
|
||||
|
||||
// When
|
||||
List<MarketItem> result = vedomostiParserService.getAllVedomostiItems();
|
||||
|
||||
// Then
|
||||
assertEquals(1, result.size());
|
||||
assertEquals("Ведомости", result.get(0).getSourceName());
|
||||
verify(marketItemRepository).findBySourceName("Ведомости");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testParseAndSaveRssFeed_WhenFeedIsNull() {
|
||||
// When
|
||||
List<MarketItem> result = vedomostiParserService.parseAndSaveRssFeed();
|
||||
|
||||
// Then
|
||||
assertNotNull(result);
|
||||
assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGenerateHash() throws Exception {
|
||||
// Given
|
||||
String url = "https://example.com/news/1";
|
||||
String title = "Test News";
|
||||
|
||||
// When
|
||||
String hash1 = (String) ReflectionTestUtils.invokeMethod(vedomostiParserService, "generateHash", url, title);
|
||||
String hash2 = (String) ReflectionTestUtils.invokeMethod(vedomostiParserService, "generateHash", url, title);
|
||||
|
||||
// Then
|
||||
assertNotNull(hash1);
|
||||
assertNotNull(hash2);
|
||||
assertEquals(hash1, hash2); // Same input should produce same hash
|
||||
assertNotEquals(hash1, (String) ReflectionTestUtils.invokeMethod(vedomostiParserService, "generateHash", url,
|
||||
"Different Title"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user