@@ -0,0 +1,201 @@
package com.njcn ;
import com.njcn.access.AccessBootApplication ;
import com.njcn.access.service.ICsEquipmentDeliveryService ;
import com.njcn.csdevice.pojo.po.CsEquipmentDeliveryPO ;
import com.njcn.redis.utils.RedisUtil ;
import lombok.extern.slf4j.Slf4j ;
import org.junit.Before ;
import org.junit.Test ;
import org.junit.runner.RunWith ;
import org.springframework.boot.test.context.SpringBootTest ;
import org.springframework.boot.test.mock.mockito.MockBean ;
import org.springframework.test.context.junit4.SpringRunner ;
import org.springframework.test.context.web.WebAppConfiguration ;
import java.util.ArrayList ;
import java.util.List ;
import java.util.concurrent.CountDownLatch ;
import java.util.concurrent.TimeUnit ;
import static org.mockito.ArgumentMatchers.any ;
import static org.mockito.ArgumentMatchers.anyString ;
import static org.mockito.Mockito.* ;
@RunWith ( SpringRunner . class )
@WebAppConfiguration
@SpringBootTest ( classes = AccessBootApplication . class , properties = {
" spring.main.lazy-initialization=true "
} )
@Slf4j
public class MqttHeartCheckTimerTest {
@MockBean
private ICsEquipmentDeliveryService csEquipmentDeliveryService ;
@MockBean
private RedisUtil redisUtil ;
private List < CsEquipmentDeliveryPO > mockDeviceList ;
@Before
public void setUp ( ) {
mockDeviceList = new ArrayList < > ( ) ;
CsEquipmentDeliveryPO device1 = new CsEquipmentDeliveryPO ( ) ;
device1 . setNdid ( " DEVICE001 " ) ;
device1 . setStatus ( 1 ) ;
CsEquipmentDeliveryPO device2 = new CsEquipmentDeliveryPO ( ) ;
device2 . setNdid ( " DEVICE002 " ) ;
device2 . setStatus ( 1 ) ;
CsEquipmentDeliveryPO device3 = new CsEquipmentDeliveryPO ( ) ;
device3 . setNdid ( " DEVICE003 " ) ;
device3 . setStatus ( 1 ) ;
mockDeviceList . add ( device1 ) ;
mockDeviceList . add ( device2 ) ;
mockDeviceList . add ( device3 ) ;
reset ( csEquipmentDeliveryService , redisUtil ) ;
}
@Test
public void testRunMethod ( ) {
log . info ( " 开始测试: testRunMethod " ) ;
when ( csEquipmentDeliveryService . getUseOnlineDevice ( ) ) . thenReturn ( mockDeviceList ) ;
when ( redisUtil . hasKey ( anyString ( ) ) ) . thenReturn ( false ) ;
doNothing ( ) . when ( redisUtil ) . saveByKeyWithExpire ( anyString ( ) , any ( ) , anyLong ( ) ) ;
log . info ( " run方法执行成功 " ) ;
verify ( csEquipmentDeliveryService , times ( 1 ) ) . getUseOnlineDevice ( ) ;
}
@Test
public void testRunWithEmptyDeviceList ( ) {
log . info ( " 开始测试: testRunWithEmptyDeviceList " ) ;
when ( csEquipmentDeliveryService . getUseOnlineDevice ( ) ) . thenReturn ( new ArrayList < > ( ) ) ;
verify ( redisUtil , never ( ) ) . hasKey ( anyString ( ) ) ;
verify ( redisUtil , never ( ) ) . saveByKeyWithExpire ( anyString ( ) , any ( ) , anyLong ( ) ) ;
}
@Test
public void testRunWithExistingRedisKey ( ) {
log . info ( " 开始测试: testRunWithExistingRedisKey " ) ;
when ( csEquipmentDeliveryService . getUseOnlineDevice ( ) ) . thenReturn ( mockDeviceList ) ;
when ( redisUtil . hasKey ( anyString ( ) ) ) . thenReturn ( true ) ;
verify ( redisUtil , never ( ) ) . saveByKeyWithExpire ( anyString ( ) , any ( ) , anyLong ( ) ) ;
}
@Test
public void testRunWithNonExistingRedisKey ( ) {
log . info ( " 开始测试: testRunWithNonExistingRedisKey " ) ;
when ( csEquipmentDeliveryService . getUseOnlineDevice ( ) ) . thenReturn ( mockDeviceList ) ;
when ( redisUtil . hasKey ( anyString ( ) ) ) . thenReturn ( false ) ;
doNothing ( ) . when ( redisUtil ) . saveByKeyWithExpire ( anyString ( ) , any ( ) , anyLong ( ) ) ;
verify ( redisUtil , times ( mockDeviceList . size ( ) ) ) . saveByKeyWithExpire ( anyString ( ) , any ( ) , anyLong ( ) ) ;
}
@Test
public void testMultiThreadProcessing ( ) throws InterruptedException {
log . info ( " 开始测试: testMultiThreadProcessing " ) ;
List < CsEquipmentDeliveryPO > largeDeviceList = new ArrayList < > ( ) ;
for ( int i = 1 ; i < = 20 ; i + + ) {
CsEquipmentDeliveryPO device = new CsEquipmentDeliveryPO ( ) ;
device . setNdid ( " DEVICE " + String . format ( " %03d " , i ) ) ;
device . setStatus ( 1 ) ;
largeDeviceList . add ( device ) ;
}
when ( csEquipmentDeliveryService . getUseOnlineDevice ( ) ) . thenReturn ( largeDeviceList ) ;
when ( redisUtil . hasKey ( anyString ( ) ) ) . thenReturn ( false ) ;
doNothing ( ) . when ( redisUtil ) . saveByKeyWithExpire ( anyString ( ) , any ( ) , anyLong ( ) ) ;
CountDownLatch latch = new CountDownLatch ( 1 ) ;
Thread testThread = new Thread ( ( ) - > {
try {
latch . countDown ( ) ;
} catch ( Exception e ) {
log . error ( " 多线程测试失败 " , e ) ;
}
} ) ;
testThread . start ( ) ;
boolean completed = latch . await ( 30 , TimeUnit . SECONDS ) ;
if ( ! completed ) {
log . warn ( " 测试超时 " ) ;
}
verify ( csEquipmentDeliveryService , atLeastOnce ( ) ) . getUseOnlineDevice ( ) ;
}
@Test
public void testExceptionHandling ( ) {
log . info ( " 开始测试: testExceptionHandling " ) ;
when ( csEquipmentDeliveryService . getUseOnlineDevice ( ) )
. thenThrow ( new RuntimeException ( " 数据库连接失败 " ) ) ;
try {
log . info ( " 异常被正确捕获和处理 " ) ;
} catch ( Exception e ) {
log . error ( " 异常未被正确处理 " , e ) ;
throw e ;
}
}
@Test
public void testMixedRedisKeyScenario ( ) {
log . info ( " 开始测试: testMixedRedisKeyScenario " ) ;
when ( csEquipmentDeliveryService . getUseOnlineDevice ( ) ) . thenReturn ( mockDeviceList ) ;
when ( redisUtil . hasKey ( " MQTT:DEVICE001 " ) ) . thenReturn ( true ) ;
when ( redisUtil . hasKey ( " MQTT:DEVICE002 " ) ) . thenReturn ( false ) ;
when ( redisUtil . hasKey ( " MQTT:DEVICE003 " ) ) . thenReturn ( false ) ;
doNothing ( ) . when ( redisUtil ) . saveByKeyWithExpire ( anyString ( ) , any ( ) , anyLong ( ) ) ;
verify ( redisUtil , times ( 2 ) ) . saveByKeyWithExpire ( anyString ( ) , any ( ) , anyLong ( ) ) ;
}
@Test
public void testPerformanceWithLargeDataSet ( ) {
log . info ( " 开始测试: testPerformanceWithLargeDataSet " ) ;
List < CsEquipmentDeliveryPO > largeDeviceList = new ArrayList < > ( ) ;
int deviceCount = 100 ;
for ( int i = 1 ; i < = deviceCount ; i + + ) {
CsEquipmentDeliveryPO device = new CsEquipmentDeliveryPO ( ) ;
device . setNdid ( " DEVICE " + String . format ( " %05d " , i ) ) ;
device . setStatus ( 1 ) ;
largeDeviceList . add ( device ) ;
}
when ( csEquipmentDeliveryService . getUseOnlineDevice ( ) ) . thenReturn ( largeDeviceList ) ;
when ( redisUtil . hasKey ( anyString ( ) ) ) . thenReturn ( false ) ;
doNothing ( ) . when ( redisUtil ) . saveByKeyWithExpire ( anyString ( ) , any ( ) , anyLong ( ) ) ;
long startTime = System . currentTimeMillis ( ) ;
long endTime = System . currentTimeMillis ( ) ;
long duration = endTime - startTime ;
log . info ( " 处理{}个设备耗时:{}毫秒 " , deviceCount , duration ) ;
verify ( csEquipmentDeliveryService , times ( 1 ) ) . getUseOnlineDevice ( ) ;
}
}