source: ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestServiceTest.java @ 1894

Last change on this file since 1894 was 1894, checked in by twagoo, 12 years ago

Fixed manipulate comment methods (for deleting without http DELETE) in rest service. Added tests for all manipulate methods.

File size: 57.4 KB
Line 
1package clarin.cmdi.componentregistry.rest;
2
3import clarin.cmdi.componentregistry.ComponentRegistry;
4import clarin.cmdi.componentregistry.ComponentRegistryFactory;
5import clarin.cmdi.componentregistry.components.CMDComponentSpec;
6import clarin.cmdi.componentregistry.impl.database.ComponentRegistryBeanFactory;
7import clarin.cmdi.componentregistry.impl.database.ComponentRegistryTestDatabase;
8import clarin.cmdi.componentregistry.model.AbstractDescription;
9import clarin.cmdi.componentregistry.model.Comment;
10import clarin.cmdi.componentregistry.model.CommentResponse;
11import clarin.cmdi.componentregistry.model.ComponentDescription;
12import clarin.cmdi.componentregistry.model.ProfileDescription;
13import clarin.cmdi.componentregistry.model.RegisterResponse;
14import com.sun.jersey.api.client.ClientResponse;
15import com.sun.jersey.api.client.UniformInterfaceException;
16import com.sun.jersey.api.representation.Form;
17import com.sun.jersey.multipart.FormDataMultiPart;
18import java.io.ByteArrayInputStream;
19import java.util.Date;
20import java.util.List;
21import javax.ws.rs.core.MediaType;
22import javax.ws.rs.core.Response.Status;
23import org.junit.Before;
24import org.junit.Test;
25import org.junit.runner.RunWith;
26import org.springframework.beans.factory.annotation.Autowired;
27import org.springframework.jdbc.core.JdbcTemplate;
28import org.springframework.test.context.ContextConfiguration;
29import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
30import org.springframework.util.Assert;
31
32import static clarin.cmdi.componentregistry.rest.ComponentRegistryRestService.USERSPACE_PARAM;
33import static org.junit.Assert.*;
34
35@RunWith(SpringJUnit4ClassRunner.class)
36@ContextConfiguration(locations = {"/applicationContext.xml"})
37public class ComponentRegistryRestServiceTest extends ComponentRegistryRestServiceTestCase {
38
39    @Autowired
40    private ComponentRegistryFactory componentRegistryFactory;
41    @Autowired
42    private ComponentRegistryBeanFactory componentRegistryBeanFactory;
43    @Autowired
44    private JdbcTemplate jdbcTemplate;
45    private ComponentRegistry testRegistry;
46
47    @Before
48    public void init() {
49        ComponentRegistryTestDatabase.resetAndCreateAllTables(jdbcTemplate);
50        createUserRecord();
51        // Get public component registry
52        testRegistry = componentRegistryBeanFactory.getNewComponentRegistry();
53    }
54
55    @Override
56    protected String getApplicationContextFile() {
57        return "classpath:applicationContext.xml";
58    }
59
60    private ComponentRegistry getTestRegistry() {
61        return testRegistry;
62    }
63
64    private String expectedUserId(String principal) {
65        return getUserDao().getByPrincipalName(principal).getId().toString();
66    }
67
68    private void fillUp() throws Exception {
69        RegistryTestHelper.addProfile(getTestRegistry(), "profile2");
70        RegistryTestHelper.addProfile(getTestRegistry(), "profile1");
71        RegistryTestHelper.addComponent(getTestRegistry(), "component2");
72        RegistryTestHelper.addComponent(getTestRegistry(), "component1");
73        RegistryTestHelper.addComment(getTestRegistry(), "comment2", "clarin.eu:cr1:profile1", "JUnit@test.com");
74        Thread.sleep(1000); // wait so order becomes predictable
75        RegistryTestHelper.addComment(getTestRegistry(), "comment1", "clarin.eu:cr1:profile1", "JUnit@test.com");
76        Thread.sleep(1000); // wait so order becomes predictable
77        RegistryTestHelper.addComment(getTestRegistry(), "comment3", "clarin.eu:cr1:component1", "JUnit@test.com");
78        Thread.sleep(1000); // wait so order becomes predictable
79        RegistryTestHelper.addComment(getTestRegistry(), "comment4", "clarin.eu:cr1:component1", "JUnit@test.com");
80        Thread.sleep(1000); // wait so order becomes predictable
81    }
82
83    @Test
84    public void testGetRegisteredProfiles() throws Exception {
85        fillUp();
86        RegistryTestHelper.addProfile(getTestRegistry(), "PROFILE2");
87        List<ProfileDescription> response = getResource().path("/registry/profiles").accept(MediaType.APPLICATION_XML).get(
88                PROFILE_LIST_GENERICTYPE);
89        assertEquals(3, response.size());
90        response = getResource().path("/registry/profiles").accept(MediaType.APPLICATION_JSON).get(PROFILE_LIST_GENERICTYPE);
91        assertEquals(3, response.size());
92        assertEquals("profile1", response.get(0).getName());
93        assertEquals("PROFILE2", response.get(1).getName());
94        assertEquals("profile2", response.get(2).getName());
95    }
96
97    @Test
98    public void testGetRegisteredComponents() throws Exception {
99        fillUp();
100        RegistryTestHelper.addComponent(getTestRegistry(), "COMPONENT2");
101        List<ComponentDescription> response = getResource().path("/registry/components").accept(MediaType.APPLICATION_XML).get(
102                COMPONENT_LIST_GENERICTYPE);
103        assertEquals(3, response.size());
104        response = getResource().path("/registry/components").accept(MediaType.APPLICATION_JSON).get(COMPONENT_LIST_GENERICTYPE);
105        assertEquals(3, response.size());
106        assertEquals("component1", response.get(0).getName());
107        assertEquals("COMPONENT2", response.get(1).getName());
108        assertEquals("component2", response.get(2).getName());
109    }
110
111    @Test
112    public void testGetUserComponents() throws Exception {
113        fillUp();
114        List<ComponentDescription> response = getUserComponents();
115        assertEquals(0, response.size());
116        response = getAuthenticatedResource(getResource().path("/registry/components")).accept(MediaType.APPLICATION_JSON).get(
117                COMPONENT_LIST_GENERICTYPE);
118        assertEquals("Get public components", 2, response.size());
119        ClientResponse cResponse = getResource().path("/registry/components").queryParam(USERSPACE_PARAM, "true").accept(
120                MediaType.APPLICATION_JSON).get(ClientResponse.class);
121        assertEquals("Trying to get userspace without credentials", 500, cResponse.getStatus());
122    }
123
124    @Test
125    public void testGetRegisteredComponent() throws Exception {
126        fillUp();
127        CMDComponentSpec component = getResource().path("/registry/components/clarin.eu:cr1:component1").accept(MediaType.APPLICATION_JSON).get(CMDComponentSpec.class);
128        assertNotNull(component);
129        assertEquals("Access", component.getCMDComponent().get(0).getName());
130        component = getResource().path("/registry/components/clarin.eu:cr1:component2").accept(MediaType.APPLICATION_XML).get(
131                CMDComponentSpec.class);
132        assertNotNull(component);
133        assertEquals("Access", component.getCMDComponent().get(0).getName());
134
135        assertEquals("clarin.eu:cr1:component2", component.getHeader().getID());
136        assertEquals("component2", component.getHeader().getName());
137        assertEquals("Test Description", component.getHeader().getDescription());
138    }
139
140    @Test
141    public void testGetRegisteredCommentsInProfile() throws Exception {
142        fillUp();
143        RegistryTestHelper.addComment(getTestRegistry(), "COMMENT1", "clarin.eu:cr1:profile1", "JUnit@test.com");
144        List<Comment> response = getResource().path("/registry/profiles/clarin.eu:cr1:profile1/comments/").accept(MediaType.APPLICATION_XML).
145                get(COMMENT_LIST_GENERICTYPE);
146        assertEquals(3, response.size());
147        response = getResource().path("/registry/profiles/clarin.eu:cr1:profile1/comments").accept(MediaType.APPLICATION_JSON).get(
148                COMMENT_LIST_GENERICTYPE);
149        assertEquals(3, response.size());
150        assertEquals("comment2", response.get(0).getComment());
151        assertEquals("comment1", response.get(1).getComment());
152        assertEquals("COMMENT1", response.get(2).getComment());
153
154        assertEquals("Database test user", response.get(0).getUserName());
155    }
156
157    @Test
158    public void testGetRegisteredCommentsInComponent() throws Exception {
159        fillUp();
160        RegistryTestHelper.addComment(getTestRegistry(), "COMMENT2", "clarin.eu:cr1:component1", "JUnit@test.com");
161        List<Comment> response = getResource().path("/registry/components/clarin.eu:cr1:component1/comments/").accept(MediaType.APPLICATION_XML).
162                get(COMMENT_LIST_GENERICTYPE);
163        assertEquals(3, response.size());
164        response = getResource().path("/registry/components/clarin.eu:cr1:component1/comments").accept(MediaType.APPLICATION_JSON).get(
165                COMMENT_LIST_GENERICTYPE);
166        assertEquals(3, response.size());
167        assertEquals("comment3", response.get(0).getComment());
168        assertEquals("comment4", response.get(1).getComment());
169        assertEquals("COMMENT2", response.get(2).getComment());
170
171        assertEquals("Database test user", response.get(0).getUserName());
172    }
173
174    @Test
175    public void testGetSpecifiedCommentInComponent() throws Exception {
176        fillUp();
177        Comment comment = getResource().path("/registry/components/clarin.eu:cr1:component1/comments/2").accept(MediaType.APPLICATION_JSON).get(Comment.class);
178        assertNotNull(comment);
179        assertEquals("comment3", comment.getComment());
180        assertEquals("2", comment.getId());
181        comment = getResource().path("/registry/components/clarin.eu:cr1:component1/comments/3").accept(MediaType.APPLICATION_JSON).get(Comment.class);
182        assertNotNull(comment);
183        assertEquals("comment4", comment.getComment());
184        assertEquals("3", comment.getId());
185        assertEquals("clarin.eu:cr1:component1", comment.getComponentDescriptionId());
186    }
187
188    @Test
189    public void testGetSpecifiedCommentInProfile() throws Exception {
190        fillUp();
191        Comment comment = getResource().path("/registry/profiles/clarin.eu:cr1:profile1/comments/0").accept(MediaType.APPLICATION_JSON).get(Comment.class);
192        assertNotNull(comment);
193        assertEquals("comment2", comment.getComment());
194        assertEquals("0", comment.getId());
195        comment = getResource().path("/registry/profiles/clarin.eu:cr1:profile1/comments/1").accept(MediaType.APPLICATION_JSON).get(Comment.class);
196        assertNotNull(comment);
197        assertEquals("comment1", comment.getComment());
198        assertEquals("1", comment.getId());
199        assertEquals("clarin.eu:cr1:profile1", comment.getProfileDescriptionId());
200    }
201
202    @Test
203    public void testDeleteCommentFromComponent() throws Exception {
204        fillUp();
205        List<Comment> comments = getResource().path("/registry/components/clarin.eu:cr1:component1/comments").get(COMMENT_LIST_GENERICTYPE);
206        assertEquals(2, comments.size());
207        Comment aComment = getResource().path("/registry/components/clarin.eu:cr1:component1/comments/2").get(Comment.class);
208        assertNotNull(aComment);
209
210        // Try to delete from other component
211        ClientResponse response = getAuthenticatedResource("/registry/components/clarin.eu:cr1:component2/comments/2").delete(ClientResponse.class);
212        assertEquals(500, response.getStatus());
213        // Delete from correct component
214        response = getAuthenticatedResource("/registry/components/clarin.eu:cr1:component1/comments/2").delete(ClientResponse.class);
215        assertEquals(200, response.getStatus());
216
217        comments = getResource().path("/registry/components/clarin.eu:cr1:component1/comments/").get(COMMENT_LIST_GENERICTYPE);
218        assertEquals(1, comments.size());
219
220        response = getAuthenticatedResource("/registry/components/clarin.eu:cr1:component1/comments/3").delete(ClientResponse.class);
221        assertEquals(200, response.getStatus());
222
223        comments = getResource().path("/registry/components/clarin.eu:cr1:component1/comments").get(COMMENT_LIST_GENERICTYPE);
224        assertEquals(0, comments.size());
225    }
226
227    @Test
228    public void testManipulateCommentFromComponent() throws Exception {
229        fillUp();
230        List<Comment> comments = getResource().path("/registry/components/clarin.eu:cr1:component1/comments").get(COMMENT_LIST_GENERICTYPE);
231        assertEquals(2, comments.size());
232        Comment aComment = getResource().path("/registry/components/clarin.eu:cr1:component1/comments/2").get(Comment.class);
233        assertNotNull(aComment);
234
235        Form manipulateForm = new Form();
236        manipulateForm.add("method", "delete");
237
238        // Try to delete from other component
239        ClientResponse response = getAuthenticatedResource("/registry/components/clarin.eu:cr1:component1/comments/2").post(ClientResponse.class, manipulateForm);
240        assertEquals(200, response.getStatus());
241
242        comments = getResource().path("/registry/components/clarin.eu:cr1:component1/comments/").get(COMMENT_LIST_GENERICTYPE);
243        assertEquals(1, comments.size());
244    }
245
246    @Test
247    public void testDeleteCommentFromProfile() throws Exception {
248        fillUp();
249        List<Comment> comments = getResource().path("/registry/profiles/clarin.eu:cr1:profile1/comments").get(COMMENT_LIST_GENERICTYPE);
250        assertEquals(2, comments.size());
251        Comment aComment = getResource().path("/registry/profiles/clarin.eu:cr1:profile1/comments/0").get(Comment.class);
252        assertNotNull(aComment);
253
254        // Try to delete from other profile
255        ClientResponse response = getAuthenticatedResource("/registry/profiles/clarin.eu:cr1:profile2/comments/0").delete(ClientResponse.class);
256        assertEquals(500, response.getStatus());
257        // Delete from correct profile
258        response = getAuthenticatedResource("/registry/profiles/clarin.eu:cr1:profile1/comments/0").delete(ClientResponse.class);
259        assertEquals(200, response.getStatus());
260
261        comments = getResource().path("/registry/profiles/clarin.eu:cr1:profile1/comments/").get(COMMENT_LIST_GENERICTYPE);
262        assertEquals(1, comments.size());
263
264        response = getAuthenticatedResource("/registry/profiles/clarin.eu:cr1:profile1/comments/1").delete(ClientResponse.class);
265        assertEquals(200, response.getStatus());
266
267        comments = getResource().path("/registry/profiles/clarin.eu:cr1:profile1/comments").get(COMMENT_LIST_GENERICTYPE);
268        assertEquals(0, comments.size());
269    }
270
271    @Test
272    public void testManipulateCommentFromProfile() throws Exception {
273        fillUp();
274        List<Comment> comments = getResource().path("/registry/profiles/clarin.eu:cr1:profile1/comments").get(COMMENT_LIST_GENERICTYPE);
275        assertEquals(2, comments.size());
276        Comment aComment = getResource().path("/registry/profiles/clarin.eu:cr1:profile1/comments/0").get(Comment.class);
277        assertNotNull(aComment);
278
279        Form manipulateForm = new Form();
280        manipulateForm.add("method", "delete");
281        // Delete from correct profile
282        ClientResponse response = getAuthenticatedResource("/registry/profiles/clarin.eu:cr1:profile1/comments/0").post(ClientResponse.class, manipulateForm);
283        assertEquals(200, response.getStatus());
284
285        comments = getResource().path("/registry/profiles/clarin.eu:cr1:profile1/comments/").get(COMMENT_LIST_GENERICTYPE);
286        assertEquals(1, comments.size());
287    }
288
289    @Test
290    public void testDeleteRegisteredComponent() throws Exception {
291        fillUp();
292        List<ComponentDescription> components = getResource().path("/registry/components").get(COMPONENT_LIST_GENERICTYPE);
293        assertEquals(2, components.size());
294        CMDComponentSpec profile = getResource().path("/registry/components/clarin.eu:cr1:component1").get(CMDComponentSpec.class);
295        assertNotNull(profile);
296        ClientResponse response = getAuthenticatedResource("/registry/components/clarin.eu:cr1:component1").delete(ClientResponse.class);
297        assertEquals(200, response.getStatus());
298
299        components = getResource().path("/registry/components").get(COMPONENT_LIST_GENERICTYPE);
300        assertEquals(1, components.size());
301
302        response = getAuthenticatedResource("/registry/components/clarin.eu:cr1:component2").delete(ClientResponse.class);
303        assertEquals(200, response.getStatus());
304
305        components = getResource().path("/registry/components").get(COMPONENT_LIST_GENERICTYPE);
306        assertEquals(0, components.size());
307
308        response = getAuthenticatedResource("/registry/components/clarin.eu:cr1:component1").delete(ClientResponse.class);
309        assertEquals(200, response.getStatus());
310    }
311
312    @Test
313    public void testDeleteRegisteredComponentStillUsed() throws Exception {
314        String content = "";
315        content += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
316        content += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
317        content += "    <Header/>\n";
318        content += "    <CMD_Component name=\"XXX\" CardinalityMin=\"1\" CardinalityMax=\"10\">\n";
319        content += "        <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n";
320        content += "    </CMD_Component>\n";
321        content += "</CMD_ComponentSpec>\n";
322        ComponentDescription compDesc1 = RegistryTestHelper.addComponent(getTestRegistry(), "XXX1", content);
323
324        content = "";
325        content += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
326        content += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
327        content += "    <Header/>\n";
328        content += "    <CMD_Component name=\"YYY\" CardinalityMin=\"1\" CardinalityMax=\"unbounded\">\n";
329        content += "        <CMD_Component ComponentId=\"" + compDesc1.getId() + "\" CardinalityMin=\"0\" CardinalityMax=\"99\">\n";
330        content += "        </CMD_Component>\n";
331        content += "    </CMD_Component>\n";
332        content += "</CMD_ComponentSpec>\n";
333        ComponentDescription compDesc2 = RegistryTestHelper.addComponent(getTestRegistry(), "YYY1", content);
334
335        content = "";
336        content += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
337        content += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
338        content += "    <Header/>\n";
339        content += "    <CMD_Component name=\"ZZZ\" CardinalityMin=\"1\" CardinalityMax=\"unbounded\">\n";
340        content += "        <CMD_Component ComponentId=\"" + compDesc1.getId() + "\" CardinalityMin=\"0\" CardinalityMax=\"99\">\n";
341        content += "        </CMD_Component>\n";
342        content += "    </CMD_Component>\n";
343        content += "</CMD_ComponentSpec>\n";
344        ProfileDescription profile = RegistryTestHelper.addProfile(getTestRegistry(), "TestProfile3", content);
345
346        List<ComponentDescription> components = getResource().path("/registry/components").get(COMPONENT_LIST_GENERICTYPE);
347        assertEquals(2, components.size());
348
349        ClientResponse response = getAuthenticatedResource("/registry/components/" + compDesc1.getId()).delete(ClientResponse.class);
350        assertEquals(Status.FORBIDDEN.getStatusCode(), response.getStatus());
351        assertEquals(
352                "Still used by the following profiles: \n - TestProfile3\nStill used by the following components: \n - YYY1\nTry to change above mentioned references first.",
353                response.getEntity(String.class));
354
355        components = getResource().path("/registry/components").get(COMPONENT_LIST_GENERICTYPE);
356        assertEquals(2, components.size());
357
358        response = getAuthenticatedResource("/registry/profiles/" + profile.getId()).delete(ClientResponse.class);
359        assertEquals(200, response.getStatus());
360        response = getAuthenticatedResource("/registry/components/" + compDesc2.getId()).delete(ClientResponse.class);
361        assertEquals(200, response.getStatus());
362        response = getAuthenticatedResource("/registry/components/" + compDesc1.getId()).delete(ClientResponse.class);
363        assertEquals(200, response.getStatus());
364        components = getResource().path("/registry/components").get(COMPONENT_LIST_GENERICTYPE);
365        assertEquals(0, components.size());
366    }
367
368    @Test
369    public void testManipulateRegisteredComponent() throws Exception {
370        fillUp();
371        List<ComponentDescription> components = getResource().path("/registry/components").get(COMPONENT_LIST_GENERICTYPE);
372        assertEquals(2, components.size());
373        CMDComponentSpec profile = getResource().path("/registry/components/clarin.eu:cr1:component1").get(CMDComponentSpec.class);
374        assertNotNull(profile);
375
376        Form manipulateForm = new Form();
377        manipulateForm.add("method", "delete");
378
379        ClientResponse response = getAuthenticatedResource("/registry/components/clarin.eu:cr1:component1").post(ClientResponse.class, manipulateForm);
380        assertEquals(200, response.getStatus());
381
382        components = getResource().path("/registry/components").get(COMPONENT_LIST_GENERICTYPE);
383        assertEquals(1, components.size());
384
385        response = getAuthenticatedResource("/registry/components/clarin.eu:cr1:component2").post(ClientResponse.class, manipulateForm);
386        assertEquals(200, response.getStatus());
387    }
388
389    @Test
390    public void testGetRegisteredProfile() throws Exception {
391        fillUp();
392        CMDComponentSpec profile = getResource().path("/registry/profiles/clarin.eu:cr1:profile1").accept(MediaType.APPLICATION_JSON).get(
393                CMDComponentSpec.class);
394        assertNotNull(profile);
395        assertEquals("Actor", profile.getCMDComponent().get(0).getName());
396        profile = getResource().path("/registry/profiles/clarin.eu:cr1:profile2").accept(MediaType.APPLICATION_XML).get(
397                CMDComponentSpec.class);
398        assertNotNull(profile);
399        assertEquals("Actor", profile.getCMDComponent().get(0).getName());
400
401        assertEquals("clarin.eu:cr1:profile2", profile.getHeader().getID());
402        assertEquals("profile2", profile.getHeader().getName());
403        assertEquals("Test Description", profile.getHeader().getDescription());
404
405        try {
406            profile = getResource().path("/registry/profiles/clarin.eu:cr1:profileXXXX").accept(MediaType.APPLICATION_XML).get(
407                    CMDComponentSpec.class);
408            fail("Exception should have been thrown resource does not exist, HttpStatusCode 404");
409        } catch (UniformInterfaceException e) {
410            assertEquals(404, e.getResponse().getStatus());
411        }
412    }
413
414    @Test
415    public void testGetRegisteredProfileRawData() throws Exception {
416        fillUp();
417        String profile = getResource().path("/registry/profiles/clarin.eu:cr1:profile1/xsd").accept(MediaType.TEXT_XML).get(String.class).trim();
418        assertTrue(profile.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?><xs:schema"));
419        assertTrue(profile.endsWith("</xs:schema>"));
420
421        profile = getResource().path("/registry/profiles/clarin.eu:cr1:profile1/xml").accept(MediaType.TEXT_XML).get(String.class).trim();
422        assertTrue(profile.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<CMD_ComponentSpec"));
423        assertTrue(profile.endsWith("</CMD_ComponentSpec>"));
424        assertTrue(profile.contains("xsi:schemaLocation"));
425
426        try {
427            getResource().path("/registry/components/clarin.eu:cr1:component1/xsl").accept(MediaType.TEXT_XML).get(String.class);
428            fail("Should have thrown exception, unsupported path parameter");
429        } catch (UniformInterfaceException e) {//server error
430        }
431    }
432
433    @Test
434    public void testPrivateProfileXsd() throws Exception {
435        FormDataMultiPart form = createFormData(RegistryTestHelper.getTestProfileContent());
436        RegisterResponse response = getAuthenticatedResource(getResource().path("/registry/profiles").queryParam(USERSPACE_PARAM, "true")).type(MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class, form);
437        assertTrue(response.isProfile());
438        assertEquals(1, getUserProfiles().size());
439        AbstractDescription desc = response.getDescription();
440        String profile = getResource().path("/registry/profiles/" + desc.getId() + "/xsd").accept(MediaType.TEXT_XML).get(String.class).trim();
441        assertTrue(profile.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?><xs:schema"));
442        assertTrue(profile.endsWith("</xs:schema>"));
443    }
444
445    @Test
446    public void testPrivateComponentXsd() throws Exception {
447        FormDataMultiPart form = createFormData(RegistryTestHelper.getComponentTestContent());
448        RegisterResponse response = getAuthenticatedResource(getResource().path("/registry/components").queryParam(USERSPACE_PARAM, "true")).type(MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class, form);
449        assertFalse(response.isProfile());
450        assertEquals(1, getUserComponents().size());
451        AbstractDescription desc = response.getDescription();
452        String profile = getResource().path("/registry/components/" + desc.getId() + "/xsd").accept(MediaType.TEXT_XML).get(String.class).trim();
453        assertTrue(profile.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?><xs:schema"));
454        assertTrue(profile.endsWith("</xs:schema>"));
455    }
456
457    @Test
458    public void testDeleteRegisteredProfile() throws Exception {
459        fillUp();
460        List<ProfileDescription> profiles = getResource().path("/registry/profiles").get(PROFILE_LIST_GENERICTYPE);
461        assertEquals(2, profiles.size());
462        CMDComponentSpec profile = getResource().path("/registry/profiles/clarin.eu:cr1:profile1").get(CMDComponentSpec.class);
463        assertNotNull(profile);
464
465        ClientResponse response = getAuthenticatedResource("/registry/profiles/clarin.eu:cr1:profile1").delete(ClientResponse.class);
466        assertEquals(200, response.getStatus());
467
468        profiles = getResource().path("/registry/profiles").get(PROFILE_LIST_GENERICTYPE);
469        assertEquals(1, profiles.size());
470
471        response = getAuthenticatedResource("/registry/profiles/clarin.eu:cr1:profile2").delete(ClientResponse.class);
472        assertEquals(200, response.getStatus());
473
474        profiles = getResource().path("/registry/profiles").get(PROFILE_LIST_GENERICTYPE);
475        assertEquals(0, profiles.size());
476
477        response = getAuthenticatedResource("/registry/profiles/clarin.eu:cr1:profile1").delete(ClientResponse.class);
478        assertEquals(200, response.getStatus());
479    }
480
481    @Test
482    public void testManipulateRegisteredProfile() throws Exception {
483        fillUp();
484        List<ProfileDescription> profiles = getResource().path("/registry/profiles").get(PROFILE_LIST_GENERICTYPE);
485        assertEquals(2, profiles.size());
486        CMDComponentSpec profile = getResource().path("/registry/profiles/clarin.eu:cr1:profile1").get(CMDComponentSpec.class);
487        assertNotNull(profile);
488
489        Form manipulateForm = new Form();
490        manipulateForm.add("method", "delete");
491
492        ClientResponse response = getAuthenticatedResource("/registry/profiles/clarin.eu:cr1:profile1").post(ClientResponse.class, manipulateForm);
493        assertEquals(200, response.getStatus());
494
495        profiles = getResource().path("/registry/profiles").get(PROFILE_LIST_GENERICTYPE);
496        assertEquals(1, profiles.size());
497
498        response = getAuthenticatedResource("/registry/profiles/clarin.eu:cr1:profile2").post(ClientResponse.class, manipulateForm);
499        assertEquals(200, response.getStatus());
500    }
501
502    @Test
503    public void testGetRegisteredComponentRawData() throws Exception {
504        fillUp();
505        String component = getResource().path("/registry/components/clarin.eu:cr1:component1/xsd").accept(MediaType.TEXT_XML).get(
506                String.class).trim();
507        assertTrue(component.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?><xs:schema"));
508        assertTrue(component.endsWith("</xs:schema>"));
509
510        component = getResource().path("/registry/components/clarin.eu:cr1:component1/xml").accept(MediaType.TEXT_XML).get(String.class).trim();
511        assertTrue(component.startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<CMD_ComponentSpec"));
512        assertTrue(component.endsWith("</CMD_ComponentSpec>"));
513        assertTrue(component.contains("xsi:schemaLocation"));
514
515        try {
516            getResource().path("/registry/components/clarin.eu:cr1:component1/jpg").accept(MediaType.TEXT_XML).get(String.class);
517            fail("Should have thrown exception, unsopported path parameter");
518        } catch (UniformInterfaceException e) {
519        }
520    }
521
522    @Test
523    public void testRegisterProfile() throws Exception {
524        FormDataMultiPart form = new FormDataMultiPart();
525        form.field(ComponentRegistryRestService.DATA_FORM_FIELD, RegistryTestHelper.getTestProfileContent(),
526                MediaType.APPLICATION_OCTET_STREAM_TYPE);
527        form.field(ComponentRegistryRestService.NAME_FORM_FIELD, "ProfileTest1");
528        form.field(ComponentRegistryRestService.DOMAIN_FORM_FIELD, "TestDomain");
529        form.field(ComponentRegistryRestService.DESCRIPTION_FORM_FIELD, "My Test Profile");
530        form.field(ComponentRegistryRestService.GROUP_FORM_FIELD, "My Test Group");
531        RegisterResponse response = getAuthenticatedResource("/registry/profiles").type(MediaType.MULTIPART_FORM_DATA).post(
532                RegisterResponse.class, form);
533        assertTrue(response.isProfile());
534        assertFalse(response.isInUserSpace());
535        ProfileDescription profileDesc = (ProfileDescription) response.getDescription();
536        assertNotNull(profileDesc);
537        assertEquals("ProfileTest1", profileDesc.getName());
538        assertEquals("My Test Profile", profileDesc.getDescription());
539        assertEquals("TestDomain", profileDesc.getDomainName());
540        assertEquals("My Test Group", profileDesc.getGroupName());
541        assertEquals(expectedUserId("JUnit@test.com"), profileDesc.getUserId());
542        assertEquals("Database test user", profileDesc.getCreatorName());
543        assertTrue(profileDesc.getId().startsWith(ComponentRegistry.REGISTRY_ID + "p_"));
544        assertNotNull(profileDesc.getRegistrationDate());
545        assertEquals("http://localhost:9998/registry/profiles/" + profileDesc.getId(), profileDesc.getHref());
546    }
547
548    @Test
549    public void testPublishProfile() throws Exception {
550        assertEquals("user registered profiles", 0, getUserProfiles().size());
551        assertEquals("public registered profiles", 0, getPublicProfiles().size());
552        FormDataMultiPart form = createFormData(RegistryTestHelper.getTestProfileContent(), "Unpublished");
553        RegisterResponse response = getAuthenticatedResource(getResource().path("/registry/profiles").queryParam(USERSPACE_PARAM, "true")).type(MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class, form);
554        assertTrue(response.isProfile());
555        AbstractDescription desc = response.getDescription();
556        assertEquals("Unpublished", desc.getDescription());
557        assertEquals(1, getUserProfiles().size());
558        assertEquals(0, getPublicProfiles().size());
559        form = createFormData(RegistryTestHelper.getTestProfileContent("publishedName"), "Published");
560        response = getAuthenticatedResource(getResource().path("/registry/profiles/" + desc.getId() + "/publish")).type(
561                MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class, form);
562
563        assertEquals(0, getUserProfiles().size());
564        List<ProfileDescription> profiles = getPublicProfiles();
565        assertEquals(1, profiles.size());
566        ProfileDescription profileDescription = profiles.get(0);
567        assertNotNull(profileDescription.getId());
568        assertEquals(desc.getId(), profileDescription.getId());
569        assertEquals("http://localhost:9998/registry/profiles/" + desc.getId(), profileDescription.getHref());
570        assertEquals("Published", profileDescription.getDescription());
571        CMDComponentSpec spec = getPublicSpec(profileDescription);
572        assertEquals("publishedName", spec.getCMDComponent().get(0).getName());
573    }
574
575    private CMDComponentSpec getPublicSpec(AbstractDescription desc) {
576        if (desc.isProfile()) {
577            return getResource().path("/registry/profiles/" + desc.getId()).get(CMDComponentSpec.class);
578        } else {
579            return getResource().path("/registry/components/" + desc.getId()).get(CMDComponentSpec.class);
580        }
581    }
582
583    @Test
584    public void testPublishComponent() throws Exception {
585        assertEquals(0, getUserComponents().size());
586        assertEquals(0, getPublicComponents().size());
587        FormDataMultiPart form = createFormData(RegistryTestHelper.getComponentTestContent(), "Unpublished");
588        RegisterResponse response = getAuthenticatedResource(getResource().path("/registry/components").queryParam(USERSPACE_PARAM, "true")).type(MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class, form);
589        assertFalse(response.isProfile());
590        AbstractDescription desc = response.getDescription();
591        assertEquals("Unpublished", desc.getDescription());
592        assertEquals(1, getUserComponents().size());
593        assertEquals(0, getPublicComponents().size());
594        form = createFormData(RegistryTestHelper.getComponentTestContent("publishedName"), "Published");
595        response = getAuthenticatedResource(getResource().path("/registry/components/" + desc.getId() + "/publish")).type(
596                MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class, form);
597
598        assertEquals(0, getUserComponents().size());
599        List<ComponentDescription> components = getPublicComponents();
600        assertEquals(1, components.size());
601        ComponentDescription componentDescription = components.get(0);
602        assertNotNull(componentDescription.getId());
603        assertEquals(desc.getId(), componentDescription.getId());
604        assertEquals("http://localhost:9998/registry/components/" + desc.getId(), componentDescription.getHref());
605        assertEquals("Published", componentDescription.getDescription());
606        CMDComponentSpec spec = getPublicSpec(componentDescription);
607        assertEquals("publishedName", spec.getCMDComponent().get(0).getName());
608    }
609
610    @Test
611    public void testRegisterUserspaceProfile() throws Exception {
612        List<ProfileDescription> profiles = getUserProfiles();
613        assertEquals("user registered profiles", 0, profiles.size());
614        assertEquals("public registered profiles", 0, getPublicProfiles().size());
615        FormDataMultiPart form = createFormData(RegistryTestHelper.getTestProfileContent());
616        RegisterResponse response = getAuthenticatedResource(getResource().path("/registry/profiles").queryParam(USERSPACE_PARAM, "true")).type(MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class, form);
617        assertTrue(response.isProfile());
618        assertTrue(response.isInUserSpace());
619        ProfileDescription profileDesc = (ProfileDescription) response.getDescription();
620        assertNotNull(profileDesc);
621        assertEquals("Test1", profileDesc.getName());
622        assertEquals("My Test", profileDesc.getDescription());
623        assertEquals(expectedUserId("JUnit@test.com"), profileDesc.getUserId());
624        assertEquals("Database test user", profileDesc.getCreatorName());
625        assertTrue(profileDesc.getId().startsWith(ComponentRegistry.REGISTRY_ID + "p_"));
626        assertNotNull(profileDesc.getRegistrationDate());
627        assertEquals("http://localhost:9998/registry/profiles/" + profileDesc.getId() + "?userspace=true", profileDesc.getHref());
628
629        profiles = getUserProfiles();
630        assertEquals(1, profiles.size());
631        assertEquals(0, getPublicProfiles().size());
632
633        //Try to post unauthenticated
634        ClientResponse cResponse = getResource().path("/registry/profiles").queryParam(USERSPACE_PARAM, "true").type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
635        assertEquals(401, cResponse.getStatus());
636
637        // Try get from public registry
638        cResponse = getResource().path("/registry/profiles/" + profileDesc.getId()).accept(MediaType.APPLICATION_XML).get(
639                ClientResponse.class);
640        // Should return 404 = not found
641        assertEquals(404, cResponse.getStatus());
642        CMDComponentSpec spec = getAuthenticatedResource(
643                getResource().path("/registry/profiles/" + profileDesc.getId()).queryParam(USERSPACE_PARAM, "true")).accept(
644                MediaType.APPLICATION_XML).get(CMDComponentSpec.class);
645        assertNotNull(spec);
646
647        cResponse = getResource().path("/registry/profiles/" + profileDesc.getId() + "/xsd").accept(MediaType.TEXT_XML).get(
648                ClientResponse.class);
649        assertEquals(200, cResponse.getStatus());
650        String profile = cResponse.getEntity(String.class);
651        assertTrue(profile.length() > 0);
652
653        profile = getAuthenticatedResource(getResource().path("/registry/profiles/" + profileDesc.getId() + "/xml")).accept(
654                MediaType.TEXT_XML).get(String.class);
655        assertTrue(profile.length() > 0);
656
657        cResponse = getAuthenticatedResource(
658                getResource().path("/registry/profiles/" + profileDesc.getId()).queryParam(USERSPACE_PARAM, "true")).delete(
659                ClientResponse.class);
660        assertEquals(200, cResponse.getStatus());
661
662        profiles = getUserProfiles();
663        assertEquals(0, profiles.size());
664    }
665
666    private List<ProfileDescription> getPublicProfiles() {
667        return getAuthenticatedResource("/registry/profiles").accept(MediaType.APPLICATION_XML).get(PROFILE_LIST_GENERICTYPE);
668    }
669
670    private List<ProfileDescription> getUserProfiles() {
671        return getAuthenticatedResource(getResource().path("/registry/profiles").queryParam(USERSPACE_PARAM, "true")).accept(
672                MediaType.APPLICATION_XML).get(PROFILE_LIST_GENERICTYPE);
673    }
674
675    private FormDataMultiPart createFormData(Object content) {
676        return createFormData(content, "My Test");
677    }
678
679    private FormDataMultiPart createFormData(Object content, String description) {
680        FormDataMultiPart form = new FormDataMultiPart();
681        form.field(ComponentRegistryRestService.DATA_FORM_FIELD, content, MediaType.APPLICATION_OCTET_STREAM_TYPE);
682        form.field(ComponentRegistryRestService.NAME_FORM_FIELD, "Test1");
683        form.field(ComponentRegistryRestService.DESCRIPTION_FORM_FIELD, description);
684        form.field(ComponentRegistryRestService.DOMAIN_FORM_FIELD, "My domain");
685        form.field(ComponentRegistryRestService.GROUP_FORM_FIELD, "TestGroup");
686        return form;
687    }
688
689    @Test
690    public void testRegisterWithUserComponents() throws Exception {
691        ComponentRegistry userRegistry = componentRegistryFactory.getComponentRegistry(true, DummyPrincipal.DUMMY_CREDENTIALS);
692        String content = "";
693        content += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
694        content += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
695        content += "    <Header/>\n";
696        content += "    <CMD_Component name=\"XXX\" CardinalityMin=\"1\" CardinalityMax=\"10\">\n";
697        content += "        <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n";
698        content += "    </CMD_Component>\n";
699        content += "</CMD_ComponentSpec>\n";
700        ComponentDescription compDesc1 = RegistryTestHelper.addComponent(userRegistry, "XXX1", content);
701
702        content = "";
703        content += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
704        content += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
705        content += "    <Header/>\n";
706        content += "    <CMD_Component name=\"YYY\" CardinalityMin=\"1\" CardinalityMax=\"unbounded\">\n";
707        content += "        <CMD_Component ComponentId=\"" + compDesc1.getId() + "\" CardinalityMin=\"0\" CardinalityMax=\"99\">\n";
708        content += "        </CMD_Component>\n";
709        content += "    </CMD_Component>\n";
710        content += "</CMD_ComponentSpec>\n";
711        FormDataMultiPart form = createFormData(content);
712        RegisterResponse response = getAuthenticatedResource("/registry/components").type(MediaType.MULTIPART_FORM_DATA).post(
713                RegisterResponse.class, form);
714        assertFalse(response.isRegistered());
715        assertEquals(1, response.getErrors().size());
716        assertEquals("referenced component cannot be found in the published components: " + compDesc1.getName() + " (" + compDesc1.getId()
717                + ")", response.getErrors().get(0));
718
719        response = getAuthenticatedResource(getResource().path("/registry/components").queryParam(USERSPACE_PARAM, "true")).type(
720                MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class, form);
721        assertTrue(response.isRegistered());
722        ComponentDescription comp2 = (ComponentDescription) response.getDescription();
723
724        content = "";
725        content += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
726        content += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
727        content += "    <Header/>\n";
728        content += "    <CMD_Component name=\"ZZZ\" CardinalityMin=\"1\" CardinalityMax=\"unbounded\">\n";
729        content += "        <CMD_Component ComponentId=\"" + comp2.getId() + "\" CardinalityMin=\"0\" CardinalityMax=\"99\">\n";
730        content += "        </CMD_Component>\n";
731        content += "    </CMD_Component>\n";
732        content += "</CMD_ComponentSpec>\n";
733
734        form = createFormData(content);
735        response = getAuthenticatedResource("/registry/profiles").type(MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class, form);
736        assertFalse(response.isRegistered());
737        assertEquals(1, response.getErrors().size());
738        assertEquals("referenced component cannot be found in the published components: " + comp2.getName() + " (" + comp2.getId() + ")",
739                response.getErrors().get(0));
740
741        response = getAuthenticatedResource(getResource().path("/registry/profiles").queryParam(USERSPACE_PARAM, "true")).type(
742                MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class, form);
743        assertTrue(response.isRegistered());
744    }
745
746    @Test
747    public void testRegisterUserspaceComponent() throws Exception {
748        List<ComponentDescription> components = getUserComponents();
749        assertEquals("user registered components", 0, components.size());
750        assertEquals("public registered components", 0, getPublicComponents().size());
751        FormDataMultiPart form = createFormData(RegistryTestHelper.getComponentTestContent());
752
753        RegisterResponse response = getAuthenticatedResource(getResource().path("/registry/components").queryParam(USERSPACE_PARAM, "true")).type(MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class, form);
754        assertTrue(response.isRegistered());
755        assertFalse(response.isProfile());
756        assertTrue(response.isInUserSpace());
757        ComponentDescription desc = (ComponentDescription) response.getDescription();
758        assertNotNull(desc);
759        assertEquals("Test1", desc.getName());
760        assertEquals("My Test", desc.getDescription());
761        assertEquals(expectedUserId("JUnit@test.com"), desc.getUserId());
762        assertEquals("Database test user", desc.getCreatorName());
763        assertEquals("TestGroup", desc.getGroupName());
764        assertTrue(desc.getId().startsWith(ComponentRegistry.REGISTRY_ID + "c_"));
765        assertNotNull(desc.getRegistrationDate());
766        String url = getResource().getUriBuilder().build().toString();
767        assertEquals(url + "registry/components/" + desc.getId() + "?userspace=true", desc.getHref());
768
769        components = getUserComponents();
770        assertEquals(1, components.size());
771        assertEquals(0, getPublicComponents().size());
772
773        //Try to post unauthenticated
774        ClientResponse cResponse = getResource().path("/registry/components").queryParam(USERSPACE_PARAM, "true").type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
775        assertEquals(401, cResponse.getStatus());
776
777        // Try to get from public registry
778        cResponse = getResource().path("/registry/components/" + desc.getId()).accept(MediaType.APPLICATION_XML).get(
779                ClientResponse.class);
780        // Should return 404 = not found
781        assertEquals(404, cResponse.getStatus());
782        CMDComponentSpec spec = getUserComponent(desc);
783        assertNotNull(spec);
784
785        cResponse = getResource().path("/registry/components/" + desc.getId() + "/xsd").accept(MediaType.TEXT_XML).get(ClientResponse.class);
786        assertEquals(200, cResponse.getStatus());
787        String result = cResponse.getEntity(String.class);
788        assertTrue(result.length() > 0);
789
790        result = getAuthenticatedResource(getResource().path("/registry/components/" + desc.getId() + "/xml")).accept(MediaType.TEXT_XML).get(String.class);
791        assertTrue(result.length() > 0);
792
793        cResponse = getAuthenticatedResource(getResource().path("/registry/components/" + desc.getId()).queryParam(USERSPACE_PARAM, "true")).delete(ClientResponse.class);
794        assertEquals(200, cResponse.getStatus());
795
796        components = getUserComponents();
797        assertEquals(0, components.size());
798    }
799
800    @Test
801    public void testCreateComponentWithRecursion() throws Exception {
802        // Create new componet
803        FormDataMultiPart form = createFormData(RegistryTestHelper.getComponentTestContent());
804        ClientResponse cResponse = getAuthenticatedResource(getResource().path("/registry/components").queryParam(USERSPACE_PARAM, "true")).type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
805        assertEquals(ClientResponse.Status.OK.getStatusCode(), cResponse.getStatus());
806        RegisterResponse response = cResponse.getEntity(RegisterResponse.class);
807        assertTrue(response.isRegistered());
808        ComponentDescription desc = (ComponentDescription) response.getDescription();
809
810        // Re-define with self-recursion
811        String compContent = "";
812        compContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
813        compContent += "\n";
814        compContent += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
815        compContent += "    xsi:noNamespaceSchemaLocation=\"../../general-component-schema.xsd\">\n";
816        compContent += "    \n";
817        compContent += "    <Header/>\n";
818        compContent += "    \n";
819        compContent += "    <CMD_Component name=\"Nested\" CardinalityMin=\"1\" CardinalityMax=\"1\">\n";
820        compContent += "        <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n";
821        compContent += "        <CMD_Component ComponentId=\"" + desc.getId() + "\" name=\"Recursive\" CardinalityMin=\"1\" CardinalityMax=\"1\" />\n";
822        compContent += "    </CMD_Component>\n";
823        compContent += "\n";
824        compContent += "</CMD_ComponentSpec>\n";
825
826        // Update component
827        form = createFormData(RegistryTestHelper.getComponentContent(compContent), "UPDATE DESCRIPTION!");
828        cResponse = getAuthenticatedResource(
829                getResource().path("/registry/components/" + desc.getId() + "/update").queryParam(USERSPACE_PARAM, "true")).type(
830                MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
831        assertEquals(ClientResponse.Status.OK.getStatusCode(), cResponse.getStatus());
832        response = cResponse.getEntity(RegisterResponse.class);
833        assertFalse("Recursive definition should fail", response.isRegistered());
834        assertEquals("There should be an error message for the recursion", 1, response.getErrors().size());
835        assertTrue("There error message should specify the point of recursion", response.getErrors().get(0).contains("already contains " + desc.getId()));
836
837    }
838
839    @Test
840    public void testUpdateComponent() throws Exception {
841        List<ComponentDescription> components = getUserComponents();
842        assertEquals("user registered components", 0, components.size());
843        assertEquals("public registered components", 0, getPublicComponents().size());
844
845        FormDataMultiPart form = createFormData(RegistryTestHelper.getComponentTestContent());
846        ClientResponse cResponse = getAuthenticatedResource(getResource().path("/registry/components").queryParam(USERSPACE_PARAM, "true")).type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
847        assertEquals(ClientResponse.Status.OK.getStatusCode(), cResponse.getStatus());
848        RegisterResponse response = cResponse.getEntity(RegisterResponse.class);
849        assertTrue(response.isRegistered());
850        assertFalse(response.isProfile());
851        assertTrue(response.isInUserSpace());
852        ComponentDescription desc = (ComponentDescription) response.getDescription();
853        assertNotNull(desc);
854        assertEquals("Test1", desc.getName());
855        assertEquals("My Test", desc.getDescription());
856        Date firstDate = AbstractDescription.getDate(desc.getRegistrationDate());
857        CMDComponentSpec spec = getUserComponent(desc);
858        assertNotNull(spec);
859        assertEquals("Access", spec.getCMDComponent().get(0).getName());
860        components = getUserComponents();
861        assertEquals(1, components.size());
862        assertEquals(0, getPublicComponents().size());
863
864        //Now update
865        form = createFormData(RegistryTestHelper.getComponentTestContent("TESTNAME"), "UPDATE DESCRIPTION!");
866        cResponse = getAuthenticatedResource(
867                getResource().path("/registry/components/" + desc.getId() + "/update").queryParam(USERSPACE_PARAM, "true")).type(
868                MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
869        assertEquals(ClientResponse.Status.OK.getStatusCode(), cResponse.getStatus());
870        response = cResponse.getEntity(RegisterResponse.class);
871        assertTrue(response.isRegistered());
872        assertFalse(response.isProfile());
873        assertTrue(response.isInUserSpace());
874        desc = (ComponentDescription) response.getDescription();
875        assertNotNull(desc);
876        assertEquals("Test1", desc.getName());
877        assertEquals("UPDATE DESCRIPTION!", desc.getDescription());
878        Date secondDate = AbstractDescription.getDate(desc.getRegistrationDate());
879        assertTrue(firstDate.before(secondDate) || firstDate.equals(secondDate));
880
881        spec = getUserComponent(desc);
882        assertNotNull(spec);
883        assertEquals("TESTNAME", spec.getCMDComponent().get(0).getName());
884        components = getUserComponents();
885        assertEquals(1, components.size());
886        assertEquals(0, getPublicComponents().size());
887    }
888
889    @Test
890    public void testUpdateProfile() throws Exception {
891        List<ProfileDescription> profiles = getUserProfiles();
892        assertEquals(0, profiles.size());
893        assertEquals(0, getPublicProfiles().size());
894
895        FormDataMultiPart form = createFormData(RegistryTestHelper.getTestProfileContent());
896        ClientResponse cResponse = getAuthenticatedResource(getResource().path("/registry/profiles").queryParam(USERSPACE_PARAM, "true")).type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
897        assertEquals(ClientResponse.Status.OK.getStatusCode(), cResponse.getStatus());
898        RegisterResponse response = cResponse.getEntity(RegisterResponse.class);
899        assertTrue(response.isRegistered());
900        assertTrue(response.isProfile());
901        assertTrue(response.isInUserSpace());
902        ProfileDescription desc = (ProfileDescription) response.getDescription();
903        assertNotNull(desc);
904        assertEquals("Test1", desc.getName());
905        assertEquals("My Test", desc.getDescription());
906        assertEquals("TestGroup", desc.getGroupName());
907        Date firstDate = AbstractDescription.getDate(desc.getRegistrationDate());
908        CMDComponentSpec spec = getUserProfile(desc);
909        assertNotNull(spec);
910        assertEquals("Actor", spec.getCMDComponent().get(0).getName());
911        profiles = getUserProfiles();
912        assertEquals(1, profiles.size());
913        assertEquals(0, getPublicComponents().size());
914
915        //Now update
916        form = createFormData(RegistryTestHelper.getTestProfileContent("TESTNAME"), "UPDATE DESCRIPTION!");
917        cResponse = getAuthenticatedResource(
918                getResource().path("/registry/profiles/" + desc.getId() + "/update").queryParam(USERSPACE_PARAM, "true")).type(
919                MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
920        assertEquals(ClientResponse.Status.OK.getStatusCode(), cResponse.getStatus());
921        response = cResponse.getEntity(RegisterResponse.class);
922        assertTrue(response.isRegistered());
923        assertTrue(response.isProfile());
924        assertTrue(response.isInUserSpace());
925        desc = (ProfileDescription) response.getDescription();
926        assertNotNull(desc);
927        assertEquals("Test1", desc.getName());
928        assertEquals("UPDATE DESCRIPTION!", desc.getDescription());
929        Date secondDate = AbstractDescription.getDate(desc.getRegistrationDate());
930        assertTrue(firstDate.before(secondDate) || firstDate.equals(secondDate));
931
932        spec = getUserProfile(desc);
933        assertNotNull(spec);
934        assertEquals("TESTNAME", spec.getCMDComponent().get(0).getName());
935        profiles = getUserProfiles();
936        assertEquals(1, profiles.size());
937        assertEquals(0, getPublicComponents().size());
938    }
939
940    private CMDComponentSpec getUserComponent(ComponentDescription desc) {
941        return getAuthenticatedResource(getResource().path("/registry/components/" + desc.getId()).queryParam(USERSPACE_PARAM, "true")).accept(MediaType.APPLICATION_XML).get(CMDComponentSpec.class);
942    }
943
944    private CMDComponentSpec getUserProfile(ProfileDescription desc) {
945        return getAuthenticatedResource(getResource().path("/registry/profiles/" + desc.getId()).queryParam(USERSPACE_PARAM, "true")).accept(MediaType.APPLICATION_XML).get(CMDComponentSpec.class);
946    }
947
948    private List<ComponentDescription> getPublicComponents() {
949        return getAuthenticatedResource("/registry/components").accept(MediaType.APPLICATION_XML).get(COMPONENT_LIST_GENERICTYPE);
950    }
951
952    private List<ComponentDescription> getUserComponents() {
953        return getAuthenticatedResource(getResource().path("/registry/components").queryParam(USERSPACE_PARAM, "true")).accept(
954                MediaType.APPLICATION_XML).get(COMPONENT_LIST_GENERICTYPE);
955    }
956
957    @Test
958    public void testRegisterComponent() throws Exception {
959        FormDataMultiPart form = new FormDataMultiPart();
960        form.field(ComponentRegistryRestService.DATA_FORM_FIELD, RegistryTestHelper.getComponentTestContent(),
961                MediaType.APPLICATION_OCTET_STREAM_TYPE);
962        form.field(ComponentRegistryRestService.NAME_FORM_FIELD, "ComponentTest1");
963        form.field(ComponentRegistryRestService.DESCRIPTION_FORM_FIELD, "My Test Component");
964        form.field(ComponentRegistryRestService.DOMAIN_FORM_FIELD, "TestDomain");
965        form.field(ComponentRegistryRestService.GROUP_FORM_FIELD, "TestGroup");
966        RegisterResponse response = getAuthenticatedResource("/registry/components").type(MediaType.MULTIPART_FORM_DATA).post(
967                RegisterResponse.class, form);
968        assertTrue(response.isRegistered());
969        assertFalse(response.isProfile());
970        assertFalse(response.isInUserSpace());
971        ComponentDescription desc = (ComponentDescription) response.getDescription();
972        assertNotNull(desc);
973        assertEquals("ComponentTest1", desc.getName());
974        assertEquals("My Test Component", desc.getDescription());
975        assertEquals(expectedUserId("JUnit@test.com"), desc.getUserId());
976        assertEquals("Database test user", desc.getCreatorName());
977        assertEquals("TestGroup", desc.getGroupName());
978        assertEquals("TestDomain", desc.getDomainName());
979        assertTrue(desc.getId().startsWith(ComponentRegistry.REGISTRY_ID + "c_"));
980        assertNotNull(desc.getRegistrationDate());
981        String url = getResource().getUriBuilder().build().toString();
982        assertEquals(url + "registry/components/" + desc.getId(), desc.getHref());
983    }
984
985    @Test
986    public void testRegisterComment() throws Exception {
987        FormDataMultiPart form = new FormDataMultiPart();
988        form.field(ComponentRegistryRestService.DATA_FORM_FIELD, RegistryTestHelper.getCommentTestContent(),
989                MediaType.APPLICATION_OCTET_STREAM_TYPE);
990        fillUp();
991        CommentResponse response = getAuthenticatedResource("/registry/profiles/clarin.eu:cr1:profile1/comments").type(MediaType.MULTIPART_FORM_DATA).post(
992                CommentResponse.class, form);
993        assertTrue(response.isRegistered());
994        assertFalse(response.isInUserSpace());
995        Comment comment = response.getComment();
996        assertNotNull(comment);
997        assertEquals("Actual", comment.getComment());
998        assertEquals("Database test user", comment.getUserName());
999        Assert.hasText(comment.getCommentDate());
1000        Assert.hasText(comment.getId());
1001
1002        // User id should not be serialized!
1003        assertEquals(null, comment.getUserId());
1004    }
1005
1006    @Test
1007    public void testRegisterCommentToNonExistent() throws Exception {
1008        FormDataMultiPart form = new FormDataMultiPart();
1009        form.field(ComponentRegistryRestService.DATA_FORM_FIELD, RegistryTestHelper.getCommentTestContent(),
1010                MediaType.APPLICATION_OCTET_STREAM_TYPE);
1011        fillUp();
1012        ClientResponse cResponse = getAuthenticatedResource("/registry/profiles/clarin.eu:cr1:profile99/comments").type(MediaType.MULTIPART_FORM_DATA).post(
1013                ClientResponse.class, form);
1014        assertEquals(500, cResponse.getStatus());
1015    }
1016
1017    @Test
1018    public void testRegisterCommentUnauthenticated() throws Exception {
1019        FormDataMultiPart form = new FormDataMultiPart();
1020        form.field(ComponentRegistryRestService.DATA_FORM_FIELD, RegistryTestHelper.getCommentTestContent(),
1021                MediaType.APPLICATION_OCTET_STREAM_TYPE);
1022        fillUp();
1023        ClientResponse cResponse = getResource().path("/registry/profiles/clarin.eu:cr1:profile1/comments").type(MediaType.MULTIPART_FORM_DATA).post(
1024                ClientResponse.class, form);
1025        assertEquals(401, cResponse.getStatus());
1026    }
1027
1028    @Test
1029    public void testRegisterProfileInvalidData() throws Exception {
1030        FormDataMultiPart form = new FormDataMultiPart();
1031        String notAValidProfile = "<CMD_ComponentSpec> </CMD_ComponentSpec>";
1032        form.field(ComponentRegistryRestService.DATA_FORM_FIELD, new ByteArrayInputStream(notAValidProfile.getBytes()),
1033                MediaType.APPLICATION_OCTET_STREAM_TYPE);
1034        form.field(ComponentRegistryRestService.NAME_FORM_FIELD, "ProfileTest1");
1035        form.field(ComponentRegistryRestService.DOMAIN_FORM_FIELD, "Domain");
1036        form.field(ComponentRegistryRestService.GROUP_FORM_FIELD, "Group");
1037        form.field(ComponentRegistryRestService.DESCRIPTION_FORM_FIELD, "My Test Profile");
1038        RegisterResponse postResponse = getAuthenticatedResource("/registry/profiles").type(MediaType.MULTIPART_FORM_DATA).post(
1039                RegisterResponse.class, form);
1040        assertTrue(postResponse.isProfile());
1041        assertFalse(postResponse.isRegistered());
1042        assertEquals(1, postResponse.getErrors().size());
1043        assertTrue(postResponse.getErrors().get(0).contains("isProfile"));
1044    }
1045
1046    /**
1047     * Two elements on the same level with the same name violates schematron rule, and should fail validation
1048     *
1049     * @throws Exception
1050     */
1051    @Test
1052    public void testRegisterInvalidProfile() throws Exception {
1053        FormDataMultiPart form = new FormDataMultiPart();
1054        String profileContent = "";
1055        profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
1056        profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
1057        profileContent += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
1058        profileContent += "    <Header />\n";
1059        profileContent += "    <CMD_Component name=\"ProfileTest1\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n";
1060        profileContent += "        <CMD_Element name=\"Age\">\n";
1061        profileContent += "            <ValueScheme>\n";
1062        profileContent += "                <pattern>[23][0-9]</pattern>\n";
1063        profileContent += "            </ValueScheme>\n";
1064        profileContent += "        </CMD_Element>\n";
1065        profileContent += "        <CMD_Element name=\"Age\">\n";
1066        profileContent += "            <ValueScheme>\n";
1067        profileContent += "                <pattern>[23][0-9]</pattern>\n";
1068        profileContent += "            </ValueScheme>\n";
1069        profileContent += "        </CMD_Element>\n";
1070        profileContent += "    </CMD_Component>\n";
1071        profileContent += "</CMD_ComponentSpec>\n";
1072        form.field(ComponentRegistryRestService.DATA_FORM_FIELD, RegistryTestHelper.getComponentContent(profileContent),
1073                MediaType.APPLICATION_OCTET_STREAM_TYPE);
1074        form.field(ComponentRegistryRestService.NAME_FORM_FIELD, "ProfileTest1");
1075        form.field(ComponentRegistryRestService.DOMAIN_FORM_FIELD, "TestDomain");
1076        form.field(ComponentRegistryRestService.DESCRIPTION_FORM_FIELD, "My Test Profile");
1077        form.field(ComponentRegistryRestService.GROUP_FORM_FIELD, "My Test Group");
1078        RegisterResponse response = getAuthenticatedResource("/registry/profiles").type(MediaType.MULTIPART_FORM_DATA).post(
1079                RegisterResponse.class, form);
1080        assertFalse("Subsequent elements should not be allowed to have the same name", response.isRegistered());
1081        assertTrue(response.getErrors().get(0).contains(MDValidator.PARSE_ERROR));
1082    }
1083
1084    @Test
1085    public void testRegisterProfileInvalidDescriptionAndContent() throws Exception {
1086        FormDataMultiPart form = new FormDataMultiPart();
1087        String profileContent = "";
1088        profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
1089        profileContent += "<CMD_ComponentSpec> \n"; //No isProfile attribute
1090        profileContent += "    <Header />\n";
1091        profileContent += "    <CMD_Component name=\"Actor\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n";
1092        profileContent += "        <AttributeList>\n";
1093        profileContent += "            <Attribute>\n";
1094        profileContent += "                <Name>Name</Name>\n";
1095        profileContent += "                <Type>string</Type>\n";
1096        profileContent += "            </Attribute>\n";
1097        profileContent += "        </AttributeList>\n";
1098        profileContent += "    </CMD_Component>\n";
1099        profileContent += "</CMD_ComponentSpec>\n";
1100        form.field("data", new ByteArrayInputStream(profileContent.getBytes()), MediaType.APPLICATION_OCTET_STREAM_TYPE);
1101        form.field("name", "");//Empty name so invalid
1102        form.field("description", "My Test Profile");
1103        RegisterResponse response = getAuthenticatedResource("/registry/profiles").type(MediaType.MULTIPART_FORM_DATA).post(
1104                RegisterResponse.class, form);
1105        assertFalse(response.isRegistered());
1106        assertEquals(2, response.getErrors().size());
1107        assertNotNull(response.getErrors().get(0));
1108        assertEquals(MDValidator.PARSE_ERROR, response.getErrors().get(1).substring(0, MDValidator.PARSE_ERROR.length()));
1109    }
1110
1111    @Test
1112    public void testRegisterComponentAsProfile() throws Exception {
1113        FormDataMultiPart form = new FormDataMultiPart();
1114        form.field(ComponentRegistryRestService.DATA_FORM_FIELD, RegistryTestHelper.getComponentTestContent(),
1115                MediaType.APPLICATION_OCTET_STREAM_TYPE);
1116        form.field(ComponentRegistryRestService.NAME_FORM_FIELD, "t");
1117        form.field(ComponentRegistryRestService.DOMAIN_FORM_FIELD, "domain");
1118        form.field(ComponentRegistryRestService.DESCRIPTION_FORM_FIELD, "My Test");
1119        form.field(ComponentRegistryRestService.GROUP_FORM_FIELD, "My Group");
1120        RegisterResponse response = getAuthenticatedResource("/registry/profiles").type(MediaType.MULTIPART_FORM_DATA).post(
1121                RegisterResponse.class, form);
1122        assertFalse(response.isRegistered());
1123        assertTrue(response.isProfile());
1124        assertEquals(1, response.getErrors().size());
1125        assertEquals(MDValidator.MISMATCH_ERROR, response.getErrors().get(0));
1126    }
1127}
Note: See TracBrowser for help on using the repository browser.