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

Last change on this file since 3088 was 3088, checked in by twagoo, 11 years ago

Changes in MdMarshaller? and its usage:

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