刘光辉
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
package jnpf.dmsService;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import jnpf.dmsEntity.DmsJiluDengjiPeizhi;
import jnpf.dmsMapper.DmsJiluDengjiPeizhiMapper;
import jnpf.dmsPermission.DmsCurrentIdentity;
import jnpf.exception.DataException;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
 
class DmsJiluShuxingServiceTest {
    private final DmsJiluDengjiPeizhiMapper mapper = mock(DmsJiluDengjiPeizhiMapper.class);
    private final DmsCurrentIdentity identity = mock(DmsCurrentIdentity.class);
    private final DmsJiluShuxingService service = new DmsJiluShuxingService(mapper, identity, new ObjectMapper());
    private DmsJiluDengjiPeizhi config(String id, String grades, String control) {
        DmsJiluDengjiPeizhi config = new DmsJiluDengjiPeizhi();
        config.setId(id); config.setVersion(1); config.setJiluLeixing("benJilu");
        config.setJiluDengji(grades); config.setShifouShoukong(control);
        return config;
    }
    private void rows(DmsJiluDengjiPeizhi... configs) {
        when(identity.tenantId()).thenReturn("tenant-a");
        when(mapper.selectEnabledByType("tenant-a", "benJilu")).thenReturn(List.of(configs));
    }
    @Test void exactGradeAndTrustedTenantDetermineConfiguration() {
        rows(config("a", "[\"A\"]", "yes"), config("b", "[\"B\",\"C\"]", "no"));
        assertEquals("no", service.resolve("benJilu", "B").getShifouShoukong());
        assertEquals("no", service.resolve("benJilu", "NA").getShifouShoukong());
        verify(mapper, times(2)).selectEnabledByType("tenant-a", "benJilu");
    }
    @Test void overlappingMatchesAreRejectedEvenWithSameResult() {
        rows(config("a", "[\"A\"]", "yes"), config("b", "[\"A\",\"B\"]", "yes"));
        assertThrows(DataException.class, () -> service.resolve("benJilu", "A"));
    }
    @Test void missingGradeDefaultsToUncontrolledWithoutInventingConfiguration() {
        when(identity.tenantId()).thenReturn("tenant-a");
        when(mapper.selectEnabledByType("tenant-a", "pishengchanJilu"))
                .thenReturn(List.of(config("a", "[\"A\"]", "yes")));
        DmsJiluDengjiPeizhi result = service.resolve("pishengchanJilu", "B");
        assertEquals("no", result.getShifouShoukong());
        assertEquals("pishengchanJilu", result.getJiluLeixing());
        assertEquals("[\"B\"]", result.getJiluDengji());
        assertNull(result.getId());
        assertNull(result.getVersion());
    }
    @Test void missingTypeConfigurationDefaultsButIncompleteSelectionIsRejected() {
        rows();
        assertEquals("no", service.resolve("benJilu", "B").getShifouShoukong());
        assertThrows(DataException.class, () -> service.resolve("benJilu", " "));
        assertThrows(DataException.class, () -> service.resolve(null, "B"));
    }
    @Test void malformedGradesAndControlAreRejected() {
        rows(config("a", "[\"A\",1]", "yes"));
        assertThrows(DataException.class, () -> service.resolve("benJilu", "A"));
        assertThrows(DataException.class, () -> service.resolve("benJilu", "B"));
        rows(config("a", "[\"A\"]", "invalid"));
        assertThrows(DataException.class, () -> service.resolve("benJilu", "A"));
        DmsJiluDengjiPeizhi missingVersion = config("a", "[\"A\"]", "no");
        missingVersion.setVersion(null);
        rows(missingVersion);
        assertThrows(DataException.class, () -> service.resolve("benJilu", "A"));
    }
}