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

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

Unit test for deleting comments from groups.

File size: 12.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.RegistrySpace;
11import clarin.cmdi.componentregistry.components.CMDComponentSpec;
12import clarin.cmdi.componentregistry.impl.database.ComponentRegistryTestDatabase;
13import clarin.cmdi.componentregistry.impl.database.GroupService;
14import clarin.cmdi.componentregistry.model.BaseDescription;
15import clarin.cmdi.componentregistry.model.Comment;
16import clarin.cmdi.componentregistry.model.ComponentDescription;
17import clarin.cmdi.componentregistry.model.Ownership;
18import clarin.cmdi.componentregistry.model.ProfileDescription;
19import clarin.cmdi.componentregistry.model.RegisterResponse;
20import clarin.cmdi.componentregistry.model.RegistryUser;
21import clarin.cmdi.componentregistry.persistence.jpa.CommentsDao;
22import clarin.cmdi.componentregistry.persistence.jpa.UserDao;
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 SanboxTest 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    private ComponentRegistry baseRegistry;
55
56    @Before
57    public void init() {
58        ComponentRegistryTestDatabase.resetAndCreateAllTables(jdbcTemplate);
59        createUserRecord();
60        baseRegistry = componentRegistryFactory.getBaseRegistry(DummyPrincipal.DUMMY_CREDENTIALS);
61    }
62
63   
64    private ComponentDescription component1;
65    private ComponentDescription component2;
66    private ProfileDescription profile1;
67    private ProfileDescription profile2;
68    private ComponentDescription component3;
69    private ProfileDescription profile3;
70    private Comment profile1Comment1;
71    private Comment profile1Comment2;
72    private Comment component1Comment3;
73    private Comment component1Comment4;
74    private Comment profile3Comment5;
75    private Comment component3Comment7;
76
77    private void fillUpPublicItems() throws Exception {
78
79        profile1 = RegistryTestHelper.addProfile(baseRegistry, "profile2", true);
80        profile2 = RegistryTestHelper.addProfile(baseRegistry, "profile1", true);
81        component1 = RegistryTestHelper.addComponent(baseRegistry,
82                "component2", true);
83        component2 = RegistryTestHelper.addComponent(baseRegistry,
84                "component1", true);
85        profile1Comment2 = RegistryTestHelper.addComment(baseRegistry, "comment2",
86                ProfileDescription.PROFILE_PREFIX + "profile1",
87                "JUnit@test.com");
88        profile1Comment1 = RegistryTestHelper.addComment(baseRegistry, "comment1",
89                ProfileDescription.PROFILE_PREFIX + "profile1",
90                "JUnit@test.com");
91        component1Comment3 = RegistryTestHelper.addComment(baseRegistry, "comment3",
92                ComponentDescription.COMPONENT_PREFIX + "component1",
93                "JUnit@test.com");
94        component1Comment4 = RegistryTestHelper.addComment(baseRegistry, "comment4",
95                ComponentDescription.COMPONENT_PREFIX + "component1",
96                "JUnit@test.com");
97    }
98
99    private void fillUpPrivateItems() throws Exception {
100        profile3 = RegistryTestHelper.addProfile(baseRegistry, "profile3", false);
101        component3 = RegistryTestHelper.addComponent(baseRegistry,
102                "component3", false);
103        profile3Comment5 = RegistryTestHelper.addComment(baseRegistry, "comment5",
104                ProfileDescription.PROFILE_PREFIX + "profile3",
105                "JUnit@test.com");
106        component3Comment7 = RegistryTestHelper.addComment(baseRegistry, "comment7",
107                ComponentDescription.COMPONENT_PREFIX + "component3",
108                "JUnit@test.com");
109    }
110   
111     protected void createAntherUserRecord() {
112        RegistryUser user = new RegistryUser();
113        user.setName("Another database test user");
114        user.setPrincipalName("anotherPrincipal");
115        userDao.save(user);
116    }
117     
118    private void MakeGroupA(){
119        groupService.createNewGroup("group A", DummyPrincipal.DUMMY_PRINCIPAL.getName());
120    }
121   
122     private void fillUpGroupA() throws ParseException, JAXBException, ItemNotFoundException{
123         
124        MakeGroupA();
125       
126        RegistryTestHelper.addProfile(baseRegistry, "profile-1", false);
127        RegistryTestHelper.addComponent(baseRegistry, "component-1", false);
128        RegistryTestHelper.addComponent(baseRegistry, "component-2", false);
129       
130        Ownership ownership = new Ownership();
131        ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"profile-1");
132        ownership.setGroupId(1);
133        ownership.setUserId(0);
134        groupService.addOwnership(ownership);
135       
136        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"component-1");
137        ownership.setGroupId(1);
138        ownership.setUserId(0);
139        groupService.addOwnership(ownership);
140       
141        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"component-2");
142        ownership.setGroupId(1);
143        ownership.setUserId(0);
144        groupService.addOwnership(ownership);
145       
146    }
147     
148     
149   
150    private void MakeGroupB() throws ItemNotFoundException{
151        createAntherUserRecord();
152        groupService.createNewGroup("group B", "anotherPrincipal");
153        groupService.makeMember(DummyPrincipal.DUMMY_PRINCIPAL.getName(), "group B");
154    }
155   
156     private void MakeGroupC() throws ItemNotFoundException{
157        groupService.createNewGroup("group C", "anotherPrincipal");
158    }
159   
160     private void fillUpGroupB() throws ParseException, JAXBException, ItemNotFoundException{
161         
162        MakeGroupB();
163       
164        RegistryTestHelper.addProfile(baseRegistry, "Bprofile-1", false);
165        RegistryTestHelper.addComponent(baseRegistry, "Bcomponent-1", false);
166        RegistryTestHelper.addComponent(baseRegistry, "Bcomponent-2", false);
167       
168        Ownership ownership = new Ownership();
169        ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"Bprofile-1");
170        ownership.setGroupId(2);
171        ownership.setUserId(0);
172        groupService.addOwnership(ownership);
173       
174        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Bcomponent-1");
175        ownership.setGroupId(2);
176        ownership.setUserId(0);
177        groupService.addOwnership(ownership);
178       
179        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Bcomponent-2");
180        ownership.setGroupId(2);
181        ownership.setUserId(0);
182        groupService.addOwnership(ownership);
183       
184    }
185     
186     private void fillUpGroupC() throws ParseException, JAXBException, ItemNotFoundException{
187         
188        MakeGroupC();
189       
190        RegistryTestHelper.addProfileAnotherPrincipal(baseRegistry, "Cprofile-1", false);
191        RegistryTestHelper.addComponentAnotherPrincipal(baseRegistry, "Ccomponent-1", false);
192        RegistryTestHelper.addComponentAnotherPrincipal(baseRegistry, "Ccomponent-2", false);
193       
194        Ownership ownership = new Ownership();
195        ownership.setComponentId(ProfileDescription.PROFILE_PREFIX+"Cprofile-1");
196        ownership.setGroupId(3);
197        ownership.setUserId(0);
198        groupService.addOwnership(ownership);
199       
200        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Ccomponent-1");
201        ownership.setGroupId(3);
202        ownership.setUserId(0);
203        groupService.addOwnership(ownership);
204       
205        ownership.setComponentId(ComponentDescription.COMPONENT_PREFIX+"Ccomponent-2");
206        ownership.setGroupId(3);
207        ownership.setUserId(0);
208        groupService.addOwnership(ownership);
209       
210    }
211   
212 
213   
214     private FormDataMultiPart createFormData(Object content) {
215        return createFormData(content, "My Test");
216    }
217
218    private FormDataMultiPart createFormData(Object content, String description) {
219        FormDataMultiPart form = new FormDataMultiPart();
220        form.field(IComponentRegistryRestService.DATA_FORM_FIELD, content,
221                MediaType.APPLICATION_OCTET_STREAM_TYPE);
222        form.field(IComponentRegistryRestService.NAME_FORM_FIELD, "Test1");
223        form.field(IComponentRegistryRestService.DESCRIPTION_FORM_FIELD,
224                description);
225        form.field(IComponentRegistryRestService.DOMAIN_FORM_FIELD, "My domain");
226        form.field(IComponentRegistryRestService.GROUP_FORM_FIELD, "TestGroup");
227        return form;
228    }
229   
230 
231   
232   
233   
234    @Test 
235    public void testDeleteCommentFromGroupComponentAndProfile() throws Exception {
236
237        System.out.println("test deleteCommentFromGroupComponent");
238
239       
240        fillUpGroupA();
241        fillUpGroupB();       
242        fillUpGroupC();
243       
244         RegistryTestHelper.addComment(baseRegistry, "COMMENTc1",  ComponentDescription.COMPONENT_PREFIX + "component-1",
245                "JUnit@test.com");
246        RegistryTestHelper.addComment(baseRegistry, "COMMENTp1",  ProfileDescription.PROFILE_PREFIX + "profile-1",
247                "JUnit@test.com");
248        RegistryTestHelper.addComment(baseRegistry, "COMMENTBc1",  ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1",
249                "anotherPrincipal");
250        RegistryTestHelper.addComment(baseRegistry, "COMMENTBp1",  ProfileDescription.PROFILE_PREFIX + "Bprofile-1",
251                "anotherPrincipal");
252       (new RegistryTestHelper()).addCommentBypassAuthorisation(commentsDao, "COMMENTCc1",  ComponentDescription.COMPONENT_PREFIX + "Ccomponent-1",
253                "anotherPrincipal");
254       (new RegistryTestHelper()).addCommentBypassAuthorisation(commentsDao, "COMMENTCp1",  ProfileDescription.PROFILE_PREFIX + "Cprofile-1","anotherPrincipal");
255         
256        ClientResponse response = getAuthenticatedResource(
257                "/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "component-1/comments/1").delete(
258                ClientResponse.class);
259        assertEquals(200, response.getStatus());
260        response = getAuthenticatedResource(
261                "/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "component-1/comments/1").get(
262                ClientResponse.class);
263        assertEquals(404, response.getStatus());
264       
265        response = getAuthenticatedResource(
266                "/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "Bcomponent-1/comments/3").delete(
267                ClientResponse.class);
268        assertEquals(403, response.getStatus());
269       
270       
271        response = getAuthenticatedResource(
272                "/registry/components/" + ComponentDescription.COMPONENT_PREFIX + "Ccomponent-1/comments/5").delete(
273                ClientResponse.class);
274        assertEquals(403, response.getStatus());
275
276       
277       response = getAuthenticatedResource(
278                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX + "profile-1/comments/2").delete(
279                ClientResponse.class);
280        assertEquals(200, response.getStatus());
281        response = getAuthenticatedResource(
282                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX + "profile-1/comments/2").get(
283                ClientResponse.class);
284        assertEquals(404, response.getStatus());
285       
286        response = getAuthenticatedResource(
287                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX + "Bprofile-1/comments/4").delete(
288                ClientResponse.class);
289        assertEquals(403, response.getStatus());
290       
291       
292        response = getAuthenticatedResource(
293                "/registry/profiles/" + ProfileDescription.PROFILE_PREFIX + "Cprofile-1/comments/6").delete(
294                ClientResponse.class);
295        assertEquals(403, response.getStatus());
296       
297    }
298
299}
Note: See TracBrowser for help on using the repository browser.