| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- package com.ylx.massage.service.impl;
- import com.baomidou.mybatisplus.core.MybatisConfiguration;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
- import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
- import com.ylx.massage.domain.MaTechnician;
- import com.ylx.massage.domain.MerchantApplyFile;
- import com.ylx.massage.domain.dto.MerchantApplyFileDto;
- import com.ylx.massage.domain.dto.MerchantApplyFileRequestDto;
- import com.ylx.massage.domain.vo.MerchantAuditFile;
- import com.ylx.massage.mapper.MaTechnicianMapper;
- import com.ylx.massage.mapper.MerchantApplyFileMapper;
- import org.apache.ibatis.builder.MapperBuilderAssistant;
- import org.junit.jupiter.api.BeforeAll;
- import org.junit.jupiter.api.Test;
- import org.mockito.ArgumentCaptor;
- import org.springframework.test.util.ReflectionTestUtils;
- import java.util.Arrays;
- import java.util.Collections;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import static org.junit.jupiter.api.Assertions.assertFalse;
- import static org.junit.jupiter.api.Assertions.assertThrows;
- import static org.junit.jupiter.api.Assertions.assertTrue;
- import static org.mockito.ArgumentMatchers.any;
- import static org.mockito.ArgumentMatchers.isNull;
- import static org.mockito.Mockito.mock;
- import static org.mockito.Mockito.verify;
- import static org.mockito.Mockito.when;
- public class MaTechnicianServiceImplTest {
- @BeforeAll
- public static void initMybatisPlusTableInfo() {
- MybatisConfiguration configuration = new MybatisConfiguration();
- TableInfoHelper.initTableInfo(new MapperBuilderAssistant(configuration, ""), MaTechnician.class);
- TableInfoHelper.initTableInfo(new MapperBuilderAssistant(configuration, ""), MerchantApplyFile.class);
- }
- @Test
- public void updateTechnicianOnlyUpdatesNicknameAndBriefForBaseInfo() {
- MaTechnicianMapper maTechnicianMapper = mock(MaTechnicianMapper.class);
- MerchantApplyFileMapper merchantApplyFileMapper = mock(MerchantApplyFileMapper.class);
- when(maTechnicianMapper.update(isNull(), any(LambdaUpdateWrapper.class))).thenReturn(1);
- MaTechnicianServiceImpl service = buildService(maTechnicianMapper, merchantApplyFileMapper);
- MerchantApplyFileRequestDto request = new MerchantApplyFileRequestDto();
- MaTechnician technician = new MaTechnician();
- technician.setId(7);
- technician.setTeName("不能修改的姓名");
- technician.setTePhone("18800000000");
- technician.setAvatar("不能修改的形象照");
- technician.setTeNickName("新昵称");
- technician.setTeBrief("新简介");
- request.setTechnician(technician);
- service.updateTechnician(request);
- ArgumentCaptor<LambdaUpdateWrapper<MaTechnician>> wrapperCaptor = ArgumentCaptor.forClass(LambdaUpdateWrapper.class);
- verify(maTechnicianMapper).update(isNull(), wrapperCaptor.capture());
- String sqlSet = String.join(",", wrapperCaptor.getValue().getSqlSet());
- assertTrue(sqlSet.contains("te_nick_name"));
- assertTrue(sqlSet.contains("te_brief"));
- assertFalse(sqlSet.contains("te_name"));
- assertFalse(sqlSet.contains("te_phone"));
- assertFalse(sqlSet.contains("avatar"));
- assertFalse(sqlSet.contains("te_avatar"));
- }
- @Test
- public void updateTechnicianUpsertsApplyFilesByMerchantAndFileType() {
- MaTechnicianMapper maTechnicianMapper = mock(MaTechnicianMapper.class);
- MerchantApplyFileMapper merchantApplyFileMapper = mock(MerchantApplyFileMapper.class);
- when(maTechnicianMapper.update(isNull(), any(LambdaUpdateWrapper.class))).thenReturn(1);
- MerchantApplyFile existsFile = new MerchantApplyFile();
- existsFile.setId(3L);
- existsFile.setMerchantId(7);
- existsFile.setFileType("1");
- when(merchantApplyFileMapper.selectOne(any(LambdaQueryWrapper.class))).thenReturn(existsFile, null);
- MaTechnicianServiceImpl service = buildService(maTechnicianMapper, merchantApplyFileMapper);
- MerchantApplyFileRequestDto request = new MerchantApplyFileRequestDto();
- MaTechnician technician = new MaTechnician();
- technician.setId(7);
- technician.setTeNickName("新昵称");
- technician.setTeBrief("新简介");
- request.setTechnician(technician);
- MerchantApplyFileDto updateFile = buildFile(7, "1", "new-image.jpg", "https://file/new-image.jpg");
- MerchantApplyFileDto insertFile = buildFile(7, "2", "life.jpg", "https://file/life.jpg");
- request.setReq(Arrays.asList(updateFile, insertFile));
- service.updateTechnician(request);
- ArgumentCaptor<MerchantApplyFile> updateCaptor = ArgumentCaptor.forClass(MerchantApplyFile.class);
- verify(merchantApplyFileMapper).updateById(updateCaptor.capture());
- MerchantApplyFile updatedFile = updateCaptor.getValue();
- assertEquals(3L, updatedFile.getId());
- assertEquals(7, updatedFile.getMerchantId());
- assertEquals("1", updatedFile.getFileType());
- assertEquals("new-image.jpg", updatedFile.getFileName());
- assertEquals("https://file/new-image.jpg", updatedFile.getFileUrl());
- assertEquals("7", updatedFile.getUpdateBy());
- assertEquals(0, updatedFile.getIsDelete());
- ArgumentCaptor<MerchantApplyFile> insertCaptor = ArgumentCaptor.forClass(MerchantApplyFile.class);
- verify(merchantApplyFileMapper).insert(insertCaptor.capture());
- MerchantApplyFile insertedFile = insertCaptor.getValue();
- assertEquals(7, insertedFile.getMerchantId());
- assertEquals("2", insertedFile.getFileType());
- assertEquals("life.jpg", insertedFile.getFileName());
- assertEquals("https://file/life.jpg", insertedFile.getFileUrl());
- assertEquals("7", insertedFile.getCreateBy());
- assertEquals("7", insertedFile.getUpdateBy());
- assertEquals(0, insertedFile.getIsDelete());
- }
- @Test
- public void getTechnicianInfoFindsMerchantByOpenidWithFiles() {
- MaTechnicianMapper maTechnicianMapper = mock(MaTechnicianMapper.class);
- MerchantApplyFileMapper merchantApplyFileMapper = mock(MerchantApplyFileMapper.class);
- MaTechnician merchant = buildMerchant(7, "openid-7");
- MerchantApplyFile file = buildApplyFile(7, "1");
- when(maTechnicianMapper.selectOne(any(LambdaQueryWrapper.class))).thenReturn(merchant);
- when(merchantApplyFileMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(Collections.singletonList(file));
- MaTechnicianServiceImpl service = buildService(maTechnicianMapper, merchantApplyFileMapper);
- MerchantAuditFile result = service.getTechnicianInfo("openid-7");
- assertEquals(merchant, result.getMerchant());
- assertEquals(1, result.getMerchantAuditFile().size());
- assertEquals(file, result.getMerchantAuditFile().get(0));
- }
- @Test
- public void getTechnicianListFindsMerchantByUserIdWithFiles() {
- MaTechnicianMapper maTechnicianMapper = mock(MaTechnicianMapper.class);
- MerchantApplyFileMapper merchantApplyFileMapper = mock(MerchantApplyFileMapper.class);
- MaTechnician merchant = buildMerchant(7, "openid-7");
- MerchantApplyFile file = buildApplyFile(7, "1");
- when(maTechnicianMapper.selectOne(any(LambdaQueryWrapper.class))).thenReturn(merchant);
- when(merchantApplyFileMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(Collections.singletonList(file));
- MaTechnicianServiceImpl service = buildService(maTechnicianMapper, merchantApplyFileMapper);
- MerchantAuditFile result = service.getTechnicianList(7);
- assertEquals(merchant, result.getMerchant());
- assertEquals(1, result.getMerchantAuditFile().size());
- assertEquals(file, result.getMerchantAuditFile().get(0));
- ArgumentCaptor<LambdaQueryWrapper<MerchantApplyFile>> fileQueryCaptor = ArgumentCaptor.forClass(LambdaQueryWrapper.class);
- verify(merchantApplyFileMapper).selectList(fileQueryCaptor.capture());
- String sqlSegment = fileQueryCaptor.getValue().getSqlSegment();
- assertTrue(sqlSegment.contains("merchant_id"));
- }
- @Test
- public void getTechnicianInfoRejectsBlankOpenid() {
- MaTechnicianMapper maTechnicianMapper = mock(MaTechnicianMapper.class);
- MerchantApplyFileMapper merchantApplyFileMapper = mock(MerchantApplyFileMapper.class);
- MaTechnicianServiceImpl service = buildService(maTechnicianMapper, merchantApplyFileMapper);
- assertThrows(IllegalArgumentException.class, () -> service.getTechnicianInfo(" "));
- }
- private MaTechnicianServiceImpl buildService(MaTechnicianMapper maTechnicianMapper,
- MerchantApplyFileMapper merchantApplyFileMapper) {
- MaTechnicianServiceImpl service = new MaTechnicianServiceImpl();
- ReflectionTestUtils.setField(service, "maTechnicianMapper", maTechnicianMapper);
- ReflectionTestUtils.setField(service, "merchantApplyFileMapper", merchantApplyFileMapper);
- return service;
- }
- private MaTechnician buildMerchant(Integer id, String openid) {
- MaTechnician merchant = new MaTechnician();
- merchant.setId(id);
- merchant.setCOpenid(openid);
- merchant.setTeNickName("merchant-" + id);
- return merchant;
- }
- private MerchantApplyFile buildApplyFile(Integer merchantId, String fileType) {
- MerchantApplyFile file = new MerchantApplyFile();
- file.setMerchantId(merchantId);
- file.setFileType(fileType);
- return file;
- }
- private MerchantApplyFileDto buildFile(Integer merchantId, String fileType, String fileName, String fileUrl) {
- MerchantApplyFileDto file = new MerchantApplyFileDto();
- file.setMerchantId(merchantId);
- file.setFileType(fileType);
- file.setFileName(fileName);
- file.setFileUrl(fileUrl);
- file.setFileSize(1024L);
- file.setContentType("image/jpeg");
- return file;
- }
- }
|