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

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

refactoring. Adding ALL as an access mode.

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