|
@@ -0,0 +1,94 @@
|
|
|
+package net.mooctest.www.android_auto_test.SecondaryTest;
|
|
|
+
|
|
|
+
|
|
|
+import net.mooctest.www.android_auto_test.common.constant.enums.SecondaryStatus;
|
|
|
+import net.mooctest.www.android_auto_test.common.exceptions.HttpNotFoundException;
|
|
|
+import net.mooctest.www.android_auto_test.dao.RedisMappers.SecondaryStatusMap;
|
|
|
+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.utils.AddressUtil;
|
|
|
+import net.mooctest.www.android_auto_test.vo.SecondaryResult;
|
|
|
+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 static org.mockito.Mockito.*;
|
|
|
+
|
|
|
+
|
|
|
+@SpringBootTest
|
|
|
+@RunWith(PowerMockRunner.class) // 这是必须的
|
|
|
+@PrepareForTest({AddressUtil.class,
|
|
|
+ SecondaryServiceImpl.class,
|
|
|
+ AddressUtil.class})
|
|
|
+public class SecondaryServiceTest {
|
|
|
+
|
|
|
+ private String traceId;
|
|
|
+ private String toolName;
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private OssService ossService;
|
|
|
+
|
|
|
+ @Mock
|
|
|
+ private SecondaryStatusMap secondaryStatusMap;
|
|
|
+
|
|
|
+ @InjectMocks
|
|
|
+ private SecondaryServiceImpl secondaryService = new SecondaryServiceImpl();
|
|
|
+
|
|
|
+ @Before
|
|
|
+ public void setUp(){
|
|
|
+ MockitoAnnotations.initMocks(this);
|
|
|
+ this.traceId = "mockId";
|
|
|
+ this.toolName = "mockTool";
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testStartWithWrongTraceId(){
|
|
|
+
|
|
|
+ PowerMockito.mockStatic(AddressUtil.class);
|
|
|
+ when(AddressUtil.getTraceDir(traceId)).thenReturn("none");
|
|
|
+
|
|
|
+ try {
|
|
|
+ secondaryService.generateSecondaryReport(traceId, toolName);
|
|
|
+ } catch (Exception e){
|
|
|
+
|
|
|
+ Assert.assertEquals(HttpNotFoundException.class, e.getClass());
|
|
|
+ Assert.assertEquals("任务不存在或信息已被清理,请重新测试", e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testStatusRunning(){
|
|
|
+
|
|
|
+ when(secondaryStatusMap.get(traceId, toolName)).thenReturn(SecondaryStatus.RUNNING);
|
|
|
+
|
|
|
+ SecondaryResult result = secondaryService.getSecondaryStatus(traceId, toolName);
|
|
|
+
|
|
|
+ Assert.assertEquals(traceId, result.getTraceId());
|
|
|
+ Assert.assertEquals(toolName, result.getToolName());
|
|
|
+ Assert.assertEquals(SecondaryStatus.RUNNING.getCode(), result.getStatusCode());
|
|
|
+ Assert.assertEquals(SecondaryStatus.RUNNING.getDescription(), result.getDescription());
|
|
|
+ Assert.assertNull(result.getDownloadUrl());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testStatusSuccess(){
|
|
|
+
|
|
|
+ when(secondaryStatusMap.get(traceId, toolName)).thenReturn(SecondaryStatus.SUCCESS);
|
|
|
+ when(ossService.getDadaJsonDownloadPath(traceId, toolName + ".json")).thenReturn("mockUrl");
|
|
|
+
|
|
|
+ SecondaryResult result = secondaryService.getSecondaryStatus(traceId, toolName);
|
|
|
+
|
|
|
+ Assert.assertEquals(traceId, result.getTraceId());
|
|
|
+ Assert.assertEquals(toolName, result.getToolName());
|
|
|
+ Assert.assertEquals(SecondaryStatus.SUCCESS.getCode(), result.getStatusCode());
|
|
|
+ Assert.assertEquals(SecondaryStatus.SUCCESS.getDescription(), result.getDescription());
|
|
|
+ Assert.assertEquals("mockUrl", result.getDownloadUrl());
|
|
|
+ }
|
|
|
+}
|