xiaoyi
3 天以前 8782476e9cb4480be5da588888d08022b7942d0c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package cn.iocoder.yudao.module.aftersales.service.returnobj;
 
import cn.iocoder.yudao.module.aftersales.dal.dataobject.returnobj.AfterSaleReturnDO;
import cn.iocoder.yudao.module.aftersales.controller.admin.returnobj.vo.AfterSaleReturnSaveReqVO;
import cn.iocoder.yudao.module.aftersales.dal.dataobject.returnobj.AfterSaleReturnItemDO;
import cn.iocoder.yudao.module.aftersales.dal.mysql.returnobj.AfterSaleReturnMapper;
import cn.iocoder.yudao.module.aftersales.dal.mysql.returnobj.AfterSaleReturnItemMapper;
import cn.iocoder.yudao.module.aftersales.dal.mysql.ticket.AfterSaleTicketMapper;
import cn.iocoder.yudao.module.aftersales.dal.dataobject.ticket.AfterSaleTicketDO;
import cn.iocoder.yudao.module.aftersales.dal.redis.no.AfterSaleNoRedisDAO;
import cn.iocoder.yudao.module.aftersales.service.permission.AfterSalePermissionService;
import cn.iocoder.yudao.module.mes.api.returnsales.MesWmReturnSalesApi;
import cn.iocoder.yudao.module.mes.api.returnsales.dto.MesWmReturnSalesLineCreateDTO;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.test.util.ReflectionTestUtils;
import java.math.BigDecimal;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
 
class AfterSaleReturnMesLinkTest {
    @Test
    void refundOnlyDoesNotCreatePhysicalReturnDraft() {
        var mapper = mock(AfterSaleReturnMapper.class);
        var service = new AfterSaleReturnServiceImpl();
        ReflectionTestUtils.setField(service, "returnMapper", mapper);
        var tickets = mock(AfterSaleTicketMapper.class);
        when(tickets.selectById(8L)).thenReturn(new AfterSaleTicketDO().setId(8L));
        ReflectionTestUtils.setField(service, "ticketMapper", tickets);
        ReflectionTestUtils.setField(service, "noRedisDAO", mock(AfterSaleNoRedisDAO.class));
        ReflectionTestUtils.setField(service, "permissionService", mock(AfterSalePermissionService.class));
        var input = new AfterSaleReturnSaveReqVO(); input.setReturnType(3); input.setTicketId(8L);
        assertThrows(RuntimeException.class, () -> service.createReturn(input, 1L));
        verifyNoInteractions(mapper);
    }
    @Test
    void noPhysicalLinesDoesNotCreateMesDocument() {
        var mapper = mock(AfterSaleReturnMapper.class);
        var items = mock(AfterSaleReturnItemMapper.class);
        var api = mock(MesWmReturnSalesApi.class);
        when(mapper.selectById(1L)).thenReturn(new AfterSaleReturnDO().setId(1L).setStatus(0).setReturnType(1));
        when(items.selectListByReturnId(1L)).thenReturn(List.of());
        var service = new AfterSaleReturnServiceImpl();
        ReflectionTestUtils.setField(service, "returnMapper", mapper);
        ReflectionTestUtils.setField(service, "returnItemMapper", items);
        ReflectionTestUtils.setField(service, "mesWmReturnSalesApi", api);
        assertThrows(RuntimeException.class, () -> service.approveReturn(1L));
        verifyNoInteractions(api);
    }
 
    @Test
    void lostApprovalRaceDoesNotCreateSecondMesDocument() {
        TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), AfterSaleReturnDO.class);
        var mapper = mock(AfterSaleReturnMapper.class);
        var items = mock(AfterSaleReturnItemMapper.class);
        var api = mock(MesWmReturnSalesApi.class);
        when(mapper.selectById(1L)).thenReturn(new AfterSaleReturnDO().setId(1L).setStatus(0).setReturnType(1));
        var line = new AfterSaleReturnItemDO(); line.setItemId(4L); line.setReturnQuantity(BigDecimal.ONE);
        when(items.selectListByReturnId(1L)).thenReturn(List.of(line));
        when(mapper.update(any(), any())).thenReturn(0);
        var service = new AfterSaleReturnServiceImpl();
        ReflectionTestUtils.setField(service, "returnMapper", mapper);
        ReflectionTestUtils.setField(service, "returnItemMapper", items);
        ReflectionTestUtils.setField(service, "mesWmReturnSalesApi", api);
        assertThrows(RuntimeException.class, () -> service.approveReturn(1L));
        verifyNoInteractions(api);
    }
    @Test
    void approvalCopiesReturnLinesAndPersistsTheirMesIds() {
        TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), AfterSaleReturnDO.class);
        var mapper = mock(AfterSaleReturnMapper.class);
        var items = mock(AfterSaleReturnItemMapper.class);
        var api = mock(MesWmReturnSalesApi.class);
        var source = new AfterSaleReturnDO().setId(1L).setTicketId(2L).setStatus(0).setNo("RET-1").setReturnType(1);
        when(mapper.selectById(1L)).thenReturn(source);
        when(mapper.update(any(), any())).thenReturn(1);
        when(mapper.updateById(any(AfterSaleReturnDO.class))).thenReturn(1);
        when(items.updateById(any(AfterSaleReturnItemDO.class))).thenReturn(1);
        var line = new AfterSaleReturnItemDO(); line.setId(3L); line.setReturnId(1L); line.setItemId(4L);
        line.setReturnQuantity(new BigDecimal("2.50")); line.setBatchId(5L); line.setBatchCode("B-1");
        when(items.selectListByReturnId(1L)).thenReturn(List.of(line));
        when(api.createReturnSales(any())).thenReturn(6L);
        when(api.createReturnSalesLine(any())).thenReturn(7L);
        var service = new AfterSaleReturnServiceImpl();
        ReflectionTestUtils.setField(service, "returnMapper", mapper);
        ReflectionTestUtils.setField(service, "returnItemMapper", items);
        ReflectionTestUtils.setField(service, "ticketMapper", mock(AfterSaleTicketMapper.class));
        ReflectionTestUtils.setField(service, "mesWmReturnSalesApi", api);
        service.approveReturn(1L);
        var capture = ArgumentCaptor.forClass(MesWmReturnSalesLineCreateDTO.class);
        verify(api).createReturnSalesLine(capture.capture());
        assertEquals(6L, capture.getValue().getReturnId());
        assertEquals(4L, capture.getValue().getItemId());
        assertEquals(new BigDecimal("2.50"), capture.getValue().getQuantity());
        assertEquals(5L, capture.getValue().getBatchId());
        var updated = ArgumentCaptor.forClass(AfterSaleReturnItemDO.class);
        verify(items).updateById(updated.capture());
        assertEquals(7L, updated.getValue().getMesReturnLineId());
    }
}