package com.flightfeather.uav.common.utils; import com.flightfeather.uav.common.exception.BizException; import com.flightfeather.uav.domain.entity.GridDataDetail; import com.flightfeather.uav.lightshare.service.SatelliteTelemetryService; import org.apache.commons.fileupload.disk.DiskFileItem; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.mock.web.MockMultipartFile; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.web.multipart.commons.CommonsMultipartFile; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.URL; import java.time.LocalDateTime; import java.util.List; import java.util.Objects; import java.util.function.Function; import static org.junit.Assert.assertEquals; @RunWith(SpringRunner.class) @SpringBootTest public class FileExchangeTest { @Autowired private SatelliteTelemetryService satelliteTelemetryService; public void start() throws BizException, IOException { test1(); test2(); test3(); test4(); test5(); test6(); test7(); } // 第一列数字类型错误 @Test public void test1() throws IOException, BizException { URL resource = getClass().getClassLoader().getResource("templates/GridData-1_type_error.xlsx"); if (resource != null) { System.out.println(resource.getPath()); // 创建文件输入流 FileInputStream inputStream = new FileInputStream(new File(resource.getPath())); // 创建MockMultipartFile对象 MockMultipartFile multipartFile = new MockMultipartFile("file", "111.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", inputStream); try { satelliteTelemetryService.importGridData(1, 1, LocalDateTime.of(2029, 7, 29, 0, 0), 0, multipartFile); }catch (BizException e) { // 验证异常消息是否与预期相符 if (!e.getMessage().contains("不是数字类型")) { System.out.println(e.getMessage()); throw e; } } } else { System.out.println("资源文件未找到"); } } // 第二列数字类型错误 @Test public void test2() throws IOException, BizException { URL resource = getClass().getClassLoader().getResource("templates/GridData-2_type_error.xlsx"); if (resource != null) { System.out.println(resource.getPath()); // 创建文件输入流 FileInputStream inputStream = new FileInputStream(new File(resource.getPath())); // 创建MockMultipartFile对象 MockMultipartFile multipartFile = new MockMultipartFile("file", "111.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", inputStream); try { satelliteTelemetryService.importGridData(1, 1, LocalDateTime.of(2029, 7, 29, 0, 0), 0, multipartFile); }catch (BizException e) { // 验证异常消息是否与预期相符 if (!e.getMessage().contains("不是数字类型")) { System.out.println(e.getMessage()); throw e; } } } else { System.out.println("资源文件未找到"); } } // 网格编号越界错误 @Test public void test3() throws IOException, BizException { URL resource = getClass().getClassLoader().getResource("templates/GridData-index_out.xlsx"); if (resource != null) { System.out.println(resource.getPath()); // 创建文件输入流 FileInputStream inputStream = new FileInputStream(new File(resource.getPath())); // 创建MockMultipartFile对象 MockMultipartFile multipartFile = new MockMultipartFile("file", "111.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", inputStream); try { satelliteTelemetryService.importGridData(1, 1, LocalDateTime.of(2029, 7, 29, 0, 0), 0, multipartFile); }catch (BizException e) { // 验证异常消息是否与预期相符 if (!e.getMessage().contains("导入数据中有多余网格单元格")) { System.out.println(e.getMessage()); throw e; } } } else { System.out.println("资源文件未找到"); } } // 网格编号缺失错误 @Test public void test4() throws IOException, BizException { URL resource = getClass().getClassLoader().getResource("templates/GridData-index_short.xlsx"); if (resource != null) { System.out.println(resource.getPath()); // 创建文件输入流 FileInputStream inputStream = new FileInputStream(new File(resource.getPath())); // 创建MockMultipartFile对象 MockMultipartFile multipartFile = new MockMultipartFile("file", "111.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", inputStream); try { satelliteTelemetryService.importGridData(1, 1, LocalDateTime.of(2029, 7, 29, 0, 0), 0, multipartFile); }catch (BizException e) { // 验证异常消息是否与预期相符 if (!e.getMessage().contains("导入数据中缺少以下网格单元格")) { System.out.println(e.getMessage()); throw e; } } } else { System.out.println("资源文件未找到"); } } // 文件类型错误 @Test public void test5() throws IOException, BizException { URL resource = getClass().getClassLoader().getResource("templates/GridData-fileType-error.txt"); if (resource != null) { System.out.println(resource.getPath()); // 创建文件输入流 FileInputStream inputStream = new FileInputStream(new File(resource.getPath())); // 创建MockMultipartFile对象 MockMultipartFile multipartFile = new MockMultipartFile("file", "111.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", inputStream); try { satelliteTelemetryService.importGridData(1, 1, LocalDateTime.of(2029, 7, 29, 0, 0), 0, multipartFile); }catch (BizException e) { // 验证异常消息是否与预期相符 if (!e.getMessage().contains("导入数据中缺少以下网格单元格")) { System.out.println(e.getMessage()); throw e; } } } else { System.out.println("资源文件未找到"); } } // 新增数据导入成功 @Test public void test6() throws IOException, BizException { URL resource = getClass().getClassLoader().getResource("templates/GridData-success-insert.xlsx"); if (resource != null) { System.out.println(resource.getPath()); // 创建文件输入流 FileInputStream inputStream = new FileInputStream(new File(resource.getPath())); // 创建MockMultipartFile对象 MockMultipartFile multipartFile = new MockMultipartFile("file", "111.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", inputStream); try { satelliteTelemetryService.importGridData(1, 1, LocalDateTime.of(2029, 7, 29, 0, 0), 0, multipartFile); }catch (BizException e) { throw e; } } else { System.out.println("资源文件未找到"); } } // 更新数据导入成功 @Test public void test7() throws IOException, BizException { URL resource = getClass().getClassLoader().getResource("templates/GridData-success-update.xlsx"); if (resource != null) { System.out.println(resource.getPath()); // 创建文件输入流 FileInputStream inputStream = new FileInputStream(new File(resource.getPath())); // 创建MockMultipartFile对象 MockMultipartFile multipartFile = new MockMultipartFile("file", "111.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", inputStream); try { satelliteTelemetryService.importGridData(1, 1, LocalDateTime.of(2029, 7, 29, 0, 0), 1, multipartFile); }catch (BizException e) { throw e; } } else { System.out.println("资源文件未找到"); } } }