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

Last change on this file since 4550 was 4550, checked in by twagoo, 10 years ago

Fixed errors due to schema change allowing only one root element in a profile or component (see r4525)

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