dengzedong
2024-12-19 85b2001c0ec2f1adc598db3bf47ad457dcca7074
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.report.service.goview;
H 2
3 import com.iailab.framework.common.pojo.PageParam;
4 import com.iailab.framework.common.pojo.PageResult;
5 import com.iailab.framework.test.core.ut.BaseDbUnitTest;
6 import com.iailab.module.report.controller.admin.goview.vo.project.GoViewProjectCreateReqVO;
7 import com.iailab.module.report.controller.admin.goview.vo.project.GoViewProjectUpdateReqVO;
8 import com.iailab.module.report.dal.dataobject.goview.GoViewProjectDO;
9 import com.iailab.module.report.dal.mysql.goview.GoViewProjectMapper;
10 import org.junit.jupiter.api.Test;
11 import org.springframework.context.annotation.Import;
12
13 import javax.annotation.Resource;
14
15 import static com.iailab.framework.common.util.object.ObjectUtils.cloneIgnoreId;
16 import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals;
17 import static com.iailab.framework.test.core.util.AssertUtils.assertServiceException;
18 import static com.iailab.framework.test.core.util.RandomUtils.*;
19 import static com.iailab.module.report.enums.ErrorCodeConstants.GO_VIEW_PROJECT_NOT_EXISTS;
20 import static org.junit.jupiter.api.Assertions.*;
21
22 /**
23  * {@link GoViewProjectServiceImpl} 的单元测试类
24  *
25  * @author iailab
26  */
27 @Import(GoViewProjectServiceImpl.class)
28 public class GoViewProjectServiceImplTest extends BaseDbUnitTest {
29
30     @Resource
31     private GoViewProjectServiceImpl goViewProjectService;
32
33     @Resource
34     private GoViewProjectMapper goViewProjectMapper;
35
36     @Test
37     public void testCreateProject_success() {
38         // 准备参数
39         GoViewProjectCreateReqVO reqVO = randomPojo(GoViewProjectCreateReqVO.class);
40
41         // 调用
42         Long goViewProjectId = goViewProjectService.createProject(reqVO);
43         // 断言
44         assertNotNull(goViewProjectId);
45         // 校验记录的属性是否正确
46         GoViewProjectDO goViewProject = goViewProjectMapper.selectById(goViewProjectId);
47         assertPojoEquals(reqVO, goViewProject);
48     }
49
50     @Test
51     public void testUpdateProject_success() {
52         // mock 数据
53         GoViewProjectDO dbGoViewProject = randomPojo(GoViewProjectDO.class);
54         goViewProjectMapper.insert(dbGoViewProject);// @Sql: 先插入出一条存在的数据
55         // 准备参数
56         GoViewProjectUpdateReqVO reqVO = randomPojo(GoViewProjectUpdateReqVO.class, o -> {
57             o.setId(dbGoViewProject.getId()); // 设置更新的 ID
58             o.setStatus(randomCommonStatus());
59         });
60
61         // 调用
62         goViewProjectService.updateProject(reqVO);
63         // 校验是否更新正确
64         GoViewProjectDO goViewProject = goViewProjectMapper.selectById(reqVO.getId()); // 获取最新的
65         assertPojoEquals(reqVO, goViewProject);
66     }
67
68     @Test
69     public void testUpdateProject_notExists() {
70         // 准备参数
71         GoViewProjectUpdateReqVO reqVO = randomPojo(GoViewProjectUpdateReqVO.class);
72
73         // 调用, 并断言异常
74         assertServiceException(() -> goViewProjectService.updateProject(reqVO), GO_VIEW_PROJECT_NOT_EXISTS);
75     }
76
77     @Test
78     public void testDeleteProject_success() {
79         // mock 数据
80         GoViewProjectDO dbGoViewProject = randomPojo(GoViewProjectDO.class);
81         goViewProjectMapper.insert(dbGoViewProject);// @Sql: 先插入出一条存在的数据
82         // 准备参数
83         Long id = dbGoViewProject.getId();
84
85         // 调用
86         goViewProjectService.deleteProject(id);
87         // 校验数据不存在了
88         assertNull(goViewProjectMapper.selectById(id));
89     }
90
91     @Test
92     public void testDeleteProject_notExists() {
93         // 准备参数
94         Long id = randomLongId();
95
96         // 调用, 并断言异常
97         assertServiceException(() -> goViewProjectService.deleteProject(id), GO_VIEW_PROJECT_NOT_EXISTS);
98     }
99
100     @Test
101     public void testGetProject() {
102         // mock 数据
103         GoViewProjectDO dbGoViewProject = randomPojo(GoViewProjectDO.class);
104         goViewProjectMapper.insert(dbGoViewProject);// @Sql: 先插入出一条存在的数据
105         // 准备参数
106         Long id = dbGoViewProject.getId();
107
108         // 调用
109         GoViewProjectDO goViewProject = goViewProjectService.getProject(id);
110         // 断言
111         assertPojoEquals(dbGoViewProject, goViewProject);
112     }
113
114     @Test
115     public void testGetMyGoViewProjectPage() {
116         // mock 数据
117         GoViewProjectDO dbGoViewProject = randomPojo(GoViewProjectDO.class, o -> { // 等会查询到
118             o.setCreator("1");
119         });
120         goViewProjectMapper.insert(dbGoViewProject);
121         // 测试 userId 不匹配
122         goViewProjectMapper.insert(cloneIgnoreId(dbGoViewProject, o -> o.setCreator("2")));
123         // 准备参数
124         PageParam reqVO = new PageParam();
125         Long userId = 1L;
126
127         // 调用
128         PageResult<GoViewProjectDO> pageResult = goViewProjectService.getMyProjectPage(reqVO, userId);
129         // 断言
130         assertEquals(1, pageResult.getTotal());
131         assertEquals(1, pageResult.getList().size());
132         assertPojoEquals(dbGoViewProject, pageResult.getList().get(0));
133     }
134
135 }