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

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

fixing serialisation issues for List<String>. Adding unit tests for goup service

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