1、报表基础内容实现,service单元测试实现,移除report微服务中没用的单元测试内容;

2、DefaultDBFieldHandler自动填充处理类中request注入改为上下文获取;
This commit is contained in:
gaoqr
2024-07-08 15:48:17 +08:00
parent 4319d6d340
commit 03cce64688
37 changed files with 1817 additions and 203 deletions
@@ -1,58 +0,0 @@
package com.cf.imes.module.report.service.goview;
import com.cf.imes.framework.test.core.ut.BaseDbUnitTest;
import com.cf.imes.module.report.controller.admin.goview.vo.data.GoViewDataRespVO;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.jdbc.support.rowset.SqlRowSetMetaData;
import javax.annotation.Resource;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@Import(GoViewDataServiceImpl.class)
public class GoViewDataServiceImplTest extends BaseDbUnitTest {
@Resource
private GoViewDataServiceImpl goViewDataService;
@MockBean
private JdbcTemplate jdbcTemplate;
@Test
public void testGetDataBySQL() {
// 准备参数
String sql = "SELECT id, name FROM system_users";
// mock 方法
SqlRowSet sqlRowSet = mock(SqlRowSet.class);
when(jdbcTemplate.queryForRowSet(eq(sql))).thenReturn(sqlRowSet);
// mock 元数据
SqlRowSetMetaData metaData = mock(SqlRowSetMetaData.class);
when(sqlRowSet.getMetaData()).thenReturn(metaData);
when(metaData.getColumnNames()).thenReturn(new String[]{"id", "name"});
// mock 数据明细
when(sqlRowSet.next()).thenReturn(true).thenReturn(true).thenReturn(false);
when(sqlRowSet.getObject("id")).thenReturn(1L).thenReturn(2L);
when(sqlRowSet.getObject("name")).thenReturn("晨丰科技").thenReturn("晨丰");
// 调用
GoViewDataRespVO dataBySQL = goViewDataService.getDataBySQL(sql);
// 断言
assertEquals(Arrays.asList("id", "name"), dataBySQL.getDimensions());
assertEquals(2, dataBySQL.getDimensions().size());
assertEquals(2, dataBySQL.getSource().get(0).size());
assertEquals(1L, dataBySQL.getSource().get(0).get("id"));
assertEquals("晨丰科技", dataBySQL.getSource().get(0).get("name"));
assertEquals(2, dataBySQL.getSource().get(1).size());
assertEquals(2L, dataBySQL.getSource().get(1).get("id"));
assertEquals("晨丰", dataBySQL.getSource().get(1).get("name"));
}
}
@@ -1,135 +0,0 @@
package com.cf.imes.module.report.service.goview;
import com.cf.imes.framework.common.pojo.PageParam;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.test.core.ut.BaseDbUnitTest;
import com.cf.imes.module.report.controller.admin.goview.vo.project.GoViewProjectCreateReqVO;
import com.cf.imes.module.report.controller.admin.goview.vo.project.GoViewProjectUpdateReqVO;
import com.cf.imes.module.report.dal.dataobject.goview.GoViewProjectDO;
import com.cf.imes.module.report.dal.mysql.goview.GoViewProjectMapper;
import com.cf.imes.module.report.enums.ErrorCodeConstants;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import;
import javax.annotation.Resource;
import static com.cf.imes.framework.common.util.object.ObjectUtils.cloneIgnoreId;
import static com.cf.imes.framework.test.core.util.AssertUtils.assertPojoEquals;
import static com.cf.imes.framework.test.core.util.AssertUtils.assertServiceException;
import static com.cf.imes.framework.test.core.util.RandomUtils.*;
import static org.junit.jupiter.api.Assertions.*;
/**
* {@link GoViewProjectServiceImpl} 的单元测试类
*
* @author 晨丰科技
*/
@Import(GoViewProjectServiceImpl.class)
public class GoViewProjectServiceImplTest extends BaseDbUnitTest {
@Resource
private GoViewProjectServiceImpl goViewProjectService;
@Resource
private GoViewProjectMapper goViewProjectMapper;
@Test
public void testCreateProject_success() {
// 准备参数
GoViewProjectCreateReqVO reqVO = randomPojo(GoViewProjectCreateReqVO.class);
// 调用
Long goViewProjectId = goViewProjectService.createProject(reqVO);
// 断言
assertNotNull(goViewProjectId);
// 校验记录的属性是否正确
GoViewProjectDO goViewProject = goViewProjectMapper.selectById(goViewProjectId);
assertPojoEquals(reqVO, goViewProject);
}
@Test
public void testUpdateProject_success() {
// mock 数据
GoViewProjectDO dbGoViewProject = randomPojo(GoViewProjectDO.class);
goViewProjectMapper.insert(dbGoViewProject);// @Sql: 先插入出一条存在的数据
// 准备参数
GoViewProjectUpdateReqVO reqVO = randomPojo(GoViewProjectUpdateReqVO.class, o -> {
o.setId(dbGoViewProject.getId()); // 设置更新的 ID
o.setStatus(randomCommonStatus());
});
// 调用
goViewProjectService.updateProject(reqVO);
// 校验是否更新正确
GoViewProjectDO goViewProject = goViewProjectMapper.selectById(reqVO.getId()); // 获取最新的
assertPojoEquals(reqVO, goViewProject);
}
@Test
public void testUpdateProject_notExists() {
// 准备参数
GoViewProjectUpdateReqVO reqVO = randomPojo(GoViewProjectUpdateReqVO.class);
// 调用, 并断言异常
assertServiceException(() -> goViewProjectService.updateProject(reqVO), ErrorCodeConstants.GO_VIEW_PROJECT_NOT_EXISTS);
}
@Test
public void testDeleteProject_success() {
// mock 数据
GoViewProjectDO dbGoViewProject = randomPojo(GoViewProjectDO.class);
goViewProjectMapper.insert(dbGoViewProject);// @Sql: 先插入出一条存在的数据
// 准备参数
Long id = dbGoViewProject.getId();
// 调用
goViewProjectService.deleteProject(id);
// 校验数据不存在了
assertNull(goViewProjectMapper.selectById(id));
}
@Test
public void testDeleteProject_notExists() {
// 准备参数
Long id = randomLongId();
// 调用, 并断言异常
assertServiceException(() -> goViewProjectService.deleteProject(id), ErrorCodeConstants.GO_VIEW_PROJECT_NOT_EXISTS);
}
@Test
public void testGetProject() {
// mock 数据
GoViewProjectDO dbGoViewProject = randomPojo(GoViewProjectDO.class);
goViewProjectMapper.insert(dbGoViewProject);// @Sql: 先插入出一条存在的数据
// 准备参数
Long id = dbGoViewProject.getId();
// 调用
GoViewProjectDO goViewProject = goViewProjectService.getProject(id);
// 断言
assertPojoEquals(dbGoViewProject, goViewProject);
}
@Test
public void testGetMyGoViewProjectPage() {
// mock 数据
GoViewProjectDO dbGoViewProject = randomPojo(GoViewProjectDO.class, o -> { // 等会查询到
o.setCreator("1");
});
goViewProjectMapper.insert(dbGoViewProject);
// 测试 userId 不匹配
goViewProjectMapper.insert(cloneIgnoreId(dbGoViewProject, o -> o.setCreator("2")));
// 准备参数
PageParam reqVO = new PageParam();
Long userId = 1L;
// 调用
PageResult<GoViewProjectDO> pageResult = goViewProjectService.getMyProjectPage(reqVO, userId);
// 断言
assertEquals(1, pageResult.getTotal());
assertEquals(1, pageResult.getList().size());
assertPojoEquals(dbGoViewProject, pageResult.getList().get(0));
}
}
@@ -0,0 +1,146 @@
package com.cf.imes.module.report.service.template;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.zip.*;
public class JsonUtil {
public static void main(String[] args) throws IOException {
String data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ureport><cell expand=\"None\" name=\"A1\" row=\"1\" col=\"1\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"B1\" row=\"1\" col=\"2\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"C1\" row=\"1\" col=\"3\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"D1\" row=\"1\" col=\"4\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"A2\" row=\"2\" col=\"1\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"B2\" row=\"2\" col=\"2\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"C2\" row=\"2\" col=\"3\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"D2\" row=\"2\" col=\"4\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"A3\" row=\"3\" col=\"1\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"B3\" row=\"3\" col=\"2\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"C3\" row=\"3\" col=\"3\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"D3\" row=\"3\" col=\"4\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><row row-number=\"1\" height=\"18\"/><row row-number=\"2\" height=\"18\"/><row row-number=\"3\" height=\"18\"/><column col-number=\"1\" width=\"80\"/><column col-number=\"2\" width=\"80\"/><column col-number=\"3\" width=\"80\"/><column col-number=\"4\" width=\"80\"/><paper type=\"A4\" left-margin=\"90\" right-margin=\"90\" top-margin=\"72\" bottom-margin=\"72\" paging-mode=\"fitpage\" fixrows=\"0\" width=\"595\" height=\"842\" orientation=\"portrait\" html-report-align=\"left\" bg-image=\"\" html-interval-refresh-value=\"0\" column-enabled=\"false\"></paper></ureport>";
System.out.println("原始大小:" + data.length());
String zip = zipString(data);
System.out.println("zip压缩后大小:"+zip.length());
String unzip = unzipString(zip);
System.out.println(unzip);
String compress = compress(data);
System.out.println("compress压缩后大小:"+compress.length());
String unCompress = unCompress(compress);
System.out.println(unCompress);
}
/**
* 压缩
*/
public static String zipString(String unzipString) {
/**
* https://www.yiibai.com/javazip/javazip_deflater.html#article-start
* 0 ~ 9 压缩等级 低到高
* public static final int BEST_COMPRESSION = 9; 最佳压缩的压缩级别。
* public static final int BEST_SPEED = 1; 压缩级别最快的压缩。
* public static final int DEFAULT_COMPRESSION = -1; 默认压缩级别。
* public static final int DEFAULT_STRATEGY = 0; 默认压缩策略。
* public static final int DEFLATED = 8; 压缩算法的压缩方法(目前唯一支持的压缩方法)。
* public static final int FILTERED = 1; 压缩策略最适用于大部分数值较小且数据分布随机分布的数据。
* public static final int FULL_FLUSH = 3; 压缩刷新模式,用于清除所有待处理的输出并重置拆卸器。
* public static final int HUFFMAN_ONLY = 2; 仅用于霍夫曼编码的压缩策略。
* public static final int NO_COMPRESSION = 0; 不压缩的压缩级别。
* public static final int NO_FLUSH = 0; 用于实现最佳压缩结果的压缩刷新模式。
* public static final int SYNC_FLUSH = 2; 用于清除所有未决输出的压缩刷新模式; 可能会降低某些压缩算法的压缩率。
*/
//使用指定的压缩级别创建一个新的压缩器。
Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
//设置压缩输入数据。
deflater.setInput(unzipString.getBytes());
//当被调用时,表示压缩应该以输入缓冲区的当前内容结束。
deflater.finish();
final byte[] bytes = new byte[256];
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(256);
while (!deflater.finished()) {
//压缩输入数据并用压缩数据填充指定的缓冲区。
int length = deflater.deflate(bytes);
outputStream.write(bytes, 0, length);
}
//关闭压缩器并丢弃任何未处理的输入。
deflater.end();
return Base64.getEncoder().encodeToString(outputStream.toByteArray());
}
/**
* 解压缩
*/
public static String unzipString(String zipString) {
byte[] decode = Base64.getDecoder().decode(zipString);
//创建一个新的解压缩器 https://www.yiibai.com/javazip/javazip_inflater.html
Inflater inflater = new Inflater();
//设置解压缩的输入数据。
inflater.setInput(decode);
final byte[] bytes = new byte[256];
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(256);
try {
//finished() 如果已到达压缩数据流的末尾,则返回true。
while (!inflater.finished()) {
//将字节解压缩到指定的缓冲区中。
int length = inflater.inflate(bytes);
outputStream.write(bytes, 0, length);
}
} catch (DataFormatException e) {
e.printStackTrace();
return null;
} finally {
//关闭解压缩器并丢弃任何未处理的输入。
inflater.end();
}
return outputStream.toString();
}
// 使用 GZIP 进行压缩
public static String compress(String str) throws IOException {
if (null == str || str.length() <= 0) {
return str;
}
// 创建一个新的输出流
ByteArrayOutputStream out = new ByteArrayOutputStream();
// 使用默认缓冲区大小创建新的输出流
GZIPOutputStream gzip = new GZIPOutputStream(out);
// 将字节写入此输出流
gzip.write(str.getBytes("utf-8")); // 因为后台默认字符集有可能是GBK字符集,所以此处需指定一个字符集
gzip.close();
// 使用指定的 charsetName,通过解码字节将缓冲区内容转换为字符串
return out.toString("ISO-8859-1");
}
// 解压缩
public static String unCompress(String str) throws IOException {
if (null == str || str.length() <= 0) {
return str;
}
// 创建一个新的输出流
ByteArrayOutputStream out = new ByteArrayOutputStream();
// 创建一个 ByteArrayInputStream,使用 buf 作为其缓冲 区数组
ByteArrayInputStream in = new ByteArrayInputStream(str.getBytes("ISO-8859-1"));
// 使用默认缓冲区大小创建新的输入流
GZIPInputStream gzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n = 0;
// 将未压缩数据读入字节数组
while ((n = gzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
// 使用指定的 charsetName,通过解码字节将缓冲区内容转换为字符串
return out.toString("utf-8");
}
}
@@ -0,0 +1,66 @@
package com.cf.imes.module.report.service.template;
import com.cf.imes.framework.organ.core.context.OrganContextHolder;
import com.cf.imes.framework.test.core.util.RandomUtils;
import com.cf.imes.module.report.controller.admin.template.vo.ReportDatasetParameterVO;
import com.cf.imes.module.report.controller.admin.template.vo.ReportDatasetSaveReqVO;
import com.cf.imes.module.report.controller.admin.template.vo.ReportDatasourceSaveReqVO;
import com.cf.imes.module.report.controller.admin.template.vo.ReportTemplateSaveReqVO;
import com.cf.imes.module.report.enums.template.ReportTemplateTypeEnum;
import java.util.ArrayList;
import java.util.List;
/**
* @author Gqr
* @since 2024/7/5 17:05
*/
public abstract class ReportCommonServiceImplTest {
protected static final String TEMPLATE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ureport><cell expand=\"None\" name=\"A1\" row=\"1\" col=\"1\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"B1\" row=\"1\" col=\"2\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"C1\" row=\"1\" col=\"3\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"D1\" row=\"1\" col=\"4\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"A2\" row=\"2\" col=\"1\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"B2\" row=\"2\" col=\"2\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"C2\" row=\"2\" col=\"3\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"D2\" row=\"2\" col=\"4\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"A3\" row=\"3\" col=\"1\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"B3\" row=\"3\" col=\"2\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"C3\" row=\"3\" col=\"3\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><cell expand=\"None\" name=\"D3\" row=\"3\" col=\"4\"><cell-style font-size=\"10\" align=\"center\" valign=\"middle\"></cell-style><simple-value><![CDATA[]]></simple-value></cell><row row-number=\"1\" height=\"18\"/><row row-number=\"2\" height=\"18\"/><row row-number=\"3\" height=\"18\"/><column col-number=\"1\" width=\"80\"/><column col-number=\"2\" width=\"80\"/><column col-number=\"3\" width=\"80\"/><column col-number=\"4\" width=\"80\"/><paper type=\"A4\" left-margin=\"90\" right-margin=\"90\" top-margin=\"72\" bottom-margin=\"72\" paging-mode=\"fitpage\" fixrows=\"0\" width=\"595\" height=\"842\" orientation=\"portrait\" html-report-align=\"left\" bg-image=\"\" html-interval-refresh-value=\"0\" column-enabled=\"false\"></paper></ureport>";
protected void setOrganId(Long... organId) {
OrganContextHolder.setOrganId(organId.length > 0 ? organId[0] : 1L);
}
protected ReportTemplateSaveReqVO prepareTemplateReqVO(Long id) {
return ReportTemplateSaveReqVO.builder()
.id(id)
.name("单元测试模板")
.template(JsonUtil.zipString(TEMPLATE))
.type(ReportTemplateTypeEnum.SYSTEM.getType())
.remark(RandomUtils.randomString())
.build();
}
protected ReportDatasourceSaveReqVO prepareDatasourceReqVO(Long id, Long reportId) {
return ReportDatasourceSaveReqVO.builder()
.id(id)
.name("单元测试数据源")
.reportId(reportId)
.type(ReportTemplateTypeEnum.SYSTEM.getType())
.dbDriver("com.mysql.cj.jdbc.Driver")
.dbUrl("jdbc:mysql://192.168.1.205:3307/imes_base?allowMultiQueries=true&useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&nullCatalogMeansCurrent=true")
.dbUsername("root")
.dbPassword("root")
.remark(RandomUtils.randomString())
.build();
}
protected ReportDatasetSaveReqVO prepareDatasetReqVO(Long id, Long datasourceId) {
ReportDatasetParameterVO idvo = new ReportDatasetParameterVO("id", 1, "1");
ReportDatasetParameterVO appidvo = new ReportDatasetParameterVO("app_id", 2, "CF7526AD51");
List<ReportDatasetParameterVO> params = new ArrayList<>() {{
add(idvo);
add(appidvo);
}};
return ReportDatasetSaveReqVO.builder()
.id(id)
.name("单元测试数据集")
.dbSql("select * from application where id = \"${param(\"id\")}\" and app_id = \"${param(\"app_id\")}\"")
.datasourceId(datasourceId)
.params(params)
.build();
}
}
@@ -0,0 +1,113 @@
package com.cf.imes.module.report.service.template;
import com.cf.imes.framework.test.core.util.RandomUtils;
import com.cf.imes.module.report.controller.admin.template.vo.*;
import com.cf.imes.module.report.dal.dataobject.template.ReportDatasetDO;
import com.cf.imes.module.report.dal.dataobject.template.ReportDatasourceDO;
import com.cf.imes.module.report.dal.dataobject.template.ReportTemplateDO;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.List;
import static com.cf.imes.framework.test.core.util.AssertUtils.assertServiceException;
import static com.cf.imes.framework.test.core.util.AssertUtils.isPojoEquals;
import static com.cf.imes.module.report.enums.ErrorCodeConstants.DATASET_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/**
* @author Gqr
* @since 2024/7/8 14:22
*/
@SpringBootTest
public class ReportDatasetServiceImplTest extends ReportCommonServiceImplTest{
@Resource
private ReportDatasetService reportDatasetService;
@Resource
private ReportTemplateService reportTemplateService;
@Resource
private ReportDatasourceService reportDatasourceService;
@Test
public void testCreateDataset_success() {
// 1、上下文添加组织id
setOrganId();
// 1、准备template参数
// 2、插入template、断言自增id
// 3、用id查template、断言对象
ReportTemplateSaveReqVO createReqVO = prepareTemplateReqVO(null);
Long templateId = reportTemplateService.createReportTemplate(createReqVO);
assertNotNull(templateId);
ReportTemplateDO template = reportTemplateService.getReportTemplate(templateId);
assertNotNull(template);
// 1、准备datasource参数
// 2、插入datasource、断言自增id
// 3、用id查datasource、断言对象
ReportDatasourceSaveReqVO datasourceSaveReqVO = prepareDatasourceReqVO(null, templateId);
Long datasourceId = reportDatasourceService.createDatasource(datasourceSaveReqVO);
assertNotNull(datasourceId);
ReportDatasourceDO datasource = reportDatasourceService.getDatasource(datasourceId);
assertNotNull(datasource);
// 1、准备dataset参数
// 2、插入dataset、断言自增id
// 3、用id查dataset、断言对象
ReportDatasetSaveReqVO datasetSaveReqVO = prepareDatasetReqVO(null, datasourceId);
Long datasetId = reportDatasetService.createDataset(datasetSaveReqVO);
assertNotNull(datasetId);
ReportDatasetDO dataset = reportDatasetService.getDataset(datasetId);
assertNotNull(dataset);
//准备更新参数
ReportDatasetSaveReqVO updateReqVO = prepareDatasetReqVO(datasetId, datasourceId);
// 更新
reportDatasetService.updateDataset(updateReqVO);
// 校验是否更新正确
ReportDatasetDO updateDataset = reportDatasetService.getDataset(updateReqVO.getId()); // 获取最新的
isPojoEquals(updateDataset, updateReqVO);
// 列表查询参数准备
ReportDatasetReqVO reqVO = new ReportDatasetReqVO();
reqVO.setName(updateReqVO.getName());
reqVO.setDatasourceId(updateReqVO.getDatasourceId());
List<ReportDatasetDO> datasetList = reportDatasetService.getDatasetList(reqVO);
assertNotNull(datasetList);
assertNotEquals(datasetList.size(), 0);
// 删除
reportTemplateService.deleteReportTemplate(templateId);
reportDatasourceService.deleteDatasource(datasourceId);
reportDatasetService.deleteDataset(datasetId);
}
@Test
public void testUpdateDataset_notExists() {
setOrganId();
// 准备参数
ReportDatasetSaveReqVO updateReqVO = prepareDatasetReqVO(RandomUtils.randomLongId(), RandomUtils.randomLongId());
// 调用, 并断言异常
assertServiceException(() -> reportDatasetService.updateDataset(updateReqVO), DATASET_NOT_EXISTS);
}
@Test
public void testDeleteDataset_notExists() {
setOrganId();
// 准备参数
Long id = RandomUtils.randomLongId();
// 调用, 并断言异常
assertServiceException(() -> reportDatasetService.deleteDataset(id), DATASET_NOT_EXISTS);
}
}
@@ -0,0 +1,100 @@
package com.cf.imes.module.report.service.template;
import com.cf.imes.framework.test.core.util.RandomUtils;
import com.cf.imes.module.report.controller.admin.template.vo.ReportDatasourceReqVO;
import com.cf.imes.module.report.controller.admin.template.vo.ReportDatasourceSaveReqVO;
import com.cf.imes.module.report.controller.admin.template.vo.ReportTemplateSaveReqVO;
import com.cf.imes.module.report.dal.dataobject.template.ReportDatasourceDO;
import com.cf.imes.module.report.dal.dataobject.template.ReportTemplateDO;
import com.cf.imes.module.report.enums.template.ReportTemplateTypeEnum;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.List;
import static com.cf.imes.framework.test.core.util.AssertUtils.assertServiceException;
import static com.cf.imes.framework.test.core.util.AssertUtils.isPojoEquals;
import static com.cf.imes.module.report.enums.ErrorCodeConstants.DATASOURCE_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.*;
/**
* @author Gqr
* @since 2024/7/5 17:01
*/
@SpringBootTest
public class ReportDatasourceServiceImplTest extends ReportCommonServiceImplTest {
@Resource
ReportDatasourceService reportDatasourceService;
@Resource
ReportTemplateService reportTemplateService;
@Test
public void testCreateTemplate_success() {
// 1、上下文添加组织id
setOrganId();
// 1、准备template参数
// 2、插入template、断言自增id
// 3、用id查template、断言对象
ReportTemplateSaveReqVO createReqVO = prepareTemplateReqVO(null);
Long templateId = reportTemplateService.createReportTemplate(createReqVO);
assertNotNull(templateId);
ReportTemplateDO template = reportTemplateService.getReportTemplate(templateId);
assertNotNull(template);
// 1、准备datasource参数
// 2、插入datasource、断言自增id
// 3、用id查datasource、断言对象
ReportDatasourceSaveReqVO datasourceSaveReqVO = prepareDatasourceReqVO(null, templateId);
Long datasourceId = reportDatasourceService.createDatasource(datasourceSaveReqVO);
assertNotNull(datasourceId);
ReportDatasourceDO datasource = reportDatasourceService.getDatasource(datasourceId);
assertNotNull(datasource);
//准备更新参数
ReportDatasourceSaveReqVO updateReqVO = prepareDatasourceReqVO(datasourceId, templateId);
// 更新
reportDatasourceService.updateDatasource(updateReqVO);
// 校验是否更新正确
ReportDatasourceDO updateDatasouce = reportDatasourceService.getDatasource(updateReqVO.getId()); // 获取最新的
isPojoEquals(updateDatasouce, updateReqVO);
// 列表查询参数准备
ReportDatasourceReqVO reqVO = new ReportDatasourceReqVO();
reqVO.setReportId(templateId);
reqVO.setType(ReportTemplateTypeEnum.SYSTEM.getType());
reqVO.setName(updateReqVO.getName());
List<ReportDatasourceDO> templateDatasourceList = reportDatasourceService.getTemplateDatasourceList(reqVO);
assertNotNull(templateDatasourceList);
assertNotEquals(templateDatasourceList.size(),0);
// 删除
reportTemplateService.deleteReportTemplate(templateId);
reportDatasourceService.deleteDatasource(datasourceId);
}
@Test
public void testUpdateTemplate_notExists() {
setOrganId();
// 准备参数
ReportDatasourceSaveReqVO updateReqVO = prepareDatasourceReqVO(RandomUtils.randomLongId(), RandomUtils.randomLongId());
// 调用, 并断言异常
assertServiceException(() -> reportDatasourceService.updateDatasource(updateReqVO), DATASOURCE_NOT_EXISTS);
}
@Test
public void testDeleteTemplate_notExists() {
setOrganId();
// 准备参数
Long id = RandomUtils.randomLongId();
// 调用, 并断言异常
assertServiceException(() -> reportDatasourceService.deleteDatasource(id), DATASOURCE_NOT_EXISTS);
}
}
@@ -0,0 +1,128 @@
package com.cf.imes.module.report.service.template;
import com.cf.imes.framework.test.core.util.RandomUtils;
import com.cf.imes.module.report.controller.admin.template.vo.ReportTemplateReqVO;
import com.cf.imes.module.report.controller.admin.template.vo.ReportTemplateSaveReqVO;
import com.cf.imes.module.report.dal.dataobject.template.ReportTemplateDO;
import com.cf.imes.module.report.enums.template.ReportTemplateTypeEnum;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.List;
import static com.cf.imes.framework.test.core.util.AssertUtils.*;
import static com.cf.imes.module.report.enums.ErrorCodeConstants.TEMPLATE_NOT_EXISTS;
import static org.junit.jupiter.api.Assertions.*;
/**
* {@link ReportTemplateServiceImpl} 的单元测试类
*
* @author Gqr
* @since 2024/7/3 14:35
*/
@SpringBootTest
public class ReportTemplateServiceImplTest extends ReportCommonServiceImplTest {
@Resource
private ReportTemplateServiceImpl reportTemplateService;
@Test
public void testCreateTemplate_success() {
setOrganId();
// 准备参数
ReportTemplateSaveReqVO createReqVO = prepareTemplateReqVO(null);
System.out.println("template压缩后大小:" + createReqVO.getTemplate().length());
// 插入
Long templateId = reportTemplateService.createReportTemplate(createReqVO);
// 断言
assertNotNull(templateId);
// 校验是否插入
ReportTemplateDO template = reportTemplateService.getReportTemplate(templateId);
assertNotNull(template);
String templateUnZip = template.getTemplate();
System.out.println("从库中读取template大小:" + templateUnZip.length());
// 校验解压内容是否正确
assertEquals(JsonUtil.unzipString(templateUnZip),TEMPLATE);
// 删除
reportTemplateService.deleteReportTemplate(templateId);
}
@Test
public void testUpdateTemplate_success() {
setOrganId();
// 准备参数
ReportTemplateSaveReqVO createReqVO = prepareTemplateReqVO(null);
System.out.println("template压缩后大小:" + createReqVO.getTemplate().length());
// 插入
Long templateId = reportTemplateService.createReportTemplate(createReqVO);
//准备更新参数
ReportTemplateSaveReqVO updateReqVO = prepareTemplateReqVO(templateId);
// 更新
reportTemplateService.updateReportTemplate(updateReqVO);
// 校验是否更新正确
ReportTemplateDO template = reportTemplateService.getReportTemplate(updateReqVO.getId()); // 获取最新的
isPojoEquals(template,updateReqVO);
// 删除
reportTemplateService.deleteReportTemplate(templateId);
}
@Test
public void testUpdateTemplate_notExists() {
setOrganId();
// 准备参数
ReportTemplateSaveReqVO updateReqVO = prepareTemplateReqVO(RandomUtils.randomLongId());
// 调用, 并断言异常
assertServiceException(() -> reportTemplateService.updateReportTemplate(updateReqVO), TEMPLATE_NOT_EXISTS);
}
@Test
public void deleteTemplate_success() {
setOrganId();
// 准备参数
ReportTemplateSaveReqVO createReqVO = prepareTemplateReqVO(null);
// 插入
Long templateId = reportTemplateService.createReportTemplate(createReqVO);
// 删除
reportTemplateService.deleteReportTemplate(templateId);
// 校验数据不存在了
assertNull(reportTemplateService.getReportTemplate(templateId));
}
@Test
public void testDeleteTemplate_notExists() {
setOrganId();
// 准备参数
Long id = RandomUtils.randomLongId();
// 调用, 并断言异常
assertServiceException(() -> reportTemplateService.deleteReportTemplate(id), TEMPLATE_NOT_EXISTS);
}
@Test
public void getTemplateList_success() {
setOrganId(2L);
// 准备参数
ReportTemplateSaveReqVO createReqVO = prepareTemplateReqVO(null);
// 插入
Long templateId = reportTemplateService.createReportTemplate(createReqVO);
ReportTemplateReqVO reqVO = new ReportTemplateReqVO();
reqVO.setType(ReportTemplateTypeEnum.SYSTEM.getType());
reqVO.setName(createReqVO.getName());
List<ReportTemplateDO> reportTemplateList = reportTemplateService.getReportTemplateList(reqVO);
assertNotNull(reportTemplateList);
assertNotEquals(reportTemplateList.size(),0);
assertEquals(reportTemplateList.get(0).getOrganId(),2L);
// 删除
reportTemplateService.deleteReportTemplate(templateId);
}
}