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

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

safe removing sources (if not other annotation refers) is incorporated into remove annotation

File size: 16.2 KB
Line 
1/*
2 * Copyright (C) 2013 DASISH
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 */
18package eu.dasish.annotation.backend.dao.impl;
19
20import eu.dasish.annotation.backend.TestBackendConstants;
21import eu.dasish.annotation.backend.TestInstances;
22import eu.dasish.annotation.backend.dao.NotebookDao;
23import eu.dasish.annotation.backend.dao.PermissionsDao;
24import eu.dasish.annotation.backend.dao.SourceDao;
25import eu.dasish.annotation.backend.identifiers.AnnotationIdentifier;
26import eu.dasish.annotation.backend.identifiers.SourceIdentifier;
27import eu.dasish.annotation.schema.Annotation;
28import eu.dasish.annotation.schema.AnnotationInfo;
29import eu.dasish.annotation.schema.NewOrExistingSourceInfo;
30import eu.dasish.annotation.schema.NewOrExistingSourceInfos;
31import eu.dasish.annotation.schema.NewSourceInfo;
32import eu.dasish.annotation.schema.ResourceREF;
33import eu.dasish.annotation.schema.SourceInfo;
34import java.sql.SQLException;
35import java.util.ArrayList;
36import java.util.HashMap;
37import java.util.List;
38import java.util.Map;
39import org.jmock.Expectations;
40import org.jmock.Mockery;
41import static org.junit.Assert.*;
42import org.junit.Ignore;
43import org.junit.Test;
44import org.junit.runner.RunWith;
45import org.springframework.beans.factory.annotation.Autowired;
46import org.springframework.test.context.ContextConfiguration;
47import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
48
49/**
50 *
51 * @author olhsha
52 */
53@RunWith(SpringJUnit4ClassRunner.class)
54@ContextConfiguration({"/spring-test-config/dataSource.xml", "/spring-test-config/mockery.xml", "/spring-test-config/mockNotebookDao.xml",
55    "/spring-test-config/mockUserDao.xml", "/spring-test-config/mockPermissionsDao.xml", "/spring-test-config/mockSourceDao.xml", "/spring-config/annotationDao.xml"})
56public class JdbcAnnotationDaoTest extends JdbcResourceDaoTest {
57
58    @Autowired
59    JdbcAnnotationDao jdbcAnnotationDao;
60    @Autowired
61    private PermissionsDao permissionsDao;
62    @Autowired
63    private NotebookDao notebookDao;
64    @Autowired
65    private SourceDao sourceDao;
66    @Autowired
67    private Mockery mockery;
68    TestInstances testInstances = new TestInstances();
69
70    /**
71     * Test of getAnnotationInfos method, of class JdbcAnnotationDao.
72     * List<AnnotationInfo> getAnnotationInfos(List<Number> annotationIDs)
73     */
74    @Test
75    public void testGetAnnotationInfos() {
76        System.out.println("getAnnotationInfos");
77        List<Number> annotIds = new ArrayList<Number>();
78        annotIds.add(2);
79        annotIds.add(3);
80        annotIds.add(4);
81
82        final List<AnnotationInfo> annotationInfos = jdbcAnnotationDao.getAnnotationInfos(annotIds);
83        assertEquals(3, annotationInfos.size());
84
85        assertEquals(TestBackendConstants._TEST_ANNOT_2_HEADLINE, annotationInfos.get(0).getHeadline());
86        assertEquals(String.valueOf(TestBackendConstants._TEST_ANNOT_2_OWNER), annotationInfos.get(0).getOwner().getRef());
87        //assertEquals(TestBackendConstants._TEST_ANNOT_1_TARGETS, annotationInfos.get(0).getTargetSources());
88
89        assertEquals(TestBackendConstants._TEST_ANNOT_3_HEADLINE, annotationInfos.get(1).getHeadline());
90        assertEquals(String.valueOf(TestBackendConstants._TEST_ANNOT_3_OWNER), annotationInfos.get(1).getOwner().getRef());
91        //assertEquals(TestBackendConstants._TEST_ANNOT_2_TARGETS, annotationInfos.get(1).getTargetSources());
92
93        assertEquals(TestBackendConstants._TEST_ANNOT_4_HEADLINE, annotationInfos.get(2).getHeadline());
94        assertEquals(String.valueOf(TestBackendConstants._TEST_ANNOT_4_OWNER), annotationInfos.get(2).getOwner().getRef());
95        //assertEquals(TestBackendConstants._TEST_ANNOT_3_TARGETS, annotationInfos.get(2).getTargetSources());
96
97        final List<AnnotationInfo> annotationInfosNull = jdbcAnnotationDao.getAnnotationInfos(null);
98        assertEquals(null, annotationInfosNull);
99
100        final List<AnnotationInfo> annotationInfosZeroSize = jdbcAnnotationDao.getAnnotationInfos(new ArrayList<Number>());
101        assertEquals(0, annotationInfosZeroSize.size());
102
103
104    }
105
106    /**
107     * Test of getAnnotationREFs method, of class JdbcAnnotationDao.
108     * List<ResourceREF> getAnnotationREFs(List<Number> annotationIDs)
109     */
110    @Test
111    public void testGetAnnotationREFs() {
112        System.out.println("getAnnotationREFs");
113        List<Number> annotIds = new ArrayList<Number>();
114        annotIds.add(2);
115        annotIds.add(3);
116        annotIds.add(4);
117
118        final List<ResourceREF> testList = jdbcAnnotationDao.getAnnotationREFs(annotIds);
119        assertEquals(3, testList.size());
120        assertEquals(String.valueOf(2), testList.get(0).getRef());
121        assertEquals(String.valueOf(3), testList.get(1).getRef());
122        assertEquals(String.valueOf(4), testList.get(2).getRef());
123
124        final List<ResourceREF> testListTwo = jdbcAnnotationDao.getAnnotationREFs(new ArrayList<Number>());
125        assertEquals(0, testListTwo.size());
126
127        final List<ResourceREF> testListThree = jdbcAnnotationDao.getAnnotationREFs(null);
128        assertEquals(null, testListThree);
129
130    }
131
132    /**
133     *
134     * Test of getAnnotationID method, of class JdbcAnnotationDao. Integer
135     * getAnnotationID(AnnotationIdentifier externalID)
136     */
137    @Test
138    public void getInternalID() throws SQLException {
139        System.out.println("test getInternalID");
140
141        final Number annotaionId = jdbcAnnotationDao.getInternalID(new AnnotationIdentifier(TestBackendConstants._TEST_ANNOT_2_EXT));
142        assertEquals(2, annotaionId.intValue());
143
144        final Number annotaionIdNE = jdbcAnnotationDao.getInternalID(new AnnotationIdentifier(TestBackendConstants._TEST_ANNOT_7_EXT_NOT_IN_DB));
145        assertEquals(null, annotaionIdNE);
146
147        final Number annotaionIdNull = jdbcAnnotationDao.getInternalID(null);
148        assertEquals(null, annotaionIdNull);
149    }
150
151    /**
152     *
153     * Test of getAnnotation method, of class JdbcAnnotationDao. Annotation
154     * getAnnotation(Number annotationlID)
155     */
156    @Test
157    public void getAnnotation() throws SQLException {
158        System.out.println("test getAnnotation");
159
160        /// dummy test
161        final Annotation annotaionNull = jdbcAnnotationDao.getAnnotation(null);
162        assertEquals(null, annotaionNull);
163        ////
164
165        final Number testAnnotationID = 2;
166
167
168        SourceInfo sourceOneInfo = new SourceInfo();
169        sourceOneInfo.setLink(TestBackendConstants._TEST_SOURCE_1_LINK);
170        sourceOneInfo.setRef(TestBackendConstants._TEST_SOURCE_1_EXT_ID);
171        sourceOneInfo.setVersion(Integer.toString(TestBackendConstants._TEST_SOURCE_1_VERSION_ID));
172
173        SourceInfo sourceTwoInfo = new SourceInfo();
174        sourceTwoInfo.setLink(TestBackendConstants._TEST_SOURCE_2_LINK);
175        sourceTwoInfo.setRef(TestBackendConstants._TEST_SOURCE_2_EXT_ID);
176        sourceTwoInfo.setVersion(Integer.toString(TestBackendConstants._TEST_SOURCE_2_VERSION_ID));
177
178        final List<SourceInfo> sourceInfoList = new ArrayList<SourceInfo>();
179        sourceInfoList.add(sourceOneInfo);
180        sourceInfoList.add(sourceTwoInfo);
181
182        NewOrExistingSourceInfo noeSourceOneInfo = new NewOrExistingSourceInfo();
183        noeSourceOneInfo.setSource(sourceOneInfo);
184        NewOrExistingSourceInfo noeSourceTwoInfo = new NewOrExistingSourceInfo();
185        noeSourceTwoInfo.setSource(sourceTwoInfo);
186
187        List<NewOrExistingSourceInfo> noeSourceInfoList = new ArrayList<NewOrExistingSourceInfo>();
188        noeSourceInfoList.add(noeSourceOneInfo);
189        noeSourceInfoList.add(noeSourceTwoInfo);
190        final NewOrExistingSourceInfos noeSourceInfos = new NewOrExistingSourceInfos();
191        noeSourceInfos.getTarget().addAll(noeSourceInfoList);
192
193        mockery.checking(new Expectations() {
194            {
195                oneOf(sourceDao).getSourceInfos(testAnnotationID);
196                will(returnValue(sourceInfoList));
197
198                oneOf(sourceDao).contructNewOrExistingSourceInfo(sourceInfoList);
199                will(returnValue(noeSourceInfos));
200            }
201        });
202
203
204        final Annotation annotation = jdbcAnnotationDao.getAnnotation(testAnnotationID);
205        assertEquals(TestBackendConstants._TEST_ANNOT_2_HEADLINE, annotation.getHeadline());
206        assertEquals(String.valueOf(TestBackendConstants._TEST_ANNOT_2_OWNER), annotation.getOwner().getRef());
207        assertEquals(TestBackendConstants._TEST_ANNOT_2_BODY, annotation.getBody().getAny().get(0)); // when the body is elaborated it may be changed
208
209        assertEquals(sourceOneInfo.getRef(), annotation.getTargetSources().getTarget().get(0).getSource().getRef());
210        assertEquals(sourceOneInfo.getLink(), annotation.getTargetSources().getTarget().get(0).getSource().getLink());
211        assertEquals(sourceOneInfo.getVersion(), annotation.getTargetSources().getTarget().get(0).getSource().getVersion());
212
213        assertEquals(sourceTwoInfo.getRef(), annotation.getTargetSources().getTarget().get(1).getSource().getRef());
214        assertEquals(sourceTwoInfo.getLink(), annotation.getTargetSources().getTarget().get(1).getSource().getLink());
215        assertEquals(sourceTwoInfo.getVersion(), annotation.getTargetSources().getTarget().get(1).getSource().getVersion());
216
217        assertEquals(TestBackendConstants._TEST_ANNOT_2_EXT, annotation.getURI());
218
219        assertEquals(TestBackendConstants._TEST_ANNOT_2_TIME_STAMP, annotation.getTimeStamp().toString());
220
221
222    }
223
224    /**
225     * Test of deletAnnotation method, of class JdbcAnnotationDao.
226     */
227    @Test
228    public void testDeleteAnnotation() throws SQLException {
229        System.out.println("deleteAnnotation");
230        final List<Number> sourceIDs = new ArrayList<Number>();
231        sourceIDs.add(3);
232        sourceIDs.add(4);
233
234        mockery.checking(new Expectations() {
235            {
236                oneOf(sourceDao).retrieveSourceIDs(5);
237                will(returnValue(sourceIDs));
238               
239                oneOf(sourceDao).deleteSource(sourceIDs.get(0));
240                will(returnValue(0));
241               
242                oneOf(sourceDao).deleteSource(sourceIDs.get(1));
243                will(returnValue(1));
244            }
245        });
246
247        int result = jdbcAnnotationDao.deleteAnnotation(5);
248        assertEquals(1, result);
249        // now, try to delete the same annotation one more time
250        // if it has been already deleted then the method under testing should return 0
251       
252        mockery.checking(new Expectations() {
253            {
254                oneOf(sourceDao).retrieveSourceIDs(5);
255                will(returnValue(new ArrayList<Number>()));               
256            }
257        });
258        result = jdbcAnnotationDao.deleteAnnotation(5);
259        assertEquals(0, result);
260    }
261
262    /**
263     * Test of addAnnotation method, of class JdbcAnnotationDao.
264     */
265    @Test
266    public void testAddAnnotationExistingSource() throws SQLException {
267        System.out.println("test_addAnnotation with an existing source");
268
269        Annotation annotationToAdd = testInstances.getAnnotationToAdd();// existing sources
270        assertEquals(null, annotationToAdd.getURI());
271        assertEquals(null, annotationToAdd.getTimeStamp());
272
273        NewOrExistingSourceInfo noesi = new NewOrExistingSourceInfo();
274        SourceInfo si = new SourceInfo();
275        si.setLink(TestBackendConstants._TEST_SOURCE_1_LINK);
276        si.setRef(TestBackendConstants._TEST_SOURCE_1_EXT_ID);
277        si.setVersion(TestBackendConstants._TEST_VERSION_1_EXT_ID);
278        noesi.setSource(si);
279
280        final Map<NewOrExistingSourceInfo, NewOrExistingSourceInfo> map = new HashMap<NewOrExistingSourceInfo, NewOrExistingSourceInfo>();
281        map.put(noesi, noesi);
282
283
284        mockery.checking(new Expectations() {
285            {
286                oneOf(sourceDao).addTargetSources(with(aNonNull(Number.class)), with(aNonNull(List.class)));
287                will(returnValue(map));
288            }
289        });
290
291        Annotation result = jdbcAnnotationDao.addAnnotation(annotationToAdd, 5);
292        assertFalse(null == result.getURI());
293        assertFalse(null == result.getTimeStamp());
294        assertEquals(annotationToAdd.getBody().getAny().get(0), result.getBody().getAny().get(0));
295        assertEquals(annotationToAdd.getHeadline(), result.getHeadline());
296        assertEquals(String.valueOf(5), result.getOwner().getRef());
297        assertEquals(annotationToAdd.getPermissions(), result.getPermissions());
298
299        SourceInfo expectedSi = annotationToAdd.getTargetSources().getTarget().get(0).getSource();
300        SourceInfo resultSi = result.getTargetSources().getTarget().get(0).getSource();
301        assertEquals(expectedSi.getLink(), resultSi.getLink());
302        assertEquals(expectedSi.getRef(), resultSi.getRef());
303        assertEquals(expectedSi.getVersion(), resultSi.getVersion());
304    }
305
306    /**
307     * Test of addAnnotation method, of class JdbcAnnotationDao.
308     */
309    @Test
310    public void testAddAnnotationNewSource() throws SQLException {
311        System.out.println("test_addAnnotation with a new source");
312
313
314        Annotation annotationToAdd = testInstances.getAnnotationToAddNewSource();// existing sources
315        assertEquals(null, annotationToAdd.getURI());
316        assertEquals(null, annotationToAdd.getTimeStamp());
317
318
319        NewOrExistingSourceInfo noesi = new NewOrExistingSourceInfo();
320        NewSourceInfo nsi = new NewSourceInfo();
321        nsi.setLink(TestBackendConstants._TEST_NEW_SOURCE_LINK);
322        nsi.setId(TestBackendConstants._TEST_TEMP_SOURCE_ID);
323        nsi.setVersion(null);
324        noesi.setNewSource(nsi);
325
326
327        NewOrExistingSourceInfo noesiTwo = new NewOrExistingSourceInfo();
328        SourceInfo si = new SourceInfo();
329        si.setLink(TestBackendConstants._TEST_NEW_SOURCE_LINK);
330        si.setRef((new SourceIdentifier()).toString());
331        si.setVersion(null);
332        noesiTwo.setSource(si);
333
334        final Map<NewOrExistingSourceInfo, NewOrExistingSourceInfo> map = new HashMap<NewOrExistingSourceInfo, NewOrExistingSourceInfo>();
335        map.put(noesi, noesiTwo);
336
337
338        mockery.checking(new Expectations() {
339            {
340                oneOf(sourceDao).addTargetSources(with(aNonNull(Number.class)), with(aNonNull(List.class)));
341                will(returnValue(map));
342            }
343        });
344
345        Annotation result = jdbcAnnotationDao.addAnnotation(annotationToAdd, 5);
346        assertFalse(null == result.getURI());
347        assertFalse(null == result.getTimeStamp());
348        assertEquals(annotationToAdd.getHeadline(), result.getHeadline());
349        assertEquals(String.valueOf(5), result.getOwner().getRef());
350        assertEquals(annotationToAdd.getPermissions(), result.getPermissions());
351
352        NewSourceInfo expectedSi = annotationToAdd.getTargetSources().getTarget().get(0).getNewSource();
353        SourceInfo resultSi = result.getTargetSources().getTarget().get(0).getSource();
354        assertEquals(expectedSi.getLink(), resultSi.getLink());
355        assertEquals(expectedSi.getVersion(), resultSi.getVersion());
356        //the reference is replaced with the persistent one
357        assertEquals(si.getRef(), resultSi.getRef());
358        ///// 
359
360        // checking the bodies: the temporary reference should be replaced
361        String expBody = annotationToAdd.getBody().getAny().get(0).toString().replaceAll(TestBackendConstants._TEST_TEMP_SOURCE_ID, si.getRef());
362        assertEquals(expBody, result.getBody().getAny().get(0).toString());
363    }
364
365    @Test
366    @Ignore
367    public void testGetExternalID() {
368        System.out.println("getAnnotationID");
369
370        final AnnotationIdentifier externalId = jdbcAnnotationDao.getExternalID(2);
371        assertEquals(new AnnotationIdentifier(TestBackendConstants._TEST_ANNOT_2_EXT), externalId);
372
373
374        final AnnotationIdentifier externalIdThree = jdbcAnnotationDao.getExternalID(null);
375        assertEquals(null, externalIdThree.getUUID());
376
377    }
378    //////////// helpers //////////////////////
379}
Note: See TracBrowser for help on using the repository browser.