新增模块wms
This commit is contained in:
parent
301e21ed5b
commit
366851d11c
@ -1,558 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.wms.service.codegen;
|
|
||||||
|
|
||||||
import cn.hutool.core.collection.ListUtil;
|
|
||||||
import cn.hutool.core.map.MapUtil;
|
|
||||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
||||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
|
||||||
import cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.CodegenCreateListReqVO;
|
|
||||||
import cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.CodegenUpdateReqVO;
|
|
||||||
import cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.column.CodegenColumnSaveReqVO;
|
|
||||||
import cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.table.CodegenTablePageReqVO;
|
|
||||||
import cn.iocoder.yudao.module.infra.controller.admin.codegen.vo.table.DatabaseTableRespVO;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.dataobject.codegen.CodegenColumnDO;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.dataobject.codegen.CodegenTableDO;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.mysql.codegen.CodegenColumnMapper;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.mysql.codegen.CodegenTableMapper;
|
|
||||||
import cn.iocoder.yudao.module.wms.enums.codegen.CodegenFrontTypeEnum;
|
|
||||||
import cn.iocoder.yudao.module.wms.enums.codegen.CodegenSceneEnum;
|
|
||||||
import cn.iocoder.yudao.module.wms.enums.codegen.CodegenTemplateTypeEnum;
|
|
||||||
import cn.iocoder.yudao.module.wms.framework.codegen.config.CodegenProperties;
|
|
||||||
import cn.iocoder.yudao.module.wms.service.codegen.CodegenServiceImpl;
|
|
||||||
import cn.iocoder.yudao.module.wms.service.codegen.inner.CodegenBuilder;
|
|
||||||
import cn.iocoder.yudao.module.wms.service.codegen.inner.CodegenEngine;
|
|
||||||
import cn.iocoder.yudao.module.wms.service.db.DatabaseTableService;
|
|
||||||
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
|
|
||||||
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
|
|
||||||
import com.baomidou.mybatisplus.generator.config.po.TableField;
|
|
||||||
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
|
||||||
import org.springframework.context.annotation.Import;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
|
||||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
|
|
||||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
|
|
||||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
|
||||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*;
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
import static org.mockito.ArgumentMatchers.*;
|
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link CodegenServiceImpl} 的单元测试类
|
|
||||||
*
|
|
||||||
|
|
||||||
*/
|
|
||||||
@Import(CodegenServiceImpl.class)
|
|
||||||
public class CodegenServiceImplTest extends BaseDbUnitTest {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private CodegenServiceImpl codegenService;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private CodegenTableMapper codegenTableMapper;
|
|
||||||
@Resource
|
|
||||||
private CodegenColumnMapper codegenColumnMapper;
|
|
||||||
|
|
||||||
@MockBean
|
|
||||||
private DatabaseTableService databaseTableService;
|
|
||||||
|
|
||||||
@MockBean
|
|
||||||
private AdminUserApi userApi;
|
|
||||||
|
|
||||||
@MockBean
|
|
||||||
private CodegenBuilder codegenBuilder;
|
|
||||||
@MockBean
|
|
||||||
private CodegenEngine codegenEngine;
|
|
||||||
|
|
||||||
@MockBean
|
|
||||||
private CodegenProperties codegenProperties;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCreateCodegenList() {
|
|
||||||
// 准备参数
|
|
||||||
Long userId = randomLongId();
|
|
||||||
CodegenCreateListReqVO reqVO = randomPojo(CodegenCreateListReqVO.class,
|
|
||||||
o -> o.setDataSourceConfigId(1L).setTableNames(Collections.singletonList("t_yunai")));
|
|
||||||
// mock 方法(TableInfo)
|
|
||||||
TableInfo tableInfo = mock(TableInfo.class);
|
|
||||||
when(databaseTableService.getTable(eq(1L), eq("t_yunai")))
|
|
||||||
.thenReturn(tableInfo);
|
|
||||||
when(tableInfo.getComment()).thenReturn("芋艿");
|
|
||||||
// mock 方法(TableInfo fields)
|
|
||||||
TableField field01 = mock(TableField.class);
|
|
||||||
when(field01.getComment()).thenReturn("主键");
|
|
||||||
TableField field02 = mock(TableField.class);
|
|
||||||
when(field02.getComment()).thenReturn("名字");
|
|
||||||
List<TableField> fields = Arrays.asList(field01, field02);
|
|
||||||
when(tableInfo.getFields()).thenReturn(fields);
|
|
||||||
// mock 方法(CodegenTableDO)
|
|
||||||
CodegenTableDO table = randomPojo(CodegenTableDO.class);
|
|
||||||
when(codegenBuilder.buildTable(same(tableInfo))).thenReturn(table);
|
|
||||||
// mock 方法(AdminUserRespDTO)
|
|
||||||
AdminUserRespDTO user = randomPojo(AdminUserRespDTO.class, o -> o.setNickname("芋头"));
|
|
||||||
when(userApi.getUser(eq(userId))).thenReturn(success(user));
|
|
||||||
// mock 方法(CodegenColumnDO)
|
|
||||||
List<CodegenColumnDO> columns = randomPojoList(CodegenColumnDO.class);
|
|
||||||
when(codegenBuilder.buildColumns(eq(table.getId()), same(fields)))
|
|
||||||
.thenReturn(columns);
|
|
||||||
// mock 方法(CodegenProperties)
|
|
||||||
when(codegenProperties.getFrontType()).thenReturn(CodegenFrontTypeEnum.VUE3.getType());
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
List<Long> result = codegenService.createCodegenList(userId, reqVO);
|
|
||||||
// 断言
|
|
||||||
assertEquals(1, result.size());
|
|
||||||
// 断言(CodegenTableDO)
|
|
||||||
CodegenTableDO dbTable = codegenTableMapper.selectList().get(0);
|
|
||||||
assertPojoEquals(table, dbTable);
|
|
||||||
assertEquals(1L, dbTable.getDataSourceConfigId());
|
|
||||||
assertEquals(CodegenSceneEnum.ADMIN.getScene(), dbTable.getScene());
|
|
||||||
assertEquals(CodegenFrontTypeEnum.VUE3.getType(), dbTable.getFrontType());
|
|
||||||
assertEquals("芋头", dbTable.getAuthor());
|
|
||||||
// 断言(CodegenColumnDO)
|
|
||||||
List<CodegenColumnDO> dbColumns = codegenColumnMapper.selectList();
|
|
||||||
assertEquals(columns.size(), dbColumns.size());
|
|
||||||
assertTrue(dbColumns.get(0).getPrimaryKey());
|
|
||||||
for (int i = 0; i < dbColumns.size(); i++) {
|
|
||||||
assertPojoEquals(columns.get(i), dbColumns.get(i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testValidateTableInfo() {
|
|
||||||
// 情况一
|
|
||||||
assertServiceException(() -> codegenService.validateTableInfo(null),
|
|
||||||
CODEGEN_IMPORT_TABLE_NULL);
|
|
||||||
// 情况二
|
|
||||||
TableInfo tableInfo = mock(TableInfo.class);
|
|
||||||
assertServiceException(() -> codegenService.validateTableInfo(tableInfo),
|
|
||||||
CODEGEN_TABLE_INFO_TABLE_COMMENT_IS_NULL);
|
|
||||||
// 情况三
|
|
||||||
when(tableInfo.getComment()).thenReturn("芋艿");
|
|
||||||
assertServiceException(() -> codegenService.validateTableInfo(tableInfo),
|
|
||||||
CODEGEN_IMPORT_COLUMNS_NULL);
|
|
||||||
// 情况四
|
|
||||||
TableField field = mock(TableField.class);
|
|
||||||
when(field.getName()).thenReturn("name");
|
|
||||||
when(tableInfo.getFields()).thenReturn(Collections.singletonList(field));
|
|
||||||
assertServiceException(() -> codegenService.validateTableInfo(tableInfo),
|
|
||||||
CODEGEN_TABLE_INFO_COLUMN_COMMENT_IS_NULL, field.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUpdateCodegen_notExists() {
|
|
||||||
// 准备参数
|
|
||||||
CodegenUpdateReqVO updateReqVO = randomPojo(CodegenUpdateReqVO.class);
|
|
||||||
// mock 方法
|
|
||||||
|
|
||||||
// 调用,并断言
|
|
||||||
assertServiceException(() -> codegenService.updateCodegen(updateReqVO),
|
|
||||||
CODEGEN_TABLE_NOT_EXISTS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUpdateCodegen_sub_masterNotExists() {
|
|
||||||
// mock 数据
|
|
||||||
CodegenTableDO table = randomPojo(CodegenTableDO.class,
|
|
||||||
o -> o.setTemplateType(CodegenTemplateTypeEnum.SUB.getType())
|
|
||||||
.setScene(CodegenSceneEnum.ADMIN.getScene()));
|
|
||||||
codegenTableMapper.insert(table);
|
|
||||||
// 准备参数
|
|
||||||
CodegenUpdateReqVO updateReqVO = randomPojo(CodegenUpdateReqVO.class,
|
|
||||||
o -> o.getTable().setId(table.getId())
|
|
||||||
.setTemplateType(CodegenTemplateTypeEnum.SUB.getType()));
|
|
||||||
|
|
||||||
// 调用,并断言
|
|
||||||
assertServiceException(() -> codegenService.updateCodegen(updateReqVO),
|
|
||||||
CODEGEN_MASTER_TABLE_NOT_EXISTS, updateReqVO.getTable().getMasterTableId());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUpdateCodegen_sub_columnNotExists() {
|
|
||||||
// mock 数据
|
|
||||||
CodegenTableDO subTable = randomPojo(CodegenTableDO.class,
|
|
||||||
o -> o.setTemplateType(CodegenTemplateTypeEnum.SUB.getType())
|
|
||||||
.setScene(CodegenSceneEnum.ADMIN.getScene()));
|
|
||||||
codegenTableMapper.insert(subTable);
|
|
||||||
// mock 数据(master)
|
|
||||||
CodegenTableDO masterTable = randomPojo(CodegenTableDO.class,
|
|
||||||
o -> o.setTemplateType(CodegenTemplateTypeEnum.MASTER_ERP.getType())
|
|
||||||
.setScene(CodegenSceneEnum.ADMIN.getScene()));
|
|
||||||
codegenTableMapper.insert(masterTable);
|
|
||||||
// 准备参数
|
|
||||||
CodegenUpdateReqVO updateReqVO = randomPojo(CodegenUpdateReqVO.class,
|
|
||||||
o -> o.getTable().setId(subTable.getId())
|
|
||||||
.setTemplateType(CodegenTemplateTypeEnum.SUB.getType())
|
|
||||||
.setMasterTableId(masterTable.getId()));
|
|
||||||
|
|
||||||
// 调用,并断言
|
|
||||||
assertServiceException(() -> codegenService.updateCodegen(updateReqVO),
|
|
||||||
CODEGEN_SUB_COLUMN_NOT_EXISTS, updateReqVO.getTable().getSubJoinColumnId());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUpdateCodegen_success() {
|
|
||||||
// mock 数据
|
|
||||||
CodegenTableDO table = randomPojo(CodegenTableDO.class,
|
|
||||||
o -> o.setTemplateType(CodegenTemplateTypeEnum.ONE.getType())
|
|
||||||
.setScene(CodegenSceneEnum.ADMIN.getScene()));
|
|
||||||
codegenTableMapper.insert(table);
|
|
||||||
CodegenColumnDO column01 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId()));
|
|
||||||
codegenColumnMapper.insert(column01);
|
|
||||||
CodegenColumnDO column02 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId()));
|
|
||||||
codegenColumnMapper.insert(column02);
|
|
||||||
// 准备参数
|
|
||||||
CodegenUpdateReqVO updateReqVO = randomPojo(CodegenUpdateReqVO.class,
|
|
||||||
o -> o.getTable().setId(table.getId())
|
|
||||||
.setTemplateType(CodegenTemplateTypeEnum.ONE.getType())
|
|
||||||
.setScene(CodegenSceneEnum.ADMIN.getScene()));
|
|
||||||
CodegenColumnSaveReqVO columnVO01 = randomPojo(CodegenColumnSaveReqVO.class,
|
|
||||||
o -> o.setId(column01.getId()).setTableId(table.getId()));
|
|
||||||
CodegenColumnSaveReqVO columnVO02 = randomPojo(CodegenColumnSaveReqVO.class,
|
|
||||||
o -> o.setId(column02.getId()).setTableId(table.getId()));
|
|
||||||
updateReqVO.setColumns(Arrays.asList(columnVO01, columnVO02));
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
codegenService.updateCodegen(updateReqVO);
|
|
||||||
// 断言
|
|
||||||
CodegenTableDO dbTable = codegenTableMapper.selectById(table.getId());
|
|
||||||
assertPojoEquals(updateReqVO.getTable(), dbTable);
|
|
||||||
List<CodegenColumnDO> dbColumns = codegenColumnMapper.selectList();
|
|
||||||
assertEquals(2, dbColumns.size());
|
|
||||||
assertPojoEquals(columnVO01, dbColumns.get(0));
|
|
||||||
assertPojoEquals(columnVO02, dbColumns.get(1));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testSyncCodegenFromDB() {
|
|
||||||
// mock 数据(CodegenTableDO)
|
|
||||||
CodegenTableDO table = randomPojo(CodegenTableDO.class, o -> o.setTableName("t_yunai")
|
|
||||||
.setDataSourceConfigId(1L).setScene(CodegenSceneEnum.ADMIN.getScene()));
|
|
||||||
codegenTableMapper.insert(table);
|
|
||||||
CodegenColumnDO column01 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId())
|
|
||||||
.setColumnName("id"));
|
|
||||||
codegenColumnMapper.insert(column01);
|
|
||||||
CodegenColumnDO column02 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId())
|
|
||||||
.setColumnName("name"));
|
|
||||||
codegenColumnMapper.insert(column02);
|
|
||||||
// 准备参数
|
|
||||||
Long tableId = table.getId();
|
|
||||||
// mock 方法(TableInfo)
|
|
||||||
TableInfo tableInfo = mock(TableInfo.class);
|
|
||||||
when(databaseTableService.getTable(eq(1L), eq("t_yunai")))
|
|
||||||
.thenReturn(tableInfo);
|
|
||||||
when(tableInfo.getComment()).thenReturn("芋艿");
|
|
||||||
// mock 方法(TableInfo fields)
|
|
||||||
TableField field01 = mock(TableField.class);
|
|
||||||
when(field01.getComment()).thenReturn("主键");
|
|
||||||
TableField field03 = mock(TableField.class);
|
|
||||||
when(field03.getComment()).thenReturn("分类");
|
|
||||||
List<TableField> fields = Arrays.asList(field01, field03);
|
|
||||||
when(tableInfo.getFields()).thenReturn(fields);
|
|
||||||
when(databaseTableService.getTable(eq(1L), eq("t_yunai")))
|
|
||||||
.thenReturn(tableInfo);
|
|
||||||
// mock 方法(CodegenTableDO)
|
|
||||||
List<CodegenColumnDO> newColumns = randomPojoList(CodegenColumnDO.class);
|
|
||||||
when(codegenBuilder.buildColumns(eq(table.getId()), argThat(tableFields -> {
|
|
||||||
assertEquals(2, tableFields.size());
|
|
||||||
assertSame(tableInfo.getFields(), tableFields);
|
|
||||||
return true;
|
|
||||||
}))).thenReturn(newColumns);
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
codegenService.syncCodegenFromDB(tableId);
|
|
||||||
// 断言
|
|
||||||
List<CodegenColumnDO> dbColumns = codegenColumnMapper.selectList();
|
|
||||||
assertEquals(newColumns.size(), dbColumns.size());
|
|
||||||
assertPojoEquals(newColumns.get(0), dbColumns.get(0));
|
|
||||||
assertPojoEquals(newColumns.get(1), dbColumns.get(1));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDeleteCodegen_notExists() {
|
|
||||||
assertServiceException(() -> codegenService.deleteCodegen(randomLongId()),
|
|
||||||
CODEGEN_TABLE_NOT_EXISTS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDeleteCodegen_success() {
|
|
||||||
// mock 数据
|
|
||||||
CodegenTableDO table = randomPojo(CodegenTableDO.class,
|
|
||||||
o -> o.setScene(CodegenSceneEnum.ADMIN.getScene()));
|
|
||||||
codegenTableMapper.insert(table);
|
|
||||||
CodegenColumnDO column = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId()));
|
|
||||||
codegenColumnMapper.insert(column);
|
|
||||||
// 准备参数
|
|
||||||
Long tableId = table.getId();
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
codegenService.deleteCodegen(tableId);
|
|
||||||
// 断言
|
|
||||||
assertNull(codegenTableMapper.selectById(tableId));
|
|
||||||
assertEquals(0, codegenColumnMapper.selectList().size());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetCodegenTableList() {
|
|
||||||
// mock 数据
|
|
||||||
CodegenTableDO table01 = randomPojo(CodegenTableDO.class,
|
|
||||||
o -> o.setScene(CodegenSceneEnum.ADMIN.getScene()));
|
|
||||||
codegenTableMapper.insert(table01);
|
|
||||||
CodegenTableDO table02 = randomPojo(CodegenTableDO.class,
|
|
||||||
o -> o.setScene(CodegenSceneEnum.ADMIN.getScene()));
|
|
||||||
codegenTableMapper.insert(table02);
|
|
||||||
// 准备参数
|
|
||||||
Long dataSourceConfigId = table01.getDataSourceConfigId();
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
List<CodegenTableDO> result = codegenService.getCodegenTableList(dataSourceConfigId);
|
|
||||||
// 断言
|
|
||||||
assertEquals(1, result.size());
|
|
||||||
assertPojoEquals(table01, result.get(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetCodegenTablePage() {
|
|
||||||
// mock 数据
|
|
||||||
CodegenTableDO tableDO = randomPojo(CodegenTableDO.class, o -> {
|
|
||||||
o.setTableName("t_yunai");
|
|
||||||
o.setTableComment("芋艿");
|
|
||||||
o.setClassName("SystemYunai");
|
|
||||||
o.setCreateTime(buildTime(2021, 3, 10));
|
|
||||||
}).setScene(CodegenSceneEnum.ADMIN.getScene());
|
|
||||||
codegenTableMapper.insert(tableDO);
|
|
||||||
// 测试 tableName 不匹配
|
|
||||||
codegenTableMapper.insert(cloneIgnoreId(tableDO, o -> o.setTableName(randomString())));
|
|
||||||
// 测试 tableComment 不匹配
|
|
||||||
codegenTableMapper.insert(cloneIgnoreId(tableDO, o -> o.setTableComment(randomString())));
|
|
||||||
// 测试 className 不匹配
|
|
||||||
codegenTableMapper.insert(cloneIgnoreId(tableDO, o -> o.setClassName(randomString())));
|
|
||||||
// 测试 createTime 不匹配
|
|
||||||
codegenTableMapper.insert(cloneIgnoreId(tableDO, logDO -> logDO.setCreateTime(buildTime(2021, 4, 10))));
|
|
||||||
// 准备参数
|
|
||||||
CodegenTablePageReqVO reqVO = new CodegenTablePageReqVO();
|
|
||||||
reqVO.setTableName("yunai");
|
|
||||||
reqVO.setTableComment("芋");
|
|
||||||
reqVO.setClassName("Yunai");
|
|
||||||
reqVO.setCreateTime(buildBetweenTime(2021, 3, 1, 2021, 3, 31));
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
PageResult<CodegenTableDO> pageResult = codegenService.getCodegenTablePage(reqVO);
|
|
||||||
// 断言,只查到了一条符合条件的
|
|
||||||
assertEquals(1, pageResult.getTotal());
|
|
||||||
assertEquals(1, pageResult.getList().size());
|
|
||||||
assertPojoEquals(tableDO, pageResult.getList().get(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetCodegenTable() {
|
|
||||||
// mock 数据
|
|
||||||
CodegenTableDO tableDO = randomPojo(CodegenTableDO.class, o -> o.setScene(CodegenSceneEnum.ADMIN.getScene()));
|
|
||||||
codegenTableMapper.insert(tableDO);
|
|
||||||
// 准备参数
|
|
||||||
Long id = tableDO.getId();
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
CodegenTableDO result = codegenService.getCodegenTable(id);
|
|
||||||
// 断言
|
|
||||||
assertPojoEquals(tableDO, result);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetCodegenColumnListByTableId() {
|
|
||||||
// mock 数据
|
|
||||||
CodegenColumnDO column01 = randomPojo(CodegenColumnDO.class);
|
|
||||||
codegenColumnMapper.insert(column01);
|
|
||||||
CodegenColumnDO column02 = randomPojo(CodegenColumnDO.class);
|
|
||||||
codegenColumnMapper.insert(column02);
|
|
||||||
// 准备参数
|
|
||||||
Long tableId = column01.getTableId();
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
List<CodegenColumnDO> result = codegenService.getCodegenColumnListByTableId(tableId);
|
|
||||||
// 断言
|
|
||||||
assertEquals(1, result.size());
|
|
||||||
assertPojoEquals(column01, result.get(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGenerationCodes_tableNotExists() {
|
|
||||||
assertServiceException(() -> codegenService.generationCodes(randomLongId()),
|
|
||||||
CODEGEN_TABLE_NOT_EXISTS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGenerationCodes_columnNotExists() {
|
|
||||||
// mock 数据(CodegenTableDO)
|
|
||||||
CodegenTableDO table = randomPojo(CodegenTableDO.class,
|
|
||||||
o -> o.setScene(CodegenSceneEnum.ADMIN.getScene())
|
|
||||||
.setTemplateType(CodegenTemplateTypeEnum.MASTER_NORMAL.getType()));
|
|
||||||
codegenTableMapper.insert(table);
|
|
||||||
// 准备参数
|
|
||||||
Long tableId = table.getId();
|
|
||||||
|
|
||||||
// 调用,并断言
|
|
||||||
assertServiceException(() -> codegenService.generationCodes(tableId),
|
|
||||||
CODEGEN_COLUMN_NOT_EXISTS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGenerationCodes_sub_tableNotExists() {
|
|
||||||
// mock 数据(CodegenTableDO)
|
|
||||||
CodegenTableDO table = randomPojo(CodegenTableDO.class,
|
|
||||||
o -> o.setScene(CodegenSceneEnum.ADMIN.getScene())
|
|
||||||
.setTemplateType(CodegenTemplateTypeEnum.MASTER_NORMAL.getType()));
|
|
||||||
codegenTableMapper.insert(table);
|
|
||||||
// mock 数据(CodegenColumnDO)
|
|
||||||
CodegenColumnDO column01 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId()));
|
|
||||||
codegenColumnMapper.insert(column01);
|
|
||||||
// 准备参数
|
|
||||||
Long tableId = table.getId();
|
|
||||||
|
|
||||||
// 调用,并断言
|
|
||||||
assertServiceException(() -> codegenService.generationCodes(tableId),
|
|
||||||
CODEGEN_MASTER_GENERATION_FAIL_NO_SUB_TABLE);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGenerationCodes_sub_columnNotExists() {
|
|
||||||
// mock 数据(CodegenTableDO)
|
|
||||||
CodegenTableDO table = randomPojo(CodegenTableDO.class,
|
|
||||||
o -> o.setScene(CodegenSceneEnum.ADMIN.getScene())
|
|
||||||
.setTemplateType(CodegenTemplateTypeEnum.MASTER_NORMAL.getType()));
|
|
||||||
codegenTableMapper.insert(table);
|
|
||||||
// mock 数据(CodegenColumnDO)
|
|
||||||
CodegenColumnDO column01 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId()));
|
|
||||||
codegenColumnMapper.insert(column01);
|
|
||||||
// mock 数据(sub CodegenTableDO)
|
|
||||||
CodegenTableDO subTable = randomPojo(CodegenTableDO.class,
|
|
||||||
o -> o.setScene(CodegenSceneEnum.ADMIN.getScene())
|
|
||||||
.setTemplateType(CodegenTemplateTypeEnum.SUB.getType())
|
|
||||||
.setMasterTableId(table.getId()));
|
|
||||||
codegenTableMapper.insert(subTable);
|
|
||||||
// 准备参数
|
|
||||||
Long tableId = table.getId();
|
|
||||||
|
|
||||||
// 调用,并断言
|
|
||||||
assertServiceException(() -> codegenService.generationCodes(tableId),
|
|
||||||
CODEGEN_SUB_COLUMN_NOT_EXISTS, subTable.getId());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGenerationCodes_one_success() {
|
|
||||||
// mock 数据(CodegenTableDO)
|
|
||||||
CodegenTableDO table = randomPojo(CodegenTableDO.class,
|
|
||||||
o -> o.setScene(CodegenSceneEnum.ADMIN.getScene())
|
|
||||||
.setTemplateType(CodegenTemplateTypeEnum.ONE.getType()));
|
|
||||||
codegenTableMapper.insert(table);
|
|
||||||
// mock 数据(CodegenColumnDO)
|
|
||||||
CodegenColumnDO column01 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId()));
|
|
||||||
codegenColumnMapper.insert(column01);
|
|
||||||
CodegenColumnDO column02 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId()));
|
|
||||||
codegenColumnMapper.insert(column02);
|
|
||||||
// mock 执行生成
|
|
||||||
Map<String, String> codes = MapUtil.of(randomString(), randomString());
|
|
||||||
when(codegenEngine.execute(eq(table), argThat(columns -> {
|
|
||||||
assertEquals(2, columns.size());
|
|
||||||
assertEquals(column01, columns.get(0));
|
|
||||||
assertEquals(column02, columns.get(1));
|
|
||||||
return true;
|
|
||||||
}), isNull(), isNull())).thenReturn(codes);
|
|
||||||
// 准备参数
|
|
||||||
Long tableId = table.getId();
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
Map<String, String> result = codegenService.generationCodes(tableId);
|
|
||||||
// 断言
|
|
||||||
assertSame(codes, result);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGenerationCodes_master_success() {
|
|
||||||
// mock 数据(CodegenTableDO)
|
|
||||||
CodegenTableDO table = randomPojo(CodegenTableDO.class,
|
|
||||||
o -> o.setScene(CodegenSceneEnum.ADMIN.getScene())
|
|
||||||
.setTemplateType(CodegenTemplateTypeEnum.MASTER_NORMAL.getType()));
|
|
||||||
codegenTableMapper.insert(table);
|
|
||||||
// mock 数据(CodegenColumnDO)
|
|
||||||
CodegenColumnDO column01 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId()));
|
|
||||||
codegenColumnMapper.insert(column01);
|
|
||||||
CodegenColumnDO column02 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId()));
|
|
||||||
codegenColumnMapper.insert(column02);
|
|
||||||
// mock 数据(sub CodegenTableDO)
|
|
||||||
CodegenTableDO subTable = randomPojo(CodegenTableDO.class,
|
|
||||||
o -> o.setScene(CodegenSceneEnum.ADMIN.getScene())
|
|
||||||
.setTemplateType(CodegenTemplateTypeEnum.SUB.getType())
|
|
||||||
.setMasterTableId(table.getId())
|
|
||||||
.setSubJoinColumnId(1024L));
|
|
||||||
codegenTableMapper.insert(subTable);
|
|
||||||
// mock 数据(sub CodegenColumnDO)
|
|
||||||
CodegenColumnDO subColumn01 = randomPojo(CodegenColumnDO.class, o -> o.setId(1024L).setTableId(subTable.getId()));
|
|
||||||
codegenColumnMapper.insert(subColumn01);
|
|
||||||
// mock 执行生成
|
|
||||||
Map<String, String> codes = MapUtil.of(randomString(), randomString());
|
|
||||||
when(codegenEngine.execute(eq(table), argThat(columns -> {
|
|
||||||
assertEquals(2, columns.size());
|
|
||||||
assertEquals(column01, columns.get(0));
|
|
||||||
assertEquals(column02, columns.get(1));
|
|
||||||
return true;
|
|
||||||
}), argThat(tables -> {
|
|
||||||
assertEquals(1, tables.size());
|
|
||||||
assertPojoEquals(subTable, tables.get(0));
|
|
||||||
return true;
|
|
||||||
}), argThat(columns -> {
|
|
||||||
assertEquals(1, columns.size());
|
|
||||||
assertPojoEquals(subColumn01, columns.size());
|
|
||||||
return true;
|
|
||||||
}))).thenReturn(codes);
|
|
||||||
// 准备参数
|
|
||||||
Long tableId = table.getId();
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
Map<String, String> result = codegenService.generationCodes(tableId);
|
|
||||||
// 断言
|
|
||||||
assertSame(codes, result);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetDatabaseTableList() {
|
|
||||||
// 准备参数
|
|
||||||
Long dataSourceConfigId = randomLongId();
|
|
||||||
String name = randomString();
|
|
||||||
String comment = randomString();
|
|
||||||
// mock 方法
|
|
||||||
TableInfo tableInfo01 = mock(TableInfo.class);
|
|
||||||
when(tableInfo01.getName()).thenReturn("t_yunai");
|
|
||||||
when(tableInfo01.getComment()).thenReturn("芋艿");
|
|
||||||
TableInfo tableInfo02 = mock(TableInfo.class);
|
|
||||||
when(tableInfo02.getName()).thenReturn("t_yunai_02");
|
|
||||||
when(tableInfo02.getComment()).thenReturn("芋艿_02");
|
|
||||||
when(databaseTableService.getTableList(eq(dataSourceConfigId), eq(name), eq(comment)))
|
|
||||||
.thenReturn(ListUtil.toList(tableInfo01, tableInfo02));
|
|
||||||
// mock 数据
|
|
||||||
CodegenTableDO tableDO = randomPojo(CodegenTableDO.class,
|
|
||||||
o -> o.setScene(CodegenSceneEnum.ADMIN.getScene())
|
|
||||||
.setTableName("t_yunai_02")
|
|
||||||
.setDataSourceConfigId(dataSourceConfigId));
|
|
||||||
codegenTableMapper.insert(tableDO);
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
List<DatabaseTableRespVO> result = codegenService.getDatabaseTableList(dataSourceConfigId, name, comment);
|
|
||||||
// 断言
|
|
||||||
assertEquals(1, result.size());
|
|
||||||
assertEquals("t_yunai", result.get(0).getName());
|
|
||||||
assertEquals("芋艿", result.get(0).getComment());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,90 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.wms.service.codegen.inner;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.dataobject.codegen.CodegenColumnDO;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.dataobject.codegen.CodegenTableDO;
|
|
||||||
import cn.iocoder.yudao.module.wms.service.codegen.inner.CodegenBuilder;
|
|
||||||
import com.baomidou.mybatisplus.generator.config.po.TableField;
|
|
||||||
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
|
|
||||||
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
|
|
||||||
import org.apache.ibatis.type.JdbcType;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.mockito.InjectMocks;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
import static org.mockito.Mockito.mock;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
public class CodegenBuilderTest extends BaseMockitoUnitTest {
|
|
||||||
|
|
||||||
@InjectMocks
|
|
||||||
private CodegenBuilder codegenBuilder;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testBuildTable() {
|
|
||||||
// 准备参数
|
|
||||||
TableInfo tableInfo = mock(TableInfo.class);
|
|
||||||
// mock 方法
|
|
||||||
when(tableInfo.getName()).thenReturn("system_user");
|
|
||||||
when(tableInfo.getComment()).thenReturn("用户");
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
CodegenTableDO table = codegenBuilder.buildTable(tableInfo);
|
|
||||||
// 断言
|
|
||||||
assertEquals("system_user", table.getTableName());
|
|
||||||
assertEquals("用户", table.getTableComment());
|
|
||||||
assertEquals("system", table.getModuleName());
|
|
||||||
assertEquals("user", table.getBusinessName());
|
|
||||||
assertEquals("User", table.getClassName());
|
|
||||||
assertEquals("用户", table.getClassComment());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testBuildColumns() {
|
|
||||||
// 准备参数
|
|
||||||
Long tableId = randomLongId();
|
|
||||||
TableField tableField = mock(TableField.class);
|
|
||||||
List<TableField> tableFields = Collections.singletonList(tableField);
|
|
||||||
// mock 方法
|
|
||||||
TableField.MetaInfo metaInfo = mock(TableField.MetaInfo.class);
|
|
||||||
when(tableField.getMetaInfo()).thenReturn(metaInfo);
|
|
||||||
when(metaInfo.getJdbcType()).thenReturn(JdbcType.BIGINT);
|
|
||||||
when(tableField.getComment()).thenReturn("编号");
|
|
||||||
when(tableField.isKeyFlag()).thenReturn(true);
|
|
||||||
when(tableField.isKeyIdentityFlag()).thenReturn(true);
|
|
||||||
IColumnType columnType = mock(IColumnType.class);
|
|
||||||
when(tableField.getColumnType()).thenReturn(columnType);
|
|
||||||
when(columnType.getType()).thenReturn("Long");
|
|
||||||
when(tableField.getName()).thenReturn("id2");
|
|
||||||
when(tableField.getPropertyName()).thenReturn("id");
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
List<CodegenColumnDO> columns = codegenBuilder.buildColumns(tableId, tableFields);
|
|
||||||
// 断言
|
|
||||||
assertEquals(1, columns.size());
|
|
||||||
CodegenColumnDO column = columns.get(0);
|
|
||||||
assertEquals(tableId, column.getTableId());
|
|
||||||
assertEquals("id2", column.getColumnName());
|
|
||||||
assertEquals("BIGINT", column.getDataType());
|
|
||||||
assertEquals("编号", column.getColumnComment());
|
|
||||||
assertFalse(column.getNullable());
|
|
||||||
assertTrue(column.getPrimaryKey());
|
|
||||||
assertTrue(column.getAutoIncrement());
|
|
||||||
assertEquals(1, column.getOrdinalPosition());
|
|
||||||
assertEquals("Long", column.getJavaType());
|
|
||||||
assertEquals("id", column.getJavaField());
|
|
||||||
assertNull(column.getDictType());
|
|
||||||
assertNotNull(column.getExample());
|
|
||||||
assertFalse(column.getCreateOperation());
|
|
||||||
assertTrue(column.getUpdateOperation());
|
|
||||||
assertFalse(column.getListOperation());
|
|
||||||
assertEquals("=", column.getListOperationCondition());
|
|
||||||
assertTrue(column.getListOperationResult());
|
|
||||||
assertEquals("input", column.getHtmlType());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,128 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.wms.service.codegen.inner;
|
|
||||||
|
|
||||||
import cn.hutool.core.io.FileUtil;
|
|
||||||
import cn.hutool.core.io.IoUtil;
|
|
||||||
import cn.hutool.core.io.resource.ResourceUtil;
|
|
||||||
import cn.hutool.core.map.MapUtil;
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
|
||||||
import cn.hutool.core.util.ZipUtil;
|
|
||||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
|
||||||
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.dataobject.codegen.CodegenColumnDO;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.dataobject.codegen.CodegenTableDO;
|
|
||||||
import cn.iocoder.yudao.module.wms.framework.codegen.config.CodegenProperties;
|
|
||||||
import cn.iocoder.yudao.module.wms.service.codegen.inner.CodegenEngine;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.mockito.InjectMocks;
|
|
||||||
import org.mockito.Spy;
|
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link CodegenEngine} 的单元测试抽象基类
|
|
||||||
*
|
|
||||||
|
|
||||||
*/
|
|
||||||
public abstract class CodegenEngineAbstractTest extends BaseMockitoUnitTest {
|
|
||||||
|
|
||||||
@InjectMocks
|
|
||||||
protected CodegenEngine codegenEngine;
|
|
||||||
|
|
||||||
@Spy
|
|
||||||
protected CodegenProperties codegenProperties = new CodegenProperties()
|
|
||||||
.setBasePackage("cn.iocoder.yudao");
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
public void setUp() {
|
|
||||||
codegenEngine.initGlobalBindingMap();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static CodegenTableDO getTable(String name) {
|
|
||||||
String content = ResourceUtil.readUtf8Str("codegen/table/" + name + ".json");
|
|
||||||
return JsonUtils.parseObject(content, "table", CodegenTableDO.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static List<CodegenColumnDO> getColumnList(String name) {
|
|
||||||
String content = ResourceUtil.readUtf8Str("codegen/table/" + name + ".json");
|
|
||||||
List<CodegenColumnDO> list = JsonUtils.parseArray(content, "columns", CodegenColumnDO.class);
|
|
||||||
list.forEach(column -> {
|
|
||||||
if (column.getNullable() == null) {
|
|
||||||
column.setNullable(false);
|
|
||||||
}
|
|
||||||
if (column.getCreateOperation() == null) {
|
|
||||||
column.setCreateOperation(false);
|
|
||||||
}
|
|
||||||
if (column.getUpdateOperation() == null) {
|
|
||||||
column.setUpdateOperation(false);
|
|
||||||
}
|
|
||||||
if (column.getListOperation() == null) {
|
|
||||||
column.setListOperation(false);
|
|
||||||
}
|
|
||||||
if (column.getListOperationResult() == null) {
|
|
||||||
column.setListOperationResult(false);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("rawtypes")
|
|
||||||
protected static void assertResult(Map<String, String> result, String path) {
|
|
||||||
String assertContent = ResourceUtil.readUtf8Str(path + "/assert.json");
|
|
||||||
List<HashMap> asserts = JsonUtils.parseArray(assertContent, HashMap.class);
|
|
||||||
assertEquals(asserts.size(), result.size());
|
|
||||||
// 校验每个文件
|
|
||||||
asserts.forEach(assertMap -> {
|
|
||||||
String contentPath = (String) assertMap.get("contentPath");
|
|
||||||
String filePath = (String) assertMap.get("filePath");
|
|
||||||
String content = ResourceUtil.readUtf8Str(path + "/" + contentPath);
|
|
||||||
assertEquals(content, result.get(filePath), filePath + ":不匹配");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// ==================== 调试专用 ====================
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 【调试使用】将生成的代码,写入到文件
|
|
||||||
*
|
|
||||||
* @param result 生成的代码
|
|
||||||
* @param path 写入文件的路径
|
|
||||||
*/
|
|
||||||
protected void writeFile(Map<String, String> result, String path) {
|
|
||||||
// 生成压缩包
|
|
||||||
String[] paths = result.keySet().toArray(new String[0]);
|
|
||||||
ByteArrayInputStream[] ins = result.values().stream().map(IoUtil::toUtf8Stream).toArray(ByteArrayInputStream[]::new);
|
|
||||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
||||||
ZipUtil.zip(outputStream, paths, ins);
|
|
||||||
// 写入文件
|
|
||||||
FileUtil.writeBytes(outputStream.toByteArray(), path);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 【调试使用】将生成的结果,写入到文件
|
|
||||||
*
|
|
||||||
* @param result 生成的代码
|
|
||||||
* @param basePath 写入文件的路径(绝对路径)
|
|
||||||
*/
|
|
||||||
protected void writeResult(Map<String, String> result, String basePath) {
|
|
||||||
// 写入文件内容
|
|
||||||
List<Map<String, String>> asserts = new ArrayList<>();
|
|
||||||
result.forEach((filePath, fileContent) -> {
|
|
||||||
String lastFilePath = StrUtil.subAfter(filePath, '/', true);
|
|
||||||
String contentPath = StrUtil.subAfter(lastFilePath, '.', true)
|
|
||||||
+ '/' + StrUtil.subBefore(lastFilePath, '.', true);
|
|
||||||
asserts.add(MapUtil.<String, String>builder().put("filePath", filePath)
|
|
||||||
.put("contentPath", contentPath).build());
|
|
||||||
FileUtil.writeUtf8String(fileContent, basePath + "/" + contentPath);
|
|
||||||
});
|
|
||||||
// 写入 assert.json 文件
|
|
||||||
FileUtil.writeUtf8String(JsonUtils.toJsonPrettyString(asserts), basePath +"/assert.json");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,203 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.wms.service.codegen.inner;
|
|
||||||
|
|
||||||
import cn.hutool.core.io.FileUtil;
|
|
||||||
import cn.hutool.core.io.IoUtil;
|
|
||||||
import cn.hutool.core.io.resource.ResourceUtil;
|
|
||||||
import cn.hutool.core.map.MapUtil;
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
|
||||||
import cn.hutool.core.util.ZipUtil;
|
|
||||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
|
||||||
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.dataobject.codegen.CodegenColumnDO;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.dataobject.codegen.CodegenTableDO;
|
|
||||||
import cn.iocoder.yudao.module.wms.enums.codegen.CodegenFrontTypeEnum;
|
|
||||||
import cn.iocoder.yudao.module.wms.enums.codegen.CodegenTemplateTypeEnum;
|
|
||||||
import cn.iocoder.yudao.module.wms.framework.codegen.config.CodegenProperties;
|
|
||||||
import cn.iocoder.yudao.module.wms.service.codegen.inner.CodegenEngine;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.mockito.InjectMocks;
|
|
||||||
import org.mockito.Spy;
|
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link CodegenEngine} 的单元测试
|
|
||||||
*
|
|
||||||
|
|
||||||
*/
|
|
||||||
public class CodegenEngineTest extends BaseMockitoUnitTest {
|
|
||||||
|
|
||||||
@InjectMocks
|
|
||||||
private CodegenEngine codegenEngine;
|
|
||||||
|
|
||||||
@Spy
|
|
||||||
private CodegenProperties codegenProperties = new CodegenProperties()
|
|
||||||
.setBasePackage("cn.iocoder.yudao");
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
public void setUp() {
|
|
||||||
codegenEngine.initGlobalBindingMap();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testExecute_vue3_one() {
|
|
||||||
// 准备参数
|
|
||||||
CodegenTableDO table = getTable("student")
|
|
||||||
.setFrontType(CodegenFrontTypeEnum.VUE3.getType())
|
|
||||||
.setTemplateType(CodegenTemplateTypeEnum.ONE.getType());
|
|
||||||
List<CodegenColumnDO> columns = getColumnList("student");
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
Map<String, String> result = codegenEngine.execute(table, columns, null, null);
|
|
||||||
// 断言
|
|
||||||
assertResult(result, "codegen/vue3_one");
|
|
||||||
// writeResult(result, "/root/ruoyi-vue-pro/yudao-module-infra/yudao-module-infra-biz/src/test/resources/codegen/vue3_one");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testExecute_vue3_tree() {
|
|
||||||
// 准备参数
|
|
||||||
CodegenTableDO table = getTable("category")
|
|
||||||
.setFrontType(CodegenFrontTypeEnum.VUE3.getType())
|
|
||||||
.setTemplateType(CodegenTemplateTypeEnum.TREE.getType());
|
|
||||||
List<CodegenColumnDO> columns = getColumnList("category");
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
Map<String, String> result = codegenEngine.execute(table, columns, null, null);
|
|
||||||
// 断言
|
|
||||||
assertResult(result, "codegen/vue3_tree");
|
|
||||||
// writeResult(result, "/root/ruoyi-vue-pro/yudao-module-infra/yudao-module-infra-biz/src/test/resources/codegen/vue3_tree");
|
|
||||||
// writeFile(result, "/Users/yunai/test/demo66.zip");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testExecute_vue3_master_normal() {
|
|
||||||
testExecute_vue3_master(CodegenTemplateTypeEnum.MASTER_NORMAL, "codegen/vue3_master_normal");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testExecute_vue3_master_erp() {
|
|
||||||
testExecute_vue3_master(CodegenTemplateTypeEnum.MASTER_ERP, "codegen/vue3_master_erp");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testExecute_vue3_master_inner() {
|
|
||||||
testExecute_vue3_master(CodegenTemplateTypeEnum.MASTER_INNER, "codegen/vue3_master_inner");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void testExecute_vue3_master(CodegenTemplateTypeEnum templateType,
|
|
||||||
String path) {
|
|
||||||
// 准备参数
|
|
||||||
CodegenTableDO table = getTable("student")
|
|
||||||
.setFrontType(CodegenFrontTypeEnum.VUE3.getType())
|
|
||||||
.setTemplateType(templateType.getType());
|
|
||||||
List<CodegenColumnDO> columns = getColumnList("student");
|
|
||||||
// 准备参数(子表)
|
|
||||||
CodegenTableDO contactTable = getTable("contact")
|
|
||||||
.setTemplateType(CodegenTemplateTypeEnum.SUB.getType())
|
|
||||||
.setFrontType(CodegenFrontTypeEnum.VUE3.getType())
|
|
||||||
.setSubJoinColumnId(100L).setSubJoinMany(true);
|
|
||||||
List<CodegenColumnDO> contactColumns = getColumnList("contact");
|
|
||||||
// 准备参数(班主任)
|
|
||||||
CodegenTableDO teacherTable = getTable("teacher")
|
|
||||||
.setTemplateType(CodegenTemplateTypeEnum.SUB.getType())
|
|
||||||
.setFrontType(CodegenFrontTypeEnum.VUE3.getType())
|
|
||||||
.setSubJoinColumnId(200L).setSubJoinMany(false);
|
|
||||||
List<CodegenColumnDO> teacherColumns = getColumnList("teacher");
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
Map<String, String> result = codegenEngine.execute(table, columns,
|
|
||||||
Arrays.asList(contactTable, teacherTable), Arrays.asList(contactColumns, teacherColumns));
|
|
||||||
// 断言
|
|
||||||
assertResult(result, path);
|
|
||||||
// writeResult(result, "/root/ruoyi-vue-pro/yudao-module-infra/yudao-module-infra-biz/src/test/resources/" + path);
|
|
||||||
// writeFile(result, "/Users/yunai/test/demo11.zip");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static CodegenTableDO getTable(String name) {
|
|
||||||
String content = ResourceUtil.readUtf8Str("codegen/table/" + name + ".json");
|
|
||||||
return JsonUtils.parseObject(content, "table", CodegenTableDO.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static List<CodegenColumnDO> getColumnList(String name) {
|
|
||||||
String content = ResourceUtil.readUtf8Str("codegen/table/" + name + ".json");
|
|
||||||
List<CodegenColumnDO> list = JsonUtils.parseArray(content, "columns", CodegenColumnDO.class);
|
|
||||||
list.forEach(column -> {
|
|
||||||
if (column.getNullable() == null) {
|
|
||||||
column.setNullable(false);
|
|
||||||
}
|
|
||||||
if (column.getCreateOperation() == null) {
|
|
||||||
column.setCreateOperation(false);
|
|
||||||
}
|
|
||||||
if (column.getUpdateOperation() == null) {
|
|
||||||
column.setUpdateOperation(false);
|
|
||||||
}
|
|
||||||
if (column.getListOperation() == null) {
|
|
||||||
column.setListOperation(false);
|
|
||||||
}
|
|
||||||
if (column.getListOperationResult() == null) {
|
|
||||||
column.setListOperationResult(false);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("rawtypes")
|
|
||||||
private static void assertResult(Map<String, String> result, String path) {
|
|
||||||
String assertContent = ResourceUtil.readUtf8Str(path + "/assert.json");
|
|
||||||
List<HashMap> asserts = JsonUtils.parseArray(assertContent, HashMap.class);
|
|
||||||
assertEquals(asserts.size(), result.size());
|
|
||||||
// 校验每个文件
|
|
||||||
asserts.forEach(assertMap -> {
|
|
||||||
String contentPath = (String) assertMap.get("contentPath");
|
|
||||||
String filePath = (String) assertMap.get("filePath");
|
|
||||||
String content = ResourceUtil.readUtf8Str(path + "/" + contentPath);
|
|
||||||
assertEquals(content, result.get(filePath), filePath + ":不匹配");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// ==================== 调试专用 ====================
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 【调试使用】将生成的代码,写入到文件
|
|
||||||
*
|
|
||||||
* @param result 生成的代码
|
|
||||||
* @param path 写入文件的路径
|
|
||||||
*/
|
|
||||||
private void writeFile(Map<String, String> result, String path) {
|
|
||||||
// 生成压缩包
|
|
||||||
String[] paths = result.keySet().toArray(new String[0]);
|
|
||||||
ByteArrayInputStream[] ins = result.values().stream().map(IoUtil::toUtf8Stream).toArray(ByteArrayInputStream[]::new);
|
|
||||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
||||||
ZipUtil.zip(outputStream, paths, ins);
|
|
||||||
// 写入文件
|
|
||||||
FileUtil.writeBytes(outputStream.toByteArray(), path);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 【调试使用】将生成的结果,写入到文件
|
|
||||||
*
|
|
||||||
* @param result 生成的代码
|
|
||||||
* @param basePath 写入文件的路径(绝对路径)
|
|
||||||
*/
|
|
||||||
private void writeResult(Map<String, String> result, String basePath) {
|
|
||||||
// 写入文件内容
|
|
||||||
List<Map<String, String>> asserts = new ArrayList<>();
|
|
||||||
result.forEach((filePath, fileContent) -> {
|
|
||||||
String lastFilePath = StrUtil.subAfter(filePath, '/', true);
|
|
||||||
String contentPath = StrUtil.subAfter(lastFilePath, '.', true)
|
|
||||||
+ '/' + StrUtil.subBefore(lastFilePath, '.', true);
|
|
||||||
asserts.add(MapUtil.<String, String>builder().put("filePath", filePath)
|
|
||||||
.put("contentPath", contentPath).build());
|
|
||||||
FileUtil.writeUtf8String(fileContent, basePath + "/" + contentPath);
|
|
||||||
});
|
|
||||||
// 写入 assert.json 文件
|
|
||||||
FileUtil.writeUtf8String(JsonUtils.toJsonPrettyString(asserts), basePath +"/assert.json");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,99 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.wms.service.codegen.inner;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.dataobject.codegen.CodegenColumnDO;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.dataobject.codegen.CodegenTableDO;
|
|
||||||
import cn.iocoder.yudao.module.wms.enums.codegen.CodegenFrontTypeEnum;
|
|
||||||
import cn.iocoder.yudao.module.wms.enums.codegen.CodegenTemplateTypeEnum;
|
|
||||||
import cn.iocoder.yudao.module.wms.service.codegen.inner.CodegenEngine;
|
|
||||||
import org.junit.jupiter.api.Disabled;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
// TODO @puhui999:单测需要 fix
|
|
||||||
/**
|
|
||||||
* {@link CodegenEngine} 的 Vue2 + Element UI 单元测试
|
|
||||||
*
|
|
||||||
|
|
||||||
*/
|
|
||||||
@Disabled
|
|
||||||
public class CodegenEngineVue2Test extends CodegenEngineAbstractTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testExecute_vue2_one() {
|
|
||||||
// 准备参数
|
|
||||||
CodegenTableDO table = getTable("student")
|
|
||||||
.setFrontType(CodegenFrontTypeEnum.VUE2.getType())
|
|
||||||
.setTemplateType(CodegenTemplateTypeEnum.ONE.getType());
|
|
||||||
List<CodegenColumnDO> columns = getColumnList("student");
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
Map<String, String> result = codegenEngine.execute(table, columns, null, null);
|
|
||||||
// 断言
|
|
||||||
assertResult(result, "codegen/vue2_one");
|
|
||||||
// writeResult(result, "/root/ruoyi-vue-pro/yudao-module-infra/yudao-module-infra-biz/src/test/resources/codegen/vue2_one");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testExecute_vue2_tree() {
|
|
||||||
// 准备参数
|
|
||||||
CodegenTableDO table = getTable("category")
|
|
||||||
.setFrontType(CodegenFrontTypeEnum.VUE2.getType())
|
|
||||||
.setTemplateType(CodegenTemplateTypeEnum.TREE.getType());
|
|
||||||
List<CodegenColumnDO> columns = getColumnList("category");
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
Map<String, String> result = codegenEngine.execute(table, columns, null, null);
|
|
||||||
// 断言
|
|
||||||
assertResult(result, "codegen/vue2_tree");
|
|
||||||
// writeResult(result, "/root/ruoyi-vue-pro/yudao-module-infra/yudao-module-infra-biz/src/test/resources/codegen/vue2_tree");
|
|
||||||
// writeFile(result, "/Users/yunai/test/demo66.zip");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testExecute_vue2_master_normal() {
|
|
||||||
testExecute_vue2_master(CodegenTemplateTypeEnum.MASTER_NORMAL, "codegen/vue2_master_normal");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testExecute_vue2_master_erp() {
|
|
||||||
testExecute_vue2_master(CodegenTemplateTypeEnum.MASTER_ERP, "codegen/vue2_master_erp");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testExecute_vue2_master_inner() {
|
|
||||||
testExecute_vue2_master(CodegenTemplateTypeEnum.MASTER_INNER, "codegen/vue2_master_inner");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void testExecute_vue2_master(CodegenTemplateTypeEnum templateType,
|
|
||||||
String path) {
|
|
||||||
// 准备参数
|
|
||||||
CodegenTableDO table = getTable("student")
|
|
||||||
.setFrontType(CodegenFrontTypeEnum.VUE2.getType())
|
|
||||||
.setTemplateType(templateType.getType());
|
|
||||||
List<CodegenColumnDO> columns = getColumnList("student");
|
|
||||||
// 准备参数(子表)
|
|
||||||
CodegenTableDO contactTable = getTable("contact")
|
|
||||||
.setTemplateType(CodegenTemplateTypeEnum.SUB.getType())
|
|
||||||
.setFrontType(CodegenFrontTypeEnum.VUE2.getType())
|
|
||||||
.setSubJoinColumnId(100L).setSubJoinMany(true);
|
|
||||||
List<CodegenColumnDO> contactColumns = getColumnList("contact");
|
|
||||||
// 准备参数(班主任)
|
|
||||||
CodegenTableDO teacherTable = getTable("teacher")
|
|
||||||
.setTemplateType(CodegenTemplateTypeEnum.SUB.getType())
|
|
||||||
.setFrontType(CodegenFrontTypeEnum.VUE2.getType())
|
|
||||||
.setSubJoinColumnId(200L).setSubJoinMany(false);
|
|
||||||
List<CodegenColumnDO> teacherColumns = getColumnList("teacher");
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
Map<String, String> result = codegenEngine.execute(table, columns,
|
|
||||||
Arrays.asList(contactTable, teacherTable), Arrays.asList(contactColumns, teacherColumns));
|
|
||||||
// 断言
|
|
||||||
assertResult(result, path);
|
|
||||||
// writeResult(result, "/root/ruoyi-vue-pro/yudao-module-infra/yudao-module-infra-biz/src/test/resources/" + path);
|
|
||||||
// writeFile(result, "/Users/yunai/test/demo11.zip");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,96 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.wms.service.codegen.inner;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.dataobject.codegen.CodegenColumnDO;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.dataobject.codegen.CodegenTableDO;
|
|
||||||
import cn.iocoder.yudao.module.wms.enums.codegen.CodegenFrontTypeEnum;
|
|
||||||
import cn.iocoder.yudao.module.wms.enums.codegen.CodegenTemplateTypeEnum;
|
|
||||||
import cn.iocoder.yudao.module.wms.service.codegen.inner.CodegenEngine;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link CodegenEngine} 的 Vue2 + Element Plus 单元测试
|
|
||||||
*
|
|
||||||
|
|
||||||
*/
|
|
||||||
public class CodegenEngineVue3Test extends CodegenEngineAbstractTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testExecute_vue3_one() {
|
|
||||||
// 准备参数
|
|
||||||
CodegenTableDO table = getTable("student")
|
|
||||||
.setFrontType(CodegenFrontTypeEnum.VUE3.getType())
|
|
||||||
.setTemplateType(CodegenTemplateTypeEnum.ONE.getType());
|
|
||||||
List<CodegenColumnDO> columns = getColumnList("student");
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
Map<String, String> result = codegenEngine.execute(table, columns, null, null);
|
|
||||||
// 断言
|
|
||||||
assertResult(result, "codegen/vue3_one");
|
|
||||||
// writeResult(result, "/root/ruoyi-vue-pro/yudao-module-infra/yudao-module-infra-biz/src/test/resources/codegen/vue3_one");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testExecute_vue3_tree() {
|
|
||||||
// 准备参数
|
|
||||||
CodegenTableDO table = getTable("category")
|
|
||||||
.setFrontType(CodegenFrontTypeEnum.VUE3.getType())
|
|
||||||
.setTemplateType(CodegenTemplateTypeEnum.TREE.getType());
|
|
||||||
List<CodegenColumnDO> columns = getColumnList("category");
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
Map<String, String> result = codegenEngine.execute(table, columns, null, null);
|
|
||||||
// 断言
|
|
||||||
assertResult(result, "codegen/vue3_tree");
|
|
||||||
// writeResult(result, "/root/ruoyi-vue-pro/yudao-module-infra/yudao-module-infra-biz/src/test/resources/codegen/vue3_tree");
|
|
||||||
// writeFile(result, "/Users/yunai/test/demo66.zip");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testExecute_vue3_master_normal() {
|
|
||||||
testExecute_vue3_master(CodegenTemplateTypeEnum.MASTER_NORMAL, "codegen/vue3_master_normal");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testExecute_vue3_master_erp() {
|
|
||||||
testExecute_vue3_master(CodegenTemplateTypeEnum.MASTER_ERP, "codegen/vue3_master_erp");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testExecute_vue3_master_inner() {
|
|
||||||
testExecute_vue3_master(CodegenTemplateTypeEnum.MASTER_INNER, "codegen/vue3_master_inner");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void testExecute_vue3_master(CodegenTemplateTypeEnum templateType,
|
|
||||||
String path) {
|
|
||||||
// 准备参数
|
|
||||||
CodegenTableDO table = getTable("student")
|
|
||||||
.setFrontType(CodegenFrontTypeEnum.VUE3.getType())
|
|
||||||
.setTemplateType(templateType.getType());
|
|
||||||
List<CodegenColumnDO> columns = getColumnList("student");
|
|
||||||
// 准备参数(子表)
|
|
||||||
CodegenTableDO contactTable = getTable("contact")
|
|
||||||
.setTemplateType(CodegenTemplateTypeEnum.SUB.getType())
|
|
||||||
.setFrontType(CodegenFrontTypeEnum.VUE3.getType())
|
|
||||||
.setSubJoinColumnId(100L).setSubJoinMany(true);
|
|
||||||
List<CodegenColumnDO> contactColumns = getColumnList("contact");
|
|
||||||
// 准备参数(班主任)
|
|
||||||
CodegenTableDO teacherTable = getTable("teacher")
|
|
||||||
.setTemplateType(CodegenTemplateTypeEnum.SUB.getType())
|
|
||||||
.setFrontType(CodegenFrontTypeEnum.VUE3.getType())
|
|
||||||
.setSubJoinColumnId(200L).setSubJoinMany(false);
|
|
||||||
List<CodegenColumnDO> teacherColumns = getColumnList("teacher");
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
Map<String, String> result = codegenEngine.execute(table, columns,
|
|
||||||
Arrays.asList(contactTable, teacherTable), Arrays.asList(contactColumns, teacherColumns));
|
|
||||||
// 断言
|
|
||||||
assertResult(result, path);
|
|
||||||
// writeResult(result, "/root/ruoyi-vue-pro/yudao-module-infra/yudao-module-infra-biz/src/test/resources/" + path);
|
|
||||||
// writeFile(result, "/Users/yunai/test/demo11.zip");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,220 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.wms.service.config;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
||||||
import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils;
|
|
||||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
|
||||||
import cn.iocoder.yudao.framework.test.core.util.RandomUtils;
|
|
||||||
import cn.iocoder.yudao.module.infra.controller.admin.config.vo.ConfigPageReqVO;
|
|
||||||
import cn.iocoder.yudao.module.infra.controller.admin.config.vo.ConfigSaveReqVO;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.dataobject.config.ConfigDO;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.mysql.config.ConfigMapper;
|
|
||||||
import cn.iocoder.yudao.module.wms.enums.config.ConfigTypeEnum;
|
|
||||||
import cn.iocoder.yudao.module.wms.service.config.ConfigServiceImpl;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.context.annotation.Import;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
|
||||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
|
|
||||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
|
|
||||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
|
||||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.*;
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
|
|
||||||
@Import(ConfigServiceImpl.class)
|
|
||||||
public class ConfigServiceImplTest extends BaseDbUnitTest {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private ConfigServiceImpl configService;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private ConfigMapper configMapper;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCreateConfig_success() {
|
|
||||||
// 准备参数
|
|
||||||
ConfigSaveReqVO reqVO = randomPojo(ConfigSaveReqVO.class)
|
|
||||||
.setId(null); // 防止 id 被赋值,导致唯一性校验失败
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
Long configId = configService.createConfig(reqVO);
|
|
||||||
// 断言
|
|
||||||
assertNotNull(configId);
|
|
||||||
// 校验记录的属性是否正确
|
|
||||||
ConfigDO config = configMapper.selectById(configId);
|
|
||||||
assertPojoEquals(reqVO, config, "id");
|
|
||||||
assertEquals(ConfigTypeEnum.CUSTOM.getType(), config.getType());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUpdateConfig_success() {
|
|
||||||
// mock 数据
|
|
||||||
ConfigDO dbConfig = randomConfigDO();
|
|
||||||
configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据
|
|
||||||
// 准备参数
|
|
||||||
ConfigSaveReqVO reqVO = randomPojo(ConfigSaveReqVO.class, o -> {
|
|
||||||
o.setId(dbConfig.getId()); // 设置更新的 ID
|
|
||||||
});
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
configService.updateConfig(reqVO);
|
|
||||||
// 校验是否更新正确
|
|
||||||
ConfigDO config = configMapper.selectById(reqVO.getId()); // 获取最新的
|
|
||||||
assertPojoEquals(reqVO, config);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDeleteConfig_success() {
|
|
||||||
// mock 数据
|
|
||||||
ConfigDO dbConfig = randomConfigDO(o -> {
|
|
||||||
o.setType(ConfigTypeEnum.CUSTOM.getType()); // 只能删除 CUSTOM 类型
|
|
||||||
});
|
|
||||||
configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据
|
|
||||||
// 准备参数
|
|
||||||
Long id = dbConfig.getId();
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
configService.deleteConfig(id);
|
|
||||||
// 校验数据不存在了
|
|
||||||
assertNull(configMapper.selectById(id));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDeleteConfig_canNotDeleteSystemType() {
|
|
||||||
// mock 数据
|
|
||||||
ConfigDO dbConfig = randomConfigDO(o -> {
|
|
||||||
o.setType(ConfigTypeEnum.SYSTEM.getType()); // SYSTEM 不允许删除
|
|
||||||
});
|
|
||||||
configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据
|
|
||||||
// 准备参数
|
|
||||||
Long id = dbConfig.getId();
|
|
||||||
|
|
||||||
// 调用, 并断言异常
|
|
||||||
assertServiceException(() -> configService.deleteConfig(id), CONFIG_CAN_NOT_DELETE_SYSTEM_TYPE);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testValidateConfigExists_success() {
|
|
||||||
// mock 数据
|
|
||||||
ConfigDO dbConfigDO = randomConfigDO();
|
|
||||||
configMapper.insert(dbConfigDO);// @Sql: 先插入出一条存在的数据
|
|
||||||
|
|
||||||
// 调用成功
|
|
||||||
configService.validateConfigExists(dbConfigDO.getId());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testValidateConfigExist_notExists() {
|
|
||||||
assertServiceException(() -> configService.validateConfigExists(randomLongId()), CONFIG_NOT_EXISTS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testValidateConfigKeyUnique_success() {
|
|
||||||
// 调用,成功
|
|
||||||
configService.validateConfigKeyUnique(randomLongId(), randomString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testValidateConfigKeyUnique_keyDuplicateForCreate() {
|
|
||||||
// 准备参数
|
|
||||||
String key = randomString();
|
|
||||||
// mock 数据
|
|
||||||
configMapper.insert(randomConfigDO(o -> o.setConfigKey(key)));
|
|
||||||
|
|
||||||
// 调用,校验异常
|
|
||||||
assertServiceException(() -> configService.validateConfigKeyUnique(null, key),
|
|
||||||
CONFIG_KEY_DUPLICATE);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testValidateConfigKeyUnique_keyDuplicateForUpdate() {
|
|
||||||
// 准备参数
|
|
||||||
Long id = randomLongId();
|
|
||||||
String key = randomString();
|
|
||||||
// mock 数据
|
|
||||||
configMapper.insert(randomConfigDO(o -> o.setConfigKey(key)));
|
|
||||||
|
|
||||||
// 调用,校验异常
|
|
||||||
assertServiceException(() -> configService.validateConfigKeyUnique(id, key),
|
|
||||||
CONFIG_KEY_DUPLICATE);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetConfigPage() {
|
|
||||||
// mock 数据
|
|
||||||
ConfigDO dbConfig = randomConfigDO(o -> { // 等会查询到
|
|
||||||
o.setName("芋艿");
|
|
||||||
o.setConfigKey("yunai");
|
|
||||||
o.setType(ConfigTypeEnum.SYSTEM.getType());
|
|
||||||
o.setCreateTime(buildTime(2021, 2, 1));
|
|
||||||
});
|
|
||||||
configMapper.insert(dbConfig);
|
|
||||||
// 测试 name 不匹配
|
|
||||||
configMapper.insert(cloneIgnoreId(dbConfig, o -> o.setName("土豆")));
|
|
||||||
// 测试 key 不匹配
|
|
||||||
configMapper.insert(cloneIgnoreId(dbConfig, o -> o.setConfigKey("tudou")));
|
|
||||||
// 测试 type 不匹配
|
|
||||||
configMapper.insert(cloneIgnoreId(dbConfig, o -> o.setType(ConfigTypeEnum.CUSTOM.getType())));
|
|
||||||
// 测试 createTime 不匹配
|
|
||||||
configMapper.insert(cloneIgnoreId(dbConfig, o -> o.setCreateTime(buildTime(2021, 1, 1))));
|
|
||||||
// 准备参数
|
|
||||||
ConfigPageReqVO reqVO = new ConfigPageReqVO();
|
|
||||||
reqVO.setName("艿");
|
|
||||||
reqVO.setKey("nai");
|
|
||||||
reqVO.setType(ConfigTypeEnum.SYSTEM.getType());
|
|
||||||
reqVO.setCreateTime(buildBetweenTime(2021, 1, 15, 2021, 2, 15));
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
PageResult<ConfigDO> pageResult = configService.getConfigPage(reqVO);
|
|
||||||
// 断言
|
|
||||||
assertEquals(1, pageResult.getTotal());
|
|
||||||
assertEquals(1, pageResult.getList().size());
|
|
||||||
assertPojoEquals(dbConfig, pageResult.getList().get(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetConfig() {
|
|
||||||
// mock 数据
|
|
||||||
ConfigDO dbConfig = randomConfigDO();
|
|
||||||
configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据
|
|
||||||
// 准备参数
|
|
||||||
Long id = dbConfig.getId();
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
ConfigDO config = configService.getConfig(id);
|
|
||||||
// 断言
|
|
||||||
assertNotNull(config);
|
|
||||||
assertPojoEquals(dbConfig, config);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetConfigByKey() {
|
|
||||||
// mock 数据
|
|
||||||
ConfigDO dbConfig = randomConfigDO();
|
|
||||||
configMapper.insert(dbConfig);// @Sql: 先插入出一条存在的数据
|
|
||||||
// 准备参数
|
|
||||||
String key = dbConfig.getConfigKey();
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
ConfigDO config = configService.getConfigByKey(key);
|
|
||||||
// 断言
|
|
||||||
assertNotNull(config);
|
|
||||||
assertPojoEquals(dbConfig, config);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ========== 随机对象 ==========
|
|
||||||
|
|
||||||
@SafeVarargs
|
|
||||||
private static ConfigDO randomConfigDO(Consumer<ConfigDO>... consumers) {
|
|
||||||
Consumer<ConfigDO> consumer = (o) -> {
|
|
||||||
o.setType(randomEle(ConfigTypeEnum.values()).getType()); // 保证 key 的范围
|
|
||||||
};
|
|
||||||
return RandomUtils.randomPojo(ConfigDO.class, ArrayUtils.append(consumer, consumers));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,206 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.wms.service.db;
|
|
||||||
|
|
||||||
import cn.hutool.core.map.MapUtil;
|
|
||||||
import cn.hutool.core.util.ReflectUtil;
|
|
||||||
import cn.hutool.crypto.symmetric.AES;
|
|
||||||
import cn.iocoder.yudao.framework.mybatis.core.type.EncryptTypeHandler;
|
|
||||||
import cn.iocoder.yudao.framework.mybatis.core.util.JdbcUtils;
|
|
||||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
|
||||||
import cn.iocoder.yudao.module.infra.controller.admin.db.vo.DataSourceConfigSaveReqVO;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.dataobject.db.DataSourceConfigDO;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.mysql.db.DataSourceConfigMapper;
|
|
||||||
import cn.iocoder.yudao.module.wms.service.db.DataSourceConfigServiceImpl;
|
|
||||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
|
|
||||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.mockito.MockedStatic;
|
|
||||||
import org.mockito.stubbing.Answer;
|
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
|
||||||
import org.springframework.context.annotation.Import;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
|
||||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.DATA_SOURCE_CONFIG_NOT_EXISTS;
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
|
||||||
import static org.mockito.Mockito.mockStatic;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link DataSourceConfigServiceImpl} 的单元测试类
|
|
||||||
*
|
|
||||||
|
|
||||||
*/
|
|
||||||
@Import(DataSourceConfigServiceImpl.class)
|
|
||||||
public class DataSourceConfigServiceImplTest extends BaseDbUnitTest {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private DataSourceConfigServiceImpl dataSourceConfigService;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private DataSourceConfigMapper dataSourceConfigMapper;
|
|
||||||
|
|
||||||
@MockBean
|
|
||||||
private AES aes;
|
|
||||||
|
|
||||||
@MockBean
|
|
||||||
private DynamicDataSourceProperties dynamicDataSourceProperties;
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
public void setUp() {
|
|
||||||
// mock 一个空实现的 StringEncryptor,避免 EncryptTypeHandler 报错
|
|
||||||
ReflectUtil.setFieldValue(EncryptTypeHandler.class, "aes", aes);
|
|
||||||
when(aes.encryptBase64(anyString())).then((Answer<String>) invocation -> invocation.getArgument(0));
|
|
||||||
when(aes.decryptStr(anyString())).then((Answer<String>) invocation -> invocation.getArgument(0));
|
|
||||||
|
|
||||||
// mock DynamicDataSourceProperties
|
|
||||||
when(dynamicDataSourceProperties.getPrimary()).thenReturn("primary");
|
|
||||||
when(dynamicDataSourceProperties.getDatasource()).thenReturn(MapUtil.of("primary",
|
|
||||||
new DataSourceProperty().setUrl("http://localhost:3306").setUsername("yunai").setPassword("tudou")));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCreateDataSourceConfig_success() {
|
|
||||||
try (MockedStatic<JdbcUtils> databaseUtilsMock = mockStatic(JdbcUtils.class)) {
|
|
||||||
// 准备参数
|
|
||||||
DataSourceConfigSaveReqVO reqVO = randomPojo(DataSourceConfigSaveReqVO.class)
|
|
||||||
.setId(null); // 避免 id 被设置
|
|
||||||
// mock 方法
|
|
||||||
databaseUtilsMock.when(() -> JdbcUtils.isConnectionOK(eq(reqVO.getUrl()),
|
|
||||||
eq(reqVO.getUsername()), eq(reqVO.getPassword()))).thenReturn(true);
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
Long dataSourceConfigId = dataSourceConfigService.createDataSourceConfig(reqVO);
|
|
||||||
// 断言
|
|
||||||
assertNotNull(dataSourceConfigId);
|
|
||||||
// 校验记录的属性是否正确
|
|
||||||
DataSourceConfigDO dataSourceConfig = dataSourceConfigMapper.selectById(dataSourceConfigId);
|
|
||||||
assertPojoEquals(reqVO, dataSourceConfig, "id");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUpdateDataSourceConfig_success() {
|
|
||||||
try (MockedStatic<JdbcUtils> databaseUtilsMock = mockStatic(JdbcUtils.class)) {
|
|
||||||
// mock 数据
|
|
||||||
DataSourceConfigDO dbDataSourceConfig = randomPojo(DataSourceConfigDO.class);
|
|
||||||
dataSourceConfigMapper.insert(dbDataSourceConfig);// @Sql: 先插入出一条存在的数据
|
|
||||||
// 准备参数
|
|
||||||
DataSourceConfigSaveReqVO reqVO = randomPojo(DataSourceConfigSaveReqVO.class, o -> {
|
|
||||||
o.setId(dbDataSourceConfig.getId()); // 设置更新的 ID
|
|
||||||
});
|
|
||||||
// mock 方法
|
|
||||||
databaseUtilsMock.when(() -> JdbcUtils.isConnectionOK(eq(reqVO.getUrl()),
|
|
||||||
eq(reqVO.getUsername()), eq(reqVO.getPassword()))).thenReturn(true);
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
dataSourceConfigService.updateDataSourceConfig(reqVO);
|
|
||||||
// 校验是否更新正确
|
|
||||||
DataSourceConfigDO dataSourceConfig = dataSourceConfigMapper.selectById(reqVO.getId()); // 获取最新的
|
|
||||||
assertPojoEquals(reqVO, dataSourceConfig);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUpdateDataSourceConfig_notExists() {
|
|
||||||
// 准备参数
|
|
||||||
DataSourceConfigSaveReqVO reqVO = randomPojo(DataSourceConfigSaveReqVO.class);
|
|
||||||
|
|
||||||
// 调用, 并断言异常
|
|
||||||
assertServiceException(() -> dataSourceConfigService.updateDataSourceConfig(reqVO), DATA_SOURCE_CONFIG_NOT_EXISTS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDeleteDataSourceConfig_success() {
|
|
||||||
// mock 数据
|
|
||||||
DataSourceConfigDO dbDataSourceConfig = randomPojo(DataSourceConfigDO.class);
|
|
||||||
dataSourceConfigMapper.insert(dbDataSourceConfig);// @Sql: 先插入出一条存在的数据
|
|
||||||
// 准备参数
|
|
||||||
Long id = dbDataSourceConfig.getId();
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
dataSourceConfigService.deleteDataSourceConfig(id);
|
|
||||||
// 校验数据不存在了
|
|
||||||
assertNull(dataSourceConfigMapper.selectById(id));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDeleteDataSourceConfig_notExists() {
|
|
||||||
// 准备参数
|
|
||||||
Long id = randomLongId();
|
|
||||||
|
|
||||||
// 调用, 并断言异常
|
|
||||||
assertServiceException(() -> dataSourceConfigService.deleteDataSourceConfig(id), DATA_SOURCE_CONFIG_NOT_EXISTS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test // 测试使用 password 查询,可以查询到数据
|
|
||||||
public void testSelectPassword() {
|
|
||||||
// mock 数据
|
|
||||||
DataSourceConfigDO dbDataSourceConfig = randomPojo(DataSourceConfigDO.class);
|
|
||||||
dataSourceConfigMapper.insert(dbDataSourceConfig);// @Sql: 先插入出一条存在的数据
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
DataSourceConfigDO result = dataSourceConfigMapper.selectOne(DataSourceConfigDO::getPassword,
|
|
||||||
EncryptTypeHandler.encrypt(dbDataSourceConfig.getPassword()));
|
|
||||||
assertPojoEquals(dbDataSourceConfig, result);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetDataSourceConfig_master() {
|
|
||||||
// 准备参数
|
|
||||||
Long id = 0L;
|
|
||||||
// mock 方法
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
DataSourceConfigDO dataSourceConfig = dataSourceConfigService.getDataSourceConfig(id);
|
|
||||||
// 断言
|
|
||||||
assertEquals(id, dataSourceConfig.getId());
|
|
||||||
assertEquals("primary", dataSourceConfig.getName());
|
|
||||||
assertEquals("http://localhost:3306", dataSourceConfig.getUrl());
|
|
||||||
assertEquals("yunai", dataSourceConfig.getUsername());
|
|
||||||
assertEquals("tudou", dataSourceConfig.getPassword());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetDataSourceConfig_normal() {
|
|
||||||
// mock 数据
|
|
||||||
DataSourceConfigDO dbDataSourceConfig = randomPojo(DataSourceConfigDO.class);
|
|
||||||
dataSourceConfigMapper.insert(dbDataSourceConfig);// @Sql: 先插入出一条存在的数据
|
|
||||||
// 准备参数
|
|
||||||
Long id = dbDataSourceConfig.getId();
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
DataSourceConfigDO dataSourceConfig = dataSourceConfigService.getDataSourceConfig(id);
|
|
||||||
// 断言
|
|
||||||
assertPojoEquals(dbDataSourceConfig, dataSourceConfig);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetDataSourceConfigList() {
|
|
||||||
// mock 数据
|
|
||||||
DataSourceConfigDO dbDataSourceConfig = randomPojo(DataSourceConfigDO.class);
|
|
||||||
dataSourceConfigMapper.insert(dbDataSourceConfig);// @Sql: 先插入出一条存在的数据
|
|
||||||
// 准备参数
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
List<DataSourceConfigDO> dataSourceConfigList = dataSourceConfigService.getDataSourceConfigList();
|
|
||||||
// 断言
|
|
||||||
assertEquals(2, dataSourceConfigList.size());
|
|
||||||
// master
|
|
||||||
assertEquals(0L, dataSourceConfigList.get(0).getId());
|
|
||||||
assertEquals("primary", dataSourceConfigList.get(0).getName());
|
|
||||||
assertEquals("http://localhost:3306", dataSourceConfigList.get(0).getUrl());
|
|
||||||
assertEquals("yunai", dataSourceConfigList.get(0).getUsername());
|
|
||||||
assertEquals("tudou", dataSourceConfigList.get(0).getPassword());
|
|
||||||
// normal
|
|
||||||
assertPojoEquals(dbDataSourceConfig, dataSourceConfigList.get(1));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,91 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.wms.service.db;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.dataobject.db.DataSourceConfigDO;
|
|
||||||
import cn.iocoder.yudao.module.wms.service.db.DataSourceConfigService;
|
|
||||||
import cn.iocoder.yudao.module.wms.service.db.DatabaseTableServiceImpl;
|
|
||||||
import com.baomidou.mybatisplus.generator.config.po.TableField;
|
|
||||||
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
|
|
||||||
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
|
|
||||||
import org.apache.ibatis.type.JdbcType;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
|
||||||
import org.springframework.context.annotation.Import;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
@Import(DatabaseTableServiceImpl.class)
|
|
||||||
public class DatabaseTableServiceImplTest extends BaseDbUnitTest {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private DatabaseTableServiceImpl databaseTableService;
|
|
||||||
|
|
||||||
@MockBean
|
|
||||||
private DataSourceConfigService dataSourceConfigService;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetTableList() {
|
|
||||||
// 准备参数
|
|
||||||
Long dataSourceConfigId = randomLongId();
|
|
||||||
// mock 方法
|
|
||||||
DataSourceConfigDO dataSourceConfig = new DataSourceConfigDO().setUsername("sa").setPassword("")
|
|
||||||
.setUrl("jdbc:h2:mem:testdb");
|
|
||||||
when(dataSourceConfigService.getDataSourceConfig(eq(dataSourceConfigId)))
|
|
||||||
.thenReturn(dataSourceConfig);
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
List<TableInfo> tables = databaseTableService.getTableList(dataSourceConfigId,
|
|
||||||
"config", "参数");
|
|
||||||
// 断言
|
|
||||||
assertEquals(1, tables.size());
|
|
||||||
assertTableInfo(tables.get(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetTable() {
|
|
||||||
// 准备参数
|
|
||||||
Long dataSourceConfigId = randomLongId();
|
|
||||||
// mock 方法
|
|
||||||
DataSourceConfigDO dataSourceConfig = new DataSourceConfigDO().setUsername("sa").setPassword("")
|
|
||||||
.setUrl("jdbc:h2:mem:testdb");
|
|
||||||
when(dataSourceConfigService.getDataSourceConfig(eq(dataSourceConfigId)))
|
|
||||||
.thenReturn(dataSourceConfig);
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
TableInfo tableInfo = databaseTableService.getTable(dataSourceConfigId, "infra_config");
|
|
||||||
// 断言
|
|
||||||
assertTableInfo(tableInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void assertTableInfo(TableInfo tableInfo) {
|
|
||||||
assertEquals("infra_config", tableInfo.getName());
|
|
||||||
assertEquals("参数配置表", tableInfo.getComment());
|
|
||||||
assertEquals(13, tableInfo.getFields().size());
|
|
||||||
// id 字段
|
|
||||||
TableField idField = tableInfo.getFields().get(0);
|
|
||||||
assertEquals("id", idField.getName());
|
|
||||||
assertEquals(JdbcType.BIGINT, idField.getMetaInfo().getJdbcType());
|
|
||||||
assertEquals("编号", idField.getComment());
|
|
||||||
assertFalse(idField.getMetaInfo().isNullable());
|
|
||||||
assertTrue(idField.isKeyFlag());
|
|
||||||
assertTrue(idField.isKeyIdentityFlag());
|
|
||||||
assertEquals(DbColumnType.LONG, idField.getColumnType());
|
|
||||||
assertEquals("id", idField.getPropertyName());
|
|
||||||
// name 字段
|
|
||||||
TableField nameField = tableInfo.getFields().get(3);
|
|
||||||
assertEquals("name", nameField.getName());
|
|
||||||
assertEquals(JdbcType.VARCHAR, nameField.getMetaInfo().getJdbcType());
|
|
||||||
assertEquals("名字", nameField.getComment());
|
|
||||||
assertFalse(nameField.getMetaInfo().isNullable());
|
|
||||||
assertFalse(nameField.isKeyFlag());
|
|
||||||
assertFalse(nameField.isKeyIdentityFlag());
|
|
||||||
assertEquals(DbColumnType.STRING, nameField.getColumnType());
|
|
||||||
assertEquals("name", nameField.getPropertyName());
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,282 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.wms.service.file;
|
|
||||||
|
|
||||||
import cn.hutool.core.date.DatePattern;
|
|
||||||
import cn.hutool.core.date.LocalDateTimeUtil;
|
|
||||||
import cn.hutool.core.map.MapUtil;
|
|
||||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
||||||
import cn.iocoder.yudao.framework.file.core.client.FileClient;
|
|
||||||
import cn.iocoder.yudao.framework.file.core.client.FileClientConfig;
|
|
||||||
import cn.iocoder.yudao.framework.file.core.client.FileClientFactory;
|
|
||||||
import cn.iocoder.yudao.framework.file.core.client.local.LocalFileClient;
|
|
||||||
import cn.iocoder.yudao.framework.file.core.client.local.LocalFileClientConfig;
|
|
||||||
import cn.iocoder.yudao.framework.file.core.enums.FileStorageEnum;
|
|
||||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
|
||||||
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.config.FileConfigPageReqVO;
|
|
||||||
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.config.FileConfigSaveReqVO;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.dataobject.file.FileConfigDO;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.mysql.file.FileConfigMapper;
|
|
||||||
import cn.iocoder.yudao.module.wms.service.file.FileConfigServiceImpl;
|
|
||||||
import lombok.Data;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
|
||||||
import org.springframework.context.annotation.Import;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import javax.validation.Validator;
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
|
||||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
|
|
||||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
|
||||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_CONFIG_DELETE_FAIL_MASTER;
|
|
||||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_CONFIG_NOT_EXISTS;
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
|
||||||
import static org.mockito.Mockito.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@link FileConfigServiceImpl} 的单元测试类
|
|
||||||
*
|
|
||||||
|
|
||||||
*/
|
|
||||||
@Import(FileConfigServiceImpl.class)
|
|
||||||
public class FileConfigServiceImplTest extends BaseDbUnitTest {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private FileConfigServiceImpl fileConfigService;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private FileConfigMapper fileConfigMapper;
|
|
||||||
|
|
||||||
@MockBean
|
|
||||||
private Validator validator;
|
|
||||||
@MockBean
|
|
||||||
private FileClientFactory fileClientFactory;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCreateFileConfig_success() {
|
|
||||||
// 准备参数
|
|
||||||
Map<String, Object> config = MapUtil.<String, Object>builder().put("basePath", "/yunai")
|
|
||||||
.put("domain", "https://www.iocoder.cn").build();
|
|
||||||
FileConfigSaveReqVO reqVO = randomPojo(FileConfigSaveReqVO.class,
|
|
||||||
o -> o.setStorage(FileStorageEnum.LOCAL.getStorage()).setConfig(config))
|
|
||||||
.setId(null); // 避免 id 被赋值
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
Long fileConfigId = fileConfigService.createFileConfig(reqVO);
|
|
||||||
// 断言
|
|
||||||
assertNotNull(fileConfigId);
|
|
||||||
// 校验记录的属性是否正确
|
|
||||||
FileConfigDO fileConfig = fileConfigMapper.selectById(fileConfigId);
|
|
||||||
assertPojoEquals(reqVO, fileConfig, "id", "config");
|
|
||||||
assertFalse(fileConfig.getMaster());
|
|
||||||
assertEquals("/yunai", ((LocalFileClientConfig) fileConfig.getConfig()).getBasePath());
|
|
||||||
assertEquals("https://www.iocoder.cn", ((LocalFileClientConfig) fileConfig.getConfig()).getDomain());
|
|
||||||
// 验证 cache
|
|
||||||
assertNull(fileConfigService.getClientCache().getIfPresent(fileConfigId));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUpdateFileConfig_success() {
|
|
||||||
// mock 数据
|
|
||||||
FileConfigDO dbFileConfig = randomPojo(FileConfigDO.class, o -> o.setStorage(FileStorageEnum.LOCAL.getStorage())
|
|
||||||
.setConfig(new LocalFileClientConfig().setBasePath("/yunai").setDomain("https://www.iocoder.cn")));
|
|
||||||
fileConfigMapper.insert(dbFileConfig);// @Sql: 先插入出一条存在的数据
|
|
||||||
// 准备参数
|
|
||||||
FileConfigSaveReqVO reqVO = randomPojo(FileConfigSaveReqVO.class, o -> {
|
|
||||||
o.setId(dbFileConfig.getId()); // 设置更新的 ID
|
|
||||||
o.setStorage(FileStorageEnum.LOCAL.getStorage());
|
|
||||||
Map<String, Object> config = MapUtil.<String, Object>builder().put("basePath", "/yunai2")
|
|
||||||
.put("domain", "https://doc.iocoder.cn").build();
|
|
||||||
o.setConfig(config);
|
|
||||||
});
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
fileConfigService.updateFileConfig(reqVO);
|
|
||||||
// 校验是否更新正确
|
|
||||||
FileConfigDO fileConfig = fileConfigMapper.selectById(reqVO.getId()); // 获取最新的
|
|
||||||
assertPojoEquals(reqVO, fileConfig, "config");
|
|
||||||
assertEquals("/yunai2", ((LocalFileClientConfig) fileConfig.getConfig()).getBasePath());
|
|
||||||
assertEquals("https://doc.iocoder.cn", ((LocalFileClientConfig) fileConfig.getConfig()).getDomain());
|
|
||||||
// 验证 cache
|
|
||||||
assertNull(fileConfigService.getClientCache().getIfPresent(fileConfig.getId()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUpdateFileConfig_notExists() {
|
|
||||||
// 准备参数
|
|
||||||
FileConfigSaveReqVO reqVO = randomPojo(FileConfigSaveReqVO.class);
|
|
||||||
|
|
||||||
// 调用, 并断言异常
|
|
||||||
assertServiceException(() -> fileConfigService.updateFileConfig(reqVO), FILE_CONFIG_NOT_EXISTS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUpdateFileConfigMaster_success() {
|
|
||||||
// mock 数据
|
|
||||||
FileConfigDO dbFileConfig = randomFileConfigDO().setMaster(false);
|
|
||||||
fileConfigMapper.insert(dbFileConfig);// @Sql: 先插入出一条存在的数据
|
|
||||||
FileConfigDO masterFileConfig = randomFileConfigDO().setMaster(true);
|
|
||||||
fileConfigMapper.insert(masterFileConfig);// @Sql: 先插入出一条存在的数据
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
fileConfigService.updateFileConfigMaster(dbFileConfig.getId());
|
|
||||||
// 断言数据
|
|
||||||
assertTrue(fileConfigMapper.selectById(dbFileConfig.getId()).getMaster());
|
|
||||||
assertFalse(fileConfigMapper.selectById(masterFileConfig.getId()).getMaster());
|
|
||||||
// 验证 cache
|
|
||||||
assertNull(fileConfigService.getClientCache().getIfPresent(0L));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUpdateFileConfigMaster_notExists() {
|
|
||||||
// 调用, 并断言异常
|
|
||||||
assertServiceException(() -> fileConfigService.updateFileConfigMaster(randomLongId()), FILE_CONFIG_NOT_EXISTS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDeleteFileConfig_success() {
|
|
||||||
// mock 数据
|
|
||||||
FileConfigDO dbFileConfig = randomFileConfigDO().setMaster(false);
|
|
||||||
fileConfigMapper.insert(dbFileConfig);// @Sql: 先插入出一条存在的数据
|
|
||||||
// 准备参数
|
|
||||||
Long id = dbFileConfig.getId();
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
fileConfigService.deleteFileConfig(id);
|
|
||||||
// 校验数据不存在了
|
|
||||||
assertNull(fileConfigMapper.selectById(id));
|
|
||||||
// 验证 cache
|
|
||||||
assertNull(fileConfigService.getClientCache().getIfPresent(id));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDeleteFileConfig_notExists() {
|
|
||||||
// 准备参数
|
|
||||||
Long id = randomLongId();
|
|
||||||
|
|
||||||
// 调用, 并断言异常
|
|
||||||
assertServiceException(() -> fileConfigService.deleteFileConfig(id), FILE_CONFIG_NOT_EXISTS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDeleteFileConfig_master() {
|
|
||||||
// mock 数据
|
|
||||||
FileConfigDO dbFileConfig = randomFileConfigDO().setMaster(true);
|
|
||||||
fileConfigMapper.insert(dbFileConfig);// @Sql: 先插入出一条存在的数据
|
|
||||||
// 准备参数
|
|
||||||
Long id = dbFileConfig.getId();
|
|
||||||
|
|
||||||
// 调用, 并断言异常
|
|
||||||
assertServiceException(() -> fileConfigService.deleteFileConfig(id), FILE_CONFIG_DELETE_FAIL_MASTER);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetFileConfigPage() {
|
|
||||||
// mock 数据
|
|
||||||
FileConfigDO dbFileConfig = randomFileConfigDO().setName("芋道源码")
|
|
||||||
.setStorage(FileStorageEnum.LOCAL.getStorage());
|
|
||||||
dbFileConfig.setCreateTime(LocalDateTimeUtil.parse("2020-01-23", DatePattern.NORM_DATE_PATTERN));// 等会查询到
|
|
||||||
fileConfigMapper.insert(dbFileConfig);
|
|
||||||
// 测试 name 不匹配
|
|
||||||
fileConfigMapper.insert(cloneIgnoreId(dbFileConfig, o -> o.setName("源码")));
|
|
||||||
// 测试 storage 不匹配
|
|
||||||
fileConfigMapper.insert(cloneIgnoreId(dbFileConfig, o -> o.setStorage(FileStorageEnum.DB.getStorage())));
|
|
||||||
// 测试 createTime 不匹配
|
|
||||||
fileConfigMapper.insert(cloneIgnoreId(dbFileConfig, o -> o.setCreateTime(LocalDateTimeUtil.parse("2020-11-23", DatePattern.NORM_DATE_PATTERN))));
|
|
||||||
// 准备参数
|
|
||||||
FileConfigPageReqVO reqVO = new FileConfigPageReqVO();
|
|
||||||
reqVO.setName("芋道");
|
|
||||||
reqVO.setStorage(FileStorageEnum.LOCAL.getStorage());
|
|
||||||
reqVO.setCreateTime((new LocalDateTime[]{buildTime(2020, 1, 1),
|
|
||||||
buildTime(2020, 1, 24)}));
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
PageResult<FileConfigDO> pageResult = fileConfigService.getFileConfigPage(reqVO);
|
|
||||||
// 断言
|
|
||||||
assertEquals(1, pageResult.getTotal());
|
|
||||||
assertEquals(1, pageResult.getList().size());
|
|
||||||
assertPojoEquals(dbFileConfig, pageResult.getList().get(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testFileConfig() throws Exception {
|
|
||||||
// mock 数据
|
|
||||||
FileConfigDO dbFileConfig = randomFileConfigDO().setMaster(false);
|
|
||||||
fileConfigMapper.insert(dbFileConfig);// @Sql: 先插入出一条存在的数据
|
|
||||||
// 准备参数
|
|
||||||
Long id = dbFileConfig.getId();
|
|
||||||
// mock 获得 Client
|
|
||||||
FileClient fileClient = mock(FileClient.class);
|
|
||||||
when(fileClientFactory.getFileClient(eq(id))).thenReturn(fileClient);
|
|
||||||
when(fileClient.upload(any(), any(), any())).thenReturn("https://www.iocoder.cn");
|
|
||||||
|
|
||||||
// 调用,并断言
|
|
||||||
assertEquals("https://www.iocoder.cn", fileConfigService.testFileConfig(id));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetFileConfig() {
|
|
||||||
// mock 数据
|
|
||||||
FileConfigDO dbFileConfig = randomFileConfigDO().setMaster(false);
|
|
||||||
fileConfigMapper.insert(dbFileConfig);// @Sql: 先插入出一条存在的数据
|
|
||||||
// 准备参数
|
|
||||||
Long id = dbFileConfig.getId();
|
|
||||||
|
|
||||||
// 调用,并断言
|
|
||||||
assertPojoEquals(dbFileConfig, fileConfigService.getFileConfig(id));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetFileClient() {
|
|
||||||
// mock 数据
|
|
||||||
FileConfigDO fileConfig = randomFileConfigDO().setMaster(false);
|
|
||||||
fileConfigMapper.insert(fileConfig);
|
|
||||||
// 准备参数
|
|
||||||
Long id = fileConfig.getId();
|
|
||||||
// mock 获得 Client
|
|
||||||
FileClient fileClient = new LocalFileClient(id, new LocalFileClientConfig());
|
|
||||||
when(fileClientFactory.getFileClient(eq(id))).thenReturn(fileClient);
|
|
||||||
|
|
||||||
// 调用,并断言
|
|
||||||
assertSame(fileClient, fileConfigService.getFileClient(id));
|
|
||||||
// 断言缓存
|
|
||||||
verify(fileClientFactory).createOrUpdateFileClient(eq(id), eq(fileConfig.getStorage()),
|
|
||||||
eq(fileConfig.getConfig()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetMasterFileClient() {
|
|
||||||
// mock 数据
|
|
||||||
FileConfigDO fileConfig = randomFileConfigDO().setMaster(true);
|
|
||||||
fileConfigMapper.insert(fileConfig);
|
|
||||||
// 准备参数
|
|
||||||
Long id = fileConfig.getId();
|
|
||||||
// mock 获得 Client
|
|
||||||
FileClient fileClient = new LocalFileClient(id, new LocalFileClientConfig());
|
|
||||||
when(fileClientFactory.getFileClient(eq(fileConfig.getId()))).thenReturn(fileClient);
|
|
||||||
|
|
||||||
// 调用,并断言
|
|
||||||
assertSame(fileClient, fileConfigService.getMasterFileClient());
|
|
||||||
// 断言缓存
|
|
||||||
verify(fileClientFactory).createOrUpdateFileClient(eq(fileConfig.getId()), eq(fileConfig.getStorage()),
|
|
||||||
eq(fileConfig.getConfig()));
|
|
||||||
}
|
|
||||||
|
|
||||||
private FileConfigDO randomFileConfigDO() {
|
|
||||||
return randomPojo(FileConfigDO.class).setStorage(randomEle(FileStorageEnum.values()).getStorage())
|
|
||||||
.setConfig(new EmptyFileClientConfig());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Data
|
|
||||||
public static class EmptyFileClientConfig implements FileClientConfig, Serializable {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,146 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.wms.service.file;
|
|
||||||
|
|
||||||
import cn.hutool.core.io.resource.ResourceUtil;
|
|
||||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
||||||
import cn.iocoder.yudao.framework.common.util.object.ObjectUtils;
|
|
||||||
import cn.iocoder.yudao.framework.file.core.client.FileClient;
|
|
||||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
|
||||||
import cn.iocoder.yudao.framework.test.core.util.AssertUtils;
|
|
||||||
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.dataobject.file.FileDO;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.mysql.file.FileMapper;
|
|
||||||
import cn.iocoder.yudao.module.wms.service.file.FileConfigService;
|
|
||||||
import cn.iocoder.yudao.module.wms.service.file.FileService;
|
|
||||||
import cn.iocoder.yudao.module.wms.service.file.FileServiceImpl;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
|
||||||
import org.springframework.context.annotation.Import;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
|
||||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_NOT_EXISTS;
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
import static org.mockito.ArgumentMatchers.same;
|
|
||||||
import static org.mockito.Mockito.*;
|
|
||||||
|
|
||||||
@Import({FileServiceImpl.class})
|
|
||||||
public class FileServiceImplTest extends BaseDbUnitTest {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private FileService fileService;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private FileMapper fileMapper;
|
|
||||||
|
|
||||||
@MockBean
|
|
||||||
private FileConfigService fileConfigService;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetFilePage() {
|
|
||||||
// mock 数据
|
|
||||||
FileDO dbFile = randomPojo(FileDO.class, o -> { // 等会查询到
|
|
||||||
o.setPath("yunai");
|
|
||||||
o.setType("image/jpg");
|
|
||||||
o.setCreateTime(buildTime(2021, 1, 15));
|
|
||||||
});
|
|
||||||
fileMapper.insert(dbFile);
|
|
||||||
// 测试 path 不匹配
|
|
||||||
fileMapper.insert(ObjectUtils.cloneIgnoreId(dbFile, o -> o.setPath("tudou")));
|
|
||||||
// 测试 type 不匹配
|
|
||||||
fileMapper.insert(ObjectUtils.cloneIgnoreId(dbFile, o -> {
|
|
||||||
o.setType("image/png");
|
|
||||||
}));
|
|
||||||
// 测试 createTime 不匹配
|
|
||||||
fileMapper.insert(ObjectUtils.cloneIgnoreId(dbFile, o -> {
|
|
||||||
o.setCreateTime(buildTime(2020, 1, 15));
|
|
||||||
}));
|
|
||||||
// 准备参数
|
|
||||||
FilePageReqVO reqVO = new FilePageReqVO();
|
|
||||||
reqVO.setPath("yunai");
|
|
||||||
reqVO.setType("jp");
|
|
||||||
reqVO.setCreateTime((new LocalDateTime[]{buildTime(2021, 1, 10), buildTime(2021, 1, 20)}));
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
PageResult<FileDO> pageResult = fileService.getFilePage(reqVO);
|
|
||||||
// 断言
|
|
||||||
assertEquals(1, pageResult.getTotal());
|
|
||||||
assertEquals(1, pageResult.getList().size());
|
|
||||||
AssertUtils.assertPojoEquals(dbFile, pageResult.getList().get(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCreateFile_success() throws Exception {
|
|
||||||
// 准备参数
|
|
||||||
String path = randomString();
|
|
||||||
byte[] content = ResourceUtil.readBytes("file/erweima.jpg");
|
|
||||||
// mock Master 文件客户端
|
|
||||||
FileClient client = mock(FileClient.class);
|
|
||||||
when(fileConfigService.getMasterFileClient()).thenReturn(client);
|
|
||||||
String url = randomString();
|
|
||||||
when(client.upload(same(content), same(path), eq("image/jpeg"))).thenReturn(url);
|
|
||||||
when(client.getId()).thenReturn(10L);
|
|
||||||
String name = "单测文件名";
|
|
||||||
// 调用
|
|
||||||
String result = fileService.createFile(name, path, content);
|
|
||||||
// 断言
|
|
||||||
assertEquals(result, url);
|
|
||||||
// 校验数据
|
|
||||||
FileDO file = fileMapper.selectOne(FileDO::getPath, path);
|
|
||||||
assertEquals(10L, file.getConfigId());
|
|
||||||
assertEquals(path, file.getPath());
|
|
||||||
assertEquals(url, file.getUrl());
|
|
||||||
assertEquals("image/jpeg", file.getType());
|
|
||||||
assertEquals(content.length, file.getSize());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDeleteFile_success() throws Exception {
|
|
||||||
// mock 数据
|
|
||||||
FileDO dbFile = randomPojo(FileDO.class, o -> o.setConfigId(10L).setPath("tudou.jpg"));
|
|
||||||
fileMapper.insert(dbFile);// @Sql: 先插入出一条存在的数据
|
|
||||||
// mock Master 文件客户端
|
|
||||||
FileClient client = mock(FileClient.class);
|
|
||||||
when(fileConfigService.getFileClient(eq(10L))).thenReturn(client);
|
|
||||||
// 准备参数
|
|
||||||
Long id = dbFile.getId();
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
fileService.deleteFile(id);
|
|
||||||
// 校验数据不存在了
|
|
||||||
assertNull(fileMapper.selectById(id));
|
|
||||||
// 校验调用
|
|
||||||
verify(client).delete(eq("tudou.jpg"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testDeleteFile_notExists() {
|
|
||||||
// 准备参数
|
|
||||||
Long id = randomLongId();
|
|
||||||
|
|
||||||
// 调用, 并断言异常
|
|
||||||
assertServiceException(() -> fileService.deleteFile(id), FILE_NOT_EXISTS);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetFileContent() throws Exception {
|
|
||||||
// 准备参数
|
|
||||||
Long configId = 10L;
|
|
||||||
String path = "tudou.jpg";
|
|
||||||
// mock 方法
|
|
||||||
FileClient client = mock(FileClient.class);
|
|
||||||
when(fileConfigService.getFileClient(eq(10L))).thenReturn(client);
|
|
||||||
byte[] content = new byte[]{};
|
|
||||||
when(client.getContent(eq("tudou.jpg"))).thenReturn(content);
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
byte[] result = fileService.getFileContent(configId, path);
|
|
||||||
// 断言
|
|
||||||
assertSame(result, content);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,110 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.wms.service.logger;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
|
||||||
import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
|
|
||||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
||||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
|
||||||
import cn.iocoder.yudao.module.infra.api.logger.dto.ApiAccessLogCreateReqDTO;
|
|
||||||
import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogPageReqVO;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.dataobject.logger.ApiAccessLogDO;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.mysql.logger.ApiAccessLogMapper;
|
|
||||||
import cn.iocoder.yudao.module.wms.service.logger.ApiAccessLogServiceImpl;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.context.annotation.Import;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import java.time.Duration;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.*;
|
|
||||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
|
|
||||||
@Import(ApiAccessLogServiceImpl.class)
|
|
||||||
public class ApiAccessLogServiceImplTest extends BaseDbUnitTest {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private ApiAccessLogServiceImpl apiAccessLogService;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private ApiAccessLogMapper apiAccessLogMapper;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetApiAccessLogPage() {
|
|
||||||
ApiAccessLogDO apiAccessLogDO = randomPojo(ApiAccessLogDO.class, o -> {
|
|
||||||
o.setUserId(2233L);
|
|
||||||
o.setUserType(UserTypeEnum.ADMIN.getValue());
|
|
||||||
o.setApplicationName("yudao-test");
|
|
||||||
o.setRequestUrl("foo");
|
|
||||||
o.setBeginTime(buildTime(2021, 3, 13));
|
|
||||||
o.setDuration(1000);
|
|
||||||
o.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
|
|
||||||
});
|
|
||||||
apiAccessLogMapper.insert(apiAccessLogDO);
|
|
||||||
// 测试 userId 不匹配
|
|
||||||
apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setUserId(3344L)));
|
|
||||||
// 测试 userType 不匹配
|
|
||||||
apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setUserType(UserTypeEnum.MEMBER.getValue())));
|
|
||||||
// 测试 applicationName 不匹配
|
|
||||||
apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setApplicationName("test")));
|
|
||||||
// 测试 requestUrl 不匹配
|
|
||||||
apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setRequestUrl("bar")));
|
|
||||||
// 测试 beginTime 不匹配:构造一个早期时间 2021-02-06 00:00:00
|
|
||||||
apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setBeginTime(buildTime(2021, 2, 6))));
|
|
||||||
// 测试 duration 不匹配
|
|
||||||
apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setDuration(100)));
|
|
||||||
// 测试 resultCode 不匹配
|
|
||||||
apiAccessLogMapper.insert(cloneIgnoreId(apiAccessLogDO, o -> o.setResultCode(2)));
|
|
||||||
// 准备参数
|
|
||||||
ApiAccessLogPageReqVO reqVO = new ApiAccessLogPageReqVO();
|
|
||||||
reqVO.setUserId(2233L);
|
|
||||||
reqVO.setUserType(UserTypeEnum.ADMIN.getValue());
|
|
||||||
reqVO.setApplicationName("yudao-test");
|
|
||||||
reqVO.setRequestUrl("foo");
|
|
||||||
reqVO.setBeginTime(buildBetweenTime(2021, 3, 13, 2021, 3, 13));
|
|
||||||
reqVO.setDuration(1000);
|
|
||||||
reqVO.setResultCode(GlobalErrorCodeConstants.SUCCESS.getCode());
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
PageResult<ApiAccessLogDO> pageResult = apiAccessLogService.getApiAccessLogPage(reqVO);
|
|
||||||
// 断言,只查到了一条符合条件的
|
|
||||||
assertEquals(1, pageResult.getTotal());
|
|
||||||
assertEquals(1, pageResult.getList().size());
|
|
||||||
assertPojoEquals(apiAccessLogDO, pageResult.getList().get(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCleanJobLog() {
|
|
||||||
// mock 数据
|
|
||||||
ApiAccessLogDO log01 = randomPojo(ApiAccessLogDO.class, o -> o.setCreateTime(addTime(Duration.ofDays(-3))));
|
|
||||||
apiAccessLogMapper.insert(log01);
|
|
||||||
ApiAccessLogDO log02 = randomPojo(ApiAccessLogDO.class, o -> o.setCreateTime(addTime(Duration.ofDays(-1))));
|
|
||||||
apiAccessLogMapper.insert(log02);
|
|
||||||
// 准备参数
|
|
||||||
Integer exceedDay = 2;
|
|
||||||
Integer deleteLimit = 1;
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
Integer count = apiAccessLogService.cleanAccessLog(exceedDay, deleteLimit);
|
|
||||||
// 断言
|
|
||||||
assertEquals(1, count);
|
|
||||||
List<ApiAccessLogDO> logs = apiAccessLogMapper.selectList();
|
|
||||||
assertEquals(1, logs.size());
|
|
||||||
assertEquals(log02, logs.get(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCreateApiAccessLog() {
|
|
||||||
// 准备参数
|
|
||||||
ApiAccessLogCreateReqDTO createDTO = randomPojo(ApiAccessLogCreateReqDTO.class);
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
apiAccessLogService.createApiAccessLog(createDTO);
|
|
||||||
// 断言
|
|
||||||
ApiAccessLogDO apiAccessLogDO = apiAccessLogMapper.selectOne(null);
|
|
||||||
assertPojoEquals(createDTO, apiAccessLogDO);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,164 +0,0 @@
|
|||||||
package cn.iocoder.yudao.module.wms.service.logger;
|
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
|
||||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
||||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
|
|
||||||
import cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO;
|
|
||||||
import cn.iocoder.yudao.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogPageReqVO;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.dataobject.logger.ApiErrorLogDO;
|
|
||||||
import cn.iocoder.yudao.module.wms.dal.mysql.logger.ApiErrorLogMapper;
|
|
||||||
import cn.iocoder.yudao.module.wms.enums.logger.ApiErrorLogProcessStatusEnum;
|
|
||||||
import cn.iocoder.yudao.module.wms.service.logger.ApiErrorLogServiceImpl;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import org.springframework.context.annotation.Import;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
import java.time.Duration;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static cn.hutool.core.util.RandomUtil.randomEle;
|
|
||||||
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.*;
|
|
||||||
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
|
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
|
|
||||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.API_ERROR_LOG_NOT_FOUND;
|
|
||||||
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.API_ERROR_LOG_PROCESSED;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
||||||
|
|
||||||
@Import(ApiErrorLogServiceImpl.class)
|
|
||||||
public class ApiErrorLogServiceImplTest extends BaseDbUnitTest {
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private ApiErrorLogServiceImpl apiErrorLogService;
|
|
||||||
|
|
||||||
@Resource
|
|
||||||
private ApiErrorLogMapper apiErrorLogMapper;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGetApiErrorLogPage() {
|
|
||||||
// mock 数据
|
|
||||||
ApiErrorLogDO apiErrorLogDO = randomPojo(ApiErrorLogDO.class, o -> {
|
|
||||||
o.setUserId(2233L);
|
|
||||||
o.setUserType(UserTypeEnum.ADMIN.getValue());
|
|
||||||
o.setApplicationName("yudao-test");
|
|
||||||
o.setRequestUrl("foo");
|
|
||||||
o.setExceptionTime(buildTime(2021, 3, 13));
|
|
||||||
o.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
|
|
||||||
});
|
|
||||||
apiErrorLogMapper.insert(apiErrorLogDO);
|
|
||||||
// 测试 userId 不匹配
|
|
||||||
apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, o -> o.setUserId(3344L)));
|
|
||||||
// 测试 userType 不匹配
|
|
||||||
apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue())));
|
|
||||||
// 测试 applicationName 不匹配
|
|
||||||
apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setApplicationName("test")));
|
|
||||||
// 测试 requestUrl 不匹配
|
|
||||||
apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setRequestUrl("bar")));
|
|
||||||
// 测试 exceptionTime 不匹配:构造一个早期时间 2021-02-06 00:00:00
|
|
||||||
apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6))));
|
|
||||||
// 测试 progressStatus 不匹配
|
|
||||||
apiErrorLogMapper.insert(cloneIgnoreId(apiErrorLogDO, logDO -> logDO.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus())));
|
|
||||||
// 准备参数
|
|
||||||
ApiErrorLogPageReqVO reqVO = new ApiErrorLogPageReqVO();
|
|
||||||
reqVO.setUserId(2233L);
|
|
||||||
reqVO.setUserType(UserTypeEnum.ADMIN.getValue());
|
|
||||||
reqVO.setApplicationName("yudao-test");
|
|
||||||
reqVO.setRequestUrl("foo");
|
|
||||||
reqVO.setExceptionTime(buildBetweenTime(2021, 3, 1, 2021, 3, 31));
|
|
||||||
reqVO.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus());
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
PageResult<ApiErrorLogDO> pageResult = apiErrorLogService.getApiErrorLogPage(reqVO);
|
|
||||||
// 断言,只查到了一条符合条件的
|
|
||||||
assertEquals(1, pageResult.getTotal());
|
|
||||||
assertEquals(1, pageResult.getList().size());
|
|
||||||
assertPojoEquals(apiErrorLogDO, pageResult.getList().get(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCreateApiErrorLog() {
|
|
||||||
// 准备参数
|
|
||||||
ApiErrorLogCreateReqDTO createDTO = randomPojo(ApiErrorLogCreateReqDTO.class);
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
apiErrorLogService.createApiErrorLog(createDTO);
|
|
||||||
// 断言
|
|
||||||
ApiErrorLogDO apiErrorLogDO = apiErrorLogMapper.selectOne(null);
|
|
||||||
assertPojoEquals(createDTO, apiErrorLogDO);
|
|
||||||
assertEquals(ApiErrorLogProcessStatusEnum.INIT.getStatus(), apiErrorLogDO.getProcessStatus());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUpdateApiErrorLogProcess_success() {
|
|
||||||
// 准备参数
|
|
||||||
ApiErrorLogDO apiErrorLogDO = randomPojo(ApiErrorLogDO.class,
|
|
||||||
o -> o.setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus()));
|
|
||||||
apiErrorLogMapper.insert(apiErrorLogDO);
|
|
||||||
// 准备参数
|
|
||||||
Long id = apiErrorLogDO.getId();
|
|
||||||
Integer processStatus = randomEle(ApiErrorLogProcessStatusEnum.values()).getStatus();
|
|
||||||
Long processUserId = randomLongId();
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
apiErrorLogService.updateApiErrorLogProcess(id, processStatus, processUserId);
|
|
||||||
// 断言
|
|
||||||
ApiErrorLogDO dbApiErrorLogDO = apiErrorLogMapper.selectById(apiErrorLogDO.getId());
|
|
||||||
assertEquals(processStatus, dbApiErrorLogDO.getProcessStatus());
|
|
||||||
assertEquals(processUserId, dbApiErrorLogDO.getProcessUserId());
|
|
||||||
assertNotNull(dbApiErrorLogDO.getProcessTime());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUpdateApiErrorLogProcess_processed() {
|
|
||||||
// 准备参数
|
|
||||||
ApiErrorLogDO apiErrorLogDO = randomPojo(ApiErrorLogDO.class,
|
|
||||||
o -> o.setProcessStatus(ApiErrorLogProcessStatusEnum.DONE.getStatus()));
|
|
||||||
apiErrorLogMapper.insert(apiErrorLogDO);
|
|
||||||
// 准备参数
|
|
||||||
Long id = apiErrorLogDO.getId();
|
|
||||||
Integer processStatus = randomEle(ApiErrorLogProcessStatusEnum.values()).getStatus();
|
|
||||||
Long processUserId = randomLongId();
|
|
||||||
|
|
||||||
// 调用,并断言异常
|
|
||||||
assertServiceException(() ->
|
|
||||||
apiErrorLogService.updateApiErrorLogProcess(id, processStatus, processUserId),
|
|
||||||
API_ERROR_LOG_PROCESSED);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUpdateApiErrorLogProcess_notFound() {
|
|
||||||
// 准备参数
|
|
||||||
Long id = randomLongId();
|
|
||||||
Integer processStatus = randomEle(ApiErrorLogProcessStatusEnum.values()).getStatus();
|
|
||||||
Long processUserId = randomLongId();
|
|
||||||
|
|
||||||
// 调用,并断言异常
|
|
||||||
assertServiceException(() ->
|
|
||||||
apiErrorLogService.updateApiErrorLogProcess(id, processStatus, processUserId),
|
|
||||||
API_ERROR_LOG_NOT_FOUND);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCleanJobLog() {
|
|
||||||
// mock 数据
|
|
||||||
ApiErrorLogDO log01 = randomPojo(ApiErrorLogDO.class, o -> o.setCreateTime(addTime(Duration.ofDays(-3))));
|
|
||||||
apiErrorLogMapper.insert(log01);
|
|
||||||
ApiErrorLogDO log02 = randomPojo(ApiErrorLogDO.class, o -> o.setCreateTime(addTime(Duration.ofDays(-1))));
|
|
||||||
apiErrorLogMapper.insert(log02);
|
|
||||||
// 准备参数
|
|
||||||
Integer exceedDay = 2;
|
|
||||||
Integer deleteLimit = 1;
|
|
||||||
|
|
||||||
// 调用
|
|
||||||
Integer count = apiErrorLogService.cleanErrorLog(exceedDay, deleteLimit);
|
|
||||||
// 断言
|
|
||||||
assertEquals(1, count);
|
|
||||||
List<ApiErrorLogDO> logs = apiErrorLogMapper.selectList();
|
|
||||||
assertEquals(1, logs.size());
|
|
||||||
assertEquals(log02, logs.get(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user