source: ComponentRegistry/branches/jeaferversion/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/RegistryTestHelper.java @ 1640

Last change on this file since 1640 was 1640, checked in by jeafer, 12 years ago

Jean-Charles branch ComponentRegistry commit5 (functionnal),
Changes regarding comment on the ComponentRegistry.

Modification of classes CommentsDao?
Response class has been split up. A general class ComponentRegistryResponse?
a abstractDescription response: RegisterResponse?
a comment response : CommentResponse?
Improvement of validation process for comments
Improvement of ComponentRegistryRestService?
New classes test CommentResponseTest? and CommentValidatorTest?

Documentation added to most classes
Clean up code in other classes

File size: 12.0 KB
Line 
1package clarin.cmdi.componentregistry.rest;
2
3import java.io.ByteArrayInputStream;
4import java.io.ByteArrayOutputStream;
5import java.io.IOException;
6import java.io.InputStream;
7import java.io.UnsupportedEncodingException;
8import java.text.ParseException;
9import java.util.regex.Matcher;
10import java.util.regex.Pattern;
11
12import javax.xml.bind.JAXBException;
13
14import clarin.cmdi.componentregistry.ComponentRegistry;
15import clarin.cmdi.componentregistry.MDMarshaller;
16import clarin.cmdi.componentregistry.components.CMDComponentSpec;
17import clarin.cmdi.componentregistry.model.Comment;
18import clarin.cmdi.componentregistry.model.ComponentDescription;
19import clarin.cmdi.componentregistry.model.ProfileDescription;
20
21/**
22 * Static helper methods to be used in tests
23 *
24 */
25public final class RegistryTestHelper {
26
27    private RegistryTestHelper() {
28    }
29
30    public static ComponentDescription addComponent(ComponentRegistry testRegistry, String id) throws ParseException, JAXBException {
31        return addComponent(testRegistry, id, getComponentTestContent());
32    }
33
34    public static ComponentDescription addComponent(ComponentRegistry testRegistry, String id, String content) throws ParseException,
35            JAXBException, UnsupportedEncodingException {
36        return addComponent(testRegistry, id, new ByteArrayInputStream(content.getBytes("UTF-8")));
37    }
38
39    private static ComponentDescription addComponent(ComponentRegistry testRegistry, String id, InputStream content) throws ParseException,
40            JAXBException {
41        ComponentDescription desc = ComponentDescription.createNewDescription();
42        desc.setCreatorName(DummyPrincipal.DUMMY_CREDENTIALS.getDisplayName());
43        desc.setUserId(DummyPrincipal.DUMMY_CREDENTIALS.getPrincipalName());
44        desc.setName(id);
45        desc.setDescription("Test Description");
46        desc.setId(ComponentRegistry.REGISTRY_ID + id);
47        desc.setHref("link:" + ComponentRegistry.REGISTRY_ID + id);
48        CMDComponentSpec spec = MDMarshaller.unmarshal(CMDComponentSpec.class, content, MDMarshaller.getCMDComponentSchema());
49        testRegistry.register(desc, spec);
50        return desc;
51    }
52
53    public static int updateComponent(ComponentRegistry testRegistry, ComponentDescription description, String content) throws JAXBException {
54        return testRegistry.update(description, getComponentFromString(content), DummyPrincipal.DUMMY_CREDENTIALS.getPrincipal(), true);
55    }
56
57    public static String getProfileTestContentString() {
58        return getProfileTestContentString("Actor");
59    }
60
61    private static String getProfileTestContentString(String name) {
62        String profileContent = "";
63        profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
64        profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
65        profileContent += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
66        profileContent += "    <Header />\n";
67        profileContent += "    <CMD_Component name=\"" + name + "\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n";
68        profileContent += "        <AttributeList>\n";
69        profileContent += "            <Attribute>\n";
70        profileContent += "                <Name>Name</Name>\n";
71        profileContent += "                <Type>string</Type>\n";
72        profileContent += "            </Attribute>\n";
73        profileContent += "        </AttributeList>\n";
74        profileContent += "        <CMD_Element name=\"Age\">\n";
75        profileContent += "            <ValueScheme>\n";
76        profileContent += "                <pattern>[23][0-9]</pattern>\n";
77        profileContent += "            </ValueScheme>\n";
78        profileContent += "        </CMD_Element>\n";
79        profileContent += "    </CMD_Component>\n";
80        profileContent += "</CMD_ComponentSpec>\n";
81        return profileContent;
82    }
83
84    public static InputStream getTestProfileContent() {
85        return getTestProfileContent("Actor");
86    }
87
88    public static InputStream getTestProfileContent(String name) {
89        return new ByteArrayInputStream(getProfileTestContentString(name).getBytes());
90    }
91
92    public static ProfileDescription addProfile(ComponentRegistry testRegistry, String id) throws ParseException, JAXBException {
93        return addProfile(testRegistry, id, RegistryTestHelper.getTestProfileContent());
94    }
95
96    public static ProfileDescription addProfile(ComponentRegistry testRegistry, String id, String content) throws ParseException,
97            JAXBException {
98        return addProfile(testRegistry, id, new ByteArrayInputStream(content.getBytes()));
99    }
100
101    private static ProfileDescription addProfile(ComponentRegistry testRegistry, String id, InputStream content) throws ParseException,
102            JAXBException {
103        ProfileDescription desc = ProfileDescription.createNewDescription();
104        desc.setCreatorName(DummyPrincipal.DUMMY_CREDENTIALS.getDisplayName());
105        desc.setUserId(DummyPrincipal.DUMMY_CREDENTIALS.getPrincipalName());
106        desc.setName(id);
107        desc.setDescription("Test Description");
108        desc.setId(ComponentRegistry.REGISTRY_ID + id);
109        desc.setHref("link:" + ComponentRegistry.REGISTRY_ID + id);
110        CMDComponentSpec spec = MDMarshaller.unmarshal(CMDComponentSpec.class, content, MDMarshaller.getCMDComponentSchema());
111        testRegistry.register(desc, spec);
112        return desc;
113    }
114
115    public static CMDComponentSpec getTestProfile() throws JAXBException {
116        return MDMarshaller.unmarshal(CMDComponentSpec.class, getTestProfileContent(), MDMarshaller.getCMDComponentSchema());
117    }
118
119    public static String getComponentTestContentString() {
120        return getComponentTestContentString("Access");
121    }
122
123    public static InputStream getComponentTestContent() {
124        return getComponentTestContent("Access");
125    }
126
127    public static String getComponentTestContentString(String componentName) {
128        String compContent = "";
129        compContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
130        compContent += "\n";
131        compContent += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
132        compContent += "    xsi:noNamespaceSchemaLocation=\"../../general-component-schema.xsd\">\n";
133        compContent += "    \n";
134        compContent += "    <Header/>\n";
135        compContent += "    \n";
136        compContent += "    <CMD_Component name=\"" + componentName + "\" CardinalityMin=\"1\" CardinalityMax=\"1\">\n";
137        compContent += "        <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n";
138        compContent += "        <CMD_Element name=\"Date\">\n";
139        compContent += "            <ValueScheme>\n";
140        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";
141        compContent += "                <pattern>(1|2)\\d{3}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])</pattern>                \n";
142        compContent += "            </ValueScheme>\n";
143        compContent += "        </CMD_Element>\n";
144        compContent += "        <CMD_Element name=\"Owner\" ValueScheme=\"string\" />\n";
145        compContent += "        <CMD_Element name=\"Publisher\" ValueScheme=\"string\" />\n";
146        compContent += "    </CMD_Component>\n";
147        compContent += "\n";
148        compContent += "</CMD_ComponentSpec>\n";
149        return compContent;
150    }
151
152    public static CMDComponentSpec getComponentFromString(String contentString) throws JAXBException {
153        return MDMarshaller.unmarshal(CMDComponentSpec.class, getComponentContent(contentString), MDMarshaller.getCMDComponentSchema());
154    }
155
156    public static InputStream getComponentContent(String content) {
157        return new ByteArrayInputStream(content.getBytes());
158    }
159
160    public static InputStream getComponentTestContent(String componentName) {
161        return getComponentContent(getComponentTestContentString(componentName));
162    }
163
164    public static CMDComponentSpec getTestComponent() throws JAXBException {
165        return MDMarshaller.unmarshal(CMDComponentSpec.class, getComponentTestContent(), MDMarshaller.getCMDComponentSchema());
166    }
167
168    public static CMDComponentSpec getTestComponent(String name) throws JAXBException {
169        return MDMarshaller.unmarshal(CMDComponentSpec.class, getComponentTestContent(name), MDMarshaller.getCMDComponentSchema());
170    }
171
172    public static String getXml(CMDComponentSpec componentSpec) throws JAXBException, UnsupportedEncodingException {
173        ByteArrayOutputStream os = new ByteArrayOutputStream();
174        MDMarshaller.marshal(componentSpec, os);
175        String xml = os.toString();
176        try {
177            os.close();
178        } catch (IOException ex) {
179        }
180        return xml;
181    }
182
183    public static Comment addComment(ComponentRegistry testRegistry, String id, String descriptionId, String principal) throws ParseException, JAXBException {
184        return addComment(testRegistry, RegistryTestHelper.getTestCommentContent(id, descriptionId), principal);
185    }
186
187    private static Comment addComment(ComponentRegistry testRegistry, InputStream content, String principal) throws ParseException,
188            JAXBException {
189        Comment spec = MDMarshaller.unmarshal(Comment.class, content, null);
190        testRegistry.registerComment(spec, principal);
191        return spec;
192    }
193
194    public static String getCommentTestContentString(String commentName, String profileId) {
195        String comContent = "";
196        comContent += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
197        comContent += "<comment xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n";
198        comContent += "    <comments>" + commentName + "</comments>\n";
199        comContent += "    <commentDate>" + Comment.createNewDate() + "</commentDate>\n";
200        comContent += "    <profileDescriptionId>" + profileId + "</profileDescriptionId>\n";
201        comContent += "    <userId>0</userId>\n";
202        comContent += "    <id>1</id>\n";
203        comContent += "</comment>\n";
204        return comContent;
205    }
206
207    public static String getCommentTestContentStringForComponent(String commentName, String componentId) {
208        String comContent = "";
209        comContent += "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
210        comContent += "<comment xmlns:ns2=\"http://www.w3.org/1999/xlink\">\n";
211        comContent += "    <comments>" + commentName + "</comments>\n";
212        comContent += "    <commentDate>" + Comment.createNewDate() + "</commentDate>\n";
213        comContent += "     <componentDescriptionId>" + componentId + "</componentDescriptionId>";
214        comContent += "    <userId>0</userId>\n";
215        comContent += "    <id>1</id>\n";
216        comContent += "</comment>\n";
217        return comContent;
218    }
219
220    public static InputStream getTestCommentContent(String content, String descriptionId) {
221        if (descriptionId.contains("profile")) {
222            return new ByteArrayInputStream(getCommentTestContentString(content, descriptionId).getBytes());
223        } else {
224            return new ByteArrayInputStream(getCommentTestContentStringForComponent(content, descriptionId).getBytes());
225        }
226    }
227
228    public static String getCommentTestContent(String commentId, String descriptionId) {
229        return getCommentTestContentString(commentId, descriptionId);
230    }
231
232    public static InputStream getCommentTestContent() {
233        return getTestCommentContent("Actual", "profile1");
234    }
235
236    /**
237     * 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
238     * in a profile/component
239     */
240    public static boolean hasComponent(String xsd, String name, String min, String max) {
241        Pattern pattern = Pattern.compile("<xs:element name=\"" + name + "\" minOccurs=\"" + min + "\" maxOccurs=\"" + max + "\">");
242        Matcher matcher = pattern.matcher(xsd);
243        return matcher.find() && !matcher.find(); //find only one
244    }
245}
Note: See TracBrowser for help on using the repository browser.