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

Last change on this file since 4098 was 4098, checked in by George.Georgovassilis@mpi.nl, 10 years ago

#360, #431, #432: JPA and unified component entities

File size: 14.6 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.MDMarshaller;
19import clarin.cmdi.componentregistry.components.CMDComponentSpec;
20import clarin.cmdi.componentregistry.model.Comment;
21import clarin.cmdi.componentregistry.model.ComponentDescription;
22import clarin.cmdi.componentregistry.model.ProfileDescription;
23
24import java.io.BufferedReader;
25import java.io.File;
26import java.io.FileOutputStream;
27import java.io.InputStreamReader;
28
29import javax.xml.transform.TransformerException;
30
31import org.junit.Before;
32import org.junit.BeforeClass;
33import org.springframework.beans.factory.annotation.Autowired;
34
35/**
36 * Static helper methods to be used in tests
37 *
38 */
39public final class RegistryTestHelper {
40
41    private static MDMarshaller marshaller;
42
43    @Autowired
44    public void setMarshaller(MDMarshaller marshaller) {
45        RegistryTestHelper.marshaller = marshaller;
46    }
47
48    public static ComponentDescription addComponent(ComponentRegistry testRegistry, String id) throws ParseException, JAXBException {
49        return addComponent(testRegistry, id, getComponentTestContent());
50    }
51
52    public static ComponentDescription addComponent(ComponentRegistry testRegistry, String id, String content) throws ParseException,
53            JAXBException, UnsupportedEncodingException {
54        return addComponent(testRegistry, id, new ByteArrayInputStream(content.getBytes("UTF-8")));
55    }
56
57    private static ComponentDescription addComponent(ComponentRegistry testRegistry, String id, InputStream content) throws ParseException,
58            JAXBException {
59        ComponentDescription desc = ComponentDescription.createNewDescription();
60        desc.setCreatorName(DummyPrincipal.DUMMY_CREDENTIALS.getDisplayName());
61        desc.setUserId(DummyPrincipal.DUMMY_CREDENTIALS.getPrincipalName());
62        desc.setName(id);
63        desc.setDescription("Test Description");
64        desc.setId(ComponentDescription.COMPONENT_PREFIX + id);
65        desc.setHref("link:" + desc.getId());
66        CMDComponentSpec spec = marshaller.unmarshal(CMDComponentSpec.class, content, marshaller.getCMDComponentSchema());
67        testRegistry.register(desc, spec);
68        return desc;
69    }
70
71    public static String getProfileTestContentString() {
72        return getProfileTestContentString("Actor");
73    }
74
75    private static String getProfileTestContentString(String name) {
76        String profileContent = "";
77        profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
78        profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
79        profileContent += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
80        profileContent += "    <Header />\n";
81        profileContent += "    <CMD_Component name=\"" + name + "\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n";
82        profileContent += "        <AttributeList>\n";
83        profileContent += "            <Attribute>\n";
84        profileContent += "                <Name>Name</Name>\n";
85        profileContent += "                <Type>string</Type>\n";
86        profileContent += "            </Attribute>\n";
87        profileContent += "        </AttributeList>\n";
88        profileContent += "        <CMD_Element name=\"Age\">\n";
89        profileContent += "            <ValueScheme>\n";
90        profileContent += "                <pattern>[23][0-9]</pattern>\n";
91        profileContent += "            </ValueScheme>\n";
92        profileContent += "        </CMD_Element>\n";
93        profileContent += "    </CMD_Component>\n";
94        profileContent += "</CMD_ComponentSpec>\n";
95        return profileContent;
96    }
97
98    public static InputStream getTestProfileContent() {
99        return getTestProfileContent("Actor");
100    }
101
102    public static InputStream getTestProfileContent(String name) {
103        return new ByteArrayInputStream(getProfileTestContentString(name).getBytes());
104    }
105
106    public static ProfileDescription addProfile(ComponentRegistry testRegistry, String id) throws ParseException, JAXBException {
107        return addProfile(testRegistry, id, RegistryTestHelper.getTestProfileContent());
108    }
109
110    public static ProfileDescription addProfile(ComponentRegistry testRegistry, String id, String content) throws ParseException,
111            JAXBException {
112        return addProfile(testRegistry, id, new ByteArrayInputStream(content.getBytes()));
113    }
114
115    private static ProfileDescription addProfile(ComponentRegistry testRegistry, String id, InputStream content) throws ParseException,
116            JAXBException {
117        ProfileDescription desc = ProfileDescription.createNewDescription();
118        desc.setCreatorName(DummyPrincipal.DUMMY_CREDENTIALS.getDisplayName());
119        desc.setUserId(DummyPrincipal.DUMMY_CREDENTIALS.getPrincipalName());
120        desc.setName(id);
121        desc.setDescription("Test Description");
122        desc.setId(ProfileDescription.PROFILE_PREFIX + id);
123        desc.setHref("link:" + ProfileDescription.PROFILE_PREFIX + id);
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 {
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 {
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}
Note: See TracBrowser for help on using the repository browser.