source: DASISH/t5.6/backend/annotator-backend/tags/git-copy-censored/annotator-backend/src/test/java/eu/dasish/annotation/backend/dao/impl/DBDispatcherTest.java @ 6063

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

replacing olhsha@mpi.nl with alice@mail.domain in the code

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