刘光辉
6 小时以前 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
package jnpf.elnService.template.impl;
 
import jnpf.elnEntity.template.ElnTemplateVersionEntity;
import org.junit.Test;
 
import java.util.concurrent.atomic.AtomicReference;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
 
public class ElnTemplateEditabilityServiceImplTest {
 
    @Test
    public void draftEnabledExactTenantTemplateAndFileMatchIsEditable() {
        AtomicReference<String> queriedTenant = new AtomicReference<>();
        AtomicReference<String> queriedTemplate = new AtomicReference<>();
        TestTenantRoute route = new TestTenantRoute();
        ElnTemplateEditabilityServiceImpl service = new ElnTemplateEditabilityServiceImpl(
                (tenantId, templateId) -> {
                    queriedTenant.set(tenantId);
                    queriedTemplate.set(templateId);
                    return template("tenant-a", "template-1", "DRAFT", "enabled", 0,
                            "[{\"fileId\":\"file-1\",\"fileExtension\":\"docx\"}]");
                }, route);
 
        assertTrue(service.isEditable("tenant-a", "template-1", "file-1"));
        assertEquals("tenant-a", queriedTenant.get());
        assertEquals("template-1", queriedTemplate.get());
        assertNull(route.currentTenant);
        assertEquals(1, route.clearCount);
    }
 
    @Test
    public void publishedDisabledDeletedFileMismatchAndTenantMismatchFailClosed() {
        assertFalse(probe(template("tenant-a", "template-1", "PUBLISHED", "enabled", 0,
                docx("file-1")), "tenant-a", "template-1", "file-1"));
        assertFalse(probe(template("tenant-a", "template-1", "DRAFT", "disabled", 0,
                docx("file-1")), "tenant-a", "template-1", "file-1"));
        assertFalse(probe(template("tenant-a", "template-1", "DRAFT", "enabled", 1,
                docx("file-1")), "tenant-a", "template-1", "file-1"));
        assertFalse(probe(template("tenant-a", "template-1", "DRAFT", "enabled", 0,
                docx("file-2")), "tenant-a", "template-1", "file-1"));
        assertFalse(probe(template("tenant-b", "template-1", "DRAFT", "enabled", 0,
                docx("file-1")), "tenant-a", "template-1", "file-1"));
        assertFalse(probe(template("tenant-a", "template-2", "DRAFT", "enabled", 0,
                docx("file-1")), "tenant-a", "template-1", "file-1"));
    }
 
    @Test
    public void malformedOrAmbiguousAttachmentsFailClosedAndClearTenant() {
        assertFalse(probe(template("tenant-a", "template-1", "DRAFT", "enabled", 0,
                "not-json"), "tenant-a", "template-1", "file-1"));
        assertFalse(probe(template("tenant-a", "template-1", "DRAFT", "enabled", 0,
                "[]"), "tenant-a", "template-1", "file-1"));
        assertFalse(probe(template("tenant-a", "template-1", "DRAFT", "enabled", 0,
                "[{\"fileId\":\"file-1\",\"fileExtension\":\"docx\"},"
                        + "{\"fileId\":\"file-2\",\"fileExtension\":\"DOCX\"}]"),
                "tenant-a", "template-1", "file-1"));
    }
 
    @Test
    public void missingParametersQueryAndTenantRoutingFailuresFailClosedAndAlwaysClear() {
        TestTenantRoute missingRoute = new TestTenantRoute();
        ElnTemplateEditabilityServiceImpl missingService = new ElnTemplateEditabilityServiceImpl(
                (tenantId, templateId) -> {
                    throw new AssertionError("missing parameters must not query");
                }, missingRoute);
        assertFalse(missingService.isEditable(" ", "template-1", "file-1"));
        assertEquals(1, missingRoute.clearCount);
 
        TestTenantRoute queryRoute = new TestTenantRoute();
        ElnTemplateEditabilityServiceImpl queryFailure = new ElnTemplateEditabilityServiceImpl(
                (tenantId, templateId) -> {
                    throw new IllegalStateException("query failed");
                }, queryRoute);
        assertFalse(queryFailure.isEditable("tenant-a", "template-1", "file-1"));
        assertNull(queryRoute.currentTenant);
        assertEquals(1, queryRoute.clearCount);
 
        TestTenantRoute switchFailureRoute = new TestTenantRoute();
        switchFailureRoute.switchFailure = new IllegalStateException("route failed");
        ElnTemplateEditabilityServiceImpl switchFailure = new ElnTemplateEditabilityServiceImpl(
                (tenantId, templateId) -> {
                    throw new AssertionError("failed routing must not query");
                }, switchFailureRoute);
        assertFalse(switchFailure.isEditable("tenant-a", "template-1", "file-1"));
        assertNull(switchFailureRoute.currentTenant);
        assertEquals(1, switchFailureRoute.clearCount);
    }
 
    private boolean probe(ElnTemplateVersionEntity template, String tenantId,
                          String templateId, String fileId) {
        TestTenantRoute route = new TestTenantRoute();
        ElnTemplateEditabilityServiceImpl service = new ElnTemplateEditabilityServiceImpl(
                (ignoredTenant, ignoredTemplate) -> template, route);
        boolean result = service.isEditable(tenantId, templateId, fileId);
        assertNull(route.currentTenant);
        assertEquals(1, route.clearCount);
        return result;
    }
 
    private static String docx(String fileId) {
        return "[{\"fileId\":\"" + fileId + "\",\"fileExtension\":\"docx\"}]";
    }
 
    private static ElnTemplateVersionEntity template(String tenantId, String templateId,
                                                       String status, String enabled,
                                                       Integer deleteMark, String files) {
        ElnTemplateVersionEntity template = new ElnTemplateVersionEntity();
        template.setFTenantId(tenantId);
        template.setId(templateId);
        template.setBizStatus(status);
        template.setBizEnabled(enabled);
        template.setFDeleteMark(deleteMark);
        template.setMubanFile(files);
        return template;
    }
 
    private static final class TestTenantRoute implements ElnTemplateEditabilityServiceImpl.TenantRoute {
        private String currentTenant;
        private int clearCount;
        private RuntimeException switchFailure;
 
        @Override
        public void switchTo(String tenantId) {
            if (switchFailure != null) {
                throw switchFailure;
            }
            currentTenant = tenantId;
        }
 
        @Override
        public void clear() {
            currentTenant = null;
            clearCount++;
        }
    }
}