|
@@ -0,0 +1,200 @@
|
|
|
+package net.mooctest.www.android_auto_test.ServiceImpl;
|
|
|
+
|
|
|
+import com.sinaapp.msdxblog.apkUtil.entity.ApkInfo;
|
|
|
+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.dao.RedisMappers.DeviceRunningStatusMap;
|
|
|
+import net.mooctest.www.android_auto_test.dao.RedisMappers.DeviceStatusMap;
|
|
|
+import net.mooctest.www.android_auto_test.models.Device;
|
|
|
+import net.mooctest.www.android_auto_test.services.Impl.DeviceServiceImpl;
|
|
|
+import net.mooctest.www.android_auto_test.utils.OsUtil;
|
|
|
+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 java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import static org.mockito.Mockito.*;
|
|
|
+
|
|
|
+@SpringBootTest
|
|
|
+@RunWith(PowerMockRunner.class) // 这是必须的
|
|
|
+@PrepareForTest({OsUtil.class})
|
|
|
+public class DeviceServiceTest {
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ DeviceRunningStatusMap deviceRunningStatusMap;
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ DeviceStatusMap deviceStatusMap;
|
|
|
+
|
|
|
+ @InjectMocks
|
|
|
+ private DeviceServiceImpl deviceService = new DeviceServiceImpl();
|
|
|
+
|
|
|
+ List<Device> devices;
|
|
|
+ List<String> selectedDevices;
|
|
|
+
|
|
|
+ @Before
|
|
|
+ public void setUp(){
|
|
|
+ MockitoAnnotations.initMocks(this);
|
|
|
+ Device d1 = new Device();
|
|
|
+ d1.setUdid("d1");
|
|
|
+ Device d2 = new Device();
|
|
|
+ d2.setUdid("d2");
|
|
|
+ Device d3 = new Device();
|
|
|
+ d3.setUdid("d3");
|
|
|
+ devices = Arrays.asList(d1, d2, d3);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void shouldReturnEmptyListWhenHaveNoDevice(){
|
|
|
+ // arrange
|
|
|
+ String command = "adb devices";
|
|
|
+ String result = "List of devices attached\n";
|
|
|
+ PowerMockito.mockStatic(OsUtil.class);
|
|
|
+ when(OsUtil.runCommand(command)).thenReturn(result);
|
|
|
+ // action
|
|
|
+ List<Device> deviceList = deviceService.getOnlineDeviceList();
|
|
|
+ // assert
|
|
|
+ Assert.assertEquals(0, deviceList.size());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void shouldReturnDeviceListWhenGetOnlineDevices(){
|
|
|
+ // arrange
|
|
|
+ String command = "adb devices";
|
|
|
+ String result = "List of devices attached\naaa\tdevice\nbbb\tdevice\n";
|
|
|
+ PowerMockito.mockStatic(OsUtil.class);
|
|
|
+ when(OsUtil.runCommand(command)).thenReturn(result);
|
|
|
+ // action
|
|
|
+ List<Device> deviceList = deviceService.getOnlineDeviceList();
|
|
|
+ // assert
|
|
|
+ Assert.assertEquals(2, deviceList.size());
|
|
|
+ Assert.assertEquals("aaa", deviceList.get(0).getUdid());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void shouldReturnDeviceListWhenSelectNoDevice(){
|
|
|
+ // arrange
|
|
|
+ ApkInfo apkInfo = new ApkInfo();
|
|
|
+ selectedDevices = new ArrayList<>();
|
|
|
+ // action
|
|
|
+ List<Device> ds = deviceService.selectDeviceByApp(apkInfo, devices, selectedDevices);
|
|
|
+ // assert
|
|
|
+ Assert.assertEquals(3, ds.size());
|
|
|
+ Assert.assertEquals("d2", ds.get(1).getUdid());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void shouldReturnDeviceListWhenSelectNoneDevice(){
|
|
|
+ // arrange
|
|
|
+ ApkInfo apkInfo = new ApkInfo();
|
|
|
+ selectedDevices = Collections.singletonList("d4");
|
|
|
+ // action
|
|
|
+ List<Device> ds = deviceService.selectDeviceByApp(apkInfo, devices, selectedDevices);
|
|
|
+ // assert
|
|
|
+ Assert.assertEquals(0, ds.size());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void shouldReturnDeviceListWhenSelectDevice(){
|
|
|
+ // arrange
|
|
|
+ ApkInfo apkInfo = new ApkInfo();
|
|
|
+ selectedDevices = Arrays.asList("d1", "d4", "d5");
|
|
|
+ // action
|
|
|
+ List<Device> ds = deviceService.selectDeviceByApp(apkInfo, devices, selectedDevices);
|
|
|
+ // assert
|
|
|
+ Assert.assertEquals(1, ds.size());
|
|
|
+ Assert.assertEquals("d1", ds.get(0).getUdid());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testUpdateDeviceRunningStatus(){
|
|
|
+ // arrange
|
|
|
+ doNothing().when(deviceRunningStatusMap).put("id","traceId", DeviceRunningStatus.PREPARE);
|
|
|
+ // action
|
|
|
+ deviceService.updateDeviceRunningStatus("traceId","id", DeviceRunningStatus.PREPARE);
|
|
|
+ // assert
|
|
|
+ verify(deviceRunningStatusMap).put("id","traceId", DeviceRunningStatus.PREPARE);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testGetDeviceRunningStatus(){
|
|
|
+ // arrange
|
|
|
+ when(deviceRunningStatusMap.get("id","traceId")).thenReturn(DeviceRunningStatus.FINISH);
|
|
|
+ // action
|
|
|
+ DeviceRunningStatus ds = deviceService.getDeviceRunningStatus("traceId", "id");
|
|
|
+ // assert
|
|
|
+ Assert.assertEquals(ds, DeviceRunningStatus.FINISH);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testUpdateDeviceStatus(){
|
|
|
+ // arrange
|
|
|
+ doNothing().when(deviceStatusMap).put("id", DeviceStatus.FREE);
|
|
|
+ // action
|
|
|
+ deviceService.updateDeviceStatus("id", DeviceStatus.FREE);
|
|
|
+ // assert
|
|
|
+ verify(deviceStatusMap).put("id", DeviceStatus.FREE);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testGetDeviceStatus(){
|
|
|
+ // arrange
|
|
|
+ when(deviceStatusMap.get("id")).thenReturn(DeviceStatus.FREE);
|
|
|
+ // action
|
|
|
+ DeviceStatus ds = deviceService.getDeviceStatus( "id");
|
|
|
+ // assert
|
|
|
+ Assert.assertEquals(ds, DeviceStatus.FREE);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testOccupyDevice(){
|
|
|
+ // arrange
|
|
|
+ when(deviceStatusMap.compareAndSet("id", DeviceStatus.FREE, DeviceStatus.OCCUPY)).thenReturn(true);
|
|
|
+ // action
|
|
|
+ boolean result = deviceService.occupyDevice("id");
|
|
|
+ // assert
|
|
|
+ Assert.assertTrue(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testFreeDevice(){
|
|
|
+ // arrange
|
|
|
+ when(deviceStatusMap.compareAndSet("id", DeviceStatus.OCCUPY, DeviceStatus.FREE)).thenReturn(false);
|
|
|
+ // action
|
|
|
+ boolean result = deviceService.freeDevice("id");
|
|
|
+ // assert
|
|
|
+ Assert.assertFalse(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testStartDevice(){
|
|
|
+ // arrange
|
|
|
+ when(deviceStatusMap.compareAndSet("id", DeviceStatus.OCCUPY, DeviceStatus.RUNNING)).thenReturn(true);
|
|
|
+ // action
|
|
|
+ boolean result = deviceService.startRunDevice("id");
|
|
|
+ // assert
|
|
|
+ Assert.assertTrue(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testEndDevice(){
|
|
|
+ // arrange
|
|
|
+ when(deviceStatusMap.compareAndSet("id", DeviceStatus.RUNNING, DeviceStatus.FREE)).thenReturn(true);
|
|
|
+ // action
|
|
|
+ boolean result = deviceService.endRunDevice("id");
|
|
|
+ // assert
|
|
|
+ Assert.assertTrue(result);
|
|
|
+ }
|
|
|
+}
|