shinnosuke
2026-08-13 ea7e8d87259a991c521190d674244cb067c7198d
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
105
106
107
108
109
110
111
112
113
114
package com.talentcard.server.service;
 
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
 
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.talentcard.server.entity.DependentCardApply;
import com.talentcard.server.entity.DependentCardApplyStatusLog;
import com.talentcard.server.mapper.DependentCardApplyMapper;
import com.talentcard.server.mapper.DependentCardApplyStatusLogMapper;
import com.talentcard.server.request.DependentCardApplyRequest;
import com.talentcard.server.response.DependentCardApplyItemResponse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
 
@ExtendWith(MockitoExtension.class)
class DependentCardApplyServiceTests {
 
    @Mock
    private DependentCardApplyMapper mapper;
    @Mock
    private DependentCardApplyStatusLogMapper statusLogMapper;
 
    private DependentCardApplyService service;
 
    @BeforeEach
    void setUp() {
        service = new DependentCardApplyService(mapper, statusLogMapper);
    }
 
    @Test
    void resubmitCreatesANewApplicationAndKeepsRejectedApplicationUnchanged() {
        DependentCardApply rejected = new DependentCardApply(
                "DCA-OLD",
                12L,
                34L,
                "持卡人",
                "13900000000",
                "TC-001",
                "卓越人才卡",
                "旧家属",
                "13800000000",
                "510105199001011234",
                "配偶"
        );
        setStatus(rejected, DependentCardApply.STATUS_REJECTED);
        when(mapper.selectByIdForUpdate(99L)).thenReturn(rejected);
        when(mapper.insert(any(DependentCardApply.class))).thenAnswer(invocation -> {
            setId(invocation.getArgument(0), 100L);
            return 1;
        });
        when(statusLogMapper.insert(any(DependentCardApplyStatusLog.class))).thenReturn(1);
 
        DependentCardApplyItemResponse result = service.resubmit(
                99L,
                new DependentCardApplyRequest(
                        34L,
                        " 新家属 ",
                        "51010519900101123x",
                        "13700000000",
                        " 子女 "
                )
        );
 
        ArgumentCaptor<DependentCardApply> applicationCaptor = ArgumentCaptor.forClass(DependentCardApply.class);
        verify(mapper).insert(applicationCaptor.capture());
        DependentCardApply created = applicationCaptor.getValue();
        assertThat(created.getId()).isEqualTo(100L);
        assertThat(created.getApplyNo()).startsWith("DCA").isNotEqualTo(rejected.getApplyNo());
        assertThat(created.getTalentId()).isEqualTo(rejected.getTalentId());
        assertThat(created.getTalentCardId()).isEqualTo(rejected.getTalentCardId());
        assertThat(created.getDependentName()).isEqualTo("新家属");
        assertThat(created.getDependentIdCardNo()).isEqualTo("51010519900101123X");
        assertThat(created.getDependentMobile()).isEqualTo("13700000000");
        assertThat(created.getRelation()).isEqualTo("子女");
        assertThat(created.getStatus()).isEqualTo(DependentCardApply.STATUS_PENDING_REVIEW);
        assertThat(result.id()).isEqualTo(100L);
        assertThat(result.status()).isEqualTo(DependentCardApply.STATUS_PENDING_REVIEW);
        assertThat(rejected.getStatus()).isEqualTo(DependentCardApply.STATUS_REJECTED);
        verify(mapper, never()).update(any(), any(Wrapper.class));
 
        ArgumentCaptor<DependentCardApplyStatusLog> logCaptor = ArgumentCaptor.forClass(DependentCardApplyStatusLog.class);
        verify(statusLogMapper).insert(logCaptor.capture());
        assertThat(logCaptor.getValue().getApplyId()).isEqualTo(100L);
        assertThat(logCaptor.getValue().getApplyNo()).isEqualTo(created.getApplyNo());
        assertThat(logCaptor.getValue().getFromStatus()).isNull();
        assertThat(logCaptor.getValue().getToStatus()).isEqualTo(DependentCardApply.STATUS_PENDING_REVIEW);
    }
 
    private void setId(DependentCardApply apply, Long id) {
        setField(apply, "id", id);
    }
 
    private void setStatus(DependentCardApply apply, Integer status) {
        setField(apply, "status", status);
    }
 
    private void setField(DependentCardApply apply, String name, Object value) {
        try {
            var field = DependentCardApply.class.getDeclaredField(name);
            field.setAccessible(true);
            field.set(apply, value);
        } catch (ReflectiveOperationException exception) {
            throw new AssertionError(exception);
        }
    }
}