Changeset 3411


Ignore:
Timestamp:
08/18/13 20:14:50 (11 years ago)
Author:
olhsha
Message:

adding 3 more test to Dispatcher

Location:
DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src
Files:
2 deleted
4 edited

Legend:

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

    r3408 r3411  
    9090 
    9191   
     92    public boolean sourceIsInUse(Number sourceID);
    9293}
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/dao/impl/JdbcSourceDao.java

    r3408 r3411  
    175175   
    176176
    177     //////////// helpers ///////////////////////
    178     /////////////////////////////////////////////////
    179    
    180       //////////////////////////////
    181     private boolean sourceIsInUse(Number sourceID) {
     177    @Override
     178    public boolean sourceIsInUse(Number sourceID) {
    182179        String sql = "SELECT " + annotation_id + " FROM " + annotationsSourcesTableName + " WHERE " + source_id + "= ? LIMIT 1";
    183180        List<Number> result = getSimpleJdbcTemplate().query(sql, annotationIDRowMapper, sourceID);
     
    195192    };
    196193   
    197  
     194  /////////// HELPERS  ////////////////
    198195   
    199196
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/main/java/eu/dasish/annotation/backend/rest/DaoDispatcher.java

    r3409 r3411  
    6464    @Autowired
    6565    NotebookDao notebookDao;
    66    
    67    
    6866
    6967    public Number getAnnotationInternalIdentifier(AnnotationIdentifier annotationIdentifier) {
     
    157155    public int[] deleteSourceWithVersions(Number sourceID) throws SQLException {
    158156        int[] result = new int[3];
    159         List<Number> versions = sourceDao.retrieveVersionList(sourceID);
    160         result[1] = sourceDao.deleteAllSourceVersion(sourceID);
    161         result[0] = sourceDao.deleteSource(sourceID);
    162         result[3] = 0;
    163         for (Number versionID : versions) {
    164             int[] deleteVersion = deleteVersionWithCachedRepresentations(versionID);
    165             result[3] = result[3] + deleteVersion[1];
     157        if (!sourceDao.sourceIsInUse(sourceID)) {
     158            List<Number> versions = sourceDao.retrieveVersionList(sourceID);
     159            result[1] = sourceDao.deleteAllSourceVersion(sourceID);
     160            result[0] = sourceDao.deleteSource(sourceID);
     161            result[3] = 0;
     162            for (Number versionID : versions) {
     163                int[] deleteVersion = deleteVersionWithCachedRepresentations(versionID);
     164                result[3] = result[3] + deleteVersion[1];
     165            }
     166        } else {
     167            result[0] = 0;
     168            result[1] = 0;
     169            result[2] = 0;
    166170        }
    167171        return result;
     
    202206     * @return the mapping temporarySourceID -> peristenExternalSOurceId adds a
    203207     * source from "sources" to the DB if it is not there, to the table
    204      * "target_source" adds the wro (annotationID, sourceID) to the joint table
    205      * "annotations_sources"
     208     * "target_source" adds  (annotationID, sourceID) to the joint table "annotations_sources"
    206209     * @throws SQLException
    207210     */
  • DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/rest/DaoDispatcherTest.java

    r3410 r3411  
    1818package eu.dasish.annotation.backend.rest;
    1919
    20 import eu.dasish.annotation.backend.rest.DaoDispatcher;
    2120import eu.dasish.annotation.backend.TestBackendConstants;
    2221import eu.dasish.annotation.backend.dao.AnnotationDao;
     
    2827import eu.dasish.annotation.backend.identifiers.AnnotationIdentifier;
    2928import eu.dasish.annotation.backend.identifiers.CachedRepresentationIdentifier;
     29import eu.dasish.annotation.backend.identifiers.SourceIdentifier;
    3030import eu.dasish.annotation.backend.identifiers.UserIdentifier;
     31import eu.dasish.annotation.backend.identifiers.VersionIdentifier;
    3132import eu.dasish.annotation.schema.Annotation;
    3233import eu.dasish.annotation.schema.CachedRepresentationInfo;
    3334import eu.dasish.annotation.schema.NewOrExistingSourceInfo;
    3435import eu.dasish.annotation.schema.NewSourceInfo;
     36import eu.dasish.annotation.schema.Source;
    3537import eu.dasish.annotation.schema.SourceInfo;
    3638import java.sql.SQLException;
    3739import java.sql.Timestamp;
    3840import java.util.ArrayList;
     41import java.util.HashMap;
    3942import java.util.List;
    4043import java.util.Map;
     
    245248        //Another test
    246249       
     250       
    247251        mockery.checking(new Expectations() {
    248252            {
     
    261265
    262266    @Test
    263     public void testDeleteSource() throws SQLException {
    264 
     267    public void testDeleteSourceSourceWithVersions() throws SQLException {
    265268        // test 1
     269         mockery.checking(new Expectations() {
     270            {
     271                oneOf(sourceDao).sourceIsInUse(5);
     272                will(returnValue(true));
     273            }
     274        });
     275
    266276        int[] result = daoDispatcher.deleteSourceWithVersions(1); //the source is in use, should not be deleted
    267277        assertEquals(0, result[0]); //
     
    269279
    270280        // test 2
    271         final int[] versionDeleted = new int[3];
    272         versionDeleted[0] = 0; // versions_cahced_representations
    273         versionDeleted[1] = 1; // version deleted
    274         versionDeleted[2] = 0; // deleted cached representations; version 7 does not have them
     281       
     282        final List<Number> versionList = new ArrayList<Number>();
     283        versionList.add(7);
     284        mockery.checking(new Expectations() {
     285            {
     286                oneOf(sourceDao).sourceIsInUse(5);
     287                will(returnValue(false));
     288               
     289                oneOf(sourceDao).retrieveVersionList(5);
     290                will(returnValue(versionList));
     291
     292                oneOf(sourceDao).deleteAllSourceVersion(5);
     293                will(returnValue(1));
     294               
     295                oneOf(sourceDao).deleteSource(5);
     296                will(returnValue(1));
     297               
     298                oneOf(versionDao).versionIsInUse(7);
     299                will(returnValue(false));
     300            }
     301        });
     302       
     303       
     304        int[] resultTwo = daoDispatcher.deleteSourceWithVersions(5);// the source will be deleted because it is not referred by any annotation
     305        assertEquals(3, resultTwo.length);
     306        assertEquals(1, resultTwo[0]); // source 7 is deleted
     307        assertEquals(1, resultTwo[1]); // row (5,7) in "sorces_versions" is deleted
     308        assertEquals(0, resultTwo[2]); // version 7 is not foub=nd, not in use
     309    }
     310
     311 
     312   
     313
     314    /**
     315     * Test of addSourceAndPairSourceVersion method, of class DaoDispatcher.
     316     */
     317    @Test
     318    public void testAddSourceAndPairSourceVersion() throws Exception {
     319        System.out.println("addSourceAndPairSourceVersion");
     320       final  NewSourceInfo newSource = new NewSourceInfo();
     321       newSource.setLink(TestBackendConstants._TEST_NEW_SOURCE_LINK);
     322       newSource.setVersion(TestBackendConstants._TEST_VERSION_3_EXT_ID);// already added version, existing
     323       
     324        mockery.checking(new Expectations() {
     325            {
     326                oneOf(versionDao).getInternalID(new VersionIdentifier(TestBackendConstants._TEST_VERSION_3_EXT_ID));
     327                will(returnValue(3));
     328
     329                oneOf(sourceDao).addSource(with(aNonNull(Source.class)));
     330                will(returnValue(6));
     331               
     332                oneOf(sourceDao).addSourceVersion(6, 3);
     333                will(returnValue(1));
     334
     335            }
     336        });
     337       
     338        Number result = daoDispatcher.addSourceAndPairSourceVersion(newSource);
     339        assertEquals(6, result.intValue());
     340       
     341        // Another test
     342       
     343        final  NewSourceInfo newSourceTwo = new NewSourceInfo();
     344        newSourceTwo.setLink(TestBackendConstants._TEST_NEW_SOURCE_LINK);
     345        newSourceTwo.setVersion(TestBackendConstants._TEST_VERSION_NONEXIST_EXT_ID);
     346        mockery.checking(new Expectations() {
     347            {
     348                oneOf(versionDao).getInternalID(new VersionIdentifier(TestBackendConstants._TEST_VERSION_NONEXIST_EXT_ID));
     349                will(returnValue(null));
     350
     351            }
     352        });
     353        Number resultTwo = daoDispatcher.addSourceAndPairSourceVersion(newSourceTwo);
     354        assertEquals(-1, resultTwo.intValue());
     355       
     356       
     357    }
     358
     359    /**
     360     * Test of addTargetSourcesToAnnotation method, of class DaoDispatcher.
     361     */
     362    @Test
     363    public void testAddTargetSourcesToAnnotation() throws Exception {
     364        System.out.println("addTargetSourcesToAnnotation");
     365       
     366        NewOrExistingSourceInfo noesi = new NewOrExistingSourceInfo();
     367        NewSourceInfo nsi = new NewSourceInfo();
     368        nsi.setLink(TestBackendConstants._TEST_NEW_SOURCE_LINK);
     369        nsi.setId(TestBackendConstants._TEST_TEMP_SOURCE_ID);
     370        nsi.setVersion(null);
     371        noesi.setNewSource(nsi);
     372
     373
     374        NewOrExistingSourceInfo noesiTwo = new NewOrExistingSourceInfo();
     375        SourceInfo si = new SourceInfo();
     376        si.setLink(TestBackendConstants._TEST_NEW_SOURCE_LINK);
     377        final SourceIdentifier sourceIdentifier = new SourceIdentifier();
     378        si.setRef(sourceIdentifier.toString());
     379        si.setVersion(null);
     380        noesiTwo.setSource(si);
     381
     382        final Map<NewOrExistingSourceInfo, NewOrExistingSourceInfo> map = new HashMap<NewOrExistingSourceInfo, NewOrExistingSourceInfo>();
     383        map.put(noesi, noesiTwo);
     384
     385
     386//       
    275387//        mockery.checking(new Expectations() {
    276388//            {
    277 //                oneOf(versionDao).deleteVersion(7);
    278 //                will(returnValue(versionDeleted)); // no other sources refer to this version # 5
    279 //            }
    280 //        });
    281 
    282         int[] resultTwo = daoDispatcher.deleteSourceWithVersions(5);// the source will be deleted because it is not referred by any annotation
    283         assertEquals(1, resultTwo[0]); // row (5,7) in "sorces_versions" is deleted
    284         assertEquals(1, resultTwo[1]);
    285     }
    286 
    287     /**
    288      * Test of addVersion method, of class JdbcVersionDao.
    289      */
    290 //    @Test
    291 //    public void testAddCachedForVersion() {
    292 //        System.out.println("test addCachedForVersion");
    293 //
    294 //        final CachedRepresentationInfo cached = new CachedRepresentationInfo();
    295 //        cached.setMimeType("text/plain");
    296 //        cached.setTool("vi");
    297 //        cached.setType("text");
    298 //        cached.setRef(null);
    299 //       
    300 //        Number[] result = jdbcCachedRepresentationDao.addCachedForVersion(6, cached);
    301 //        assertEquals(8, result[0].intValue());
    302 //        assertEquals(1, result[1].intValue());
    303 //    }
    304     /**
    305      *
    306      */
    307 //    @Test
    308 //    public void tesDeleteCachedForVersion() {
    309 //        System.out.println("test delete CachedRepresentationForVersion");
    310 //        System.out.println("deleteVersion");
    311 //       
    312 //        int[] result = jdbcCachedRepresentationDao.deleteCachedForVersion(6, 5);
    313 //        assertEquals(1, result[0]); //versions-cached
    314 //        assertEquals(0, result[1]);//cached 5 is in use
    315 //       
    316 //        int[] resultTwo = jdbcCachedRepresentationDao.deleteCachedForVersion(6, 4); // no such pair
    317 //        assertEquals(0, resultTwo[0]);
    318 //        assertEquals(0, resultTwo[1]);
    319 //
    320 //
    321 //    }
    322 //
    323 //    /**
    324 //     * Test of deleteVersionWithCachedRepresentations method, of class DaoDispatcher.
    325 //     */
    326 //    @Test
    327 //    public void testDeleteVersionWithCachedRepresentations() {
    328 //        System.out.println("deleteVersionWithCachedRepresentations");
    329 //        Number versionID = null;
    330 //        DaoDispatcher instance = new DaoDispatcher();
    331 //        int[] expResult = null;
    332 //        int[] result = instance.deleteVersionWithCachedRepresentations(versionID);
    333 //        assertArrayEquals(expResult, result);
    334 //        // TODO review the generated test code and remove the default call to fail.
    335 //        fail("The test case is a prototype.");
    336 //    }
    337     /**
    338      * Test of deleteSourceWithVersions method, of class DaoDispatcher.
    339      */
    340     @Test
    341     public void testDeleteSourceWithVersions() throws SQLException {
    342         System.out.println("deleteSourceWithVersions");
    343         Number sourceID = null;
    344         DaoDispatcher instance = new DaoDispatcher();
    345         int[] expResult = null;
    346         int[] result = instance.deleteSourceWithVersions(sourceID);
    347         assertArrayEquals(expResult, result);
    348         // TODO review the generated test code and remove the default call to fail.
    349         fail("The test case is a prototype.");
    350     }
    351 
    352     /**
    353      * Test of addSourceAndPairSourceVersion method, of class DaoDispatcher.
    354      */
    355     @Test
    356     public void testAddSourceAndPairSourceVersion() throws Exception {
    357         System.out.println("addSourceAndPairSourceVersion");
    358         NewSourceInfo newSource = null;
    359         DaoDispatcher instance = new DaoDispatcher();
    360         Number expResult = null;
    361         Number result = instance.addSourceAndPairSourceVersion(newSource);
    362         assertEquals(expResult, result);
    363         // TODO review the generated test code and remove the default call to fail.
    364         fail("The test case is a prototype.");
    365     }
    366 
    367     /**
    368      * Test of addTargetSourcesToAnnotation method, of class DaoDispatcher.
    369      */
    370     @Test
    371     public void testAddTargetSourcesToAnnotation() throws Exception {
    372         System.out.println("addTargetSourcesToAnnotation");
    373 
    374 
    375 //        NewOrExistingSourceInfo noesi = new NewOrExistingSourceInfo();
    376 //        NewSourceInfo nsi = new NewSourceInfo();
    377 //        nsi.setLink(TestBackendConstants._TEST_NEW_SOURCE_LINK);
    378 //        nsi.setId(TestBackendConstants._TEST_TEMP_SOURCE_ID);
    379 //        nsi.setVersion(null);
    380 //        noesi.setNewSource(nsi);
    381 //
    382 //
    383 //        NewOrExistingSourceInfo noesiTwo = new NewOrExistingSourceInfo();
    384 //        SourceInfo si = new SourceInfo();
    385 //        si.setLink(TestBackendConstants._TEST_NEW_SOURCE_LINK);
    386 //        si.setRef((new SourceIdentifier()).toString());
    387 //        si.setVersion(null);
    388 //        noesiTwo.setSource(si);
    389 //
    390 //        final Map<NewOrExistingSourceInfo, NewOrExistingSourceInfo> map = new HashMap<NewOrExistingSourceInfo, NewOrExistingSourceInfo>();
    391 //        map.put(noesi, noesiTwo);
    392 //
    393 //
    394 //        mockery.checking(new Expectations() {
    395 //            {
     389//                oneOf(sourceDao.getInternalID(sourceIdentifier));
     390//                will(returnValue());
     391//               
     392//                oneOf(annotationDao).addAnnotationSourcePair(with(aNonNull(Number.class)), with(aNonNull(Number.class)));
     393//                will(returnValue(1));
     394//               
    396395//                oneOf(sourceDao).addTargetSources(with(aNonNull(Number.class)), with(aNonNull(List.class)));
    397396//                will(returnValue(map));
     
    400399
    401400
    402         Number annotationID = null;
    403         List<NewOrExistingSourceInfo> sources = null;
    404         DaoDispatcher instance = new DaoDispatcher();
    405         Map expResult = null;
    406         Map result = instance.addTargetSourcesToAnnotation(annotationID, sources);
    407         assertEquals(expResult, result);
    408         // TODO review the generated test code and remove the default call to fail.
    409         fail("The test case is a prototype.");
     401       
    410402    }
    411403
Note: See TracChangeset for help on using the changeset viewer.