刘光辉
4 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package jnpf.dmsService;
 
import jnpf.dmsEntity.permission.entity.DmsDocumentEntity;
import jnpf.dmsEntity.permission.entity.DmsFileVersionEntity;
import jnpf.dmsEntity.storage.form.DmsFileLifecycleUpdateForm;
import jnpf.dmsEntity.storage.vo.DmsFileLifecycleUpdateVO;
import jnpf.dmsIntegration.audit.DmsVersionStateAuditRow;
import jnpf.dmsMapper.DmsDocumentMapper;
import jnpf.dmsMapper.DmsFileVersionMapper;
import jnpf.dmsMapper.DmsVersionStateMapper;
import jnpf.exception.DataException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
 
import java.util.Date;
import java.util.UUID;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
 
class DmsFileLifecycleServiceTest {
    private static final String TENANT = "tenant-1";
    private static final String SERVICE_ID = "jnpf-file";
    private static final UUID VERSION = UUID.fromString("018f0000-0000-7000-8000-000000000901");
    private static final UUID DOCUMENT = UUID.fromString("018f0000-0000-7000-8000-000000000902");
    private static final Date EFFECTIVE_FROM = new Date(1726329600000L);
    private static final Date EFFECTIVE_TO = new Date(1757865600000L);
 
    private final DmsFileVersionMapper versions = mock(DmsFileVersionMapper.class);
    private final DmsDocumentMapper documents = mock(DmsDocumentMapper.class);
    private final DmsVersionStateMapper states = mock(DmsVersionStateMapper.class);
    private final jnpf.dmsService.expiry.DmsFileExpiryProjection projection = mock(jnpf.dmsService.expiry.DmsFileExpiryProjection.class);
    private final DmsBusinessLifecycleGuard businessOccupancy = mock(DmsBusinessLifecycleGuard.class);
    private final DmsFileLifecycleService service = new DmsFileLifecycleService(
            versions, documents, states, projection, businessOccupancy);
    private DmsFileLifecycleUpdateForm form;
    private DmsVersionStateAuditRow state;
 
    @BeforeEach
    void setUp() {
        when(businessOccupancy.tryEnterExpiry(TENANT, VERSION)).thenReturn(true);
        form = new DmsFileLifecycleUpdateForm();
        form.setTenantId(" tenant-1 ");
        form.setFileVersionId(VERSION);
        form.setLifecycleStatus("EXPIRED");
        form.setEffectiveFrom(EFFECTIVE_FROM);
        form.setEffectiveTo(EFFECTIVE_TO);
        form.setStatusReason(" 到期检查确认 ");
 
        DmsFileVersionEntity version = new DmsFileVersionEntity();
        version.setDocumentId(DOCUMENT);
        when(versions.selectByTenantAndId(TENANT, VERSION)).thenReturn(version);
        when(documents.selectByTenantAndIdForUpdate(TENANT, DOCUMENT))
                .thenReturn(new DmsDocumentEntity());
        state = new DmsVersionStateAuditRow();
        state.setLifecycleStatus("EFFECTIVE");
        state.setEffectiveFrom(EFFECTIVE_FROM);
        state.setEffectiveTo(EFFECTIVE_TO);
        when(states.selectAuditState(TENANT, VERSION)).thenReturn(state);
        when(states.expire(TENANT, VERSION,
                "到期检查确认", SERVICE_ID)).thenReturn(1);
    }
 
    @Test
    void occupiedFileWaitsWithoutTakingDocumentLockOrChangingDates() {
        when(businessOccupancy.tryEnterExpiry(TENANT, VERSION)).thenReturn(false);
        assertThrows(DataException.class, () -> service.updateExpired(form, SERVICE_ID));
        verifyNoInteractions(versions, documents, states, projection);
    }
 
    @Test
    void expiresEffectiveFileAndReturnsPersistedValues() {
        DmsFileLifecycleUpdateVO result = service.updateExpired(form, SERVICE_ID);
 
        assertEquals(VERSION, result.getFileVersionId());
        assertEquals("EXPIRED", result.getLifecycleStatus());
        assertSame(EFFECTIVE_FROM, result.getEffectiveFrom());
        assertSame(EFFECTIVE_TO, result.getEffectiveTo());
        verify(documents).selectByTenantAndIdForUpdate(TENANT, DOCUMENT);
        verify(states).expire(TENANT, VERSION,
                "到期检查确认", SERVICE_ID);
        verify(projection).apply(TENANT, VERSION, EFFECTIVE_TO, SERVICE_ID);
    }
 
    @Test
    void sameExpiredRequestIsIdempotent() {
        state.setLifecycleStatus("EXPIRED");
        state.setEffectiveTo(EFFECTIVE_TO);
 
        DmsFileLifecycleUpdateVO result = service.updateExpired(form, SERVICE_ID);
 
        assertEquals("EXPIRED", result.getLifecycleStatus());
        verify(projection).apply(TENANT, VERSION, EFFECTIVE_TO, SERVICE_ID);
        verify(states, never()).expire(TENANT, VERSION,
                "到期检查确认", SERVICE_ID);
    }
 
    @Test
    void rejectsRetryWithDifferentLifecycleTimes() {
        state.setLifecycleStatus("EXPIRED");
        state.setEffectiveTo(new Date(EFFECTIVE_TO.getTime() - 1000L));
 
        assertThrows(DataException.class, () -> service.updateExpired(form, SERVICE_ID));
        verify(states, never()).expire(TENANT, VERSION,
                "到期检查确认", SERVICE_ID);
    }
 
    @Test
    void databaseControlsDueBoundaryAndPreventsProjectionWhenUpdateLosesRace() {
        when(states.expire(TENANT, VERSION, "到期检查确认", SERVICE_ID)).thenReturn(0);
        assertThrows(DataException.class, () -> service.updateExpired(form, SERVICE_ID));
        verifyNoInteractions(projection);
    }
 
    @Test
    void rejectsUnlimitedVersions() {
        state.setEffectiveTo(null);
        assertThrows(DataException.class, () -> service.updateExpired(form, SERVICE_ID));
        verifyNoInteractions(projection);
    }
 
    @Test
    void rejectsNonEffectiveLifecycleState() {
        state.setLifecycleStatus("SUPERSEDED");
 
        assertThrows(DataException.class, () -> service.updateExpired(form, SERVICE_ID));
        verify(states, never()).expire(TENANT, VERSION,
                "到期检查确认", SERVICE_ID);
    }
 
    @Test
    void acceptsMinimalCommandAndNeverOverwritesStartFromLegacyCaller() {
        form.setEffectiveFrom(new Date(0));
        form.setEffectiveTo(null);
        assertEquals(EFFECTIVE_FROM, service.updateExpired(form, SERVICE_ID).getEffectiveFrom());
        verify(projection).apply(TENANT, VERSION, EFFECTIVE_TO, SERVICE_ID);
    }
}