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.lang.reflect.Field; import java.util.Arrays; import java.util.Collections; import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotEquals; 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.times; 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> 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("1", "new-image.jpg", "https://file/new-image.jpg"); MerchantApplyFileDto insertFile = buildFile("2", "life.jpg", "https://file/life.jpg"); request.setReq(Arrays.asList(updateFile, insertFile)); service.updateTechnician(request); ArgumentCaptor 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 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 merchantApplyFileDtoDoesNotExposeMerchantIdInRequestPayload() { for (Field field : MerchantApplyFileDto.class.getDeclaredFields()) { assertNotEquals("merchantId", field.getName()); } } @Test public void applyFileReplacesRequestedTypesAndInsertsAllFilesInGroups() { 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.setTeNickName("新昵称"); technician.setTeBrief("新简介"); request.setTechnician(technician); request.setReq(Arrays.asList( buildFileGroup("1", buildFile(null, "portrait-1.jpg", "https://file/portrait-1.jpg"), buildFile(null, "portrait-2.jpg", "https://file/portrait-2.jpg")), buildFileGroup("3", buildFile(null, "video.mp4", "https://file/video.mp4")) )); service.applyFile(request); verify(merchantApplyFileMapper, times(2)).delete(any(LambdaQueryWrapper.class)); ArgumentCaptor insertCaptor = ArgumentCaptor.forClass(MerchantApplyFile.class); verify(merchantApplyFileMapper, times(3)).insert(insertCaptor.capture()); List insertedFiles = insertCaptor.getAllValues(); assertEquals("1", insertedFiles.get(0).getFileType()); assertEquals("portrait-1.jpg", insertedFiles.get(0).getFileName()); assertEquals("1", insertedFiles.get(1).getFileType()); assertEquals("portrait-2.jpg", insertedFiles.get(1).getFileName()); assertEquals("3", insertedFiles.get(2).getFileType()); assertEquals("video.mp4", insertedFiles.get(2).getFileName()); insertedFiles.forEach(file -> { assertEquals(7, file.getMerchantId()); assertEquals("7", file.getCreateBy()); assertEquals("7", file.getUpdateBy()); assertEquals(0, file.getIsDelete()); }); } @Test public void applyFileUsesTechnicianIdWhenReqHasNoMerchantId() { 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.setTeNickName("新昵称"); technician.setTeBrief("新简介"); request.setTechnician(technician); request.setReq(Collections.singletonList( buildFileGroup("2", buildFile(null, "life.jpg", "https://file/life.jpg")) )); service.applyFile(request); ArgumentCaptor 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("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> 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(String fileType, String fileName, String fileUrl) { MerchantApplyFileDto file = new MerchantApplyFileDto(); file.setFileType(fileType); file.setFileName(fileName); file.setFileUrl(fileUrl); file.setFileSize(1024L); file.setContentType("image/jpeg"); return file; } private MerchantApplyFileDto buildFileGroup(String fileType, MerchantApplyFileDto... files) { MerchantApplyFileDto group = new MerchantApplyFileDto(); group.setFileType(fileType); group.setFiles(Arrays.asList(files)); return group; } }