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