source: ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/SanboxTest.java @ 5560

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

Unit test for getting comments of profiles and components from groups.

File size: 13.4 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.Ownership;
15import clarin.cmdi.componentregistry.model.ProfileDescription;
16import clarin.cmdi.componentregistry.model.RegistryUser;
17import clarin.cmdi.componentregistry.persistence.jpa.CommentsDao;
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.xml.bind.JAXBException;
24import org.junit.Before;
25import org.springframework.beans.factory.annotation.Autowired;
26import org.springframework.jdbc.core.JdbcTemplate;
27
28import static org.junit.Assert.*;
29import org.junit.Test;
30
31/**
32 *
33 * @author olhsha
34 */
35public class SanboxTest extends ComponentRegistryRestServiceTestCase {
36   
37    @Autowired
38    private ComponentRegistryFactory componentRegistryFactory;
39   
40    @Autowired
41    private JdbcTemplate jdbcTemplate;
42    @Autowired 
43    private GroupService groupService;
44    @Autowired 
45    private UserDao userDao;
46     @Autowired 
47    private CommentsDao commentsDao;
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   
58    private ComponentDescription component1;
59    private ComponentDescription component2;
60    private ProfileDescription profile1;
61    private ProfileDescription profile2;
62    private ComponentDescription component3;
63    private ProfileDescription profile3;
64    private Comment profile1Comment1;
65    private Comment profile1Comment2;
66    private Comment component1Comment3;
67    private Comment component1Comment4;
68    private Comment profile3Comment5;
69    private Comment component3Comment7;
70
71    private void fillUpPublicItems() throws Exception {
72
73        profile1 = RegistryTestHelper.addProfile(baseRegistry, "profile2", true);
74        profile2 = RegistryTestHelper.addProfile(baseRegistry, "profile1", true);
75        component1 = RegistryTestHelper.addComponent(baseRegistry,
76                "component2", true);
77        component2 = RegistryTestHelper.addComponent(baseRegistry,
78                "component1", true);
79        profile1Comment2 = RegistryTestHelper.addComment(baseRegistry, "comment2",
80                ProfileDescription.PROFILE_PREFIX + "profile1",
81                "JUnit@test.com");
82        profile1Comment1 = RegistryTestHelper.addComment(baseRegistry, "comment1",
83                ProfileDescription.PROFILE_PREFIX + "profile1",
84                "JUnit@test.com");
85        component1Comment3 = RegistryTestHelper.addComment(baseRegistry, "comment3",
86                ComponentDescription.COMPONENT_PREFIX + "component1",
87                "JUnit@test.com");
88        component1Comment4 = RegistryTestHelper.addComment(baseRegistry, "comment4",
89                ComponentDescription.COMPONENT_PREFIX + "component1",
90                "JUnit@test.com");
91    }
92
93    private void fillUpPrivateItems() throws Exception {
94        profile3 = RegistryTestHelper.addProfile(baseRegistry, "profile3", false);
95        component3 = RegistryTestHelper.addComponent(baseRegistry,
96                "component3", false);
97        profile3Comment5 = RegistryTestHelper.addComment(baseRegistry, "comment5",
98                ProfileDescription.PROFILE_PREFIX + "profile3",
99                "JUnit@test.com");
100        component3Comment7 = RegistryTestHelper.addComment(baseRegistry, "comment7",
101                ComponentDescription.COMPONENT_PREFIX + "component3",
102                "JUnit@test.com");
103    }
104   
105     protected void createAntherUserRecord() {
106        RegistryUser user = new RegistryUser();
107        user.setName("Another database test user");
108        user.setPrincipalName("anotherPrincipal");
109        userDao.save(user);
110    }
111     
112    private void MakeGroupA(){
113        groupService.createNewGroup("group A", DummyPrincipal.DUMMY_PRINCIPAL.getName());
114    }
115   
116     private void fillUpGroupA() throws ParseException, JAXBException, ItemNotFoundException{
117         
118        MakeGroupA();
119       
120        RegistryTestHelper.addProfile(baseRegistry, "profile-1", false);
121        RegistryTestHelper.addComponent(baseRegistry, "component-1", false);
122        RegistryTestHelper.addComponent(baseRegistry, "component-2", false);
123       
124        Ownership ownership = new Ownership();
125        ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"profile-1");
126        ownership.setGroupId(1);
127        ownership.setUserId(0);
128        groupService.addOwnership(ownership);
129       
130        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"component-1");
131        ownership.setGroupId(1);
132        ownership.setUserId(0);
133        groupService.addOwnership(ownership);
134       
135        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"component-2");
136        ownership.setGroupId(1);
137        ownership.setUserId(0);
138        groupService.addOwnership(ownership);
139       
140    }
141     
142     
143   
144    private void MakeGroupB() throws ItemNotFoundException{
145        createAntherUserRecord();
146        groupService.createNewGroup("group B", "anotherPrincipal");
147        groupService.makeMember(DummyPrincipal.DUMMY_PRINCIPAL.getName(), "group B");
148    }
149   
150     private void MakeGroupC() throws ItemNotFoundException{
151        groupService.createNewGroup("group C", "anotherPrincipal");
152    }
153   
154     private void fillUpGroupB() throws ParseException, JAXBException, ItemNotFoundException{
155         
156        MakeGroupB();
157       
158        RegistryTestHelper.addProfile(baseRegistry, "Bprofile-1", false);
159        RegistryTestHelper.addComponent(baseRegistry, "Bcomponent-1", false);
160        RegistryTestHelper.addComponent(baseRegistry, "Bcomponent-2", false);
161       
162        Ownership ownership = new Ownership();
163        ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"Bprofile-1");
164        ownership.setGroupId(2);
165        ownership.setUserId(0);
166        groupService.addOwnership(ownership);
167       
168        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Bcomponent-1");
169        ownership.setGroupId(2);
170        ownership.setUserId(0);
171        groupService.addOwnership(ownership);
172       
173        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Bcomponent-2");
174        ownership.setGroupId(2);
175        ownership.setUserId(0);
176        groupService.addOwnership(ownership);
177       
178    }
179     
180     private void fillUpGroupC() throws ParseException, JAXBException, ItemNotFoundException{
181         
182        MakeGroupC();
183       
184        RegistryTestHelper.addProfileAnotherPrincipal(baseRegistry, "Cprofile-1", false);
185        RegistryTestHelper.addComponentAnotherPrincipal(baseRegistry, "Ccomponent-1", false);
186        RegistryTestHelper.addComponentAnotherPrincipal(baseRegistry, "Ccomponent-2", false);
187       
188        Ownership ownership = new Ownership();
189        ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"Cprofile-1");
190        ownership.setGroupId(3);
191        ownership.setUserId(0);
192        groupService.addOwnership(ownership);
193       
194        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1");
195        ownership.setGroupId(3);
196        ownership.setUserId(0);
197        groupService.addOwnership(ownership);
198       
199        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Ccomponent-2");
200        ownership.setGroupId(3);
201        ownership.setUserId(0);
202        groupService.addOwnership(ownership);
203       
204    }
205   
206 
207   
208    @Test
209    public void testGetGroupComments() throws Exception {
210
211        System.out.println("test getGroupComments");
212
213        fillUpGroupA();
214        fillUpGroupB();       
215        fillUpGroupC();
216
217       
218        RegistryTestHelper.addComment(baseRegistry, "COMMENTc1",  ComponentDescription.COMPONENT_PREFIX + "component-1",
219                "JUnit@test.com");
220        RegistryTestHelper.addComment(baseRegistry, "COMMENTp1",  ProfileDescription.PROFILE_PREFIX + "profile-1",
221                "JUnit@test.com");
222        RegistryTestHelper.addComment(baseRegistry, "COMMENTBc1",  ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1",
223                "anotherPrincipal");
224        RegistryTestHelper.addComment(baseRegistry, "COMMENTBp1",  ProfileDescription.PROFILE_PREFIX + "Bprofile-1",
225                "anotherPrincipal");
226       (new RegistryTestHelper()).addCommentBypassAuthorisation(commentsDao, "COMMENTCc1",  ComponentDescription.COMPONENT_PREFIX + "Ccomponent-1",
227                "anotherPrincipal");
228       (new RegistryTestHelper()).addCommentBypassAuthorisation(commentsDao, "COMMENTCp1",  ProfileDescription.PROFILE_PREFIX + "Cprofile-1","anotherPrincipal");
229         
230         
231                // lists
232       
233        List<Comment> response = this.getAuthenticatedResource(getResource()
234                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "component-1" + "/comments/"))
235                .accept(MediaType.APPLICATION_XML)
236                .get(COMMENT_LIST_GENERICTYPE);       
237        assertEquals(1, response.size());
238       
239        response = this.getAuthenticatedResource(getResource()
240                .path("/registry/components/" + ProfileDescription.PROFILE_PREFIX + "profile-1" + "/comments/"))
241                .accept(MediaType.APPLICATION_XML)
242                .get(COMMENT_LIST_GENERICTYPE);       
243        assertEquals(1, response.size());
244       
245        response = this.getAuthenticatedResource(getResource()
246                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1" + "/comments/"))
247                .accept(MediaType.APPLICATION_XML)
248                .get(COMMENT_LIST_GENERICTYPE);       
249        assertEquals(1, response.size());
250       
251        response = this.getAuthenticatedResource(getResource()
252                .path("/registry/components/" + ProfileDescription.PROFILE_PREFIX + "Bprofile-1" + "/comments/"))
253                .accept(MediaType.APPLICATION_XML)
254                .get(COMMENT_LIST_GENERICTYPE);       
255        assertEquals(1, response.size());
256       
257       
258   
259        ClientResponse clientResponse = this.getAuthenticatedResource(getResource()
260                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Cprofile-1"))
261                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
262        assertEquals(403, clientResponse.getStatus());
263       
264         clientResponse = this.getAuthenticatedResource(getResource()
265                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1"))
266                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
267        assertEquals(403, clientResponse.getStatus());
268       
269        // particular comments
270       
271       Comment responseComment = this.getAuthenticatedResource(getResource()
272                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "component-1" + "/comments/1"))
273                .accept(MediaType.APPLICATION_XML)
274                .get(Comment.class);       
275        assertNotNull(responseComment);
276     
277        assertEquals(1, Long.parseLong(responseComment.getId()));
278       
279        responseComment = this.getAuthenticatedResource(getResource()
280                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX + "profile-1" + "/comments/2"))
281                .accept(MediaType.APPLICATION_XML)
282                .get(Comment.class);       
283        assertNotNull(responseComment);
284       
285        assertEquals(2, Long.parseLong(responseComment.getId()));
286       
287       responseComment = this.getAuthenticatedResource(getResource()
288                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1" + "/comments/3"))
289                .accept(MediaType.APPLICATION_XML)
290                .get(Comment.class);       
291        assertNotNull(responseComment);
292        assertEquals(3, Long.parseLong(responseComment.getId()));
293       
294        responseComment = this.getAuthenticatedResource(getResource()
295                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX + "Bprofile-1" + "/comments/4"))
296                .accept(MediaType.APPLICATION_XML)
297                .get(Comment.class);       
298        assertNotNull(responseComment);
299        assertEquals(4, Long.parseLong(responseComment.getId()));
300       
301        clientResponse = this.getAuthenticatedResource(getResource()
302                .path("/registry/profiles/" + ProfileDescription.PROFILE_PREFIX+"Cprofile-1/comments/6"))
303                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
304        assertEquals(403, clientResponse.getStatus());
305       
306         clientResponse = this.getAuthenticatedResource(getResource()
307                .path("/registry/components/" + ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1/comments/5"))
308                .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);
309        assertEquals(403, clientResponse.getStatus());
310       
311    }
312
313}
Note: See TracBrowser for help on using the repository browser.