source: ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/RestGroupServiceTest.java @ 5563

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

Unit test for updating profiles and components from groups.

File size: 42.7 KB
Line 
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package clarin.cmdi.componentregistry.rest;
6
7import clarin.cmdi.componentregistry.ComponentRegistry;
8import clarin.cmdi.componentregistry.ComponentRegistryFactory;
9import clarin.cmdi.componentregistry.ItemNotFoundException;
10import clarin.cmdi.componentregistry.components.CMDComponentSpec;
11import clarin.cmdi.componentregistry.impl.database.ComponentRegistryTestDatabase;
12import clarin.cmdi.componentregistry.impl.database.GroupService;
13import clarin.cmdi.componentregistry.model.Comment;
14import clarin.cmdi.componentregistry.model.ComponentDescription;
15import clarin.cmdi.componentregistry.model.Group;
16import clarin.cmdi.componentregistry.model.Ownership;
17import clarin.cmdi.componentregistry.model.ProfileDescription;
18import clarin.cmdi.componentregistry.model.RegisterResponse;
19import clarin.cmdi.componentregistry.model.RegistryUser;
20import clarin.cmdi.componentregistry.persistence.jpa.CommentsDao;
21import clarin.cmdi.componentregistry.persistence.jpa.UserDao;
22import clarin.cmdi.componentregistry.rss.Rss;
23import com.sun.jersey.api.client.ClientResponse;
24import com.sun.jersey.multipart.FormDataMultiPart;
25import java.text.ParseException;
26import java.util.Date;
27import java.util.List;
28import javax.ws.rs.core.MediaType;
29import javax.xml.bind.JAXBException;
30import org.junit.Before;
31import org.springframework.beans.factory.annotation.Autowired;
32import org.springframework.jdbc.core.JdbcTemplate;
33
34import static org.junit.Assert.*;
35import org.junit.Test;
36
37/**
38 *
39 * @author olhsha
40 */
41public class RestGroupServiceTest extends ComponentRegistryRestServiceTestCase {
42   
43    @Autowired
44    private ComponentRegistryFactory componentRegistryFactory;
45   
46    @Autowired
47    private JdbcTemplate jdbcTemplate;
48    @Autowired 
49    private GroupService groupService;
50    @Autowired 
51    private UserDao userDao;
52    @Autowired 
53    private CommentsDao commentsDao;
54   
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     protected void createAntherUserRecord() {
115        RegistryUser user = new RegistryUser();
116        user.setName("Another database test user");
117        user.setPrincipalName("anotherPrincipal");
118        userDao.save(user);
119    }
120     
121    private void MakeGroupA(){
122        groupService.createNewGroup("group A", DummyPrincipal.DUMMY_PRINCIPAL.getName());
123    }
124   
125     private void fillUpGroupA() throws ParseException, JAXBException, ItemNotFoundException{
126         
127        MakeGroupA();
128       
129        RegistryTestHelper.addProfile(baseRegistry, "profile-1", false);
130        RegistryTestHelper.addComponent(baseRegistry, "component-1", false);
131        RegistryTestHelper.addComponent(baseRegistry, "component-2", false);
132       
133        Ownership ownership = new Ownership();
134        ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"profile-1");
135        ownership.setGroupId(1);
136        ownership.setUserId(0);
137        groupService.addOwnership(ownership);
138       
139        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"component-1");
140        ownership.setGroupId(1);
141        ownership.setUserId(0);
142        groupService.addOwnership(ownership);
143       
144        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"component-2");
145        ownership.setGroupId(1);
146        ownership.setUserId(0);
147        groupService.addOwnership(ownership);
148       
149    }
150     
151     
152   
153    private void MakeGroupB() throws ItemNotFoundException{
154        createAntherUserRecord();
155        groupService.createNewGroup("group B", "anotherPrincipal");
156        groupService.makeMember(DummyPrincipal.DUMMY_PRINCIPAL.getName(), "group B");
157    }
158   
159     private void MakeGroupC() throws ItemNotFoundException{
160        groupService.createNewGroup("group C", "anotherPrincipal");
161    }
162   
163     private void fillUpGroupB() throws ParseException, JAXBException, ItemNotFoundException{
164         
165        MakeGroupB();
166       
167        RegistryTestHelper.addProfile(baseRegistry, "Bprofile-1", false);
168        RegistryTestHelper.addComponent(baseRegistry, "Bcomponent-1", false);
169        RegistryTestHelper.addComponent(baseRegistry, "Bcomponent-2", false);
170       
171        Ownership ownership = new Ownership();
172        ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"Bprofile-1");
173        ownership.setGroupId(2);
174        ownership.setUserId(0);
175        groupService.addOwnership(ownership);
176       
177        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Bcomponent-1");
178        ownership.setGroupId(2);
179        ownership.setUserId(0);
180        groupService.addOwnership(ownership);
181       
182        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Bcomponent-2");
183        ownership.setGroupId(2);
184        ownership.setUserId(0);
185        groupService.addOwnership(ownership);
186       
187    }
188     
189     private void fillUpGroupC() throws ParseException, JAXBException, ItemNotFoundException{
190         
191        MakeGroupC();
192       
193        RegistryTestHelper.addProfileAnotherPrincipal(baseRegistry, "Cprofile-1", false);
194        RegistryTestHelper.addComponentAnotherPrincipal(baseRegistry, "Ccomponent-1", false);
195        RegistryTestHelper.addComponentAnotherPrincipal(baseRegistry, "Ccomponent-2", false);
196       
197        Ownership ownership = new Ownership();
198        ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"Cprofile-1");
199        ownership.setGroupId(3);
200        ownership.setUserId(0);
201        groupService.addOwnership(ownership);
202       
203        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1");
204        ownership.setGroupId(3);
205        ownership.setUserId(0);
206        groupService.addOwnership(ownership);
207       
208        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Ccomponent-2");
209        ownership.setGroupId(3);
210        ownership.setUserId(0);
211        groupService.addOwnership(ownership);
212       
213    }
214   
215 //    Response createNewGroup(String groupName) throws IOException;
216   
217    @Test
218    public void testCreateNewGroup() {
219        System.out.println("test createNewGroup");
220        ClientResponse cr = this.getAuthenticatedResource(getResource()
221                .path("/registry/groups/create").queryParam("groupName", "newGroup"))
222                .accept(MediaType.TEXT_XML).post(ClientResponse.class);
223        assertEquals(200, cr.getStatus());
224        assertEquals("Group with the name newGroup is created and given an id 1", cr.getEntity(String.class));
225    }
226   
227   
228   
229   
230//  List<Group> getGroupsOwnedByUser(String pricipalName) throws IOException;
231   
232    @Test
233    public void testGetGroupsOwnedByUser() {
234        System.out.println("test GetGroupsOwnedByUser");
235        ClientResponse cr = this.getAuthenticatedResource(getResource()
236                .path("/registry/groups/create").queryParam("groupName", "newGroup1"))
237                .accept(MediaType.TEXT_XML).post(ClientResponse.class);
238        assertEquals(200, cr.getStatus());
239        ClientResponse cr1 = this.getAuthenticatedResource(getResource()
240                .path("/registry/groups/create").queryParam("groupName", "newGroup2"))
241                .accept(MediaType.TEXT_XML).post(ClientResponse.class);
242        assertEquals(200, cr1.getStatus());
243       
244        // test itself
245       
246       List<Group> result = this.getAuthenticatedResource(getResource()
247                .path("/registry/groups/principal").queryParam("principalName", DummyPrincipal.DUMMY_PRINCIPAL.getName()))
248                .accept(MediaType.APPLICATION_XML).get(GROUP_LIST_GENERICTYPE);
249       
250       assertEquals(2, result.size());
251       assertEquals("newGroup1", result.get(0).getName());
252       assertEquals("newGroup2", result.get(1).getName());
253    }
254   
255   
256//    List<Group> getGroupsTheCurrentUserIsAMemberOf();   
257   
258    @Test
259    public void testGetGroupsTheCurrentUserIsAMemberOf() throws ItemNotFoundException{
260        System.out.println("test getGroupsTheCurrentUserIsAMemberOfr");
261       
262        MakeGroupA();
263        MakeGroupB();
264        // test itself
265       
266       List<Group> result = this.getAuthenticatedResource(getResource()
267                .path("/registry/groups/usermembership"))
268                .accept(MediaType.APPLICATION_XML).get(GROUP_LIST_GENERICTYPE);
269       
270       assertEquals(1, result.size());
271       assertEquals("group B", result.get(0).getName());
272    }
273//   
274//    List<Group> getGroupsTheItemIsAMemberOf(String itemId);
275   
276    @Test
277    public void testGetGroupsTheItemIsAMemberOf() throws ParseException, JAXBException, ItemNotFoundException{
278        System.out.println("test getGroupsTheItemIsAMemberOf");
279       
280        fillUpGroupA();
281        fillUpGroupB();
282        // test itself
283       
284       List<Group> result = this.getAuthenticatedResource(getResource()
285                .path("/registry/items/"+ComponentDescription.COMPONENT_PREFIX+"component-1/groups"))
286                .accept(MediaType.APPLICATION_XML).get(GROUP_LIST_GENERICTYPE);
287       
288       assertEquals(1, result.size());
289       assertEquals("group A", result.get(0).getName());
290       
291       result = this.getAuthenticatedResource(getResource()
292                .path("/registry/items/"+ComponentDescription.COMPONENT_PREFIX+"Bcomponent-1/groups"))
293                .accept(MediaType.APPLICATION_XML).get(GROUP_LIST_GENERICTYPE);
294       
295       assertEquals(1, result.size());
296       assertEquals("group B", result.get(0).getName());
297       
298       result = this.getAuthenticatedResource(getResource()
299                .path("/registry/items/"+ProfileDescription.PROFILE_PREFIX+"Bprofile-1/groups"))
300                .accept(MediaType.APPLICATION_XML).get(GROUP_LIST_GENERICTYPE);
301       
302       assertEquals(1, result.size());
303       assertEquals("group B", result.get(0).getName());
304       
305       result = this.getAuthenticatedResource(getResource()
306                .path("/registry/items/"+ProfileDescription.PROFILE_PREFIX+"profile-1/groups"))
307                .accept(MediaType.APPLICATION_XML).get(GROUP_LIST_GENERICTYPE);
308       
309       assertEquals(1, result.size());
310       assertEquals("group A", result.get(0).getName());
311    }
312   
313
314
315 
316     
317    @Test
318    public void testListGroupNames() throws ItemNotFoundException{
319        System.out.println("test listGroupNames");
320       
321        MakeGroupA();
322        MakeGroupB();
323       
324        // test itself
325       
326       ClientResponse cr  = this.getAuthenticatedResource(getResource()
327                .path("/registry/groups/names")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
328       assertEquals(200, cr.getStatus());
329       List<String> result = cr.getEntity(ComponentRegistryRestService.StringsWrapper.class).strings;
330       assertEquals(2, result.size());
331       assertEquals("group A", result.get(0));
332       assertEquals("group B", result.get(1));
333    }
334   
335//
336//    Response isOwner(String groupName) throws IOException;
337   
338    @Test
339    public void testIsOwner() throws ItemNotFoundException{
340        System.out.println("test isOwner");
341       
342        MakeGroupA();
343        MakeGroupB();
344       
345        // test itself
346       
347       ClientResponse cr  = this.getAuthenticatedResource(getResource()
348                .path("/registry/groups/ownership").queryParam("groupName", "group A")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
349       assertEquals(200, cr.getStatus());
350       String result = cr.getEntity(String.class);
351       assertEquals("true", result);
352       
353       cr  = this.getAuthenticatedResource(getResource()
354                .path("/registry/groups/ownership").queryParam("groupName", "group B")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
355       assertEquals(200, cr.getStatus());
356       result = cr.getEntity(String.class);
357       assertEquals("false", result);
358    }
359   
360   
361//
362//    Response makeGroupMember(String groupName, String principalName) throws IOException;
363   
364    @Test
365    public void testMakeGroupMember() throws ItemNotFoundException{
366        System.out.println("test makeGroupMember");
367       
368        MakeGroupA();
369        MakeGroupB();
370        // test itself
371       
372       //MultivaluedMap<String, String> params  = new  MultivaluedHashMap<String, String>();
373       ClientResponse cr  = this.getAuthenticatedResource(getResource()
374                .path("/registry/groups/makemember").queryParam("groupName", "group A").queryParam("principalName", "anotherPrincipal")).accept(MediaType.APPLICATION_XML).post(ClientResponse.class);
375       assertEquals(200, cr.getStatus());
376       
377       assertTrue(groupService.userGroupMember("anotherPrincipal", "1"));
378       
379               ;
380       cr  = this.getAuthenticatedResource(getResource()
381                .path("/registry/groups/makemember").queryParam("groupName", "group B").queryParam("principalName", "anotherPrincipal")).accept(MediaType.APPLICATION_XML).post(ClientResponse.class);
382       assertEquals(403, cr.getStatus());
383    }
384   
385//
386//   Response listProfiles(String groupId) throws IOException;
387   
388    @Test
389    public void testListProfilesAndComponents() throws Exception{
390        System.out.println("test listProfilesAndComponents");
391       
392        fillUpGroupA();
393       
394        // test itself
395       
396       ClientResponse cr  = this.getAuthenticatedResource(getResource()
397                .path("/registry/groups/profiles").queryParam("groupId", "1")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
398       assertEquals(200, cr.getStatus());
399       List<String> result = cr.getEntity(ComponentRegistryRestService.StringsWrapper.class).strings;
400       assertEquals(1, result.size());
401       assertEquals(ProfileDescription.PROFILE_PREFIX+"profile-1", result.get(0));
402       
403       cr  = this.getAuthenticatedResource(getResource()
404                .path("/registry/groups/components").queryParam("groupId", "1")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
405       assertEquals(200, cr.getStatus());
406       result = cr.getEntity(ComponentRegistryRestService.StringsWrapper.class).strings;
407       assertEquals(2, result.size());
408       assertEquals(ComponentDescription.COMPONENT_PREFIX+"component-1", result.get(0));
409       assertEquals(ComponentDescription.COMPONENT_PREFIX+"component-2", result.get(1));
410    }
411   
412
413 
414//
415//    Response getGroupNameById(String groupId) throws IOException;
416   
417    @Test
418    public void testGetGroupNamebyId() throws Exception{
419        System.out.println("test getGroupNamebyId");
420       
421        MakeGroupA();
422        MakeGroupB();
423        // test itself
424       
425       ClientResponse cr  = this.getAuthenticatedResource(getResource()
426                .path("/registry/groups/nameById").queryParam("groupId", "1")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
427       assertEquals(200, cr.getStatus());
428       String result = cr.getEntity(String.class);
429       assertEquals("group A", result);
430       
431      cr  = this.getAuthenticatedResource(getResource()
432                .path("/registry/groups/nameById").queryParam("groupId", "2")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
433       assertEquals(200, cr.getStatus());
434       result = cr.getEntity(String.class);
435       assertEquals("group B", result);
436    }
437   
438//
439//    Response getGroupIdByName(String groupName) throws IOException;
440   
441    @Test
442    public void testGetIdByGroupName() throws Exception{
443        System.out.println("test getIdByGroupName");
444       
445        MakeGroupA();
446        MakeGroupB();
447        // test itself
448       
449       ClientResponse cr  = this.getAuthenticatedResource(getResource()
450                .path("/registry/groups/idByName").queryParam("groupName", "group B")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
451       assertEquals(200, cr.getStatus());
452       String result = cr.getEntity(String.class);
453       assertEquals("2", result);
454       
455      cr  = this.getAuthenticatedResource(getResource()
456                .path("/registry/groups/idByName").queryParam("groupName", "group A")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
457       assertEquals(200, cr.getStatus());
458       result = cr.getEntity(String.class);
459       assertEquals("1", result);
460    }
461   
462     
463//    Response transferItemOwnershipToGroup(String itemId, long groupId) throws IOException;   
464//   
465
466    @Test
467    public void testTransferOwnership() throws Exception{
468        System.out.println("test makeTransferOwnership");
469       
470        fillUpGroupA();
471        fillUpGroupB();       
472        fillUpGroupC();
473        // test itself
474       
475       
476       RegistryTestHelper.addComponent(baseRegistry, "test_component", false);
477       RegistryTestHelper.addProfile(baseRegistry, "test_profile", false);
478       String test_profile_id = ProfileDescription.PROFILE_PREFIX+"test_profile";
479       String test_component_id = ComponentDescription.COMPONENT_PREFIX+"test_component";
480       //I'm not a member
481       ClientResponse cr  = this.getAuthenticatedResource(getResource()
482                .path("/registry/items/"+test_profile_id+"/transferownership").queryParam("groupId", "3")).accept(MediaType.APPLICATION_XML).post(ClientResponse.class);
483       assertEquals(403, cr.getStatus());
484       
485       //make me a member
486       groupService.makeMember(DummyPrincipal.DUMMY_PRINCIPAL.getName(), "group C");
487       assertTrue(groupService.userGroupMember(DummyPrincipal.DUMMY_PRINCIPAL.getName(), "3"));
488       
489       cr  = this.getAuthenticatedResource(getResource()
490                .path("/registry/items/"+test_profile_id+"/transferownership").queryParam("groupId", "3")).accept(MediaType.APPLICATION_XML).post(ClientResponse.class);
491       assertEquals(200, cr.getStatus());
492       cr  = this.getAuthenticatedResource(getResource()
493                .path("/registry/items/"+test_component_id+"/transferownership").queryParam("groupId", "3")).accept(MediaType.APPLICATION_XML).post(ClientResponse.class);
494       assertEquals(200, cr.getStatus());
495       
496       
497       List<String> components = groupService.getComponentIdsInGroup(3);
498       assertEquals(3, components.size());
499       assertEquals(test_component_id, components.get(2));
500       List<String> profiles = groupService.getProfileIdsInGroup(3);
501       assertEquals(2, profiles.size());
502       assertEquals(test_profile_id, profiles.get(1));
503       
504    }
505   
506    @Test
507    public void testGetGroupProfilesAndComponents() throws Exception {
508
509        System.out.println("test getGroupProfiles");
510
511        fillUpGroupA();
512        fillUpGroupB();       
513        fillUpGroupC();
514
515        // lists
516       
517        List<ProfileDescription> response = this.getAuthenticatedResource(getResource()
518                .path("/registry/profiles").queryParam("registrySpace", "group").queryParam("groupid", "1")).accept(MediaType.APPLICATION_XML)
519                .get(PROFILE_LIST_GENERICTYPE);
520        assertEquals(1, response.size());
521       
522        List<ComponentDescription> responseC = this.getAuthenticatedResource(getResource()
523                .path("/registry/components").queryParam("registrySpace", "group").queryParam("groupid", "1")).accept(MediaType.APPLICATION_XML)
524                .get(COMPONENT_LIST_GENERICTYPE);
525        assertEquals(2, responseC.size());
526       
527        response = this.getAuthenticatedResource(getResource()
528                .path("/registry/profiles").queryParam("registrySpace", "group").queryParam("groupid", "2")).accept(MediaType.APPLICATION_XML)
529                .get(PROFILE_LIST_GENERICTYPE);
530        assertEquals(1, response.size());
531       
532        responseC = this.getAuthenticatedResource(getResource()
533                .path("/registry/components").queryParam("registrySpace", "group").queryParam("groupid", "2")).accept(MediaType.APPLICATION_XML)
534                .get(COMPONENT_LIST_GENERICTYPE);
535        assertEquals(2, responseC.size());
536       
537        ClientResponse clientResponse = this.getAuthenticatedResource(getResource()
538                .path("/registry/components").queryParam("registrySpace", "group").queryParam("groupid", "3")).accept(MediaType.APPLICATION_XML)
539                .get(ClientResponse.class);
540       
541        assertEquals(403, clientResponse.getStatus());
542       
543        // particular components and profiles
544       
545        CMDComponentSpec component = this.getAuthenticatedResource(getResource()
546                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"profile-1"))
547                .accept(MediaType.APPLICATION_JSON).get(CMDComponentSpec.class);
548        assertNotNull(component);
549        assertEquals("Actor", component.getCMDComponent().getName());
550   
551        component = this.getAuthenticatedResource(getResource()
552                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"component-1"))
553                .accept(MediaType.APPLICATION_JSON).get(CMDComponentSpec.class);
554        assertNotNull(component);
555        assertEquals("Access", component.getCMDComponent().getName());
556   
557        component = this.getAuthenticatedResource(getResource()
558                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Bprofile-1"))
559                .accept(MediaType.APPLICATION_JSON).get(CMDComponentSpec.class);
560        assertNotNull(component);
561        assertEquals("Actor", component.getCMDComponent().getName());
562   
563        component = this.getAuthenticatedResource(getResource()
564                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Bcomponent-1"))
565                .accept(MediaType.APPLICATION_JSON).get(CMDComponentSpec.class);
566        assertNotNull(component);
567        assertEquals("Access", component.getCMDComponent().getName());
568   
569        clientResponse = this.getAuthenticatedResource(getResource()
570                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Cprofile-1"))
571                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
572        assertEquals(403, clientResponse.getStatus());
573       
574         clientResponse = this.getAuthenticatedResource(getResource()
575                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1"))
576                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
577        assertEquals(403, clientResponse.getStatus());
578       
579    }
580   
581   @Test
582    public void testGetGroupComments() throws Exception {
583
584        System.out.println("test getGroupComments");
585
586        fillUpGroupA();
587        fillUpGroupB();       
588        fillUpGroupC();
589
590       
591        RegistryTestHelper.addComment(baseRegistry, "COMMENTc1",  ComponentDescription.COMPONENT_PREFIX + "component-1",
592                "JUnit@test.com");
593        RegistryTestHelper.addComment(baseRegistry, "COMMENTp1",  ProfileDescription.PROFILE_PREFIX + "profile-1",
594                "JUnit@test.com");
595        RegistryTestHelper.addComment(baseRegistry, "COMMENTBc1",  ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1",
596                "anotherPrincipal");
597        RegistryTestHelper.addComment(baseRegistry, "COMMENTBp1",  ProfileDescription.PROFILE_PREFIX + "Bprofile-1",
598                "anotherPrincipal");
599       (new RegistryTestHelper()).addCommentBypassAuthorisation(commentsDao, "COMMENTCc1",  ComponentDescription.COMPONENT_PREFIX + "Ccomponent-1",
600                "anotherPrincipal");
601       (new RegistryTestHelper()).addCommentBypassAuthorisation(commentsDao, "COMMENTCp1",  ProfileDescription.PROFILE_PREFIX + "Cprofile-1","anotherPrincipal");
602         
603         
604                // lists
605       
606        List<Comment> response = this.getAuthenticatedResource(getResource()
607                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "component-1" + "/comments/"))
608                .accept(MediaType.APPLICATION_XML)
609                .get(COMMENT_LIST_GENERICTYPE);       
610        assertEquals(1, response.size());
611       
612        response = this.getAuthenticatedResource(getResource()
613                .path("/registry/components/" + ProfileDescription.PROFILE_PREFIX + "profile-1" + "/comments/"))
614                .accept(MediaType.APPLICATION_XML)
615                .get(COMMENT_LIST_GENERICTYPE);       
616        assertEquals(1, response.size());
617       
618        response = this.getAuthenticatedResource(getResource()
619                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1" + "/comments/"))
620                .accept(MediaType.APPLICATION_XML)
621                .get(COMMENT_LIST_GENERICTYPE);       
622        assertEquals(1, response.size());
623       
624        response = this.getAuthenticatedResource(getResource()
625                .path("/registry/components/" + ProfileDescription.PROFILE_PREFIX + "Bprofile-1" + "/comments/"))
626                .accept(MediaType.APPLICATION_XML)
627                .get(COMMENT_LIST_GENERICTYPE);       
628        assertEquals(1, response.size());
629       
630       
631   
632        ClientResponse clientResponse = this.getAuthenticatedResource(getResource()
633                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Cprofile-1/comments"))
634                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
635        assertEquals(403, clientResponse.getStatus());
636       
637         clientResponse = this.getAuthenticatedResource(getResource()
638                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1/comments"))
639                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
640        assertEquals(403, clientResponse.getStatus());
641       
642        // particular comments
643       
644       Comment responseComment = this.getAuthenticatedResource(getResource()
645                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "component-1" + "/comments/1"))
646                .accept(MediaType.APPLICATION_XML)
647                .get(Comment.class);       
648        assertNotNull(responseComment);
649     
650        assertEquals(1, Long.parseLong(responseComment.getId()));
651       
652        responseComment = this.getAuthenticatedResource(getResource()
653                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX + "profile-1" + "/comments/2"))
654                .accept(MediaType.APPLICATION_XML)
655                .get(Comment.class);       
656        assertNotNull(responseComment);
657       
658        assertEquals(2, Long.parseLong(responseComment.getId()));
659       
660       responseComment = this.getAuthenticatedResource(getResource()
661                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1" + "/comments/3"))
662                .accept(MediaType.APPLICATION_XML)
663                .get(Comment.class);       
664        assertNotNull(responseComment);
665        assertEquals(3, Long.parseLong(responseComment.getId()));
666       
667        responseComment = this.getAuthenticatedResource(getResource()
668                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX + "Bprofile-1" + "/comments/4"))
669                .accept(MediaType.APPLICATION_XML)
670                .get(Comment.class);       
671        assertNotNull(responseComment);
672        assertEquals(4, Long.parseLong(responseComment.getId()));
673       
674        clientResponse = this.getAuthenticatedResource(getResource()
675                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Cprofile-1/comments/6"))
676                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
677        assertEquals(403, clientResponse.getStatus());
678       
679         clientResponse = this.getAuthenticatedResource(getResource()
680                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1/comments/5"))
681                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
682        assertEquals(403, clientResponse.getStatus());
683       
684    }
685   
686    @Test
687    public void testGetGroupRss() throws Exception {
688
689        System.out.println("test getGroupRss");
690
691        fillUpGroupA();
692        fillUpGroupB();       
693        fillUpGroupC();
694
695       
696         // lists of profiles and components
697       
698        Rss response = this.getAuthenticatedResource(getResource()
699                .path("/registry/profiles/rss").queryParam("registrySpace", "group").queryParam("groupid", "1")).accept(MediaType.APPLICATION_XML)
700                .get(Rss.class);
701        assertEquals(1, response.getChannel().getItem().size());
702       
703         response = this.getAuthenticatedResource(getResource()
704                .path("/registry/components/rss").queryParam("registrySpace", "group").queryParam("groupid", "1")).accept(MediaType.APPLICATION_XML)
705                .get(Rss.class);
706        assertEquals(2, response.getChannel().getItem().size());
707       
708        response = this.getAuthenticatedResource(getResource()
709                .path("/registry/profiles/rss").queryParam("registrySpace", "group").queryParam("groupid", "2")).accept(MediaType.APPLICATION_XML)
710                .get(Rss.class);
711        assertEquals(1, response.getChannel().getItem().size());
712       
713        response = this.getAuthenticatedResource(getResource()
714                .path("/registry/components/rss").queryParam("registrySpace", "group").queryParam("groupid", "2")).accept(MediaType.APPLICATION_XML)
715                .get(Rss.class);
716        assertEquals(2, response.getChannel().getItem().size());
717       
718        ClientResponse clientResponse = this.getAuthenticatedResource(getResource()
719                .path("/registry/components").queryParam("registrySpace", "group").queryParam("groupid", "3")).accept(MediaType.APPLICATION_XML)
720                .get(ClientResponse.class);
721       
722        assertEquals(403, clientResponse.getStatus());
723       
724        RegistryTestHelper.addComment(baseRegistry, "COMMENTc1",  ComponentDescription.COMPONENT_PREFIX + "component-1",
725                "JUnit@test.com");
726        RegistryTestHelper.addComment(baseRegistry, "COMMENTp1",  ProfileDescription.PROFILE_PREFIX + "profile-1",
727                "JUnit@test.com");
728        RegistryTestHelper.addComment(baseRegistry, "COMMENTBc1",  ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1",
729                "anotherPrincipal");
730        RegistryTestHelper.addComment(baseRegistry, "COMMENTBp1",  ProfileDescription.PROFILE_PREFIX + "Bprofile-1",
731                "anotherPrincipal");
732       (new RegistryTestHelper()).addCommentBypassAuthorisation(commentsDao, "COMMENTCc1",  ComponentDescription.COMPONENT_PREFIX + "Ccomponent-1",
733                "anotherPrincipal");
734       (new RegistryTestHelper()).addCommentBypassAuthorisation(commentsDao, "COMMENTCp1",  ProfileDescription.PROFILE_PREFIX + "Cprofile-1","anotherPrincipal");
735         
736         
737                // lists
738       
739        response = this.getAuthenticatedResource(getResource()
740                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "component-1" + "/comments/rss"))
741                .accept(MediaType.APPLICATION_XML)
742                .get(Rss.class);       
743        assertEquals(1, response.getChannel().getItem().size());
744       
745        response = this.getAuthenticatedResource(getResource()
746                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX + "profile-1" + "/comments/rss"))
747                .accept(MediaType.APPLICATION_XML)
748                .get(Rss.class);       
749        assertEquals(1, response.getChannel().getItem().size());
750       
751        response = this.getAuthenticatedResource(getResource()
752                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1" + "/comments/rss"))
753                .accept(MediaType.APPLICATION_XML)
754                .get(Rss.class);       
755        assertEquals(1, response.getChannel().getItem().size());
756       
757        response = this.getAuthenticatedResource(getResource()
758                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX + "Bprofile-1" + "/comments/rss"))
759                .accept(MediaType.APPLICATION_XML)
760                .get(Rss.class);       
761        assertEquals(1, response.getChannel().getItem().size());
762       
763       
764   
765        clientResponse = this.getAuthenticatedResource(getResource()
766                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Cprofile-1/comments/rss"))
767                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
768        assertEquals(403, clientResponse.getStatus());
769       
770         clientResponse = this.getAuthenticatedResource(getResource()
771                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1/comments/rss"))
772                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
773        assertEquals(403, clientResponse.getStatus());
774       
775        // particular comments
776       
777       Comment responseComment = this.getAuthenticatedResource(getResource()
778                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "component-1" + "/comments/1"))
779                .accept(MediaType.APPLICATION_XML)
780                .get(Comment.class);       
781        assertNotNull(responseComment);
782     
783        assertEquals(1, Long.parseLong(responseComment.getId()));
784       
785        responseComment = this.getAuthenticatedResource(getResource()
786                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX + "profile-1" + "/comments/2"))
787                .accept(MediaType.APPLICATION_XML)
788                .get(Comment.class);       
789        assertNotNull(responseComment);
790       
791        assertEquals(2, Long.parseLong(responseComment.getId()));
792       
793       responseComment = this.getAuthenticatedResource(getResource()
794                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1" + "/comments/3"))
795                .accept(MediaType.APPLICATION_XML)
796                .get(Comment.class);       
797        assertNotNull(responseComment);
798        assertEquals(3, Long.parseLong(responseComment.getId()));
799       
800        responseComment = this.getAuthenticatedResource(getResource()
801                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX + "Bprofile-1" + "/comments/4"))
802                .accept(MediaType.APPLICATION_XML)
803                .get(Comment.class);       
804        assertNotNull(responseComment);
805        assertEquals(4, Long.parseLong(responseComment.getId()));
806       
807        clientResponse = this.getAuthenticatedResource(getResource()
808                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Cprofile-1/comments/6"))
809                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
810        assertEquals(403, clientResponse.getStatus());
811       
812         clientResponse = this.getAuthenticatedResource(getResource()
813                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1/comments/5"))
814                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
815        assertEquals(403, clientResponse.getStatus());
816       
817    }
818   
819   
820    private FormDataMultiPart createFormData(Object content) {
821        return createFormData(content, "My Test");
822    }
823
824    private FormDataMultiPart createFormData(Object content, String description) {
825        FormDataMultiPart form = new FormDataMultiPart();
826        form.field(IComponentRegistryRestService.DATA_FORM_FIELD, content,
827                MediaType.APPLICATION_OCTET_STREAM_TYPE);
828        form.field(IComponentRegistryRestService.NAME_FORM_FIELD, "Test1");
829        form.field(IComponentRegistryRestService.DESCRIPTION_FORM_FIELD,
830                description);
831        form.field(IComponentRegistryRestService.DOMAIN_FORM_FIELD, "My domain");
832        form.field(IComponentRegistryRestService.GROUP_FORM_FIELD, "TestGroup");
833        return form;
834    }
835   
836    @Test
837    public void testUpdateGroupComponentAndProfile() throws Exception {
838
839        System.out.println("test updateGorupComponent");
840
841        fillUpGroupA();
842        fillUpGroupB();       
843        fillUpGroupC();
844
845     
846       
847        FormDataMultiPart form = createFormData(
848                RegistryTestHelper.getComponentTestContentAsStream("TESTNAME"),
849                "UPDATE DESCRIPTION!");
850        ClientResponse cResponse = getAuthenticatedResource(getResource().path(
851                "/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"component-1" + "/update")).type(
852                MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
853        assertEquals(ClientResponse.Status.OK.getStatusCode(),
854                cResponse.getStatus());
855        RegisterResponse response = cResponse.getEntity(RegisterResponse.class);
856        assertTrue(response.isRegistered());
857        assertFalse(response.isProfile());
858        assertTrue(response.isInUserSpace());
859        ComponentDescription desc = (ComponentDescription) response.getDescription();
860        assertNotNull(desc);
861        assertEquals("Test1", desc.getName());
862        assertEquals("UPDATE DESCRIPTION!", desc.getDescription());
863       
864        form = createFormData(
865                RegistryTestHelper.getComponentTestContentAsStream("TESTNAME"),
866                "UPDATE DESCRIPTION!");
867        cResponse = getAuthenticatedResource(getResource().path(
868                "/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Bcomponent-1" + "/update")).type(
869                MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
870        assertEquals(ClientResponse.Status.OK.getStatusCode(),
871                cResponse.getStatus());
872        response = cResponse.getEntity(RegisterResponse.class);
873        assertTrue(response.isRegistered());
874        assertFalse(response.isProfile());
875        assertTrue(response.isInUserSpace());
876        desc = (ComponentDescription) response.getDescription();
877        assertNotNull(desc);
878        assertEquals("Test1", desc.getName());
879        assertEquals("UPDATE DESCRIPTION!", desc.getDescription());
880       
881        form = createFormData(
882                RegistryTestHelper.getComponentTestContentAsStream("TESTNAME"),
883                "UPDATE DESCRIPTION!");
884        cResponse = getAuthenticatedResource(getResource().path(
885                "/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1" + "/update")).type(
886                MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
887        assertEquals(403, cResponse.getStatus());
888       
889        // profile
890       
891        form = createFormData(RegistryTestHelper.getTestProfileContent("TESTNAME"),
892                "UPDATE DESCRIPTION!");
893        cResponse = getAuthenticatedResource(getResource().path(
894                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"profile-1" + "/update")).type(
895                MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
896        assertEquals(ClientResponse.Status.OK.getStatusCode(),
897                cResponse.getStatus());
898        response = cResponse.getEntity(RegisterResponse.class);
899        assertTrue(response.isRegistered());
900        assertTrue(response.isProfile());
901        assertTrue(response.isInUserSpace());
902        ProfileDescription pdesc = (ProfileDescription) response.getDescription();
903        assertNotNull(pdesc);
904        assertEquals("Test1", pdesc.getName());
905        assertEquals("UPDATE DESCRIPTION!", pdesc.getDescription());
906       
907        form = createFormData(
908                RegistryTestHelper.getTestProfileContent("TESTNAME"),
909                "UPDATE DESCRIPTION!");
910        cResponse = getAuthenticatedResource(getResource().path(
911                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Bprofile-1" + "/update")).type(
912                MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
913        assertEquals(ClientResponse.Status.OK.getStatusCode(),
914                cResponse.getStatus());
915        response = cResponse.getEntity(RegisterResponse.class);
916        assertTrue(response.isRegistered());
917        assertTrue(response.isProfile());
918        assertTrue(response.isInUserSpace());
919        pdesc = (ProfileDescription) response.getDescription();
920        assertNotNull(pdesc);
921        assertEquals("Test1", pdesc.getName());
922        assertEquals("UPDATE DESCRIPTION!", pdesc.getDescription());
923       
924         form = createFormData(
925                RegistryTestHelper.getTestProfileContent("TESTNAME"),
926                "UPDATE DESCRIPTION!");
927        cResponse = getAuthenticatedResource(getResource().path(
928                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Cprofile-1" + "/update")).type(
929                MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form);
930        assertEquals(403, cResponse.getStatus());
931       
932    }
933
934}
Note: See TracBrowser for help on using the repository browser.