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

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

Adding transfer owner ship and fixing the method

File size: 20.3 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.impl.database.ComponentRegistryTestDatabase;
11import clarin.cmdi.componentregistry.impl.database.GroupService;
12import clarin.cmdi.componentregistry.model.Comment;
13import clarin.cmdi.componentregistry.model.ComponentDescription;
14import clarin.cmdi.componentregistry.model.Group;
15import clarin.cmdi.componentregistry.model.Ownership;
16import clarin.cmdi.componentregistry.model.ProfileDescription;
17import clarin.cmdi.componentregistry.model.RegistryUser;
18import clarin.cmdi.componentregistry.persistence.jpa.UserDao;
19import com.sun.jersey.api.client.ClientResponse;
20import java.text.ParseException;
21import java.util.List;
22import javax.ws.rs.core.MediaType;
23import javax.ws.rs.core.MultivaluedMap;
24import javax.xml.bind.JAXBException;
25import org.junit.Before;
26import org.springframework.beans.factory.annotation.Autowired;
27import org.springframework.jdbc.core.JdbcTemplate;
28
29import static org.junit.Assert.*;
30import org.junit.Test;
31
32/**
33 *
34 * @author olhsha
35 */
36public class RestGroupServiceTest extends ComponentRegistryRestServiceTestCase {
37   
38    @Autowired
39    private ComponentRegistryFactory componentRegistryFactory;
40   
41    @Autowired
42    private JdbcTemplate jdbcTemplate;
43    @Autowired 
44    private GroupService groupService;
45    @Autowired 
46    private UserDao userDao;
47   
48    private ComponentRegistry baseRegistry;
49
50    @Before
51    public void init() {
52        ComponentRegistryTestDatabase.resetAndCreateAllTables(jdbcTemplate);
53        createUserRecord();
54        baseRegistry = componentRegistryFactory.getBaseRegistry(DummyPrincipal.DUMMY_CREDENTIALS);
55    }
56
57    private String expectedUserId(String principal) {
58        return getUserDao().getByPrincipalName(principal).getId().toString();
59    }
60    private ComponentDescription component1;
61    private ComponentDescription component2;
62    private ProfileDescription profile1;
63    private ProfileDescription profile2;
64    private ComponentDescription component3;
65    private ProfileDescription profile3;
66    private Comment profile1Comment1;
67    private Comment profile1Comment2;
68    private Comment component1Comment3;
69    private Comment component1Comment4;
70    private Comment profile3Comment5;
71    private Comment component3Comment7;
72
73    private void fillUpPublicItems() throws Exception {
74
75        profile1 = RegistryTestHelper.addProfile(baseRegistry, "profile2", true);
76        profile2 = RegistryTestHelper.addProfile(baseRegistry, "profile1", true);
77        component1 = RegistryTestHelper.addComponent(baseRegistry,
78                "component2", true);
79        component2 = RegistryTestHelper.addComponent(baseRegistry,
80                "component1", true);
81        profile1Comment2 = RegistryTestHelper.addComment(baseRegistry, "comment2",
82                ProfileDescription.PROFILE_PREFIX + "profile1",
83                "JUnit@test.com");
84        profile1Comment1 = RegistryTestHelper.addComment(baseRegistry, "comment1",
85                ProfileDescription.PROFILE_PREFIX + "profile1",
86                "JUnit@test.com");
87        component1Comment3 = RegistryTestHelper.addComment(baseRegistry, "comment3",
88                ComponentDescription.COMPONENT_PREFIX + "component1",
89                "JUnit@test.com");
90        component1Comment4 = RegistryTestHelper.addComment(baseRegistry, "comment4",
91                ComponentDescription.COMPONENT_PREFIX + "component1",
92                "JUnit@test.com");
93    }
94
95    private void fillUpPrivateItems() throws Exception {
96        profile3 = RegistryTestHelper.addProfile(baseRegistry, "profile3", false);
97        component3 = RegistryTestHelper.addComponent(baseRegistry,
98                "component3", false);
99        profile3Comment5 = RegistryTestHelper.addComment(baseRegistry, "comment5",
100                ProfileDescription.PROFILE_PREFIX + "profile3",
101                "JUnit@test.com");
102        component3Comment7 = RegistryTestHelper.addComment(baseRegistry, "comment7",
103                ComponentDescription.COMPONENT_PREFIX + "component3",
104                "JUnit@test.com");
105    }
106   
107     protected void createAntherUserRecord() {
108        RegistryUser user = new RegistryUser();
109        user.setName("Another database test user");
110        user.setPrincipalName("anotherPrincipal");
111        userDao.save(user);
112    }
113     
114    private void MakeGroupA(){
115        groupService.createNewGroup("group A", DummyPrincipal.DUMMY_PRINCIPAL.getName());
116    }
117   
118     private void fillUpGroupA() throws ParseException, JAXBException, ItemNotFoundException{
119         
120        MakeGroupA();
121       
122        RegistryTestHelper.addProfile(baseRegistry, "profile-1", false);
123        RegistryTestHelper.addComponent(baseRegistry, "component-1", false);
124        RegistryTestHelper.addComponent(baseRegistry, "component-2", false);
125       
126        Ownership ownership = new Ownership();
127        ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"profile-1");
128        ownership.setGroupId(1);
129        ownership.setUserId(0);
130        groupService.addOwnership(ownership);
131       
132        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"component-1");
133        ownership.setGroupId(1);
134        ownership.setUserId(0);
135        groupService.addOwnership(ownership);
136       
137        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"component-2");
138        ownership.setGroupId(1);
139        ownership.setUserId(0);
140        groupService.addOwnership(ownership);
141       
142    }
143     
144     
145   
146    private void MakeGroupB() throws ItemNotFoundException{
147        createAntherUserRecord();
148        groupService.createNewGroup("group B", "anotherPrincipal");
149        groupService.makeMember(DummyPrincipal.DUMMY_PRINCIPAL.getName(), "group B");
150    }
151   
152     private void MakeGroupC() throws ItemNotFoundException{
153        groupService.createNewGroup("group C", "anotherPrincipal");
154    }
155   
156     private void fillUpGroupB() throws ParseException, JAXBException, ItemNotFoundException{
157         
158        MakeGroupB();
159       
160        RegistryTestHelper.addProfile(baseRegistry, "Bprofile-1", false);
161        RegistryTestHelper.addComponent(baseRegistry, "Bcomponent-1", false);
162        RegistryTestHelper.addComponent(baseRegistry, "Bcomponent-2", false);
163       
164        Ownership ownership = new Ownership();
165        ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"Bprofile-1");
166        ownership.setGroupId(2);
167        ownership.setUserId(0);
168        groupService.addOwnership(ownership);
169       
170        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Bcomponent-1");
171        ownership.setGroupId(2);
172        ownership.setUserId(0);
173        groupService.addOwnership(ownership);
174       
175        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Bcomponent-2");
176        ownership.setGroupId(2);
177        ownership.setUserId(0);
178        groupService.addOwnership(ownership);
179       
180    }
181     
182     private void fillUpGroupC() throws ParseException, JAXBException, ItemNotFoundException{
183         
184        MakeGroupC();
185       
186        RegistryTestHelper.addProfile(baseRegistry, "Cprofile-1", false);
187        RegistryTestHelper.addComponent(baseRegistry, "Ccomponent-1", false);
188        RegistryTestHelper.addComponent(baseRegistry, "Ccomponent-2", false);
189       
190        Ownership ownership = new Ownership();
191        ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"Cprofile-1");
192        ownership.setGroupId(3);
193        ownership.setUserId(0);
194        groupService.addOwnership(ownership);
195       
196        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1");
197        ownership.setGroupId(3);
198        ownership.setUserId(0);
199        groupService.addOwnership(ownership);
200       
201        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Ccomponent-2");
202        ownership.setGroupId(3);
203        ownership.setUserId(0);
204        groupService.addOwnership(ownership);
205       
206    }
207   
208 //    Response createNewGroup(String groupName) throws IOException;
209   
210    @Test
211    public void testCreateNewGroup() {
212        System.out.println("test createNewGroup");
213        ClientResponse cr = this.getAuthenticatedResource(getResource()
214                .path("/registry/groups/create").queryParam("groupName", "newGroup"))
215                .accept(MediaType.TEXT_XML).post(ClientResponse.class);
216        assertEquals(200, cr.getStatus());
217        assertEquals("Group with the name newGroup is created and given an id 1", cr.getEntity(String.class));
218    }
219   
220   
221   
222   
223//  List<Group> getGroupsOwnedByUser(String pricipalName) throws IOException;
224   
225    @Test
226    public void testGetGroupsOwnedByUser() {
227        System.out.println("test GetGroupsOwnedByUser");
228        ClientResponse cr = this.getAuthenticatedResource(getResource()
229                .path("/registry/groups/create").queryParam("groupName", "newGroup1"))
230                .accept(MediaType.TEXT_XML).post(ClientResponse.class);
231        assertEquals(200, cr.getStatus());
232        ClientResponse cr1 = this.getAuthenticatedResource(getResource()
233                .path("/registry/groups/create").queryParam("groupName", "newGroup2"))
234                .accept(MediaType.TEXT_XML).post(ClientResponse.class);
235        assertEquals(200, cr1.getStatus());
236       
237        // test itself
238       
239       List<Group> result = this.getAuthenticatedResource(getResource()
240                .path("/registry/groups/principal").queryParam("principalName", DummyPrincipal.DUMMY_PRINCIPAL.getName()))
241                .accept(MediaType.APPLICATION_XML).get(GROUP_LIST_GENERICTYPE);
242       
243       assertEquals(2, result.size());
244       assertEquals("newGroup1", result.get(0).getName());
245       assertEquals("newGroup2", result.get(1).getName());
246    }
247   
248   
249//    List<Group> getGroupsTheCurrentUserIsAMemberOf();   
250   
251    @Test
252    public void testGetGroupsTheCurrentUserIsAMemberOf() throws ItemNotFoundException{
253        System.out.println("test getGroupsTheCurrentUserIsAMemberOfr");
254       
255        MakeGroupA();
256        MakeGroupB();
257        // test itself
258       
259       List<Group> result = this.getAuthenticatedResource(getResource()
260                .path("/registry/groups/usermembership"))
261                .accept(MediaType.APPLICATION_XML).get(GROUP_LIST_GENERICTYPE);
262       
263       assertEquals(1, result.size());
264       assertEquals("group B", result.get(0).getName());
265    }
266//   
267//    List<Group> getGroupsTheItemIsAMemberOf(String itemId);
268   
269    @Test
270    public void testGetGroupsTheItemIsAMemberOf() throws ParseException, JAXBException, ItemNotFoundException{
271        System.out.println("test getGroupsTheItemIsAMemberOf");
272       
273        fillUpGroupA();
274        fillUpGroupB();
275        // test itself
276       
277       List<Group> result = this.getAuthenticatedResource(getResource()
278                .path("/registry/items/"+ComponentDescription.COMPONENT_PREFIX+"component-1/groups"))
279                .accept(MediaType.APPLICATION_XML).get(GROUP_LIST_GENERICTYPE);
280       
281       assertEquals(1, result.size());
282       assertEquals("group A", result.get(0).getName());
283       
284       result = this.getAuthenticatedResource(getResource()
285                .path("/registry/items/"+ComponentDescription.COMPONENT_PREFIX+"Bcomponent-1/groups"))
286                .accept(MediaType.APPLICATION_XML).get(GROUP_LIST_GENERICTYPE);
287       
288       assertEquals(1, result.size());
289       assertEquals("group B", result.get(0).getName());
290       
291       result = this.getAuthenticatedResource(getResource()
292                .path("/registry/items/"+ProfileDescription.PROFILE_PREFIX+"Bprofile-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+"profile-1/groups"))
300                .accept(MediaType.APPLICATION_XML).get(GROUP_LIST_GENERICTYPE);
301       
302       assertEquals(1, result.size());
303       assertEquals("group A", result.get(0).getName());
304    }
305   
306
307
308 
309     
310    @Test
311    public void testListGroupNames() throws ItemNotFoundException{
312        System.out.println("test listGroupNames");
313       
314        MakeGroupA();
315        MakeGroupB();
316       
317        // test itself
318       
319       ClientResponse cr  = this.getAuthenticatedResource(getResource()
320                .path("/registry/groups/names")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
321       assertEquals(200, cr.getStatus());
322       List<String> result = cr.getEntity(ComponentRegistryRestService.StringsWrapper.class).strings;
323       assertEquals(2, result.size());
324       assertEquals("group A", result.get(0));
325       assertEquals("group B", result.get(1));
326    }
327   
328//
329//    Response isOwner(String groupName) throws IOException;
330   
331    @Test
332    public void testIsOwner() throws ItemNotFoundException{
333        System.out.println("test isOwner");
334       
335        MakeGroupA();
336        MakeGroupB();
337       
338        // test itself
339       
340       ClientResponse cr  = this.getAuthenticatedResource(getResource()
341                .path("/registry/groups/ownership").queryParam("groupName", "group A")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
342       assertEquals(200, cr.getStatus());
343       String result = cr.getEntity(String.class);
344       assertEquals("true", result);
345       
346       cr  = this.getAuthenticatedResource(getResource()
347                .path("/registry/groups/ownership").queryParam("groupName", "group B")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
348       assertEquals(200, cr.getStatus());
349       result = cr.getEntity(String.class);
350       assertEquals("false", result);
351    }
352   
353   
354//
355//    Response makeGroupMember(String groupName, String principalName) throws IOException;
356   
357    @Test
358    public void testMakeGroupMember() throws ItemNotFoundException{
359        System.out.println("test makeGroupMember");
360       
361        MakeGroupA();
362        MakeGroupB();
363        // test itself
364       
365       //MultivaluedMap<String, String> params  = new  MultivaluedHashMap<String, String>();
366       ClientResponse cr  = this.getAuthenticatedResource(getResource()
367                .path("/registry/groups/makemember").queryParam("groupName", "group A").queryParam("principalName", "anotherPrincipal")).accept(MediaType.APPLICATION_XML).post(ClientResponse.class);
368       assertEquals(200, cr.getStatus());
369       
370       assertTrue(groupService.userGroupMember("anotherPrincipal", "1"));
371       
372               ;
373       cr  = this.getAuthenticatedResource(getResource()
374                .path("/registry/groups/makemember").queryParam("groupName", "group B").queryParam("principalName", "anotherPrincipal")).accept(MediaType.APPLICATION_XML).post(ClientResponse.class);
375       assertEquals(403, cr.getStatus());
376    }
377   
378//
379//   Response listProfiles(String groupId) throws IOException;
380   
381    @Test
382    public void testListProfilesAndComponents() throws Exception{
383        System.out.println("test listProfilesAndComponents");
384       
385        fillUpGroupA();
386       
387        // test itself
388       
389       ClientResponse cr  = this.getAuthenticatedResource(getResource()
390                .path("/registry/groups/profiles").queryParam("groupId", "1")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
391       assertEquals(200, cr.getStatus());
392       List<String> result = cr.getEntity(ComponentRegistryRestService.StringsWrapper.class).strings;
393       assertEquals(1, result.size());
394       assertEquals(ProfileDescription.PROFILE_PREFIX+"profile-1", result.get(0));
395       
396       cr  = this.getAuthenticatedResource(getResource()
397                .path("/registry/groups/components").queryParam("groupId", "1")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
398       assertEquals(200, cr.getStatus());
399       result = cr.getEntity(ComponentRegistryRestService.StringsWrapper.class).strings;
400       assertEquals(2, result.size());
401       assertEquals(ComponentDescription.COMPONENT_PREFIX+"component-1", result.get(0));
402       assertEquals(ComponentDescription.COMPONENT_PREFIX+"component-2", result.get(1));
403    }
404   
405
406 
407//
408//    Response getGroupNameById(String groupId) throws IOException;
409   
410    @Test
411    public void testGetGroupNamebyId() throws Exception{
412        System.out.println("test getGroupNamebyId");
413       
414        MakeGroupA();
415        MakeGroupB();
416        // test itself
417       
418       ClientResponse cr  = this.getAuthenticatedResource(getResource()
419                .path("/registry/groups/nameById").queryParam("groupId", "1")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
420       assertEquals(200, cr.getStatus());
421       String result = cr.getEntity(String.class);
422       assertEquals("group A", result);
423       
424      cr  = this.getAuthenticatedResource(getResource()
425                .path("/registry/groups/nameById").queryParam("groupId", "2")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
426       assertEquals(200, cr.getStatus());
427       result = cr.getEntity(String.class);
428       assertEquals("group B", result);
429    }
430   
431//
432//    Response getGroupIdByName(String groupName) throws IOException;
433   
434    @Test
435    public void testGetIdByGroupName() throws Exception{
436        System.out.println("test getIdByGroupName");
437       
438        MakeGroupA();
439        MakeGroupB();
440        // test itself
441       
442       ClientResponse cr  = this.getAuthenticatedResource(getResource()
443                .path("/registry/groups/idByName").queryParam("groupName", "group B")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
444       assertEquals(200, cr.getStatus());
445       String result = cr.getEntity(String.class);
446       assertEquals("2", result);
447       
448      cr  = this.getAuthenticatedResource(getResource()
449                .path("/registry/groups/idByName").queryParam("groupName", "group A")).accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
450       assertEquals(200, cr.getStatus());
451       result = cr.getEntity(String.class);
452       assertEquals("1", result);
453    }
454   
455     
456//    Response transferItemOwnershipToGroup(String itemId, long groupId) throws IOException;   
457//   
458
459    @Test
460    public void testTransferOwnership() throws Exception{
461        System.out.println("test makeTransferOwnership");
462       
463        fillUpGroupA();
464        fillUpGroupB();       
465        fillUpGroupC();
466        // test itself
467       
468       
469       RegistryTestHelper.addComponent(baseRegistry, "test_component", false);
470       RegistryTestHelper.addProfile(baseRegistry, "test_profile", false);
471       String test_profile_id = ProfileDescription.PROFILE_PREFIX+"test_profile";
472       String test_component_id = ComponentDescription.COMPONENT_PREFIX+"test_component";
473       //I'm not a member
474       ClientResponse cr  = this.getAuthenticatedResource(getResource()
475                .path("/registry/items/"+test_profile_id+"/transferownership").queryParam("groupId", "3")).accept(MediaType.APPLICATION_XML).post(ClientResponse.class);
476       assertEquals(403, cr.getStatus());
477       
478       //make me a member
479       groupService.makeMember(DummyPrincipal.DUMMY_PRINCIPAL.getName(), "group C");
480       assertTrue(groupService.userGroupMember(DummyPrincipal.DUMMY_PRINCIPAL.getName(), "3"));
481       
482       cr  = this.getAuthenticatedResource(getResource()
483                .path("/registry/items/"+test_profile_id+"/transferownership").queryParam("groupId", "3")).accept(MediaType.APPLICATION_XML).post(ClientResponse.class);
484       assertEquals(200, cr.getStatus());
485       cr  = this.getAuthenticatedResource(getResource()
486                .path("/registry/items/"+test_component_id+"/transferownership").queryParam("groupId", "3")).accept(MediaType.APPLICATION_XML).post(ClientResponse.class);
487       assertEquals(200, cr.getStatus());
488       
489       
490       List<String> components = groupService.getComponentIdsInGroup(3);
491       assertEquals(3, components.size());
492       assertEquals(test_component_id, components.get(2));
493       List<String> profiles = groupService.getProfileIdsInGroup(3);
494       assertEquals(2, profiles.size());
495       assertEquals(test_profile_id, profiles.get(1));
496       
497    }
498   
499
500}
Note: See TracBrowser for help on using the repository browser.