MaTechnicianServiceImplTest.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package com.ylx.massage.service.impl;
  2. import com.baomidou.mybatisplus.core.MybatisConfiguration;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  5. import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
  6. import com.ylx.massage.domain.MaTechnician;
  7. import com.ylx.massage.domain.MerchantApplyFile;
  8. import com.ylx.massage.domain.dto.MerchantApplyFileDto;
  9. import com.ylx.massage.domain.dto.MerchantApplyFileRequestDto;
  10. import com.ylx.massage.domain.vo.MerchantAuditFile;
  11. import com.ylx.massage.mapper.MaTechnicianMapper;
  12. import com.ylx.massage.mapper.MerchantApplyFileMapper;
  13. import org.apache.ibatis.builder.MapperBuilderAssistant;
  14. import org.junit.jupiter.api.BeforeAll;
  15. import org.junit.jupiter.api.Test;
  16. import org.mockito.ArgumentCaptor;
  17. import org.springframework.test.util.ReflectionTestUtils;
  18. import java.lang.reflect.Field;
  19. import java.util.Arrays;
  20. import java.util.Collections;
  21. import java.util.List;
  22. import static org.junit.jupiter.api.Assertions.assertEquals;
  23. import static org.junit.jupiter.api.Assertions.assertFalse;
  24. import static org.junit.jupiter.api.Assertions.assertNotEquals;
  25. import static org.junit.jupiter.api.Assertions.assertThrows;
  26. import static org.junit.jupiter.api.Assertions.assertTrue;
  27. import static org.mockito.ArgumentMatchers.any;
  28. import static org.mockito.ArgumentMatchers.isNull;
  29. import static org.mockito.Mockito.mock;
  30. import static org.mockito.Mockito.times;
  31. import static org.mockito.Mockito.verify;
  32. import static org.mockito.Mockito.when;
  33. public class MaTechnicianServiceImplTest {
  34. @BeforeAll
  35. public static void initMybatisPlusTableInfo() {
  36. MybatisConfiguration configuration = new MybatisConfiguration();
  37. TableInfoHelper.initTableInfo(new MapperBuilderAssistant(configuration, ""), MaTechnician.class);
  38. TableInfoHelper.initTableInfo(new MapperBuilderAssistant(configuration, ""), MerchantApplyFile.class);
  39. }
  40. @Test
  41. public void updateTechnicianOnlyUpdatesNicknameAndBriefForBaseInfo() {
  42. MaTechnicianMapper maTechnicianMapper = mock(MaTechnicianMapper.class);
  43. MerchantApplyFileMapper merchantApplyFileMapper = mock(MerchantApplyFileMapper.class);
  44. when(maTechnicianMapper.update(isNull(), any(LambdaUpdateWrapper.class))).thenReturn(1);
  45. MaTechnicianServiceImpl service = buildService(maTechnicianMapper, merchantApplyFileMapper);
  46. MerchantApplyFileRequestDto request = new MerchantApplyFileRequestDto();
  47. MaTechnician technician = new MaTechnician();
  48. technician.setId(7);
  49. technician.setTeName("不能修改的姓名");
  50. technician.setTePhone("18800000000");
  51. technician.setAvatar("不能修改的形象照");
  52. technician.setTeNickName("新昵称");
  53. technician.setTeBrief("新简介");
  54. request.setTechnician(technician);
  55. service.updateTechnician(request);
  56. ArgumentCaptor<LambdaUpdateWrapper<MaTechnician>> wrapperCaptor = ArgumentCaptor.forClass(LambdaUpdateWrapper.class);
  57. verify(maTechnicianMapper).update(isNull(), wrapperCaptor.capture());
  58. String sqlSet = String.join(",", wrapperCaptor.getValue().getSqlSet());
  59. assertTrue(sqlSet.contains("te_nick_name"));
  60. assertTrue(sqlSet.contains("te_brief"));
  61. assertFalse(sqlSet.contains("te_name"));
  62. assertFalse(sqlSet.contains("te_phone"));
  63. assertFalse(sqlSet.contains("avatar"));
  64. assertFalse(sqlSet.contains("te_avatar"));
  65. }
  66. @Test
  67. public void updateTechnicianUpsertsApplyFilesByMerchantAndFileType() {
  68. MaTechnicianMapper maTechnicianMapper = mock(MaTechnicianMapper.class);
  69. MerchantApplyFileMapper merchantApplyFileMapper = mock(MerchantApplyFileMapper.class);
  70. when(maTechnicianMapper.update(isNull(), any(LambdaUpdateWrapper.class))).thenReturn(1);
  71. MerchantApplyFile existsFile = new MerchantApplyFile();
  72. existsFile.setId(3L);
  73. existsFile.setMerchantId(7);
  74. existsFile.setFileType("1");
  75. when(merchantApplyFileMapper.selectOne(any(LambdaQueryWrapper.class))).thenReturn(existsFile, null);
  76. MaTechnicianServiceImpl service = buildService(maTechnicianMapper, merchantApplyFileMapper);
  77. MerchantApplyFileRequestDto request = new MerchantApplyFileRequestDto();
  78. MaTechnician technician = new MaTechnician();
  79. technician.setId(7);
  80. technician.setTeNickName("新昵称");
  81. technician.setTeBrief("新简介");
  82. request.setTechnician(technician);
  83. MerchantApplyFileDto updateFile = buildFile("1", "new-image.jpg", "https://file/new-image.jpg");
  84. MerchantApplyFileDto insertFile = buildFile("2", "life.jpg", "https://file/life.jpg");
  85. request.setReq(Arrays.asList(updateFile, insertFile));
  86. service.updateTechnician(request);
  87. ArgumentCaptor<MerchantApplyFile> updateCaptor = ArgumentCaptor.forClass(MerchantApplyFile.class);
  88. verify(merchantApplyFileMapper).updateById(updateCaptor.capture());
  89. MerchantApplyFile updatedFile = updateCaptor.getValue();
  90. assertEquals(3L, updatedFile.getId());
  91. assertEquals(7, updatedFile.getMerchantId());
  92. assertEquals("1", updatedFile.getFileType());
  93. assertEquals("new-image.jpg", updatedFile.getFileName());
  94. assertEquals("https://file/new-image.jpg", updatedFile.getFileUrl());
  95. assertEquals("7", updatedFile.getUpdateBy());
  96. assertEquals(0, updatedFile.getIsDelete());
  97. ArgumentCaptor<MerchantApplyFile> insertCaptor = ArgumentCaptor.forClass(MerchantApplyFile.class);
  98. verify(merchantApplyFileMapper).insert(insertCaptor.capture());
  99. MerchantApplyFile insertedFile = insertCaptor.getValue();
  100. assertEquals(7, insertedFile.getMerchantId());
  101. assertEquals("2", insertedFile.getFileType());
  102. assertEquals("life.jpg", insertedFile.getFileName());
  103. assertEquals("https://file/life.jpg", insertedFile.getFileUrl());
  104. assertEquals("7", insertedFile.getCreateBy());
  105. assertEquals("7", insertedFile.getUpdateBy());
  106. assertEquals(0, insertedFile.getIsDelete());
  107. }
  108. @Test
  109. public void merchantApplyFileDtoDoesNotExposeMerchantIdInRequestPayload() {
  110. for (Field field : MerchantApplyFileDto.class.getDeclaredFields()) {
  111. assertNotEquals("merchantId", field.getName());
  112. }
  113. }
  114. @Test
  115. public void applyFileReplacesRequestedTypesAndInsertsAllFilesInGroups() {
  116. MaTechnicianMapper maTechnicianMapper = mock(MaTechnicianMapper.class);
  117. MerchantApplyFileMapper merchantApplyFileMapper = mock(MerchantApplyFileMapper.class);
  118. when(maTechnicianMapper.update(isNull(), any(LambdaUpdateWrapper.class))).thenReturn(1);
  119. MaTechnicianServiceImpl service = buildService(maTechnicianMapper, merchantApplyFileMapper);
  120. MerchantApplyFileRequestDto request = new MerchantApplyFileRequestDto();
  121. MaTechnician technician = new MaTechnician();
  122. technician.setId(7);
  123. technician.setTeNickName("新昵称");
  124. technician.setTeBrief("新简介");
  125. request.setTechnician(technician);
  126. request.setReq(Arrays.asList(
  127. buildFileGroup("1",
  128. buildFile(null, "portrait-1.jpg", "https://file/portrait-1.jpg"),
  129. buildFile(null, "portrait-2.jpg", "https://file/portrait-2.jpg")),
  130. buildFileGroup("3",
  131. buildFile(null, "video.mp4", "https://file/video.mp4"))
  132. ));
  133. service.applyFile(request);
  134. verify(merchantApplyFileMapper, times(2)).delete(any(LambdaQueryWrapper.class));
  135. ArgumentCaptor<MerchantApplyFile> insertCaptor = ArgumentCaptor.forClass(MerchantApplyFile.class);
  136. verify(merchantApplyFileMapper, times(3)).insert(insertCaptor.capture());
  137. List<MerchantApplyFile> insertedFiles = insertCaptor.getAllValues();
  138. assertEquals("1", insertedFiles.get(0).getFileType());
  139. assertEquals("portrait-1.jpg", insertedFiles.get(0).getFileName());
  140. assertEquals("1", insertedFiles.get(1).getFileType());
  141. assertEquals("portrait-2.jpg", insertedFiles.get(1).getFileName());
  142. assertEquals("3", insertedFiles.get(2).getFileType());
  143. assertEquals("video.mp4", insertedFiles.get(2).getFileName());
  144. insertedFiles.forEach(file -> {
  145. assertEquals(7, file.getMerchantId());
  146. assertEquals("7", file.getCreateBy());
  147. assertEquals("7", file.getUpdateBy());
  148. assertEquals(0, file.getIsDelete());
  149. });
  150. }
  151. @Test
  152. public void applyFileUsesTechnicianIdWhenReqHasNoMerchantId() {
  153. MaTechnicianMapper maTechnicianMapper = mock(MaTechnicianMapper.class);
  154. MerchantApplyFileMapper merchantApplyFileMapper = mock(MerchantApplyFileMapper.class);
  155. when(maTechnicianMapper.update(isNull(), any(LambdaUpdateWrapper.class))).thenReturn(1);
  156. MaTechnicianServiceImpl service = buildService(maTechnicianMapper, merchantApplyFileMapper);
  157. MerchantApplyFileRequestDto request = new MerchantApplyFileRequestDto();
  158. MaTechnician technician = new MaTechnician();
  159. technician.setId(7);
  160. technician.setTeNickName("新昵称");
  161. technician.setTeBrief("新简介");
  162. request.setTechnician(technician);
  163. request.setReq(Collections.singletonList(
  164. buildFileGroup("2",
  165. buildFile(null, "life.jpg", "https://file/life.jpg"))
  166. ));
  167. service.applyFile(request);
  168. ArgumentCaptor<MerchantApplyFile> insertCaptor = ArgumentCaptor.forClass(MerchantApplyFile.class);
  169. verify(merchantApplyFileMapper).insert(insertCaptor.capture());
  170. MerchantApplyFile insertedFile = insertCaptor.getValue();
  171. assertEquals(7, insertedFile.getMerchantId());
  172. assertEquals("2", insertedFile.getFileType());
  173. assertEquals("life.jpg", insertedFile.getFileName());
  174. assertEquals("7", insertedFile.getCreateBy());
  175. assertEquals("7", insertedFile.getUpdateBy());
  176. assertEquals(0, insertedFile.getIsDelete());
  177. }
  178. @Test
  179. public void getTechnicianInfoFindsMerchantByOpenidWithFiles() {
  180. MaTechnicianMapper maTechnicianMapper = mock(MaTechnicianMapper.class);
  181. MerchantApplyFileMapper merchantApplyFileMapper = mock(MerchantApplyFileMapper.class);
  182. MaTechnician merchant = buildMerchant(7, "openid-7");
  183. MerchantApplyFile file = buildApplyFile(7, "1");
  184. when(maTechnicianMapper.selectOne(any(LambdaQueryWrapper.class))).thenReturn(merchant);
  185. when(merchantApplyFileMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(Collections.singletonList(file));
  186. MaTechnicianServiceImpl service = buildService(maTechnicianMapper, merchantApplyFileMapper);
  187. MerchantAuditFile result = service.getTechnicianInfo("openid-7");
  188. assertEquals(merchant, result.getMerchant());
  189. assertEquals(1, result.getMerchantAuditFile().size());
  190. assertEquals(file, result.getMerchantAuditFile().get(0));
  191. }
  192. @Test
  193. public void getTechnicianListFindsMerchantByUserIdWithFiles() {
  194. MaTechnicianMapper maTechnicianMapper = mock(MaTechnicianMapper.class);
  195. MerchantApplyFileMapper merchantApplyFileMapper = mock(MerchantApplyFileMapper.class);
  196. MaTechnician merchant = buildMerchant(7, "openid-7");
  197. MerchantApplyFile file = buildApplyFile(7, "1");
  198. when(maTechnicianMapper.selectOne(any(LambdaQueryWrapper.class))).thenReturn(merchant);
  199. when(merchantApplyFileMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(Collections.singletonList(file));
  200. MaTechnicianServiceImpl service = buildService(maTechnicianMapper, merchantApplyFileMapper);
  201. MerchantAuditFile result = service.getTechnicianList(7);
  202. assertEquals(merchant, result.getMerchant());
  203. assertEquals(1, result.getMerchantAuditFile().size());
  204. assertEquals(file, result.getMerchantAuditFile().get(0));
  205. ArgumentCaptor<LambdaQueryWrapper<MerchantApplyFile>> fileQueryCaptor = ArgumentCaptor.forClass(LambdaQueryWrapper.class);
  206. verify(merchantApplyFileMapper).selectList(fileQueryCaptor.capture());
  207. String sqlSegment = fileQueryCaptor.getValue().getSqlSegment();
  208. assertTrue(sqlSegment.contains("merchant_id"));
  209. }
  210. @Test
  211. public void getTechnicianInfoRejectsBlankOpenid() {
  212. MaTechnicianMapper maTechnicianMapper = mock(MaTechnicianMapper.class);
  213. MerchantApplyFileMapper merchantApplyFileMapper = mock(MerchantApplyFileMapper.class);
  214. MaTechnicianServiceImpl service = buildService(maTechnicianMapper, merchantApplyFileMapper);
  215. assertThrows(IllegalArgumentException.class, () -> service.getTechnicianInfo(" "));
  216. }
  217. private MaTechnicianServiceImpl buildService(MaTechnicianMapper maTechnicianMapper,
  218. MerchantApplyFileMapper merchantApplyFileMapper) {
  219. MaTechnicianServiceImpl service = new MaTechnicianServiceImpl();
  220. ReflectionTestUtils.setField(service, "maTechnicianMapper", maTechnicianMapper);
  221. ReflectionTestUtils.setField(service, "merchantApplyFileMapper", merchantApplyFileMapper);
  222. return service;
  223. }
  224. private MaTechnician buildMerchant(Integer id, String openid) {
  225. MaTechnician merchant = new MaTechnician();
  226. merchant.setId(id);
  227. merchant.setCOpenid(openid);
  228. merchant.setTeNickName("merchant-" + id);
  229. return merchant;
  230. }
  231. private MerchantApplyFile buildApplyFile(Integer merchantId, String fileType) {
  232. MerchantApplyFile file = new MerchantApplyFile();
  233. file.setMerchantId(merchantId);
  234. file.setFileType(fileType);
  235. return file;
  236. }
  237. private MerchantApplyFileDto buildFile(String fileType, String fileName, String fileUrl) {
  238. MerchantApplyFileDto file = new MerchantApplyFileDto();
  239. file.setFileType(fileType);
  240. file.setFileName(fileName);
  241. file.setFileUrl(fileUrl);
  242. file.setFileSize(1024L);
  243. file.setContentType("image/jpeg");
  244. return file;
  245. }
  246. private MerchantApplyFileDto buildFileGroup(String fileType, MerchantApplyFileDto... files) {
  247. MerchantApplyFileDto group = new MerchantApplyFileDto();
  248. group.setFileType(fileType);
  249. group.setFiles(Arrays.asList(files));
  250. return group;
  251. }
  252. }