|
@@ -0,0 +1,164 @@
|
|
|
+package net.mooctest.www.android_auto_test.ServiceImpl;
|
|
|
+
|
|
|
+import net.mooctest.www.android_auto_test.common.constant.Consts;
|
|
|
+import net.mooctest.www.android_auto_test.common.constant.enums.DeviceRunningStatus;
|
|
|
+import net.mooctest.www.android_auto_test.common.constant.enums.DeviceStatus;
|
|
|
+import net.mooctest.www.android_auto_test.common.constant.enums.TraceStatus;
|
|
|
+import net.mooctest.www.android_auto_test.common.exceptions.HttpNotFoundException;
|
|
|
+import net.mooctest.www.android_auto_test.services.AutoTestService;
|
|
|
+import net.mooctest.www.android_auto_test.services.Impl.AutoTestServiceImpl;
|
|
|
+import net.mooctest.www.android_auto_test.services.Impl.SecondaryServiceImpl;
|
|
|
+import net.mooctest.www.android_auto_test.services.OssService;
|
|
|
+import net.mooctest.www.android_auto_test.services.TraceService;
|
|
|
+import net.mooctest.www.android_auto_test.utils.AddressUtil;
|
|
|
+import net.mooctest.www.android_auto_test.vo.DeviceStatusResult;
|
|
|
+import net.mooctest.www.android_auto_test.vo.TraceStatusResult;
|
|
|
+import org.junit.Assert;
|
|
|
+import org.junit.Before;
|
|
|
+import org.junit.Test;
|
|
|
+import org.junit.runner.RunWith;
|
|
|
+import org.mockito.InjectMocks;
|
|
|
+import org.mockito.Mock;
|
|
|
+import org.mockito.MockitoAnnotations;
|
|
|
+import org.powermock.api.mockito.PowerMockito;
|
|
|
+import org.powermock.core.classloader.annotations.PrepareForTest;
|
|
|
+import org.powermock.modules.junit4.PowerMockRunner;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+@SpringBootTest
|
|
|
+@RunWith(PowerMockRunner.class) // 这是必须的
|
|
|
+@PrepareForTest({Thread.class,
|
|
|
+ Thread[].class,
|
|
|
+ AutoTestServiceImpl.class})
|
|
|
+public class AutoTestServiceTest {
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ TraceService traceService;
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ OssService ossService;
|
|
|
+
|
|
|
+ @InjectMocks
|
|
|
+ AutoTestServiceImpl autoTestService = new AutoTestServiceImpl();
|
|
|
+
|
|
|
+ private TraceStatusResult ts;
|
|
|
+
|
|
|
+ private List<DeviceStatusResult> ds;
|
|
|
+
|
|
|
+ @Before
|
|
|
+ public void setUp(){
|
|
|
+ MockitoAnnotations.initMocks(this);
|
|
|
+
|
|
|
+ ts = new TraceStatusResult();
|
|
|
+ ts.setTraceId("123");
|
|
|
+
|
|
|
+ DeviceStatusResult dsr1 = new DeviceStatusResult();
|
|
|
+ dsr1.setDeviceId("ID1");
|
|
|
+ dsr1.setStatusCode(DeviceRunningStatus.RUNNING.getCode());
|
|
|
+ dsr1.setDescription(DeviceRunningStatus.RUNNING.getDescription());
|
|
|
+ DeviceStatusResult dsr2 = new DeviceStatusResult();
|
|
|
+ dsr1.setDeviceId("ID2");
|
|
|
+ dsr1.setStatusCode(DeviceRunningStatus.RUNNING.getCode());
|
|
|
+ dsr1.setDescription(DeviceRunningStatus.RUNNING.getDescription());
|
|
|
+ ds = Arrays.asList(dsr1, dsr2);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testStopTraceWhenTraceIdNotExists() {
|
|
|
+ // arrange
|
|
|
+ Thread t = PowerMockito.mock(Thread.class);
|
|
|
+ ThreadGroup tg = PowerMockito.mock(ThreadGroup.class);
|
|
|
+ PowerMockito.mockStatic(Thread.class);
|
|
|
+ when(Thread.currentThread()).thenReturn(t);
|
|
|
+ when(t.getThreadGroup()).thenReturn(tg);
|
|
|
+ when(tg.activeCount()).thenReturn(0);
|
|
|
+ when(tg.enumerate(any(Thread[].class))).thenReturn(0);
|
|
|
+
|
|
|
+ // action
|
|
|
+ try {
|
|
|
+ autoTestService.stopTrace("123");
|
|
|
+ } catch (Exception e) {
|
|
|
+ Assert.assertEquals(HttpNotFoundException.class, e.getClass());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testStopTraceWhenTraceIdExists() {
|
|
|
+ // arrange
|
|
|
+ Thread t = PowerMockito.mock(Thread.class);
|
|
|
+ ThreadGroup tg = PowerMockito.mock(ThreadGroup.class);
|
|
|
+ PowerMockito.mockStatic(Thread.class);
|
|
|
+ when(Thread.currentThread()).thenReturn(t);
|
|
|
+ when(t.getThreadGroup()).thenReturn(tg);
|
|
|
+ when(tg.activeCount()).thenReturn(2);
|
|
|
+ when(tg.enumerate(any(Thread[].class))).thenReturn(2);
|
|
|
+
|
|
|
+ // action
|
|
|
+ try {
|
|
|
+ autoTestService.stopTrace("123");
|
|
|
+ } catch (Exception e) {
|
|
|
+ // assert
|
|
|
+ Assert.assertEquals(NullPointerException.class, e.getClass());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testGetTraceResultWhenTraceIdNotExist(){
|
|
|
+ // arrange
|
|
|
+ when(traceService.getTraceStatue("123")).thenReturn(null);
|
|
|
+ // action
|
|
|
+ try {
|
|
|
+ autoTestService.getResult("123");
|
|
|
+ } catch (Exception e) {
|
|
|
+ // assert
|
|
|
+ Assert.assertEquals(HttpNotFoundException.class, e.getClass());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testGetTraceResultWhenNotFinished(){
|
|
|
+ // arrange
|
|
|
+ when(traceService.getTraceStatue("123")).thenReturn(TraceStatus.RUNNING);
|
|
|
+ when(traceService.getTraceDeviceStatus("123")).thenReturn(new ArrayList<>());
|
|
|
+ // action
|
|
|
+ TraceStatusResult traceStatusResult = autoTestService.getResult("123");
|
|
|
+ // assert
|
|
|
+ Assert.assertEquals("123", traceStatusResult.getTraceId());
|
|
|
+ Assert.assertNull(traceStatusResult.getDownloadUrl());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testGetTraceResultWhenHaveDevices(){
|
|
|
+ // arrange
|
|
|
+ when(traceService.getTraceStatue("123")).thenReturn(TraceStatus.RUNNING);
|
|
|
+ when(traceService.getTraceDeviceStatus("123")).thenReturn(ds);
|
|
|
+ // action
|
|
|
+ TraceStatusResult traceStatusResult = autoTestService.getResult("123");
|
|
|
+ // assert
|
|
|
+ Assert.assertEquals("123", traceStatusResult.getTraceId());
|
|
|
+ Assert.assertEquals(2, traceStatusResult.getExtraInfo().getJSONArray("deviceStatus").size());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testGetTraceStatusWhenFinished() {
|
|
|
+ // arrange
|
|
|
+ when(traceService.getTraceStatue("123")).thenReturn(TraceStatus.FINISH);
|
|
|
+ when(traceService.getTraceDeviceStatus("123")).thenReturn(ds);
|
|
|
+ when(ossService.getDadaJsonDownloadPath("123", Consts.REPORT_FILE_NAME)).thenReturn("url");
|
|
|
+ // action
|
|
|
+ TraceStatusResult traceStatusResult = autoTestService.getResult("123");
|
|
|
+ // assert
|
|
|
+ Assert.assertEquals("123", traceStatusResult.getTraceId());
|
|
|
+ Assert.assertEquals(2, traceStatusResult.getExtraInfo().getJSONArray("deviceStatus").size());
|
|
|
+ Assert.assertEquals("url", traceStatusResult.getDownloadUrl());
|
|
|
+ }
|
|
|
+}
|