刘光辉
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
package jnpf.dmsService;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import jnpf.permission.UserApi;
import jnpf.permission.entity.UserEntity;
import jnpf.exception.DataException;
import org.junit.jupiter.api.Test;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.mockito.ArgumentMatchers.*;
 
class DmsApprovedVersionPropertiesServiceTest {
    UserApi users = mock(UserApi.class);
    DmsApprovedVersionPropertiesService service = new DmsApprovedVersionPropertiesService(users, new ObjectMapper());
 
    @Test void snapshotsOnlyPersistedReviewersAndDeduplicatesWithoutLosingOrder() {
        when(users.getUserName(anyList())).thenReturn(List.of(user("author", "制定人"), user("a", "审核甲"), user("b", "审核乙")));
        var value = service.create("author", "[\"a\",\"b\",\"a\"]");
        assertEquals("制定人", value.path("prepared_by_name").asText());
        assertEquals(2, value.path("reviewers").size());
        assertEquals("a", value.path("reviewers").get(0).path("user_id").asText());
        assertEquals("审核乙", value.path("reviewers").get(1).path("name").asText());
        verify(users).getUserName(List.of("a", "b", "author"));
    }
 
    @Test void scalarWorkflowOperatorBecomesOneReviewerWithoutNumericConversion() {
        String id = "878193794397329605";
        when(users.getUserName(anyList())).thenReturn(List.of(user(id, "审核人")));
        var value = service.create("author", id);
        assertEquals(1, value.path("reviewers").size());
        assertEquals(id, value.path("reviewers").get(0).path("user_id").asText());
        assertEquals("审核人", value.path("reviewers").get(0).path("name").asText());
        verify(users).getUserName(List.of(id, "author"));
    }
 
    @Test void scalarAndArrayInputsProduceTheSamePublicSnapshot() {
        when(users.getUserName(anyList())).thenReturn(List.of(user("user-a", "审核人")));
        assertEquals(service.create("author", "user-a"), service.create("author", "[\"user-a\"]"));
    }
 
    @Test void emptyFieldDoesNotInventAReviewer() {
        when(users.getUserName(anyList())).thenReturn(List.of(user("author", "制定人")));
        for (String empty : Arrays.asList(null, "", "  ", "[]")) {
            assertTrue(service.create("author", empty).path("reviewers").isEmpty());
        }
    }
 
    @Test void missingDirectoryUserRetainsIdentityWithoutInventingName() {
        when(users.getUserName(anyList())).thenReturn(List.of());
        var reviewer = service.create("author", "[\"deleted\"]").path("reviewers").get(0);
        assertEquals("deleted", reviewer.path("user_id").asText());
        assertEquals("", reviewer.path("name").asText());
    }
 
    @Test void malformedMappingsFailInsteadOfSilentlyDroppingReviewers() {
        for (String invalid : List.of("a,b", "{}", "null", "[null]", "[1]", "[\"\"]", "[{\"id\":\"a\"}]")) {
            assertThrows(DataException.class, () -> service.create("author", invalid), invalid);
        }
        verifyNoInteractions(users);
    }
 
    @Test void nullDirectoryResponsePreservesReviewerIdentity() {
        when(users.getUserName(anyList())).thenReturn(null);
        var value = assertDoesNotThrow(() -> service.create("author", "a"));
        assertEquals("", value.path("prepared_by_name").asText());
        assertEquals("a", value.path("reviewers").get(0).path("user_id").asText());
        assertEquals("", value.path("reviewers").get(0).path("name").asText());
    }
 
    @Test void directoryExceptionDoesNotBlockSnapshotCreationOrLoseReviewerIds() {
        when(users.getUserName(anyList())).thenThrow(new IllegalStateException("directory unavailable"));
        var value = assertDoesNotThrow(() -> service.create("author", "[\"a\",\"b\"]"));
        assertEquals("", value.path("prepared_by_name").asText());
        assertEquals(2, value.path("reviewers").size());
        assertEquals("a", value.path("reviewers").get(0).path("user_id").asText());
        assertEquals("b", value.path("reviewers").get(1).path("user_id").asText());
        assertEquals("", value.path("reviewers").get(0).path("name").asText());
        assertEquals("", value.path("reviewers").get(1).path("name").asText());
    }
 
    @Test void partialDirectoryResponseKeepsNamesFoundAndIdsWithoutNames() {
        when(users.getUserName(anyList())).thenReturn(List.of(user("author", "制定人"), user("a", "审核甲")));
        var value = service.create("author", "[\"a\",\"b\"]");
        assertEquals("制定人", value.path("prepared_by_name").asText());
        assertEquals("审核甲", value.path("reviewers").get(0).path("name").asText());
        assertEquals("b", value.path("reviewers").get(1).path("user_id").asText());
        assertEquals("", value.path("reviewers").get(1).path("name").asText());
    }
 
    UserEntity user(String id, String name) { var u = new UserEntity(); u.setId(id); u.setRealName(name); return u; }
}