1 | package clarin.cmdi.componentregistry.rest; |
---|
2 | |
---|
3 | import clarin.cmdi.componentregistry.BaseUnitTest; |
---|
4 | import clarin.cmdi.componentregistry.ComponentRegistry; |
---|
5 | import clarin.cmdi.componentregistry.ComponentRegistryFactory; |
---|
6 | import clarin.cmdi.componentregistry.OwnerUser; |
---|
7 | import clarin.cmdi.componentregistry.RegistrySpace; |
---|
8 | import clarin.cmdi.componentregistry.UserUnauthorizedException; |
---|
9 | import clarin.cmdi.componentregistry.components.CMDComponentSpec; |
---|
10 | import clarin.cmdi.componentregistry.model.ComponentDescription; |
---|
11 | import clarin.cmdi.componentregistry.model.ProfileDescription; |
---|
12 | import clarin.cmdi.componentregistry.model.RegistryUser; |
---|
13 | import java.io.ByteArrayInputStream; |
---|
14 | import java.io.InputStream; |
---|
15 | |
---|
16 | import javax.xml.transform.TransformerException; |
---|
17 | |
---|
18 | import org.junit.Before; |
---|
19 | import org.junit.Ignore; |
---|
20 | import org.junit.Test; |
---|
21 | import org.springframework.beans.factory.annotation.Autowired; |
---|
22 | |
---|
23 | |
---|
24 | /** |
---|
25 | * |
---|
26 | * @author george.georgovassilis@mpi.nl |
---|
27 | * |
---|
28 | */ |
---|
29 | public class MDValidatorTest extends BaseUnitTest { |
---|
30 | |
---|
31 | @Autowired |
---|
32 | private ComponentRegistryFactory componentRegistryFactory; |
---|
33 | @Autowired |
---|
34 | private ComponentRegistry testRegistry; |
---|
35 | |
---|
36 | private RegistryUser user; |
---|
37 | |
---|
38 | |
---|
39 | @Before |
---|
40 | @Override |
---|
41 | public void setUp() throws TransformerException, UserUnauthorizedException { |
---|
42 | testRegistry = componentRegistryFactory.getPublicRegistry(); |
---|
43 | user = componentRegistryFactory.getOrCreateUser(DummyPrincipal.DUMMY_CREDENTIALS); |
---|
44 | } |
---|
45 | |
---|
46 | |
---|
47 | |
---|
48 | @Test |
---|
49 | public void testValidateSucces() throws UserUnauthorizedException{ |
---|
50 | MDValidator validator = this.getValidProfileValidator(); |
---|
51 | boolean result = validator.validate(); |
---|
52 | assertTrue(result); |
---|
53 | } |
---|
54 | |
---|
55 | @Test |
---|
56 | public void testValidateIllegalComponentAttributeName() throws UserUnauthorizedException{ |
---|
57 | String profileContent = ""; |
---|
58 | profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
---|
59 | profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
60 | profileContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
61 | profileContent += " <Header />\n"; |
---|
62 | profileContent += " <CMD_Component name=\"Actor\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n"; |
---|
63 | profileContent += " <AttributeList>\n"; |
---|
64 | profileContent += " <Attribute>\n"; |
---|
65 | profileContent += " <Name>myattribute</Name>\n"; // this should be |
---|
66 | // allowed |
---|
67 | profileContent += " <Type>string</Type>\n"; |
---|
68 | profileContent += " </Attribute>\n"; |
---|
69 | profileContent += " <Attribute>\n"; |
---|
70 | profileContent += " <Name>ref</Name>\n"; // this should NOT be |
---|
71 | // allowed |
---|
72 | profileContent += " <Type>string</Type>\n"; |
---|
73 | profileContent += " </Attribute>\n"; |
---|
74 | profileContent += " <Attribute>\n"; |
---|
75 | profileContent += " <Name>ComponentId</Name>\n"; // neither should |
---|
76 | // this |
---|
77 | profileContent += " <Type>string</Type>\n"; |
---|
78 | profileContent += " </Attribute>\n"; |
---|
79 | profileContent += " </AttributeList>\n"; |
---|
80 | profileContent += " <CMD_Element name=\"Age\">\n"; |
---|
81 | profileContent += " <AttributeList>\n"; |
---|
82 | profileContent += " <Attribute>\n"; |
---|
83 | profileContent += " <Name>ref</Name>\n"; // allowed here, only |
---|
84 | // forbidden on components |
---|
85 | profileContent += " <Type>string</Type>\n"; |
---|
86 | profileContent += " </Attribute>\n"; |
---|
87 | profileContent += " <Attribute>\n"; |
---|
88 | profileContent += " <Name>ComponentId</Name>\n"; // allowed here, only |
---|
89 | // forbidden on |
---|
90 | // components |
---|
91 | profileContent += " <Type>string</Type>\n"; |
---|
92 | profileContent += " </Attribute>\n"; |
---|
93 | profileContent += " </AttributeList>\n"; |
---|
94 | profileContent += " <ValueScheme>\n"; |
---|
95 | profileContent += " <pattern>[23][0-9]</pattern>\n"; |
---|
96 | profileContent += " </ValueScheme>\n"; |
---|
97 | profileContent += " </CMD_Element>\n"; |
---|
98 | profileContent += " </CMD_Component>\n"; |
---|
99 | profileContent += "</CMD_ComponentSpec>\n"; |
---|
100 | InputStream input = new ByteArrayInputStream(profileContent.getBytes()); |
---|
101 | |
---|
102 | ProfileDescription desc = ProfileDescription.createNewDescription(); |
---|
103 | |
---|
104 | MDValidator validator = new MDValidator(input, desc, testRegistry, marshaller); |
---|
105 | assertFalse(validator.validate()); |
---|
106 | assertEquals(4, validator.getErrorMessages().size()); |
---|
107 | assertTrue(validator.getErrorMessages().get(0) |
---|
108 | .startsWith(MDValidator.PARSE_ERROR)); |
---|
109 | assertTrue(validator.getErrorMessages().get(1) |
---|
110 | .startsWith(MDValidator.PARSE_ERROR)); |
---|
111 | } |
---|
112 | |
---|
113 | @Test |
---|
114 | public void testValidateNoComponentId() throws Exception { |
---|
115 | String profileContent = ""; |
---|
116 | profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
---|
117 | profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
118 | profileContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
119 | profileContent += " <Header />\n"; |
---|
120 | profileContent += " <CMD_Component filename=\"component-actor.xml\"/>\n"; |
---|
121 | profileContent += "</CMD_ComponentSpec>\n"; |
---|
122 | InputStream input = new ByteArrayInputStream(profileContent.getBytes()); |
---|
123 | |
---|
124 | ProfileDescription desc = ProfileDescription.createNewDescription(); |
---|
125 | desc.setPublic(true); |
---|
126 | MDValidator validator = new MDValidator(input, desc, testRegistry, marshaller); |
---|
127 | assertFalse(validator.validate()); |
---|
128 | assertTrue(validator |
---|
129 | .getErrorMessages() |
---|
130 | .get(0) |
---|
131 | .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR)); |
---|
132 | // |
---|
133 | } |
---|
134 | |
---|
135 | @Test |
---|
136 | public void testValidateComponentIdNotRegistered() throws Exception { |
---|
137 | String id1 = "component1"; |
---|
138 | String id2 = "component2"; |
---|
139 | |
---|
140 | String profileContent = ""; |
---|
141 | profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; |
---|
142 | profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
143 | profileContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">"; |
---|
144 | profileContent += " <Header />"; |
---|
145 | profileContent += " <CMD_Component name=\"Test\">"; |
---|
146 | profileContent += " <CMD_Component ComponentId=\"" |
---|
147 | + ComponentDescription.COMPONENT_PREFIX + id1 + "\"/>"; // id not |
---|
148 | // registered |
---|
149 | profileContent += " <CMD_Component ComponentId=\"" |
---|
150 | + ComponentDescription.COMPONENT_PREFIX + id2 + "\"/>"; // id not |
---|
151 | // registered |
---|
152 | profileContent += " </CMD_Component>"; |
---|
153 | profileContent += "</CMD_ComponentSpec>"; |
---|
154 | |
---|
155 | // Ids not registered. will return 2 errors: one per each id. |
---|
156 | ProfileDescription desc = ProfileDescription.createNewDescription(); |
---|
157 | desc.setPublic(true); |
---|
158 | MDValidator validator = new MDValidator(new ByteArrayInputStream( |
---|
159 | profileContent.getBytes()), desc, testRegistry, marshaller); |
---|
160 | assertFalse(validator.validate()); |
---|
161 | assertEquals(1, validator.getErrorMessages().size()); |
---|
162 | assertTrue(validator |
---|
163 | .getErrorMessages() |
---|
164 | .get(0) |
---|
165 | .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR)); |
---|
166 | |
---|
167 | |
---|
168 | // id1 will be added as public and therefore only id2 is not registered |
---|
169 | RegistryTestHelper.addComponent(testRegistry, id1, true); |
---|
170 | validator = new MDValidator(new ByteArrayInputStream( |
---|
171 | profileContent.getBytes()), desc, testRegistry |
---|
172 | , marshaller); |
---|
173 | assertFalse(validator.validate()); |
---|
174 | assertEquals(1, validator.getErrorMessages().size()); |
---|
175 | assertTrue(validator |
---|
176 | .getErrorMessages() |
---|
177 | .get(0) |
---|
178 | .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR)); |
---|
179 | |
---|
180 | // id2 is added as public, no more errors shoud be return |
---|
181 | RegistryTestHelper.addComponent(testRegistry, id2, true); |
---|
182 | validator = new MDValidator(new ByteArrayInputStream( |
---|
183 | profileContent.getBytes()), desc, testRegistry, marshaller); |
---|
184 | assertTrue("component is registered should be valid now", |
---|
185 | validator.validate()); |
---|
186 | assertEquals(0, validator.getErrorMessages().size()); |
---|
187 | } |
---|
188 | |
---|
189 | @Test |
---|
190 | public void testValidateBaseAndUserRegistry() throws Exception { |
---|
191 | String id1 = "component1"; |
---|
192 | String id2 = "component2"; |
---|
193 | |
---|
194 | String profileContent = ""; |
---|
195 | profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; |
---|
196 | profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""; |
---|
197 | profileContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">"; |
---|
198 | profileContent += " <Header />"; |
---|
199 | profileContent += " <CMD_Component name=\"Test\">"; |
---|
200 | profileContent += " <CMD_Component ComponentId=\"" |
---|
201 | + ComponentDescription.COMPONENT_PREFIX + id1 + "\"/>"; // id not |
---|
202 | // registered |
---|
203 | profileContent += " <CMD_Component ComponentId=\"" |
---|
204 | + ComponentDescription.COMPONENT_PREFIX + id2 + "\"/>"; // id not |
---|
205 | // registered |
---|
206 | profileContent += " </CMD_Component>"; |
---|
207 | profileContent += "</CMD_ComponentSpec>"; |
---|
208 | |
---|
209 | // Public Registry |
---|
210 | ProfileDescription desc = ProfileDescription.createNewDescription(); |
---|
211 | MDValidator validator = new MDValidator(new ByteArrayInputStream( |
---|
212 | profileContent.getBytes()), desc, testRegistry, marshaller); |
---|
213 | assertFalse(validator.validate()); |
---|
214 | assertEquals(1, validator.getErrorMessages().size()); // the exception is thrown and propagated on the first non-registered component |
---|
215 | assertTrue(validator.getErrorMessages().get(0) |
---|
216 | .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR)); |
---|
217 | |
---|
218 | |
---|
219 | // registering publically the first component |
---|
220 | RegistryTestHelper.addComponent(testRegistry, id1, true); |
---|
221 | validator = new MDValidator(new ByteArrayInputStream( |
---|
222 | profileContent.getBytes()), desc, testRegistry, marshaller); |
---|
223 | assertFalse(validator.validate()); |
---|
224 | assertEquals(1, validator.getErrorMessages().size()); // the exception is thrown on the second non-registered component |
---|
225 | assertTrue(validator.getErrorMessages().get(0) |
---|
226 | .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR)); |
---|
227 | |
---|
228 | |
---|
229 | |
---|
230 | RegistryTestHelper.addComponent(testRegistry, id2, false); |
---|
231 | validator = new MDValidator(new ByteArrayInputStream( |
---|
232 | profileContent.getBytes()), desc, testRegistry, marshaller); |
---|
233 | assertFalse(validator.validate()); |
---|
234 | assertEquals(1, validator.getErrorMessages().size()); |
---|
235 | assertTrue(validator |
---|
236 | .getErrorMessages() |
---|
237 | .get(0) |
---|
238 | .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_IN_APPROPRIATE_SPACE_ERROR)); |
---|
239 | |
---|
240 | |
---|
241 | // make it user registry |
---|
242 | testRegistry.setRegistryOwner(new OwnerUser(user.getId())); |
---|
243 | testRegistry.setRegistrySpace(RegistrySpace.PRIVATE); |
---|
244 | |
---|
245 | |
---|
246 | validator = new MDValidator(new ByteArrayInputStream( |
---|
247 | profileContent.getBytes()), desc, testRegistry, marshaller); |
---|
248 | Boolean result =validator.validate(); |
---|
249 | assertTrue(result); |
---|
250 | assertEquals(0, validator.getErrorMessages().size()); |
---|
251 | } |
---|
252 | |
---|
253 | @Test |
---|
254 | public void testValidateNestedComponents() throws Exception { |
---|
255 | String id1 = "component1"; |
---|
256 | |
---|
257 | String content = ""; |
---|
258 | content += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
---|
259 | content += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
260 | content += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
261 | content += " <Header />\n"; |
---|
262 | content += " <CMD_Component name=\"Actor\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n"; |
---|
263 | content += " <CMD_Element name=\"Name\" ValueScheme=\"string\" />\n"; |
---|
264 | content += " <CMD_Component ComponentId=\"" |
---|
265 | + ComponentDescription.COMPONENT_PREFIX + id1 + "\"/>\n"; // id not |
---|
266 | // registered |
---|
267 | content += " </CMD_Component>\n"; |
---|
268 | content += "</CMD_ComponentSpec>\n"; |
---|
269 | |
---|
270 | ComponentDescription desc = ComponentDescription.createNewDescription(); |
---|
271 | MDValidator validator = new MDValidator(new ByteArrayInputStream( |
---|
272 | content.getBytes()), desc, testRegistry, marshaller); |
---|
273 | assertFalse(validator.validate()); |
---|
274 | assertEquals(1, validator.getErrorMessages().size()); |
---|
275 | assertTrue(validator |
---|
276 | .getErrorMessages() |
---|
277 | .get(0) |
---|
278 | .startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR)); |
---|
279 | |
---|
280 | RegistryTestHelper.addComponent(testRegistry, id1, true); |
---|
281 | validator = new MDValidator( |
---|
282 | new ByteArrayInputStream(content.getBytes()), desc,testRegistry, marshaller); |
---|
283 | assertTrue(validator.validate()); |
---|
284 | assertEquals(0, validator.getErrorMessages().size()); |
---|
285 | } |
---|
286 | |
---|
287 | /** |
---|
288 | * Test of getCMDComponentSpec method, of class MDValidator. |
---|
289 | */ |
---|
290 | @Test |
---|
291 | public void testGetCMDComponentSpec() throws Exception { |
---|
292 | String profileContent = getValidProfileString(); |
---|
293 | InputStream input = new ByteArrayInputStream(profileContent.getBytes()); |
---|
294 | |
---|
295 | ProfileDescription desc = ProfileDescription.createNewDescription(); |
---|
296 | MDValidator validator = new MDValidator(input, desc, testRegistry,marshaller); |
---|
297 | |
---|
298 | // Spec is created during validation, before it should be null |
---|
299 | assertNull(validator.getCMDComponentSpec()); |
---|
300 | validator.validate(); |
---|
301 | |
---|
302 | // Get spec created during validation |
---|
303 | final CMDComponentSpec cmdComponentSpec = validator |
---|
304 | .getCMDComponentSpec(); |
---|
305 | assertNotNull(cmdComponentSpec); |
---|
306 | |
---|
307 | // Spec content should match XML |
---|
308 | assertTrue(cmdComponentSpec.isIsProfile()); |
---|
309 | assertEquals("Actor", cmdComponentSpec.getCMDComponent().getName()); |
---|
310 | |
---|
311 | // Spec copy should be a freshly unmarshalled copy |
---|
312 | final CMDComponentSpec specCopy = validator.getCopyOfCMDComponentSpec(); |
---|
313 | assertNotSame(cmdComponentSpec, specCopy); |
---|
314 | |
---|
315 | // Content should still match XML |
---|
316 | assertTrue(specCopy.isIsProfile()); |
---|
317 | assertEquals("Actor", specCopy.getCMDComponent().getName()); |
---|
318 | } |
---|
319 | |
---|
320 | private MDValidator getValidProfileValidator() { |
---|
321 | final String profileContent = getValidProfileString(); |
---|
322 | InputStream input = new ByteArrayInputStream(profileContent.getBytes()); |
---|
323 | ProfileDescription desc = ProfileDescription.createNewDescription(); |
---|
324 | desc.setPublic(true); |
---|
325 | desc.setName("test_name"); |
---|
326 | MDValidator validator = new MDValidator(input, desc, testRegistry, marshaller); |
---|
327 | return validator; |
---|
328 | } |
---|
329 | |
---|
330 | private String getValidProfileString() { |
---|
331 | String profileContent = ""; |
---|
332 | profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
---|
333 | profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
334 | profileContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
335 | profileContent += " <Header />\n"; |
---|
336 | profileContent += " <CMD_Component name=\"Actor\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n"; |
---|
337 | profileContent += " <CMD_Element name=\"Age\">\n"; |
---|
338 | profileContent += " <ValueScheme>\n"; |
---|
339 | profileContent += " <pattern>[23][0-9]</pattern>\n"; |
---|
340 | profileContent += " </ValueScheme>\n"; |
---|
341 | profileContent += " </CMD_Element>\n"; |
---|
342 | profileContent += " </CMD_Component>\n"; |
---|
343 | profileContent += "</CMD_ComponentSpec>\n"; |
---|
344 | return profileContent; |
---|
345 | } |
---|
346 | } |
---|