source: ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/RegistryTestHelper.java @ 5556

Last change on this file since 5556 was 5556, checked in by olhsha@mpi.nl, 10 years ago

Unit test for getting profiles and components from groups. A little bug is fixed

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