刘光辉
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
package jnpf.dmsController.filecenter;
 
import jnpf.dmsController.DmsControllerTestSupport;
import jnpf.dmsEntity.storage.form.DmsFileExtendValidityForm;
import jnpf.dmsService.DmsFileValidityService;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
 
import java.time.Instant;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
 
class DmsFileValidityControllerTest extends DmsControllerTestSupport {
    private final DmsFileValidityService service = mock(DmsFileValidityService.class);
    private final MockMvc mvc = MockMvcBuilders.standaloneSetup(new DmsFileValidityController(service)).build();
 
    @Test
    void acceptsBatchAndParsesEachBeijingTimeWithoutShifting() throws Exception {
        mvc.perform(post("/workflow-files/extend-validity").contentType(MediaType.APPLICATION_JSON)
                .content(request("\"sourceId\":\"flow-1\",\"taskId\":\"task-1\",")))
                .andExpect(status().isOk()).andExpect(jsonPath("$.code").value(200));
 
        ArgumentCaptor<DmsFileExtendValidityForm> form = ArgumentCaptor.forClass(DmsFileExtendValidityForm.class);
        verify(service).extendValidity(form.capture());
        assertEquals("flow-1", form.getValue().getSourceId());
        assertEquals(2, form.getValue().getFiles().size());
        assertEquals(Instant.parse("2027-09-14T15:59:59Z"),
                form.getValue().getFiles().get(0).getEffectiveTo().toInstant());
        assertEquals(Instant.parse("2028-09-14T15:59:59Z"),
                form.getValue().getFiles().get(1).getEffectiveTo().toInstant());
    }
 
    @Test
    void acceptsBatchWithoutWorkflowIdentifiers() throws Exception {
        mvc.perform(post("/workflow-files/extend-validity").contentType(MediaType.APPLICATION_JSON)
                .content(request("")))
                .andExpect(status().isOk());
 
        ArgumentCaptor<DmsFileExtendValidityForm> form = ArgumentCaptor.forClass(DmsFileExtendValidityForm.class);
        verify(service).extendValidity(form.capture());
        assertNull(form.getValue().getSourceId());
        assertNull(form.getValue().getTaskId());
    }
 
    @Test
    void rejectsOnlyOneWorkflowIdentifier() throws Exception {
        mvc.perform(post("/workflow-files/extend-validity").contentType(MediaType.APPLICATION_JSON)
                .content(request("\"sourceId\":\"flow-1\",")))
                .andExpect(status().isBadRequest());
        verifyNoInteractions(service);
    }
 
    @Test
    void rejectsMissingFileList() throws Exception {
        mvc.perform(post("/workflow-files/extend-validity").contentType(MediaType.APPLICATION_JSON)
                .content("{}"))
                .andExpect(status().isBadRequest());
        verifyNoInteractions(service);
    }
 
    @Test
    void rejectsImpossibleCalendarDateInAnyFile() throws Exception {
        mvc.perform(post("/workflow-files/extend-validity").contentType(MediaType.APPLICATION_JSON)
                .content(request("").replace("2028-09-14 23:59:59", "2028-02-30 00:00:00")))
                .andExpect(status().isBadRequest());
        verifyNoInteractions(service);
    }
 
    private String request(String outerFields) {
        return "{" + outerFields + "\"files\":["
                + "{\"fileVersionId\":\"018f0000-0000-7000-8000-000000000803\","
                + "\"effectiveTo\":\"2027-09-14 23:59:59\"},"
                + "{\"fileVersionId\":\"018f0000-0000-7000-8000-000000000804\","
                + "\"effectiveTo\":\"2028-09-14 23:59:59\"}]}";
    }
}