source: ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/MDValidatorTest.java @ 1640

Last change on this file since 1640 was 1640, checked in by jeafer, 12 years ago

Jean-Charles branch ComponentRegistry commit5 (functionnal),
Changes regarding comment on the ComponentRegistry.

Modification of classes CommentsDao?
Response class has been split up. A general class ComponentRegistryResponse?
a abstractDescription response: RegisterResponse?
a comment response : CommentResponse?
Improvement of validation process for comments
Improvement of ComponentRegistryRestService?
New classes test CommentResponseTest? and CommentValidatorTest?

Documentation added to most classes
Clean up code in other classes

File size: 12.0 KB
Line 
1package clarin.cmdi.componentregistry.rest;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertFalse;
5import static org.junit.Assert.assertTrue;
6
7import java.io.ByteArrayInputStream;
8import java.io.InputStream;
9
10import org.junit.Before;
11import org.junit.Test;
12import org.junit.runner.RunWith;
13import org.springframework.beans.factory.annotation.Autowired;
14import org.springframework.jdbc.core.JdbcTemplate;
15import org.springframework.test.context.ContextConfiguration;
16import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
17
18import clarin.cmdi.componentregistry.ComponentRegistry;
19import clarin.cmdi.componentregistry.ComponentRegistryFactory;
20import clarin.cmdi.componentregistry.impl.database.ComponentRegistryTestDatabase;
21import clarin.cmdi.componentregistry.model.ComponentDescription;
22import clarin.cmdi.componentregistry.model.ProfileDescription;
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
34    @Before
35    public void init() {
36        ComponentRegistryTestDatabase.resetAndCreateAllTables(jdbcTemplate);
37        publicRegistry = componentRegistryFactory.getPublicRegistry();
38    }
39
40    @Test
41    public void testValidateSucces() {
42        String profileContent = "";
43        profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
44        profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
45        profileContent += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
46        profileContent += "    <Header />\n";
47        profileContent += "    <CMD_Component name=\"Actor\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n";
48        profileContent += "        <CMD_Element name=\"Age\">\n";
49        profileContent += "            <ValueScheme>\n";
50        profileContent += "                <pattern>[23][0-9]</pattern>\n";
51        profileContent += "            </ValueScheme>\n";
52        profileContent += "        </CMD_Element>\n";
53        profileContent += "    </CMD_Component>\n";
54        profileContent += "</CMD_ComponentSpec>\n";
55        InputStream input = new ByteArrayInputStream(profileContent.getBytes());
56
57        ProfileDescription desc = ProfileDescription.createNewDescription();
58        MDValidator validator = new MDValidator(input, desc, publicRegistry, null, publicRegistry);
59        assertTrue(validator.validate());
60    }
61
62    @Test
63    public void testValidateIllegalComponentAttributeName() {
64        String profileContent = "";
65        profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
66        profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
67        profileContent += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
68        profileContent += "    <Header />\n";
69        profileContent += "    <CMD_Component name=\"Actor\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n";
70        profileContent += "        <AttributeList>\n";
71        profileContent += "             <Attribute>\n";
72        profileContent += "                 <Name>myattribute</Name>\n"; // this should be allowed
73        profileContent += "                 <Type>string</Type>\n";
74        profileContent += "             </Attribute>\n";
75        profileContent += "             <Attribute>\n";
76        profileContent += "                 <Name>ref</Name>\n"; // this should NOT be allowed
77        profileContent += "                 <Type>string</Type>\n";
78        profileContent += "             </Attribute>\n";
79        profileContent += "             <Attribute>\n";
80        profileContent += "                 <Name>ComponentId</Name>\n"; // neither should this
81        profileContent += "                 <Type>string</Type>\n";
82        profileContent += "             </Attribute>\n";
83        profileContent += "        </AttributeList>\n";
84        profileContent += "        <CMD_Element name=\"Age\">\n";
85        profileContent += "             <AttributeList>\n";
86        profileContent += "                 <Attribute>\n";
87        profileContent += "                     <Name>ref</Name>\n"; // allowed here, only forbidden on components
88        profileContent += "                     <Type>string</Type>\n";
89        profileContent += "                 </Attribute>\n";
90        profileContent += "                 <Attribute>\n";
91        profileContent += "                     <Name>ComponentId</Name>\n"; // allowed here, only forbidden on components
92        profileContent += "                     <Type>string</Type>\n";
93        profileContent += "                 </Attribute>\n";
94        profileContent += "             </AttributeList>\n";
95        profileContent += "            <ValueScheme>\n";
96        profileContent += "                <pattern>[23][0-9]</pattern>\n";
97        profileContent += "            </ValueScheme>\n";
98        profileContent += "        </CMD_Element>\n";
99        profileContent += "    </CMD_Component>\n";
100        profileContent += "</CMD_ComponentSpec>\n";
101        InputStream input = new ByteArrayInputStream(profileContent.getBytes());
102
103        ProfileDescription desc = ProfileDescription.createNewDescription();
104        MDValidator validator = new MDValidator(input, desc, publicRegistry, null, publicRegistry);
105        assertFalse(validator.validate());
106        assertEquals(validator.getErrorMessages().size(), 2);
107        assertTrue(validator.getErrorMessages().get(0).startsWith(MDValidator.ILLEGAL_ATTRIBUTE_NAME_ERROR));
108        assertTrue(validator.getErrorMessages().get(1).startsWith(MDValidator.ILLEGAL_ATTRIBUTE_NAME_ERROR));
109    }
110
111    @Test
112    public void testValidateNoComponentId() throws Exception {
113        String profileContent = "";
114        profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
115        profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
116        profileContent += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
117        profileContent += "    <Header />\n";
118        profileContent += "    <CMD_Component filename=\"component-actor.xml\"/>\n";
119        profileContent += "</CMD_ComponentSpec>\n";
120        InputStream input = new ByteArrayInputStream(profileContent.getBytes());
121
122        ProfileDescription desc = ProfileDescription.createNewDescription();
123        MDValidator validator = new MDValidator(input, desc, publicRegistry, null, publicRegistry);
124        assertFalse(validator.validate());
125        assertTrue(validator.getErrorMessages().get(0).startsWith(MDValidator.COMPONENT_NOT_PUBLICLY_REGISTERED_ERROR));
126    }
127
128    @Test
129    public void testValidateComponentIdNotRegistered() throws Exception {
130        String id1 = "component1";
131        String id2 = "component2";
132
133        String profileContent = "";
134        profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
135        profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
136        profileContent += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
137        profileContent += "    <Header />\n";
138        profileContent += "    <CMD_Component ComponentId=\"" + ComponentRegistry.REGISTRY_ID + id1 + "\"/>\n"; //id not registered
139        profileContent += "    <CMD_Component ComponentId=\"" + ComponentRegistry.REGISTRY_ID + id2 + "\"/>\n"; //id not registered
140        profileContent += "</CMD_ComponentSpec>\n";
141
142        // Ids not registered will return two errors. One for each id
143        ProfileDescription desc = ProfileDescription.createNewDescription();
144        MDValidator validator = new MDValidator(new ByteArrayInputStream(profileContent.getBytes()), desc, publicRegistry, null, publicRegistry);
145        assertFalse(validator.validate());
146        assertEquals(2, validator.getErrorMessages().size());
147        assertTrue(validator.getErrorMessages().get(0).startsWith(MDValidator.COMPONENT_NOT_PUBLICLY_REGISTERED_ERROR));
148        assertTrue(validator.getErrorMessages().get(1).startsWith(MDValidator.COMPONENT_NOT_PUBLICLY_REGISTERED_ERROR));
149
150        // id1 will be added and therefore only id2 is not registered
151        RegistryTestHelper.addComponent(publicRegistry, id1);
152        validator = new MDValidator(new ByteArrayInputStream(profileContent.getBytes()), desc, publicRegistry, null, publicRegistry);
153        assertFalse(validator.validate());
154        assertEquals(1, validator.getErrorMessages().size());
155        assertTrue(validator.getErrorMessages().get(0).startsWith(MDValidator.COMPONENT_NOT_PUBLICLY_REGISTERED_ERROR));
156
157        // id2 is added, no more errors shoud be return
158        RegistryTestHelper.addComponent(publicRegistry, id2);
159        validator = new MDValidator(new ByteArrayInputStream(profileContent.getBytes()), desc, publicRegistry, null, publicRegistry);
160        assertTrue("component is registered should be valid now", validator.validate());
161        assertEquals(0, validator.getErrorMessages().size());
162    }
163
164    @Test
165    public void testValidateUserRegistry() throws Exception {
166        String id1 = "component1";
167        String id2 = "component2";
168        ComponentRegistry userRegistry = componentRegistryFactory.getComponentRegistry(true, DummyPrincipal.DUMMY_CREDENTIALS);
169
170        String profileContent = "";
171        profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
172        profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
173        profileContent += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
174        profileContent += "    <Header />\n";
175        profileContent += "    <CMD_Component ComponentId=\"" + ComponentRegistry.REGISTRY_ID + id1 + "\"/>\n"; //id not registered
176        profileContent += "    <CMD_Component ComponentId=\"" + ComponentRegistry.REGISTRY_ID + id2 + "\"/>\n"; //id not registered
177        profileContent += "</CMD_ComponentSpec>\n";
178
179        ProfileDescription desc = ProfileDescription.createNewDescription();
180        MDValidator validator = new MDValidator(new ByteArrayInputStream(profileContent.getBytes()), desc, userRegistry, userRegistry, publicRegistry);
181        assertFalse(validator.validate());
182        assertEquals(2, validator.getErrorMessages().size());
183        assertTrue(validator.getErrorMessages().get(0).startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR));
184        assertTrue(validator.getErrorMessages().get(1).startsWith(MDValidator.COMPONENT_NOT_REGISTERED_ERROR));
185
186        RegistryTestHelper.addComponent(userRegistry, id1);
187        RegistryTestHelper.addComponent(publicRegistry, id2);
188        validator = new MDValidator(new ByteArrayInputStream(profileContent.getBytes()), desc, publicRegistry, null, publicRegistry);
189        assertFalse(validator.validate());
190        assertEquals(1, validator.getErrorMessages().size());
191        assertTrue(validator.getErrorMessages().get(0).startsWith(MDValidator.COMPONENT_NOT_PUBLICLY_REGISTERED_ERROR));
192
193        validator = new MDValidator(new ByteArrayInputStream(profileContent.getBytes()), desc, userRegistry, userRegistry, publicRegistry);
194        assertTrue(validator.validate());
195        assertEquals(0, validator.getErrorMessages().size());
196    }
197
198    @Test
199    public void testValidateNestedComponents() throws Exception {
200        String id1 = "component1";
201
202        String content = "";
203        content += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
204        content += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
205        content += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
206        content += "    <Header />\n";
207        content += "    <CMD_Component name=\"Actor\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n";
208        content += "        <CMD_Element name=\"Name\" ValueScheme=\"string\" />\n";
209        content += "      <CMD_Component ComponentId=\"" + ComponentRegistry.REGISTRY_ID + id1 + "\"/>\n"; //id not registered
210        content += "    </CMD_Component>\n";
211        content += "</CMD_ComponentSpec>\n";
212
213        ComponentDescription desc = ComponentDescription.createNewDescription();
214        MDValidator validator = new MDValidator(new ByteArrayInputStream(content.getBytes()), desc, publicRegistry, null, publicRegistry);
215        assertFalse(validator.validate());
216        assertEquals(1, validator.getErrorMessages().size());
217        assertTrue(validator.getErrorMessages().get(0).startsWith(MDValidator.COMPONENT_NOT_PUBLICLY_REGISTERED_ERROR));
218
219        RegistryTestHelper.addComponent(publicRegistry, id1);
220        validator = new MDValidator(new ByteArrayInputStream(content.getBytes()), desc, publicRegistry, null, publicRegistry);
221        assertTrue(validator.validate());
222        assertEquals(0, validator.getErrorMessages().size());
223
224    }
225}
Note: See TracBrowser for help on using the repository browser.