| | |
| | | import static org.junit.jupiter.api.Assertions.assertFalse; |
| | | import static org.junit.jupiter.api.Assertions.assertNotNull; |
| | | import static org.junit.jupiter.api.Assertions.assertThrows; |
| | | import static org.junit.jupiter.api.Assertions.assertTrue; |
| | | |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | |
| | | import org.junit.jupiter.api.Test; |
| | | import org.junit.jupiter.api.extension.ExtendWith; |
| | | import org.mockito.ArgumentCaptor; |
| | | import org.mockito.InjectMocks; |
| | | import org.mockito.Mock; |
| | | import org.mockito.junit.jupiter.MockitoExtension; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.system.domain.TCollector; |
| | | import com.ruoyi.system.mapper.TCollectorMapper; |
| | | import com.ruoyi.device.discovery.InMemoryDeviceDiscoveryCandidateStore; |
| | | import com.ruoyi.device.discovery.DeviceDiscoveryCandidateStore.CollectorCandidate; |
| | | |
| | | /** |
| | | * 采集器 Service 单元测试。 |
| | |
| | | @Mock |
| | | private TCollectorMapper collectorMapper; |
| | | |
| | | @InjectMocks |
| | | private TCollectorServiceImpl collectorService; |
| | | private InMemoryDeviceDiscoveryCandidateStore candidateStore; |
| | | |
| | | private TCollector collector; |
| | | |
| | | @BeforeEach |
| | | void setUp() |
| | | { |
| | | candidateStore = new InMemoryDeviceDiscoveryCandidateStore(); |
| | | collectorService = new TCollectorServiceImpl(collectorMapper, candidateStore); |
| | | java.time.Instant now = java.time.Instant.now(); |
| | | candidateStore.replaceCollectors("dev-sim", java.util.List.of(new CollectorCandidate( |
| | | "dev-sim", "COL-001", "192.168.1.10", "AA:BB:CC:DD:EE:FF", "一号采集器", |
| | | "开发模拟器", "Virtual Collector", 8, true, now, now.plusSeconds(60)))); |
| | | collector = new TCollector(); |
| | | collector.setCollectorId(1L); |
| | | collector.setCollectorCode("COL-001"); |
| | |
| | | |
| | | assertFalse(insertSql.contains("update_by")); |
| | | assertFalse(insertSql.contains("update_time")); |
| | | } |
| | | } |
| | | |
| | | @Test |
| | | void registeredCollectorCodeSqlShouldIgnoreDeletedRows() throws IOException |
| | | { |
| | | try (InputStream input = getClass().getResourceAsStream( |
| | | "/mapper/system/TCollectorMapper.xml")) |
| | | { |
| | | assertNotNull(input); |
| | | String mapperXml = new String(input.readAllBytes(), StandardCharsets.UTF_8); |
| | | int selectStart = mapperXml.indexOf("<select id=\"selectRegisteredCollectorCodes\""); |
| | | assertTrue(selectStart >= 0); |
| | | int selectEnd = mapperXml.indexOf("</select>", selectStart); |
| | | String selectSql = mapperXml.substring(selectStart, selectEnd); |
| | | |
| | | assertTrue(selectSql.contains("where del_flag = '0'")); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | @Test |
| | | void insertCollectorShouldRejectInvalidIpAddress() |
| | | void insertCollectorShouldValidateAuthoritativeCandidateTopology() |
| | | { |
| | | collector.setCollectorId(null); |
| | | collector.setIpAddress("invalid-host"); |
| | | collector.setMacAddress("invalid-mac"); |
| | | collector.setChannelCount(99); |
| | | when(collectorMapper.insertCollector(any(TCollector.class))).thenReturn(1); |
| | | |
| | | ServiceException exception = assertThrows(ServiceException.class, |
| | | () -> collectorService.insertCollector(collector)); |
| | | assertEquals(1, collectorService.insertCollector(collector)); |
| | | |
| | | assertEquals("IP地址格式不正确", exception.getMessage()); |
| | | verify(collectorMapper, never()).insertCollector(any(TCollector.class)); |
| | | ArgumentCaptor<TCollector> captor = ArgumentCaptor.forClass(TCollector.class); |
| | | verify(collectorMapper).insertCollector(captor.capture()); |
| | | assertEquals("192.168.1.10", captor.getValue().getIpAddress()); |
| | | assertEquals("AA:BB:CC:DD:EE:FF", captor.getValue().getMacAddress()); |
| | | assertEquals(8, captor.getValue().getChannelCount()); |
| | | } |
| | | |
| | | @Test |
| | |
| | | } |
| | | |
| | | @Test |
| | | void selectRegisteredCollectorCodesShouldReturnUndeletedCodes() |
| | | { |
| | | when(collectorMapper.selectRegisteredCollectorCodes()).thenReturn(java.util.List.of("COL-001")); |
| | | |
| | | assertEquals(java.util.Set.of("COL-001"), collectorService.selectRegisteredCollectorCodes()); |
| | | } |
| | | |
| | | @Test |
| | | void deleteCollectorByIdsShouldRejectTestingCollectorBeforeAnyDelete() |
| | | { |
| | | TCollector idle = new TCollector(); |