修复员工资产空导入返回500问题
This commit is contained in:
@@ -60,7 +60,7 @@ public class CcdiAssetInfoController extends BaseController {
|
|||||||
public AjaxResult importData(MultipartFile file) throws Exception {
|
public AjaxResult importData(MultipartFile file) throws Exception {
|
||||||
List<CcdiAssetInfoExcel> list = EasyExcelUtil.importExcel(file.getInputStream(), CcdiAssetInfoExcel.class);
|
List<CcdiAssetInfoExcel> list = EasyExcelUtil.importExcel(file.getInputStream(), CcdiAssetInfoExcel.class);
|
||||||
if (list == null || list.isEmpty()) {
|
if (list == null || list.isEmpty()) {
|
||||||
return error("至少需要一条数据");
|
return warn("至少需要一条数据");
|
||||||
}
|
}
|
||||||
|
|
||||||
String taskId = assetInfoImportService.importAssetInfo(list);
|
String taskId = assetInfoImportService.importAssetInfo(list);
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
package com.ruoyi.info.collection.controller;
|
||||||
|
|
||||||
|
import com.ruoyi.common.constant.HttpStatus;
|
||||||
|
import com.ruoyi.common.core.domain.AjaxResult;
|
||||||
|
import com.ruoyi.info.collection.domain.excel.CcdiAssetInfoExcel;
|
||||||
|
import com.ruoyi.info.collection.service.ICcdiAssetInfoImportService;
|
||||||
|
import com.ruoyi.info.collection.utils.EasyExcelUtil;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockedStatic;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
import org.springframework.mock.web.MockMultipartFile;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.mockStatic;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class CcdiAssetInfoControllerTest {
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private CcdiAssetInfoController controller;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private ICcdiAssetInfoImportService assetInfoImportService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void importData_shouldReturnWarnWhenExcelHasNoRows() throws Exception {
|
||||||
|
MockMultipartFile file = new MockMultipartFile(
|
||||||
|
"file",
|
||||||
|
"asset-empty.xlsx",
|
||||||
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||||
|
"empty".getBytes(StandardCharsets.UTF_8)
|
||||||
|
);
|
||||||
|
|
||||||
|
try (MockedStatic<EasyExcelUtil> mocked = mockStatic(EasyExcelUtil.class)) {
|
||||||
|
mocked.when(() -> EasyExcelUtil.importExcel(any(InputStream.class), eq(CcdiAssetInfoExcel.class)))
|
||||||
|
.thenReturn(List.of());
|
||||||
|
|
||||||
|
AjaxResult result = controller.importData(file);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.WARN, result.get(AjaxResult.CODE_TAG));
|
||||||
|
assertEquals("至少需要一条数据", result.get(AjaxResult.MSG_TAG));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void importData_shouldReturnSuccessWhenTaskCreated() throws Exception {
|
||||||
|
MockMultipartFile file = new MockMultipartFile(
|
||||||
|
"file",
|
||||||
|
"asset.xlsx",
|
||||||
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||||
|
"asset".getBytes(StandardCharsets.UTF_8)
|
||||||
|
);
|
||||||
|
CcdiAssetInfoExcel excel = new CcdiAssetInfoExcel();
|
||||||
|
excel.setPersonId("320101199001010011");
|
||||||
|
when(assetInfoImportService.importAssetInfo(List.of(excel))).thenReturn("task-1");
|
||||||
|
|
||||||
|
try (MockedStatic<EasyExcelUtil> mocked = mockStatic(EasyExcelUtil.class)) {
|
||||||
|
mocked.when(() -> EasyExcelUtil.importExcel(any(InputStream.class), eq(CcdiAssetInfoExcel.class)))
|
||||||
|
.thenReturn(List.of(excel));
|
||||||
|
|
||||||
|
AjaxResult result = controller.importData(file);
|
||||||
|
|
||||||
|
assertEquals(HttpStatus.SUCCESS, result.get(AjaxResult.CODE_TAG));
|
||||||
|
assertEquals("导入任务已提交,正在后台处理", result.get(AjaxResult.MSG_TAG));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1279,6 +1279,8 @@ export default {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.startAssetImportStatusPolling(taskId);
|
this.startAssetImportStatusPolling(taskId);
|
||||||
|
} else if (response.code === 601) {
|
||||||
|
this.$modal.msgWarning(response.msg);
|
||||||
} else {
|
} else {
|
||||||
this.$modal.msgError(response.msg);
|
this.$modal.msgError(response.msg);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user