|
@@ -0,0 +1,85 @@
|
|
|
+package net.mooctest.www.android_auto_test.ServiceImpl;
|
|
|
+
|
|
|
+import com.aliyun.oss.OSSClient;
|
|
|
+import net.mooctest.www.android_auto_test.services.Impl.OssServiceImpl;
|
|
|
+import org.junit.Assert;
|
|
|
+import org.junit.Before;
|
|
|
+import org.junit.Test;
|
|
|
+import org.junit.runner.RunWith;
|
|
|
+import org.mockito.InjectMocks;
|
|
|
+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.util.ReflectionTestUtils;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.InputStream;
|
|
|
+
|
|
|
+import static org.mockito.ArgumentMatchers.*;
|
|
|
+import static org.mockito.Mockito.doNothing;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+@SpringBootTest
|
|
|
+@RunWith(PowerMockRunner.class) // 这是必须的
|
|
|
+@PrepareForTest({OSSClient.class,
|
|
|
+ FileInputStream.class,
|
|
|
+ InputStream.class,
|
|
|
+ OssServiceImpl.class})
|
|
|
+public class OssServiceTest {
|
|
|
+
|
|
|
+ @InjectMocks
|
|
|
+ OssServiceImpl ossService = new OssServiceImpl();
|
|
|
+
|
|
|
+ @Before
|
|
|
+ public void setUp(){
|
|
|
+ MockitoAnnotations.initMocks(this);
|
|
|
+ ReflectionTestUtils.setField(ossService, "endPoint", "http://oss-cn-shanghai.aliyuncs.com");
|
|
|
+ ReflectionTestUtils.setField(ossService, "accessKeyId", "LTAI4FwTqQ2grekcxanKHnBL");
|
|
|
+ ReflectionTestUtils.setField(ossService, "accessKeySecret", "90nz7r8aImh2NhcNh0HAg8xiOJtn5V");
|
|
|
+ ReflectionTestUtils.setField(ossService, "bucketName", "mooctest-enterprise-site");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testGetReportPath(){
|
|
|
+ // arrange
|
|
|
+ String except = "http://mooctest-enterprise-site.oss-cn-shanghai.aliyuncs.com/trace_data/traceId/data.json";
|
|
|
+ // action
|
|
|
+ String path = ossService.getDadaJsonDownloadPath("traceId","data.json");
|
|
|
+ // assert
|
|
|
+ Assert.assertEquals(except, path);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testUploadFileToOss() throws Exception {
|
|
|
+ // arrange
|
|
|
+ String except = "http://mooctest-enterprise-site.oss-cn-shanghai.aliyuncs.com/trace_data/traceId/xx.xx";
|
|
|
+ FileInputStream is = PowerMockito.mock(FileInputStream.class);
|
|
|
+ OSSClient c = PowerMockito.mock(OSSClient.class);
|
|
|
+ PowerMockito.whenNew(OSSClient.class).withAnyArguments().thenReturn(c);
|
|
|
+ PowerMockito.whenNew(FileInputStream.class).withAnyArguments().thenReturn(is);
|
|
|
+ when(c.putObject(anyString(), anyString(), eq(is))).thenReturn(null);
|
|
|
+ doNothing().when(c).shutdown();
|
|
|
+ // action
|
|
|
+ String path = ossService.uploadFileToTraceDir(new File(""), "traceId", "xx.xx");
|
|
|
+ // assert
|
|
|
+ Assert.assertEquals(except, path);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testUploadInputStreamToOss() throws Exception {
|
|
|
+ // arrange
|
|
|
+ String except = "http://mooctest-enterprise-site.oss-cn-shanghai.aliyuncs.com/trace_data/traceId/xx.xx";
|
|
|
+ FileInputStream is = PowerMockito.mock(FileInputStream.class);
|
|
|
+ OSSClient c = PowerMockito.mock(OSSClient.class);
|
|
|
+ PowerMockito.whenNew(OSSClient.class).withAnyArguments().thenReturn(c);
|
|
|
+ when(c.putObject(anyString(), anyString(), any(InputStream.class))).thenReturn(null);
|
|
|
+ doNothing().when(c).shutdown();
|
|
|
+ // action
|
|
|
+ String path = ossService.uploadInputStreamToTraceDir(is, "traceId", "xx.xx");
|
|
|
+ // assert
|
|
|
+ Assert.assertEquals(except, path);
|
|
|
+ }
|
|
|
+}
|