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

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

Added group service. Tested via the tomcat on loclahots (test URI and postman), old unit tests are adjusted and work well. Todo: retest on localhost tomcat, look at run-time exceptions, add new unit tests, adjust front-end

File size: 68.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.components.CMDComponentType;
7import clarin.cmdi.componentregistry.impl.database.ComponentRegistryBeanFactory;
8import clarin.cmdi.componentregistry.impl.database.ComponentRegistryTestDatabase;
9import clarin.cmdi.componentregistry.model.BaseDescription;
10import clarin.cmdi.componentregistry.model.Comment;
11import clarin.cmdi.componentregistry.model.CommentResponse;
12import clarin.cmdi.componentregistry.model.ComponentDescription;
13import clarin.cmdi.componentregistry.model.ProfileDescription;
14import clarin.cmdi.componentregistry.model.RegisterResponse;
15import static clarin.cmdi.componentregistry.rest.ComponentRegistryRestService.REGISTRY_SPACE_PARAM;
16
17import com.sun.jersey.api.client.ClientResponse;
18import com.sun.jersey.api.client.UniformInterfaceException;
19import com.sun.jersey.api.representation.Form;
20import com.sun.jersey.multipart.FormDataMultiPart;
21
22import java.io.ByteArrayInputStream;
23import java.util.ArrayList;
24import java.util.Date;
25import java.util.List;
26
27import javax.ws.rs.core.MediaType;
28import javax.ws.rs.core.Response.Status;
29
30import static org.junit.Assert.*;
31
32import org.junit.Before;
33import org.junit.Ignore;
34import org.junit.Test;
35import org.springframework.beans.factory.annotation.Autowired;
36import org.springframework.jdbc.core.JdbcTemplate;
37import org.springframework.util.Assert;
38
39/**
40 * Launches a servlet container environment and performs HTTP calls to the
41 * {@link ComponentRegistryRestService}
42 *
43 * @author george.georgovassilis@mpi.nl
44 *
45 */
46public class ComponentRegistryRestServiceTest extends
47        ComponentRegistryRestServiceTestCase {
48
49    @Autowired
50    private ComponentRegistryFactory componentRegistryFactory;
51    @Autowired
52    private ComponentRegistryBeanFactory componentRegistryBeanFactory;
53    @Autowired
54    private JdbcTemplate jdbcTemplate;
55    private ComponentRegistry baseRegistry;
56
57    @Before
58    public void init() {
59        ComponentRegistryTestDatabase.resetAndCreateAllTables(jdbcTemplate);
60        createUserRecord();
61        baseRegistry = componentRegistryFactory.getBaseRegistry(DummyPrincipal.DUMMY_CREDENTIALS);
62    }
63
64    private String expectedUserId(String principal) {
65        return getUserDao().getByPrincipalName(principal).getId().toString();
66    }
67
68    private ComponentDescription component1;
69    private ComponentDescription component2;
70    private ProfileDescription profile1;
71    private ProfileDescription profile2;
72   
73    private ComponentDescription component3;
74    private ProfileDescription profile3;
75   
76    private Comment profile1Comment1;
77    private Comment profile1Comment2;
78    private Comment component1Comment3;
79    private Comment component1Comment4;
80   
81    private Comment profile3Comment5;
82    private Comment component3Comment7;
83
84    private void fillUpPublicItems() throws Exception {
85       
86        profile1 = RegistryTestHelper.addProfile(baseRegistry, "profile2", true);
87        profile2 = RegistryTestHelper.addProfile(baseRegistry, "profile1", true);
88        component1 = RegistryTestHelper.addComponent(baseRegistry,
89                "component2", true);
90        component2 = RegistryTestHelper.addComponent(baseRegistry,
91                "component1", true);
92        profile1Comment2 = RegistryTestHelper.addComment(baseRegistry, "comment2",
93                ProfileDescription.PROFILE_PREFIX + "profile1",
94                "JUnit@test.com");
95        profile1Comment1 = RegistryTestHelper.addComment(baseRegistry, "comment1",
96                ProfileDescription.PROFILE_PREFIX + "profile1",
97                "JUnit@test.com");
98        component1Comment3 = RegistryTestHelper.addComment(baseRegistry, "comment3",
99                ComponentDescription.COMPONENT_PREFIX + "component1",
100                "JUnit@test.com");
101        component1Comment4 = RegistryTestHelper.addComment(baseRegistry, "comment4",
102                ComponentDescription.COMPONENT_PREFIX + "component1",
103                "JUnit@test.com");
104    }
105   
106    private void fillUpPrivateItems() throws Exception {
107        profile3 = RegistryTestHelper.addProfile(baseRegistry, "profile3", false);
108        component3 = RegistryTestHelper.addComponent(baseRegistry,
109                "component3", false);
110        profile3Comment5 = RegistryTestHelper.addComment(baseRegistry, "comment5",
111                ProfileDescription.PROFILE_PREFIX + "profile3",
112                "JUnit@test.com");
113        component3Comment7 = RegistryTestHelper.addComment(baseRegistry, "comment7",
114                ComponentDescription.COMPONENT_PREFIX + "component3",
115                "JUnit@test.com");
116    }
117
118    @Test//ok
119    public void testGetPublicProfiles() throws Exception {
120       
121        fillUpPublicItems();
122       
123        RegistryTestHelper.addProfile(baseRegistry, "PROFILE2", true);
124        List<ProfileDescription> response = this.getAuthenticatedResource(getResource()
125                .path("/registry/profiles")).accept(MediaType.APPLICATION_XML)
126                .get(PROFILE_LIST_GENERICTYPE);
127        assertEquals(3, response.size());
128        response = this.getAuthenticatedResource(getResource()
129                .path("/registry/profiles"))
130                .accept(MediaType.APPLICATION_JSON)
131                .get(PROFILE_LIST_GENERICTYPE);
132        assertEquals(3, response.size());
133        assertEquals("profile1", response.get(0).getName());
134        assertEquals("PROFILE2", response.get(1).getName());
135        assertEquals("profile2", response.get(2).getName());
136    }
137
138    @Test //ok
139    public void testGetPublicComponents() throws Exception {
140       
141        fillUpPublicItems();
142       
143        RegistryTestHelper.addComponent(baseRegistry, "COMPONENT2", true);
144        List<ComponentDescription> response = this.getAuthenticatedResource(getResource()
145                .path("/registry/components")).accept(MediaType.APPLICATION_XML)
146                .get(COMPONENT_LIST_GENERICTYPE);
147        assertEquals(3, response.size());
148        response = this.getAuthenticatedResource(getResource()
149                .path("/registry/components"))
150                .accept(MediaType.APPLICATION_JSON)
151                .get(COMPONENT_LIST_GENERICTYPE);
152        assertEquals(3, response.size());
153        assertEquals("component1", response.get(0).getName());
154        assertEquals("COMPONENT2", response.get(1).getName());
155        assertEquals("component2", response.get(2).getName());
156    }
157
158    @Test  //ok
159    public void testGetUserComponents() throws Exception {
160       
161        fillUpPrivateItems();
162       
163        List<ComponentDescription> response = this.getUserComponents();
164        assertEquals(1, response.size());
165       
166        ////
167       
168        fillUpPublicItems();
169        response = getAuthenticatedResource(getResource().path("/registry/components")).accept(
170                MediaType.APPLICATION_JSON).get(COMPONENT_LIST_GENERICTYPE);
171        assertEquals("Get public components", 2, response.size());
172       
173        ClientResponse cResponse = getResource().path("/registry/components")
174                .queryParam(REGISTRY_SPACE_PARAM, "private")
175                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
176        assertEquals("Trying to get userspace without credentials", 401,
177                cResponse.getStatus());
178    }
179
180    @Test //ok
181    public void testGetPublicComponent() throws Exception {
182       
183        fillUpPublicItems();
184       
185        String id = ComponentDescription.COMPONENT_PREFIX + "component1";
186        String id2 = ComponentDescription.COMPONENT_PREFIX + "component2";
187        CMDComponentSpec component = this.getAuthenticatedResource(getResource()
188                .path("/registry/components/" + id))
189                .accept(MediaType.APPLICATION_JSON).get(CMDComponentSpec.class);
190        assertNotNull(component);
191        assertEquals("Access", component.getCMDComponent().getName());
192       
193       
194        component = this.getAuthenticatedResource(getResource().path("/registry/components/" + id2))
195                .accept(MediaType.APPLICATION_XML).get(CMDComponentSpec.class);
196        assertNotNull(component);
197        assertEquals("Access", component.getCMDComponent().getName());
198        assertEquals(id2, component.getHeader().getID());
199        assertEquals("component2", component.getHeader().getName());
200        assertEquals("Test Description", component.getHeader().getDescription());
201    }
202
203    @Test   // ok   
204    public void testGetCommentsInPublicProfile() throws Exception {
205       
206        fillUpPublicItems();
207       
208        String id = ProfileDescription.PROFILE_PREFIX + "profile1";
209        RegistryTestHelper.addComment(baseRegistry, "COMMENT1", id,
210                "JUnit@test.com");
211        List<Comment> response = this.getAuthenticatedResource(getResource()
212                .path("/registry/profiles/" + id + "/comments/"))
213                .accept(MediaType.APPLICATION_XML)
214                .get(COMMENT_LIST_GENERICTYPE);
215        assertEquals(3, response.size());
216       
217        response = this.getAuthenticatedResource(getResource().path("/registry/profiles/" + id + "/comments"))
218                .accept(MediaType.APPLICATION_JSON)
219                .get(COMMENT_LIST_GENERICTYPE);
220        assertEquals(3, response.size());
221        assertEquals("comment2", response.get(0).getComment());
222        assertEquals("comment1", response.get(1).getComment());
223        assertEquals("COMMENT1", response.get(2).getComment());
224
225        assertEquals("Database test user", response.get(0).getUserName());
226    }
227
228    @Test // ok
229    public void testGetCommentsInPubliComponent() throws Exception {
230        fillUpPublicItems();
231       
232        String id = ComponentDescription.COMPONENT_PREFIX + "component1";
233        RegistryTestHelper.addComment(baseRegistry, "COMMENT2", id,
234                "JUnit@test.com");
235        List<Comment> response = this.getAuthenticatedResource(getResource()
236                .path("/registry/components/" + id + "/comments/"))
237                .accept(MediaType.APPLICATION_XML)
238                .get(COMMENT_LIST_GENERICTYPE);
239        assertEquals(3, response.size());
240        response = this.getAuthenticatedResource(getResource()
241                .path("/registry/components/" + id + "/comments"))
242                .accept(MediaType.APPLICATION_JSON)
243                .get(COMMENT_LIST_GENERICTYPE);
244        assertEquals(3, response.size());
245        assertEquals("comment3", response.get(0).getComment());
246        assertEquals("comment4", response.get(1).getComment());
247        assertEquals("COMMENT2", response.get(2).getComment());
248
249        assertEquals("Database test user", response.get(0).getUserName());
250    }
251
252    @Test // ok
253    public void testGetSpecifiedCommentInPublicComponent() throws Exception {
254       
255        fillUpPublicItems();
256       
257        String id = ComponentDescription.COMPONENT_PREFIX + "component1";
258        Comment comment = this.getAuthenticatedResource(getResource()
259                .path("/registry/components/" + id + "/comments/"+component1Comment3.getId()))
260                .accept(MediaType.APPLICATION_JSON).get(Comment.class);
261        assertNotNull(comment);
262        assertEquals("comment3", comment.getComment());
263        assertEquals(component1Comment3.getId(), comment.getId());
264        comment = this.getAuthenticatedResource(getResource()
265                .path("/registry/components/" + id + "/comments/"+component1Comment4.getId()))
266                .accept(MediaType.APPLICATION_JSON).get(Comment.class);
267        assertNotNull(comment);
268        assertEquals("comment4", comment.getComment());
269        assertEquals(component1Comment4.getId(), comment.getId());
270        assertEquals(id, comment.getComponentId());
271    }
272
273    @Test //ok
274    public void testGetSpecifiedCommentInPublicProfile() throws Exception {
275       
276        fillUpPublicItems();
277       
278        String id = ProfileDescription.PROFILE_PREFIX + "profile1";
279        Comment comment = this.getAuthenticatedResource(getResource()
280                .path("/registry/profiles/" + id + "/comments/"+profile1Comment2.getId()))
281                .accept(MediaType.APPLICATION_JSON).get(Comment.class);
282        assertNotNull(comment);
283        assertEquals("comment2", comment.getComment());
284        assertEquals(profile1Comment2.getId(), comment.getId());
285       
286        comment = this.getAuthenticatedResource(getResource()
287                .path("/registry/profiles/" + id + "/comments/"+profile1Comment1.getId()))
288                .accept(MediaType.APPLICATION_JSON).get(Comment.class);
289        assertNotNull(comment);
290        assertEquals("comment1", comment.getComment());
291        assertEquals(profile1Comment1.getId(), comment.getId());
292        assertEquals(id, comment.getComponentId());
293    }
294
295    @Test  //ok
296    public void testDeleteCommentFromPublicComponent() throws Exception {
297       
298        fillUpPublicItems();
299       
300        String id = ComponentDescription.COMPONENT_PREFIX + "component1";
301        String id2 = ComponentDescription.COMPONENT_PREFIX + "component2";
302        List<Comment> comments = this.getAuthenticatedResource(getResource().path(
303                "/registry/components/" + id + "/comments")).get(
304                COMMENT_LIST_GENERICTYPE);
305        assertEquals(2, comments.size());
306        Comment aComment = this.getAuthenticatedResource(getResource().path(
307                "/registry/components/" + id + "/comments/"+component1Comment3.getId()))
308                .get(Comment.class);
309        assertNotNull(aComment);
310
311        // Try to delete from other component
312        ClientResponse response = getAuthenticatedResource(
313                "/registry/components/" + id2 + "/comments/"+component1Comment4.getId()).delete(
314                ClientResponse.class);
315        assertEquals(500, response.getStatus());
316        // Delete from correct component
317        response = getAuthenticatedResource(
318                "/registry/components/" + id + "/comments/"+component1Comment3.getId()).delete(
319                ClientResponse.class);
320        assertEquals(200, response.getStatus());
321
322        comments = this.getAuthenticatedResource(getResource().path(
323                "/registry/components/" + id + "/comments/")).get(
324                COMMENT_LIST_GENERICTYPE);
325        assertEquals(1, comments.size());
326
327        response = getAuthenticatedResource(
328                "/registry/components/" + id + "/comments/"+component1Comment4.getId()).delete(
329                ClientResponse.class);
330        assertEquals(200, response.getStatus());
331
332        comments = this.getAuthenticatedResource(getResource().path(
333                "/registry/components/" + id + "/comments")).get(
334                COMMENT_LIST_GENERICTYPE);
335        assertEquals(0, comments.size());
336    }
337
338    @Test
339    public void testManipulateCommentFromPublicComponent() throws Exception {
340       
341        fillUpPublicItems();
342       
343        List<Comment> comments = this.getAuthenticatedResource(getResource().path(
344                "/registry/components/" + ComponentDescription.COMPONENT_PREFIX
345                        + "component1/comments")).get(COMMENT_LIST_GENERICTYPE);
346        assertEquals(2, comments.size());
347        Comment aComment = this.getAuthenticatedResource(getResource().path(
348                "/registry/components/" + ComponentDescription.COMPONENT_PREFIX
349                        + "component1/comments/"+component1Comment3.getId())).get(Comment.class);
350        assertNotNull(aComment);
351
352        Form manipulateForm = new Form();
353        manipulateForm.add("method", "delete");
354
355        // Try to delete from other component
356        ClientResponse response = getAuthenticatedResource(
357                "/registry/components/" + ComponentDescription.COMPONENT_PREFIX
358                        + "component1/comments/"+component1Comment3.getId()).post(ClientResponse.class,
359                manipulateForm);
360        assertEquals(200, response.getStatus());
361
362        comments = this.getAuthenticatedResource(getResource().path(
363                "/registry/components/" + ComponentDescription.COMPONENT_PREFIX
364                        + "component1/comments/")).get(COMMENT_LIST_GENERICTYPE);
365        assertEquals(1, comments.size());
366    }
367
368    @Test
369    public void testDeleteCommentFromPublicProfile() throws Exception {
370       
371        fillUpPublicItems();
372       
373        List<Comment> comments = this.getAuthenticatedResource(getResource().path(
374                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
375                        + "profile1/comments")).get(COMMENT_LIST_GENERICTYPE);
376        assertEquals(2, comments.size());
377        Comment aComment = this.getAuthenticatedResource(getResource().path(
378                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
379                        + "profile1/comments/"+profile1Comment1.getId())).get(Comment.class);
380        assertNotNull(aComment);
381
382        // Try to delete from other profile
383        ClientResponse response = getAuthenticatedResource(
384                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
385                        + "profile2/comments/9999").delete(ClientResponse.class);
386        assertEquals(500, response.getStatus());
387        // Delete from correct profile
388        response = getAuthenticatedResource(
389                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
390                        + "profile1/comments/"+profile1Comment1.getId()).delete(ClientResponse.class);
391        assertEquals(200, response.getStatus());
392
393        comments = this.getAuthenticatedResource(getResource().path(
394                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
395                        + "profile1/comments/")).get(COMMENT_LIST_GENERICTYPE);
396        assertEquals(1, comments.size());
397
398        response = getAuthenticatedResource(
399                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
400                        + "profile1/comments/"+profile1Comment2.getId()).delete(ClientResponse.class);
401        assertEquals(200, response.getStatus());
402
403        comments = this.getAuthenticatedResource(getResource().path(
404                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
405                        + "profile1/comments")).get(COMMENT_LIST_GENERICTYPE);
406        assertEquals(0, comments.size());
407    }
408
409    @Test
410    public void testManipulateCommentFromPublicProfile() throws Exception {
411       
412        fillUpPublicItems();
413       
414        List<Comment> comments = this.getAuthenticatedResource(getResource().path(
415                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
416                        + "profile1/comments")).get(COMMENT_LIST_GENERICTYPE);
417        assertEquals(2, comments.size());
418        Comment aComment = this.getAuthenticatedResource(getResource().path(
419                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
420                        + "profile1/comments/"+profile1Comment2.getId())).get(Comment.class);
421        assertNotNull(aComment);
422
423        Form manipulateForm = new Form();
424        manipulateForm.add("method", "delete");
425        // Delete from correct profile
426        ClientResponse response = getAuthenticatedResource(
427                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
428                        + "profile1/comments/"+profile1Comment2.getId()).post(ClientResponse.class,
429                manipulateForm);
430        assertEquals(200, response.getStatus());
431
432        comments = this.getAuthenticatedResource(getResource().path(
433                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
434                        + "profile1/comments/")).get(COMMENT_LIST_GENERICTYPE);
435        assertEquals(1, comments.size());
436    }
437
438    @Test
439    public void testDeletePublicComponent() throws Exception {
440       
441        fillUpPublicItems();
442       
443        List<ComponentDescription> components = this.getAuthenticatedResource(getResource().path(
444                "/registry/components")).get(COMPONENT_LIST_GENERICTYPE);
445        assertEquals(2, components.size());
446        CMDComponentSpec profile = this.getAuthenticatedResource(getResource().path(
447                "/registry/components/" + ComponentDescription.COMPONENT_PREFIX
448                        + "component1")).get(CMDComponentSpec.class);
449        assertNotNull(profile);
450        ClientResponse response = getAuthenticatedResource(
451                "/registry/components/" + ComponentDescription.COMPONENT_PREFIX
452                        + "component1").delete(ClientResponse.class);
453        assertEquals(200, response.getStatus());
454
455        components = this.getAuthenticatedResource(getResource().path("/registry/components")).get(
456                COMPONENT_LIST_GENERICTYPE);
457        assertEquals(1, components.size());
458
459        response = getAuthenticatedResource(
460                "/registry/components/" + ComponentDescription.COMPONENT_PREFIX
461                        + "component2").delete(ClientResponse.class);
462        assertEquals(200, response.getStatus());
463
464        components = this.getAuthenticatedResource(getResource().path("/registry/components")).get(
465                COMPONENT_LIST_GENERICTYPE);
466        assertEquals(0, components.size());
467
468        response = getAuthenticatedResource(
469                "/registry/components/" + ComponentDescription.COMPONENT_PREFIX
470                        + "component1").delete(ClientResponse.class);
471        assertEquals(200, response.getStatus());
472    }
473
474    @Test
475    public void testDeletePublicComponentStillUsed() throws Exception {
476       
477        fillUpPublicItems();
478       
479        String content = "";
480        content += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
481        content += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
482        content += "    <Header/>\n";
483        content += "    <CMD_Component name=\"XXX\" CardinalityMin=\"1\" CardinalityMax=\"10\">\n";
484        content += "        <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n";
485        content += "    </CMD_Component>\n";
486        content += "</CMD_ComponentSpec>\n";
487        ComponentDescription compDesc1 = RegistryTestHelper.addComponent(
488                baseRegistry, "XXX1", content, true);
489
490        content = "";
491        content += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
492        content += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
493        content += "    <Header/>\n";
494        content += "    <CMD_Component name=\"YYY\" CardinalityMin=\"1\" CardinalityMax=\"unbounded\">\n";
495        content += "        <CMD_Component ComponentId=\"" + compDesc1.getId()
496                + "\" CardinalityMin=\"0\" CardinalityMax=\"99\">\n";
497        content += "        </CMD_Component>\n";
498        content += "    </CMD_Component>\n";
499        content += "</CMD_ComponentSpec>\n";
500        ComponentDescription compDesc2 = RegistryTestHelper.addComponent(
501                baseRegistry, "YYY1", content,true);
502
503        content = "";
504        content += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
505        content += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
506        content += "    <Header/>\n";
507        content += "    <CMD_Component name=\"ZZZ\" CardinalityMin=\"1\" CardinalityMax=\"unbounded\">\n";
508        content += "        <CMD_Component ComponentId=\"" + compDesc1.getId()
509                + "\" CardinalityMin=\"0\" CardinalityMax=\"99\">\n";
510        content += "        </CMD_Component>\n";
511        content += "    </CMD_Component>\n";
512        content += "</CMD_ComponentSpec>\n";
513        ProfileDescription profile = RegistryTestHelper.addProfile(
514                baseRegistry, "TestProfile3", content, true);
515
516        List<ComponentDescription> components = this.getAuthenticatedResource(getResource().path(
517                "/registry/components")).get(COMPONENT_LIST_GENERICTYPE);
518        assertEquals(4, components.size());
519
520        ClientResponse response = getAuthenticatedResource(
521                "/registry/components/" + compDesc1.getId()).delete(
522                ClientResponse.class);
523        assertEquals(Status.FORBIDDEN.getStatusCode(), response.getStatus());
524        assertEquals(
525                "Component is still in use by other components or profiles. Request component usage for details.",
526                response.getEntity(String.class));
527
528        components = this.getAuthenticatedResource(getResource().path("/registry/components")).get(
529                COMPONENT_LIST_GENERICTYPE);
530        assertEquals(4, components.size());
531
532        response = getAuthenticatedResource(
533                "/registry/profiles/" + profile.getId()).delete(
534                ClientResponse.class);
535        assertEquals(200, response.getStatus());
536        response = getAuthenticatedResource(
537                "/registry/components/" + compDesc2.getId()).delete(
538                ClientResponse.class);
539        assertEquals(200, response.getStatus());
540        response = getAuthenticatedResource(
541                "/registry/components/" + compDesc1.getId()).delete(
542                ClientResponse.class);
543        assertEquals(200, response.getStatus());
544        components = this.getAuthenticatedResource(getResource().path("/registry/components")).get(
545                COMPONENT_LIST_GENERICTYPE);
546        assertEquals(2, components.size());
547    }
548
549    @Test
550    public void testManipulatePublicComponent() throws Exception {
551       
552        fillUpPublicItems();
553       
554        List<ComponentDescription> components = this.getAuthenticatedResource(getResource().path(
555                "/registry/components")).get(COMPONENT_LIST_GENERICTYPE);
556        assertEquals(2, components.size());
557        CMDComponentSpec profile = this.getAuthenticatedResource(getResource().path(
558                "/registry/components/" + ComponentDescription.COMPONENT_PREFIX
559                        + "component1")).get(CMDComponentSpec.class);
560        assertNotNull(profile);
561
562        Form manipulateForm = new Form();
563        manipulateForm.add("method", "delete");
564
565        ClientResponse response = getAuthenticatedResource(
566                "/registry/components/" + ComponentDescription.COMPONENT_PREFIX
567                        + "component1").post(ClientResponse.class,
568                manipulateForm);
569        assertEquals(200, response.getStatus());
570
571        components = this.getAuthenticatedResource(getResource().path("/registry/components")).get(
572                COMPONENT_LIST_GENERICTYPE);
573        assertEquals(1, components.size());
574
575        response = getAuthenticatedResource(
576                "/registry/components/" + ComponentDescription.COMPONENT_PREFIX
577                        + "component2").post(ClientResponse.class,
578                manipulateForm);
579        assertEquals(200, response.getStatus());
580    }
581
582    @Test
583    public void testGetRegisteredProfile() throws Exception {
584       
585        fillUpPublicItems();
586       
587        String id = ProfileDescription.PROFILE_PREFIX + "profile1";
588        String id2 = ProfileDescription.PROFILE_PREFIX + "profile2";
589        CMDComponentSpec profile = this.getAuthenticatedResource(getResource()
590                .path("/registry/profiles/" + id))
591                .accept(MediaType.APPLICATION_JSON).get(CMDComponentSpec.class);
592        assertNotNull(profile);
593        assertEquals("Actor", profile.getCMDComponent().getName());
594        profile = this.getAuthenticatedResource(getResource().path("/registry/profiles/" + id2))
595                .accept(MediaType.APPLICATION_XML).get(CMDComponentSpec.class);
596        assertNotNull(profile);
597        assertEquals("Actor", profile.getCMDComponent().getName());
598
599        assertEquals(id2, profile.getHeader().getID());
600        assertEquals("profile2", profile.getHeader().getName());
601        assertEquals("Test Description", profile.getHeader().getDescription());
602
603        try {
604            profile = this.getAuthenticatedResource(getResource()
605                    .path("/registry/profiles/"
606                            + ProfileDescription.PROFILE_PREFIX + "profileXXXX"))
607                    .accept(MediaType.APPLICATION_XML)
608                    .get(CMDComponentSpec.class);
609            fail("Exception should have been thrown resource does not exist, HttpStatusCode 404");
610        } catch (UniformInterfaceException e) {
611            assertEquals(404, e.getResponse().getStatus());
612        }
613    }
614
615    @Test
616    public void testGetRegisteredProfileRawData() throws Exception {
617       
618        fillUpPublicItems();
619       
620        String profile = this.getAuthenticatedResource(getResource()
621                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
622                        + "profile1/xsd")).accept(MediaType.TEXT_XML)
623                .get(String.class).trim();
624        assertTrue(profile
625                .startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?><xs:schema"));
626        assertTrue(profile.endsWith("</xs:schema>"));
627
628        profile = this.getAuthenticatedResource(getResource()
629                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
630                        + "profile1/xml")).accept(MediaType.TEXT_XML)
631                .get(String.class).trim();
632        assertTrue(profile
633                .startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<CMD_ComponentSpec"));
634        assertTrue(profile.endsWith("</CMD_ComponentSpec>"));
635        assertTrue(profile.contains("xsi:schemaLocation"));
636
637        try {
638            this.getAuthenticatedResource(getResource()
639                    .path("/registry/components/"
640                            + ComponentDescription.COMPONENT_PREFIX
641                            + "component1/xsl")).accept(MediaType.TEXT_XML)
642                    .get(String.class);
643            fail("Should have thrown exception, unsupported path parameter");
644        } catch (UniformInterfaceException e) {// server error
645        }
646    }
647
648    //
649    @Test
650    public void testPrivateProfileXsd() throws Exception {
651       
652        fillUpPublicItems();
653       
654        FormDataMultiPart form = createFormData(RegistryTestHelper
655                .getTestProfileContent());
656        RegisterResponse response = getAuthenticatedResource(getResource().path("/registry/profiles").queryParam(
657                        REGISTRY_SPACE_PARAM, "private")).type(
658                MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class,
659                form);
660        assertTrue(response.isProfile());
661        assertEquals(1, getUserProfiles().size());
662        BaseDescription desc = response.getDescription();
663        String profile = this.getAuthenticatedResource(getResource()
664                .path("/registry/profiles/" + desc.getId() + "/xsd"))
665                .accept(MediaType.TEXT_XML).get(String.class).trim();
666        assertTrue(profile
667                .startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?><xs:schema"));
668        assertTrue(profile.endsWith("</xs:schema>"));
669    }
670
671    @Test
672    public void testPrivateComponentXsd() throws Exception {
673       
674        fillUpPublicItems();
675       
676        FormDataMultiPart form = createFormData(RegistryTestHelper
677                .getComponentTestContent());
678        RegisterResponse response = getAuthenticatedResource(
679                getResource().path("/registry/components").queryParam(
680                        REGISTRY_SPACE_PARAM, "private")).type(
681                MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class,
682                form);
683        assertFalse(response.isProfile());
684        assertEquals(1, getUserComponents().size());
685        BaseDescription desc = response.getDescription();
686        String profile = this.getAuthenticatedResource(getResource()
687                .path("/registry/components/" + desc.getId() + "/xsd"))
688                .accept(MediaType.TEXT_XML).get(String.class).trim();
689        assertTrue(profile
690                .startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?><xs:schema"));
691        assertTrue(profile.endsWith("</xs:schema>"));
692    }
693
694    @Test
695    public void testDeleteRegisteredProfile() throws Exception {
696       
697        fillUpPublicItems();
698       
699        List<ProfileDescription> profiles = this.getAuthenticatedResource(getResource().path(
700                "/registry/profiles")).get(PROFILE_LIST_GENERICTYPE);
701        assertEquals(2, profiles.size());
702        CMDComponentSpec profile = this.getAuthenticatedResource(getResource().path(
703                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
704                        + "profile1")).get(CMDComponentSpec.class);
705        assertNotNull(profile);
706
707        ClientResponse response = getAuthenticatedResource(
708                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
709                        + "profile1").delete(ClientResponse.class);
710        assertEquals(200, response.getStatus());
711
712        profiles = this.getAuthenticatedResource(getResource().path("/registry/profiles")).get(
713                PROFILE_LIST_GENERICTYPE);
714        assertEquals(1, profiles.size());
715
716        response = getAuthenticatedResource(
717                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
718                        + "profile2").delete(ClientResponse.class);
719        assertEquals(200, response.getStatus());
720
721        profiles = this.getAuthenticatedResource(getResource().path("/registry/profiles")).get(
722                PROFILE_LIST_GENERICTYPE);
723        assertEquals(0, profiles.size());
724
725        response = getAuthenticatedResource(
726                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
727                        + "profile1").delete(ClientResponse.class);
728        assertEquals(404, response.getStatus());
729    }
730
731    @Test
732    public void testManipulateRegisteredProfile() throws Exception {
733       
734        fillUpPublicItems();
735       
736        List<ProfileDescription> profiles = this.getAuthenticatedResource(getResource().path(
737                "/registry/profiles")).get(PROFILE_LIST_GENERICTYPE);
738        assertEquals(2, profiles.size());
739        CMDComponentSpec profile = this.getAuthenticatedResource(getResource().path(
740                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
741                        + "profile1")).get(CMDComponentSpec.class);
742        assertNotNull(profile);
743
744        Form manipulateForm = new Form();
745        manipulateForm.add("method", "delete");
746
747        ClientResponse response = getAuthenticatedResource(
748                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
749                        + "profile1")
750                .post(ClientResponse.class, manipulateForm);
751        assertEquals(200, response.getStatus());
752
753        profiles = this.getAuthenticatedResource(getResource().path("/registry/profiles")).get(
754                PROFILE_LIST_GENERICTYPE);
755        assertEquals(1, profiles.size());
756
757        response = getAuthenticatedResource(
758                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX
759                        + "profile2")
760                .post(ClientResponse.class, manipulateForm);
761        assertEquals(200, response.getStatus());
762    }
763
764    @Test
765    public void testGetRegisteredComponentRawData() throws Exception {
766       
767        fillUpPublicItems();
768       
769        String id = ComponentDescription.COMPONENT_PREFIX + "component1";
770        String component = this.getAuthenticatedResource(getResource()
771                .path("/registry/components/" + id + "/xsd"))
772                .accept(MediaType.TEXT_XML).get(String.class).trim();
773        assertTrue(component
774                .startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\"?><xs:schema"));
775        assertTrue(component.endsWith("</xs:schema>"));
776
777        component = this.getAuthenticatedResource(getResource().path("/registry/components/" + id + "/xml"))
778                .accept(MediaType.TEXT_XML).get(String.class).trim();
779        assertTrue(component
780                .startsWith("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<CMD_ComponentSpec"));
781        assertTrue(component.endsWith("</CMD_ComponentSpec>"));
782        assertTrue(component.contains("xsi:schemaLocation"));
783
784        try {
785            this.getAuthenticatedResource(getResource()
786                    .path("/registry/components/"
787                            + ComponentDescription.COMPONENT_PREFIX
788                            + "component1/jpg")).accept(MediaType.TEXT_XML)
789                    .get(String.class);
790            fail("Should have thrown exception, unsopported path parameter");
791        } catch (UniformInterfaceException e) {
792        }
793    }
794
795    @Test
796    public void testRegisterProfile() throws Exception {
797       
798        fillUpPublicItems();
799       
800        FormDataMultiPart form = new FormDataMultiPart();
801        form.field(IComponentRegistryRestService.DATA_FORM_FIELD,
802                RegistryTestHelper.getTestProfileContent(),
803                MediaType.APPLICATION_OCTET_STREAM_TYPE);
804        form.field(IComponentRegistryRestService.NAME_FORM_FIELD,
805                "ProfileTest1");
806        form.field(IComponentRegistryRestService.DOMAIN_FORM_FIELD,
807                "TestDomain");
808        form.field(IComponentRegistryRestService.DESCRIPTION_FORM_FIELD,
809                "My Test Profile");
810        form.field(IComponentRegistryRestService.GROUP_FORM_FIELD,
811                "My Test Group");
812        RegisterResponse response = getAuthenticatedResource(
813                "/registry/profiles").type(MediaType.MULTIPART_FORM_DATA).post(
814                RegisterResponse.class, form);
815        assertTrue(response.isProfile());
816        assertTrue(response.isInUserSpace());
817        ProfileDescription profileDesc = (ProfileDescription) response
818                .getDescription();
819        assertNotNull(profileDesc);
820        assertEquals("ProfileTest1", profileDesc.getName());
821        assertEquals("My Test Profile", profileDesc.getDescription());
822        assertEquals("TestDomain", profileDesc.getDomainName());
823        assertEquals("My Test Group", profileDesc.getGroupName());
824        assertEquals(expectedUserId("JUnit@test.com"), profileDesc.getUserId());
825        assertEquals("Database test user", profileDesc.getCreatorName());
826        assertTrue(profileDesc.getId().startsWith(
827                ComponentRegistry.REGISTRY_ID + "p_"));
828        assertNotNull(profileDesc.getRegistrationDate());
829        assertEquals(
830                "http://localhost:9998/registry/profiles/"
831                        + profileDesc.getId(), profileDesc.getHref());
832    }
833
834    @Test
835    public void testPublishProfile() throws Exception {
836       
837        fillUpPrivateItems();
838        fillUpPublicItems();
839       
840        assertEquals("user registered profiles", 1, getUserProfiles().size());
841        assertEquals("public registered profiles", 2, getPublicProfiles()
842                .size());
843        FormDataMultiPart form = createFormData(
844                RegistryTestHelper.getTestProfileContent(), "Unpublished");
845        RegisterResponse response = getAuthenticatedResource(getResource().path("/registry/profiles")).type(
846                MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class,
847                form);
848        assertTrue(response.isProfile());
849        BaseDescription desc = response.getDescription();
850        assertEquals("Unpublished", desc.getDescription());
851        assertEquals(2, getUserProfiles().size());
852        assertEquals(2, getPublicProfiles().size());
853        form = createFormData(
854                RegistryTestHelper.getTestProfileContent("publishedName"),
855                "Published");
856        response = getAuthenticatedResource(getResource().path(
857                        "/registry/profiles/" + desc.getId() + "/publish"))
858                .type(MediaType.MULTIPART_FORM_DATA).post(
859                        RegisterResponse.class, form);
860
861        assertEquals(1, getUserProfiles().size());
862        List<ProfileDescription> profiles = getPublicProfiles();
863        assertEquals(3, profiles.size());
864        ProfileDescription profileDescription = profiles.get(2);
865        assertNotNull(profileDescription.getId());
866        assertEquals(desc.getId(), profileDescription.getId());
867        assertEquals("http://localhost:9998/registry/profiles/" + desc.getId(),
868                profileDescription.getHref());
869        assertEquals("Published", profileDescription.getDescription());
870        CMDComponentSpec spec = getPublicSpec(profileDescription);
871        assertEquals("publishedName", spec.getCMDComponent().getName());
872    }
873
874    private CMDComponentSpec getPublicSpec(BaseDescription desc) {
875        if (desc.isProfile()) {
876            return this.getAuthenticatedResource(getResource().path("/registry/profiles/" + desc.getId()))
877                    .get(CMDComponentSpec.class);
878        } else {
879            return this.getAuthenticatedResource(getResource().path("/registry/components/" + desc.getId()))
880                    .get(CMDComponentSpec.class);
881        }
882    }
883
884    @Test
885    public void testPublishRegisteredComponent() throws Exception {
886       
887        fillUpPrivateItems();
888        fillUpPublicItems();
889       
890        assertEquals(1, getUserComponents().size());
891        assertEquals(2, getPublicComponents().size());
892       
893        FormDataMultiPart form = createFormData(
894                RegistryTestHelper.getComponentTestContent(), "Unpublished");
895        RegisterResponse response = getAuthenticatedResource(getResource().path("/registry/components")).type(
896                MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class,
897                form);
898        assertFalse(response.isProfile());
899        BaseDescription desc = response.getDescription();
900        assertEquals("Unpublished", desc.getDescription());
901        assertEquals(2, getUserComponents().size());
902        assertEquals(2, getPublicComponents().size());
903        form = createFormData(
904                RegistryTestHelper.getComponentTestContentAsStream("publishedName"),
905                "Published");
906        response = getAuthenticatedResource(getResource().path(
907                        "/registry/components/" + desc.getId() + "/publish"))
908                .type(MediaType.MULTIPART_FORM_DATA).post(
909                        RegisterResponse.class, form);
910
911        assertEquals(1, getUserComponents().size());
912        List<ComponentDescription> components = getPublicComponents();
913        assertEquals(3, components.size());
914        ComponentDescription componentDescription = components.get(2);
915        assertNotNull(componentDescription.getId());
916        assertEquals(desc.getId(), componentDescription.getId());
917        assertEquals(
918                "http://localhost:9998/registry/components/" + desc.getId(),
919                componentDescription.getHref());
920        assertEquals("Published", componentDescription.getDescription());
921        CMDComponentSpec spec = getPublicSpec(componentDescription);
922        assertEquals("publishedName", spec.getCMDComponent().getName());
923    }
924
925    // duplicates ("extendedly") testRegisterProfile, since you can pot only in user space, and then publish or move to group or so
926    @Test
927    public void testRegisterUserspaceProfile() throws Exception {
928       
929        fillUpPrivateItems();
930        fillUpPublicItems();
931       
932        List<ProfileDescription> profiles = getUserProfiles();
933        assertEquals("user registered profiles", 1, profiles.size());
934        assertEquals("public registered profiles", 2, getPublicProfiles()
935                .size());
936        FormDataMultiPart form = createFormData(RegistryTestHelper
937                .getTestProfileContent());
938        RegisterResponse response = getAuthenticatedResource(getResource().path("/registry/profiles")).type(
939                MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class,
940                form);
941        assertTrue(response.isProfile());
942        assertTrue(response.isInUserSpace());
943        ProfileDescription profileDesc = (ProfileDescription) response
944                .getDescription();
945        assertNotNull(profileDesc);
946        assertEquals("Test1", profileDesc.getName());
947        assertEquals("My Test", profileDesc.getDescription());
948        assertEquals(expectedUserId("JUnit@test.com"), profileDesc.getUserId());
949        assertEquals("Database test user", profileDesc.getCreatorName());
950        assertTrue(profileDesc.getId().startsWith(
951                ComponentRegistry.REGISTRY_ID + "p_"));
952        assertNotNull(profileDesc.getRegistrationDate());
953        assertEquals(
954                "http://localhost:9998/registry/profiles/"
955                        + profileDesc.getId(),
956                profileDesc.getHref());
957
958        profiles = getUserProfiles();
959        assertEquals(2, profiles.size());
960        assertEquals(2, getPublicProfiles().size());
961
962        // Try to post unauthenticated
963        ClientResponse cResponse = getResource().path("/registry/profiles")
964                .type(MediaType.MULTIPART_FORM_DATA)
965                .post(ClientResponse.class, form);
966        assertEquals(401, cResponse.getStatus());
967
968       
969       
970        CMDComponentSpec spec = getAuthenticatedResource(getResource().path("/registry/profiles/" + profileDesc.getId())
971                        .queryParam(REGISTRY_SPACE_PARAM, "private")).accept(
972                MediaType.APPLICATION_XML).get(CMDComponentSpec.class);
973        assertNotNull(spec);
974
975        cResponse = this.getAuthenticatedResource(getResource()
976                .path("/registry/profiles/" + profileDesc.getId() + "/xsd"))
977                .accept(MediaType.TEXT_XML).get(ClientResponse.class);
978        assertEquals(200, cResponse.getStatus());
979        String profile = cResponse.getEntity(String.class);
980        assertTrue(profile.length() > 0);
981
982        profile = getAuthenticatedResource(getResource().path(
983                        "/registry/profiles/" + profileDesc.getId() + "/xml"))
984                .accept(MediaType.TEXT_XML).get(String.class);
985        assertTrue(profile.length() > 0);
986
987        cResponse = getAuthenticatedResource(getResource().path("/registry/profiles/" + profileDesc.getId())
988                        .queryParam(REGISTRY_SPACE_PARAM, "private")).delete(
989                ClientResponse.class);
990        assertEquals(200, cResponse.getStatus());
991
992        profiles = getUserProfiles();
993        assertEquals(1, profiles.size());
994    }
995
996    private List<ProfileDescription> getPublicProfiles() {
997        return getAuthenticatedResource("/registry/profiles").accept(
998                MediaType.APPLICATION_XML).get(PROFILE_LIST_GENERICTYPE);
999    }
1000
1001    private List<ProfileDescription> getUserProfiles() {
1002        return getAuthenticatedResource(getResource().path("/registry/profiles").queryParam(
1003                        REGISTRY_SPACE_PARAM, "private")).accept(
1004                MediaType.APPLICATION_XML).get(PROFILE_LIST_GENERICTYPE);
1005    }
1006
1007    private FormDataMultiPart createFormData(Object content) {
1008        return createFormData(content, "My Test");
1009    }
1010
1011    private FormDataMultiPart createFormData(Object content, String description) {
1012        FormDataMultiPart form = new FormDataMultiPart();
1013        form.field(IComponentRegistryRestService.DATA_FORM_FIELD, content,
1014                MediaType.APPLICATION_OCTET_STREAM_TYPE);
1015        form.field(IComponentRegistryRestService.NAME_FORM_FIELD, "Test1");
1016        form.field(IComponentRegistryRestService.DESCRIPTION_FORM_FIELD,
1017                description);
1018        form.field(IComponentRegistryRestService.DOMAIN_FORM_FIELD, "My domain");
1019        form.field(IComponentRegistryRestService.GROUP_FORM_FIELD, "TestGroup");
1020        return form;
1021    }
1022
1023    @Test
1024    public void testRegisterWithUserComponents() throws Exception {
1025        fillUpPrivateItems();
1026       
1027        // kid component, not public
1028        String content = "";
1029        content += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
1030        content += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
1031        content += "    <Header/>\n";
1032        content += "    <CMD_Component name=\"XXX\" CardinalityMin=\"1\" CardinalityMax=\"10\">\n";
1033        content += "        <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n";
1034        content += "    </CMD_Component>\n";
1035        content += "</CMD_ComponentSpec>\n";
1036        ComponentDescription compDesc1 = RegistryTestHelper.addComponent(
1037                baseRegistry, "XXX1", content, false);
1038
1039        // a containing component, referring to the kid (which is not public, so the containing component cannot be registered
1040        content = "";
1041        content += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
1042        content += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
1043        content += "    <Header/>\n";
1044        content += "    <CMD_Component name=\"YYY\" CardinalityMin=\"1\" CardinalityMax=\"unbounded\">\n";
1045        content += "        <CMD_Component ComponentId=\"" + compDesc1.getId()
1046                + "\" CardinalityMin=\"0\" CardinalityMax=\"99\">\n";
1047        content += "        </CMD_Component>\n";
1048        content += "    </CMD_Component>\n";
1049        content += "</CMD_ComponentSpec>\n";
1050        FormDataMultiPart form = createFormData(content);
1051        RegisterResponse response = getAuthenticatedResource(
1052                "/registry/components").type(MediaType.MULTIPART_FORM_DATA)
1053                .post(RegisterResponse.class, form);
1054        // we first alway register in a private space, so reference to a private component should work
1055        assertTrue(response.isRegistered());
1056
1057
1058        /// profiles ///
1059        content = "";
1060        content += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
1061        content += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
1062        content += "    <Header/>\n";
1063        content += "    <CMD_Component name=\"ZZZ\" CardinalityMin=\"1\" CardinalityMax=\"unbounded\">\n";
1064        content += "        <CMD_Component ComponentId=\"" + compDesc1.getId()
1065                + "\" CardinalityMin=\"0\" CardinalityMax=\"99\">\n";
1066        content += "        </CMD_Component>\n";
1067        content += "    </CMD_Component>\n";
1068        content += "</CMD_ComponentSpec>\n";
1069
1070        form = createFormData(content);
1071        response = getAuthenticatedResource("/registry/profiles").type(
1072                MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class,
1073                form);
1074        assertTrue(response.isRegistered());
1075
1076    }
1077
1078    @Test
1079    public void testRegisterUserspaceComponent() throws Exception {
1080       
1081        fillUpPrivateItems();
1082       
1083        List<ComponentDescription> components = getUserComponents();
1084        assertEquals("user registered components", 1, components.size());
1085        assertEquals("public registered components", 0, getPublicComponents()
1086                .size());
1087        FormDataMultiPart form = createFormData(RegistryTestHelper
1088                .getComponentTestContent());
1089
1090        RegisterResponse response = getAuthenticatedResource(getResource().path("/registry/components")).type(
1091                MediaType.MULTIPART_FORM_DATA).post(RegisterResponse.class,
1092                form);
1093        assertTrue(response.isRegistered());
1094        assertFalse(response.isProfile());
1095        assertTrue(response.isInUserSpace());
1096        ComponentDescription desc = (ComponentDescription) response
1097                .getDescription();
1098        assertNotNull(desc);
1099        assertEquals("Test1", desc.getName());
1100        assertEquals("My Test", desc.getDescription());
1101        assertEquals(expectedUserId("JUnit@test.com"), desc.getUserId());
1102        assertEquals("Database test user", desc.getCreatorName());
1103        assertEquals("TestGroup", desc.getGroupName());
1104        assertTrue(desc.getId()
1105                .startsWith(ComponentRegistry.REGISTRY_ID + "c_"));
1106        assertNotNull(desc.getRegistrationDate());
1107        String url = getResource().getUriBuilder().build().toString();
1108        assertEquals(url + "registry/components/" + desc.getId(), desc.getHref());
1109
1110        components = getUserComponents();
1111        assertEquals(2, components.size());
1112        assertEquals(0, getPublicComponents().size());
1113
1114        // Try to post unauthenticated
1115        ClientResponse cResponse = getResource().path("/registry/components")
1116                .type(MediaType.MULTIPART_FORM_DATA)
1117                .post(ClientResponse.class, form);
1118        assertEquals(401, cResponse.getStatus());
1119
1120        // Try to get
1121        cResponse = this.getAuthenticatedResource(getResource().path("/registry/components/" + desc.getId()))
1122                .accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
1123       
1124        assertEquals(200, cResponse.getStatus());
1125        CMDComponentSpec spec = getUserComponent(desc);
1126        assertNotNull(spec);
1127
1128        cResponse = this.getAuthenticatedResource(getResource()
1129                .path("/registry/components/" + desc.getId() + "/xsd"))
1130                .accept(MediaType.TEXT_XML).get(ClientResponse.class);
1131        assertEquals(200, cResponse.getStatus());
1132        String result = cResponse.getEntity(String.class);
1133        assertTrue(result.length() > 0);
1134
1135        result = getAuthenticatedResource(getResource().path(
1136                        "/registry/components/" + desc.getId() + "/xml"))
1137                .accept(MediaType.TEXT_XML).get(String.class);
1138        assertTrue(result.length() > 0);
1139
1140        cResponse = getAuthenticatedResource(getResource().path("/registry/components/" + desc.getId())).delete(
1141                ClientResponse.class);
1142        assertEquals(200, cResponse.getStatus());
1143
1144        components = getUserComponents();
1145        assertEquals(1, components.size());
1146    }
1147//
1148    @Test
1149    public void testCreatePrivateComponentWithRecursion() throws Exception {
1150       
1151        fillUpPrivateItems();
1152        // Create new componet
1153        FormDataMultiPart form = createFormData(RegistryTestHelper
1154                .getComponentTestContent());
1155        ClientResponse cResponse = getAuthenticatedResource(getResource().path("/registry/components")).type(
1156                MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
1157        assertEquals(ClientResponse.Status.OK.getStatusCode(),
1158                cResponse.getStatus());
1159        RegisterResponse response = cResponse.getEntity(RegisterResponse.class);
1160        assertTrue(response.isRegistered());
1161        ComponentDescription desc = (ComponentDescription) response
1162                .getDescription();
1163
1164        // Re-define with self-recursion
1165        String compContent = "";
1166        compContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
1167        compContent += "\n";
1168        compContent += "<CMD_ComponentSpec isProfile=\"false\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
1169        compContent += "    xsi:noNamespaceSchemaLocation=\"../../general-component-schema.xsd\">\n";
1170        compContent += "    \n";
1171        compContent += "    <Header/>\n";
1172        compContent += "    \n";
1173        compContent += "    <CMD_Component name=\"Nested\" CardinalityMin=\"1\" CardinalityMax=\"1\">\n";
1174        compContent += "        <CMD_Element name=\"Availability\" ValueScheme=\"string\" />\n";
1175        compContent += "        <CMD_Component ComponentId=\""
1176                + desc.getId()
1177                + "\" name=\"Recursive\" CardinalityMin=\"1\" CardinalityMax=\"1\" />\n";
1178        compContent += "    </CMD_Component>\n";
1179        compContent += "\n";
1180        compContent += "</CMD_ComponentSpec>\n";
1181
1182        // Update component
1183        form = createFormData(
1184                RegistryTestHelper.getComponentContentAsStream(compContent),
1185                "UPDATE DESCRIPTION!");
1186        cResponse = getAuthenticatedResource(getResource().path(
1187                        "/registry/components/" + desc.getId() + "/update")).type(
1188                MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
1189        assertEquals(ClientResponse.Status.OK.getStatusCode(),
1190                cResponse.getStatus());
1191        response = cResponse.getEntity(RegisterResponse.class);
1192        assertFalse("Recursive definition should fail", response.isRegistered());
1193        assertEquals("There should be an error message for the recursion", 1,
1194                response.getErrors().size());
1195        assertTrue(
1196                "There error message should specify the point of recursion",
1197                response.getErrors().get(0)
1198                        .contains("already contains " + desc.getId()));
1199
1200    }
1201
1202    @Test
1203    public void testUpdatePrivateComponent() throws Exception {
1204       
1205        fillUpPrivateItems();
1206       
1207        List<ComponentDescription> components = getUserComponents();
1208        assertEquals("user registered components", 1, components.size());
1209        assertEquals("public registered components", 0, getPublicComponents()
1210                .size());
1211       
1212        // first, post a component to update
1213        FormDataMultiPart form = createFormData(RegistryTestHelper
1214                .getComponentTestContent());
1215        ClientResponse cResponse = getAuthenticatedResource(getResource().path("/registry/components")).type(
1216                MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
1217        assertEquals(ClientResponse.Status.OK.getStatusCode(),
1218                cResponse.getStatus());
1219        RegisterResponse response = cResponse.getEntity(RegisterResponse.class);
1220        assertTrue(response.isRegistered());
1221        assertFalse(response.isProfile());
1222        assertTrue(response.isInUserSpace());
1223        ComponentDescription desc = (ComponentDescription) response
1224                .getDescription();
1225        assertNotNull(desc);
1226        assertEquals("Test1", desc.getName());
1227        assertEquals("My Test", desc.getDescription());
1228        Date firstDate = desc.getRegistrationDate();
1229        CMDComponentSpec spec = getUserComponent(desc);
1230        assertNotNull(spec);
1231        assertEquals("Access", spec.getCMDComponent().getName());
1232        components = getUserComponents();
1233        assertEquals(2, components.size());
1234        assertEquals(0, getPublicComponents().size());
1235
1236        // second, now update
1237        form = createFormData(
1238                RegistryTestHelper.getComponentTestContentAsStream("TESTNAME"),
1239                "UPDATE DESCRIPTION!");
1240        cResponse = getAuthenticatedResource(getResource().path(
1241                        "/registry/components/" + desc.getId() + "/update")
1242                        .queryParam(REGISTRY_SPACE_PARAM, "private")).type(
1243                MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
1244        assertEquals(ClientResponse.Status.OK.getStatusCode(),
1245                cResponse.getStatus());
1246        response = cResponse.getEntity(RegisterResponse.class);
1247        assertTrue(response.isRegistered());
1248        assertFalse(response.isProfile());
1249        assertTrue(response.isInUserSpace());
1250        desc = (ComponentDescription) response.getDescription();
1251        assertNotNull(desc);
1252        assertEquals("Test1", desc.getName());
1253        assertEquals("UPDATE DESCRIPTION!", desc.getDescription());
1254        Date secondDate = desc.getRegistrationDate();
1255        assertTrue(firstDate.before(secondDate) || firstDate.equals(secondDate));
1256
1257        spec = getUserComponent(desc);
1258        assertNotNull(spec);
1259        assertEquals("TESTNAME", spec.getCMDComponent().getName());
1260        components = getUserComponents();
1261        assertEquals(2, components.size());
1262        assertEquals(0, getPublicComponents().size());
1263    }
1264
1265    @Test
1266    public void testUpdatePrivateProfile() throws Exception {
1267       
1268        fillUpPrivateItems();
1269       
1270        List<ProfileDescription> profiles = getUserProfiles();
1271        assertEquals(1, profiles.size());
1272        assertEquals(0, getPublicProfiles().size());
1273
1274        FormDataMultiPart form = createFormData(RegistryTestHelper
1275                .getTestProfileContent());
1276        ClientResponse cResponse = getAuthenticatedResource(getResource().path("/registry/profiles")).type(
1277                MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
1278        assertEquals(ClientResponse.Status.OK.getStatusCode(),
1279                cResponse.getStatus());
1280        RegisterResponse response = cResponse.getEntity(RegisterResponse.class);
1281        assertTrue(response.isRegistered());
1282        assertTrue(response.isProfile());
1283        assertTrue(response.isInUserSpace());
1284        ProfileDescription desc = (ProfileDescription) response
1285                .getDescription();
1286        assertNotNull(desc);
1287        assertEquals("Test1", desc.getName());
1288        assertEquals("My Test", desc.getDescription());
1289        assertEquals("TestGroup", desc.getGroupName());
1290        Date firstDate = desc.getRegistrationDate();
1291        CMDComponentSpec spec = getUserProfile(desc);
1292        assertNotNull(spec);
1293        assertEquals("Actor", spec.getCMDComponent().getName());
1294        profiles = getUserProfiles();
1295        assertEquals(2, profiles.size());
1296        assertEquals(0, getPublicComponents().size());
1297
1298        // Now update
1299        form = createFormData(
1300                RegistryTestHelper.getTestProfileContent("TESTNAME"),
1301                "UPDATE DESCRIPTION!");
1302        cResponse = getAuthenticatedResource(getResource().path(
1303                        "/registry/profiles/" + desc.getId() + "/update")).type(
1304                MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
1305        assertEquals(ClientResponse.Status.OK.getStatusCode(),
1306                cResponse.getStatus());
1307        response = cResponse.getEntity(RegisterResponse.class);
1308        assertTrue(response.isRegistered());
1309        assertTrue(response.isProfile());
1310        assertTrue(response.isInUserSpace());
1311        desc = (ProfileDescription) response.getDescription();
1312        assertNotNull(desc);
1313        assertEquals("Test1", desc.getName());
1314        assertEquals("UPDATE DESCRIPTION!", desc.getDescription());
1315        Date secondDate = desc.getRegistrationDate();
1316        assertTrue(firstDate.before(secondDate) || firstDate.equals(secondDate));
1317
1318        spec = getUserProfile(desc);
1319        assertNotNull(spec);
1320        assertEquals("TESTNAME", spec.getCMDComponent().getName());
1321        profiles = getUserProfiles();
1322        assertEquals(2, profiles.size());
1323        assertEquals(0, getPublicComponents().size());
1324    }
1325
1326    private CMDComponentSpec getUserComponent(ComponentDescription desc) {
1327        return getAuthenticatedResource(getResource().path("/registry/components/" + desc.getId())
1328                        .queryParam(REGISTRY_SPACE_PARAM, "private")).accept(
1329                MediaType.APPLICATION_XML).get(CMDComponentSpec.class);
1330    }
1331
1332    private CMDComponentSpec getUserProfile(ProfileDescription desc) {
1333        return getAuthenticatedResource(getResource().path("/registry/profiles/" + desc.getId())
1334                        .queryParam(REGISTRY_SPACE_PARAM, "private")).accept(
1335                MediaType.APPLICATION_XML).get(CMDComponentSpec.class);
1336    }
1337
1338    private List<ComponentDescription> getPublicComponents() {
1339        return getAuthenticatedResource("/registry/components").accept(
1340                MediaType.APPLICATION_XML).get(COMPONENT_LIST_GENERICTYPE);
1341    }
1342
1343    private List<ComponentDescription> getUserComponents() {
1344        return getAuthenticatedResource(getResource().path("/registry/components").queryParam(
1345                        REGISTRY_SPACE_PARAM, "private")).accept(
1346                MediaType.APPLICATION_XML).get(COMPONENT_LIST_GENERICTYPE);
1347    }
1348
1349    @Test
1350    public void testRegisterComponent() throws Exception {
1351       
1352        fillUpPrivateItems();
1353       
1354        FormDataMultiPart form = new FormDataMultiPart();
1355        form.field(IComponentRegistryRestService.DATA_FORM_FIELD,
1356                RegistryTestHelper.getComponentTestContent(),
1357                MediaType.APPLICATION_OCTET_STREAM_TYPE);
1358        form.field(IComponentRegistryRestService.NAME_FORM_FIELD,
1359                "ComponentTest1");
1360        form.field(IComponentRegistryRestService.DESCRIPTION_FORM_FIELD,
1361                "My Test Component");
1362        form.field(IComponentRegistryRestService.DOMAIN_FORM_FIELD,
1363                "TestDomain");
1364        form.field(IComponentRegistryRestService.GROUP_FORM_FIELD, "TestGroup");
1365        RegisterResponse response = getAuthenticatedResource(
1366                "/registry/components").type(MediaType.MULTIPART_FORM_DATA)
1367                .post(RegisterResponse.class, form);
1368        assertTrue(response.isRegistered());
1369        assertFalse(response.isProfile());
1370        assertTrue(response.isInUserSpace());
1371        ComponentDescription desc = (ComponentDescription) response
1372                .getDescription();
1373        assertNotNull(desc);
1374        assertEquals("ComponentTest1", desc.getName());
1375        assertEquals("My Test Component", desc.getDescription());
1376        assertEquals(expectedUserId("JUnit@test.com"), desc.getUserId());
1377        assertEquals("Database test user", desc.getCreatorName());
1378        assertEquals("TestGroup", desc.getGroupName());
1379        assertEquals("TestDomain", desc.getDomainName());
1380        assertTrue(desc.getId()
1381                .startsWith(ComponentRegistry.REGISTRY_ID + "c_"));
1382        assertNotNull(desc.getRegistrationDate());
1383        String url = getResource().getUriBuilder().build().toString();
1384        assertEquals(url + "registry/components/" + desc.getId(),
1385                desc.getHref());
1386    }
1387
1388    @Test
1389    public void testRegisterComment() throws Exception {
1390       
1391        fillUpPublicItems();
1392       
1393        FormDataMultiPart form = new FormDataMultiPart();
1394        form.field(IComponentRegistryRestService.DATA_FORM_FIELD,
1395                RegistryTestHelper.getCommentTestContent(),
1396                MediaType.APPLICATION_OCTET_STREAM_TYPE);
1397        String id = ProfileDescription.PROFILE_PREFIX + "profile1";
1398        CommentResponse response = getAuthenticatedResource(
1399                "/registry/profiles/" + id + "/comments").type(
1400                MediaType.MULTIPART_FORM_DATA)
1401                .post(CommentResponse.class, form);
1402        assertTrue(response.isRegistered());
1403        assertFalse(response.isInUserSpace());
1404        Comment comment = response.getComment();
1405        assertNotNull(comment);
1406        assertEquals("Actual", comment.getComment());
1407        assertEquals("Database test user", comment.getUserName());
1408        Assert.notNull(comment.getCommentDate());
1409        Assert.hasText(comment.getId());
1410
1411        // User id should not be serialized!
1412        assertEquals(0, comment.getUserId());
1413    }
1414
1415    @Test
1416    public void testRegisterCommentToNonExistent() throws Exception {
1417        fillUpPublicItems();
1418       
1419        FormDataMultiPart form = new FormDataMultiPart();
1420        form.field(IComponentRegistryRestService.DATA_FORM_FIELD,
1421                RegistryTestHelper.getCommentTestContent(),
1422                MediaType.APPLICATION_OCTET_STREAM_TYPE);
1423        ClientResponse cResponse = getAuthenticatedResource(
1424                "/registry/profiles/clarin.eu:cr1:profile99/comments").type(
1425                MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
1426        assertEquals(404, cResponse.getStatus());
1427    }
1428
1429    @Test
1430    public void testRegisterCommentUnauthenticated() throws Exception {
1431        fillUpPublicItems();
1432       
1433        FormDataMultiPart form = new FormDataMultiPart();
1434        form.field(IComponentRegistryRestService.DATA_FORM_FIELD,
1435                RegistryTestHelper.getCommentTestContent(),
1436                MediaType.APPLICATION_OCTET_STREAM_TYPE);
1437        ClientResponse cResponse = getResource()
1438                .path("/registry/profiles/clarin.eu:cr1:profile1/comments")
1439                .type(MediaType.MULTIPART_FORM_DATA)
1440                .post(ClientResponse.class, form);
1441        assertEquals(401, cResponse.getStatus());
1442    }
1443
1444    @Test
1445    public void testRegisterProfileInvalidData() throws Exception {
1446        fillUpPrivateItems();
1447        FormDataMultiPart form = new FormDataMultiPart();
1448        String notAValidProfile = "<CMD_ComponentSpec> </CMD_ComponentSpec>";
1449        form.field(IComponentRegistryRestService.DATA_FORM_FIELD,
1450                new ByteArrayInputStream(notAValidProfile.getBytes()),
1451                MediaType.APPLICATION_OCTET_STREAM_TYPE);
1452        form.field(IComponentRegistryRestService.NAME_FORM_FIELD,
1453                "ProfileTest1");
1454        form.field(IComponentRegistryRestService.DOMAIN_FORM_FIELD, "Domain");
1455        form.field(IComponentRegistryRestService.GROUP_FORM_FIELD, "Group");
1456        form.field(IComponentRegistryRestService.DESCRIPTION_FORM_FIELD,
1457                "My Test Profile");
1458        RegisterResponse postResponse = getAuthenticatedResource(
1459                "/registry/profiles").type(MediaType.MULTIPART_FORM_DATA).post(
1460                RegisterResponse.class, form);
1461        assertTrue(postResponse.isProfile());
1462        assertFalse(postResponse.isRegistered());
1463        assertEquals(1, postResponse.getErrors().size());
1464        assertTrue(postResponse.getErrors().get(0).contains("isProfile"));
1465    }
1466
1467    /**
1468     * Two elements on the same level with the same name violates schematron
1469     * rule, and should fail validation
1470     *
1471     * @throws Exception
1472     */
1473    @Test
1474    public void testRegisterInvalidProfile() throws Exception {
1475        fillUpPrivateItems();
1476        FormDataMultiPart form = new FormDataMultiPart();
1477        String profileContent = "";
1478        profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
1479        profileContent += "<CMD_ComponentSpec isProfile=\"true\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
1480        profileContent += "    xsi:noNamespaceSchemaLocation=\"general-component-schema.xsd\">\n";
1481        profileContent += "    <Header />\n";
1482        profileContent += "    <CMD_Component name=\"ProfileTest1\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n";
1483        profileContent += "        <CMD_Element name=\"Age\">\n";
1484        profileContent += "            <ValueScheme>\n";
1485        profileContent += "                <pattern>[23][0-9]</pattern>\n";
1486        profileContent += "            </ValueScheme>\n";
1487        profileContent += "        </CMD_Element>\n";
1488        profileContent += "        <CMD_Element name=\"Age\">\n";
1489        profileContent += "            <ValueScheme>\n";
1490        profileContent += "                <pattern>[23][0-9]</pattern>\n";
1491        profileContent += "            </ValueScheme>\n";
1492        profileContent += "        </CMD_Element>\n";
1493        profileContent += "    </CMD_Component>\n";
1494        profileContent += "</CMD_ComponentSpec>\n";
1495        form.field(IComponentRegistryRestService.DATA_FORM_FIELD,
1496                RegistryTestHelper.getComponentContentAsStream(profileContent),
1497                MediaType.APPLICATION_OCTET_STREAM_TYPE);
1498        form.field(IComponentRegistryRestService.NAME_FORM_FIELD,
1499                "ProfileTest1");
1500        form.field(IComponentRegistryRestService.DOMAIN_FORM_FIELD,
1501                "TestDomain");
1502        form.field(IComponentRegistryRestService.DESCRIPTION_FORM_FIELD,
1503                "My Test Profile");
1504        form.field(IComponentRegistryRestService.GROUP_FORM_FIELD,
1505                "My Test Group");
1506        RegisterResponse response = getAuthenticatedResource(
1507                "/registry/profiles").type(MediaType.MULTIPART_FORM_DATA).post(
1508                RegisterResponse.class, form);
1509        assertFalse(
1510                "Subsequent elements should not be allowed to have the same name",
1511                response.isRegistered());
1512        assertTrue(response.getErrors().get(0)
1513                .contains(MDValidator.PARSE_ERROR));
1514    }
1515
1516    @Test
1517    public void testRegisterProfileInvalidDescriptionAndContent()
1518            throws Exception {
1519        fillUpPrivateItems();
1520        FormDataMultiPart form = new FormDataMultiPart();
1521        String profileContent = "";
1522        profileContent += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
1523        profileContent += "<CMD_ComponentSpec> \n"; // No isProfile attribute
1524        profileContent += "    <Header />\n";
1525        profileContent += "    <CMD_Component name=\"Actor\" CardinalityMin=\"0\" CardinalityMax=\"unbounded\">\n";
1526        profileContent += "        <AttributeList>\n";
1527        profileContent += "            <Attribute>\n";
1528        profileContent += "                <Name>Name</Name>\n";
1529        profileContent += "                <Type>string</Type>\n";
1530        profileContent += "            </Attribute>\n";
1531        profileContent += "        </AttributeList>\n";
1532        profileContent += "    </CMD_Component>\n";
1533        profileContent += "</CMD_ComponentSpec>\n";
1534        form.field("data", new ByteArrayInputStream(profileContent.getBytes()),
1535                MediaType.APPLICATION_OCTET_STREAM_TYPE);
1536        form.field("name", "");// Empty name so invalid
1537        form.field("description", "My Test Profile");
1538        RegisterResponse response = getAuthenticatedResource(
1539                "/registry/profiles").type(MediaType.MULTIPART_FORM_DATA).post(
1540                RegisterResponse.class, form);
1541        assertFalse(response.isRegistered());
1542        assertEquals(2, response.getErrors().size());
1543        assertNotNull(response.getErrors().get(0));
1544        assertEquals(MDValidator.PARSE_ERROR, response.getErrors().get(1)
1545                .substring(0, MDValidator.PARSE_ERROR.length()));
1546    }
1547
1548    @Test
1549    public void testRegisterLargeProfile() throws Exception {
1550        //fillUpPrivateItems(); not necessary
1551        FormDataMultiPart form = new FormDataMultiPart();
1552        form.field(IComponentRegistryRestService.DATA_FORM_FIELD,
1553                RegistryTestHelper.getLargeProfileContent(),
1554                MediaType.APPLICATION_OCTET_STREAM_TYPE);
1555        form.field(IComponentRegistryRestService.NAME_FORM_FIELD,
1556                "ProfileTest1");
1557        form.field(IComponentRegistryRestService.DOMAIN_FORM_FIELD,
1558                "TestDomain");
1559        form.field(IComponentRegistryRestService.DESCRIPTION_FORM_FIELD,
1560                "My Test Profile");
1561        form.field(IComponentRegistryRestService.GROUP_FORM_FIELD,
1562                "My Test Group");
1563        ClientResponse response = getAuthenticatedResource("/registry/profiles")
1564                .type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class,
1565                        form);
1566        assertEquals(200, response.getStatus());
1567    }
1568
1569    @Test
1570    public void testRegisterComponentAsProfile() throws Exception {
1571        //fillUpPrivateItems();; not necessary
1572        FormDataMultiPart form = new FormDataMultiPart();
1573        form.field(IComponentRegistryRestService.DATA_FORM_FIELD,
1574                RegistryTestHelper.getComponentTestContent(),
1575                MediaType.APPLICATION_OCTET_STREAM_TYPE);
1576        form.field(IComponentRegistryRestService.NAME_FORM_FIELD, "t");
1577        form.field(IComponentRegistryRestService.DOMAIN_FORM_FIELD, "domain");
1578        form.field(IComponentRegistryRestService.DESCRIPTION_FORM_FIELD,
1579                "My Test");
1580        form.field(IComponentRegistryRestService.GROUP_FORM_FIELD, "My Group");
1581        RegisterResponse response = getAuthenticatedResource(
1582                "/registry/profiles").type(MediaType.MULTIPART_FORM_DATA).post(
1583                RegisterResponse.class, form);
1584        assertFalse(response.isRegistered());
1585        assertTrue(response.isProfile());
1586        assertEquals(1, response.getErrors().size());
1587        assertEquals(MDValidator.MISMATCH_ERROR, response.getErrors().get(0));
1588    }
1589
1590    /**
1591     * creates a tree-like component, whose root has two kids, and the kids have
1592     * three their own kids each the corresponding filenames are: "wortel",
1593     * "nodel-L", "node-R", "leaf-LL", "leaf-LM", "leaf-LR", "leaf-RL",
1594     * "leaf-RM", "leaf-RR"
1595     *
1596     * @throws Exception
1597     */
1598    @Test
1599    public void testSetFilenamesToNull() throws Exception {
1600
1601        // helper assists in generating a test component
1602        CMDComponentSetFilenamesToNullTestRunner helper = new CMDComponentSetFilenamesToNullTestRunner();
1603
1604        // making an (up to ternary) tree of test components
1605        // inductive explaination of the variable names, an example: leaf-LM is
1606        // the middle child of the nodeL
1607        // the filename is this leaf is "leaf-LM"
1608
1609        // making children of the node L
1610        CMDComponentType leafLR = helper.makeTestComponent("leaf-LR", null);
1611        CMDComponentType leafLM = helper.makeTestComponent("leaf-LM", null);
1612        CMDComponentType leafLL = helper.makeTestComponent("leaf-LL", null);
1613
1614        // making node L
1615        List<CMDComponentType> nodeLchild = (new ArrayList<CMDComponentType>());
1616        nodeLchild.add(leafLL);
1617        nodeLchild.add(leafLM);
1618        nodeLchild.add(leafLR);
1619        CMDComponentType nodeL = helper.makeTestComponent("node-L", nodeLchild);
1620
1621        // making children of the node R
1622        CMDComponentType leafRR = helper.makeTestComponent("leaf-RR", null);
1623        CMDComponentType leafRM = helper.makeTestComponent("leaf-RM", null);
1624        CMDComponentType leafRL = helper.makeTestComponent("leaf-RL", null);
1625
1626        // making node R
1627        List<CMDComponentType> nodeRchild = (new ArrayList<CMDComponentType>());
1628        nodeRchild.add(leafRL);
1629        nodeRchild.add(leafRM);
1630        nodeRchild.add(leafRR);
1631        CMDComponentType nodeR = helper.makeTestComponent("node-R", nodeRchild);
1632
1633        // making the root, which has children NodeL and nodeR
1634        List<CMDComponentType> wortelchild = (new ArrayList<CMDComponentType>());
1635        wortelchild.add(nodeL);
1636        wortelchild.add(nodeR);
1637        CMDComponentType root = helper.makeTestComponent("wortel", wortelchild);
1638
1639        // checking if the test compnent has the expected structure and the
1640        // expected filenames
1641        // ALSO this checking code below shows the strtucture of the tree
1642
1643        assertEquals(root.getCMDComponent().size(), 2);
1644        assertEquals(root.getCMDComponent().get(0).getCMDComponent().size(), 3);
1645        assertEquals(root.getCMDComponent().get(1).getCMDComponent().size(), 3);
1646
1647        assertEquals(root.getFilename(), "wortel");
1648
1649        assertEquals(root.getCMDComponent().get(0).getFilename(), "node-L");
1650        assertEquals(root.getCMDComponent().get(1).getFilename(), "node-R");
1651
1652        assertEquals(root.getCMDComponent().get(0).getCMDComponent().get(0)
1653                .getFilename(), "leaf-LL");
1654        assertEquals(root.getCMDComponent().get(0).getCMDComponent().get(1)
1655                .getFilename(), "leaf-LM");
1656        assertEquals(root.getCMDComponent().get(0).getCMDComponent().get(2)
1657                .getFilename(), "leaf-LR");
1658
1659        assertEquals(root.getCMDComponent().get(1).getCMDComponent().get(0)
1660                .getFilename(), "leaf-RL");
1661        assertEquals(root.getCMDComponent().get(1).getCMDComponent().get(1)
1662                .getFilename(), "leaf-RM");
1663        assertEquals(root.getCMDComponent().get(1).getCMDComponent().get(2)
1664                .getFilename(), "leaf-RR");
1665
1666        // the actual job
1667        // nulling the filenames will be called as a method of testrestservice
1668        ComponentRegistryRestService restservice = new ComponentRegistryRestService();
1669        restservice.setFileNamesToNullCurrent(root);
1670
1671        // check if the filenames are nulled
1672        assertEquals(root.getFilename(), null);
1673
1674        assertEquals(root.getCMDComponent().get(0).getFilename(), null);
1675        assertEquals(root.getCMDComponent().get(1).getFilename(), null);
1676
1677        assertEquals(root.getCMDComponent().get(0).getCMDComponent().get(0)
1678                .getFilename(), null);
1679        assertEquals(root.getCMDComponent().get(0).getCMDComponent().get(1)
1680                .getFilename(), null);
1681        assertEquals(root.getCMDComponent().get(0).getCMDComponent().get(2)
1682                .getFilename(), null);
1683
1684        assertEquals(root.getCMDComponent().get(1).getCMDComponent().get(0)
1685                .getFilename(), null);
1686        assertEquals(root.getCMDComponent().get(1).getCMDComponent().get(1)
1687                .getFilename(), null);
1688        assertEquals(root.getCMDComponent().get(1).getCMDComponent().get(2)
1689                .getFilename(), null);
1690    }
1691   
1692    @Test
1693    public void testGetDescription() throws Exception{
1694        fillUpPublicItems();
1695        String id = ComponentDescription.COMPONENT_PREFIX + "component1";
1696        ComponentDescription component = getAuthenticatedResource(getResource().path("/registry/items/" + id)).accept(MediaType.APPLICATION_JSON).get(ComponentDescription.class);
1697        assertNotNull(component);
1698        assertEquals(id, component.getId());
1699        assertEquals("component1", component.getName());
1700        assertEquals("Test Description", component.getDescription());
1701
1702    }
1703}
Note: See TracBrowser for help on using the repository browser.