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

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

adding trasnactional, refactoring and fixing bugs in updated annotations, removing try-catch from resource methods (The Greek's advice)

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