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

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

Fixed issued arising after testing on localhost tomcat (completed)

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