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

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

Unit test for posting comments for descriptions in groups

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