1 | package clarin.cmdi.componentregistry.rest; |
---|
2 | |
---|
3 | import clarin.cmdi.componentregistry.ComponentRegistryException; |
---|
4 | |
---|
5 | import java.io.ByteArrayInputStream; |
---|
6 | import java.io.ByteArrayOutputStream; |
---|
7 | import java.io.IOException; |
---|
8 | import java.io.InputStream; |
---|
9 | import java.io.UnsupportedEncodingException; |
---|
10 | import java.text.ParseException; |
---|
11 | import java.util.regex.Matcher; |
---|
12 | import java.util.regex.Pattern; |
---|
13 | |
---|
14 | import javax.xml.bind.JAXBException; |
---|
15 | |
---|
16 | import clarin.cmdi.componentregistry.ComponentRegistry; |
---|
17 | import clarin.cmdi.componentregistry.DatesHelper; |
---|
18 | import clarin.cmdi.componentregistry.ItemNotFoundException; |
---|
19 | import clarin.cmdi.componentregistry.MDMarshaller; |
---|
20 | import clarin.cmdi.componentregistry.UserUnauthorizedException; |
---|
21 | import clarin.cmdi.componentregistry.components.CMDComponentSpec; |
---|
22 | import clarin.cmdi.componentregistry.model.Comment; |
---|
23 | import clarin.cmdi.componentregistry.model.ComponentDescription; |
---|
24 | import clarin.cmdi.componentregistry.model.ProfileDescription; |
---|
25 | |
---|
26 | import java.io.BufferedReader; |
---|
27 | import java.io.File; |
---|
28 | import java.io.FileOutputStream; |
---|
29 | import java.io.InputStreamReader; |
---|
30 | |
---|
31 | import org.springframework.beans.factory.annotation.Autowired; |
---|
32 | |
---|
33 | /** |
---|
34 | * Static helper methods to be used in tests |
---|
35 | * |
---|
36 | */ |
---|
37 | public final class RegistryTestHelper { |
---|
38 | |
---|
39 | private static MDMarshaller marshaller; |
---|
40 | |
---|
41 | @Autowired |
---|
42 | public void setMarshaller(MDMarshaller marshaller) { |
---|
43 | RegistryTestHelper.marshaller = marshaller; |
---|
44 | } |
---|
45 | |
---|
46 | public static ComponentDescription addComponent(ComponentRegistry testRegistry, String id, boolean isPublic) throws ParseException, JAXBException { |
---|
47 | return addComponent(testRegistry, id, getComponentTestContent(), isPublic); |
---|
48 | } |
---|
49 | |
---|
50 | public static ComponentDescription addComponent(ComponentRegistry testRegistry, String id, String content, boolean isPublic) throws ParseException, |
---|
51 | JAXBException, UnsupportedEncodingException { |
---|
52 | return addComponent(testRegistry, id, new ByteArrayInputStream(content.getBytes("UTF-8")), isPublic); |
---|
53 | } |
---|
54 | |
---|
55 | private static ComponentDescription addComponent(ComponentRegistry testRegistry, String id, InputStream content, boolean isPublic) throws ParseException, |
---|
56 | JAXBException { |
---|
57 | ComponentDescription desc = ComponentDescription.createNewDescription(); |
---|
58 | desc.setCreatorName(DummyPrincipal.DUMMY_CREDENTIALS.getDisplayName()); |
---|
59 | desc.setUserId(DummyPrincipal.DUMMY_PRINCIPAL.getName()); |
---|
60 | desc.setName(id); |
---|
61 | desc.setDescription("Test Description"); |
---|
62 | desc.setId(ComponentDescription.COMPONENT_PREFIX + id); |
---|
63 | desc.setHref("link:" + desc.getId()); |
---|
64 | desc.setPublic(isPublic); |
---|
65 | CMDComponentSpec spec = marshaller.unmarshal(CMDComponentSpec.class, content, marshaller.getCMDComponentSchema()); |
---|
66 | testRegistry.register(desc, spec); |
---|
67 | return desc; |
---|
68 | } |
---|
69 | |
---|
70 | public static String getProfileTestContentString() { |
---|
71 | return getProfileTestContentString("Actor"); |
---|
72 | } |
---|
73 | |
---|
74 | private static String getProfileTestContentString(String name) { |
---|
75 | String profileContent = ""; |
---|
76 | profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
---|
77 | profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
78 | profileContent += " xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n"; |
---|
79 | profileContent += " <Header />\n"; |
---|
80 | profileContent += " <CMD_Component name=\"" + name + "\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n"; |
---|
81 | profileContent += " <AttributeList>\n"; |
---|
82 | profileContent += " <Attribute>\n"; |
---|
83 | profileContent += " <Name>Name</Name>\n"; |
---|
84 | profileContent += " <Type>string</Type>\n"; |
---|
85 | profileContent += " </Attribute>\n"; |
---|
86 | profileContent += " </AttributeList>\n"; |
---|
87 | profileContent += " <CMD_Element name=\"Age\">\n"; |
---|
88 | profileContent += " <ValueScheme>\n"; |
---|
89 | profileContent += " <pattern>[23][0-9]</pattern>\n"; |
---|
90 | profileContent += " </ValueScheme>\n"; |
---|
91 | profileContent += " </CMD_Element>\n"; |
---|
92 | profileContent += " </CMD_Component>\n"; |
---|
93 | profileContent += "</CMD_ComponentSpec>\n"; |
---|
94 | return profileContent; |
---|
95 | } |
---|
96 | |
---|
97 | public static InputStream getTestProfileContent() { |
---|
98 | return getTestProfileContent("Actor"); |
---|
99 | } |
---|
100 | |
---|
101 | public static InputStream getTestProfileContent(String name) { |
---|
102 | return new ByteArrayInputStream(getProfileTestContentString(name).getBytes()); |
---|
103 | } |
---|
104 | |
---|
105 | public static ProfileDescription addProfile(ComponentRegistry testRegistry, String id, boolean isPublic) throws ParseException, JAXBException, ItemNotFoundException { |
---|
106 | return addProfile(testRegistry, id, RegistryTestHelper.getTestProfileContent(), isPublic); |
---|
107 | } |
---|
108 | |
---|
109 | public static ProfileDescription addProfile(ComponentRegistry testRegistry, String id, String content, boolean isPublic) throws ParseException, |
---|
110 | JAXBException, ItemNotFoundException { |
---|
111 | return addProfile(testRegistry, id, new ByteArrayInputStream(content.getBytes()), isPublic); |
---|
112 | } |
---|
113 | |
---|
114 | private static ProfileDescription addProfile(ComponentRegistry testRegistry, String id, InputStream content, boolean isPublic) throws ParseException, |
---|
115 | JAXBException, ItemNotFoundException { |
---|
116 | ProfileDescription desc = ProfileDescription.createNewDescription(); |
---|
117 | desc.setCreatorName(DummyPrincipal.DUMMY_CREDENTIALS.getDisplayName()); |
---|
118 | desc.setUserId(DummyPrincipal.DUMMY_CREDENTIALS.getPrincipalName()); |
---|
119 | desc.setName(id); |
---|
120 | desc.setDescription("Test Description"); |
---|
121 | desc.setId(ProfileDescription.PROFILE_PREFIX + id); |
---|
122 | desc.setHref("link:" + ProfileDescription.PROFILE_PREFIX + id); |
---|
123 | desc.setPublic(isPublic); |
---|
124 | CMDComponentSpec spec = marshaller.unmarshal(CMDComponentSpec.class, content, marshaller.getCMDComponentSchema()); |
---|
125 | testRegistry.register(desc, spec); |
---|
126 | return desc; |
---|
127 | } |
---|
128 | |
---|
129 | public static CMDComponentSpec getTestProfile() throws JAXBException { |
---|
130 | return marshaller.unmarshal(CMDComponentSpec.class, getTestProfileContent(), marshaller.getCMDComponentSchema()); |
---|
131 | } |
---|
132 | |
---|
133 | public static String getComponentTestContentString() { |
---|
134 | return getComponentTestContentString("Access"); |
---|
135 | } |
---|
136 | |
---|
137 | public static InputStream getComponentTestContent() { |
---|
138 | return getComponentTestContentAsStream("Access"); |
---|
139 | } |
---|
140 | |
---|
141 | public static String getComponentTestContentString(String componentName) { |
---|
142 | String compContent = ""; |
---|
143 | compContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
---|
144 | compContent += "\n"; |
---|
145 | compContent += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"; |
---|
146 | compContent += " xsi:noNamespaceSchemaLocation=\"../../general-component-schema.xsd\">\n"; |
---|
147 | compContent += " \n"; |
---|
148 | compContent += " <Header/>\n"; |
---|
149 | compContent += " \n"; |
---|
150 | compContent += " <CMD_Component name=\"" + componentName + "\" CardinalityMin=\"1\" CardinalityMax=\"1\">\n"; |
---|
151 | compContent += " <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n"; |
---|
152 | compContent += " <CMD_Element name=\"Date\">\n"; |
---|
153 | compContent += " <ValueScheme>\n"; |
---|
154 | compContent += " <!-- matching dates of the pattern yyyy-mm-dd (ISO 8601); this only matches dates from the years 1000 through 2999 and does allow some invalid dates (e.g. February, the 30th) -->\n"; |
---|
155 | compContent += " <pattern>(1|2)\\d{3}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])</pattern> \n"; |
---|
156 | compContent += " </ValueScheme>\n"; |
---|
157 | compContent += " </CMD_Element>\n"; |
---|
158 | compContent += " <CMD_Element name=\"Owner\" ValueScheme=\"string\" />\n"; |
---|
159 | compContent += " <CMD_Element name=\"Publisher\" ValueScheme=\"string\" />\n"; |
---|
160 | compContent += " </CMD_Component>\n"; |
---|
161 | compContent += "\n"; |
---|
162 | compContent += "</CMD_ComponentSpec>\n"; |
---|
163 | return compContent; |
---|
164 | } |
---|
165 | |
---|
166 | public static String getStringFromStream(InputStream largeProfileStream) throws IOException { |
---|
167 | try { |
---|
168 | BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(largeProfileStream)); |
---|
169 | StringBuilder profileStringBuilder = new StringBuilder(); |
---|
170 | String line; |
---|
171 | while (null != (line = bufferedReader.readLine())) { |
---|
172 | profileStringBuilder.append(line); |
---|
173 | } |
---|
174 | return profileStringBuilder.toString(); |
---|
175 | } finally { |
---|
176 | largeProfileStream.close(); |
---|
177 | } |
---|
178 | } |
---|
179 | /////////////////////////////////////////////////////// |
---|
180 | |
---|
181 | public static String getLargeProfileContent() throws IOException { |
---|
182 | InputStream largeProfileStream = RegistryTestHelper.class.getResourceAsStream("/xml/largeProfile.xml"); |
---|
183 | return getStringFromStream(largeProfileStream); |
---|
184 | } |
---|
185 | |
---|
186 | ////////////////////// |
---|
187 | public static CMDComponentSpec getComponentFromString(String contentString) throws JAXBException { |
---|
188 | return marshaller.unmarshal(CMDComponentSpec.class, getComponentContentAsStream(contentString), marshaller.getCMDComponentSchema()); |
---|
189 | } |
---|
190 | |
---|
191 | public static InputStream getComponentContentAsStream(String content) { |
---|
192 | return new ByteArrayInputStream(content.getBytes()); |
---|
193 | } |
---|
194 | |
---|
195 | public static InputStream getComponentTestContentAsStream(String componentName) { |
---|
196 | return getComponentContentAsStream(getComponentTestContentString(componentName)); |
---|
197 | } |
---|
198 | |
---|
199 | public static CMDComponentSpec getTestComponent() throws JAXBException { |
---|
200 | return marshaller.unmarshal(CMDComponentSpec.class, getComponentTestContent(), marshaller.getCMDComponentSchema()); |
---|
201 | } |
---|
202 | |
---|
203 | public static CMDComponentSpec getTestComponent(String name) throws JAXBException { |
---|
204 | return marshaller.unmarshal(CMDComponentSpec.class, getComponentTestContentAsStream(name), marshaller.getCMDComponentSchema()); |
---|
205 | } |
---|
206 | |
---|
207 | public static String getXml(CMDComponentSpec componentSpec) throws JAXBException, UnsupportedEncodingException { |
---|
208 | ByteArrayOutputStream os = new ByteArrayOutputStream(); |
---|
209 | marshaller.marshal(componentSpec, os); |
---|
210 | String xml = os.toString(); |
---|
211 | try { |
---|
212 | os.close(); |
---|
213 | } catch (IOException ex) { |
---|
214 | } |
---|
215 | return xml; |
---|
216 | } |
---|
217 | |
---|
218 | public static Comment addComment(ComponentRegistry testRegistry, String id, String descriptionId, String principal) throws ParseException, JAXBException, ComponentRegistryException, ItemNotFoundException,UserUnauthorizedException { |
---|
219 | return addComment(testRegistry, RegistryTestHelper.getTestCommentContent(id, descriptionId), principal); |
---|
220 | } |
---|
221 | |
---|
222 | private static Comment addComment(ComponentRegistry testRegistry, InputStream content, String principal) throws ParseException, |
---|
223 | JAXBException, |
---|
224 | ComponentRegistryException, ItemNotFoundException,UserUnauthorizedException { |
---|
225 | Comment spec = marshaller.unmarshal(Comment.class, content, null); |
---|
226 | testRegistry.registerComment(spec, principal); |
---|
227 | return spec; |
---|
228 | } |
---|
229 | |
---|
230 | public static String getCommentTestContentStringForProfile(String commentName, String profileId) { |
---|
231 | String comContent = ""; |
---|
232 | comContent += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; |
---|
233 | comContent += "<comment xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n"; |
---|
234 | comContent += " <comments>" + commentName + "</comments>\n"; |
---|
235 | comContent += " <commentDate>" + DatesHelper.createNewDate() + "</commentDate>\n"; |
---|
236 | comContent += " <componentId>" + profileId + "</componentId>\n"; |
---|
237 | comContent += " <userName>J. Unit</userName>\n"; |
---|
238 | comContent += "</comment>\n"; |
---|
239 | return comContent; |
---|
240 | } |
---|
241 | |
---|
242 | public static String getCommentTestContentStringForComponent(String commentName, String componentId) { |
---|
243 | String comContent = ""; |
---|
244 | comContent += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; |
---|
245 | comContent += "<comment xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n"; |
---|
246 | comContent += " <comments>" + commentName + "</comments>\n"; |
---|
247 | comContent += " <commentDate>" + DatesHelper.createNewDate() + "</commentDate>\n"; |
---|
248 | comContent += " <componentId>" + componentId + "</componentId>"; |
---|
249 | comContent += " <userName>J. Unit</userName>\n"; |
---|
250 | comContent += "</comment>\n"; |
---|
251 | return comContent; |
---|
252 | } |
---|
253 | |
---|
254 | public static InputStream getTestCommentContent(String content, String descriptionId) { |
---|
255 | if (descriptionId.contains("profile")) { |
---|
256 | return new ByteArrayInputStream(getCommentTestContentStringForProfile(content, descriptionId).getBytes()); |
---|
257 | } else { |
---|
258 | return new ByteArrayInputStream(getCommentTestContentStringForComponent(content, descriptionId).getBytes()); |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | public static String getCommentTestContent(String commentId, String descriptionId) { |
---|
263 | return getCommentTestContentStringForProfile(commentId, descriptionId); |
---|
264 | } |
---|
265 | |
---|
266 | public static InputStream getCommentTestContent() { |
---|
267 | return getTestCommentContent("Actual", ProfileDescription.PROFILE_PREFIX+"profile1"); |
---|
268 | } |
---|
269 | |
---|
270 | /** |
---|
271 | * 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 |
---|
272 | * in a profile/component |
---|
273 | */ |
---|
274 | public static boolean hasComponent(String xsd, String name, String min, String max) { |
---|
275 | Pattern pattern = Pattern.compile("<xs:element name=\"" + name + "\" minOccurs=\"" + min + "\" maxOccurs=\"" + max + "\">"); |
---|
276 | Matcher matcher = pattern.matcher(xsd); |
---|
277 | return matcher.find() && !matcher.find(); //find only one |
---|
278 | } |
---|
279 | |
---|
280 | /** |
---|
281 | * |
---|
282 | * @param bytes is an array of bytes to be written in the file filename (from scratch!) |
---|
283 | * @param filename is the name of the file where the array "bytes" is to be written to |
---|
284 | * @throws IOException |
---|
285 | * @throws JAXBException |
---|
286 | */ |
---|
287 | public static void writeBytesToFile(byte[] bytes, String filename) throws IOException, JAXBException { |
---|
288 | |
---|
289 | File file = new File(filename); |
---|
290 | FileOutputStream fop = new FileOutputStream(file); |
---|
291 | |
---|
292 | fop.write(bytes); |
---|
293 | |
---|
294 | fop.flush(); |
---|
295 | fop.close(); |
---|
296 | |
---|
297 | |
---|
298 | } |
---|
299 | |
---|
300 | /** |
---|
301 | * |
---|
302 | * @param str is a string which is to be written into the filename (from scratch!) |
---|
303 | * @param filename is a filename where the string is to be written to |
---|
304 | * @throws IOException |
---|
305 | * @throws JAXBException |
---|
306 | */ |
---|
307 | public static void writeStringToFile(String str, String filename) throws IOException, JAXBException { |
---|
308 | |
---|
309 | writeBytesToFile(str.getBytes(), filename); |
---|
310 | |
---|
311 | |
---|
312 | } |
---|
313 | |
---|
314 | /** |
---|
315 | * |
---|
316 | * @param os is an output stream which is to be written into the filename (from scratch!) |
---|
317 | * @param filename is a filename where the stream is to be written to |
---|
318 | * @throws IOException |
---|
319 | * @throws JAXBException |
---|
320 | */ |
---|
321 | public static void writeStreamToFile(ByteArrayOutputStream os, String filename) throws IOException, JAXBException { |
---|
322 | |
---|
323 | writeBytesToFile(os.toByteArray(), filename); |
---|
324 | |
---|
325 | |
---|
326 | } |
---|
327 | |
---|
328 | /** |
---|
329 | * |
---|
330 | * @param cdesc is a component which is to be written into the filename (from scratch!) |
---|
331 | * @param filename is a filename where the component is to be written to |
---|
332 | * @throws IOException |
---|
333 | * @throws JAXBException |
---|
334 | */ |
---|
335 | public static void writeComponentIntoFile(ComponentDescription cdesc, String filename) throws IOException, JAXBException { |
---|
336 | |
---|
337 | |
---|
338 | ByteArrayOutputStream os = new ByteArrayOutputStream(); |
---|
339 | marshaller.marshal(cdesc, os); |
---|
340 | |
---|
341 | writeStreamToFile(os, filename); |
---|
342 | |
---|
343 | } |
---|
344 | |
---|
345 | /** |
---|
346 | * opens a temporary sub-directory dirName in /target/ |
---|
347 | * |
---|
348 | * @param dirName is the name of the temporary subdirectory which is to be opened |
---|
349 | * @return the absolute part for this directory |
---|
350 | */ |
---|
351 | public static String openTestDir(String dirName) { |
---|
352 | |
---|
353 | File testDir = new File("target/" + dirName); |
---|
354 | |
---|
355 | |
---|
356 | testDir.mkdir(); |
---|
357 | |
---|
358 | System.out.println(dirName); |
---|
359 | //String retval = new File(testDir, dirName).getAbsolutePath(); |
---|
360 | String retval = new File(testDir, "/").getAbsolutePath(); |
---|
361 | |
---|
362 | return (retval); |
---|
363 | |
---|
364 | } |
---|
365 | } |
---|