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

Last change on this file since 4028 was 4028, checked in by olhsha, 11 years ago

updating body of the annotation. Testing Get methods and verifying their produced xml-s. Ok.

File size: 35.8 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.TestBackendConstants;
22import eu.dasish.annotation.backend.TestInstances;
23import eu.dasish.annotation.backend.dao.AnnotationDao;
24import eu.dasish.annotation.backend.dao.CachedRepresentationDao;
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.Permission;
34import eu.dasish.annotation.schema.ReferenceList;
35import eu.dasish.annotation.schema.Target;
36import eu.dasish.annotation.schema.TargetInfo;
37import eu.dasish.annotation.schema.User;
38import java.io.ByteArrayInputStream;
39import java.sql.SQLException;
40import java.sql.Timestamp;
41import java.util.ArrayList;
42import java.util.HashMap;
43import java.util.List;
44import java.util.Map;
45import java.util.UUID;
46import javax.sql.rowset.serial.SerialBlob;
47import javax.sql.rowset.serial.SerialException;
48import javax.xml.datatype.XMLGregorianCalendar;
49import org.jmock.Expectations;
50import org.jmock.Mockery;
51import org.junit.Test;
52import static org.junit.Assert.*;
53import org.junit.runner.RunWith;
54import org.springframework.beans.factory.annotation.Autowired;
55import org.springframework.test.context.ContextConfiguration;
56import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
57
58/**
59 *
60 * @author olhsha
61 */
62@RunWith(SpringJUnit4ClassRunner.class)
63@ContextConfiguration({"/spring-test-config/dataSource.xml", "/spring-test-config/mockeryDao.xml", "/spring-test-config/mockAnnotationDao.xml",
64    "/spring-test-config/mockUserDao.xml", "/spring-test-config/mockTargetDao.xml", "/spring-test-config/mockCachedRepresentationDao.xml", 
65    "/spring-config/dbIntegrityService.xml"})
66public class DBIntegrityServiceTest {
67
68    @Autowired
69    private DBIntegrityServiceImlp dbIntegrityService;
70    @Autowired
71    private Mockery mockeryDao;
72    @Autowired
73    private UserDao userDao;
74    @Autowired
75    private CachedRepresentationDao cachedRepresentationDao;   
76    @Autowired
77    private TargetDao targetDao;
78    @Autowired
79    private AnnotationDao annotationDao;
80    TestInstances testInstances = new TestInstances();
81
82    public DBIntegrityServiceTest() {
83    }
84
85    ///////// GETTERS /////////////
86    /**
87     * Test of getAnnotationInternalIdentifier method, of class
88     * DBIntegrityServiceImlp.
89     */
90    @Test
91   
92    public void testGetAnnotationInternalIdentifier() {
93        System.out.println("getAnnotationInternalIdentifier");
94        final UUID externalID = UUID.fromString(TestBackendConstants._TEST_ANNOT_2_EXT);
95
96        mockeryDao.checking(new Expectations() {
97            {
98                oneOf(annotationDao).getInternalID(externalID);
99                will(returnValue(2));
100            }
101        });
102        assertEquals(2, dbIntegrityService.getAnnotationInternalIdentifier(externalID));
103    }
104
105    /**
106     * Test of getAnnotationExternalIdentifier method, of class
107     * DBIntegrityServiceImlp.
108     */
109    @Test
110   
111    public void testGetAnnotationExternalIdentifier() {
112        System.out.println("getAnnotationExternalIdentifier");
113        final UUID externalID = UUID.fromString(TestBackendConstants._TEST_ANNOT_2_EXT);
114
115        mockeryDao.checking(new Expectations() {
116            {
117                oneOf(annotationDao).getExternalID(2);
118                will(returnValue(externalID));
119            }
120        });
121        assertEquals(TestBackendConstants._TEST_ANNOT_2_EXT, dbIntegrityService.getAnnotationExternalIdentifier(2).toString());
122    }
123
124    /**
125     * Test of getUserInternalIdentifier method, of class
126     * DBIntegrityServiceImlp.
127     */
128    @Test
129   
130    public void testGetUserInternalIdentifier() {
131        System.out.println("getUserInternalIdentifier");
132
133        final UUID externalID = UUID.fromString(TestBackendConstants._TEST_USER_5_EXT_ID);
134
135        mockeryDao.checking(new Expectations() {
136            {
137                oneOf(userDao).getInternalID(externalID);
138                will(returnValue(5));
139            }
140        });
141        assertEquals(5, dbIntegrityService.getUserInternalIdentifier(externalID));
142    }
143
144    /**
145     * Test of getUserExternalIdentifier method, of class
146     * DBIntegrityServiceImlp.
147     */
148    @Test
149   
150    public void testGetUserExternalIdentifier() {
151        System.out.println("getUserExternalIdentifier");
152        final UUID externalID = UUID.fromString(TestBackendConstants._TEST_USER_5_EXT_ID);
153
154        mockeryDao.checking(new Expectations() {
155            {
156                oneOf(userDao).getExternalID(5);
157                will(returnValue(externalID));
158            }
159        });
160        assertEquals(TestBackendConstants._TEST_USER_5_EXT_ID, dbIntegrityService.getUserExternalIdentifier(5).toString());
161    }
162
163    /**
164     * Test of getAnnotation method, of class DBIntegrityServiceImlp.
165     */
166    @Test
167   
168    public void testGetAnnotation() throws Exception {
169        System.out.println("test getAnnotation");
170
171        final Annotation mockAnnotation = new Annotation();// corresponds to the annotation # 2
172        mockAnnotation.setURI(TestBackendConstants._TEST_SERVLET_URI_annotations +TestBackendConstants._TEST_ANNOT_2_EXT);
173        mockAnnotation.setHeadline(TestBackendConstants._TEST_ANNOT_2_HEADLINE);
174        XMLGregorianCalendar mockTimeStamp = Helpers.setXMLGregorianCalendar(Timestamp.valueOf("2013-08-12 11:25:00.383000"));
175        mockAnnotation.setTimeStamp(mockTimeStamp);
176        mockAnnotation.setOwnerRef("3");
177
178        AnnotationBody mockBody = new AnnotationBody();
179        TextBody textBody = new AnnotationBody.TextBody();
180        mockBody.setTextBody(textBody);
181        textBody.setMimeType("text/plain");
182        textBody.setValue(TestBackendConstants._TEST_ANNOT_2_BODY);
183        mockAnnotation.setBody(mockBody);
184        mockAnnotation.setTargets(null);
185
186
187        final List<Number> mockTargetIDs = new ArrayList<Number>();
188        mockTargetIDs.add(1);
189        mockTargetIDs.add(2);
190
191        final Target mockTargetOne = new Target();
192        mockTargetOne.setLink(TestBackendConstants._TEST_Target_1_LINK);
193        mockTargetOne.setURI(TestBackendConstants._TEST_SERVLET_URI_Targets +TestBackendConstants._TEST_Target_1_EXT_ID);
194        mockTargetOne.setVersion(TestBackendConstants._TEST_Target_1_EXT_ID);
195
196        final Target mockTargetTwo = new Target();
197        mockTargetTwo.setLink(TestBackendConstants._TEST_Target_2_LINK);
198        mockTargetTwo.setURI(TestBackendConstants._TEST_SERVLET_URI_Targets +TestBackendConstants._TEST_Target_2_EXT_ID);
199        mockTargetTwo.setVersion(TestBackendConstants._TEST_Target_2_EXT_ID);
200
201        final List<Map<Number, String>> listMap = new ArrayList<Map<Number, String>>();
202        Map<Number, String> map3 = new HashMap<Number, String>();
203        map3.put(3, "owner");
204        listMap.add(map3);
205        Map<Number, String> map4 = new HashMap<Number, String>();
206        map4.put(4, "writer");
207        listMap.add(map4);
208        Map<Number, String> map5 = new HashMap<Number, String>();
209        map5.put(5, "reader");
210        listMap.add(map5);
211
212        final UUID externalID3 = UUID.fromString(TestBackendConstants._TEST_USER_3_EXT_ID);
213        final UUID externalID4 = UUID.fromString(TestBackendConstants._TEST_USER_4_EXT_ID);
214        final UUID externalID5 = UUID.fromString(TestBackendConstants._TEST_USER_5_EXT_ID);
215
216        final String uri3 = TestBackendConstants._TEST_SERVLET_URI_users +TestBackendConstants._TEST_USER_3_EXT_ID;
217        final String uri4 = TestBackendConstants._TEST_SERVLET_URI_users +TestBackendConstants._TEST_USER_4_EXT_ID;
218        final String uri5 = TestBackendConstants._TEST_SERVLET_URI_users +TestBackendConstants._TEST_USER_5_EXT_ID;
219
220        final Map<Annotation, Number> mockAnnotationOwnerID = new HashMap<Annotation, Number>();
221        mockAnnotationOwnerID.put(mockAnnotation, 3);
222       
223        mockeryDao.checking(new Expectations() {
224            {
225                oneOf(annotationDao).getAnnotationWithoutTargetsAndPermissions(2);
226                will(returnValue(mockAnnotationOwnerID));
227
228                oneOf(userDao).getURIFromInternalID(3);
229                will(returnValue(uri3));
230               
231                oneOf(annotationDao).retrieveTargetIDs(2);
232                will(returnValue(mockTargetIDs));
233
234                oneOf(targetDao).getTarget(1);
235                will(returnValue(mockTargetOne));
236
237                oneOf(targetDao).getTarget(2);
238                will(returnValue(mockTargetTwo));
239
240                /// getPermissionsForAnnotation
241
242                oneOf(annotationDao).getPermissions(2);
243                will(returnValue(listMap));
244
245                oneOf(userDao).getURIFromInternalID(3);
246                will(returnValue(uri3));
247
248                oneOf(userDao).getURIFromInternalID(4);
249                will(returnValue(uri4));
250
251                 oneOf(userDao).getURIFromInternalID(5);
252                will(returnValue(uri5));
253            }
254        });
255
256        Annotation result = dbIntegrityService.getAnnotation(2);
257        assertEquals(TestBackendConstants._TEST_SERVLET_URI_annotations+TestBackendConstants._TEST_ANNOT_2_EXT, result.getURI());
258        assertEquals("text/plain", result.getBody().getTextBody().getMimeType());
259        assertEquals(TestBackendConstants._TEST_ANNOT_2_BODY, result.getBody().getTextBody().getValue());
260        assertEquals(TestBackendConstants._TEST_ANNOT_2_HEADLINE, result.getHeadline());
261        assertEquals(TestBackendConstants._TEST_ANNOT_2_TIME_STAMP, result.getTimeStamp().toString());
262        assertEquals(TestBackendConstants._TEST_SERVLET_URI_users+TestBackendConstants._TEST_USER_3_EXT_ID, result.getOwnerRef());
263
264        assertEquals(mockTargetOne.getLink(), result.getTargets().getTargetInfo().get(0).getLink());
265        assertEquals(mockTargetOne.getURI(), result.getTargets().getTargetInfo().get(0).getRef());
266        assertEquals(mockTargetOne.getVersion(), result.getTargets().getTargetInfo().get(0).getVersion());
267        assertEquals(mockTargetTwo.getLink(), result.getTargets().getTargetInfo().get(1).getLink());
268        assertEquals(mockTargetTwo.getURI(), result.getTargets().getTargetInfo().get(1).getRef());
269        assertEquals(mockTargetTwo.getVersion(), result.getTargets().getTargetInfo().get(1).getVersion());
270
271        assertEquals(Permission.OWNER, result.getPermissions().getUserWithPermission().get(0).getPermission());
272        assertEquals(uri3, result.getPermissions().getUserWithPermission().get(0).getRef());
273
274        assertEquals(Permission.WRITER, result.getPermissions().getUserWithPermission().get(1).getPermission());
275        assertEquals(uri4, result.getPermissions().getUserWithPermission().get(1).getRef());
276
277        assertEquals(Permission.READER, result.getPermissions().getUserWithPermission().get(2).getPermission());
278        assertEquals(uri5, result.getPermissions().getUserWithPermission().get(2).getRef());
279    }
280
281    /**
282     * Test of getFilteredAnnotationIDs method, of class DBIntegrityServiceImlp.
283     */
284    @Test   
285    public void testGetFilteredAnnotationIDs() {
286        System.out.println("test getFilteredAnnotationIDs");
287
288        final String word = "nl.wikipedia.org";
289
290        final List<Number> mockTargetIDs = new ArrayList<Number>();
291        mockTargetIDs.add(1);
292        mockTargetIDs.add(2);
293
294        final List<Number> mockAnnotationIDs1 = new ArrayList<Number>();
295        mockAnnotationIDs1.add(3);
296        mockAnnotationIDs1.add(4);
297       
298        final List<Number> mockAnnotationIDs2 = new ArrayList<Number>();
299        mockAnnotationIDs2.add(2);
300        mockAnnotationIDs2.add(3);
301
302        final String text = "some html";
303        final UUID owner = UUID.fromString(TestBackendConstants._TEST_USER_4_EXT_ID);
304        final Timestamp after = new Timestamp(0);
305        final Timestamp before = new Timestamp(System.currentTimeMillis());
306
307        final List<Number> mockRetval = new ArrayList<Number>();
308        mockRetval.add(2);
309       
310       
311
312        mockeryDao.checking(new Expectations() {
313            {
314                oneOf(targetDao).getTargetsReferringTo(word);
315                will(returnValue(mockTargetIDs));
316
317                oneOf(annotationDao).getAnnotationIDsForUserWithPermission(3, "reader");
318                will(returnValue(mockAnnotationIDs1));
319
320               
321                oneOf(annotationDao).retrieveAnnotationList(mockTargetIDs);
322                will(returnValue(mockAnnotationIDs2));
323
324                oneOf(userDao).getInternalID(owner);
325                will(returnValue(4));
326               
327               
328                oneOf(annotationDao).getFilteredAnnotationIDs(mockAnnotationIDs1, text, null, 4, after, before);
329                will(returnValue(mockRetval));
330
331            }
332        });
333
334
335        List result = dbIntegrityService.getFilteredAnnotationIDs(word, text, 3, "reader", null, owner, after, before);
336        assertEquals(1, result.size());
337        assertEquals(2, result.get(0));
338    }
339
340    @Test
341   
342    public void testGetAnnotationTargets() throws SQLException{
343        System.out.println("test getAnnotationTargets");
344        final Number annotationID = 2;
345        final List<Number> TargetIDs = new ArrayList<Number>();
346        TargetIDs.add(1);
347        TargetIDs.add(2);
348        mockeryDao.checking(new Expectations() {
349            {
350                oneOf(annotationDao).retrieveTargetIDs(annotationID);
351                will(returnValue(TargetIDs));
352
353                oneOf(targetDao).getURIFromInternalID(1);
354                will(returnValue(TestBackendConstants._TEST_SERVLET_URI_Targets+TestBackendConstants._TEST_Target_1_EXT_ID));
355
356                oneOf(targetDao).getURIFromInternalID(2);
357                will(returnValue(TestBackendConstants._TEST_SERVLET_URI_Targets+TestBackendConstants._TEST_Target_2_EXT_ID));
358
359            }
360        });
361       
362        ReferenceList result = dbIntegrityService.getAnnotationTargets(annotationID);
363        assertEquals(2, result.getRef().size());
364        assertEquals(TestBackendConstants._TEST_SERVLET_URI_Targets+TestBackendConstants._TEST_Target_1_EXT_ID, result.getRef().get(0));
365        assertEquals(TestBackendConstants._TEST_SERVLET_URI_Targets+TestBackendConstants._TEST_Target_2_EXT_ID, result.getRef().get(1));
366       
367    }
368   
369   
370//     @Override
371//    public AnnotationInfoList getFilteredAnnotationInfos(String link, String text, String access, String namespace, UUID
372//            owner, Timestamp after, Timestamp before){
373//        List<Number> annotationIDs = getFilteredAnnotationIDs(link, text, access, namespace, owner, after, before);
374//        List<AnnotationInfo> listAnnotationInfo = annotationDao.getAnnotationInfos(annotationIDs);
375 //       AnnotationInfoList result = new AnnotationInfoList();
376 //       result.getAnnotation().addAll(listAnnotationInfo);
377 //       return result;
378//    }
379   
380    @Test
381    public void testGetFilteredAnnotationInfos() throws SQLException{
382        System.out.println("test getetFilteredAnnotationInfos");
383       
384        final String word = "nl.wikipedia.org";
385
386        final List<Number> mockTargetIDs = new ArrayList<Number>();
387        mockTargetIDs.add(1);
388        mockTargetIDs.add(2);
389
390         final List<Number> mockAnnotationIDs1 = new ArrayList<Number>();
391        mockAnnotationIDs1.add(3);
392        mockAnnotationIDs1.add(4);
393       
394        final List<Number> mockAnnotationIDs2 = new ArrayList<Number>();
395        mockAnnotationIDs2.add(2);
396        mockAnnotationIDs2.add(3);
397
398
399        final String text = "some html";
400        final UUID ownerUUID = UUID.fromString(TestBackendConstants._TEST_USER_4_EXT_ID);
401        final Timestamp after = new Timestamp(0);
402        final Timestamp before = new Timestamp(System.currentTimeMillis());
403
404        final List<Number> mockAnnotIDs = new ArrayList<Number>();
405        mockAnnotIDs.add(2);
406       
407        final AnnotationInfo mockAnnotInfo = new AnnotationInfo();
408       
409        mockAnnotInfo.setHeadline(TestBackendConstants._TEST_ANNOT_2_HEADLINE);       
410        mockAnnotInfo.setRef(TestBackendConstants._TEST_SERVLET_URI_annotations + TestBackendConstants._TEST_ANNOT_2_EXT);
411       
412        final List<Number> TargetIDs = new ArrayList<Number>();
413        TargetIDs.add(1);
414        TargetIDs.add(2);
415       
416        final Map<AnnotationInfo,Number> mockPair = new HashMap<AnnotationInfo, Number>();
417        mockPair.put(mockAnnotInfo, 3);
418       
419        mockeryDao.checking(new Expectations() {
420            {
421                oneOf(annotationDao).getAnnotationIDsForUserWithPermission(3, "reader");
422                will(returnValue(mockAnnotationIDs1));
423
424               
425                // getFilteredAnnotationIds
426                oneOf(targetDao).getTargetsReferringTo(word);
427                will(returnValue(mockTargetIDs));
428               
429                oneOf(annotationDao).retrieveAnnotationList(mockTargetIDs);
430                will(returnValue(mockAnnotationIDs2));
431               
432                oneOf(userDao).getInternalID(ownerUUID);
433                will(returnValue(4));
434             
435                oneOf(annotationDao).getFilteredAnnotationIDs(mockAnnotationIDs1, text, null, 4, after, before);
436                will(returnValue(mockAnnotIDs));
437               
438               
439//                ///////////////////////////////////
440//               
441                oneOf(annotationDao).getAnnotationInfoWithoutTargets(2);
442                will(returnValue(mockPair));
443               
444                ////
445                oneOf(annotationDao).retrieveTargetIDs(2);
446                will(returnValue(TargetIDs));
447               
448                oneOf(targetDao).getURIFromInternalID(1);
449                will(returnValue(TestBackendConstants._TEST_SERVLET_URI_Targets +TestBackendConstants._TEST_Target_1_EXT_ID));
450               
451                oneOf(targetDao).getURIFromInternalID(2);
452                will(returnValue(TestBackendConstants._TEST_SERVLET_URI_Targets +TestBackendConstants._TEST_Target_2_EXT_ID));
453                ////
454               
455                oneOf(userDao).getURIFromInternalID(3);
456                will(returnValue(TestBackendConstants._TEST_SERVLET_URI_users +TestBackendConstants._TEST_USER_3_EXT_ID)); 
457             
458               
459            }
460        });
461       
462     
463        AnnotationInfoList result = dbIntegrityService.getFilteredAnnotationInfos(word, text, 3, "reader", null, ownerUUID, after, before);
464        assertEquals(1, result.getAnnotationInfo().size()); 
465        AnnotationInfo resultAnnotInfo = result.getAnnotationInfo().get(0);
466        assertEquals(mockAnnotInfo.getHeadline(), resultAnnotInfo.getHeadline());
467        assertEquals(TestBackendConstants._TEST_SERVLET_URI_users +TestBackendConstants._TEST_USER_3_EXT_ID, resultAnnotInfo.getOwnerRef());
468        assertEquals(mockAnnotInfo.getRef(),result.getAnnotationInfo().get(0).getRef() );
469        assertEquals(TestBackendConstants._TEST_SERVLET_URI_Targets +TestBackendConstants._TEST_Target_1_EXT_ID, resultAnnotInfo.getTargets().getRef().get(0));
470        assertEquals(TestBackendConstants._TEST_SERVLET_URI_Targets +TestBackendConstants._TEST_Target_2_EXT_ID, resultAnnotInfo.getTargets().getRef().get(1));
471         
472    }
473   
474    @Test   
475    public void testGetTargetsWithNoCachedRepresentation(){
476        System.out.println("test getTargetsWithNoCachedRepresentation");
477        final Number annotationID = 4;
478        final List<Number> TargetIDs = new ArrayList<Number>();
479        TargetIDs.add(5);
480        TargetIDs.add(7);
481       
482        final List<Number> cachedIDs5 = new ArrayList<Number>();
483        cachedIDs5.add(7);
484        final List<Number> cachedIDs7 = new ArrayList<Number>();
485       
486       
487       
488        mockeryDao.checking(new Expectations() {
489            {
490                oneOf(annotationDao).retrieveTargetIDs(annotationID);
491                will(returnValue(TargetIDs));
492
493                oneOf(targetDao).getCachedRepresentations(5);
494                will(returnValue(cachedIDs5));
495               
496                oneOf(targetDao).getCachedRepresentations(7);
497                will(returnValue(cachedIDs7));
498               
499                oneOf(targetDao).getURIFromInternalID(7);
500                will(returnValue("00000000-0000-0000-0000-000000000037"));
501               
502            }
503        });
504       
505        List<String> result = dbIntegrityService.getTargetsWithNoCachedRepresentation(annotationID);
506        assertEquals(1, result.size());
507        assertEquals("00000000-0000-0000-0000-000000000037", result.get(0)); // Target number 7 has no cached
508    }
509   
510   
511    ////////////// ADDERS /////////////////////////
512    /**
513     * Test of addCachedForVersion method, of class DBIntegrityServiceImlp.
514     */
515    @Test   
516    public void testAddCachedForVersion() throws SerialException, SQLException {
517        System.out.println("addCachedForVersion");
518        String mime = "text/html";
519        String type = "text";
520        String tool = "latex";
521        String externalID = UUID.randomUUID().toString();
522        final CachedRepresentationInfo newCachedInfo = new CachedRepresentationInfo();
523        newCachedInfo.setMimeType(mime);
524        newCachedInfo.setType(type);
525        newCachedInfo.setTool(tool);
526        newCachedInfo.setURI(TestBackendConstants._TEST_SERVLET_URI_cached + externalID);
527
528        String blobString = "aaa";
529        byte[] blobBytes = blobString.getBytes();
530        final ByteArrayInputStream newCachedBlob = new ByteArrayInputStream(blobBytes);
531        final Number newCachedID = 8;
532        final Number versionID = 1;
533        mockeryDao.checking(new Expectations() {
534            {
535
536                oneOf(cachedRepresentationDao).getInternalIDFromURI(newCachedInfo.getURI());
537                will(returnValue(null));
538
539                oneOf(cachedRepresentationDao).addCachedRepresentation(newCachedInfo, newCachedBlob);
540                will(returnValue(newCachedID));
541
542                one(targetDao).addTargetCachedRepresentation(versionID, newCachedID, "#(1,2)");
543                will(returnValue(1));
544
545            }
546        });
547
548
549        Number[] result = dbIntegrityService.addCachedForTarget(versionID, "#(1,2)", newCachedInfo, newCachedBlob);
550        assertEquals(2, result.length);
551        assertEquals(1, result[0]);
552        assertEquals(newCachedID, result[1]);
553    }
554
555    /**
556     * Test of updateSiblingTargetClassForTarget method, of class
557     * DBIntegrityServiceImlp.
558     *
559  **/
560   
561
562    /**
563     * Test of addTargetsForAnnotation method, of class DBIntegrityServiceImlp.
564     */
565    @Test
566   
567    public void testAddTargetsForAnnotation() throws Exception {
568        System.out.println("test addTargetsForAnnotation");
569
570//        @Override
571//        public Map<String, String> addTargetsForAnnotation(Number annotationID, List<TargetInfo> Targets) throws SQLException {
572//        Map<String, String> result = new HashMap<String, String>();
573//        Number TargetIDRunner;
574//        for (TargetInfo TargetInfo : Targets) {
575//            TargetIDRunner = TargetDao.getInternalIDFromURI(TargetInfo.getRef());
576//            if (TargetIDRunner != null) {
577//                int affectedRows = annotationDao.addAnnotationTarget(annotationID, TargetIDRunner);
578//            } else {
579//                Target newTarget = createFreshTarget(TargetInfo);
580//                Number TargetID = TargetDao.addTarget(newTarget);
581//                String TargetTemporaryID = TargetDao.stringURItoExternalID(TargetInfo.getRef());
582//                result.put(TargetTemporaryID, TargetDao.getExternalID(TargetID).toString());
583//                int affectedRows = annotationDao.addAnnotationTarget(annotationID, TargetID);
584//            }
585//        }
586//        return result;
587//    }
588       
589        // test 1: adding an existing Target
590        TargetInfo testTargetOne = new TargetInfo();
591        testTargetOne.setLink(TestBackendConstants._TEST_Target_1_LINK);
592        testTargetOne.setRef(TestBackendConstants._TEST_SERVLET_URI_Targets + TestBackendConstants._TEST_Target_1_EXT_ID);
593        testTargetOne.setVersion(TestBackendConstants._TEST_Target_2_VERSION );
594        final List<TargetInfo> mockTargetListOne = new ArrayList<TargetInfo>();
595        mockTargetListOne.add(testTargetOne);
596
597        mockeryDao.checking(new Expectations() {
598            {
599                oneOf(targetDao).getInternalIDFromURI(mockTargetListOne.get(0).getRef());
600                will(returnValue(1));
601
602                oneOf(annotationDao).addAnnotationTarget(1, 1);
603                will(returnValue(1));
604            }
605        });
606
607        Map<String, String> result = dbIntegrityService.addTargetsForAnnotation(1, mockTargetListOne);
608        assertEquals(0, result.size());
609       
610        //        @Override
611//        public Map<String, String> addTargetsForAnnotation(Number annotationID, List<TargetInfo> Targets) throws SQLException {
612//        Map<String, String> result = new HashMap<String, String>();
613//        Number TargetIDRunner;
614//        for (TargetInfo TargetInfo : Targets) {
615//            TargetIDRunner = TargetDao.getInternalIDFromURI(TargetInfo.getRef());
616//            if (TargetIDRunner != null) {
617//                int affectedRows = annotationDao.addAnnotationTarget(annotationID, TargetIDRunner);
618//            } else {
619//                Target newTarget = createFreshTarget(TargetInfo);
620//                Number TargetID = TargetDao.addTarget(newTarget);
621//                String TargetTemporaryID = TargetDao.stringURItoExternalID(TargetInfo.getRef());
622//                result.put(TargetTemporaryID, TargetDao.getExternalID(TargetID).toString());
623//                int affectedRows = annotationDao.addAnnotationTarget(annotationID, TargetID);
624//            }
625//        }
626//        return result;
627//    }
628       
629
630        // test 2: adding a new Target
631        TargetInfo testTargetTwo = new TargetInfo();
632        final String tempTargetID = UUID.randomUUID().toString();
633        testTargetTwo.setRef(TestBackendConstants._TEST_SERVLET_URI_Targets + tempTargetID);
634        testTargetTwo.setLink(TestBackendConstants._TEST_NEW_Target_LINK);
635        testTargetTwo.setVersion("version 1.0");
636        final List<TargetInfo> mockTargetListTwo = new ArrayList<TargetInfo>();
637        mockTargetListTwo.add(testTargetTwo);
638
639        final UUID mockNewTargetUUID = UUID.randomUUID();
640
641        mockeryDao.checking(new Expectations() {
642            {
643                oneOf(targetDao).getInternalIDFromURI(mockTargetListTwo.get(0).getRef());
644                will(returnValue(null));
645
646                oneOf(targetDao).addTarget(with(aNonNull(Target.class)));
647                will(returnValue(8)); //# the next new number is 8, we have already 7 Targets
648
649                oneOf(targetDao).stringURItoExternalID(mockTargetListTwo.get(0).getRef());
650                will(returnValue(tempTargetID));
651
652                oneOf(targetDao).getExternalID(8);
653                will(returnValue(mockNewTargetUUID));
654
655                oneOf(annotationDao).addAnnotationTarget(1, 8);
656                will(returnValue(1));
657
658            }
659        });
660
661        Map<String, String> resultTwo = dbIntegrityService.addTargetsForAnnotation(1, mockTargetListTwo);
662        assertEquals(1, resultTwo.size());
663        assertEquals(mockNewTargetUUID.toString(), resultTwo.get(tempTargetID));
664
665    }
666
667    /**
668     * Test of addUsersAnnotation method, of class DBIntegrityServiceImlp.
669     */
670    @Test
671   
672    public void testAddUsersAnnotation() throws Exception {
673        System.out.println("test addUsersAnnotation");
674
675        // expectations for addUsersannotation itself
676        final Annotation testAnnotation = testInstances.getAnnotationToAdd();
677
678        mockeryDao.checking(new Expectations() {
679            {
680                oneOf(annotationDao).addAnnotation(testAnnotation, 5);
681                will(returnValue(6)); // the next free number is 6
682
683                //  expectations for addTargetsForannotation
684                oneOf(targetDao).getInternalIDFromURI(with(aNonNull(String.class)));
685                will(returnValue(1));
686
687                oneOf(annotationDao).addAnnotationTarget(6, 1);
688                will(returnValue(1));
689
690                ///////////
691
692                oneOf(annotationDao).updateAnnotationBodyText(6, testAnnotation.getBody().getTextBody().getValue());
693                will(returnValue(1)); // the DB update will be called at perform anyway, even if the body is not changed (can be optimized)
694
695                oneOf(annotationDao).addAnnotationPrincipalPermission(6, 5, Permission.OWNER);
696                will(returnValue(1));
697            }
698        });
699
700        Number result = dbIntegrityService.addUsersAnnotation(5, testAnnotation);
701        assertEquals(6, result);
702    }
703
704    @Test
705   
706    public void testAddUser() {
707        System.out.println("test addUser");
708        final User freshUser = new User();
709        freshUser.setDisplayName("Guilherme");
710        freshUser.setEMail("guisil@mpi.nl");
711        mockeryDao.checking(new Expectations() {
712            {
713                oneOf(userDao).userExists(freshUser);
714                will(returnValue(false));
715
716                oneOf(userDao).addUser(freshUser, "xx");
717                will(returnValue(7));
718            }
719        });
720
721
722        assertEquals(7, dbIntegrityService.addUser(freshUser, "xx").intValue());
723
724        /// user already exists
725        final User user = new User();
726        freshUser.setDisplayName("Olha");
727        freshUser.setEMail("olhsha@mpi.nl");
728        mockeryDao.checking(new Expectations() {
729            {
730                oneOf(userDao).userExists(user);
731                will(returnValue(true));
732
733            }
734        });
735
736        assertTrue(null == dbIntegrityService.addUser(user, "yy"));
737    }
738
739    //////////////////// DELETERS ////////////////
740    @Test
741   
742    public void testDeleteUser() {
743        System.out.println("test deleteUser");
744
745        mockeryDao.checking(new Expectations() {
746            {
747                oneOf(userDao).deleteUser(2);
748                will(returnValue(0));
749
750                oneOf(userDao).deleteUser(5);
751                will(returnValue(0));
752
753                oneOf(userDao).deleteUser(6);
754                will(returnValue(1));
755
756            }
757        });
758
759        assertEquals(0, dbIntegrityService.deleteUser(2));
760        assertEquals(0, dbIntegrityService.deleteUser(5));
761        assertEquals(1, dbIntegrityService.deleteUser(6));
762    }
763
764    /**
765     * Test of deleteCachedForVersion method, of class DBIntegrityServiceImlp.
766     */
767    @Test
768   
769    public void testDeleteCachedRepresentationForTarget() throws SQLException{
770        System.out.println("test deleteCachedRepresentationForTarget");
771        mockeryDao.checking(new Expectations() {
772            {
773                oneOf(targetDao).deleteTargetCachedRepresentation(5, 7);
774                will(returnValue(1));
775
776                oneOf(cachedRepresentationDao).deleteCachedRepresentation(7);
777                will(returnValue(1)); // cached is used by another version
778
779            }
780        });
781
782        int[] result = dbIntegrityService.deleteCachedRepresentationOfTarget(5, 7);
783        assertEquals(2, result.length);
784        assertEquals(1, result[0]);
785        assertEquals(1, result[1]);
786    }
787
788    /////////////////////////////////////////////
789    @Test
790   
791    public void testDeleteAllCachedRepresentationsOfTarget() throws SQLException{
792        System.out.println("test deleteAllCachedRepresentationsOfTarget");
793        final List<Number> cachedList = new ArrayList<Number>();
794        cachedList.add(1);
795        cachedList.add(2);
796       
797        mockeryDao.checking(new Expectations() {
798            {
799                oneOf(targetDao).getCachedRepresentations(1);
800                will(returnValue(cachedList));
801               
802                oneOf(targetDao).deleteTargetCachedRepresentation(1, 1);
803                will(returnValue(1));
804               
805                oneOf(cachedRepresentationDao).deleteCachedRepresentation(1);
806                will(returnValue(1));
807               
808                oneOf(targetDao).deleteTargetCachedRepresentation(1, 2);
809                will(returnValue(1));
810               
811                 oneOf(cachedRepresentationDao).deleteCachedRepresentation(2);
812                 will(returnValue(1));
813
814            }
815        });
816
817        int[] result = dbIntegrityService.deleteAllCachedRepresentationsOfTarget(1);
818        assertEquals(2, result[0]); // # affected rows in Targets_cacheds
819        assertEquals(2, result[1]); // # affected rows in cacheds
820    }
821
822   
823
824    /**
825     * Test of deleteAnnotationWithTargets method, of class
826     * DBIntegrityServiceImlp.
827     */
828    @Test
829   
830    public void testDeleteAnnotation() throws Exception {
831        System.out.println("deleteAnnotation ");
832
833        // deleting annotation 3, which has its target Target 2  (used by annotation # 1)
834        final List<Number> mockTargetIDs = new ArrayList<Number>();
835        mockTargetIDs.add(2);
836       
837        final List<Number> mockCachedIDs = new ArrayList<Number>();
838        mockCachedIDs.add(3);
839
840        mockeryDao.checking(new Expectations() {
841            {
842                oneOf(annotationDao).deleteAnnotationPrincipalPermissions(3);
843                will(returnValue(3));
844
845                oneOf(annotationDao).retrieveTargetIDs(3);
846                will(returnValue(mockTargetIDs));               
847             
848                oneOf(annotationDao).deleteAllAnnotationTarget(3);
849                will(returnValue(1));
850
851                oneOf(annotationDao).deleteAnnotation(3);
852                will(returnValue(1));
853               
854                oneOf(targetDao).getCachedRepresentations(2);
855                will(returnValue(mockCachedIDs));
856               
857                oneOf(targetDao).deleteTargetCachedRepresentation(2, 3);
858                will(returnValue(1));
859               
860                oneOf(cachedRepresentationDao).deleteCachedRepresentation(3);
861                will(returnValue(1));
862               
863                oneOf(targetDao).deleteTarget(2);
864                will(returnValue(1));
865
866               
867            }
868        });
869        int[] result = dbIntegrityService.deleteAnnotation(3);// the Target will be deleted because it is not referred by any annotation
870        assertEquals(4, result.length);
871        assertEquals(1, result[0]); // annotation 3 is deleted
872        assertEquals(3, result[1]); // 3 rows in "annotation principal permissions are deleted"
873        assertEquals(1, result[2]);  // row (3,2) in "annotations_Targets" is deleted
874        assertEquals(1, result[3]); //  Target 3 is deleted
875    }
876//    @Test
877//    public void testCreateTarget(){ 
878//        NewTargetInfo newTargetInfo = new NewTargetInfo();
879//        newTargetInfo.setLink(TestBackendConstants._TEST_NEW_Target_LINK);
880//        newTargetInfo.setVersion(null);
881//       
882//        Target result = dbIntegrityService.createTarget(newTargetInfo);
883//        assertEquals(TestBackendConstants._TEST_NEW_Target_LINK, result.getLink());
884//        assertFalse(null == result.getURI());
885//       
886//    }
887//   
888//    @Test
889//    public void testCreateVersion(){ 
890//        NewTargetInfo newTargetInfo = new NewTargetInfo();
891//        newTargetInfo.setLink(TestBackendConstants._TEST_NEW_Target_LINK);
892//        newTargetInfo.setVersion(null);
893//       
894//        Version result = dbIntegrityService.createVersion(newTargetInfo);
895//        assertFalse(null == result.getVersion()); // will be chnaged once the schema for version is fixed: ID is added
896//       
897//    }
898}
Note: See TracBrowser for help on using the repository browser.