source: ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/MDValidatorTest.java @ 4550

Last change on this file since 4550 was 4550, checked in by twagoo, 10 years ago

Fixed errors due to schema change allowing only one root element in a profile or component (see r4525)

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