潘志宝
2024-11-11 31bd2c17088ec34072deabe106ff1d695c8b2b49
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.bpm.service.definition;
H 2
3 import cn.hutool.core.util.RandomUtil;
4 import com.iailab.framework.common.pojo.PageResult;
5 import com.iailab.framework.common.util.json.JsonUtils;
6 import com.iailab.framework.test.core.ut.BaseDbUnitTest;
7 import com.iailab.module.bpm.controller.admin.definition.vo.form.BpmFormSaveReqVO;
8 import com.iailab.module.bpm.controller.admin.definition.vo.form.BpmFormPageReqVO;
9 import com.iailab.module.bpm.dal.dataobject.definition.BpmFormDO;
10 import com.iailab.module.bpm.dal.mysql.definition.BpmFormMapper;
11 import com.iailab.module.bpm.service.definition.dto.BpmFormFieldRespDTO;
12 import org.junit.jupiter.api.Test;
13 import org.springframework.context.annotation.Import;
14
15 import javax.annotation.Resource;
16 import java.util.List;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
19
20 import static com.iailab.framework.common.util.object.ObjectUtils.cloneIgnoreId;
21 import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals;
22 import static com.iailab.framework.test.core.util.AssertUtils.assertServiceException;
23 import static com.iailab.framework.test.core.util.RandomUtils.randomLongId;
24 import static com.iailab.framework.test.core.util.RandomUtils.randomPojo;
25 import static com.iailab.module.bpm.enums.ErrorCodeConstants.FORM_NOT_EXISTS;
26 import static org.junit.jupiter.api.Assertions.*;
27
28 /**
29  * {@link BpmFormServiceImpl} 的单元测试类
30  *
31  * @author iailab
32  */
33 @Import(BpmFormServiceImpl.class)
34 public class BpmFormServiceTest extends BaseDbUnitTest {
35
36     @Resource
37     private BpmFormServiceImpl formService;
38
39     @Resource
40     private BpmFormMapper formMapper;
41
42     @Test
43     public void testCreateForm_success() {
44         // 准备参数
45         BpmFormSaveReqVO reqVO = randomPojo(BpmFormSaveReqVO.class, o -> {
46             o.setConf("{}");
47             o.setFields(randomFields());
48         });
49
50         // 调用
51         Long formId = formService.createForm(reqVO);
52         // 断言
53         assertNotNull(formId);
54         // 校验记录的属性是否正确
55         BpmFormDO form = formMapper.selectById(formId);
56         assertPojoEquals(reqVO, form);
57     }
58
59     @Test
60     public void testUpdateForm_success() {
61         // mock 数据
62         BpmFormDO dbForm = randomPojo(BpmFormDO.class, o -> {
63             o.setConf("{}");
64             o.setFields(randomFields());
65         });
66         formMapper.insert(dbForm);// @Sql: 先插入出一条存在的数据
67         // 准备参数
68         BpmFormSaveReqVO reqVO = randomPojo(BpmFormSaveReqVO.class, o -> {
69             o.setId(dbForm.getId()); // 设置更新的 ID
70             o.setConf("{'iailab': 'yuanma'}");
71             o.setFields(randomFields());
72         });
73
74         // 调用
75         formService.updateForm(reqVO);
76         // 校验是否更新正确
77         BpmFormDO form = formMapper.selectById(reqVO.getId()); // 获取最新的
78         assertPojoEquals(reqVO, form);
79     }
80
81     @Test
82     public void testUpdateForm_notExists() {
83         // 准备参数
84         BpmFormSaveReqVO reqVO = randomPojo(BpmFormSaveReqVO.class, o -> {
85             o.setConf("{'iailab': 'yuanma'}");
86             o.setFields(randomFields());
87         });
88
89         // 调用, 并断言异常
90         assertServiceException(() -> formService.updateForm(reqVO), FORM_NOT_EXISTS);
91     }
92
93     @Test
94     public void testDeleteForm_success() {
95         // mock 数据
96         BpmFormDO dbForm = randomPojo(BpmFormDO.class);
97         formMapper.insert(dbForm);// @Sql: 先插入出一条存在的数据
98         // 准备参数
99         Long id = dbForm.getId();
100
101         // 调用
102         formService.deleteForm(id);
103         // 校验数据不存在了
104         assertNull(formMapper.selectById(id));
105     }
106
107     @Test
108     public void testDeleteForm_notExists() {
109         // 准备参数
110         Long id = randomLongId();
111
112         // 调用, 并断言异常
113         assertServiceException(() -> formService.deleteForm(id), FORM_NOT_EXISTS);
114     }
115
116     @Test
117     public void testGetFormPage() {
118         // mock 数据
119         BpmFormDO dbForm = randomPojo(BpmFormDO.class, o -> { // 等会查询到
120             o.setName("iailab");
121         });
122         formMapper.insert(dbForm);
123         // 测试 name 不匹配
124         formMapper.insert(cloneIgnoreId(dbForm, o -> o.setName("源码")));
125         // 准备参数
126         BpmFormPageReqVO reqVO = new BpmFormPageReqVO();
127         reqVO.setName("平台");
128
129         // 调用
130         PageResult<BpmFormDO> pageResult = formService.getFormPage(reqVO);
131         // 断言
132         assertEquals(1, pageResult.getTotal());
133         assertEquals(1, pageResult.getList().size());
134         assertPojoEquals(dbForm, pageResult.getList().get(0));
135     }
136
137     private List<String> randomFields() {
138         int size = RandomUtil.randomInt(1, 3);
139         return Stream.iterate(0, i -> i).limit(size)
140                 .map(i -> JsonUtils.toJsonString(randomPojo(BpmFormFieldRespDTO.class)))
141                 .collect(Collectors.toList());
142     }
143
144 }