1 | package clarin.cmdi.componentregistry; |
---|
2 | |
---|
3 | import static junit.framework.Assert.assertEquals; |
---|
4 | import static org.junit.Assert.assertFalse; |
---|
5 | import static org.junit.Assert.assertNotNull; |
---|
6 | import static org.junit.Assert.assertNull; |
---|
7 | import static org.junit.Assert.assertTrue; |
---|
8 | import static org.junit.Assert.fail; |
---|
9 | |
---|
10 | import java.io.File; |
---|
11 | import java.util.regex.Matcher; |
---|
12 | import java.util.regex.Pattern; |
---|
13 | |
---|
14 | import javax.xml.bind.JAXBException; |
---|
15 | |
---|
16 | import org.apache.commons.io.FileUtils; |
---|
17 | import org.junit.After; |
---|
18 | import org.junit.Test; |
---|
19 | |
---|
20 | import clarin.cmdi.componentregistry.components.CMDComponentSpec; |
---|
21 | import clarin.cmdi.componentregistry.model.ComponentDescription; |
---|
22 | import clarin.cmdi.componentregistry.model.ProfileDescription; |
---|
23 | import clarin.cmdi.componentregistry.rest.DummyPrincipal; |
---|
24 | import clarin.cmdi.componentregistry.rest.RegistryTestHelper; |
---|
25 | |
---|
26 | public class ComponentRegistryImplTest { |
---|
27 | |
---|
28 | private File tmpRegistryDir; |
---|
29 | private final static DummyPrincipal PRINCIPAL = DummyPrincipal.DUMMY_PRINCIPAL; |
---|
30 | |
---|
31 | @Test |
---|
32 | public void testRegisterMDProfile() throws JAXBException { |
---|
33 | ComponentRegistry register = getTestRegistry(getRegistryDir()); |
---|
34 | ProfileDescription description = ProfileDescription.createNewDescription(); |
---|
35 | description.setName("Aap"); |
---|
36 | description.setDescription("MyDescription"); |
---|
37 | |
---|
38 | assertEquals(0, register.getComponentDescriptions().size()); |
---|
39 | assertEquals(0, register.getProfileDescriptions().size()); |
---|
40 | |
---|
41 | CMDComponentSpec testProfile = RegistryTestHelper.getTestProfile(); |
---|
42 | assertNull(testProfile.getHeader().getID()); |
---|
43 | assertNull(testProfile.getHeader().getName()); |
---|
44 | assertNull(testProfile.getHeader().getDescription()); |
---|
45 | |
---|
46 | register.registerMDProfile(description, testProfile); |
---|
47 | |
---|
48 | assertEquals(0, register.getComponentDescriptions().size()); |
---|
49 | assertEquals(1, register.getProfileDescriptions().size()); |
---|
50 | ProfileDescription desc = register.getProfileDescriptions().get(0); |
---|
51 | assertNull(register.getMDComponent(desc.getId())); |
---|
52 | |
---|
53 | CMDComponentSpec profile = register.getMDProfile(desc.getId()); |
---|
54 | assertNotNull(profile); |
---|
55 | assertEquals("Header id should be set from description id", description.getId(), profile.getHeader().getID()); |
---|
56 | assertEquals("Aap", profile.getHeader().getName()); |
---|
57 | assertEquals("MyDescription", profile.getHeader().getDescription()); |
---|
58 | } |
---|
59 | |
---|
60 | @Test |
---|
61 | public void testRegisterMDComponent() throws JAXBException { |
---|
62 | ComponentRegistry register = getTestRegistry(getRegistryDir()); |
---|
63 | ComponentDescription description = ComponentDescription.createNewDescription(); |
---|
64 | description.setName("Aap"); |
---|
65 | description.setDescription("MyDescription"); |
---|
66 | |
---|
67 | assertEquals(0, register.getComponentDescriptions().size()); |
---|
68 | assertEquals(0, register.getProfileDescriptions().size()); |
---|
69 | |
---|
70 | CMDComponentSpec testComponent = RegistryTestHelper.getTestComponent(); |
---|
71 | assertNull(testComponent.getHeader().getID()); |
---|
72 | assertNull(testComponent.getHeader().getName()); |
---|
73 | assertNull(testComponent.getHeader().getDescription()); |
---|
74 | testComponent.getHeader().setDescription("Will not be overwritten"); |
---|
75 | |
---|
76 | register.registerMDComponent(description, testComponent); |
---|
77 | |
---|
78 | assertEquals(1, register.getComponentDescriptions().size()); |
---|
79 | assertEquals(0, register.getProfileDescriptions().size()); |
---|
80 | ComponentDescription desc = register.getComponentDescriptions().get(0); |
---|
81 | assertNull(register.getMDProfile(desc.getId())); |
---|
82 | |
---|
83 | CMDComponentSpec component = register.getMDComponent(desc.getId()); |
---|
84 | assertNotNull(component); |
---|
85 | assertEquals("Header id should be set from description id", description.getId(), component.getHeader().getID()); |
---|
86 | assertEquals("Aap", component.getHeader().getName()); |
---|
87 | assertEquals("Will not be overwritten", component.getHeader().getDescription()); |
---|
88 | } |
---|
89 | |
---|
90 | @Test |
---|
91 | public void testCache() throws JAXBException { |
---|
92 | ComponentRegistryImpl register = (ComponentRegistryImpl) ComponentRegistryImpl.getInstance(); |
---|
93 | Configuration config = new Configuration(); |
---|
94 | config.setRegistryRoot(getRegistryDir()); |
---|
95 | config.init(); |
---|
96 | register.setConfiguration(config); |
---|
97 | |
---|
98 | ProfileDescription description = new ProfileDescription(); |
---|
99 | description.setName("Aap"); |
---|
100 | String id = "Aap" + System.currentTimeMillis(); |
---|
101 | description.setId(id); |
---|
102 | |
---|
103 | assertEquals(0, register.getComponentDescriptions().size()); |
---|
104 | assertEquals(0, register.getProfileDescriptions().size()); |
---|
105 | register.registerMDProfile(description, RegistryTestHelper.getTestProfile()); |
---|
106 | assertEquals(0, register.getComponentDescriptions().size()); |
---|
107 | assertEquals(1, register.getProfileDescriptions().size()); |
---|
108 | assertNull(register.getMDComponent(id)); |
---|
109 | assertNotNull(register.getMDProfile(id)); |
---|
110 | |
---|
111 | register.setConfiguration(config); //triggers cache |
---|
112 | assertEquals(1, register.getProfileDescriptions().size()); |
---|
113 | assertEquals(0, register.getComponentDescriptions().size()); |
---|
114 | assertNotNull(register.getMDProfile(id)); |
---|
115 | } |
---|
116 | |
---|
117 | @Test |
---|
118 | public void testCacheCorruptFile() throws JAXBException { |
---|
119 | ComponentRegistryImpl register = (ComponentRegistryImpl) ComponentRegistryImpl.getInstance(); |
---|
120 | Configuration config = new Configuration(); |
---|
121 | config.setRegistryRoot(getRegistryDir()); |
---|
122 | config.init(); |
---|
123 | register.setConfiguration(config); |
---|
124 | |
---|
125 | ProfileDescription description = new ProfileDescription(); |
---|
126 | description.setName("Aap"); |
---|
127 | String id = "Aap" + System.currentTimeMillis(); |
---|
128 | description.setId(id); |
---|
129 | |
---|
130 | assertEquals(0, register.getComponentDescriptions().size()); |
---|
131 | assertEquals(0, register.getProfileDescriptions().size()); |
---|
132 | register.registerMDProfile(description, RegistryTestHelper.getTestProfile()); |
---|
133 | description = new ProfileDescription(); |
---|
134 | description.setName("Aap2"); |
---|
135 | String id2 = "Aap2" + System.currentTimeMillis(); |
---|
136 | description.setId(id2); |
---|
137 | register.registerMDProfile(description, RegistryTestHelper.getTestProfile()); |
---|
138 | |
---|
139 | assertEquals(0, register.getComponentDescriptions().size()); |
---|
140 | assertEquals(2, register.getProfileDescriptions().size()); |
---|
141 | assertNull(register.getMDComponent(id)); |
---|
142 | assertNotNull(register.getMDProfile(id)); |
---|
143 | assertNotNull(register.getMDProfile(id2)); |
---|
144 | |
---|
145 | File profileFile = new File(config.getProfileDir(), id + File.separator + id + ".xml"); |
---|
146 | assertTrue(profileFile.exists()); |
---|
147 | assertTrue(profileFile.delete()); //profile file deleted so file system corrupt file should no longer be loaded in cache |
---|
148 | assertFalse(profileFile.exists()); |
---|
149 | |
---|
150 | register.setConfiguration(config); //triggers cache |
---|
151 | assertEquals(1, register.getProfileDescriptions().size()); |
---|
152 | assertEquals(0, register.getComponentDescriptions().size()); |
---|
153 | assertNull(register.getMDProfile(id)); |
---|
154 | assertNotNull(register.getMDProfile(id2)); |
---|
155 | } |
---|
156 | |
---|
157 | @Test |
---|
158 | public void testGetProfileAsXsd() throws Exception { |
---|
159 | ComponentRegistry register = getTestRegistry(getRegistryDir()); |
---|
160 | String profileContent = ""; |
---|
161 | profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
162 | profileContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
163 | profileContent += " <Header />\n"; |
---|
164 | profileContent += " <CMD_Component name=\"Actor\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n"; |
---|
165 | profileContent += " <CMD_Element name=\"Age\">\n"; |
---|
166 | profileContent += " <ValueScheme>\n"; |
---|
167 | profileContent += " <pattern>[0-9][0-9]</pattern>\n"; |
---|
168 | profileContent += " </ValueScheme>\n"; |
---|
169 | profileContent += " </CMD_Element>\n"; |
---|
170 | profileContent += " </CMD_Component>\n"; |
---|
171 | profileContent += "</CMD_ComponentSpec>\n"; |
---|
172 | |
---|
173 | String id = "profile1"; |
---|
174 | ProfileDescription description = RegistryTestHelper.addProfile(register, id, profileContent); |
---|
175 | |
---|
176 | String xsd = register.getMDProfileAsXsd(description.getId()); |
---|
177 | assertTrue(xsd.endsWith("</xs:schema>")); |
---|
178 | |
---|
179 | assertTrue(hasComponent(xsd, "Actor", "0", "unbounded")); |
---|
180 | } |
---|
181 | |
---|
182 | @Test |
---|
183 | public void testGetNestedComponentAsXsd() throws Exception { |
---|
184 | ComponentRegistry register = getTestRegistry(getRegistryDir()); |
---|
185 | |
---|
186 | String compId = "component1"; |
---|
187 | String compContent = ""; |
---|
188 | compContent += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
189 | compContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
190 | compContent += " <Header/>\n"; |
---|
191 | compContent += " <CMD_Component name=\"Actor\" CardinalityMin=\"1\" CardinalityMax=\"10\">\n"; |
---|
192 | compContent += " <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n"; |
---|
193 | compContent += " </CMD_Component>\n"; |
---|
194 | compContent += "</CMD_ComponentSpec>\n"; |
---|
195 | |
---|
196 | ComponentDescription compDesc = RegistryTestHelper.addComponent(register, compId, compContent); |
---|
197 | |
---|
198 | String profileContent = ""; |
---|
199 | profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
200 | profileContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
201 | profileContent += " <Header />\n"; |
---|
202 | profileContent += " <CMD_Component ComponentId=\"" + compDesc.getId() |
---|
203 | + "\" filename=\"component-test-file\" CardinalityMin=\"0\" CardinalityMax=\"5\">\n"; |
---|
204 | profileContent += " </CMD_Component>\n"; |
---|
205 | profileContent += "</CMD_ComponentSpec>\n"; |
---|
206 | |
---|
207 | String id = "profile1"; |
---|
208 | ProfileDescription description = RegistryTestHelper.addProfile(register, id, profileContent); |
---|
209 | |
---|
210 | String xsd = register.getMDProfileAsXsd(description.getId()); |
---|
211 | |
---|
212 | assertTrue(xsd.endsWith("</xs:schema>")); |
---|
213 | assertTrue(hasComponent(xsd, "Actor", "0", "5")); |
---|
214 | } |
---|
215 | |
---|
216 | @Test |
---|
217 | public void testGetNestedComponentAsXsdComplex() throws Exception { |
---|
218 | ComponentRegistry register = getTestRegistry(getRegistryDir()); |
---|
219 | |
---|
220 | String compContent = ""; |
---|
221 | compContent += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
222 | compContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
223 | compContent += " <Header/>\n"; |
---|
224 | compContent += " <CMD_Component name=\"XXX\" CardinalityMin=\"1\" CardinalityMax=\"10\">\n"; |
---|
225 | compContent += " <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n"; |
---|
226 | compContent += " </CMD_Component>\n"; |
---|
227 | compContent += "</CMD_ComponentSpec>\n"; |
---|
228 | ComponentDescription compDesc1 = RegistryTestHelper.addComponent(register, "component1", compContent); |
---|
229 | |
---|
230 | compContent = ""; |
---|
231 | compContent += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
232 | compContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
233 | compContent += " <Header/>\n"; |
---|
234 | compContent += " <CMD_Component name=\"YYY\" CardinalityMin=\"1\" CardinalityMax=\"1\">\n"; |
---|
235 | compContent += " <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n"; |
---|
236 | compContent += " <CMD_Component ComponentId=\"" + compDesc1.getId() + "\" filename=\"component-test-file\">\n"; |
---|
237 | compContent += " </CMD_Component>\n"; |
---|
238 | compContent += " </CMD_Component>\n"; |
---|
239 | compContent += "</CMD_ComponentSpec>\n"; |
---|
240 | ComponentDescription compDesc2 = RegistryTestHelper.addComponent(register, "component2", compContent); |
---|
241 | |
---|
242 | compContent = ""; |
---|
243 | compContent += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
244 | compContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
245 | compContent += " <Header/>\n"; |
---|
246 | compContent += " <CMD_Component name=\"ZZZ\" CardinalityMin=\"1\" CardinalityMax=\"unbounded\">\n"; |
---|
247 | compContent += " <CMD_Component ComponentId=\"" + compDesc2.getId() |
---|
248 | + "\" filename=\"component-test-file\" CardinalityMin=\"0\" CardinalityMax=\"2\">\n"; |
---|
249 | compContent += " </CMD_Component>\n"; |
---|
250 | compContent += " <CMD_Component ComponentId=\"" + compDesc1.getId() |
---|
251 | + "\" filename=\"component-test-file\" CardinalityMin=\"0\" CardinalityMax=\"99\">\n"; |
---|
252 | compContent += " </CMD_Component>\n"; |
---|
253 | compContent += " </CMD_Component>\n"; |
---|
254 | compContent += "</CMD_ComponentSpec>\n"; |
---|
255 | ComponentDescription compDesc3 = RegistryTestHelper.addComponent(register, "component3", compContent); |
---|
256 | |
---|
257 | String xsd = register.getMDComponentAsXsd(compDesc3.getId()); |
---|
258 | assertTrue(xsd.endsWith("</xs:schema>")); |
---|
259 | System.out.println(xsd); |
---|
260 | |
---|
261 | assertTrue(hasComponent(xsd, "ZZZ", "1", "unbounded")); |
---|
262 | assertTrue(hasComponent(xsd, "YYY", "0", "2")); |
---|
263 | assertTrue(hasComponent(xsd, "XXX", "1", "10")); |
---|
264 | assertTrue(hasComponent(xsd, "XXX", "0", "99")); |
---|
265 | } |
---|
266 | |
---|
267 | @Test |
---|
268 | public void testDeleteProfile() throws Exception { |
---|
269 | ComponentRegistry register = getTestRegistry(getRegistryDir()); |
---|
270 | ProfileDescription description = ProfileDescription.createNewDescription(); |
---|
271 | description.setName("Aap"); |
---|
272 | description.setCreatorName(PRINCIPAL.getName()); |
---|
273 | description.setDescription("MyDescription"); |
---|
274 | CMDComponentSpec testProfile = RegistryTestHelper.getTestProfile(); |
---|
275 | |
---|
276 | register.registerMDProfile(description, testProfile); |
---|
277 | |
---|
278 | assertEquals(1, register.getProfileDescriptions().size()); |
---|
279 | assertNotNull(register.getMDProfile(description.getId())); |
---|
280 | |
---|
281 | try { |
---|
282 | register.deleteMDProfile(description.getId(), new DummyPrincipal("Fake User")); |
---|
283 | fail("Should have thrown exception"); |
---|
284 | }catch(UserUnauthorizedException e) {} |
---|
285 | register.deleteMDComponent(description.getId(), new DummyPrincipal("Fake User")); |
---|
286 | |
---|
287 | assertEquals(1, register.getProfileDescriptions().size()); |
---|
288 | assertNotNull(register.getMDProfile(description.getId())); |
---|
289 | |
---|
290 | register.deleteMDProfile(description.getId(), PRINCIPAL); |
---|
291 | |
---|
292 | assertEquals(0, register.getProfileDescriptions().size()); |
---|
293 | assertNull(register.getMDProfile(description.getId())); |
---|
294 | } |
---|
295 | |
---|
296 | @Test |
---|
297 | public void testDeleteComponent() throws Exception { |
---|
298 | ComponentRegistry register = getTestRegistry(getRegistryDir()); |
---|
299 | ComponentDescription description = ComponentDescription.createNewDescription(); |
---|
300 | description.setName("Aap"); |
---|
301 | description.setCreatorName(PRINCIPAL.getName()); |
---|
302 | description.setDescription("MyDescription"); |
---|
303 | CMDComponentSpec testProfile = RegistryTestHelper.getTestProfile(); |
---|
304 | |
---|
305 | register.registerMDComponent(description, testProfile); |
---|
306 | try { |
---|
307 | register.deleteMDComponent(description.getId(), new DummyPrincipal("Fake User")); |
---|
308 | fail("Should have thrown exception"); |
---|
309 | }catch(UserUnauthorizedException e) {} |
---|
310 | |
---|
311 | assertEquals(1, register.getComponentDescriptions().size()); |
---|
312 | assertNotNull(register.getMDComponent(description.getId())); |
---|
313 | |
---|
314 | register.deleteMDComponent(description.getId(), PRINCIPAL); |
---|
315 | |
---|
316 | assertEquals(0, register.getComponentDescriptions().size()); |
---|
317 | assertNull(register.getMDProfile(description.getId())); |
---|
318 | |
---|
319 | } |
---|
320 | |
---|
321 | private File getRegistryDir() { |
---|
322 | if (tmpRegistryDir == null) |
---|
323 | tmpRegistryDir = createTempRegistryDir(); |
---|
324 | return tmpRegistryDir; |
---|
325 | } |
---|
326 | |
---|
327 | @After |
---|
328 | public void cleanupRegistryDir() { |
---|
329 | cleanUpRegistryDir(tmpRegistryDir); |
---|
330 | tmpRegistryDir = null; |
---|
331 | } |
---|
332 | |
---|
333 | public static ComponentRegistry getTestRegistry(File registryRoot) { |
---|
334 | ComponentRegistryImpl register = (ComponentRegistryImpl) ComponentRegistryImpl.getInstance(); |
---|
335 | Configuration config = new Configuration(); |
---|
336 | config.setRegistryRoot(registryRoot); |
---|
337 | config.init(); |
---|
338 | register.setConfiguration(config); |
---|
339 | return register; |
---|
340 | } |
---|
341 | |
---|
342 | public static File createTempRegistryDir() { |
---|
343 | final String baseTempPath = System.getProperty("java.io.tmpdir"); |
---|
344 | File tempDir = new File(baseTempPath + File.separator + "testRegistry_" + System.currentTimeMillis()); |
---|
345 | tempDir.mkdir(); |
---|
346 | tempDir.deleteOnExit(); |
---|
347 | return tempDir; |
---|
348 | } |
---|
349 | |
---|
350 | public static void cleanUpRegistryDir(File registryDir) { |
---|
351 | if (registryDir != null && registryDir.exists()) { |
---|
352 | assertTrue(FileUtils.deleteQuietly(registryDir)); |
---|
353 | } |
---|
354 | } |
---|
355 | |
---|
356 | /** |
---|
357 | * Testing a big xsd string is a bit hard, so doing a best effort by checking the xs:element which represent the nested components used |
---|
358 | * in a profile/component |
---|
359 | */ |
---|
360 | private boolean hasComponent(String xsd, String name, String min, String max) { |
---|
361 | Pattern pattern = Pattern.compile("<xs:element name=\"" + name + "\" minOccurs=\"" + min + "\" maxOccurs=\"" + max + "\">"); |
---|
362 | Matcher matcher = pattern.matcher(xsd); |
---|
363 | return matcher.find() && !matcher.find(); //find only one |
---|
364 | } |
---|
365 | } |
---|