source: DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/rest/AnnotationResourceTest.java @ 5850

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

when annotation update is received from a "Writer" then it is checked if it tries to change permissions. If so, 403 is the respond

File size: 18.1 KB
RevLine 
[4603]1/*
2 * Copyright (C) 2013 DASISH
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 */
18package eu.dasish.annotation.backend.rest;
19
[5850]20import eu.dasish.annotation.backend.ForbiddenException;
[5393]21import eu.dasish.annotation.backend.Helpers;
[4834]22import eu.dasish.annotation.backend.NotInDataBaseException;
[4603]23import eu.dasish.annotation.backend.Resource;
[5088]24import eu.dasish.annotation.backend.dao.DBDispatcher;
[4603]25import eu.dasish.annotation.backend.TestInstances;
[4834]26import eu.dasish.annotation.schema.Access;
[4603]27import eu.dasish.annotation.schema.Action;
28import eu.dasish.annotation.schema.ActionList;
29import eu.dasish.annotation.schema.Annotation;
30import eu.dasish.annotation.schema.ObjectFactory;
31import eu.dasish.annotation.schema.ResponseBody;
32import eu.dasish.annotation.schema.AnnotationActionName;
[4887]33import eu.dasish.annotation.schema.AnnotationBody;
34import eu.dasish.annotation.schema.AnnotationBody.TextBody;
[4603]35import java.io.IOException;
36import javax.xml.bind.JAXBElement;
37import org.jmock.Expectations;
38import org.jmock.Mockery;
39import org.junit.Test;
40import static org.junit.Assert.*;
41import org.junit.runner.RunWith;
42import org.springframework.beans.factory.annotation.Autowired;
43import org.springframework.test.context.ContextConfiguration;
44import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
45import java.util.UUID;
46import org.springframework.mock.web.MockHttpServletRequest;
47
48/**
49 *
50 * @author olhsha
51 */
52@RunWith(value = SpringJUnit4ClassRunner.class)
[5088]53@ContextConfiguration(locations = {"/spring-test-config/mockeryRest.xml", "/spring-test-config/mockDBDispatcher.xml",
[5145]54    "/spring-config/jaxbMarshallerFactory.xml", "/spring-config/jaxbUnmarshallerFactory.xml"})
[4603]55public class AnnotationResourceTest {
56
57    @Autowired
58    private Mockery mockeryRest;
59    @Autowired
[5385]60    private DBDispatcher mockDbDispatcher;   
[4603]61    @Autowired
62    private AnnotationResource annotationResource;
63    private MockHttpServletRequest mockRequest;
64
65    public AnnotationResourceTest() {
66        mockRequest = new MockHttpServletRequest();
67    }
[4887]68
[4800]69//    public Number getPrincipalID() throws IOException {
[5385]70//        dbIntegrityService.setServiceURI(this.getRelativeServiceURI());
[4671]71//        verboseOutput = new VerboseOutput(httpServletResponse, loggerServer);
[4800]72//        String remotePrincipal = httpServletRequest.getRemotePrincipal();
73//        if (remotePrincipal != null) {
74//            if (!remotePrincipal.equals(anonym)) {
75//                final Number principalID = dbIntegrityService.getPrincipalInternalIDFromRemoteID(remotePrincipal);
76//                if (principalID != null) {
77//                    return principalID;
[4671]78//                }
[4800]79//                verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remotePrincipal, dbIntegrityService.getDataBaseAdmin().getDisplayName(), dbIntegrityService.getDataBaseAdmin().getEMail());
[4671]80//                return null;
81//            }
82//        }
83//
84//        verboseOutput.NOT_LOGGED_IN(dbIntegrityService.getDataBaseAdmin().getDisplayName(), dbIntegrityService.getDataBaseAdmin().getEMail());
85//        return null;
86//
87//    }
[4603]88    @Test
[4887]89    public void testGetAnnotation() throws NotInDataBaseException, IOException {
[4603]90        System.out.println("getAnnotation");
91        final String externalIDstring = "00000000-0000-0000-0000-000000000021";
[5385]92        final Annotation expectedAnnotation = (new TestInstances("/api")).getAnnotationOne();
[4603]93        annotationResource.setHttpServletRequest(mockRequest);
[4670]94        mockRequest.setRemoteUser("olhsha@mpi.nl");
[5385]95        mockRequest.setContextPath("/backend");
96        mockRequest.setServletPath("/api");
[4603]97        mockeryRest.checking(new Expectations() {
[4887]98            {
[5385]99               
100                oneOf(mockDbDispatcher).setResourcesPaths("/backend/api");
[4887]101
[5088]102                oneOf(mockDbDispatcher).getPrincipalInternalIDFromRemoteID("olhsha@mpi.nl");
[4603]103                will(returnValue(3));
104
[5088]105                oneOf(mockDbDispatcher).getResourceInternalIdentifier(with(aNonNull(UUID.class)), with(aNonNull((Resource.class))));
[4603]106                will(returnValue(1));
107
[4887]108
[5836]109                oneOf(mockDbDispatcher).canDo(Access.READ, 3, 1, Resource.ANNOTATION);
[4603]110                will(returnValue(true));
111
[5088]112                oneOf(mockDbDispatcher).getAnnotation(1);
[4603]113                will(returnValue(expectedAnnotation));
114            }
115        });
116
117
118        JAXBElement<Annotation> result = annotationResource.getAnnotation(externalIDstring);
119        assertTrue(expectedAnnotation.equals(result.getValue()));
120    }
121
122    /**
123     * Test of deleteAnnotation method, of class AnnotationResource.
124     */
125    @Test
[4834]126    public void testDeleteAnnotation() throws NotInDataBaseException, IOException {
[4603]127        System.out.println("deleteAnnotation");
128
129        final int[] mockDelete = new int[4];
130        mockDelete[0] = 1; // # deleted annotations
[4800]131        mockDelete[3] = 1; // # deleted annotation_prinipal_accesss
[4603]132        mockDelete[2] = 2; // # deleted  annotations_target_Targets, (4,3), (4,4)
133        mockDelete[3] = 1; // # deletd Targets, 4
134
135        annotationResource.setHttpServletRequest(mockRequest);
[4670]136        mockRequest.setRemoteUser("olhsha@mpi.nl");
[5385]137        mockRequest.setContextPath("/backend");
138        mockRequest.setServletPath("/api");
139       
[4603]140        mockeryRest.checking(new Expectations() {
141            {
[5385]142                oneOf(mockDbDispatcher).setResourcesPaths("/backend/api");
[4887]143
[5088]144                oneOf(mockDbDispatcher).getPrincipalInternalIDFromRemoteID("olhsha@mpi.nl");
[4603]145                will(returnValue(3));
146
[5088]147                oneOf(mockDbDispatcher).getResourceInternalIdentifier(with(aNonNull(UUID.class)), with(aNonNull((Resource.class))));
[4603]148                will(returnValue(4));
149
150
[5088]151                oneOf(mockDbDispatcher).getAnnotationOwnerID(4);
[4603]152                will(returnValue(3));
[5086]153               
[5836]154                oneOf(mockDbDispatcher).canDo(Access.ALL, 3, 4, Resource.ANNOTATION);
[5086]155                will(returnValue(true));
[4603]156
[5088]157                oneOf(mockDbDispatcher).deleteAnnotation(4);
[4603]158                will(returnValue(mockDelete));
159            }
160        });
161
162
163        String result = annotationResource.deleteAnnotation("00000000-0000-0000-0000-000000000024");
[5086]164        assertEquals("1 annotation(s) is(are) deleted.", result);
[4603]165    }
166
167    /**
168     * Test of createAnnotation method, of class AnnotationResource.
169     */
170    @Test
[4903]171    public void testCreateAnnotation() throws IOException, NotInDataBaseException {
[4603]172        System.out.println("test createAnnotation");
173
[5385]174        final Annotation annotationToAdd = (new TestInstances("/api")).getAnnotationToAdd();
[4887]175        final Number newAnnotationID = 5;
[4603]176
177
178        final Annotation addedAnnotation = (new ObjectFactory()).createAnnotation(annotationToAdd).getValue();
[5393]179        String externalId = Helpers.generateUUID().toString();
[5385]180        addedAnnotation.setId(externalId);
181        addedAnnotation.setHref("/backend/api/annotations/" + externalId);
182        addedAnnotation.setOwnerHref("/backend/api/principals/00000000-0000-0000-0000-000000000113");
[4603]183
184        final ResponseBody mockEnvelope = new ResponseBody();
185        final Action action = new Action();
186        final ActionList actionList = new ActionList();
187        mockEnvelope.setAnnotation(addedAnnotation);
188        mockEnvelope.setActionList(actionList);
189        actionList.getAction().add(action);
190        action.setMessage(AnnotationActionName.CREATE_CACHED_REPRESENTATION.value());
[5385]191        action.setObject("/backend/api/targets/00000000-0000-0000-0000-000000000036");
[4603]192
193        annotationResource.setHttpServletRequest(mockRequest);
[4670]194        mockRequest.setRemoteUser("olhsha@mpi.nl");
[5385]195         mockRequest.setContextPath("/backend");
196        mockRequest.setServletPath("/api");
[4603]197        mockeryRest.checking(new Expectations() {
198            {
[5385]199               
200                oneOf(mockDbDispatcher).setResourcesPaths("/backend/api");
[4887]201
[5088]202                oneOf(mockDbDispatcher).getPrincipalInternalIDFromRemoteID("olhsha@mpi.nl");
[4603]203                will(returnValue(3));
204
[5088]205                oneOf(mockDbDispatcher).addPrincipalsAnnotation(3, annotationToAdd);
[4603]206                will(returnValue(newAnnotationID));
207
[5088]208                oneOf(mockDbDispatcher).getAnnotation(newAnnotationID);
[4603]209                will(returnValue(addedAnnotation));
[4887]210               
[5088]211                oneOf(mockDbDispatcher).makeAnnotationResponseEnvelope(newAnnotationID);
[4603]212                will(returnValue(mockEnvelope));
213
214            }
215        });
216
217
218
219        JAXBElement<ResponseBody> result = annotationResource.createAnnotation(annotationToAdd);
220        Annotation newAnnotation = result.getValue().getAnnotation();
221        String actionName = result.getValue().getActionList().getAction().get(0).getMessage();
[5385]222        assertEquals(addedAnnotation.getOwnerHref(), newAnnotation.getOwnerHref());
223        assertEquals(addedAnnotation.getId(), newAnnotation.getId());
224        assertEquals(addedAnnotation.getHref(), newAnnotation.getHref());
[4603]225        assertEquals(addedAnnotation.getHeadline(), newAnnotation.getHeadline());
226        assertEquals(addedAnnotation.getTargets(), newAnnotation.getTargets());
227        assertEquals(addedAnnotation.getLastModified(), newAnnotation.getLastModified());
228        assertEquals(addedAnnotation.getBody(), newAnnotation.getBody());
229        assertEquals(AnnotationActionName.CREATE_CACHED_REPRESENTATION.value(), actionName);
[4887]230        assertEquals(Access.WRITE, addedAnnotation.getPermissions().getPublic());
[4603]231    }
[4887]232
233    @Test
[5850]234    public void testUpdateAnnotation() throws NotInDataBaseException, IOException, ForbiddenException{
[4887]235        System.out.println("test updateAnnotation");
236
[5385]237        final Annotation annotation = (new TestInstances("/backend/api")).getAnnotationOne();
[4887]238        annotation.getPermissions().setPublic(Access.READ);
239        annotation.setHeadline("updated annotation 1");
240        annotation.getPermissions().getPermission().get(1).setLevel(Access.WRITE);
241        AnnotationBody ab = new AnnotationBody();
242        TextBody tb = new TextBody();
243        ab.setTextBody(tb);
244        tb.setMimeType("text/plain");
245        tb.setBody("some text body l");
246        annotation.setBody(ab);
247
248        final ResponseBody mockEnvelope = new ResponseBody();
249        final ActionList actionList = new ActionList();
250        mockEnvelope.setAnnotation(annotation);
251        mockEnvelope.setActionList(actionList);
252
253        annotationResource.setHttpServletRequest(mockRequest);
254        mockRequest.setRemoteUser("twagoo@mpi.nl");
[5385]255        mockRequest.setContextPath("/backend");
256        mockRequest.setServletPath("/api");
[4887]257        final UUID externalId = UUID.fromString("00000000-0000-0000-0000-000000000021");
258
259        //  Number annotationID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.ANNOTATION);
260        //  if (principalID.equals(dbIntegrityService.getAnnotationOwnerID(annotationID)) || dbIntegrityService.getTypeOfPrincipalAccount(principalID).equals(admin)) {
261        //  int updatedRows = dbIntegrityService.updateAnnotation(annotation);
262        //  return new ObjectFactory().createResponseBody(dbIntegrityService.makeAnnotationResponseEnvelope(annotationID));
263
264
265        mockeryRest.checking(new Expectations() {
266            {
[5385]267                oneOf(mockDbDispatcher).setResourcesPaths("/backend/api");
[4887]268
[5088]269                oneOf(mockDbDispatcher).getPrincipalInternalIDFromRemoteID("twagoo@mpi.nl");
[5385]270                will(returnValue(1));               
271               
[5088]272                oneOf(mockDbDispatcher).getResourceInternalIdentifier(externalId, Resource.ANNOTATION);
[4887]273                will(returnValue(1));
274               
[5088]275                oneOf(mockDbDispatcher).getAnnotationOwnerID(1);
[4887]276                will(returnValue(1));
277               
[5836]278                oneOf(mockDbDispatcher).canDo(Access.WRITE, 1, 1, Resource.ANNOTATION);
[5086]279                will(returnValue(true));
280               
[5774]281                oneOf(mockDbDispatcher).updateAnnotation(annotation, "twagoo@mpi.nl");
[4887]282                will(returnValue(1));
283               
[5088]284                oneOf(mockDbDispatcher).makeAnnotationResponseEnvelope(1);
[4887]285                will(returnValue(mockEnvelope));
286
287            }
288        });
289
290
291
292        JAXBElement<ResponseBody> result = annotationResource.updateAnnotation("00000000-0000-0000-0000-000000000021", annotation);
293        Annotation newAnnotation = result.getValue().getAnnotation();
[5385]294        assertEquals(annotation.getOwnerHref(), newAnnotation.getOwnerHref());
295        assertEquals(annotation.getId(), newAnnotation.getId());
296        assertEquals(annotation.getHref(), newAnnotation.getHref());
[4887]297        assertEquals("updated annotation 1", newAnnotation.getHeadline());
298        assertEquals("text/plain", newAnnotation.getBody().getTextBody().getMimeType());
299        assertEquals("some text body l", newAnnotation.getBody().getTextBody().getBody());
300        assertEquals(Access.WRITE, annotation.getPermissions().getPermission().get(1).getLevel());
301        assertEquals(Access.READ, annotation.getPermissions().getPublic());
302    }
303   
304    @Test
305    public void testUpdateAnnotationBody() throws NotInDataBaseException, IOException{
306        System.out.println("test updateAnnotationBody");
307
[5385]308        Annotation annotation = (new TestInstances("/backend/api")).getAnnotationOne();
[4887]309       
310        final AnnotationBody ab = new AnnotationBody();
311        TextBody tb = new TextBody();
312        ab.setTextBody(tb);
313        tb.setMimeType("text/plain");
314        tb.setBody("some text body l");
315        annotation.setBody(ab);
316
317        final ResponseBody mockEnvelope = new ResponseBody();
318        final ActionList actionList = new ActionList();
319        mockEnvelope.setAnnotation(annotation);
320        mockEnvelope.setActionList(actionList);
321
322        annotationResource.setHttpServletRequest(mockRequest);
323        mockRequest.setRemoteUser("twagoo@mpi.nl");
[5385]324        mockRequest.setContextPath("/backend");
325        mockRequest.setServletPath("/api");
[4887]326        final UUID externalId = UUID.fromString("00000000-0000-0000-0000-000000000021");
327
328     
329       //final Number annotationID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.ANNOTATION);
330       // (dbIntegrityService.canDo(Access.WRITE, principalID, annotationID)) {
331        // int updatedRows = dbIntegrityService.updateAnnotationBody(annotationID, annotationBody);
332       //       return new ObjectFactory().createResponseBody(dbIntegrityService.makeAnnotationResponseEnvelope(annotationID));
333       
334        mockeryRest.checking(new Expectations() {
335            {
[5385]336               
337                oneOf(mockDbDispatcher).setResourcesPaths("/backend/api");
[4887]338
[5088]339                oneOf(mockDbDispatcher).getPrincipalInternalIDFromRemoteID("twagoo@mpi.nl");
[4887]340                will(returnValue(1));
341               
342               
[5088]343                oneOf(mockDbDispatcher).getResourceInternalIdentifier(externalId, Resource.ANNOTATION);
[4887]344                will(returnValue(1));
345               
[5836]346                oneOf(mockDbDispatcher).canDo(Access.WRITE, 1, 1, Resource.ANNOTATION);
[5086]347                will(returnValue(true));               
[4887]348               
[5086]349               
[5088]350                oneOf(mockDbDispatcher).updateAnnotationBody(1, ab);
[4887]351                will(returnValue(1));
352               
[5088]353                oneOf(mockDbDispatcher).makeAnnotationResponseEnvelope(1);
[4887]354                will(returnValue(mockEnvelope));
355
356            }
357        });
358
359
360
361        JAXBElement<ResponseBody> result = annotationResource.updateAnnotationBody("00000000-0000-0000-0000-000000000021", ab);
362        Annotation newAnnotation = result.getValue().getAnnotation();
363        assertEquals("text/plain", newAnnotation.getBody().getTextBody().getMimeType());
364        assertEquals("some text body l", newAnnotation.getBody().getTextBody().getBody());
365    }
[5086]366   
367    @Test
368    public void testUpdateAnnotationHeadline() throws NotInDataBaseException, IOException{
369        System.out.println("test updateAnnotationHeadline");
370
[5385]371        Annotation annotation = (new TestInstances("/api")).getAnnotationOne();
[5086]372       
373        final String newHeadline = "new Headline";       
374        annotation.setHeadline(newHeadline);
375
376        final ResponseBody mockEnvelope = new ResponseBody();
377        final ActionList actionList = new ActionList();
378        mockEnvelope.setAnnotation(annotation);
379        mockEnvelope.setActionList(actionList);
380
381        annotationResource.setHttpServletRequest(mockRequest);
382        mockRequest.setRemoteUser("twagoo@mpi.nl");
[5385]383        mockRequest.setContextPath("/backend");
384        mockRequest.setServletPath("/api");
[5086]385        final UUID externalId = UUID.fromString("00000000-0000-0000-0000-000000000021");
386
387     
388        mockeryRest.checking(new Expectations() {
389            {
[5385]390                 oneOf(mockDbDispatcher).setResourcesPaths("/backend/api");
[5086]391
[5088]392                oneOf(mockDbDispatcher).getPrincipalInternalIDFromRemoteID("twagoo@mpi.nl");
[5086]393                will(returnValue(1));
394               
395               
[5088]396                oneOf(mockDbDispatcher).getResourceInternalIdentifier(externalId, Resource.ANNOTATION);
[5086]397                will(returnValue(1));
398               
[5836]399                oneOf(mockDbDispatcher).canDo(Access.WRITE, 1, 1, Resource.ANNOTATION);
[5086]400                will(returnValue(true));               
401               
402               
[5088]403                oneOf(mockDbDispatcher).updateAnnotationHeadline(1, newHeadline);
[5086]404                will(returnValue(1));
405               
[5088]406                oneOf(mockDbDispatcher).makeAnnotationResponseEnvelope(1);
[5086]407                will(returnValue(mockEnvelope));
408
409            }
410        });
411
412
413        JAXBElement<ResponseBody> result = annotationResource.updateAnnotationHeadline("00000000-0000-0000-0000-000000000021", newHeadline);
414        Annotation newAnnotation = result.getValue().getAnnotation();
415        assertEquals("new Headline", newAnnotation.getHeadline());
416    }
[5685]417   
418     
419           
[4603]420}
Note: See TracBrowser for help on using the repository browser.