MaTechnicianServiceImplTest.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.util.Arrays;
  19. import java.util.Collections;
  20. import static org.junit.jupiter.api.Assertions.assertEquals;
  21. import static org.junit.jupiter.api.Assertions.assertFalse;
  22. import static org.junit.jupiter.api.Assertions.assertThrows;
  23. import static org.junit.jupiter.api.Assertions.assertTrue;
  24. import static org.mockito.ArgumentMatchers.any;
  25. import static org.mockito.ArgumentMatchers.isNull;
  26. import static org.mockito.Mockito.mock;
  27. import static org.mockito.Mockito.verify;
  28. import static org.mockito.Mockito.when;
  29. public class MaTechnicianServiceImplTest {
  30. @BeforeAll
  31. public static void initMybatisPlusTableInfo() {
  32. MybatisConfiguration configuration = new MybatisConfiguration();
  33. TableInfoHelper.initTableInfo(new MapperBuilderAssistant(configuration, ""), MaTechnician.class);
  34. TableInfoHelper.initTableInfo(new MapperBuilderAssistant(configuration, ""), MerchantApplyFile.class);
  35. }
  36. @Test
  37. public void updateTechnicianOnlyUpdatesNicknameAndBriefForBaseInfo() {
  38. MaTechnicianMapper maTechnicianMapper = mock(MaTechnicianMapper.class);
  39. MerchantApplyFileMapper merchantApplyFileMapper = mock(MerchantApplyFileMapper.class);
  40. when(maTechnicianMapper.update(isNull(), any(LambdaUpdateWrapper.class))).thenReturn(1);
  41. MaTechnicianServiceImpl service = buildService(maTechnicianMapper, merchantApplyFileMapper);
  42. MerchantApplyFileRequestDto request = new MerchantApplyFileRequestDto();
  43. MaTechnician technician = new MaTechnician();
  44. technician.setId(7);
  45. technician.setTeName("不能修改的姓名");
  46. technician.setTePhone("18800000000");
  47. technician.setAvatar("不能修改的形象照");
  48. technician.setTeNickName("新昵称");
  49. technician.setTeBrief("新简介");
  50. request.setTechnician(technician);
  51. service.updateTechnician(request);
  52. ArgumentCaptor<LambdaUpdateWrapper<MaTechnician>> wrapperCaptor = ArgumentCaptor.forClass(LambdaUpdateWrapper.class);
  53. verify(maTechnicianMapper).update(isNull(), wrapperCaptor.capture());
  54. String sqlSet = String.join(",", wrapperCaptor.getValue().getSqlSet());
  55. assertTrue(sqlSet.contains("te_nick_name"));
  56. assertTrue(sqlSet.contains("te_brief"));
  57. assertFalse(sqlSet.contains("te_name"));
  58. assertFalse(sqlSet.contains("te_phone"));
  59. assertFalse(sqlSet.contains("avatar"));
  60. assertFalse(sqlSet.contains("te_avatar"));
  61. }
  62. @Test
  63. public void updateTechnicianUpsertsApplyFilesByMerchantAndFileType() {
  64. MaTechnicianMapper maTechnicianMapper = mock(MaTechnicianMapper.class);
  65. MerchantApplyFileMapper merchantApplyFileMapper = mock(MerchantApplyFileMapper.class);
  66. when(maTechnicianMapper.update(isNull(), any(LambdaUpdateWrapper.class))).thenReturn(1);
  67. MerchantApplyFile existsFile = new MerchantApplyFile();
  68. existsFile.setId(3L);
  69. existsFile.setMerchantId(7);
  70. existsFile.setFileType("1");
  71. when(merchantApplyFileMapper.selectOne(any(LambdaQueryWrapper.class))).thenReturn(existsFile, null);
  72. MaTechnicianServiceImpl service = buildService(maTechnicianMapper, merchantApplyFileMapper);
  73. MerchantApplyFileRequestDto request = new MerchantApplyFileRequestDto();
  74. MaTechnician technician = new MaTechnician();
  75. technician.setId(7);
  76. technician.setTeNickName("新昵称");
  77. technician.setTeBrief("新简介");
  78. request.setTechnician(technician);
  79. MerchantApplyFileDto updateFile = buildFile(7, "1", "new-image.jpg", "https://file/new-image.jpg");
  80. MerchantApplyFileDto insertFile = buildFile(7, "2", "life.jpg", "https://file/life.jpg");
  81. request.setReq(Arrays.asList(updateFile, insertFile));
  82. service.updateTechnician(request);
  83. ArgumentCaptor<MerchantApplyFile> updateCaptor = ArgumentCaptor.forClass(MerchantApplyFile.class);
  84. verify(merchantApplyFileMapper).updateById(updateCaptor.capture());
  85. MerchantApplyFile updatedFile = updateCaptor.getValue();
  86. assertEquals(3L, updatedFile.getId());
  87. assertEquals(7, updatedFile.getMerchantId());
  88. assertEquals("1", updatedFile.getFileType());
  89. assertEquals("new-image.jpg", updatedFile.getFileName());
  90. assertEquals("https://file/new-image.jpg", updatedFile.getFileUrl());
  91. assertEquals("7", updatedFile.getUpdateBy());
  92. assertEquals(0, updatedFile.getIsDelete());
  93. ArgumentCaptor<MerchantApplyFile> insertCaptor = ArgumentCaptor.forClass(MerchantApplyFile.class);
  94. verify(merchantApplyFileMapper).insert(insertCaptor.capture());
  95. MerchantApplyFile insertedFile = insertCaptor.getValue();
  96. assertEquals(7, insertedFile.getMerchantId());
  97. assertEquals("2", insertedFile.getFileType());
  98. assertEquals("life.jpg", insertedFile.getFileName());
  99. assertEquals("https://file/life.jpg", insertedFile.getFileUrl());
  100. assertEquals("7", insertedFile.getCreateBy());
  101. assertEquals("7", insertedFile.getUpdateBy());
  102. assertEquals(0, insertedFile.getIsDelete());
  103. }
  104. @Test
  105. public void getTechnicianInfoFindsMerchantByOpenidWithFiles() {
  106. MaTechnicianMapper maTechnicianMapper = mock(MaTechnicianMapper.class);
  107. MerchantApplyFileMapper merchantApplyFileMapper = mock(MerchantApplyFileMapper.class);
  108. MaTechnician merchant = buildMerchant(7, "openid-7");
  109. MerchantApplyFile file = buildApplyFile(7, "1");
  110. when(maTechnicianMapper.selectOne(any(LambdaQueryWrapper.class))).thenReturn(merchant);
  111. when(merchantApplyFileMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(Collections.singletonList(file));
  112. MaTechnicianServiceImpl service = buildService(maTechnicianMapper, merchantApplyFileMapper);
  113. MerchantAuditFile result = service.getTechnicianInfo("openid-7");
  114. assertEquals(merchant, result.getMerchant());
  115. assertEquals(1, result.getMerchantAuditFile().size());
  116. assertEquals(file, result.getMerchantAuditFile().get(0));
  117. }
  118. @Test
  119. public void getTechnicianListFindsMerchantByUserIdWithFiles() {
  120. MaTechnicianMapper maTechnicianMapper = mock(MaTechnicianMapper.class);
  121. MerchantApplyFileMapper merchantApplyFileMapper = mock(MerchantApplyFileMapper.class);
  122. MaTechnician merchant = buildMerchant(7, "openid-7");
  123. MerchantApplyFile file = buildApplyFile(7, "1");
  124. when(maTechnicianMapper.selectOne(any(LambdaQueryWrapper.class))).thenReturn(merchant);
  125. when(merchantApplyFileMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(Collections.singletonList(file));
  126. MaTechnicianServiceImpl service = buildService(maTechnicianMapper, merchantApplyFileMapper);
  127. MerchantAuditFile result = service.getTechnicianList(7);
  128. assertEquals(merchant, result.getMerchant());
  129. assertEquals(1, result.getMerchantAuditFile().size());
  130. assertEquals(file, result.getMerchantAuditFile().get(0));
  131. ArgumentCaptor<LambdaQueryWrapper<MerchantApplyFile>> fileQueryCaptor = ArgumentCaptor.forClass(LambdaQueryWrapper.class);
  132. verify(merchantApplyFileMapper).selectList(fileQueryCaptor.capture());
  133. String sqlSegment = fileQueryCaptor.getValue().getSqlSegment();
  134. assertTrue(sqlSegment.contains("merchant_id"));
  135. }
  136. @Test
  137. public void getTechnicianInfoRejectsBlankOpenid() {
  138. MaTechnicianMapper maTechnicianMapper = mock(MaTechnicianMapper.class);
  139. MerchantApplyFileMapper merchantApplyFileMapper = mock(MerchantApplyFileMapper.class);
  140. MaTechnicianServiceImpl service = buildService(maTechnicianMapper, merchantApplyFileMapper);
  141. assertThrows(IllegalArgumentException.class, () -> service.getTechnicianInfo(" "));
  142. }
  143. private MaTechnicianServiceImpl buildService(MaTechnicianMapper maTechnicianMapper,
  144. MerchantApplyFileMapper merchantApplyFileMapper) {
  145. MaTechnicianServiceImpl service = new MaTechnicianServiceImpl();
  146. ReflectionTestUtils.setField(service, "maTechnicianMapper", maTechnicianMapper);
  147. ReflectionTestUtils.setField(service, "merchantApplyFileMapper", merchantApplyFileMapper);
  148. return service;
  149. }
  150. private MaTechnician buildMerchant(Integer id, String openid) {
  151. MaTechnician merchant = new MaTechnician();
  152. merchant.setId(id);
  153. merchant.setCOpenid(openid);
  154. merchant.setTeNickName("merchant-" + id);
  155. return merchant;
  156. }
  157. private MerchantApplyFile buildApplyFile(Integer merchantId, String fileType) {
  158. MerchantApplyFile file = new MerchantApplyFile();
  159. file.setMerchantId(merchantId);
  160. file.setFileType(fileType);
  161. return file;
  162. }
  163. private MerchantApplyFileDto buildFile(Integer merchantId, String fileType, String fileName, String fileUrl) {
  164. MerchantApplyFileDto file = new MerchantApplyFileDto();
  165. file.setMerchantId(merchantId);
  166. file.setFileType(fileType);
  167. file.setFileName(fileName);
  168. file.setFileUrl(fileUrl);
  169. file.setFileSize(1024L);
  170. file.setContentType("image/jpeg");
  171. return file;
  172. }
  173. }