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

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

When registering comment, use display name from DB. Fixes #144

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