Changeset 3448


Ignore:
Timestamp:
08/20/13 15:05:30 (11 years ago)
Author:
olhsha
Message:

DaoDispatcher? passed testing!

Location:
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/AnnotationResource.java

    r3429 r3448  
    8888            userID = daoDispatcher.getUserInternalIdentifier(new UserIdentifier(remoteUser));
    8989        }
    90         Annotation newAnnotation =  daoDispatcher.addUsersAnnotation(annotation, userID);
     90        Number newAnnotationID =  daoDispatcher.addUsersAnnotation(annotation, userID);
     91        Annotation newAnnotation = daoDispatcher.getAnnotation(newAnnotationID);
    9192        return (new ObjectFactory().createAnnotation(newAnnotation));
    9293    }
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/DaoDispatcher.java

    r3429 r3448  
    3333import eu.dasish.annotation.schema.CachedRepresentationInfo;
    3434import eu.dasish.annotation.schema.NewOrExistingSourceInfo;
     35import eu.dasish.annotation.schema.NewOrExistingSourceInfos;
    3536import eu.dasish.annotation.schema.NewSourceInfo;
    3637import eu.dasish.annotation.schema.Permission;
     
    8788        Annotation result = annotationDao.getAnnotationWithoutSources(annotationID);
    8889        List<Number> sourceIDs = annotationDao.retrieveSourceIDs(annotationID);
     90        NewOrExistingSourceInfos noesis = new NewOrExistingSourceInfos();
    8991        for (Number sourceID : sourceIDs) {
    9092            NewOrExistingSourceInfo noesi = new NewOrExistingSourceInfo();
     
    9597            sourceInfo.setVersion(source.getVersion());
    9698            noesi.setSource(sourceInfo);
    97             result.getTargetSources().getTarget().add(noesi);
    98         }
     99            noesis.getTarget().add(noesi);
     100        }
     101        result.setTargetSources(noesis);
    99102        return result;
    100103    }
     
    138141      public Number[] addSiblingVersionForSource(Number sourceID, Version version) throws SQLException {
    139142        Number[] result = new Number[2];
    140         result[0] = versionDao.getInternalID(new VersionIdentifier(version.getVersion())); // TOT: change to getURI after the schem is fixed
    141         if (result[0] == null) {
    142             result[0] = versionDao.addVersion(version);
    143         }
    144         result[1] = sourceDao.addSourceVersion(sourceID, result[0]);
     143        result[1] = versionDao.getInternalID(new VersionIdentifier(version.getVersion())); // TOT: change to getURI after the schem is fixed
     144        if (result[1] == null) {
     145            result[1] = versionDao.addVersion(version);
     146        }
     147        result[0] = sourceDao.addSourceVersion(sourceID, result[1]);
    145148        return result;
    146149    }
     
    167170
    168171   
    169     public Annotation addUsersAnnotation(Annotation annotation, Number userID) throws SQLException {
     172    public Number addUsersAnnotation(Annotation annotation, Number userID) throws SQLException {
    170173
    171174        Number annotationID = annotationDao.addAnnotation(annotation, userID);
     
    181184        int affectedPermissions = annotationDao.addAnnotationPrincipalPermission(annotationID, userID, Permission.OWNER);
    182185
    183         Annotation newAnnotation = getAnnotation(annotationID);
    184         return newAnnotation;
     186        return annotationID;
    185187    }
    186188
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/TestBackendConstants.java

    r3429 r3448  
    2222
    2323package eu.dasish.annotation.backend;
     24
     25import java.sql.Timestamp;
    2426
    2527public class TestBackendConstants {
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/rest/DaoDispatcherTest.java

    r3429 r3448  
    1818package eu.dasish.annotation.backend.rest;
    1919
     20import eu.dasish.annotation.backend.Helpers;
    2021import eu.dasish.annotation.backend.TestBackendConstants;
     22import eu.dasish.annotation.backend.TestInstances;
    2123import eu.dasish.annotation.backend.dao.AnnotationDao;
    2224import eu.dasish.annotation.backend.dao.CachedRepresentationDao;
     
    3133import eu.dasish.annotation.backend.identifiers.VersionIdentifier;
    3234import eu.dasish.annotation.schema.Annotation;
     35import eu.dasish.annotation.schema.AnnotationBody;
    3336import eu.dasish.annotation.schema.CachedRepresentationInfo;
    3437import eu.dasish.annotation.schema.NewOrExistingSourceInfo;
    3538import eu.dasish.annotation.schema.NewSourceInfo;
     39import eu.dasish.annotation.schema.Permission;
     40import eu.dasish.annotation.schema.ResourceREF;
    3641import eu.dasish.annotation.schema.Source;
    3742import eu.dasish.annotation.schema.SourceInfo;
     43import eu.dasish.annotation.schema.Version;
    3844import java.sql.SQLException;
    3945import java.sql.Timestamp;
    4046import java.util.ArrayList;
     47import java.util.HashMap;
    4148import java.util.List;
    4249import java.util.Map;
     50import javax.xml.datatype.XMLGregorianCalendar;
    4351import org.jmock.Expectations;
    4452import org.jmock.Mockery;
     
    7684    @Autowired
    7785    private NotebookDao notebookDao;
     86    TestInstances testInstances = new TestInstances();
    7887
    7988    public DaoDispatcherTest() {
    8089    }
    8190
     91    ///////// GETTERS /////////////
    8292    /**
    8393     * Test of getAnnotationInternalIdentifier method, of class DaoDispatcher.
     
    150160
    151161    /**
    152      * Test of deleteCachedForVersion method, of class DaoDispatcher.
    153      */
    154     @Test
    155     public void testDeleteCachedForVersion() {
    156         System.out.println("deleteCachedForVersion");
    157         mockery.checking(new Expectations() {
    158             {
    159                 oneOf(versionDao).deleteVersionCachedRepresentation(6, 5);
    160                 will(returnValue(1));
    161 
    162                 oneOf(cachedRepresentationDao).deleteCachedRepresentationInfo(5);
    163                 will(returnValue(0)); // cached is used by another version
    164 
    165             }
    166         });
    167 
    168         int[] result = daoDispatcher.deleteCachedOfVersion(6, 5);
    169         assertEquals(2, result.length);
    170         assertEquals(1, result[0]);
    171         assertEquals(0, result[1]);
    172     }
    173 
     162     * Test of getAnnotation method, of class DaoDispatcher.
     163     */
     164    @Test
     165    public void testGetAnnotation() throws Exception {
     166        System.out.println("test getAnnotation");
     167
     168        final Annotation mockAnnotation = new Annotation();// corresponds to the annotation # 2
     169        mockAnnotation.setURI(TestBackendConstants._TEST_ANNOT_2_EXT);
     170        mockAnnotation.setHeadline(TestBackendConstants._TEST_ANNOT_2_HEADLINE);
     171        XMLGregorianCalendar mockTimeStamp = Helpers.setXMLGregorianCalendar(Timestamp.valueOf("2013-08-12 11:25:00.383000"));
     172        mockAnnotation.setTimeStamp(mockTimeStamp);
     173        ResourceREF mockOwner = new ResourceREF();
     174        mockOwner.setRef(Integer.toString(TestBackendConstants._TEST_ANNOT_2_OWNER));
     175        mockAnnotation.setOwner(mockOwner);
     176        AnnotationBody mockBody = new AnnotationBody();
     177        mockBody.getAny().add(TestBackendConstants._TEST_ANNOT_2_BODY); // TODO: will be changed after body serialization is fixed
     178        mockAnnotation.setBody(mockBody);
     179        mockAnnotation.setTargetSources(null);
     180
     181
     182        final List<Number> mockSourceIDs = new ArrayList<Number>();
     183        mockSourceIDs.add(1);
     184        mockSourceIDs.add(2);
     185
     186        final Source mockSourceOne = new Source();
     187        mockSourceOne.setLink(TestBackendConstants._TEST_SOURCE_1_LINK);
     188        mockSourceOne.setURI(TestBackendConstants._TEST_SOURCE_1_EXT_ID);
     189        mockSourceOne.setVersion(TestBackendConstants._TEST_SOURCE_1_EXT_ID);
     190
     191        final Source mockSourceTwo = new Source();
     192        mockSourceTwo.setLink(TestBackendConstants._TEST_SOURCE_2_LINK);
     193        mockSourceTwo.setURI(TestBackendConstants._TEST_SOURCE_2_EXT_ID);
     194        mockSourceTwo.setVersion(TestBackendConstants._TEST_SOURCE_2_EXT_ID);
     195
     196
     197        // NOT CHECKED: time stamp for sources
     198
     199        mockery.checking(new Expectations() {
     200            {
     201                oneOf(annotationDao).getAnnotationWithoutSources(1);
     202                will(returnValue(mockAnnotation));
     203
     204                oneOf(annotationDao).retrieveSourceIDs(1);
     205                will(returnValue(mockSourceIDs));
     206
     207                oneOf(sourceDao).getSource(1);
     208                will(returnValue(mockSourceOne));
     209
     210                oneOf(sourceDao).getSource(2);
     211                will(returnValue(mockSourceTwo));
     212
     213            }
     214        });
     215
     216        Annotation result = daoDispatcher.getAnnotation(1);
     217        assertEquals(TestBackendConstants._TEST_ANNOT_2_EXT, result.getURI());
     218        assertEquals(TestBackendConstants._TEST_ANNOT_2_BODY, result.getBody().getAny().get(0));
     219        assertEquals(TestBackendConstants._TEST_ANNOT_2_HEADLINE, result.getHeadline());
     220        assertEquals(TestBackendConstants._TEST_ANNOT_2_TIME_STAMP, result.getTimeStamp().toString());
     221        assertEquals(TestBackendConstants._TEST_ANNOT_2_OWNER, Integer.parseInt(result.getOwner().getRef()));
     222        assertEquals(mockSourceOne.getLink(), result.getTargetSources().getTarget().get(0).getSource().getLink());
     223        assertEquals(mockSourceOne.getURI(), result.getTargetSources().getTarget().get(0).getSource().getRef());
     224        assertEquals(mockSourceOne.getVersion(), result.getTargetSources().getTarget().get(0).getSource().getVersion());
     225        assertEquals(mockSourceTwo.getLink(), result.getTargetSources().getTarget().get(1).getSource().getLink());
     226        assertEquals(mockSourceTwo.getURI(), result.getTargetSources().getTarget().get(1).getSource().getRef());
     227        assertEquals(mockSourceTwo.getVersion(), result.getTargetSources().getTarget().get(1).getSource().getVersion());
     228    }
     229
     230    /**
     231     * Test of getFilteredAnnotationIDs method, of class DaoDispatcher.
     232     */
     233    @Test
     234    public void testGetFilteredAnnotationIDs() {
     235        System.out.println("test getFilteredAnnotationIDs");
     236
     237        final String link = "nl.wikipedia.org";
     238
     239        final List<Number> mockSourceIDs = new ArrayList<Number>();
     240        mockSourceIDs.add(1);
     241        mockSourceIDs.add(2);
     242
     243        final List<Number> mockAnnotationIDs = new ArrayList<Number>();
     244        mockAnnotationIDs.add(2);
     245        mockAnnotationIDs.add(3);
     246
     247        final String text = "some html";
     248        final String access = null;
     249        final String namespace = null;
     250        final UserIdentifier owner = new UserIdentifier(TestBackendConstants._TEST_USER_3_EXT_ID);
     251        final Timestamp after = new Timestamp(0);
     252        final Timestamp before = new Timestamp(System.currentTimeMillis());
     253
     254        final List<Number> mockRetval = new ArrayList<Number>();
     255        mockRetval.add(2);
     256
     257        mockery.checking(new Expectations() {
     258            {
     259                oneOf(sourceDao).getSourcesForLink(link);
     260                will(returnValue(mockSourceIDs));
     261
     262                oneOf(annotationDao).retrieveAnnotationList(mockSourceIDs);
     263                will(returnValue(mockAnnotationIDs));
     264
     265                oneOf(userDao).getInternalID(owner);
     266                will(returnValue(3));
     267
     268                oneOf(annotationDao).getFilteredAnnotationIDs(mockAnnotationIDs, text, access, namespace, 3, after, before);
     269                will(returnValue(mockRetval));
     270
     271            }
     272        });
     273
     274
     275        List result = daoDispatcher.getFilteredAnnotationIDs(link, text, access, namespace, owner, after, before);
     276        assertEquals(1, result.size());
     277        assertEquals(2, result.get(0));
     278    }
     279
     280    ////////////// ADDERS /////////////////////////
    174281    /**
    175282     * Test of addCachedForVersion method, of class DaoDispatcher.
     
    185292        newCached.setType(type);
    186293        newCached.setTool(tool);
    187         final Number newID = 8;
     294        final Number newCachedID = 8;
    188295        final Number versionID = 1;
    189296        mockery.checking(new Expectations() {
     
    193300
    194301                oneOf(cachedRepresentationDao).addCachedRepresentationInfo(newCached);
    195                 will(returnValue(newID));
    196 
    197                 one(versionDao).addVersionCachedRepresentation(versionID, newID);
     302                will(returnValue(newCachedID));
     303
     304                one(versionDao).addVersionCachedRepresentation(versionID, newCachedID);
    198305                will(returnValue(1));
    199306
     
    204311        Number[] result = daoDispatcher.addCachedForVersion(versionID, newCached);
    205312        assertEquals(2, result.length);
    206         assertEquals(newID, result[0]);
    207         assertEquals(1, result[1]);
     313        assertEquals(1, result[0]);
     314        assertEquals(newCachedID, result[1]);
     315    }
     316
     317    /**
     318     * Test of addSiblingVersionForSource method, of class DaoDispatcher.
     319     */
     320    @Test
     321    public void testAddSiblingVersionForSource() throws Exception {
     322        System.out.println("test addSiblingVersionForSource ");
     323
     324        // test adding completely new version
     325        final Version mockVersion = new Version(); // should be # 8
     326        final VersionIdentifier mockVersionIdentifier = new VersionIdentifier();
     327        mockVersion.setVersion(mockVersionIdentifier.toString());
     328
     329        mockery.checking(new Expectations() {
     330            {
     331                oneOf(versionDao).getInternalID(mockVersionIdentifier);
     332                will(returnValue(null));
     333
     334                oneOf(versionDao).addVersion(mockVersion);
     335                will(returnValue(8));
     336
     337                oneOf(sourceDao).addSourceVersion(1, 8);
     338                will(returnValue(1));
     339
     340            }
     341        });
     342
     343        Number[] result = daoDispatcher.addSiblingVersionForSource(1, mockVersion);
     344        assertEquals(2, result.length);
     345        assertEquals(1, result[0]);
     346        assertEquals(8, result[1]);
     347
     348
     349        // Another test: connecting the existing version to the source
     350
     351        final Version mockVersionTwo = new Version(); // should be # 3
     352        final VersionIdentifier mockVersionIdentifierTwo = new VersionIdentifier(TestBackendConstants._TEST_VERSION_3_EXT_ID);
     353        mockVersionTwo.setVersion(mockVersionIdentifierTwo.toString());
     354
     355        mockery.checking(new Expectations() {
     356            {
     357                oneOf(versionDao).getInternalID(mockVersionIdentifierTwo);
     358                will(returnValue(3));
     359
     360                oneOf(sourceDao).addSourceVersion(1, 3);
     361                will(returnValue(1));
     362            }
     363        });
     364        Number[] resultTwo = daoDispatcher.addSiblingVersionForSource(1, mockVersionTwo);
     365        assertEquals(2, resultTwo.length);
     366        assertEquals(1, resultTwo[0]);
     367        assertEquals(3, resultTwo[1]);
     368    }
     369
     370    /**
     371     * Test of addSourcesForAnnotation method, of class DaoDispatcher.
     372     */
     373    @Test
     374    public void testAddSourcesForAnnotation() throws Exception {
     375        System.out.println("test addSourcesForAnnotation");
     376
     377        // test 1: adding an existing source
     378        NewOrExistingSourceInfo testSourceOne = new NewOrExistingSourceInfo();
     379        final SourceIdentifier mockSourceIdentifierOne = new SourceIdentifier(TestBackendConstants._TEST_SOURCE_1_EXT_ID);
     380        SourceInfo testOldSource = new SourceInfo();
     381        testOldSource.setLink(TestBackendConstants._TEST_SOURCE_1_LINK);
     382        testOldSource.setRef(TestBackendConstants._TEST_SOURCE_1_EXT_ID);
     383        testOldSource.setVersion(TestBackendConstants._TEST_VERSION_1_EXT_ID);
     384        testSourceOne.setSource(testOldSource);
     385        List<NewOrExistingSourceInfo> mockSourceSOne = new ArrayList<NewOrExistingSourceInfo>();
     386        mockSourceSOne.add(testSourceOne);
     387
     388        mockery.checking(new Expectations() {
     389            {
     390                oneOf(sourceDao).getInternalID(with(aNonNull(SourceIdentifier.class)));
     391                will(returnValue(1));
     392
     393                oneOf(annotationDao).addAnnotationSourcePair(1, 1);
     394                will(returnValue(1));
     395            }
     396        });
     397
     398        Map<String, String> result = daoDispatcher.addSourcesForAnnotation(1, mockSourceSOne);
     399        assertEquals(0, result.size());
     400
     401        // test 2: adding a new source
     402
     403        NewOrExistingSourceInfo testSourceTwo = new NewOrExistingSourceInfo();
     404        NewSourceInfo testNewSource = new NewSourceInfo();
     405        testNewSource.setLink(TestBackendConstants._TEST_NEW_SOURCE_LINK);
     406        testNewSource.setVersion(null);
     407        testSourceTwo.setNewSource(testNewSource);
     408        List<NewOrExistingSourceInfo> mockSourceSTwo = new ArrayList<NewOrExistingSourceInfo>();
     409        mockSourceSTwo.add(testSourceTwo);
     410
     411//        final Source mockNewSource = daoDispatcher.createSource(testNewSource);
     412//        final Version mockNewVersion = daoDispatcher.createVersion(testNewSource);
     413
     414        mockery.checking(new Expectations() {
     415            {
     416                oneOf(sourceDao).addSource(with(aNonNull(Source.class)));
     417                will(returnValue(6)); //# the next new number is 6, we have already 5 sources
     418
     419                ////////////  mockery in the call addSiblingVersionForSource //////////////
     420
     421                oneOf(versionDao).getInternalID(with(aNonNull(VersionIdentifier.class)));
     422                will(returnValue(null));
     423
     424                oneOf(versionDao).addVersion(with(aNonNull(Version.class)));
     425                will(returnValue(8)); // next free nummber
     426
     427                oneOf(sourceDao).addSourceVersion(6, 8);
     428                will(returnValue(1));
     429
     430                //////////////////////////////////////
     431
     432                oneOf(annotationDao).addAnnotationSourcePair(1, 6);
     433                will(returnValue(1));
     434
     435            }
     436        });
     437
     438        Map<String, String> resultTwo = daoDispatcher.addSourcesForAnnotation(1, mockSourceSTwo);
     439        assertEquals(1, resultTwo.size());
     440        assertFalse(null == resultTwo.get(testNewSource.getId()));
     441
     442    }
     443
     444    /**
     445     * Test of addUsersAnnotation method, of class DaoDispatcher.
     446     */
     447    @Test
     448    public void testAddUsersAnnotation() throws Exception {
     449        System.out.println("test addUsersAnnotation");
     450
     451        // expectations for addUsersannotation itself
     452        final Annotation testAnnotation = testInstances.getAnnotationToAdd();
     453
     454        mockery.checking(new Expectations() {
     455            {
     456                oneOf(annotationDao).addAnnotation(testAnnotation, 5);
     457                will(returnValue(6)); // the next free number is 6
     458
     459                //  expectations for addSourcesForannotation
     460                oneOf(sourceDao).getInternalID(with(aNonNull(SourceIdentifier.class)));
     461                will(returnValue(1));
     462
     463                oneOf(annotationDao).addAnnotationSourcePair(6, 1);
     464                will(returnValue(1)); // the source is old, no new persistent identifier were produced
     465
     466                ///////////
     467
     468                oneOf(annotationDao).updateBody(6, testAnnotation.getBody().getAny().get(0).toString());// will be changed after serialization is fixed
     469                will(returnValue(1)); // the DB update will be called at perform anyway, even if the body is not changed (can be optimized)
     470
     471                oneOf(annotationDao).addAnnotationPrincipalPermission(6, 5, Permission.OWNER);
     472                will(returnValue(1));
     473            }
     474        });
     475
     476        Number result = daoDispatcher.addUsersAnnotation(testAnnotation, 5);
     477        assertEquals(6, result);
     478    }
     479
     480    //////////////////// DELETERS ////////////////
     481    /**
     482     * Test of deleteCachedForVersion method, of class DaoDispatcher.
     483     */
     484    @Test
     485    public void testDeleteCachedForVersion() {
     486        System.out.println("deleteCachedForVersion");
     487        mockery.checking(new Expectations() {
     488            {
     489                oneOf(versionDao).deleteVersionCachedRepresentation(6, 5);
     490                will(returnValue(1));
     491
     492                oneOf(cachedRepresentationDao).deleteCachedRepresentationInfo(5);
     493                will(returnValue(0)); // cached is used by another version
     494
     495            }
     496        });
     497
     498        int[] result = daoDispatcher.deleteCachedOfVersion(6, 5);
     499        assertEquals(2, result.length);
     500        assertEquals(1, result[0]);
     501        assertEquals(0, result[1]);
    208502    }
    209503
    210504    /////////////////////////////////////////////
    211     public void testDeleteVersionWithCachedRepresentations() {
     505    @Test
     506    public void testDeleteAalCachedOfVersion() {
    212507        System.out.println("deleteVersion");
    213508        final List<Number> cachedList = new ArrayList<Number>();
     
    230525                oneOf(cachedRepresentationDao).deleteCachedRepresentationInfo(5);
    231526                will(returnValue(0)); // cached is used by another version
    232 
    233 
    234 
    235527            }
    236528        });
     
    260552
    261553    @Test
    262     public void testDeleteSourceSourceWithVersions() throws SQLException {
     554    public void testDeleteAllVersionsOfSource() throws SQLException {
    263555        // test 1: source in use by some annotation
    264556        mockery.checking(new Expectations() {
     
    312604
    313605    /**
    314      * Test of addSourceAndPairSourceVersion method, of class DaoDispatcher.
    315      */
    316     @Test
    317     public void testAddSourceAndPairSourceVersion() throws Exception {
    318         System.out.println("addSourceAndPairSourceVersion");
    319         final NewSourceInfo newSource = new NewSourceInfo();
    320         newSource.setLink(TestBackendConstants._TEST_NEW_SOURCE_LINK);
    321         newSource.setVersion(TestBackendConstants._TEST_VERSION_3_EXT_ID);// already added version, existing
    322 
    323 //        mockery.checking(new Expectations() {
    324 //            {
    325 //                oneOf(versionDao).getInternalID(new VersionIdentifier(TestBackendConstants._TEST_VERSION_3_EXT_ID));
    326 //                will(returnValue(3));
    327 //
    328 //                oneOf(sourceDao).addSource(with(aNonNull(Source.class)));
    329 //                will(returnValue(6));
    330 //
    331 //                oneOf(sourceDao).addSourceVersion(6, 3);
    332 //                will(returnValue(1));
    333 //
    334 //            }
    335 //        });
    336 //
    337 //        Number result = daoDispatcher.addSiblingVersionForSource(newSource);
    338 //        assertEquals(6, result.intValue());
    339 //
    340 //        // Another test
    341 //
    342 //        final NewSourceInfo newSourceTwo = new NewSourceInfo();
    343 //        newSourceTwo.setLink(TestBackendConstants._TEST_NEW_SOURCE_LINK);
    344 //        newSourceTwo.setVersion(TestBackendConstants._TEST_VERSION_NONEXIST_EXT_ID);
    345 //        mockery.checking(new Expectations() {
    346 //            {
    347 //                oneOf(versionDao).getInternalID(new VersionIdentifier(TestBackendConstants._TEST_VERSION_NONEXIST_EXT_ID));
    348 //                will(returnValue(null));
    349 //
    350 //            }
    351 //        });
    352 //        Number resultTwo = daoDispatcher.addSourceAndPairSourceVersion(newSourceTwo);
    353 //        assertEquals(-1, resultTwo.intValue());
    354 
    355 
    356     }
    357 
    358     /**
    359      * Test of addTargetSourcesToAnnotation method, of class DaoDispatcher.
    360      */
    361     @Test
    362     public void testAddTargetSourcesToAnnotation() throws Exception {
    363         System.out.println("addTargetSourcesToAnnotation");
    364 
    365         // test 1: adding an existing source
    366         NewOrExistingSourceInfo noesiOne = new NewOrExistingSourceInfo();
    367         // this corresponds to the Source # 1 in teh test database
    368         final SourceIdentifier sourceIdentifier = new SourceIdentifier(TestBackendConstants._TEST_SOURCE_1_EXT_ID);
    369         SourceInfo si = new SourceInfo();
    370         si.setLink(TestBackendConstants._TEST_SOURCE_1_LINK);
    371         si.setRef(TestBackendConstants._TEST_SOURCE_1_EXT_ID);
    372         si.setVersion(TestBackendConstants._TEST_VERSION_1_EXT_ID);
    373         noesiOne.setSource(si);
    374         List<NewOrExistingSourceInfo> sources = new ArrayList<NewOrExistingSourceInfo>();
    375         sources.add(noesiOne);
    376 
    377 //        mockery.checking(new Expectations() {
    378 //            {
    379 //                oneOf(sourceDao).getInternalID(sourceIdentifier);
    380 //                will(returnValue(1));
    381 //
    382 //                oneOf(annotationDao).addAnnotationSourcePair(1, 1);
    383 //                will(returnValue(1));
    384 //            }
    385 //        });
    386 //
    387 //        Map<String, String> result = daoDispatcher.addTargetSourcesToAnnotation(1, sources);
    388 //        assertEquals(0, result.size());
     606     * Test of deleteAnnotationWithSources method, of class DaoDispatcher.
     607     */
     608    @Test
     609    public void testDeleteAnnotation() throws Exception {
     610        System.out.println("deleteAnnotation ");
     611
     612        // deleting annotation 3, which has its target source 2  (used by annotation # 1)
     613        final List<Number> mockSourceIDs = new ArrayList<Number>();
     614        mockSourceIDs.add(2);
     615
     616        mockery.checking(new Expectations() {
     617            {
     618                oneOf(annotationDao).deleteAnnotationPrincipalPermissions(3);
     619                will(returnValue(3));
     620
     621                oneOf(annotationDao).retrieveSourceIDs(3);
     622                will(returnValue(mockSourceIDs));
     623
     624                oneOf(annotationDao).deleteAllAnnotationSource(3);
     625                will(returnValue(1));
     626
     627                oneOf(annotationDao).deleteAnnotation(3);
     628                will(returnValue(1));
     629
     630                oneOf(sourceDao).sourceIsInUse(2); // expectation for deleteAllVersionsOfSource
     631                will(returnValue(true));
     632            }
     633        });
     634        int[] result = daoDispatcher.deleteAnnotation(3);// the source will be deleted because it is not referred by any annotation
     635        assertEquals(4, result.length);
     636        assertEquals(1, result[0]); // annotation 3 is deleted
     637        assertEquals(3, result[1]); // 3 rows in "annotation proncipal permissions are deleted"
     638        assertEquals(1, result[2]);  // row (3,2) in "annotations_sources" is deleted
     639        assertEquals(0, result[3]); //the source 2 is not deleted because it is still in use by anntation 2
     640    }
     641   
     642   
     643//    @Test
     644//    public void testCreateSource(){ 
     645//        NewSourceInfo newSourceInfo = new NewSourceInfo();
     646//        newSourceInfo.setLink(TestBackendConstants._TEST_NEW_SOURCE_LINK);
     647//        newSourceInfo.setVersion(null);
    389648//       
    390 //        // test 2: adding a new source
    391 //
    392 //        NewOrExistingSourceInfo noesiTwo = new NewOrExistingSourceInfo();
    393 //        NewSourceInfo nsi = new NewSourceInfo();
    394 //        nsi.setLink(TestBackendConstants._TEST_NEW_SOURCE_LINK);
    395 //        nsi.setId(TestBackendConstants._TEST_TEMP_SOURCE_ID);
    396 //        nsi.setVersion(null);
    397 //        noesiTwo.setNewSource(nsi);
    398 //
    399 //
    400 //        NewOrExistingSourceInfo noesiTwoImage = new NewOrExistingSourceInfo();
    401 //        SourceInfo siTwo = new SourceInfo();
    402 //        siTwo.setLink(TestBackendConstants._TEST_NEW_SOURCE_LINK);
    403 //        final SourceIdentifier sourceIdentifierTwo = new SourceIdentifier();
    404 //        si.setRef(sourceIdentifier.toString());
    405 //        si.setVersion(null);
    406 //        noesiTwo.setSource(si);
    407 //
    408 //        final Map<NewOrExistingSourceInfo, NewOrExistingSourceInfo> map = new HashMap<NewOrExistingSourceInfo, NewOrExistingSourceInfo>();
    409 //        map.put(noesi, noesiTwo);
    410 
    411 
     649//        Source result = daoDispatcher.createSource(newSourceInfo);
     650//        assertEquals(TestBackendConstants._TEST_NEW_SOURCE_LINK, result.getLink());
     651//        assertFalse(null == result.getURI());
    412652//       
    413 //        mockery.checking(new Expectations() {
    414 //            {
    415 //                oneOf(sourceDao.getInternalID(sourceIdentifier));
    416 //                will(returnValue());
    417 //               
    418 //                oneOf(annotationDao).addAnnotationSourcePair(with(aNonNull(Number.class)), with(aNonNull(Number.class)));
    419 //                will(returnValue(1));
    420 //               
    421 //                oneOf(sourceDao).addTargetSources(with(aNonNull(Number.class)), with(aNonNull(List.class)));
    422 //                will(returnValue(map));
    423 //            }
    424 //        });
    425 
    426 
    427 
    428     }
    429 
    430     /**
    431      * Test of getFilteredAnnotationIDs method, of class DaoDispatcher.
    432      */
    433     @Test
    434     public void testGetFilteredAnnotationIDs() {
    435         System.out.println("getFilteredAnnotationIDs");
    436         String link = "";
    437         String text = "";
    438         String access = "";
    439         String namespace = "";
    440         UserIdentifier owner = null;
    441         Timestamp after = null;
    442         Timestamp before = null;
    443         DaoDispatcher instance = new DaoDispatcher();
    444         List expResult = null;
    445         List result = instance.getFilteredAnnotationIDs(link, text, access, namespace, owner, after, before);
    446         assertEquals(expResult, result);
    447         // TODO review the generated test code and remove the default call to fail.
    448         fail("The test case is a prototype.");
    449     }
    450 
    451     /**
    452      * Test of deleteAnnotationWithSources method, of class DaoDispatcher.
    453      */
    454     @Test
    455     public void testDeleteAnnotationWithSources() throws Exception {
    456 //        System.out.println("deleteAnnotationWithSources");
    457 //        Number annotationID = null;
    458 //        DaoDispatcher instance = new DaoDispatcher();
    459 //        int[] expResult = null;
    460 //        int[] result = instance.deleteAnnotationAllSourcesAndPermissions(annotationID);
    461 //        assertArrayEquals(expResult, result);
    462 //        // TODO review the generated test code and remove the default call to fail.
    463 //        fail("The test case is a prototype.");
    464     }
    465 
    466     /**
    467      * Test of getAnnotation method, of class DaoDispatcher.
    468      */
    469     @Test
    470     public void testGetAnnotation() throws Exception {
    471 //        System.out.println("getAnnotation");
    472 //        Number annotationID = null;
    473 //        DaoDispatcher instance = new DaoDispatcher();
    474 //        Annotation expResult = null;
    475 //        Annotation result = instance.getAnnotation(annotationID);
    476 //        assertEquals(expResult, result);
    477 //        // TODO review the generated test code and remove the default call to fail.
    478 //        fail("The test case is a prototype.");
    479     }
    480 
    481     /**
    482      *
    483      * @param annotationId
    484      * @return result[0] = # removed "annotations_principals_permissions" rows
    485      * result[1] = # removed "annotations_target_sources" rows result[2] = #
    486      * removed annotation rows (should be 1)
    487      */
    488     @Test
    489     public void testDeleteAnnotationWithSourcesAndPermissions() throws SQLException {
    490         System.out.println("test deleteAnnotationWithSourcesAndPermissions");
    491         // result[0] = # removed "annotations_principals_perissions" rows
    492         // result[1] = # removed "annotatiobs_target_sources" rows
    493         // result[2] = # removed annotation rows (should be 1)
    494 
    495 //        int[] result = daoDispatcher.deleteAnnotationWithSourcesAndPermissions(5);
    496 //        assertEquals(3, result[0]);
    497 //        assertEquals(2, result[1]);
    498 //        assertEquals(1, result[2]);
    499 //
    500 //        // now, try to delete the same annotation one more time
    501 //        // if it has been already deleted then the method under testing should return 0
    502 //
    503 //        result = daoDispatcher.deleteAnnotationWithSourcesAndPermissions(5);
    504 //        assertEquals(0, result[0]);
    505 //        assertEquals(0, result[1]);
    506 //        assertEquals(0, result[2]);
    507     }
    508 
    509     /**
    510      * Test of contructNewOrExistingSourceInfo method, of class JdbcSourceDao.
    511      */
    512     @Test
    513     public void testContructNewOrExistingSourceInfo() {
    514         System.out.println("contructNewOrExistingSourceInfo");
    515 
    516         List<SourceInfo> sourceInfoList = new ArrayList<SourceInfo>();
    517 
    518         SourceInfo sourceInfoOne = new SourceInfo();
    519         sourceInfoOne.setLink(TestBackendConstants._TEST_SOURCE_1_LINK);
    520         sourceInfoOne.setRef(TestBackendConstants._TEST_SOURCE_1_EXT_ID);
    521         sourceInfoOne.setRef(TestBackendConstants._TEST_VERSION_1_EXT_ID);
    522 
    523         SourceInfo sourceInfoTwo = new SourceInfo();
    524         sourceInfoTwo.setLink(TestBackendConstants._TEST_SOURCE_2_LINK);
    525         sourceInfoTwo.setRef(TestBackendConstants._TEST_SOURCE_2_EXT_ID);
    526         sourceInfoTwo.setRef(TestBackendConstants._TEST_VERSION_3_EXT_ID);
    527 
    528         sourceInfoList.add(sourceInfoOne);
    529         sourceInfoList.add(sourceInfoTwo);
    530 
    531 //        NewOrExistingSourceInfos result = jdbcSourceDao.contructNewOrExistingSourceInfo(sourceInfoList);
    532 //        assertEquals(2, result.getTarget().size());
    533 //        assertEquals(sourceInfoOne, result.getTarget().get(0).getSource());
    534 //        assertEquals(sourceInfoTwo, result.getTarget().get(1).getSource());
    535 
    536     }
    537 //    /**
    538 //     * Test of addTargetSources method, of class JdbcSourceDao. public
    539 //     * Map<NewOrExistingSourceInfo, NewOrExistingSourceInfo>
    540 //     * addTargetSources(Number annotationID, List<NewOrExistingSourceInfo>
    541 //     * sources)
    542 //     */
    543 //    @Test
    544 //    public void testAddTargetSourcesOnExistingSource() {
    545 //        System.out.println("addTargetSources : adding the old source");
    546 //
    547 //        NewOrExistingSourceInfo noesi = new NewOrExistingSourceInfo();
    548 //        SourceInfo si = new SourceInfo();
    549 //        si.setLink(TestBackendConstants._TEST_SOURCE_1_LINK);
    550 //        si.setRef(TestBackendConstants._TEST_SOURCE_1_EXT_ID);
    551 //        si.setVersion(TestBackendConstants._TEST_VERSION_1_EXT_ID);
    552 //        noesi.setSource(si);
    553 //
    554 //        List<NewOrExistingSourceInfo> listnoesi = new ArrayList<NewOrExistingSourceInfo>();
    555 //        listnoesi.add(noesi);
    556 //
    557 //        try {
    558 //            Map<String, String> result = jdbcSourceDao.addTargetSources(5, listnoesi);
    559 //            assertEquals(0, result.size()); // no new peristsent source IDs are produced
    560 //        } catch (SQLException e) {
    561 //            System.out.println(e);
    562 //        }
    563 //    }
    564 //
    565 //    /**
    566 //     * Test of addTargetSources method, of class JdbcSourceDao. public
    567 //     * Map<NewOrExistingSourceInfo, NewOrExistingSourceInfo>
    568 //     * addTargetSources(Number annotationID, List<NewOrExistingSourceInfo>
    569 //     * sources)
    570 //     */
    571 //    @Test
    572 //    public void testAddTargetSourcesOnNewSource() {
    573 //        System.out.println("addTargetSources : adding the new source");
    574 //
    575 //        NewOrExistingSourceInfo noesi = new NewOrExistingSourceInfo();
    576 //        NewSourceInfo nsi = new NewSourceInfo();
    577 //        nsi.setLink(TestBackendConstants._TEST_NEW_SOURCE_LINK);
    578 //        nsi.setId(TestBackendConstants._TEST_TEMP_SOURCE_ID);
    579 //        nsi.setVersion(TestBackendConstants._TEST_VERSION_1_EXT_ID);
    580 //        noesi.setNewSource(nsi);
    581 //
    582 //        List<NewOrExistingSourceInfo> listnoesiTwo = new ArrayList<NewOrExistingSourceInfo>();
    583 //        listnoesiTwo.add(noesi);
    584 //       
    585 //        mockery.checking(new Expectations() {
    586 //            {
    587 //                oneOf(versionDao).getInternalID(new VersionIdentifier(TestBackendConstants._TEST_VERSION_1_EXT_ID));
    588 //                will(returnValue(1));
    589 //            }
    590 //        });
    591 //
    592 //        try {
    593 //            Map<String, String> result = jdbcSourceDao.addTargetSources(5, listnoesiTwo);
    594 //            assertEquals(1, result.size());// a new identifier must be produced
    595 //            SourceIdentifier sourceIdentifier = new SourceIdentifier(result.get(TestBackendConstants._TEST_TEMP_SOURCE_ID));
    596 //            assertFalse(null == sourceIdentifier.getUUID()); // check if a proper uuid has been assigned
    597 //        } catch (SQLException e) {
    598 //            System.out.print(e);
    599 //        }
    600 //
    601653//    }
    602654//   
     655//    @Test
     656//    public void testCreateVersion(){ 
     657//        NewSourceInfo newSourceInfo = new NewSourceInfo();
     658//        newSourceInfo.setLink(TestBackendConstants._TEST_NEW_SOURCE_LINK);
     659//        newSourceInfo.setVersion(null);
     660//       
     661//        Version result = daoDispatcher.createVersion(newSourceInfo);
     662//        assertFalse(null == result.getVersion()); // will be chnaged once the schema for version is fixed: ID is added
     663//       
     664//    }
    603665}
     666
     667
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/resources/test-data/InsertTestData.sql

    r3413 r3448  
    2222INSERT INTO notebook (title, owner_id, external_id) VALUES ('a notebook', 2, '00000000-0000-0000-0000-000000000001'); -- 1
    2323-- INSERT INTO notebook (title, owner_id, external_id) VALUES ('a notebook', 1, 1);
     24
     25
    2426INSERT INTO annotation (owner_id,headline,body_xml, external_id) VALUES (1, 'a headline','<html><body>some html</body></html>', '00000000-0000-0000-0000-000000000005'); --1
    2527
Note: See TracChangeset for help on using the changeset viewer.