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

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

refactoring. Adding ALL as an access mode.

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