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