source: DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/DBDispatcherTest.java @ 5838

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

units tests updating and adding (all-access)

File size: 91.3 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.dao.impl;
19
20import eu.dasish.annotation.backend.Helpers;
21import eu.dasish.annotation.backend.MatchMode;
22import eu.dasish.annotation.backend.NotInDataBaseException;
23import eu.dasish.annotation.backend.PrincipalCannotBeDeleted;
24import eu.dasish.annotation.backend.PrincipalExists;
25import eu.dasish.annotation.backend.Resource;
26import eu.dasish.annotation.backend.TestInstances;
27import eu.dasish.annotation.backend.dao.AnnotationDao;
28import eu.dasish.annotation.backend.dao.CachedRepresentationDao;
29import eu.dasish.annotation.backend.dao.NotebookDao;
30import eu.dasish.annotation.backend.dao.TargetDao;
31import eu.dasish.annotation.backend.dao.PrincipalDao;
32import eu.dasish.annotation.schema.Annotation;
33import eu.dasish.annotation.schema.AnnotationBody;
34import eu.dasish.annotation.schema.AnnotationBody.TextBody;
35import eu.dasish.annotation.schema.AnnotationInfo;
36import eu.dasish.annotation.schema.AnnotationInfoList;
37import eu.dasish.annotation.schema.CachedRepresentationInfo;
38import eu.dasish.annotation.schema.Notebook;
39import eu.dasish.annotation.schema.NotebookInfo;
40import eu.dasish.annotation.schema.NotebookInfoList;
41import eu.dasish.annotation.schema.Access;
42import eu.dasish.annotation.schema.ReferenceList;
43import eu.dasish.annotation.schema.Target;
44import eu.dasish.annotation.schema.TargetInfo;
45import eu.dasish.annotation.schema.Principal;
46import eu.dasish.annotation.schema.Permission;
47import eu.dasish.annotation.schema.PermissionList;
48import java.io.ByteArrayInputStream;
49import java.io.IOException;
50import java.sql.SQLException;
51import java.sql.Timestamp;
52import java.util.ArrayList;
53import java.util.HashMap;
54import java.util.List;
55import java.util.Map;
56import java.util.UUID;
57import javax.sql.rowset.serial.SerialException;
58import javax.xml.datatype.DatatypeConfigurationException;
59import javax.xml.datatype.DatatypeFactory;
60import javax.xml.datatype.XMLGregorianCalendar;
61import org.jmock.Expectations;
62import org.jmock.Mockery;
63import org.junit.Test;
64import static org.junit.Assert.*;
65import org.junit.Ignore;
66import org.junit.runner.RunWith;
67import org.springframework.beans.factory.annotation.Autowired;
68import org.springframework.test.context.ContextConfiguration;
69import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
70
71/**
72 *
73 * @author olhsha
74 */
75@RunWith(SpringJUnit4ClassRunner.class)
76@ContextConfiguration({"/spring-test-config/dataSource.xml", "/spring-test-config/mockeryDao.xml", "/spring-test-config/mockAnnotationDao.xml",
77    "/spring-test-config/mockPrincipalDao.xml", "/spring-test-config/mockTargetDao.xml", "/spring-test-config/mockCachedRepresentationDao.xml",
78    "/spring-test-config/mockNotebookDao.xml",
79    "/spring-config/dbDispatcher.xml"})
80public class DBDispatcherTest {
81
82    @Autowired
83    private DBDispatcherImlp dbDispatcher;
84    @Autowired
85    private Mockery mockeryDao;
86    @Autowired
87    private PrincipalDao principalDao;
88    @Autowired
89    private CachedRepresentationDao cachedRepresentationDao;
90    @Autowired
91    private TargetDao targetDao;
92    @Autowired
93    private AnnotationDao annotationDao;
94    @Autowired
95    private NotebookDao notebookDao;
96    TestInstances testInstances = new TestInstances("/api");
97
98    public DBDispatcherTest() {       
99    }
100   
101   
102
103    ///////// GETTERS /////////////
104    /**
105     * Test of getAnnotationInternalIdentifier method, of class
106     * DBIntegrityServiceImlp.
107     */
108   
109    @Test
110    public void testGetAnnotationInternalIdentifier() throws NotInDataBaseException {
111        System.out.println("getAnnotationInternalIdentifier");
112       
113        final UUID externalID = UUID.fromString("00000000-0000-0000-0000-000000000021");
114        mockeryDao.checking(new Expectations() {
115            {
116                oneOf(annotationDao).getInternalID(externalID);
117                will(returnValue(1));
118            }
119        });
120        assertEquals(1, dbDispatcher.getResourceInternalIdentifier(externalID, Resource.ANNOTATION));
121    }
122
123    /**
124     * Test of getAnnotationExternalIdentifier method, of class
125     * DBIntegrityServiceImlp.
126     */
127    @Test
128    public void testGetAnnotationExternalIdentifier() {
129        System.out.println("getAnnotationExternalIdentifier");
130        final UUID externalID = UUID.fromString("00000000-0000-0000-0000-000000000021");
131       
132        mockeryDao.checking(new Expectations() {
133            {
134                oneOf(annotationDao).getExternalID(1);
135                will(returnValue(externalID));
136            }
137        });
138        assertEquals("00000000-0000-0000-0000-000000000021", dbDispatcher.getResourceExternalIdentifier(1, Resource.ANNOTATION).toString());
139    }
140
141    /**
142     * Test of getPrincipalInternalIdentifier method, of class
143     * DBIntegrityServiceImlp.
144     */
145    @Test
146    public void testGetPrincipalInternalIdentifier() throws NotInDataBaseException {
147        System.out.println("getPrincipalInternalIdentifier");
148       
149        final UUID externalID = UUID.fromString("00000000-0000-0000-0000-000000000111");
150
151        mockeryDao.checking(new Expectations() {
152            {
153                oneOf(principalDao).getInternalID(externalID);
154                will(returnValue(1));
155            }
156        });
157        assertEquals(1, dbDispatcher.getResourceInternalIdentifier(externalID, Resource.PRINCIPAL));
158    }
159
160    /**
161     * Test of getPrincipalExternalIdentifier method, of class
162     * DBIntegrityServiceImlp.
163     */
164    @Test
165    public void testGetPrincipalExternalIdentifier() {
166        System.out.println("getPrincipalExternalIdentifier");
167        final UUID externalID = UUID.fromString("00000000-0000-0000-0000-000000000111");
168       
169        mockeryDao.checking(new Expectations() {
170            {
171                oneOf(principalDao).getExternalID(1);
172                will(returnValue(externalID));
173            }
174        });
175        assertEquals("00000000-0000-0000-0000-000000000111", dbDispatcher.getResourceExternalIdentifier(1, Resource.PRINCIPAL).toString());
176    }
177
178    /**
179     * Test of getAnnotation method, of class DBIntegrityServiceImlp.
180     */
181    @Test
182    public void testGetAnnotation() throws Exception {
183        System.out.println("test getAnnotation");
184       
185        final Annotation mockAnnotation = new Annotation();// corresponds to the annotation # 1
186        mockAnnotation.setHref("/api/annotations/00000000-0000-0000-0000-000000000021");
187        mockAnnotation.setId("00000000-0000-0000-0000-000000000021");
188        mockAnnotation.setHeadline("Sagrada Famiglia");
189        XMLGregorianCalendar mockTimeStamp = DatatypeFactory.newInstance().newXMLGregorianCalendar("2013-08-12T09:25:00.383000Z");
190        mockAnnotation.setLastModified(mockTimeStamp);
191        mockAnnotation.setOwnerHref("/api/principals/00000000-0000-0000-0000-000000000111");
192
193        AnnotationBody mockBody = new AnnotationBody();
194        TextBody textBody = new AnnotationBody.TextBody();
195        mockBody.setTextBody(textBody);
196        textBody.setMimeType("text/plain");
197        textBody.setBody("<html><body>some html 1</body></html>");
198        mockAnnotation.setBody(mockBody);
199        PermissionList permissions = new PermissionList();
200        permissions.setPublic(Access.WRITE);
201        mockAnnotation.setPermissions(permissions);
202        mockAnnotation.setTargets(null);
203
204
205        final List<Number> mockTargetIDs = new ArrayList<Number>();
206        mockTargetIDs.add(1);
207        mockTargetIDs.add(2);
208
209        final Target mockTargetOne = new Target();
210        mockTargetOne.setLink("http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia");
211        mockTargetOne.setId("00000000-0000-0000-0000-000000000031");
212        mockTargetOne.setHref("/api/targets/00000000-0000-0000-0000-000000000031");
213        mockTargetOne.setVersion("version 1.0");
214
215        final Target mockTargetTwo = new Target();
216        mockTargetTwo.setLink("http://nl.wikipedia.org/wiki/Antoni_Gaud%C3%AD");
217        mockTargetTwo.setId("00000000-0000-0000-0000-000000000032");
218        mockTargetTwo.setHref("/api/targets/00000000-0000-0000-0000-000000000032");
219        mockTargetTwo.setVersion("version 1.1");
220
221        final List<Map<Number, String>> listMap = new ArrayList<Map<Number, String>>();
222        Map<Number, String> map2 = new HashMap<Number, String>();
223        map2.put(2, "write");
224        listMap.add(map2);
225        Map<Number, String> map3 = new HashMap<Number, String>();
226        map3.put(3, "read");
227        listMap.add(map3);
228        Map<Number, String> map4 = new HashMap<Number, String>();
229        map4.put(11, "read");
230        listMap.add(map4);
231
232        final String uri1 = "/api/principals/00000000-0000-0000-0000-000000000111";
233        final String uri2 = "/api/principals/00000000-0000-0000-0000-000000000112";
234        final String uri3 = "/api/principals/00000000-0000-0000-0000-000000000113";
235        final String uri4 = "/api/principals/00000000-0000-0000-0000-000000000221";
236
237
238        mockeryDao.checking(new Expectations() {
239            {
240                oneOf(annotationDao).getAnnotationWithoutTargetsAndPemissionList(1);
241                will(returnValue(mockAnnotation));
242
243                oneOf(annotationDao).getOwner(1);
244                will(returnValue(1));
245
246                oneOf(principalDao).getHrefFromInternalID(1);
247                will(returnValue(uri1));
248
249                oneOf(targetDao).getTargetIDs(1);
250                will(returnValue(mockTargetIDs));
251
252                oneOf(targetDao).getTarget(1);
253                will(returnValue(mockTargetOne));
254
255                oneOf(targetDao).getTarget(2);
256                will(returnValue(mockTargetTwo));
257
258                /// getPermissionsForAnnotation
259
260                oneOf(annotationDao).getPermissions(1);
261                will(returnValue(listMap));
262
263                oneOf(principalDao).getHrefFromInternalID(2);
264                will(returnValue(uri2));
265
266                oneOf(principalDao).getHrefFromInternalID(3);
267                will(returnValue(uri3));
268
269                oneOf(principalDao).getHrefFromInternalID(11);
270                will(returnValue(uri4));
271            }
272        });
273
274        Annotation result = dbDispatcher.getAnnotation(1);
275        assertEquals("00000000-0000-0000-0000-000000000021", result.getId());
276        assertEquals("/api/annotations/00000000-0000-0000-0000-000000000021", result.getHref());
277        assertEquals("text/plain", result.getBody().getTextBody().getMimeType());
278        assertEquals("<html><body>some html 1</body></html>", result.getBody().getTextBody().getBody());
279        assertEquals("Sagrada Famiglia", result.getHeadline());
280        assertEquals("2013-08-12T09:25:00.383000Z", result.getLastModified().toString());
281        assertEquals("/api/principals/00000000-0000-0000-0000-000000000111", result.getOwnerHref());
282
283        assertEquals(mockTargetOne.getLink(), result.getTargets().getTargetInfo().get(0).getLink());
284        assertEquals(mockTargetOne.getHref(), result.getTargets().getTargetInfo().get(0).getHref());
285        assertEquals(mockTargetOne.getVersion(), result.getTargets().getTargetInfo().get(0).getVersion());
286        assertEquals(mockTargetTwo.getLink(), result.getTargets().getTargetInfo().get(1).getLink());
287        assertEquals(mockTargetTwo.getHref(), result.getTargets().getTargetInfo().get(1).getHref());
288        assertEquals(mockTargetTwo.getVersion(), result.getTargets().getTargetInfo().get(1).getVersion());
289
290        assertEquals(3, result.getPermissions().getPermission().size());
291
292        assertEquals(Access.WRITE, result.getPermissions().getPermission().get(0).getLevel());
293        assertEquals(uri2, result.getPermissions().getPermission().get(0).getPrincipalHref());
294
295        assertEquals(Access.READ, result.getPermissions().getPermission().get(1).getLevel());
296        assertEquals(uri3, result.getPermissions().getPermission().get(1).getPrincipalHref());
297
298        assertEquals(Access.READ, result.getPermissions().getPermission().get(2).getLevel());
299        assertEquals(uri4, result.getPermissions().getPermission().get(2).getPrincipalHref());
300
301        assertEquals(Access.WRITE, result.getPermissions().getPublic());
302    }
303
304    /**
305     * Test of getFilteredAnnotationIDs method, of class DBIntegrityServiceImlp.
306     */
307    @Test
308    public void testGetFilteredAnnotationIDsContains() throws NotInDataBaseException {
309        System.out.println("test getFilteredAnnotationIDs");
310       
311        final List<Number> mockAnnotationIDs1 = new ArrayList<Number>();
312        mockAnnotationIDs1.add(1);
313     
314        final List<Number> mockAnnotationIDsOwned = new ArrayList<Number>();
315        //mockAnnotationIDsOwned.add(3);
316
317        final List<Number> mockAnnotationIDsRead = new ArrayList<Number>();
318        mockAnnotationIDsRead.add(1);
319        mockAnnotationIDsRead.add(2);
320       
321
322        final List<Number> mockAnnotationIDsPublicRead = new ArrayList<Number>();
323        mockAnnotationIDsPublicRead.add(1);
324        mockAnnotationIDsPublicRead.add(2);
325       
326        final List<Number> mockTargets1 = new ArrayList<Number>();
327        mockTargets1.add(1);
328        mockTargets1.add(2);
329       
330       
331        final String link1 = "http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia";
332        final String link2 = "http://nl.wikipedia.org/wiki/Antoni_Gaud%C3%AD";
333
334        final String after = (new Timestamp(0)).toString();
335        final String before = (new Timestamp(System.currentTimeMillis())).toString();
336
337        final Number loggedIn = 3;
338
339        mockeryDao.checking(new Expectations() {
340            {
341               
342
343                oneOf(annotationDao).getFilteredAnnotationIDs(null, "some html 1", null, after, before);
344                will(returnValue(mockAnnotationIDs1));
345
346                oneOf(annotationDao).getAnnotationIDsPermissionAtLeast(loggedIn, Access.READ);
347                will(returnValue(mockAnnotationIDsRead));
348
349                oneOf(annotationDao).getAnnotationIDsPublicAtLeast(Access.READ);
350                will(returnValue(mockAnnotationIDsPublicRead));               
351
352                oneOf(annotationDao).getFilteredAnnotationIDs(loggedIn, "some html 1", null, after, before);
353                will(returnValue(mockAnnotationIDsOwned));
354               
355                oneOf(targetDao).getTargetIDs(1);
356                will(returnValue(mockTargets1));
357               
358                oneOf(targetDao).getLink(1);
359                will(returnValue(link1));
360               
361                oneOf(targetDao).getLink(2);
362                will(returnValue(link2));
363             
364
365            }
366        });
367
368
369        List resultContains = dbDispatcher.getFilteredAnnotationIDs(null, "Sagrada_", MatchMode.CONTAINS, "some html 1", 3, "read", null, after, before);
370        assertEquals(1, resultContains.size());
371        assertEquals(1, resultContains.get(0));
372       
373       
374       
375    }
376   
377   
378    /**
379     * Test of getFilteredAnnotationIDs method, of class DBIntegrityServiceImlp.
380     */
381    @Test
382    public void testGetFilteredAnnotationIDsExact() throws NotInDataBaseException {
383        System.out.println("test getFilteredAnnotationIDs");
384       
385        final List<Number> mockAnnotationIDs1 = new ArrayList<Number>();
386        mockAnnotationIDs1.add(1);
387     
388        final List<Number> mockAnnotationIDsOwned = new ArrayList<Number>();
389        //mockAnnotationIDsOwned.add(3);
390
391        final List<Number> mockAnnotationIDsRead = new ArrayList<Number>();
392        mockAnnotationIDsRead.add(1);
393        mockAnnotationIDsRead.add(2);
394       
395
396        final List<Number> mockAnnotationIDsPublicRead = new ArrayList<Number>();
397        mockAnnotationIDsPublicRead.add(1);
398        mockAnnotationIDsPublicRead.add(2);
399       
400        final List<Number> mockTargets1 = new ArrayList<Number>();
401        mockTargets1.add(1);
402        mockTargets1.add(2);
403       
404       
405        final String link1 = "http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia";
406        final String link2 = "http://nl.wikipedia.org/wiki/Antoni_Gaud%C3%AD";
407
408        final String after = (new Timestamp(0)).toString();
409        final String before = (new Timestamp(System.currentTimeMillis())).toString();
410
411        final Number loggedIn = 3;
412
413        mockeryDao.checking(new Expectations() {
414            {
415               
416
417                oneOf(annotationDao).getFilteredAnnotationIDs(null, "some html 1", null, after, before);
418                will(returnValue(mockAnnotationIDs1));
419
420                oneOf(annotationDao).getAnnotationIDsPermissionAtLeast(loggedIn, Access.READ);
421                will(returnValue(mockAnnotationIDsRead));
422
423                oneOf(annotationDao).getAnnotationIDsPublicAtLeast(Access.READ);
424                will(returnValue(mockAnnotationIDsPublicRead));               
425
426                oneOf(annotationDao).getFilteredAnnotationIDs(loggedIn, "some html 1", null, after, before);
427                will(returnValue(mockAnnotationIDsOwned));
428               
429                oneOf(targetDao).getTargetIDs(1);
430                will(returnValue(mockTargets1));
431               
432                oneOf(targetDao).getLink(1);
433                will(returnValue(link1));
434               
435                oneOf(targetDao).getLink(2);
436                will(returnValue(link2));
437             
438
439            }
440        });
441
442
443       
444        List resultExact = dbDispatcher.getFilteredAnnotationIDs(null, "http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia", MatchMode.EXACT, "some html 1", 3, "read", null, after, before);
445        assertEquals(1, resultExact.size());
446        assertEquals(1, resultExact.get(0));
447       
448     
449    }
450   
451    /**
452     * Test of getFilteredAnnotationIDs method, of class DBIntegrityServiceImlp.
453     */
454    @Test
455    public void testGetFilteredAnnotationIDsStartsWith() throws NotInDataBaseException {
456        System.out.println("test getFilteredAnnotationIDs");
457       
458        final List<Number> mockAnnotationIDs1 = new ArrayList<Number>();
459        mockAnnotationIDs1.add(1);
460     
461        final List<Number> mockAnnotationIDsOwned = new ArrayList<Number>();
462        //mockAnnotationIDsOwned.add(3);
463
464        final List<Number> mockAnnotationIDsRead = new ArrayList<Number>();
465        mockAnnotationIDsRead.add(1);
466        mockAnnotationIDsRead.add(2);
467       
468
469        final List<Number> mockAnnotationIDsPublicRead = new ArrayList<Number>();
470        mockAnnotationIDsPublicRead.add(1);
471        mockAnnotationIDsPublicRead.add(2);
472       
473        final List<Number> mockTargets1 = new ArrayList<Number>();
474        mockTargets1.add(1);
475        mockTargets1.add(2);
476       
477       
478        final String link1 = "http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia";
479        final String link2 = "http://nl.wikipedia.org/wiki/Antoni_Gaud%C3%AD";
480
481        final String after = (new Timestamp(0)).toString();
482        final String before = (new Timestamp(System.currentTimeMillis())).toString();
483
484        final Number loggedIn = 3;
485
486        mockeryDao.checking(new Expectations() {
487            {
488               
489
490                oneOf(annotationDao).getFilteredAnnotationIDs(null, "some html 1", null, after, before);
491                will(returnValue(mockAnnotationIDs1));
492
493                oneOf(annotationDao).getAnnotationIDsPermissionAtLeast(loggedIn, Access.READ);
494                will(returnValue(mockAnnotationIDsRead));
495
496                oneOf(annotationDao).getAnnotationIDsPublicAtLeast(Access.READ);
497                will(returnValue(mockAnnotationIDsPublicRead));               
498
499                oneOf(annotationDao).getFilteredAnnotationIDs(loggedIn, "some html 1", null, after, before);
500                will(returnValue(mockAnnotationIDsOwned));
501               
502                oneOf(targetDao).getTargetIDs(1);
503                will(returnValue(mockTargets1));
504               
505                oneOf(targetDao).getLink(1);
506                will(returnValue(link1));
507               
508                oneOf(targetDao).getLink(2);
509                will(returnValue(link2));
510             
511
512            }
513        });
514
515
516       
517        List resultStartsWith = dbDispatcher.getFilteredAnnotationIDs(null, "http://nl.wikipedia.org/wiki/Sagrada_", MatchMode.STARTS_WITH, "some html 1", 3, "read", null, after, before);
518        assertEquals(1, resultStartsWith.size());
519        assertEquals(1, resultStartsWith.get(0));
520       
521       
522       
523    }
524   
525    /**
526     * Test of getFilteredAnnotationIDs method, of class DBIntegrityServiceImlp.
527     */
528    @Test
529    public void testGetFilteredAnnotationIDsEndsWith() throws NotInDataBaseException {
530        System.out.println("test getFilteredAnnotationIDs");
531       
532        final List<Number> mockAnnotationIDs1 = new ArrayList<Number>();
533        mockAnnotationIDs1.add(1);
534     
535        final List<Number> mockAnnotationIDsOwned = new ArrayList<Number>();
536        //mockAnnotationIDsOwned.add(3);
537
538        final List<Number> mockAnnotationIDsRead = new ArrayList<Number>();
539        mockAnnotationIDsRead.add(1);
540        mockAnnotationIDsRead.add(2);
541       
542
543        final List<Number> mockAnnotationIDsPublicRead = new ArrayList<Number>();
544        mockAnnotationIDsPublicRead.add(1);
545        mockAnnotationIDsPublicRead.add(2);
546       
547        final List<Number> mockTargets1 = new ArrayList<Number>();
548        mockTargets1.add(1);
549        mockTargets1.add(2);
550       
551       
552        final String link1 = "http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia";
553        final String link2 = "http://nl.wikipedia.org/wiki/Antoni_Gaud%C3%AD";
554
555        final String after = (new Timestamp(0)).toString();
556        final String before = (new Timestamp(System.currentTimeMillis())).toString();
557
558        final Number loggedIn = 3;
559
560        mockeryDao.checking(new Expectations() {
561            {
562               
563
564                oneOf(annotationDao).getFilteredAnnotationIDs(null, "some html 1", null, after, before);
565                will(returnValue(mockAnnotationIDs1));
566
567                oneOf(annotationDao).getAnnotationIDsPermissionAtLeast(loggedIn, Access.READ);
568                will(returnValue(mockAnnotationIDsRead));
569
570                oneOf(annotationDao).getAnnotationIDsPublicAtLeast(Access.READ);
571                will(returnValue(mockAnnotationIDsPublicRead));               
572
573                oneOf(annotationDao).getFilteredAnnotationIDs(loggedIn, "some html 1", null, after, before);
574                will(returnValue(mockAnnotationIDsOwned));
575               
576                oneOf(targetDao).getTargetIDs(1);
577                will(returnValue(mockTargets1));
578               
579                oneOf(targetDao).getLink(1);
580                will(returnValue(link1));
581               
582                oneOf(targetDao).getLink(2);
583                will(returnValue(link2));
584             
585
586            }
587        });
588
589       
590        List resultEndsWith = dbDispatcher.getFilteredAnnotationIDs(null, "Fam%C3%ADlia", MatchMode.ENDS_WITH, "some html 1", 3, "read", null, after, before);
591        assertEquals(1, resultEndsWith.size());
592        assertEquals(1, resultEndsWith.get(0));
593       
594    }
595
596   
597    @Test
598    public void testGetFilteredAnnotationIDs2Contains() throws NotInDataBaseException {
599        System.out.println("test getFilteredAnnotationIDs");
600       
601        final List<Number> mockAnnotationIDs1 = new ArrayList<Number>();
602        mockAnnotationIDs1.add(1);
603
604     
605        final List<Number> mockAnnotationIDsOwned = new ArrayList<Number>();
606        //mockAnnotationIDsOwned.add(3);
607
608        final List<Number> mockAnnotationIDsWrite = new ArrayList<Number>();
609        mockAnnotationIDsWrite.add(2);
610       
611
612        final List<Number> mockAnnotationIDsPublicWrite = new ArrayList<Number>();
613        mockAnnotationIDsPublicWrite.add(1);
614       
615        final List<Number> mockTargets1 = new ArrayList<Number>();
616        mockTargets1.add(1);
617        mockTargets1.add(2);
618       
619       
620        final String link1 = "http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia";
621        final String link2 = "http://nl.wikipedia.org/wiki/Antoni_Gaud%C3%AD";
622
623        final String after = (new Timestamp(0)).toString();
624        final String before = (new Timestamp(System.currentTimeMillis())).toString();
625
626        final Number loggedIn = 3;
627
628        mockeryDao.checking(new Expectations() {
629            {
630                oneOf(annotationDao).getFilteredAnnotationIDs(null, "some html 1", null, after, before);
631                will(returnValue(mockAnnotationIDs1));
632
633                oneOf(annotationDao).getAnnotationIDsPermissionAtLeast(loggedIn, Access.WRITE);
634                will(returnValue(mockAnnotationIDsWrite));
635
636                oneOf(annotationDao).getAnnotationIDsPublicAtLeast(Access.WRITE);
637                will(returnValue(mockAnnotationIDsPublicWrite));               
638
639                oneOf(annotationDao).getFilteredAnnotationIDs(loggedIn, "some html 1", null, after, before);
640                will(returnValue(mockAnnotationIDsOwned));
641               
642                oneOf(targetDao).getTargetIDs(1);
643                will(returnValue(mockTargets1));
644               
645                oneOf(targetDao).getLink(1);
646                will(returnValue(link1));
647               
648                oneOf(targetDao).getLink(2);
649                will(returnValue(link2));
650               
651            }
652        });
653
654
655       
656        List resultContains = dbDispatcher.getFilteredAnnotationIDs(null, "Sagrada_", MatchMode.CONTAINS, "some html 1", 3, "write", null, after, before);
657        assertEquals(1, resultContains.size());
658        assertEquals(1, resultContains.get(0));
659    }
660   
661    @Test
662    public void testGetFilteredAnnotationIDs2Exact() throws NotInDataBaseException {
663        System.out.println("test getFilteredAnnotationIDs");
664       
665        final List<Number> mockAnnotationIDs1 = new ArrayList<Number>();
666        mockAnnotationIDs1.add(1);
667
668     
669        final List<Number> mockAnnotationIDsOwned = new ArrayList<Number>();
670        //mockAnnotationIDsOwned.add(3);
671
672        final List<Number> mockAnnotationIDsWrite = new ArrayList<Number>();
673        mockAnnotationIDsWrite.add(2);
674       
675
676        final List<Number> mockAnnotationIDsPublicWrite = new ArrayList<Number>();
677        mockAnnotationIDsPublicWrite.add(1);
678       
679        final List<Number> mockTargets1 = new ArrayList<Number>();
680        mockTargets1.add(1);
681        mockTargets1.add(2);
682       
683       
684        final String link1 = "http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia";
685        final String link2 = "http://nl.wikipedia.org/wiki/Antoni_Gaud%C3%AD";
686
687        final String after = (new Timestamp(0)).toString();
688        final String before = (new Timestamp(System.currentTimeMillis())).toString();
689
690        final Number loggedIn = 3;
691
692        mockeryDao.checking(new Expectations() {
693            {
694                oneOf(annotationDao).getFilteredAnnotationIDs(null, "some html 1", null, after, before);
695                will(returnValue(mockAnnotationIDs1));
696
697                oneOf(annotationDao).getAnnotationIDsPermissionAtLeast(loggedIn, Access.WRITE);
698                will(returnValue(mockAnnotationIDsWrite));
699
700                oneOf(annotationDao).getAnnotationIDsPublicAtLeast(Access.WRITE);
701                will(returnValue(mockAnnotationIDsPublicWrite));               
702
703                oneOf(annotationDao).getFilteredAnnotationIDs(loggedIn, "some html 1", null, after, before);
704                will(returnValue(mockAnnotationIDsOwned));
705               
706                oneOf(targetDao).getTargetIDs(1);
707                will(returnValue(mockTargets1));
708               
709                oneOf(targetDao).getLink(1);
710                will(returnValue(link1));
711               
712                oneOf(targetDao).getLink(2);
713                will(returnValue(link2));
714               
715            }
716        });
717
718
719       
720        List resultExact = dbDispatcher.getFilteredAnnotationIDs(null, "http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia", MatchMode.EXACT, "some html 1", 3, "write", null, after, before);
721        assertEquals(1, resultExact.size());
722        assertEquals(1, resultExact.get(0));
723       
724       
725    }
726   
727    @Test
728    public void testGetFilteredAnnotationIDs2StartsWith() throws NotInDataBaseException {
729        System.out.println("test getFilteredAnnotationIDs");
730       
731        final List<Number> mockAnnotationIDs1 = new ArrayList<Number>();
732        mockAnnotationIDs1.add(1);
733
734     
735        final List<Number> mockAnnotationIDsOwned = new ArrayList<Number>();
736        //mockAnnotationIDsOwned.add(3);
737
738        final List<Number> mockAnnotationIDsWrite = new ArrayList<Number>();
739        mockAnnotationIDsWrite.add(2);
740       
741
742        final List<Number> mockAnnotationIDsPublicWrite = new ArrayList<Number>();
743        mockAnnotationIDsPublicWrite.add(1);
744       
745        final List<Number> mockTargets1 = new ArrayList<Number>();
746        mockTargets1.add(1);
747        mockTargets1.add(2);
748       
749       
750        final String link1 = "http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia";
751        final String link2 = "http://nl.wikipedia.org/wiki/Antoni_Gaud%C3%AD";
752
753        final String after = (new Timestamp(0)).toString();
754        final String before = (new Timestamp(System.currentTimeMillis())).toString();
755
756        final Number loggedIn = 3;
757
758        mockeryDao.checking(new Expectations() {
759            {
760                oneOf(annotationDao).getFilteredAnnotationIDs(null, "some html 1", null, after, before);
761                will(returnValue(mockAnnotationIDs1));
762
763                oneOf(annotationDao).getAnnotationIDsPermissionAtLeast(loggedIn, Access.WRITE);
764                will(returnValue(mockAnnotationIDsWrite));
765
766                oneOf(annotationDao).getAnnotationIDsPublicAtLeast(Access.WRITE);
767                will(returnValue(mockAnnotationIDsPublicWrite));               
768
769                oneOf(annotationDao).getFilteredAnnotationIDs(loggedIn, "some html 1", null, after, before);
770                will(returnValue(mockAnnotationIDsOwned));
771               
772                oneOf(targetDao).getTargetIDs(1);
773                will(returnValue(mockTargets1));
774               
775                oneOf(targetDao).getLink(1);
776                will(returnValue(link1));
777               
778                oneOf(targetDao).getLink(2);
779                will(returnValue(link2));
780               
781            }
782        });
783
784
785        List resultStartsWith = dbDispatcher.getFilteredAnnotationIDs(null, "http://nl.wikipedia.org/wiki/Sagrada_", MatchMode.STARTS_WITH, "some html 1", 3, "write", null, after, before);
786        assertEquals(1, resultStartsWith.size());
787        assertEquals(1, resultStartsWith.get(0));
788       
789       
790    }
791   
792    @Test
793    public void testGetFilteredAnnotationIDs2EndsWith() throws NotInDataBaseException {
794        System.out.println("test getFilteredAnnotationIDs");
795       
796        final List<Number> mockAnnotationIDs1 = new ArrayList<Number>();
797        mockAnnotationIDs1.add(1);
798
799     
800        final List<Number> mockAnnotationIDsOwned = new ArrayList<Number>();
801        //mockAnnotationIDsOwned.add(3);
802
803        final List<Number> mockAnnotationIDsWrite = new ArrayList<Number>();
804        mockAnnotationIDsWrite.add(2);
805       
806
807        final List<Number> mockAnnotationIDsPublicWrite = new ArrayList<Number>();
808        mockAnnotationIDsPublicWrite.add(1);
809       
810        final List<Number> mockTargets1 = new ArrayList<Number>();
811        mockTargets1.add(1);
812        mockTargets1.add(2);
813       
814       
815        final String link1 = "http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia";
816        final String link2 = "http://nl.wikipedia.org/wiki/Antoni_Gaud%C3%AD";
817
818        final String after = (new Timestamp(0)).toString();
819        final String before = (new Timestamp(System.currentTimeMillis())).toString();
820
821        final Number loggedIn = 3;
822
823        mockeryDao.checking(new Expectations() {
824            {
825                oneOf(annotationDao).getFilteredAnnotationIDs(null, "some html 1", null, after, before);
826                will(returnValue(mockAnnotationIDs1));
827
828                oneOf(annotationDao).getAnnotationIDsPermissionAtLeast(loggedIn, Access.WRITE);
829                will(returnValue(mockAnnotationIDsWrite));
830
831                oneOf(annotationDao).getAnnotationIDsPublicAtLeast(Access.WRITE);
832                will(returnValue(mockAnnotationIDsPublicWrite));               
833
834                oneOf(annotationDao).getFilteredAnnotationIDs(loggedIn, "some html 1", null, after, before);
835                will(returnValue(mockAnnotationIDsOwned));
836               
837                oneOf(targetDao).getTargetIDs(1);
838                will(returnValue(mockTargets1));
839               
840                oneOf(targetDao).getLink(1);
841                will(returnValue(link1));
842               
843                oneOf(targetDao).getLink(2);
844                will(returnValue(link2));
845               
846            }
847        });
848
849
850       
851        List resultEndsWith = dbDispatcher.getFilteredAnnotationIDs(null, "Fam%C3%ADlia", MatchMode.ENDS_WITH, "some html 1", 3, "write", null, after, before);
852        assertEquals(1, resultEndsWith.size());
853        assertEquals(1, resultEndsWith.get(0));
854
855    }
856
857    @Test
858    public void testGetFilteredAnnotationIDs3() throws NotInDataBaseException {
859        System.out.println("test getFilteredAnnotationIDs");
860       
861        final List<Number> mockAnnotationIDs1 = new ArrayList<Number>();
862       
863        final String after = (new Timestamp(0)).toString();
864        final String before = (new Timestamp(System.currentTimeMillis())).toString();
865
866       
867        mockeryDao.checking(new Expectations() {
868            {               
869
870                oneOf(annotationDao).getFilteredAnnotationIDs(3, "some html 1", null, after, before);
871                will(returnValue(mockAnnotationIDs1));
872               
873                oneOf(annotationDao).getFilteredAnnotationIDs(3, "some html 1", null, after, before);
874                will(returnValue(mockAnnotationIDs1));
875               
876                oneOf(annotationDao).getFilteredAnnotationIDs(3, "some html 1", null, after, before);
877                will(returnValue(mockAnnotationIDs1));
878               
879                oneOf(annotationDao).getFilteredAnnotationIDs(3, "some html 1", null, after, before);
880                will(returnValue(mockAnnotationIDs1));
881
882            }
883        });
884
885
886        List resultContains = dbDispatcher.getFilteredAnnotationIDs(null, "Sagrada_", MatchMode.CONTAINS, "some html 1", 3, "owner", null, after, before);
887        assertEquals(0, resultContains.size());
888       
889        List resultExact = dbDispatcher.getFilteredAnnotationIDs(null, "http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia", MatchMode.EXACT, "some html 1", 3, "owner", null, after, before);
890        assertEquals(0, resultExact.size());
891       
892        List resultStartsWith = dbDispatcher.getFilteredAnnotationIDs(null, "http://nl.wikipedia.org/wiki/Sagrada_", MatchMode.STARTS_WITH, "some html 1", 3, "owner", null, after, before);
893        assertEquals(0, resultStartsWith.size());
894       
895        List resultEndsWith = dbDispatcher.getFilteredAnnotationIDs(null, "Fam%C3%ADlia", MatchMode.ENDS_WITH, "some html 1", 3, "owner", null, after, before);
896        assertEquals(0, resultEndsWith.size());
897    }
898
899   
900    @Test
901    public void testGetFilteredAnnotationIDs4() throws NotInDataBaseException {
902        System.out.println("test getFilteredAnnotationIDs");
903       
904        final String after = (new Timestamp(0)).toString();
905        final String before = (new Timestamp(System.currentTimeMillis())).toString();
906
907//        final List<Number> mockRetval = new ArrayList<Number>();
908//        mockRetval.add(1);
909
910        final Number loggedIn = 3;
911
912        mockeryDao.checking(new Expectations() {
913            {
914
915                oneOf(principalDao).getExternalID(loggedIn);
916                will(returnValue(UUID.fromString("00000000-0000-0000-0000-000000000113")));
917
918
919            }
920        });
921
922
923        List result = dbDispatcher.getFilteredAnnotationIDs(UUID.fromString("00000000-0000-0000-0000-000000000111"), "nl.wikipedia.org", MatchMode.CONTAINS, "some html 1", 3, "owner", null, after, before);
924        assertEquals(0, result.size());
925    }
926
927    @Test
928    public void testGetAnnotationTargets() throws SQLException {
929        System.out.println("test getAnnotationTargets");
930       
931        final List<Number> targetIDs = new ArrayList<Number>();
932        targetIDs.add(1);
933        targetIDs.add(2);
934        mockeryDao.checking(new Expectations() {
935            {
936                oneOf(targetDao).getTargetIDs(1);
937                will(returnValue(targetIDs));
938
939                oneOf(targetDao).getHrefFromInternalID(1);
940                will(returnValue("/api/targets/00000000-0000-0000-0000-000000000031"));
941
942                oneOf(targetDao).getHrefFromInternalID(2);
943                will(returnValue("/api/targets/00000000-0000-0000-0000-000000000032"));
944
945            }
946        });
947
948        ReferenceList result = dbDispatcher.getAnnotationTargets(1);
949        assertEquals(2, result.getHref().size());
950        assertEquals("/api/targets/00000000-0000-0000-0000-000000000031", result.getHref().get(0));
951        assertEquals("/api/targets/00000000-0000-0000-0000-000000000032", result.getHref().get(1));
952
953    }
954
955//     @Override
956//    public AnnotationInfoList getFilteredAnnotationInfos(String link, String text, String access, String namespace, UUID
957//            owner, Timestamp after, Timestamp before){
958//        List<Number> annotationIDs = getFilteredAnnotationIDs(link, text, access, namespace, owner, after, before);
959//        List<AnnotationInfo> listAnnotationInfo = annotationDao.getAnnotationInfos(annotationIDs);
960    //       AnnotationInfoList result = new AnnotationInfoList();
961    //       result.getAnnotation().addAll(listAnnotationInfo);
962    //       return result;
963//    }
964    @Test
965    public void testGetFilteredAnnotationInfos() throws NotInDataBaseException {
966        System.out.println("test getetFilteredAnnotationInfos");
967       
968        final List<Number> mockAnnotationIDs1 = new ArrayList<Number>();
969        mockAnnotationIDs1.add(1);
970
971       
972
973        final UUID ownerUUID = UUID.fromString("00000000-0000-0000-0000-000000000111");
974        final String after = (new Timestamp(0)).toString();
975        final String before = (new Timestamp(System.currentTimeMillis())).toString();
976
977
978
979        final List<Number> mockAnnotationIDsOwned = new ArrayList<Number>();
980        //mockAnnotationIDsOwned.add(3);
981
982        final List<Number> mockAnnotationIDsRead = new ArrayList<Number>();
983        mockAnnotationIDsRead.add(1);
984        mockAnnotationIDsRead.add(2);
985       
986        final List<Number> mockAnnotationIDsPublicRead = new ArrayList<Number>();
987        mockAnnotationIDsPublicRead.add(1);
988        mockAnnotationIDsPublicRead.add(2);
989
990       
991       
992       
993        final List<Number> mockTargets1 = new ArrayList<Number>();
994        mockTargets1.add(1);
995        mockTargets1.add(2);
996       
997       
998        final String link1 = "http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia";
999        final String link2 = "http://nl.wikipedia.org/wiki/Antoni_Gaud%C3%AD";
1000       
1001
1002        final AnnotationInfo mockAnnotInfo = new AnnotationInfo();
1003
1004        mockAnnotInfo.setHeadline("Sagrada Famiglia");
1005        mockAnnotInfo.setHref("/api/annotations/00000000-0000-0000-0000-000000000021");
1006        mockAnnotInfo.setOwnerHref("/api/principals/00000000-0000-0000-0000-000000000111");
1007
1008        final List<Number> targetIDs = new ArrayList<Number>();
1009        targetIDs.add(1);
1010        targetIDs.add(2);
1011
1012        final Number loggedIn = 3;
1013
1014        mockeryDao.checking(new Expectations() {
1015            {
1016                oneOf(principalDao).getInternalID(ownerUUID);
1017                will(returnValue(1));
1018
1019             
1020                oneOf(annotationDao).getFilteredAnnotationIDs(1, "some html 1", null, after, before);
1021                will(returnValue(mockAnnotationIDs1));
1022
1023                oneOf(annotationDao).getAnnotationIDsPermissionAtLeast(loggedIn, Access.READ);
1024                will(returnValue(mockAnnotationIDsRead));
1025
1026                oneOf(annotationDao).getAnnotationIDsPublicAtLeast(Access.READ);
1027                will(returnValue(mockAnnotationIDsPublicRead));
1028             
1029
1030                oneOf(annotationDao).getFilteredAnnotationIDs(3, "some html 1", null, after, before);
1031                will(returnValue(mockAnnotationIDsOwned));
1032
1033                oneOf(targetDao).getTargetIDs(1);
1034                will(returnValue(mockTargets1));
1035               
1036                oneOf(targetDao).getLink(1);
1037                will(returnValue(link1));
1038               
1039                oneOf(targetDao).getLink(2);
1040                will(returnValue(link2));
1041
1042//                ///////////////////////////////////
1043//               
1044                oneOf(annotationDao).getAnnotationInfoWithoutTargetsAndOwner(1);
1045                will(returnValue(mockAnnotInfo));
1046
1047                oneOf(annotationDao).getOwner(1);
1048                will(returnValue(1));
1049
1050                oneOf(principalDao).getHrefFromInternalID(1);
1051                will(returnValue("/api/principals/00000000-0000-0000-0000-000000000111"));
1052
1053                ////
1054                oneOf(targetDao).getTargetIDs(1);
1055                will(returnValue(targetIDs));
1056
1057                oneOf(targetDao).getHrefFromInternalID(1);
1058                will(returnValue("/api/targets/00000000-0000-0000-0000-000000000031"));
1059
1060
1061                oneOf(targetDao).getHrefFromInternalID(2);
1062                will(returnValue("/api/targets/00000000-0000-0000-0000-000000000032"));
1063
1064
1065            }
1066        });
1067
1068
1069        AnnotationInfoList result = dbDispatcher.getFilteredAnnotationInfos(ownerUUID, "http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia", MatchMode.EXACT, "some html 1", 3, "read", null, after, before);
1070        assertEquals(1, result.getAnnotationInfo().size());
1071        AnnotationInfo resultAnnotInfo = result.getAnnotationInfo().get(0);
1072        assertEquals("Sagrada Famiglia", resultAnnotInfo.getHeadline());
1073        assertEquals("/api/principals/00000000-0000-0000-0000-000000000111", resultAnnotInfo.getOwnerHref());
1074        assertEquals("/api/annotations/00000000-0000-0000-0000-000000000021", result.getAnnotationInfo().get(0).getHref());
1075        assertEquals("/api/targets/00000000-0000-0000-0000-000000000031", resultAnnotInfo.getTargets().getHref().get(0));
1076        assertEquals("/api/targets/00000000-0000-0000-0000-000000000032", resultAnnotInfo.getTargets().getHref().get(1));
1077
1078    }
1079
1080    @Test
1081    public void testGetTargetsWithNoCachedRepresentation() {
1082        System.out.println("test getTargetsWithNoCachedRepresentation");
1083     
1084        final List<Number> targetIDs = new ArrayList<Number>();
1085        targetIDs.add(5);
1086        targetIDs.add(7);
1087
1088        final List<Number> cachedIDs5 = new ArrayList<Number>();
1089        cachedIDs5.add(7);
1090        final List<Number> cachedIDs7 = new ArrayList<Number>();
1091
1092
1093
1094        mockeryDao.checking(new Expectations() {
1095            {
1096                oneOf(targetDao).getTargetIDs(3);
1097                will(returnValue(targetIDs));
1098
1099                oneOf(cachedRepresentationDao).getCachedRepresentationsForTarget(5);
1100                will(returnValue(cachedIDs5));
1101
1102                oneOf(cachedRepresentationDao).getCachedRepresentationsForTarget(7);
1103                will(returnValue(cachedIDs7));
1104
1105                oneOf(targetDao).getHrefFromInternalID(7);
1106                will(returnValue("/api/targets/00000000-0000-0000-0000-000000000037"));
1107
1108            }
1109        });
1110
1111        List<String> result = dbDispatcher.getTargetsWithNoCachedRepresentation(3);
1112        assertEquals(1, result.size());
1113        assertEquals("/api/targets/00000000-0000-0000-0000-000000000037", result.get(0)); // Target number 7 has no cached
1114    }
1115
1116    ////////////// ADDERS /////////////////////////
1117    /**
1118     * Test of addCachedForVersion method, of class DBIntegrityServiceImlp.
1119     */
1120    @Test
1121    public void testAddCached() throws SerialException, IOException, NotInDataBaseException {
1122        System.out.println("test addCached");
1123       
1124        String mime = "text/html";
1125        String type = "text";
1126        String tool = "latex";
1127        String externalID = Helpers.generateUUID().toString();
1128        final CachedRepresentationInfo newCachedInfo = new CachedRepresentationInfo();
1129        newCachedInfo.setMimeType(mime);
1130        newCachedInfo.setType(type);
1131        newCachedInfo.setTool(tool);
1132        newCachedInfo.setHref("/api/cached/" + externalID);
1133        newCachedInfo.setId(externalID);
1134       
1135        String blobString = "aaa";
1136        byte[] blobBytes = blobString.getBytes();
1137        final ByteArrayInputStream newCachedBlob = new ByteArrayInputStream(blobBytes);
1138
1139        mockeryDao.checking(new Expectations() {
1140            {
1141
1142                oneOf(cachedRepresentationDao).addCachedRepresentation(newCachedInfo, newCachedBlob);
1143                will(returnValue(8));
1144
1145                one(targetDao).addTargetCachedRepresentation(1, 8, "#(1,2)");
1146                will(returnValue(1));
1147
1148            }
1149        });
1150
1151
1152        Number[] result = dbDispatcher.addCachedForTarget(1, "#(1,2)", newCachedInfo, newCachedBlob);
1153        assertEquals(2, result.length);
1154        assertEquals(1, result[0]);
1155        assertEquals(8, result[1]);
1156    }
1157
1158    /**
1159     * Test of updateSiblingTargetClassForTarget method, of class
1160     * DBIntegrityServiceImlp.
1161     *
1162     *
1163     */
1164    /**
1165     * Test of addTargetsForAnnotation method, of class DBIntegrityServiceImlp.
1166     */
1167    @Test
1168    public void testAddTargetsForAnnotation() throws Exception {
1169        System.out.println("test addTargetsForAnnotation");
1170       
1171        // test 1: adding an existing target
1172        TargetInfo testTargetOne = new TargetInfo();
1173        testTargetOne.setLink("http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia");
1174        testTargetOne.setHref("/api/targets/00000000-0000-0000-0000-000000000031");
1175        testTargetOne.setVersion("version 1.0");
1176        final List<TargetInfo> mockTargetListOne = new ArrayList<TargetInfo>();
1177        mockTargetListOne.add(testTargetOne);
1178
1179        mockeryDao.checking(new Expectations() {
1180            {
1181                oneOf(targetDao).getInternalIDFromHref(mockTargetListOne.get(0).getHref());
1182                will(returnValue(1));
1183
1184                oneOf(annotationDao).addAnnotationTarget(4, 1);
1185                will(returnValue(1));
1186            }
1187        });
1188
1189        Map<String, String> result = dbDispatcher.addTargetsForAnnotation(4, mockTargetListOne);
1190        assertEquals(0, result.size());
1191
1192        // test 2: adding a new Target
1193        TargetInfo testTargetTwo = new TargetInfo();
1194        final String tempTargetID = "/api/targets/"+Helpers.generateUUID().toString();
1195        testTargetTwo.setHref(tempTargetID);
1196        testTargetTwo.setLink("http://www.sagradafamilia.cat/docs_instit/historia.php");
1197        testTargetTwo.setVersion("version 1.0");
1198        final List<TargetInfo> mockTargetListTwo = new ArrayList<TargetInfo>();
1199        mockTargetListTwo.add(testTargetTwo);
1200
1201        final UUID mockNewTargetUUID = Helpers.generateUUID();
1202        final NotInDataBaseException e = new NotInDataBaseException("target", "external ID", tempTargetID);
1203
1204//        Target newTarget = this.createFreshTarget(targetInfo);
1205//                Number targetID = targetDao.addTarget(newTarget);
1206//                String targetTemporaryId = targetInfo.getHref();
1207//                result.put(targetTemporaryId, targetDao.getExternalID(targetID).toString());
1208//                int affectedRows = annotationDao.addAnnotationTarget(annotationID, targetID);
1209//       
1210        mockeryDao.checking(new Expectations() {
1211            {
1212                oneOf(targetDao).getInternalIDFromHref(mockTargetListTwo.get(0).getHref());
1213                will(throwException(e));
1214
1215                oneOf(targetDao).addTarget(with(aNonNull(Target.class)));
1216                will(returnValue(8)); //# the next new number is 8, we have already 7 Targets
1217
1218                oneOf(targetDao).getHrefFromInternalID(8);
1219                will(returnValue("/api/targets/"+mockNewTargetUUID.toString()));
1220
1221                oneOf(annotationDao).addAnnotationTarget(1, 8);
1222                will(returnValue(1));
1223
1224            }
1225        });
1226
1227        Map<String, String> resultTwo = dbDispatcher.addTargetsForAnnotation(1, mockTargetListTwo);
1228        assertEquals(1, resultTwo.size());
1229        assertEquals("/api/targets/"+mockNewTargetUUID.toString(), resultTwo.get(tempTargetID));
1230
1231    }
1232
1233    /**
1234     * Test of addPrincipalsAnnotation method, of class DBIntegrityServiceImlp.
1235     */
1236    @Test
1237    public void testAddPrincipalsAnnotation() throws Exception {
1238        System.out.println("test addPrincipalsAnnotation");
1239
1240        // expectations for addPrincipalsannotation itself
1241        final Annotation testAnnotation = testInstances.getAnnotationToAdd();
1242
1243        mockeryDao.checking(new Expectations() {
1244            {
1245                oneOf(annotationDao).addAnnotation(testAnnotation, 3);
1246                will(returnValue(5)); // the next free number is 5
1247
1248                //  expectations for addTargetsForannotation
1249                oneOf(targetDao).getInternalIDFromHref("/api/targets/00000000-0000-0000-0000-000000000031");
1250                will(returnValue(1));
1251
1252                oneOf(annotationDao).addAnnotationTarget(5, 1);
1253                will(returnValue(1));
1254
1255                ///
1256
1257                oneOf(annotationDao).updateAnnotationBody(5, testAnnotation.getBody().getTextBody().getBody(), testAnnotation.getBody().getTextBody().getMimeType(), false);
1258                will(returnValue(1)); // the DB update will be called at perform anyway, even if the body is not changed (can be optimized)
1259
1260                //////
1261               
1262               
1263                oneOf(principalDao).getInternalIDFromHref("/api/principals/00000000-0000-0000-0000-000000000111");
1264                will(returnValue(1));
1265               
1266                oneOf(annotationDao).addPermission(5, 1, Access.ALL);
1267                will(returnValue(1));
1268            }
1269        });
1270
1271        Number result = dbDispatcher.addPrincipalsAnnotation(3, testAnnotation);
1272        assertEquals(5, result);
1273    }
1274
1275    @Test
1276    public void testAddPrincipal() throws NotInDataBaseException, PrincipalExists {
1277        System.out.println("test addPrincipal");
1278        final Principal freshPrincipal = new Principal();
1279        freshPrincipal.setDisplayName("Guilherme");
1280        freshPrincipal.setEMail("Guilherme.Silva@mpi.nl");
1281        mockeryDao.checking(new Expectations() {
1282            {
1283                oneOf(principalDao).principalExists("guisil@mpi.nl");
1284                will(returnValue(false));
1285
1286                oneOf(principalDao).addPrincipal(freshPrincipal, "guisil@mpi.nl");
1287                will(returnValue(11));
1288            }
1289        });
1290
1291
1292        assertEquals(11, dbDispatcher.addPrincipal(freshPrincipal, "guisil@mpi.nl").intValue());
1293
1294        /// principal already exists
1295        final Principal principal = new Principal();
1296        freshPrincipal.setDisplayName("Olha");
1297        freshPrincipal.setEMail("Olha.Shakaravska@mpi.nl");
1298        mockeryDao.checking(new Expectations() {
1299            {
1300                oneOf(principalDao).principalExists("olhsha@mpi.nl");
1301                will(returnValue(true));
1302
1303            }
1304        });
1305
1306        PrincipalExists ex = null;
1307        try {
1308            dbDispatcher.addPrincipal(principal, "olhsha@mpi.nl");
1309        } catch (PrincipalExists e) {
1310            ex = e;
1311        }
1312        assertFalse(ex == null);
1313    }
1314
1315    //////////////////// DELETERS ////////////////
1316    @Test
1317    public void testDeletePrincipal() throws PrincipalCannotBeDeleted{
1318        System.out.println("test deletePrincipal");
1319
1320        mockeryDao.checking(new Expectations() {
1321            {
1322                oneOf(principalDao).deletePrincipal(1);
1323                will(returnValue(0));
1324
1325                oneOf(principalDao).deletePrincipal(3);
1326                will(returnValue(0));
1327
1328                oneOf(principalDao).deletePrincipal(10);
1329                will(returnValue(1));
1330
1331            }
1332        });
1333
1334        assertEquals(0, dbDispatcher.deletePrincipal(1));
1335        assertEquals(0, dbDispatcher.deletePrincipal(3));
1336        assertEquals(1, dbDispatcher.deletePrincipal(10));
1337    }
1338
1339    /**
1340     * Test of deleteCachedForVersion method, of class DBIntegrityServiceImlp.
1341     */
1342    @Test
1343    public void testDeleteCachedRepresentationForTarget() throws SQLException {
1344        System.out.println("test deleteCachedRepresentationForTarget");
1345        mockeryDao.checking(new Expectations() {
1346            {
1347                oneOf(targetDao).deleteTargetCachedRepresentation(5, 7);
1348                will(returnValue(1));
1349
1350                oneOf(cachedRepresentationDao).deleteCachedRepresentation(7);
1351                will(returnValue(1)); // cached is used by another version
1352
1353            }
1354        });
1355
1356        int[] result = dbDispatcher.deleteCachedRepresentationOfTarget(5, 7);
1357        assertEquals(2, result.length);
1358        assertEquals(1, result[0]);
1359        assertEquals(1, result[1]);
1360    }
1361
1362    /////////////////////////////////////////////
1363    @Test
1364    public void testDeleteAllCachedRepresentationsOfTarget() throws SQLException {
1365        System.out.println("test deleteAllCachedRepresentationsOfTarget");
1366        final List<Number> cachedList = new ArrayList<Number>();
1367        cachedList.add(1);
1368        cachedList.add(2);
1369
1370        mockeryDao.checking(new Expectations() {
1371            {
1372                oneOf(cachedRepresentationDao).getCachedRepresentationsForTarget(1);
1373                will(returnValue(cachedList));
1374
1375                oneOf(targetDao).deleteTargetCachedRepresentation(1, 1);
1376                will(returnValue(1));
1377
1378                oneOf(cachedRepresentationDao).deleteCachedRepresentation(1);
1379                will(returnValue(1));
1380
1381                oneOf(targetDao).deleteTargetCachedRepresentation(1, 2);
1382                will(returnValue(1));
1383
1384                oneOf(cachedRepresentationDao).deleteCachedRepresentation(2);
1385                will(returnValue(1));
1386
1387            }
1388        });
1389
1390        int[] result = dbDispatcher.deleteAllCachedRepresentationsOfTarget(1);
1391        assertEquals(2, result[0]); // # affected rows in Targets_cacheds
1392        assertEquals(2, result[1]); // # affected rows in cacheds
1393    }
1394
1395    /**
1396     * Test of deleteAnnotationWithTargets method, of class
1397     * DBIntegrityServiceImlp.
1398     */
1399    @Test
1400    public void testDeleteAnnotation() throws Exception {
1401        System.out.println("deleteAnnotation ");
1402
1403        // deleting annotation 3, which has its target Target 2  (used by annotation # 1)
1404        final List<Number> mockTargetIDs = new ArrayList<Number>();
1405        mockTargetIDs.add(2);
1406
1407        final List<Number> mockCachedIDs = new ArrayList<Number>();
1408        mockCachedIDs.add(3);
1409
1410        mockeryDao.checking(new Expectations() {
1411            {
1412                oneOf(annotationDao).deletePermissions(2);
1413                will(returnValue(2));
1414
1415                oneOf(targetDao).getTargetIDs(2);
1416                will(returnValue(mockTargetIDs));
1417
1418                oneOf(annotationDao).deleteAllAnnotationTarget(2);
1419                will(returnValue(1));
1420
1421                oneOf(annotationDao).deleteAnnotation(2);
1422                will(returnValue(1));
1423
1424                oneOf(cachedRepresentationDao).getCachedRepresentationsForTarget(2);
1425                will(returnValue(mockCachedIDs));
1426
1427                oneOf(targetDao).deleteTargetCachedRepresentation(2, 3);
1428                will(returnValue(1));
1429
1430                oneOf(cachedRepresentationDao).deleteCachedRepresentation(3);
1431                will(returnValue(1));
1432
1433                oneOf(annotationDao).targetIsInUse(2);
1434                will(returnValue(true));
1435
1436                oneOf(targetDao).deleteTarget(2);
1437                will(returnValue(0));
1438
1439                oneOf(annotationDao).deleteAnnotationFromAllNotebooks(2);
1440                will(returnValue(1));
1441
1442
1443            }
1444        });
1445        int[] result = dbDispatcher.deleteAnnotation(2);// the Target will be deleted because it is not referred by any annotation
1446        assertEquals(5, result.length);
1447        assertEquals(1, result[0]); // annotation 3 is deleted
1448        assertEquals(2, result[1]); // 2 rows in "annotation principal accesss are deleted"
1449        assertEquals(1, result[2]);  // row (3,2) in "annotations_Targets" is deleted
1450        assertEquals(0, result[3]); //  target 2 is not deleted deleted since it is used by annotation 1
1451        assertEquals(1, result[4]); // deleted from 1 notebook
1452    }
1453
1454    /**
1455     * NOTEBOOKS
1456     */
1457    /**
1458     * Getters
1459     */
1460//     public Number getNotebookInternalIdentifier(UUID externalIdentifier){
1461//        return notebookDao.getInternalID(externalIdentifier);
1462//    }
1463    @Test
1464    public void testGetNotebookInternalIdentifier() throws NotInDataBaseException {
1465
1466        final UUID mockUUID = UUID.fromString("00000000-0000-0000-0000-000000000021");
1467
1468        mockeryDao.checking(new Expectations() {
1469            {
1470                oneOf(notebookDao).getInternalID(mockUUID);
1471                will(returnValue(1));
1472            }
1473        });
1474
1475        assertEquals(1, dbDispatcher.getResourceInternalIdentifier(mockUUID, Resource.NOTEBOOK));
1476
1477    }
1478
1479//    public NotebookInfoList getNotebooks(Number principalID, String access) {
1480//        NotebookInfoList result = new NotebookInfoList();
1481//        if (access.equalsIgnoreCase("read") || access.equalsIgnoreCase("write")) {
1482//            List<Number> notebookIDs = notebookDao.getNotebookIDs(principalID, Access.fromValue(access));
1483//            for (Number notebookID : notebookIDs) {
1484//                NotebookInfo notebookInfo = notebookDao.getNotebookInfoWithoutOwner(notebookID);
1485//                Number ownerID = notebookDao.getOwner(notebookID);
1486//                notebookInfo.setOwnerRef(principalDao.getURIFromInternalID(ownerID));
1487//                result.getNotebookInfo().add(notebookInfo);
1488//            }
1489//        } else {
1490//            if (access.equalsIgnoreCase("owner")) {
1491//                List<Number> notebookIDs = notebookDao.getNotebookIDsOwnedBy(principalID);
1492//                String ownerRef = principalDao.getURIFromInternalID(principalID);
1493//                for (Number notebookID : notebookIDs) {
1494//                    NotebookInfo notebookInfo = notebookDao.getNotebookInfoWithoutOwner(notebookID);
1495//                    notebookInfo.setOwnerRef(ownerRef);
1496//                    result.getNotebookInfo().add(notebookInfo);
1497//                }
1498//            } else {
1499//                return null;
1500//            }
1501//        }
1502//        return result;
1503//    }
1504    @Test
1505    public void testGetNotebooksREADBranch() {
1506
1507        final List<Number> mockNotebookIDs = new ArrayList<Number>();
1508        mockNotebookIDs.add(1);
1509
1510        final NotebookInfo mockNotebookInfo = new NotebookInfo();
1511        mockNotebookInfo.setHref("/api/notebooks/00000000-0000-0000-0000-000000000011");
1512        mockNotebookInfo.setTitle("Notebook 1");
1513
1514        mockeryDao.checking(new Expectations() {
1515            {
1516                oneOf(notebookDao).getNotebookIDs(3, Access.READ);
1517                will(returnValue(mockNotebookIDs));
1518
1519                oneOf(notebookDao).getNotebookInfoWithoutOwner(1);
1520                will(returnValue(mockNotebookInfo));
1521
1522                oneOf(notebookDao).getOwner(1);
1523                will(returnValue(1));
1524
1525                oneOf(principalDao).getHrefFromInternalID(1);
1526                will(returnValue("/api/principals/00000000-0000-0000-0000-000000000111"));
1527
1528            }
1529        });
1530
1531        NotebookInfoList result = dbDispatcher.getNotebooks(3, Access.READ);
1532        assertEquals("/api/notebooks/00000000-0000-0000-0000-000000000011", result.getNotebookInfo().get(0).getHref());
1533        assertEquals("/api/principals/00000000-0000-0000-0000-000000000111", result.getNotebookInfo().get(0).getOwnerHref());
1534        assertEquals("Notebook 1", result.getNotebookInfo().get(0).getTitle());
1535
1536    }
1537
1538    @Test
1539    @Ignore
1540    public void testGetNotebooksOwnerBranch() {
1541
1542        final List<Number> mockNotebookIDs = new ArrayList<Number>();
1543        mockNotebookIDs.add(3);
1544        mockNotebookIDs.add(4);
1545
1546        final NotebookInfo mockNotebookInfo1 = new NotebookInfo();
1547        mockNotebookInfo1.setHref("/api/notebooks/00000000-0000-0000-0000-000000000013");
1548        mockNotebookInfo1.setTitle("Notebook 3");
1549
1550        final NotebookInfo mockNotebookInfo2 = new NotebookInfo();
1551        mockNotebookInfo2.setHref("/api/notebooks/00000000-0000-0000-0000-000000000014");
1552        mockNotebookInfo2.setTitle("Notebook 4");
1553
1554        mockeryDao.checking(new Expectations() {
1555            {
1556                oneOf(notebookDao).getNotebookIDsOwnedBy(3);
1557                will(returnValue(mockNotebookIDs));
1558
1559                oneOf(principalDao).getHrefFromInternalID(3);
1560                will(returnValue("/api/principals/00000000-0000-0000-0000-000000000113"));
1561
1562                oneOf(notebookDao).getNotebookInfoWithoutOwner(3);
1563                will(returnValue(mockNotebookInfo1));
1564
1565                oneOf(notebookDao).getNotebookInfoWithoutOwner(4);
1566                will(returnValue(mockNotebookInfo2));
1567
1568            }
1569        });
1570
1571        //??
1572//        NotebookInfoList result = dbDispatcher.getNotebooks(3, "owner");
1573//        assertEquals("00000000-0000-0000-0000-000000000013", result.getNotebookInfo().get(0).getRef());//        assertEquals("00000000-0000-0000-0000-000000000113", result.getNotebookInfo().get(0).getOwnerRef());
1574//        assertEquals("Notebook 3", result.getNotebookInfo().get(0).getTitle());
1575//        assertEquals("00000000-0000-0000-0000-000000000014", result.getNotebookInfo().get(1).getRef());
1576//        assertEquals("00000000-0000-0000-0000-000000000113", result.getNotebookInfo().get(1).getOwnerRef());
1577//        assertEquals("Notebook 4", result.getNotebookInfo().get(1).getTitle());
1578
1579    }
1580
1581//    public ReferenceList getNotebooksOwnedBy(Number principalID) {
1582//        ReferenceList result = new ReferenceList();
1583//        List<Number> notebookIDs = notebookDao.getNotebookIDsOwnedBy(principalID);
1584//        for (Number notebookID : notebookIDs) {
1585//            String reference = notebookDao.getURIFromInternalID(notebookID);
1586//            result.getRef().add(reference);
1587//        }
1588//        return result;
1589//    }
1590    @Test
1591    public void testGetNotebooksOwnedBy() {
1592
1593        final List<Number> mockNotebookIDs = new ArrayList<Number>();
1594        mockNotebookIDs.add(3);
1595        mockNotebookIDs.add(4);
1596
1597        mockeryDao.checking(new Expectations() {
1598            {
1599                oneOf(notebookDao).getNotebookIDsOwnedBy(3);
1600                will(returnValue(mockNotebookIDs));
1601
1602                oneOf(notebookDao).getHrefFromInternalID(3);
1603                will(returnValue("/api/notebooks/00000000-0000-0000-0000-000000000013"));
1604
1605                oneOf(notebookDao).getHrefFromInternalID(4);
1606                will(returnValue("/api/notebooks/00000000-0000-0000-0000-000000000014"));
1607
1608            }
1609        });
1610
1611        ReferenceList result = dbDispatcher.getNotebooksOwnedBy(3);
1612        assertEquals(2, result.getHref().size());
1613        assertEquals("/api/notebooks/00000000-0000-0000-0000-000000000013", result.getHref().get(0));
1614        assertEquals("/api/notebooks/00000000-0000-0000-0000-000000000014", result.getHref().get(1));
1615    }
1616
1617    /*      public boolean hasAccess(Number notebookID, Number principalID, Access access){
1618     List<Number> notebookIDs = notebookDao.getNotebookIDs(principalID, access);
1619     if (notebookIDs == null) {
1620     return false;
1621     }
1622     return notebookIDs.contains(notebookID);
1623     } */
1624    @Test
1625    public void testHasAccess() {
1626
1627
1628        final Access write = Access.fromValue("write");
1629        final List<Number> mockNotebookIDwrite = new ArrayList<Number>();
1630        mockNotebookIDwrite.add(1);
1631        mockNotebookIDwrite.add(4);
1632
1633        mockeryDao.checking(new Expectations() {
1634            {
1635                oneOf(notebookDao).getNotebookIDs(2, write);
1636                will(returnValue(mockNotebookIDwrite));
1637
1638                oneOf(notebookDao).getNotebookIDs(2, write);
1639                will(returnValue(mockNotebookIDwrite));
1640
1641            }
1642        });
1643
1644        assertTrue(dbDispatcher.hasAccess(4, 2, write));
1645        assertFalse(dbDispatcher.hasAccess(5, 2, write));
1646    }
1647
1648    /*
1649     public ReferenceList getPrincipals(Number notebookID, String access) {
1650     ReferenceList result = new ReferenceList();
1651     List<Number> principalIDs = notebookDao.getPrincipalIDsWithAccess(notebookID, Access.fromValue(access));
1652     for (Number principalID : principalIDs) {
1653     String reference = principalDao.getURIFromInternalID(principalID);
1654     result.getRef().add(reference);
1655     }
1656     return result;
1657     }
1658     }*/
1659    @Test
1660    public void testGetPrincipals() {
1661        final List<Number> mockPrincipalIDs = new ArrayList<Number>();
1662        mockPrincipalIDs.add(2);
1663        mockPrincipalIDs.add(4);
1664
1665        mockeryDao.checking(new Expectations() {
1666            {
1667                oneOf(principalDao).getPrincipalIDsWithAccessForNotebook(1, Access.WRITE);
1668                will(returnValue(mockPrincipalIDs));
1669
1670                oneOf(principalDao).getHrefFromInternalID(2);
1671                will(returnValue("/api/principals/00000000-0000-0000-0000-000000000112"));
1672
1673                oneOf(principalDao).getHrefFromInternalID(4);
1674                will(returnValue("/api/principals/00000000-0000-0000-0000-000000000114"));
1675
1676
1677            }
1678        });
1679
1680        ReferenceList result = dbDispatcher.getPrincipals(1, "write");
1681        assertEquals("/api/principals/00000000-0000-0000-0000-000000000112", result.getHref().get(0).toString());
1682        assertEquals("/api/principals/00000000-0000-0000-0000-000000000114", result.getHref().get(1).toString());
1683
1684    }
1685
1686//   @Override
1687//    public Notebook getNotebook(Number notebookID) {
1688//        Notebook result = notebookDao.getNotebookWithoutAnnotationsAndAccesssAndOwner(notebookID);
1689//
1690//        result.setOwnerRef(principalDao.getURIFromInternalID(notebookDao.getOwner(notebookID)));
1691//
1692//        ReferenceList annotations = new ReferenceList();
1693//        List<Number> annotationIDs = notebookDao.getAnnotations(notebookID);
1694//        for (Number annotationID : annotationIDs) {
1695//            annotations.getRef().add(annotationDao.getURIFromInternalID(annotationID));
1696//        }
1697//        result.setAnnotations(annotations);
1698//
1699//        PermissionList ups = new PermissionList();
1700//        List<Access> accesss = new ArrayList<Access>();
1701//        accesss.add(Access.READ);
1702//        accesss.add(Access.WRITE);
1703//        for (Access access : accesss) {
1704//            List<Number> principals = notebookDao.getPrincipalIDsWithAccess(notebookID, access);
1705//            if (principals != null) {
1706//                for (Number principal : principals) {
1707//                    Permission up = new Permission();
1708//                    up.setRef(principalDao.getURIFromInternalID(principal));
1709//                    up.setAccess(access);
1710//                    ups.getPermission().add(up);
1711//                }
1712//            }
1713//        }
1714//
1715//        result.setAccesss(ups);
1716//        return result;
1717//    }
1718    @Test
1719    public void testGetNotebook() throws DatatypeConfigurationException {
1720
1721        final Notebook mockNotebook = new Notebook();
1722        mockNotebook.setHref("/api/notebooks/00000000-0000-0000-0000-000000000012");
1723        mockNotebook.setId("00000000-0000-0000-0000-000000000012");
1724        mockNotebook.setTitle("Notebook 2");
1725        mockNotebook.setLastModified(DatatypeFactory.newInstance().newXMLGregorianCalendar("2014-02-12T09:25:00.383000Z"));
1726
1727        final List<Number> mockAnnotations = new ArrayList<Number>();
1728        mockAnnotations.add(3);
1729
1730        final List<Number> mockREADs = new ArrayList<Number>();
1731        mockREADs.add(1);
1732
1733        final List<Number> mockWRITEs = new ArrayList<Number>();
1734        mockWRITEs.add(3);
1735
1736        mockeryDao.checking(new Expectations() {
1737            {
1738                oneOf(notebookDao).getNotebookWithoutAnnotationsAndAccesssAndOwner(2);
1739                will(returnValue(mockNotebook));
1740
1741                oneOf(notebookDao).getOwner(2);
1742                will(returnValue(2));
1743
1744                oneOf(principalDao).getHrefFromInternalID(2);
1745                will(returnValue("/api/principals/00000000-0000-0000-0000-000000000112"));
1746
1747                oneOf(annotationDao).getAnnotations(2);
1748                will(returnValue(mockAnnotations));
1749
1750                oneOf(annotationDao).getHrefFromInternalID(3);
1751                will(returnValue("/api/annotations/00000000-0000-0000-0000-000000000023"));
1752
1753                oneOf(principalDao).getPrincipalIDsWithAccessForNotebook(2, Access.READ);
1754                will(returnValue(mockREADs));
1755
1756                oneOf(principalDao).getHrefFromInternalID(1);
1757                will(returnValue("/api/principals/00000000-0000-0000-0000-000000000111"));
1758
1759                oneOf(principalDao).getPrincipalIDsWithAccessForNotebook(2, Access.WRITE);
1760                will(returnValue(mockWRITEs));
1761
1762                oneOf(principalDao).getHrefFromInternalID(3);
1763                will(returnValue("/api/principals/00000000-0000-0000-0000-000000000113"));
1764
1765               oneOf(principalDao).getPrincipalIDsWithAccessForNotebook(2, Access.ALL);
1766                will(returnValue(new ArrayList<Number>()));
1767
1768            }
1769        });
1770
1771        Notebook result = dbDispatcher.getNotebook(2);
1772        assertEquals("/api/notebooks/00000000-0000-0000-0000-000000000012", result.getHref());
1773        assertEquals("00000000-0000-0000-0000-000000000012", result.getId());
1774        assertEquals("/api/principals/00000000-0000-0000-0000-000000000112", result.getOwnerRef());
1775        assertEquals("2014-02-12T09:25:00.383000Z", result.getLastModified().toString());
1776        assertEquals("Notebook 2", result.getTitle());
1777        assertEquals(1, result.getAnnotations().getHref().size());
1778        assertEquals("/api/annotations/00000000-0000-0000-0000-000000000023", result.getAnnotations().getHref().get(0));
1779        assertEquals(2, result.getPermissions().getPermission().size());
1780        assertEquals("/api/principals/00000000-0000-0000-0000-000000000111", result.getPermissions().getPermission().get(0).getPrincipalHref());
1781        assertEquals("read", result.getPermissions().getPermission().get(0).getLevel().value());
1782        assertEquals("/api/principals/00000000-0000-0000-0000-000000000113", result.getPermissions().getPermission().get(1).getPrincipalHref());
1783        assertEquals("write", result.getPermissions().getPermission().get(1).getLevel().value());
1784
1785    }
1786
1787    @Test
1788    public void testAnnotationsForNotebook() {
1789        final List<Number> mockAnnotationIDs = new ArrayList<Number>();
1790        mockAnnotationIDs.add(1);
1791        mockAnnotationIDs.add(2);
1792
1793        mockeryDao.checking(new Expectations() {
1794            {
1795                oneOf(annotationDao).getAnnotations(1);
1796                will(returnValue(mockAnnotationIDs));
1797
1798                oneOf(annotationDao).sublistOrderedAnnotationIDs(mockAnnotationIDs, 0, 3, "last_modified", "DESC");
1799                will(returnValue(mockAnnotationIDs));
1800
1801                oneOf(annotationDao).getHrefFromInternalID(1);
1802                will(returnValue("/api/annotations/00000000-0000-0000-0000-000000000021"));
1803
1804                oneOf(annotationDao).getHrefFromInternalID(2);
1805                will(returnValue("/api/annotations/00000000-0000-0000-0000-000000000022"));
1806
1807
1808
1809            }
1810        });
1811
1812        ReferenceList result = dbDispatcher.getAnnotationsForNotebook(1, -1, 3, "last_modified", true);
1813        assertEquals(2, result.getHref().size());
1814        assertEquals("/api/annotations/00000000-0000-0000-0000-000000000021", result.getHref().get(0).toString());
1815        assertEquals("/api/annotations/00000000-0000-0000-0000-000000000022", result.getHref().get(1).toString());
1816
1817    }
1818
1819    /**
1820     * Updaters
1821     */
1822//    public boolean updateNotebookMetadata(Number notebookID, NotebookInfo upToDateNotebookInfo) {
1823//        Number ownerID = principalDao.getInternalIDFromURI(upToDateNotebookInfo.getOwnerRef());
1824//        return notebookDao.updateNotebookMetadata(notebookID, upToDateNotebookInfo.getTitle(), ownerID);
1825//    }
1826    @Test
1827    public void testUpdateNotebookMetadata() throws NotInDataBaseException {
1828
1829        final NotebookInfo mockNotebookInfo = new NotebookInfo();
1830        mockNotebookInfo.setOwnerHref("/api/principals/00000000-0000-0000-0000-000000000113");
1831        mockNotebookInfo.setTitle("New Title");
1832
1833        mockeryDao.checking(new Expectations() {
1834            {
1835                oneOf(principalDao).getInternalIDFromHref("/api/principals/00000000-0000-0000-0000-000000000113");
1836                will(returnValue(3));
1837
1838                oneOf(notebookDao).updateNotebookMetadata(1, "New Title", 3);
1839                will(returnValue(true));
1840            }
1841        });
1842
1843        boolean result = dbDispatcher.updateNotebookMetadata(1, mockNotebookInfo);
1844        assertTrue(result);
1845    }
1846
1847//
1848//    public boolean addAnnotationToNotebook(Number notebookID, Number annotationID) {
1849//        return notebookDao.addAnnotationToNotebook(notebookID, annotationID);
1850//    }
1851    @Test
1852    public void testAddAnnotationToNotebook() {
1853
1854        mockeryDao.checking(new Expectations() {
1855            {
1856
1857                oneOf(notebookDao).addAnnotationToNotebook(1, 3);
1858                will(returnValue(true));
1859            }
1860        });
1861
1862        assertTrue(dbDispatcher.addAnnotationToNotebook(1, 3));
1863    }
1864
1865    /**
1866     * Adders
1867     */
1868//    public Number createNotebook(Notebook notebook, Number ownerID) {
1869//        Number notebookID = notebookDao.createNotebookWithoutAccesssAndAnnotations(notebook, ownerID);
1870//        boolean updateOwner = notebookDao.setOwner(notebookID, ownerID);
1871//        List<Permission> accesss = notebook.getPermissions().getPermission();
1872//        for (Permission principalAccess : accesss) {
1873//            Number principalID = principalDao.getInternalIDFromURI(principalAccess.getRef());
1874//            Access access = principalAccess.getAccess();
1875//            boolean updateAccesss = notebookDao.addAccessToNotebook(notebookID, principalID, access);
1876//        }
1877//        return notebookID;
1878//    }
1879    @Test
1880    public void testCreateNotebook() throws NotInDataBaseException {
1881
1882        final Notebook notebook = new Notebook();
1883        notebook.setOwnerRef("tmpXXX");
1884        notebook.setTitle("(Almost) Copy of Notebook 1");
1885        notebook.setId("tmpYYY");
1886        notebook.setHref("whatever");
1887
1888        PermissionList accesss = new PermissionList();
1889        Permission p1 = new Permission();
1890        p1.setLevel(Access.WRITE);
1891        p1.setPrincipalHref("/api/principals/00000000-0000-0000-0000-000000000112");
1892        accesss.getPermission().add(p1);
1893        Permission p2 = new Permission();
1894        p2.setLevel(Access.READ);
1895        p2.setPrincipalHref("/api/principals/00000000-0000-0000-0000-000000000113");
1896        accesss.getPermission().add(p2);
1897        notebook.setPermissions(accesss);
1898
1899        mockeryDao.checking(new Expectations() {
1900            {
1901                oneOf(notebookDao).createNotebookWithoutAccesssAndAnnotations(notebook, 1);
1902                will(returnValue(5));
1903
1904                oneOf(notebookDao).setOwner(5, 1);
1905                will(returnValue(true));
1906
1907                oneOf(principalDao).getInternalIDFromHref("/api/principals/00000000-0000-0000-0000-000000000112");
1908                will(returnValue(2));
1909
1910                oneOf(principalDao).getInternalIDFromHref("/api/principals/00000000-0000-0000-0000-000000000113");
1911                will(returnValue(3));
1912
1913                oneOf(notebookDao).addAccessToNotebook(5, 2, Access.WRITE);
1914                will(returnValue(true));
1915
1916                oneOf(notebookDao).addAccessToNotebook(5, 3, Access.READ);
1917                will(returnValue(true));
1918
1919            }
1920        });
1921
1922        Number result = dbDispatcher.createNotebook(notebook, 1);
1923        assertEquals(5, result);
1924
1925    }
1926
1927//    public boolean createAnnotationInNotebook(Number notebookID, Annotation annotation, Number ownerID) {
1928//        Number newAnnotationID = this.addPrincipalsAnnotation(ownerID, annotation);
1929//        return notebookDao.addAnnotationToNotebook(notebookID, newAnnotationID);
1930//    }
1931    @Test
1932    public void testCreateAnnotationInNotebook() throws NotInDataBaseException {
1933
1934        final Annotation testAnnotation = testInstances.getAnnotationToAdd();
1935
1936        mockeryDao.checking(new Expectations() {
1937            {
1938                oneOf(annotationDao).addAnnotation(testAnnotation, 3);
1939                will(returnValue(5)); // the next free number is 5
1940
1941                //  expectations for addTargetsForannotation
1942                oneOf(targetDao).getInternalIDFromHref("/api/targets/00000000-0000-0000-0000-000000000031");
1943                will(returnValue(1));
1944
1945                oneOf(annotationDao).addAnnotationTarget(5, 1);
1946                will(returnValue(1));
1947
1948                oneOf(annotationDao).updateAnnotationBody(5, testAnnotation.getBody().getTextBody().getBody(), testAnnotation.getBody().getTextBody().getMimeType(), false);
1949                will(returnValue(1)); // the DB update will be called at perform anyway, even if the body is not changed (can be optimized)
1950
1951                oneOf(annotationDao).updatePublicAccess(5, Access.WRITE);
1952                will(returnValue(1));
1953               
1954                ////               
1955                   
1956                oneOf(principalDao).getInternalIDFromHref("/api/principals/00000000-0000-0000-0000-000000000111");
1957                will(returnValue(1));
1958
1959                /////////////////////////
1960
1961                oneOf(notebookDao).addAnnotationToNotebook(1, 5);
1962                will(returnValue(true));
1963               
1964                oneOf(annotationDao).addPermission(5, 1, Access.ALL);
1965                will(returnValue(1));
1966            }
1967        });
1968
1969        assertTrue(dbDispatcher.createAnnotationInNotebook(1, testAnnotation, 3));
1970
1971    }
1972
1973    /**
1974     * Deleters
1975     */
1976//      public boolean deleteNotebook(Number notebookID) {
1977//        if (notebookDao.deleteAllAccesssForNotebook(notebookID) || notebookDao.deleteAllAnnotationsFromNotebook(notebookID)) {
1978//            return notebookDao.deleteNotebook(notebookID);
1979//        } else {
1980//            return false;
1981//        }
1982//   
1983//   }
1984    @Test
1985    public void testDeleteNotebook() {
1986
1987        mockeryDao.checking(new Expectations() {
1988            {
1989
1990                oneOf(notebookDao).deleteAllAccesssForNotebook(1);
1991                will(returnValue(true));
1992
1993                oneOf(notebookDao).deleteAllAnnotationsFromNotebook(1);
1994                will(returnValue(true));
1995
1996                oneOf(notebookDao).deleteNotebook(1);
1997                will(returnValue(true));
1998            }
1999        });
2000
2001        assertTrue(dbDispatcher.deleteNotebook(1));
2002    }
2003
2004    @Test
2005    public void testGetAccess() {
2006        System.out.println("test getAccess");
2007
2008        mockeryDao.checking(new Expectations() {
2009            {
2010
2011                oneOf(annotationDao).getAccess(1, 3);
2012                will(returnValue(Access.READ));
2013
2014                oneOf(annotationDao).getPublicAttribute(1);
2015                will(returnValue(Access.WRITE));
2016
2017            }
2018        });
2019
2020        assertEquals(Access.WRITE, dbDispatcher.getAccess(1, 3));
2021
2022        //////
2023
2024        mockeryDao.checking(new Expectations() {
2025            {
2026                oneOf(annotationDao).getAccess(2, 3);
2027                will(returnValue(Access.READ));
2028
2029                oneOf(annotationDao).getPublicAttribute(2);
2030                will(returnValue(Access.READ));
2031
2032            }
2033        });
2034        assertEquals(Access.READ, dbDispatcher.getAccess(2, 3));
2035
2036        //////
2037
2038        mockeryDao.checking(new Expectations() {
2039            {
2040                oneOf(annotationDao).getAccess(3, 3);
2041                will(returnValue(Access.NONE));
2042
2043                oneOf(annotationDao).getPublicAttribute(3);
2044                will(returnValue(Access.NONE));
2045
2046            }
2047        });
2048        assertEquals(Access.NONE, dbDispatcher.getAccess(3, 3));
2049
2050        //////
2051    }
2052
2053    @Test
2054    public void testPublicAttribute() {
2055
2056        System.out.println("test getPublicAttribute");
2057        mockeryDao.checking(new Expectations() {
2058            {
2059                oneOf(annotationDao).getPublicAttribute(2);
2060                will(returnValue(Access.READ));
2061
2062            }
2063        });
2064        assertEquals(Access.READ, dbDispatcher.getPublicAttribute(2));
2065    }
2066
2067//      @Override
2068//    public int updateAnnotation(Annotation annotation) throws NotInDataBaseException {
2069//        Number annotationID = annotationDao.getInternalIDFromURI(annotation.getURI());
2070//        int updatedAnnotations = annotationDao.updateAnnotation(annotation, annotationID, principalDao.getInternalIDFromURI(annotation.getOwnerRef()));
2071//        int deletedTargets = annotationDao.deleteAllAnnotationTarget(annotationID);
2072//        int deletedPrinsipalsAccesss = annotationDao.deleteAnnotationPermissions(annotationID);
2073//        int addedTargets = addTargets(annotation, annotationID);
2074//        int addedPrincipalsAccesss = addPermissions(annotation.getPermissions().getPermission(), annotationID);
2075//        int updatedPublicAttribute = annotationDao.updatePublicAttribute(annotationID, annotation.getPermissions().getPublic());
2076//        return updatedAnnotations;
2077//    }
2078//    for (TargetInfo targetInfo : targets) {
2079//            try {
2080//                Number targetIDRunner = targetDao.getInternalIDFromURI(targetInfo.getRef());
2081//                int affectedRows = annotationDao.addAnnotationTarget(annotationID, targetIDRunner);
2082//            } catch (NotInDataBaseException e) {
2083//                Target newTarget = this.createFreshTarget(targetInfo);
2084//                Number targetID = targetDao.addTarget(newTarget);
2085//                String targetTemporaryID = targetDao.stringURItoExternalID(targetInfo.getRef());
2086//                result.put(targetTemporaryID, targetDao.getExternalID(targetID).toString());
2087//                int affectedRows = annotationDao.addAnnotationTarget(annotationID, targetID);
2088//            }
2089//        }
2090    @Test
2091    public void testUpdateAnnotation() throws NotInDataBaseException {
2092
2093        System.out.println("test updateAnnotation");
2094
2095        final Annotation annotation = (new TestInstances("/api")).getAnnotationOne();
2096        final NotInDataBaseException e = new NotInDataBaseException("annotation", "external ID", "00000000-0000-0000-0000-000000000031");
2097        final UUID mockNewID = Helpers.generateUUID();
2098        final PermissionList permissions = annotation.getPermissions();
2099
2100
2101        System.out.println("test updateAnnotation");
2102        mockeryDao.checking(new Expectations() {
2103            {
2104                oneOf(annotationDao).getInternalID(UUID.fromString(annotation.getId()));
2105                will(returnValue(1));
2106
2107                oneOf(principalDao).getInternalIDFromHref(annotation.getOwnerHref());
2108                will(returnValue(1));
2109
2110                oneOf(annotationDao).updateAnnotation(annotation, 1, 1);
2111                will(returnValue(1));
2112
2113                oneOf(annotationDao).deleteAllAnnotationTarget(1);
2114                will(returnValue(1));
2115
2116                oneOf(annotationDao).deletePermissions(1);
2117                will(returnValue(3));
2118
2119
2120                /// adding the first target, not found in the DB
2121
2122                oneOf(targetDao).getInternalIDFromHref(annotation.getTargets().getTargetInfo().get(0).getHref());
2123                will(throwException(e));
2124
2125                oneOf(targetDao).addTarget(with(aNonNull(Target.class)));
2126                will(returnValue(8));
2127
2128                oneOf(targetDao).getHrefFromInternalID(8);
2129                will(returnValue("/api/targets/"+mockNewID.toString()));
2130
2131                oneOf(annotationDao).addAnnotationTarget(1, 8);
2132                will(returnValue(1));
2133
2134                /////////
2135                oneOf(targetDao).getInternalIDFromHref(annotation.getTargets().getTargetInfo().get(1).getHref());
2136                will(returnValue(2));
2137
2138                oneOf(annotationDao).addAnnotationTarget(1, 2);
2139                will(returnValue(1));
2140               
2141                /////
2142               
2143                oneOf(principalDao).getPrincipalInternalIDFromRemoteID("userello");
2144                will(returnValue(1));
2145
2146                /////
2147                oneOf(principalDao).getInternalIDFromHref(permissions.getPermission().get(0).getPrincipalHref());
2148                will(returnValue(2));
2149
2150                oneOf(annotationDao).addPermission(1, 2, Access.WRITE);
2151                will(returnValue(1));
2152
2153                oneOf(principalDao).getInternalIDFromHref(permissions.getPermission().get(1).getPrincipalHref());
2154                will(returnValue(3));
2155
2156                oneOf(annotationDao).addPermission(1, 3, Access.READ);
2157                will(returnValue(1));
2158
2159
2160                ////
2161
2162                oneOf(annotationDao).updateAnnotationBody(1, annotation.getBody().getTextBody().getBody(), "text/html", false);
2163                will(returnValue(1));
2164
2165            }
2166        });
2167        assertEquals(1, dbDispatcher.updateAnnotation(annotation, "userello"));
2168    }
2169
2170    @Test
2171    public void testUpdateHeadline() throws NotInDataBaseException {
2172
2173        System.out.println("test updateAnnotationHeadline  ");
2174        mockeryDao.checking(new Expectations() {
2175            { oneOf(annotationDao).updateAnnotationHeadline(1, "new Headline");
2176                will(returnValue(1));
2177
2178               
2179            }
2180        });
2181        assertEquals(1, dbDispatcher.updateAnnotationHeadline(1, "new Headline"));
2182    }
2183   
2184//    public int updateAnnotationPrincipalAccess(Number annotationID, Number principalID, Access access) {
2185//        int result;
2186//        Access currentAccess = annotationDao.getAccess(annotationID, principalID);
2187//        if (currentAccess != Access.NONE) {
2188//            result = annotationDao.updateAnnotationPrincipalAccess(annotationID, principalID, access);
2189//        } else {
2190//            if (!access.equals(Access.NONE)) {
2191//                result = annotationDao.deleteAnnotationPrincipalAccess(annotationID, principalID);
2192//                result = annotationDao.addAnnotationPrincipalAccess(annotationID, principalID, access);
2193//            } else {
2194//                result = 0;
2195//            }
2196//        }
2197//        return result;
2198//    }
2199    @Test
2200    public void testUpdatePermission() {
2201        System.out.println("test updateAnnotationPrincipalAccess");
2202        mockeryDao.checking(new Expectations() {
2203            {
2204                oneOf(annotationDao).hasExplicitAccess(1, 2);
2205                will(returnValue(true));
2206
2207                oneOf(annotationDao).updatePermission(1, 2, Access.READ);
2208                will(returnValue(1));
2209
2210                oneOf(annotationDao).hasExplicitAccess(1, 4);
2211                will(returnValue(false));
2212               
2213                oneOf(annotationDao).addPermission(1, 4, Access.WRITE);
2214                will(returnValue(1));
2215               
2216                oneOf(annotationDao).deletePermission(1, 2);
2217                will(returnValue(1));
2218
2219            }
2220        });
2221
2222        assertEquals(1, dbDispatcher.updatePermission(1, 2, Access.READ));
2223        assertEquals(1, dbDispatcher.updatePermission(1, 4, Access.WRITE));
2224        assertEquals(1, dbDispatcher.updatePermission(1, 2, null));
2225    }
2226
2227    @Test
2228    public void testUpdatePermissions() throws NotInDataBaseException {
2229        System.out.println("test updatePermissions");
2230
2231        final PermissionList permissions = new PermissionList();
2232       
2233        Permission permission2 = new Permission();
2234        permission2.setPrincipalHref("/api/principals/ref2");
2235        permission2.setLevel(Access.WRITE);
2236
2237        Permission permission3 = new Permission();
2238        permission3.setPrincipalHref("/api/principals/ref3");
2239        permission3.setLevel(Access.READ);
2240             
2241        Permission permission4 = new Permission();
2242        permission4.setLevel(Access.READ);
2243        permission4.setPrincipalHref("/api/principals/ref4");
2244       
2245        permissions.getPermission().add(permission2);
2246        permissions.getPermission().add(permission3);
2247        permissions.getPermission().add(permission4);       
2248        permissions.setPublic(Access.WRITE);
2249
2250        mockeryDao.checking(new Expectations() {
2251            {
2252                /////
2253                oneOf(annotationDao).updatePublicAccess(1, permissions.getPublic());
2254                will(returnValue(1));
2255
2256                oneOf(principalDao).getInternalIDFromHref(permissions.getPermission().get(0).getPrincipalHref());
2257                will(returnValue(2));
2258
2259                oneOf(annotationDao).hasExplicitAccess(1, 2);
2260                will(returnValue(true));
2261
2262                oneOf(principalDao).getInternalIDFromHref(permissions.getPermission().get(1).getPrincipalHref());
2263                will(returnValue(3));
2264
2265                oneOf(annotationDao).hasExplicitAccess(1, 3);
2266                will(returnValue(true));
2267               
2268                oneOf(principalDao).getInternalIDFromHref(permissions.getPermission().get(2).getPrincipalHref());
2269                will(returnValue(4));
2270
2271                oneOf(annotationDao).hasExplicitAccess(1, 4);
2272                will(returnValue(false));
2273               
2274                oneOf(annotationDao).updatePermission(1, 2, Access.WRITE);
2275                will(returnValue(1));
2276
2277                oneOf(annotationDao).updatePermission(1, 3, Access.READ);
2278                will(returnValue(1));
2279               
2280                oneOf(annotationDao).addPermission(1, 4, Access.READ);
2281                will(returnValue(1));
2282
2283            }
2284        });
2285
2286        assertEquals(3, dbDispatcher.updatePermissions(1, permissions));
2287
2288    }
2289
2290   
2291
2292    @Test
2293    public void testUpdatePublicAttribute(){
2294        System.out.println("test updatePublicAttribute");
2295
2296       
2297        mockeryDao.checking(new Expectations() {
2298            {
2299                /////
2300                oneOf(annotationDao).updatePublicAccess(1, Access.NONE);
2301                will(returnValue(1));
2302
2303
2304            }
2305        });
2306
2307        assertEquals(1, dbDispatcher.updatePublicAttribute(1, Access.NONE));
2308
2309    }
2310   
2311    @Test
2312    public void testCanDo(){
2313        System.out.println("test canDo");
2314       
2315        mockeryDao.checking(new Expectations() {            {
2316                /////
2317                oneOf(annotationDao).getOwner(1);
2318                will(returnValue(1));
2319                oneOf(principalDao).getTypeOfPrincipalAccount(1);
2320                will(returnValue("developer"));
2321            }
2322        });
2323        assertTrue(dbDispatcher.canDo(Access.ALL, 1, 1, Resource.ANNOTATION));
2324       
2325        mockeryDao.checking(new Expectations() {            {
2326                /////
2327                oneOf(annotationDao).getOwner(2);
2328                will(returnValue(2));
2329                oneOf(principalDao).getTypeOfPrincipalAccount(2);
2330                will(returnValue("developer"));
2331               
2332                oneOf(annotationDao).getAccess(2, 1);
2333                will(returnValue(Access.READ));
2334                oneOf(annotationDao).getPublicAttribute(2);
2335                will(returnValue(Access.READ));
2336            }
2337        });
2338        assertFalse(dbDispatcher.canDo(Access.WRITE, 1, 2, Resource.ANNOTATION));
2339       
2340        mockeryDao.checking(new Expectations() {            {
2341                /////
2342                oneOf(annotationDao).getOwner(3);
2343                will(returnValue(3));
2344                oneOf(principalDao).getTypeOfPrincipalAccount(1);
2345                will(returnValue("developer"));
2346               
2347                oneOf(annotationDao).getAccess(3, 1);
2348                will(returnValue(Access.READ));
2349                oneOf(annotationDao).getPublicAttribute(3);
2350                will(returnValue(Access.ALL));
2351            }
2352        });
2353        assertTrue(dbDispatcher.canDo(Access.WRITE, 1, 3, Resource.ANNOTATION));
2354       
2355        mockeryDao.checking(new Expectations() {            {
2356                /////
2357                oneOf(annotationDao).getOwner(4);
2358                will(returnValue(3));
2359                oneOf(principalDao).getTypeOfPrincipalAccount(11);
2360                will(returnValue("user"));
2361               
2362                oneOf(annotationDao).getAccess(4, 11);
2363                will(returnValue(Access.NONE));
2364                oneOf(annotationDao).getPublicAttribute(4);
2365                will(returnValue(Access.NONE));
2366            }
2367        });
2368        assertFalse(dbDispatcher.canDo(Access.READ, 11, 4, Resource.ANNOTATION));
2369    }
2370}
Note: See TracBrowser for help on using the repository browser.