刘光辉
昨天 bb638871a7fb692d80f1b7a758f991dc0879002c
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.bizcommon.onlyoffice.service.impl;
 
import jnpf.bizcommon.onlyoffice.entity.OnlyOfficeConfigParam;
import jnpf.bizcommon.onlyoffice.entity.OnlyOfficeProperties;
import jnpf.exception.DataException;
import org.junit.Before;
import org.junit.Test;
 
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
 
public class OnlyOfficePluginResolverTest {
 
    private OnlyOfficePluginResolver resolver;
 
    @Before
    public void setUp() {
        OnlyOfficeProperties properties = new OnlyOfficeProperties();
        OnlyOfficeProperties.PluginDefinition definition = definition(
                "asc.{2D6D6CB6-F6FC-45B7-8EC7-0CC1940F1A6F}",
                "http://localhost:4173/onlyoffice-plugins/", "0.1.0-dev", "http://localhost:30000");
        Map<String, OnlyOfficeProperties.PluginDefinition> definitions = new LinkedHashMap<>();
        definitions.put("eln-template-annotator", definition);
        properties.getPlugins().setDefinitions(definitions);
 
        OnlyOfficeProperties.PluginSceneMapping mapping = new OnlyOfficeProperties.PluginSceneMapping();
        mapping.setBizScene("eln.template.annotate");
        mapping.setBizModule("eln.template");
        mapping.setPluginCodes(Collections.singletonList("eln-template-annotator"));
        mapping.setAutostartPluginCode("eln-template-annotator");
        mapping.setDisabledPluginGuids(Collections.singletonList(
                "asc.{AA2EA9B6-9EC2-415F-9762-634EE8D9A95E}"));
        properties.getPlugins().setScenes(Collections.singletonList(mapping));
        resolver = new OnlyOfficePluginResolver(properties);
    }
 
    @Test
    public void emptySceneDoesNotResolvePlugin() {
        assertEquals(Collections.emptyList(), resolver.resolve(param(null, null, null)));
    }
 
    @Test
    public void validSceneBuildsManifestAndRuntimeContext() {
        OnlyOfficePluginResolver.ResolvedPlugin resolved = resolver.resolve(
                param("eln.template.annotate", "eln.template", "record-1")).get(0);
 
        assertEquals("http://localhost:4173/onlyoffice-plugins/0.1.0-dev/eln-template-annotator/config.json",
                resolved.getManifestUrl());
        Map<String, Object> context = resolved.runtimeContext("file-1", "record-1");
        assertEquals("file-1", context.get("documentId"));
        assertEquals("record-1", context.get("bizDataId"));
        assertEquals("eln.template", context.get("bizModule"));
        assertEquals("eln.template.annotate", context.get("bizScene"));
        assertEquals("0.1.0", context.get("pluginVersion"));
        assertNull(context.get("canAnnotate"));
        assertEquals("http://localhost:30000", context.get("apiBaseUrl"));
        assertEquals("eln-template-annotator", resolved.getPluginCode());
        assertEquals("asc.{2D6D6CB6-F6FC-45B7-8EC7-0CC1940F1A6F}", resolved.getGuid());
        assertEquals(true, resolved.isAutostart());
        assertEquals(Collections.singletonList("asc.{AA2EA9B6-9EC2-415F-9762-634EE8D9A95E}"),
                resolved.getDisabledPluginGuids());
    }
 
    @Test
    public void multiplePluginsPreserveConfiguredOrder() {
        OnlyOfficeProperties properties = new OnlyOfficeProperties();
        Map<String, OnlyOfficeProperties.PluginDefinition> definitions = new LinkedHashMap<>();
        definitions.put("plugin-b", definition("asc.{GUID-B}", "https://plugins.example.com", "2.0.0", "https://api-b.example.com"));
        definitions.put("plugin-a", definition("asc.{GUID-A}", "https://plugins.example.com", "1.0.0", "https://api-a.example.com"));
        properties.getPlugins().setDefinitions(definitions);
        OnlyOfficeProperties.PluginSceneMapping mapping = new OnlyOfficeProperties.PluginSceneMapping();
        mapping.setBizScene("multi.scene");
        mapping.setBizModule("multi.module");
        mapping.setPluginCodes(java.util.Arrays.asList("plugin-a", "plugin-b"));
        properties.getPlugins().setScenes(Collections.singletonList(mapping));
 
        List<OnlyOfficePluginResolver.ResolvedPlugin> resolved = new OnlyOfficePluginResolver(properties)
                .resolve(param("multi.scene", "multi.module", "record-1"));
 
        assertEquals("asc.{GUID-A}", resolved.get(0).getGuid());
        assertEquals("https://plugins.example.com/1.0.0/plugin-a/config.json", resolved.get(0).getManifestUrl());
        assertEquals("asc.{GUID-B}", resolved.get(1).getGuid());
    }
 
    @Test(expected = DataException.class)
    public void unknownPluginReferenceIsRejected() {
        OnlyOfficeProperties properties = new OnlyOfficeProperties();
        OnlyOfficeProperties.PluginSceneMapping mapping = new OnlyOfficeProperties.PluginSceneMapping();
        mapping.setBizScene("unknown.plugin");
        mapping.setBizModule("eln.template");
        mapping.setPluginCodes(Collections.singletonList("missing"));
        properties.getPlugins().setScenes(Collections.singletonList(mapping));
        new OnlyOfficePluginResolver(properties).resolve(param("unknown.plugin", "eln.template", "record-1"));
    }
 
    @Test(expected = DataException.class)
    public void duplicatePluginGuidIsRejected() {
        OnlyOfficeProperties properties = new OnlyOfficeProperties();
        Map<String, OnlyOfficeProperties.PluginDefinition> definitions = new LinkedHashMap<>();
        definitions.put("plugin-a", definition("asc.{SAME-GUID}", "https://plugins.example.com", "1.0.0", "https://api.example.com"));
        definitions.put("plugin-b", definition("asc.{SAME-GUID}", "https://plugins.example.com", "1.0.0", "https://api.example.com"));
        properties.getPlugins().setDefinitions(definitions);
        OnlyOfficeProperties.PluginSceneMapping mapping = new OnlyOfficeProperties.PluginSceneMapping();
        mapping.setBizScene("duplicate.guid");
        mapping.setBizModule("eln.template");
        mapping.setPluginCodes(java.util.Arrays.asList("plugin-a", "plugin-b"));
        properties.getPlugins().setScenes(Collections.singletonList(mapping));
 
        new OnlyOfficePluginResolver(properties).resolve(param("duplicate.guid", "eln.template", "record-1"));
    }
 
    @Test(expected = DataException.class)
    public void unknownSceneIsRejected() {
        resolver.resolve(param("eln.unknown", "eln.template", "record-1"));
    }
 
    @Test(expected = DataException.class)
    public void mismatchedModuleIsRejected() {
        resolver.resolve(param("eln.template.annotate", "other", "record-1"));
    }
 
    private OnlyOfficeConfigParam param(String bizScene, String bizModule, String bizDataId) {
        OnlyOfficeConfigParam param = new OnlyOfficeConfigParam();
        param.setBizScene(bizScene);
        param.setBizModule(bizModule);
        param.setBizDataId(bizDataId);
        return param;
    }
 
    private OnlyOfficeProperties.PluginDefinition definition(String guid, String publicBaseUrl, String release, String apiBaseUrl) {
        OnlyOfficeProperties.PluginDefinition definition = new OnlyOfficeProperties.PluginDefinition();
        definition.setGuid(guid);
        definition.setVersion(release.replace("-dev", ""));
        definition.setPublicBaseUrl(publicBaseUrl);
        definition.setRelease(release);
        definition.setApiBaseUrl(apiBaseUrl);
        return definition;
    }
}