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

Last change on this file since 4521 was 4521, checked in by olhsha, 10 years ago

Integrity-Service tests are done (mocking NotebookDao?).

File size: 48.5 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.TestBackendConstants;
21import eu.dasish.annotation.backend.TestInstances;
22import eu.dasish.annotation.backend.dao.AnnotationDao;
23import eu.dasish.annotation.backend.dao.CachedRepresentationDao;
24import eu.dasish.annotation.backend.dao.NotebookDao;
25import eu.dasish.annotation.backend.dao.TargetDao;
26import eu.dasish.annotation.backend.dao.UserDao;
27import eu.dasish.annotation.schema.Annotation;
28import eu.dasish.annotation.schema.AnnotationBody;
29import eu.dasish.annotation.schema.AnnotationBody.TextBody;
30import eu.dasish.annotation.schema.AnnotationInfo;
31import eu.dasish.annotation.schema.AnnotationInfoList;
32import eu.dasish.annotation.schema.CachedRepresentationInfo;
33import eu.dasish.annotation.schema.Notebook;
34import eu.dasish.annotation.schema.NotebookInfo;
35import eu.dasish.annotation.schema.NotebookInfoList;
36import eu.dasish.annotation.schema.Permission;
37import eu.dasish.annotation.schema.ReferenceList;
38import eu.dasish.annotation.schema.Target;
39import eu.dasish.annotation.schema.TargetInfo;
40import eu.dasish.annotation.schema.User;
41import eu.dasish.annotation.schema.UserWithPermission;
42import eu.dasish.annotation.schema.UserWithPermissionList;
43import java.io.ByteArrayInputStream;
44import java.sql.SQLException;
45import java.sql.Timestamp;
46import java.util.ArrayList;
47import java.util.HashMap;
48import java.util.List;
49import java.util.Map;
50import java.util.UUID;
51import javax.sql.rowset.serial.SerialException;
52import javax.xml.datatype.DatatypeFactory;
53import javax.xml.datatype.XMLGregorianCalendar;
54import org.jmock.Expectations;
55import org.jmock.Mockery;
56import org.junit.Test;
57import static org.junit.Assert.*;
58import org.junit.runner.RunWith;
59import org.springframework.beans.factory.annotation.Autowired;
60import org.springframework.test.context.ContextConfiguration;
61import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
62
63/**
64 *
65 * @author olhsha
66 */
67@RunWith(SpringJUnit4ClassRunner.class)
68@ContextConfiguration({"/spring-test-config/dataSource.xml", "/spring-test-config/mockeryDao.xml", "/spring-test-config/mockAnnotationDao.xml",
69    "/spring-test-config/mockUserDao.xml", "/spring-test-config/mockTargetDao.xml", "/spring-test-config/mockCachedRepresentationDao.xml",
70    "/spring-test-config/mockNotebookDao.xml",
71    "/spring-config/dbIntegrityService.xml"})
72public class DBIntegrityServiceTest {
73
74    @Autowired
75    private DBIntegrityServiceImlp dbIntegrityService;
76    @Autowired
77    private Mockery mockeryDao;
78    @Autowired
79    private UserDao userDao;
80    @Autowired
81    private CachedRepresentationDao cachedRepresentationDao;
82    @Autowired
83    private TargetDao targetDao;
84    @Autowired
85    private AnnotationDao annotationDao;
86    @Autowired
87    private NotebookDao notebookDao;
88    TestInstances testInstances = new TestInstances(TestBackendConstants._TEST_SERVLET_URI);
89
90    public DBIntegrityServiceTest() {
91    }
92
93    ///////// GETTERS /////////////
94    /**
95     * Test of getAnnotationInternalIdentifier method, of class
96     * DBIntegrityServiceImlp.
97     */
98    @Test
99    public void testGetAnnotationInternalIdentifier() {
100        System.out.println("getAnnotationInternalIdentifier");
101        final UUID externalID = UUID.fromString("00000000-0000-0000-0000-000000000021");
102
103        mockeryDao.checking(new Expectations() {
104            {
105                oneOf(annotationDao).getInternalID(externalID);
106                will(returnValue(1));
107            }
108        });
109        assertEquals(1, dbIntegrityService.getAnnotationInternalIdentifier(externalID));
110    }
111
112    /**
113     * Test of getAnnotationExternalIdentifier method, of class
114     * DBIntegrityServiceImlp.
115     */
116    @Test
117    public void testGetAnnotationExternalIdentifier() {
118        System.out.println("getAnnotationExternalIdentifier");
119        final UUID externalID = UUID.fromString("00000000-0000-0000-0000-000000000021");
120
121        mockeryDao.checking(new Expectations() {
122            {
123                oneOf(annotationDao).getExternalID(1);
124                will(returnValue(externalID));
125            }
126        });
127        assertEquals("00000000-0000-0000-0000-000000000021", dbIntegrityService.getAnnotationExternalIdentifier(1).toString());
128    }
129
130    /**
131     * Test of getUserInternalIdentifier method, of class
132     * DBIntegrityServiceImlp.
133     */
134    @Test
135    public void testGetUserInternalIdentifier() {
136        System.out.println("getUserInternalIdentifier");
137
138        final UUID externalID = UUID.fromString("00000000-0000-0000-0000-000000000111");
139
140        mockeryDao.checking(new Expectations() {
141            {
142                oneOf(userDao).getInternalID(externalID);
143                will(returnValue(1));
144            }
145        });
146        assertEquals(1, dbIntegrityService.getUserInternalIdentifier(externalID));
147    }
148
149    /**
150     * Test of getUserExternalIdentifier method, of class
151     * DBIntegrityServiceImlp.
152     */
153    @Test
154    public void testGetUserExternalIdentifier() {
155        System.out.println("getUserExternalIdentifier");
156        final UUID externalID = UUID.fromString("00000000-0000-0000-0000-000000000111");
157
158        mockeryDao.checking(new Expectations() {
159            {
160                oneOf(userDao).getExternalID(1);
161                will(returnValue(externalID));
162            }
163        });
164        assertEquals("00000000-0000-0000-0000-000000000111", dbIntegrityService.getUserExternalIdentifier(1).toString());
165    }
166
167    /**
168     * Test of getAnnotation method, of class DBIntegrityServiceImlp.
169     */
170    @Test
171    public void testGetAnnotation() throws Exception {
172        System.out.println("test getAnnotation");
173
174        final Annotation mockAnnotation = new Annotation();// corresponds to the annotation # 1
175        mockAnnotation.setURI(TestBackendConstants._TEST_SERVLET_URI_annotations + "00000000-0000-0000-0000-000000000021");
176        mockAnnotation.setHeadline("Sagrada Famiglia");
177        XMLGregorianCalendar mockTimeStamp = DatatypeFactory.newInstance().newXMLGregorianCalendar("2013-08-12T09:25:00.383000Z");
178        mockAnnotation.setLastModified(mockTimeStamp);
179        mockAnnotation.setOwnerRef(TestBackendConstants._TEST_SERVLET_URI_annotations + "00000000-0000-0000-0000-000000000111");
180
181        AnnotationBody mockBody = new AnnotationBody();
182        TextBody textBody = new AnnotationBody.TextBody();
183        mockBody.setTextBody(textBody);
184        textBody.setMimeType("text/plain");
185        textBody.setBody("<html><body>some html 1</body></html>");
186        mockAnnotation.setBody(mockBody);
187        mockAnnotation.setTargets(null);
188
189
190        final List<Number> mockTargetIDs = new ArrayList<Number>();
191        mockTargetIDs.add(1);
192        mockTargetIDs.add(2);
193
194        final Target mockTargetOne = new Target();
195        mockTargetOne.setLink("http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia");
196        mockTargetOne.setURI(TestBackendConstants._TEST_SERVLET_URI_Targets + "00000000-0000-0000-0000-000000000031");
197        mockTargetOne.setVersion("version 1.0");
198
199        final Target mockTargetTwo = new Target();
200        mockTargetTwo.setLink("http://nl.wikipedia.org/wiki/Antoni_Gaud%C3%AD");
201        mockTargetTwo.setURI(TestBackendConstants._TEST_SERVLET_URI_Targets + "00000000-0000-0000-0000-000000000032");
202        mockTargetTwo.setVersion("version 1.1");
203
204        final List<Map<Number, String>> listMap = new ArrayList<Map<Number, String>>();
205        Map<Number, String> map2 = new HashMap<Number, String>();
206        map2.put(2, "writer");
207        listMap.add(map2);
208        Map<Number, String> map3 = new HashMap<Number, String>();
209        map3.put(3, "reader");
210        listMap.add(map3);
211
212        final String uri1 = TestBackendConstants._TEST_SERVLET_URI_users + "00000000-0000-0000-0000-000000000111";
213        final String uri2 = TestBackendConstants._TEST_SERVLET_URI_users + "00000000-0000-0000-0000-000000000112";
214        final String uri3 = TestBackendConstants._TEST_SERVLET_URI_users + "00000000-0000-0000-0000-000000000113";
215
216
217        mockeryDao.checking(new Expectations() {
218            {
219                oneOf(annotationDao).getAnnotationWithoutTargetsAndPermissions(1);
220                will(returnValue(mockAnnotation));
221
222                oneOf(annotationDao).getOwner(1);
223                will(returnValue(1));
224
225                oneOf(userDao).getURIFromInternalID(1);
226                will(returnValue(uri1));
227
228                oneOf(annotationDao).retrieveTargetIDs(1);
229                will(returnValue(mockTargetIDs));
230
231                oneOf(targetDao).getTarget(1);
232                will(returnValue(mockTargetOne));
233
234                oneOf(targetDao).getTarget(2);
235                will(returnValue(mockTargetTwo));
236
237                /// getPermissionsForAnnotation
238
239                oneOf(annotationDao).getPermissions(1);
240                will(returnValue(listMap));
241
242                oneOf(userDao).getURIFromInternalID(2);
243                will(returnValue(uri2));
244
245                oneOf(userDao).getURIFromInternalID(3);
246                will(returnValue(uri3));
247            }
248        });
249
250        Annotation result = dbIntegrityService.getAnnotation(1);
251        assertEquals(TestBackendConstants._TEST_SERVLET_URI_annotations + "00000000-0000-0000-0000-000000000021", result.getURI());
252        assertEquals("text/plain", result.getBody().getTextBody().getMimeType());
253        assertEquals("<html><body>some html 1</body></html>", result.getBody().getTextBody().getBody());
254        assertEquals("Sagrada Famiglia", result.getHeadline());
255        assertEquals("2013-08-12T09:25:00.383000Z", result.getLastModified().toString());
256        assertEquals(TestBackendConstants._TEST_SERVLET_URI_users + "00000000-0000-0000-0000-000000000111", result.getOwnerRef());
257
258        assertEquals(mockTargetOne.getLink(), result.getTargets().getTargetInfo().get(0).getLink());
259        assertEquals(mockTargetOne.getURI(), result.getTargets().getTargetInfo().get(0).getRef());
260        assertEquals(mockTargetOne.getVersion(), result.getTargets().getTargetInfo().get(0).getVersion());
261        assertEquals(mockTargetTwo.getLink(), result.getTargets().getTargetInfo().get(1).getLink());
262        assertEquals(mockTargetTwo.getURI(), result.getTargets().getTargetInfo().get(1).getRef());
263        assertEquals(mockTargetTwo.getVersion(), result.getTargets().getTargetInfo().get(1).getVersion());
264
265        assertEquals(Permission.WRITER, result.getPermissions().getUserWithPermission().get(0).getPermission());
266        assertEquals(uri2, result.getPermissions().getUserWithPermission().get(0).getRef());
267
268        assertEquals(Permission.READER, result.getPermissions().getUserWithPermission().get(1).getPermission());
269        assertEquals(uri3, result.getPermissions().getUserWithPermission().get(1).getRef());
270    }
271
272    /**
273     * Test of getFilteredAnnotationIDs method, of class DBIntegrityServiceImlp.
274     */
275    @Test
276    public void testGetFilteredAnnotationIDs() {
277        System.out.println("test getFilteredAnnotationIDs");
278
279        final List<Number> mockTargetIDs = new ArrayList<Number>();
280        mockTargetIDs.add(1);
281        mockTargetIDs.add(2);
282
283        final List<Number> mockAnnotationIDs1 = new ArrayList<Number>();
284        mockAnnotationIDs1.add(1);
285
286        final List<Number> mockAnnotationIDs2 = new ArrayList<Number>();
287        mockAnnotationIDs2.add(1);
288        mockAnnotationIDs2.add(2);
289
290        final String after = (new Timestamp(0)).toString();
291        final String before = (new Timestamp(System.currentTimeMillis())).toString();
292
293        final List<Number> mockRetval = new ArrayList<Number>();
294        mockRetval.add(1);
295
296
297        mockeryDao.checking(new Expectations() {
298            {
299                oneOf(targetDao).getTargetsReferringTo("nl.wikipedia.org");
300                will(returnValue(mockTargetIDs));
301
302                oneOf(annotationDao).getAnnotationIDsForTargets(mockTargetIDs);
303                will(returnValue(mockAnnotationIDs2));
304
305                oneOf(annotationDao).getFilteredAnnotationIDs(null, "some html 1", null, after, before);
306                will(returnValue(mockAnnotationIDs1));
307
308                oneOf(annotationDao).getAnnotationIDsForUserWithPermission(3, "reader");
309                will(returnValue(mockAnnotationIDs1));
310
311
312
313            }
314        });
315
316
317        List result = dbIntegrityService.getFilteredAnnotationIDs(null, "nl.wikipedia.org", "some html 1", 3, "reader", null, after, before);
318        assertEquals(1, result.size());
319        assertEquals(1, result.get(0));
320    }
321
322    @Test
323    public void testGetAnnotationTargets() throws SQLException {
324        System.out.println("test getAnnotationTargets");
325        final List<Number> TargetIDs = new ArrayList<Number>();
326        TargetIDs.add(1);
327        TargetIDs.add(2);
328        mockeryDao.checking(new Expectations() {
329            {
330                oneOf(annotationDao).retrieveTargetIDs(1);
331                will(returnValue(TargetIDs));
332
333                oneOf(targetDao).getURIFromInternalID(1);
334                will(returnValue(TestBackendConstants._TEST_SERVLET_URI_Targets + "00000000-0000-0000-0000-000000000031"));
335
336                oneOf(targetDao).getURIFromInternalID(2);
337                will(returnValue(TestBackendConstants._TEST_SERVLET_URI_Targets + "00000000-0000-0000-0000-000000000032"));
338
339            }
340        });
341
342        ReferenceList result = dbIntegrityService.getAnnotationTargets(1);
343        assertEquals(2, result.getRef().size());
344        assertEquals(TestBackendConstants._TEST_SERVLET_URI_Targets + "00000000-0000-0000-0000-000000000031", result.getRef().get(0));
345        assertEquals(TestBackendConstants._TEST_SERVLET_URI_Targets + "00000000-0000-0000-0000-000000000032", result.getRef().get(1));
346
347    }
348
349//     @Override
350//    public AnnotationInfoList getFilteredAnnotationInfos(String link, String text, String access, String namespace, UUID
351//            owner, Timestamp after, Timestamp before){
352//        List<Number> annotationIDs = getFilteredAnnotationIDs(link, text, access, namespace, owner, after, before);
353//        List<AnnotationInfo> listAnnotationInfo = annotationDao.getAnnotationInfos(annotationIDs);
354    //       AnnotationInfoList result = new AnnotationInfoList();
355    //       result.getAnnotation().addAll(listAnnotationInfo);
356    //       return result;
357//    }
358    @Test
359    public void testGetFilteredAnnotationInfos() throws SQLException {
360        System.out.println("test getetFilteredAnnotationInfos");
361
362        final List<Number> mockTargetIDs = new ArrayList<Number>();
363        mockTargetIDs.add(1);
364        mockTargetIDs.add(2);
365
366        final List<Number> mockAnnotationIDs1 = new ArrayList<Number>();
367        mockAnnotationIDs1.add(1);
368
369        final List<Number> mockAnnotationIDs2 = new ArrayList<Number>();
370        mockAnnotationIDs2.add(1);
371        mockAnnotationIDs2.add(2);
372
373        final UUID ownerUUID = UUID.fromString("00000000-0000-0000-0000-000000000111");
374        final String after = (new Timestamp(0)).toString();
375        final String before = (new Timestamp(System.currentTimeMillis())).toString();
376
377        final List<Number> mockAnnotIDs = new ArrayList<Number>();
378        mockAnnotIDs.add(1);
379
380        final AnnotationInfo mockAnnotInfo = new AnnotationInfo();
381
382        mockAnnotInfo.setHeadline("Sagrada Famiglia");
383        mockAnnotInfo.setRef(TestBackendConstants._TEST_SERVLET_URI_annotations + "00000000-0000-0000-0000-000000000021");
384        mockAnnotInfo.setOwnerRef(TestBackendConstants._TEST_SERVLET_URI_users + "00000000-0000-0000-0000-000000000111");
385
386        final List<Number> targetIDs = new ArrayList<Number>();
387        targetIDs.add(1);
388        targetIDs.add(2);
389
390
391
392        mockeryDao.checking(new Expectations() {
393            {
394                oneOf(userDao).getInternalID(ownerUUID);
395                will(returnValue(1));
396
397                oneOf(targetDao).getTargetsReferringTo("nl.wikipedia.org");
398                will(returnValue(mockTargetIDs));
399
400                oneOf(annotationDao).getAnnotationIDsForTargets(mockTargetIDs);
401                will(returnValue(mockAnnotationIDs2));
402
403                oneOf(annotationDao).getFilteredAnnotationIDs(1, "some html 1", null, after, before);
404                will(returnValue(mockAnnotationIDs1));
405
406                oneOf(annotationDao).getAnnotationIDsForUserWithPermission(3, "reader");
407                will(returnValue(mockAnnotationIDs1));
408
409
410//                ///////////////////////////////////
411//               
412                oneOf(annotationDao).getAnnotationInfoWithoutTargets(1);
413                will(returnValue(mockAnnotInfo));
414
415                oneOf(annotationDao).getOwner(1);
416                will(returnValue(1));
417
418                oneOf(userDao).getURIFromInternalID(1);
419                will(returnValue(TestBackendConstants._TEST_SERVLET_URI_users + "00000000-0000-0000-0000-000000000111"));
420
421                ////
422                oneOf(annotationDao).retrieveTargetIDs(1);
423                will(returnValue(targetIDs));
424
425                oneOf(targetDao).getURIFromInternalID(1);
426                will(returnValue(TestBackendConstants._TEST_SERVLET_URI_Targets + "00000000-0000-0000-0000-000000000031"));
427
428
429                oneOf(targetDao).getURIFromInternalID(2);
430                will(returnValue(TestBackendConstants._TEST_SERVLET_URI_Targets + "00000000-0000-0000-0000-000000000032"));
431
432
433            }
434        });
435
436
437        AnnotationInfoList result = dbIntegrityService.getFilteredAnnotationInfos(ownerUUID, "nl.wikipedia.org", "some html 1", 3, "reader", null, after, before);
438        assertEquals(1, result.getAnnotationInfo().size());
439        AnnotationInfo resultAnnotInfo = result.getAnnotationInfo().get(0);
440        assertEquals(mockAnnotInfo.getHeadline(), resultAnnotInfo.getHeadline());
441        assertEquals(mockAnnotInfo.getRef(), resultAnnotInfo.getRef());
442        assertEquals(mockAnnotInfo.getOwnerRef(), resultAnnotInfo.getOwnerRef());
443        assertEquals(mockAnnotInfo.getRef(), result.getAnnotationInfo().get(0).getRef());
444        assertEquals(TestBackendConstants._TEST_SERVLET_URI_Targets + "00000000-0000-0000-0000-000000000031", resultAnnotInfo.getTargets().getRef().get(0));
445        assertEquals(TestBackendConstants._TEST_SERVLET_URI_Targets + "00000000-0000-0000-0000-000000000032", resultAnnotInfo.getTargets().getRef().get(1));
446
447    }
448
449    @Test
450    public void testGetTargetsWithNoCachedRepresentation() {
451        System.out.println("test getTargetsWithNoCachedRepresentation");
452        final List<Number> targetIDs = new ArrayList<Number>();
453        targetIDs.add(5);
454        targetIDs.add(7);
455
456        final List<Number> cachedIDs5 = new ArrayList<Number>();
457        cachedIDs5.add(7);
458        final List<Number> cachedIDs7 = new ArrayList<Number>();
459
460
461
462        mockeryDao.checking(new Expectations() {
463            {
464                oneOf(annotationDao).retrieveTargetIDs(3);
465                will(returnValue(targetIDs));
466
467                oneOf(targetDao).getCachedRepresentations(5);
468                will(returnValue(cachedIDs5));
469
470                oneOf(targetDao).getCachedRepresentations(7);
471                will(returnValue(cachedIDs7));
472
473                oneOf(targetDao).getURIFromInternalID(7);
474                will(returnValue("00000000-0000-0000-0000-000000000037"));
475
476            }
477        });
478
479        List<String> result = dbIntegrityService.getTargetsWithNoCachedRepresentation(3);
480        assertEquals(1, result.size());
481        assertEquals("00000000-0000-0000-0000-000000000037", result.get(0)); // Target number 7 has no cached
482    }
483
484    ////////////// ADDERS /////////////////////////
485    /**
486     * Test of addCachedForVersion method, of class DBIntegrityServiceImlp.
487     */
488    @Test
489    public void testAddCached() throws SerialException, SQLException {
490        System.out.println("addCached");
491        String mime = "text/html";
492        String type = "text";
493        String tool = "latex";
494        String externalID = UUID.randomUUID().toString();
495        final CachedRepresentationInfo newCachedInfo = new CachedRepresentationInfo();
496        newCachedInfo.setMimeType(mime);
497        newCachedInfo.setType(type);
498        newCachedInfo.setTool(tool);
499        newCachedInfo.setURI(TestBackendConstants._TEST_SERVLET_URI_cached + externalID);
500
501        String blobString = "aaa";
502        byte[] blobBytes = blobString.getBytes();
503        final ByteArrayInputStream newCachedBlob = new ByteArrayInputStream(blobBytes);
504
505        mockeryDao.checking(new Expectations() {
506            {
507
508                oneOf(cachedRepresentationDao).getInternalIDFromURI(newCachedInfo.getURI());
509                will(returnValue(null));
510
511                oneOf(cachedRepresentationDao).addCachedRepresentation(newCachedInfo, newCachedBlob);
512                will(returnValue(8));
513
514                one(targetDao).addTargetCachedRepresentation(1, 8, "#(1,2)");
515                will(returnValue(1));
516
517            }
518        });
519
520
521        Number[] result = dbIntegrityService.addCachedForTarget(1, "#(1,2)", newCachedInfo, newCachedBlob);
522        assertEquals(2, result.length);
523        assertEquals(1, result[0]);
524        assertEquals(8, result[1]);
525    }
526
527    /**
528     * Test of updateSiblingTargetClassForTarget method, of class
529     * DBIntegrityServiceImlp.
530     *
531     *
532     */
533    /**
534     * Test of addTargetsForAnnotation method, of class DBIntegrityServiceImlp.
535     */
536    @Test
537    public void testAddTargetsForAnnotation() throws Exception {
538        System.out.println("test addTargetsForAnnotation");
539
540        // test 1: adding an existing target
541        TargetInfo testTargetOne = new TargetInfo();
542        testTargetOne.setLink("http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia");
543        testTargetOne.setRef(TestBackendConstants._TEST_SERVLET_URI_Targets + "00000000-0000-0000-0000-000000000031");
544        testTargetOne.setVersion("version 1.0");
545        final List<TargetInfo> mockTargetListOne = new ArrayList<TargetInfo>();
546        mockTargetListOne.add(testTargetOne);
547
548        mockeryDao.checking(new Expectations() {
549            {
550                oneOf(targetDao).getInternalIDFromURI(mockTargetListOne.get(0).getRef());
551                will(returnValue(1));
552
553                oneOf(annotationDao).addAnnotationTarget(4, 1);
554                will(returnValue(1));
555            }
556        });
557
558        Map<String, String> result = dbIntegrityService.addTargetsForAnnotation(4, mockTargetListOne);
559        assertEquals(0, result.size());
560
561        // test 2: adding a new Target
562        TargetInfo testTargetTwo = new TargetInfo();
563        final String tempTargetID = UUID.randomUUID().toString();
564        testTargetTwo.setRef(TestBackendConstants._TEST_SERVLET_URI_Targets + tempTargetID);
565        testTargetTwo.setLink("http://www.sagradafamilia.cat/docs_instit/historia.php");
566        testTargetTwo.setVersion("version 1.0");
567        final List<TargetInfo> mockTargetListTwo = new ArrayList<TargetInfo>();
568        mockTargetListTwo.add(testTargetTwo);
569
570        final UUID mockNewTargetUUID = UUID.randomUUID();
571
572        mockeryDao.checking(new Expectations() {
573            {
574                oneOf(targetDao).getInternalIDFromURI(mockTargetListTwo.get(0).getRef());
575                will(returnValue(null));
576
577                oneOf(targetDao).addTarget(with(aNonNull(Target.class)));
578                will(returnValue(8)); //# the next new number is 8, we have already 7 Targets
579
580                oneOf(targetDao).stringURItoExternalID(mockTargetListTwo.get(0).getRef());
581                will(returnValue(tempTargetID));
582
583                oneOf(targetDao).getExternalID(8);
584                will(returnValue(mockNewTargetUUID));
585
586                oneOf(annotationDao).addAnnotationTarget(1, 8);
587                will(returnValue(1));
588
589            }
590        });
591
592        Map<String, String> resultTwo = dbIntegrityService.addTargetsForAnnotation(1, mockTargetListTwo);
593        assertEquals(1, resultTwo.size());
594        assertEquals(mockNewTargetUUID.toString(), resultTwo.get(tempTargetID));
595
596    }
597
598    /**
599     * Test of addUsersAnnotation method, of class DBIntegrityServiceImlp.
600     */
601    @Test
602    public void testAddUsersAnnotation() throws Exception {
603        System.out.println("test addUsersAnnotation");
604
605        // expectations for addUsersannotation itself
606        final Annotation testAnnotation = testInstances.getAnnotationToAdd();
607
608        mockeryDao.checking(new Expectations() {
609            {
610                oneOf(annotationDao).addAnnotation(testAnnotation, 3);
611                will(returnValue(5)); // the next free number is 5
612
613                //  expectations for addTargetsForannotation
614                oneOf(targetDao).getInternalIDFromURI(TestBackendConstants._TEST_SERVLET_URI_Targets + "00000000-0000-0000-0000-000000000031");
615                will(returnValue(1));
616
617                oneOf(annotationDao).addAnnotationTarget(5, 1);
618                will(returnValue(1));
619
620                ///////////
621
622                oneOf(annotationDao).updateAnnotationBody(5, testAnnotation.getBody().getTextBody().getBody(), testAnnotation.getBody().getTextBody().getMimeType(), false);
623                will(returnValue(1)); // the DB update will be called at perform anyway, even if the body is not changed (can be optimized)
624
625
626            }
627        });
628
629        Number result = dbIntegrityService.addUsersAnnotation(3, testAnnotation);
630        assertEquals(5, result);
631    }
632
633    @Test
634    public void testAddUser() {
635        System.out.println("test addUser");
636        final User freshUser = new User();
637        freshUser.setDisplayName("Guilherme");
638        freshUser.setEMail("Guilherme.Silva@mpi.nl");
639        mockeryDao.checking(new Expectations() {
640            {
641                oneOf(userDao).userExists(freshUser);
642                will(returnValue(false));
643
644                oneOf(userDao).addUser(freshUser, "guisil@mpi.nl");
645                will(returnValue(11));
646            }
647        });
648
649
650        assertEquals(11, dbIntegrityService.addUser(freshUser, "guisil@mpi.nl").intValue());
651
652        /// user already exists
653        final User user = new User();
654        freshUser.setDisplayName("Olha");
655        freshUser.setEMail("Olha.Shakaravska@mpi.nl");
656        mockeryDao.checking(new Expectations() {
657            {
658                oneOf(userDao).userExists(user);
659                will(returnValue(true));
660
661            }
662        });
663
664        assertTrue(null == dbIntegrityService.addUser(user, "olhsha@mpi.nl"));
665    }
666
667    //////////////////// DELETERS ////////////////
668    @Test
669    public void testDeleteUser() {
670        System.out.println("test deleteUser");
671
672        mockeryDao.checking(new Expectations() {
673            {
674                oneOf(userDao).deleteUser(1);
675                will(returnValue(0));
676
677                oneOf(userDao).deleteUser(3);
678                will(returnValue(0));
679
680                oneOf(userDao).deleteUser(10);
681                will(returnValue(1));
682
683            }
684        });
685
686        assertEquals(0, dbIntegrityService.deleteUser(1));
687        assertEquals(0, dbIntegrityService.deleteUser(3));
688        assertEquals(1, dbIntegrityService.deleteUser(10));
689    }
690
691    /**
692     * Test of deleteCachedForVersion method, of class DBIntegrityServiceImlp.
693     */
694    @Test
695    public void testDeleteCachedRepresentationForTarget() throws SQLException {
696        System.out.println("test deleteCachedRepresentationForTarget");
697        mockeryDao.checking(new Expectations() {
698            {
699                oneOf(targetDao).deleteTargetCachedRepresentation(5, 7);
700                will(returnValue(1));
701
702                oneOf(cachedRepresentationDao).deleteCachedRepresentation(7);
703                will(returnValue(1)); // cached is used by another version
704
705            }
706        });
707
708        int[] result = dbIntegrityService.deleteCachedRepresentationOfTarget(5, 7);
709        assertEquals(2, result.length);
710        assertEquals(1, result[0]);
711        assertEquals(1, result[1]);
712    }
713
714    /////////////////////////////////////////////
715    @Test
716    public void testDeleteAllCachedRepresentationsOfTarget() throws SQLException {
717        System.out.println("test deleteAllCachedRepresentationsOfTarget");
718        final List<Number> cachedList = new ArrayList<Number>();
719        cachedList.add(1);
720        cachedList.add(2);
721
722        mockeryDao.checking(new Expectations() {
723            {
724                oneOf(targetDao).getCachedRepresentations(1);
725                will(returnValue(cachedList));
726
727                oneOf(targetDao).deleteTargetCachedRepresentation(1, 1);
728                will(returnValue(1));
729
730                oneOf(cachedRepresentationDao).deleteCachedRepresentation(1);
731                will(returnValue(1));
732
733                oneOf(targetDao).deleteTargetCachedRepresentation(1, 2);
734                will(returnValue(1));
735
736                oneOf(cachedRepresentationDao).deleteCachedRepresentation(2);
737                will(returnValue(1));
738
739            }
740        });
741
742        int[] result = dbIntegrityService.deleteAllCachedRepresentationsOfTarget(1);
743        assertEquals(2, result[0]); // # affected rows in Targets_cacheds
744        assertEquals(2, result[1]); // # affected rows in cacheds
745    }
746
747    /**
748     * Test of deleteAnnotationWithTargets method, of class
749     * DBIntegrityServiceImlp.
750     */
751    @Test
752    public void testDeleteAnnotation() throws Exception {
753        System.out.println("deleteAnnotation ");
754
755        // deleting annotation 3, which has its target Target 2  (used by annotation # 1)
756        final List<Number> mockTargetIDs = new ArrayList<Number>();
757        mockTargetIDs.add(2);
758
759        final List<Number> mockCachedIDs = new ArrayList<Number>();
760        mockCachedIDs.add(3);
761
762        mockeryDao.checking(new Expectations() {
763            {
764                oneOf(annotationDao).deleteAnnotationPrincipalPermissions(2);
765                will(returnValue(2));
766
767                oneOf(annotationDao).retrieveTargetIDs(2);
768                will(returnValue(mockTargetIDs));
769
770                oneOf(annotationDao).deleteAllAnnotationTarget(2);
771                will(returnValue(1));
772
773                oneOf(annotationDao).deleteAnnotation(2);
774                will(returnValue(1));
775
776                oneOf(targetDao).getCachedRepresentations(2);
777                will(returnValue(mockCachedIDs));
778
779                oneOf(targetDao).deleteTargetCachedRepresentation(2, 3);
780                will(returnValue(1));
781
782                oneOf(cachedRepresentationDao).deleteCachedRepresentation(3);
783                will(returnValue(1));
784
785                oneOf(targetDao).deleteTarget(2);
786                will(returnValue(1));
787
788
789            }
790        });
791        int[] result = dbIntegrityService.deleteAnnotation(2);// the Target will be deleted because it is not referred by any annotation
792        assertEquals(4, result.length);
793        assertEquals(1, result[0]); // annotation 3 is deleted
794        assertEquals(2, result[1]); // 2 rows in "annotation principal permissions are deleted"
795        assertEquals(1, result[2]);  // row (3,2) in "annotations_Targets" is deleted
796        assertEquals(1, result[3]); //  Target 3 is deleted
797    }
798//    @Test
799//    public void testCreateTarget(){ 
800//        NewTargetInfo newTargetInfo = new NewTargetInfo();
801//        newTargetInfo.setLink(TestBackendConstants._TEST_NEW_Target_LINK);
802//        newTargetInfo.setVersion(null);
803//       
804//        Target result = dbIntegrityService.createTarget(newTargetInfo);
805//        assertEquals(TestBackendConstants._TEST_NEW_Target_LINK, result.getLink());
806//        assertFalse(null == result.getURI());
807//       
808//    }
809//   
810//    @Test
811//    public void testCreateVersion(){ 
812//        NewTargetInfo newTargetInfo = new NewTargetInfo();
813//        newTargetInfo.setLink(TestBackendConstants._TEST_NEW_Target_LINK);
814//        newTargetInfo.setVersion(null);
815//       
816//        Version result = dbIntegrityService.createVersion(newTargetInfo);
817//        assertFalse(null == result.getVersion()); // will be chnaged once the schema for version is fixed: ID is added
818//       
819//    }
820
821    /**
822     * NOTEBOOKS
823     */
824    /**
825     * Getters
826     */
827    /// notebooks ///
828    //public NotebookInfoList getNotebooks(Number prinipalID, Permission permission);
829//     NotebookInfoList result = new NotebookInfoList();
830//        List<Number> notebookIDs = notebookDao.getNotebookIDs(prinipalID, permission);
831//        for (Number notebookID : notebookIDs) {
832//            NotebookInfo notebookInfo = notebookDao.getNotebookInfoWithoutOwner(notebookID);
833//            Number ownerID = notebookDao.getOwner(notebookID);
834//            notebookInfo.setOwnerRef(userDao.getURIFromInternalID(ownerID));
835//            result.getNotebookInfo().add(notebookInfo);
836//        }
837//
838//        return result;
839    @Test
840    public void testGetNotebooks() {
841
842        final List<Number> mockNotebookIDs = new ArrayList<Number>();
843        mockNotebookIDs.add(1);
844
845        final NotebookInfo mockNotebookInfo = new NotebookInfo();
846        mockNotebookInfo.setRef("00000000-0000-0000-0000-000000000011");
847        mockNotebookInfo.setTitle("Notebook 1");
848
849        mockeryDao.checking(new Expectations() {
850            {
851                oneOf(notebookDao).getNotebookIDs(3, Permission.READER);
852                will(returnValue(mockNotebookIDs));
853
854                oneOf(notebookDao).getNotebookInfoWithoutOwner(1);
855                will(returnValue(mockNotebookInfo));
856
857                oneOf(notebookDao).getOwner(1);
858                will(returnValue(1));
859
860                oneOf(userDao).getURIFromInternalID(1);
861                will(returnValue("00000000-0000-0000-0000-000000000111"));
862
863            }
864        });
865
866        NotebookInfoList result = dbIntegrityService.getNotebooks(3, Permission.READER);
867        assertEquals("00000000-0000-0000-0000-000000000011", result.getNotebookInfo().get(0).getRef());
868        assertEquals("00000000-0000-0000-0000-000000000111", result.getNotebookInfo().get(0).getOwnerRef());
869        assertEquals("Notebook 1", result.getNotebookInfo().get(0).getTitle());
870
871    }
872
873    ;
874   
875
876    /*public NotebookInfoList getNotebooksOwnedBy(Number principalID){
877        NotebookInfoList result = new NotebookInfoList();
878        List<Number> notebookIDs = notebookDao.getNotebookIDsOwnedBy(principalID);
879        String ownerRef = userDao.getURIFromInternalID(principalID);
880        for (Number notebookID : notebookIDs) {
881            NotebookInfo notebookInfo = notebookDao.getNotebookInfoWithoutOwner(notebookID);
882            notebookInfo.setOwnerRef(ownerRef);
883            result.getNotebookInfo().add(notebookInfo);
884        }
885
886        return result;
887    }*/
888   
889    @Test
890    public void testGetNotebooksOwnedBy() {
891
892        final List<Number> mockNotebookIDs = new ArrayList<Number>();
893        mockNotebookIDs.add(3);
894        mockNotebookIDs.add(4);
895
896        final NotebookInfo mockNotebookInfo1 = new NotebookInfo();
897        mockNotebookInfo1.setRef("00000000-0000-0000-0000-000000000013");
898        mockNotebookInfo1.setTitle("Notebook 3");
899
900        final NotebookInfo mockNotebookInfo2 = new NotebookInfo();
901        mockNotebookInfo2.setRef("00000000-0000-0000-0000-000000000014");
902        mockNotebookInfo2.setTitle("Notebook 4");
903
904        mockeryDao.checking(new Expectations() {
905            {
906                oneOf(notebookDao).getNotebookIDsOwnedBy(3);
907                will(returnValue(mockNotebookIDs));
908
909                oneOf(userDao).getURIFromInternalID(3);
910                will(returnValue("00000000-0000-0000-0000-000000000113"));
911
912                oneOf(notebookDao).getNotebookInfoWithoutOwner(3);
913                will(returnValue(mockNotebookInfo1));
914
915                oneOf(notebookDao).getNotebookInfoWithoutOwner(4);
916                will(returnValue(mockNotebookInfo2));
917
918            }
919        });
920
921        NotebookInfoList result = dbIntegrityService.getNotebooksOwnedBy(3);
922        assertEquals("00000000-0000-0000-0000-000000000013", result.getNotebookInfo().get(0).getRef());
923        assertEquals("00000000-0000-0000-0000-000000000113", result.getNotebookInfo().get(0).getOwnerRef());
924        assertEquals("Notebook 3", result.getNotebookInfo().get(0).getTitle());
925        assertEquals("00000000-0000-0000-0000-000000000014", result.getNotebookInfo().get(1).getRef());
926        assertEquals("00000000-0000-0000-0000-000000000113", result.getNotebookInfo().get(1).getOwnerRef());
927        assertEquals("Notebook 4", result.getNotebookInfo().get(1).getTitle());
928
929    }
930
931    ;
932
933
934    /*
935     public List<UUID> getPrincipals(Number notebookID, Permission permission) {
936        List<UUID> result = new ArrayList<UUID>();
937        List<Number> principalIDs = notebookDao.getPrincipalIDsWithPermission(notebookID, permission);
938        for (Number principalID : principalIDs) {
939            UUID uuid = userDao.getExternalID(principalID);
940            result.add(uuid);
941        }
942        return result;
943    }*/
944   
945    @Test
946    public void testGetPrincipals() {
947        final List<Number> mockPrincipalIDs = new ArrayList<Number>();
948        mockPrincipalIDs.add(2);
949        mockPrincipalIDs.add(4);
950
951        final NotebookInfo mockNotebookInfo = new NotebookInfo();
952        mockNotebookInfo.setRef("00000000-0000-0000-0000-000000000011");
953        mockNotebookInfo.setTitle("Notebook 1");
954
955        mockeryDao.checking(new Expectations() {
956            {
957                oneOf(notebookDao).getPrincipalIDsWithPermission(1, Permission.WRITER);
958                will(returnValue(mockPrincipalIDs));
959
960                oneOf(userDao).getExternalID(2);
961                will(returnValue(UUID.fromString("00000000-0000-0000-0000-000000000112")));
962               
963                 oneOf(userDao).getExternalID(4);
964                will(returnValue(UUID.fromString("00000000-0000-0000-0000-000000000114")));
965               
966
967            }
968        });
969
970        List<UUID> result = dbIntegrityService.getPrincipals(1, Permission.WRITER);
971        assertEquals("00000000-0000-0000-0000-000000000112", result.get(0).toString());
972        assertEquals("00000000-0000-0000-0000-000000000114", result.get(1).toString());
973
974    }
975   
976   
977//    public NotebookInfo getNotebookInfo(Number notebookID) {
978//        NotebookInfo result = notebookDao.getNotebookInfoWithoutOwner(notebookID);
979//        result.setOwnerRef(userDao.getURIFromInternalID(notebookDao.getOwner(notebookID)));
980//        return result;
981//    }
982   
983 
984    @Test
985    public void testGetNotebookInfo() {
986
987        final NotebookInfo mockNotebookInfo = new NotebookInfo();
988        mockNotebookInfo.setRef("00000000-0000-0000-0000-000000000011");
989        mockNotebookInfo.setTitle("Notebook 1");
990
991        mockeryDao.checking(new Expectations() {
992            {
993                oneOf(notebookDao).getNotebookInfoWithoutOwner(1);
994                will(returnValue(mockNotebookInfo));
995               
996                oneOf(notebookDao).getOwner(1);
997                will(returnValue(1));
998
999                oneOf(userDao).getURIFromInternalID(1);
1000                will(returnValue("00000000-0000-0000-0000-000000000111"));
1001               
1002
1003            }
1004        });
1005
1006        NotebookInfo result = dbIntegrityService.getNotebookInfo(1);
1007        assertEquals("00000000-0000-0000-0000-000000000011", result.getRef());
1008        assertEquals("00000000-0000-0000-0000-000000000111", result.getOwnerRef());
1009        assertEquals("Notebook 1", result.getTitle());
1010
1011    }
1012   
1013   
1014//    public List<UUID> getAnnotationsForNotebook(Number notebookID, int startAnnotation, int maximumAnnotations, String orderedBy, boolean desc) {
1015//        List<Number> annotationIDs = notebookDao.getAnnotations(notebookID);
1016//
1017//        if (startAnnotation < -1) {
1018//            logger.info("Variable's startAnnotation value " + startAnnotation + " is invalid. I will return null.");
1019//            return null;
1020//        }
1021//
1022//        if (maximumAnnotations < -1) {
1023//            logger.info("Variable's maximumAnnotations value " + maximumAnnotations + " is invalid. I will return null.");
1024//            return null;
1025//        }
1026//
1027//        int offset = (startAnnotation > 0) ? startAnnotation - 1 : 0;
1028//        String direction = desc ? " DESC " : " ASC ";
1029//        List<Number> selectedAnnotIDs = annotationDao.sublistOrderedAnnotationIDs(annotationIDs, offset, maximumAnnotations, orderedBy, direction);
1030//        List<UUID> annotationUUIDs = new ArrayList<UUID>();
1031//        for (Number annotationID : selectedAnnotIDs) {
1032//            annotationUUIDs.add(annotationDao.getExternalID(annotationID));
1033//        }
1034//        return annotationUUIDs;
1035//    }
1036   
1037   
1038    @Test
1039    public void testAnnotationsForNotebook() {
1040        final List<Number> mockAnnotationIDs = new ArrayList<Number>();
1041        mockAnnotationIDs.add(1);
1042        mockAnnotationIDs.add(2);
1043
1044        mockeryDao.checking(new Expectations() {
1045            {
1046                oneOf(notebookDao).getAnnotations(1);
1047                will(returnValue(mockAnnotationIDs));
1048               
1049                oneOf(annotationDao).sublistOrderedAnnotationIDs(mockAnnotationIDs, 0, 3, "last_modified", "DESC");
1050                will(returnValue(mockAnnotationIDs));
1051
1052                oneOf(annotationDao).getExternalID(1);
1053                will(returnValue(UUID.fromString("00000000-0000-0000-0000-000000000021")));
1054               
1055                oneOf(annotationDao).getExternalID(2);
1056                will(returnValue(UUID.fromString("00000000-0000-0000-0000-000000000022")));
1057               
1058
1059            }
1060        });
1061
1062        List<UUID> result = dbIntegrityService.getAnnotationsForNotebook(1, -1, 3, "last_modified", true);
1063        assertEquals("00000000-0000-0000-0000-000000000021", result.get(0).toString());
1064        assertEquals("00000000-0000-0000-0000-000000000022", result.get(1).toString());
1065
1066    }
1067   
1068   
1069    /**
1070 * Updaters
1071 */
1072 
1073//    public boolean updateNotebookMetadata(Number notebookID, NotebookInfo upToDateNotebookInfo) {
1074//        Number ownerID = userDao.getInternalIDFromURI(upToDateNotebookInfo.getOwnerRef());
1075//        return notebookDao.updateNotebookMetadata(notebookID, upToDateNotebookInfo.getTitle(), ownerID);
1076//    }
1077   
1078   
1079    @Test
1080    public void testUpdateNotebookMetadata() {       
1081       
1082        final NotebookInfo mockNotebookInfo= new NotebookInfo();
1083        mockNotebookInfo.setOwnerRef(TestBackendConstants._TEST_SERVLET_URI_users+"00000000-0000-0000-0000-000000000113");
1084        mockNotebookInfo.setTitle("New Title");
1085
1086        mockeryDao.checking(new Expectations() {
1087            {
1088                oneOf(userDao).getInternalIDFromURI(TestBackendConstants._TEST_SERVLET_URI_users+"00000000-0000-0000-0000-000000000113");
1089                will(returnValue(3));
1090               
1091                oneOf(notebookDao).updateNotebookMetadata(1, "New Title", 3);
1092                will(returnValue(true));
1093            }
1094        });
1095
1096        boolean result = dbIntegrityService.updateNotebookMetadata(1, mockNotebookInfo);
1097        assertTrue(result);
1098    } 
1099   
1100
1101//
1102//    public boolean addAnnotationToNotebook(Number notebookID, Number annotationID) {
1103//        return notebookDao.addAnnotationToNotebook(notebookID, annotationID);
1104//    }
1105
1106    @Test
1107    public void testAddAnnotationToNotebook() {       
1108       
1109        mockeryDao.checking(new Expectations() {
1110            {
1111               
1112                oneOf(notebookDao).addAnnotationToNotebook(1, 3);
1113                will(returnValue(true));
1114            }
1115        });
1116
1117        assertTrue(dbIntegrityService.addAnnotationToNotebook(1,3));
1118    } 
1119   
1120   
1121/**
1122 * Adders
1123 */
1124
1125//    public Number createNotebook(Notebook notebook, Number ownerID) {
1126//        Number notebookID = notebookDao.createNotebookWithoutPermissionsAndAnnotations(notebook, ownerID);
1127//        boolean updateOwner = notebookDao.setOwner(notebookID, ownerID);
1128//        List<UserWithPermission> permissions = notebook.getPermissions().getUserWithPermission();
1129//        for (UserWithPermission principalPermission : permissions) {
1130//            Number principalID = userDao.getInternalIDFromURI(principalPermission.getRef());
1131//            Permission permission = principalPermission.getPermission();
1132//            boolean updatePermissions = notebookDao.addPermissionToNotebook(notebookID, principalID, permission);
1133//        }
1134//        return notebookID;
1135//    }
1136
1137   
1138    @Test
1139    public void testCreateNotebook() {
1140       
1141        final Notebook notebook = new Notebook();
1142        notebook.setOwnerRef("tmpXXX");
1143        notebook.setTitle("(Almost) Copy of Notebook 1");
1144        notebook.setURI("tmpYYY");       
1145       
1146        UserWithPermissionList permissions = new UserWithPermissionList();
1147        UserWithPermission p1 = new UserWithPermission();
1148        p1.setPermission(Permission.WRITER);
1149        p1.setRef(TestBackendConstants._TEST_SERVLET_URI_users + "00000000-0000-0000-0000-000000000112");
1150        permissions.getUserWithPermission().add(p1);
1151        UserWithPermission p2 = new UserWithPermission();
1152        p2.setPermission(Permission.READER);
1153        p2.setRef(TestBackendConstants._TEST_SERVLET_URI_users + "00000000-0000-0000-0000-000000000113");
1154        permissions.getUserWithPermission().add(p2);
1155        notebook.setPermissions(permissions);
1156       
1157        mockeryDao.checking(new Expectations() {
1158            {
1159                oneOf(notebookDao).createNotebookWithoutPermissionsAndAnnotations(notebook, 1);
1160                will(returnValue(5));
1161               
1162                oneOf(notebookDao).setOwner(5, 1);
1163                will(returnValue(true));
1164               
1165                oneOf(userDao).getInternalIDFromURI(TestBackendConstants._TEST_SERVLET_URI_users + "00000000-0000-0000-0000-000000000112");
1166                will(returnValue(2));
1167               
1168                oneOf(userDao).getInternalIDFromURI(TestBackendConstants._TEST_SERVLET_URI_users + "00000000-0000-0000-0000-000000000113");
1169                will(returnValue(3));
1170               
1171               oneOf(notebookDao).addPermissionToNotebook(5, 2, Permission.WRITER);
1172               will(returnValue(true));
1173               
1174                oneOf(notebookDao).addPermissionToNotebook(5, 3, Permission.READER);
1175               will(returnValue(true));
1176               
1177            }
1178        });
1179
1180        Number result = dbIntegrityService.createNotebook(notebook, 1);
1181        assertEquals(5, result);
1182
1183    }
1184     
1185   
1186   
1187   
1188//    public boolean createAnnotationInNotebook(Number notebookID, Annotation annotation, Number ownerID) {
1189//        Number newAnnotationID = this.addUsersAnnotation(ownerID, annotation);
1190//        return notebookDao.addAnnotationToNotebook(notebookID, newAnnotationID);
1191//    }
1192   
1193    @Test
1194    public void testCreateAnnotationInNotebook() {
1195       
1196        final Annotation testAnnotation = testInstances.getAnnotationToAdd();
1197       
1198        mockeryDao.checking(new Expectations() {
1199            {
1200                oneOf(annotationDao).addAnnotation(testAnnotation, 3);
1201                will(returnValue(5)); // the next free number is 5
1202
1203                //  expectations for addTargetsForannotation
1204                oneOf(targetDao).getInternalIDFromURI(TestBackendConstants._TEST_SERVLET_URI_Targets + "00000000-0000-0000-0000-000000000031");
1205                will(returnValue(1));
1206
1207                oneOf(annotationDao).addAnnotationTarget(5, 1);
1208                will(returnValue(1));
1209
1210                oneOf(annotationDao).updateAnnotationBody(5, testAnnotation.getBody().getTextBody().getBody(), testAnnotation.getBody().getTextBody().getMimeType(), false);
1211                will(returnValue(1)); // the DB update will be called at perform anyway, even if the body is not changed (can be optimized)
1212
1213                /////////////////////////
1214               
1215                oneOf(notebookDao).addAnnotationToNotebook(1, 5);
1216                will(returnValue(true));
1217            }
1218        });
1219
1220        assertTrue(dbIntegrityService.createAnnotationInNotebook(1, testAnnotation, 3));
1221
1222    }   
1223
1224/**
1225 * Deleters
1226 */
1227   
1228//      public boolean deleteNotebook(Number notebookID) {
1229//        if (notebookDao.deleteAllPermissionsForNotebook(notebookID) || notebookDao.deleteAllAnnotationsFromNotebook(notebookID)) {
1230//            return notebookDao.deleteNotebook(notebookID);
1231//        } else {
1232//            return false;
1233//        }
1234//   
1235//   }
1236   
1237    @Test
1238    public void testDeleteNotebook() {       
1239       
1240        mockeryDao.checking(new Expectations() {
1241            {
1242               
1243                oneOf(notebookDao).deleteAllPermissionsForNotebook(1);
1244                will(returnValue(true));
1245               
1246                oneOf(notebookDao).deleteAllAnnotationsFromNotebook(1);
1247                will(returnValue(true));
1248               
1249                oneOf(notebookDao).deleteNotebook(1);
1250                will(returnValue(true));
1251            }
1252        });
1253
1254        assertTrue(dbIntegrityService.deleteNotebook(1));
1255    } 
1256   
1257}
Note: See TracBrowser for help on using the repository browser.