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

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

AnnotationResource? and its test are adjusted, however: there must an interface for DaoDispatcher? to be able to mock it. Moreover make a bean for it as well, and remove comnent annotation, so it can be autowired.

File size: 14.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.Helpers;
21import eu.dasish.annotation.backend.TestBackendConstants;
22import eu.dasish.annotation.backend.TestInstances;
23import eu.dasish.annotation.backend.identifiers.AnnotationIdentifier;
24import eu.dasish.annotation.schema.Annotation;
25import eu.dasish.annotation.schema.AnnotationInfo;
26import eu.dasish.annotation.schema.Permission;
27import eu.dasish.annotation.schema.ResourceREF;
28import java.sql.SQLException;
29import java.sql.Timestamp;
30import java.util.ArrayList;
31import java.util.List;
32import java.util.Map;
33import static org.junit.Assert.*;
34import org.junit.Test;
35import org.junit.runner.RunWith;
36import org.springframework.beans.factory.annotation.Autowired;
37import org.springframework.test.context.ContextConfiguration;
38import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
39
40/**
41 *
42 * @author olhsha
43 */
44@RunWith(SpringJUnit4ClassRunner.class)
45@ContextConfiguration({"/spring-test-config/dataSource.xml", "/spring-config/annotationDao.xml"})
46public class JdbcAnnotationDaoTest extends JdbcResourceDaoTest {
47
48    @Autowired
49    JdbcAnnotationDao jdbcAnnotationDao;   
50    TestInstances testInstances = new TestInstances();
51
52   
53      /**
54     * Test of retrieveSourceIDs method, of class JdbcAnnotationDao.
55     */
56    @Test
57    public void testRetrieveSourceIDs() {
58        System.out.println("retrieveSourceIDs");
59        Number annotationID = 2;
60        List<Number> result = jdbcAnnotationDao.retrieveSourceIDs(annotationID);
61        assertEquals(2, result.size());
62        assertEquals(1, result.get(0));
63        assertEquals(2, result.get(1));
64    }
65   
66    ///////////////////////////////////////////
67    @Test
68    public void testDeleteAllAnnotationSource() throws SQLException{
69        System.out.println("test deleteAllAnnotationSources");
70        assertEquals(2, jdbcAnnotationDao.deleteAllAnnotationSource(2));
71        assertEquals(0, jdbcAnnotationDao.deleteAllAnnotationSource(2));
72    }
73   
74    ///////////////////////////////////////////
75    @Test
76    public void testDeleteAnnotationPrinciplePermissions() throws SQLException{
77        System.out.println("test deleteAllAnnotationSources");
78        int result = jdbcAnnotationDao.deleteAnnotationPrincipalPermissions(2);
79        assertEquals(3, result);
80        assertEquals(0, jdbcAnnotationDao.deleteAnnotationPrincipalPermissions(2));
81    }
82   
83    ///////////////////////////////////////////
84    @Test
85    public void testAddAnnotationPrinciplePermission() throws SQLException{
86        System.out.println("test addAnnotationSources");
87        int result = jdbcAnnotationDao.addAnnotationPrincipalPermission(2, 1, Permission.READER);
88        assertEquals(1, result);
89    }
90   
91    ///////////////////////////////////////////
92    @Test
93    public void testAddAnnotationSourcePair() throws SQLException{
94        System.out.println("test addAnnotationSourcePair");
95        assertEquals(1, jdbcAnnotationDao.addAnnotationSourcePair(1,2));
96    }
97   
98    ////////////////////////////////
99   
100    @Test
101    public void testGetAnnotationInfos() {
102        System.out.println("getAnnotationInfos");
103        List<Number> annotIds = new ArrayList<Number>();
104        annotIds.add(2);
105        annotIds.add(3);
106        annotIds.add(4);
107
108        final List<AnnotationInfo> annotationInfos = jdbcAnnotationDao.getAnnotationInfos(annotIds);
109        assertEquals(3, annotationInfos.size());
110
111        assertEquals(TestBackendConstants._TEST_ANNOT_2_HEADLINE, annotationInfos.get(0).getHeadline());
112        assertEquals(String.valueOf(TestBackendConstants._TEST_ANNOT_2_OWNER), annotationInfos.get(0).getOwner().getRef());
113        //assertEquals(TestBackendConstants._TEST_ANNOT_1_TARGETS, annotationInfos.get(0).getTargetSources());
114
115        assertEquals(TestBackendConstants._TEST_ANNOT_3_HEADLINE, annotationInfos.get(1).getHeadline());
116        assertEquals(String.valueOf(TestBackendConstants._TEST_ANNOT_3_OWNER), annotationInfos.get(1).getOwner().getRef());
117        //assertEquals(TestBackendConstants._TEST_ANNOT_2_TARGETS, annotationInfos.get(1).getTargetSources());
118
119        assertEquals(TestBackendConstants._TEST_ANNOT_4_HEADLINE, annotationInfos.get(2).getHeadline());
120        assertEquals(String.valueOf(TestBackendConstants._TEST_ANNOT_4_OWNER), annotationInfos.get(2).getOwner().getRef());
121        //assertEquals(TestBackendConstants._TEST_ANNOT_3_TARGETS, annotationInfos.get(2).getTargetSources());
122
123        final List<AnnotationInfo> annotationInfosNull = jdbcAnnotationDao.getAnnotationInfos(null);
124        assertEquals(null, annotationInfosNull);
125
126        final List<AnnotationInfo> annotationInfosZeroSize = jdbcAnnotationDao.getAnnotationInfos(new ArrayList<Number>());
127        assertEquals(0, annotationInfosZeroSize.size());
128
129
130    }
131
132    /**
133     * Test of getAnnotationREFs method, of class JdbcAnnotationDao.
134     * List<ResourceREF> getAnnotationREFs(List<Number> annotationIDs)
135     */
136    @Test
137    public void testGetAnnotationREFs() {
138        System.out.println("getAnnotationREFs");
139        List<Number> annotIds = new ArrayList<Number>();
140        annotIds.add(2);
141        annotIds.add(3);
142        annotIds.add(4);
143
144        final List<ResourceREF> testList = jdbcAnnotationDao.getAnnotationREFs(annotIds);
145        assertEquals(3, testList.size());
146        assertEquals(String.valueOf(2), testList.get(0).getRef());
147        assertEquals(String.valueOf(3), testList.get(1).getRef());
148        assertEquals(String.valueOf(4), testList.get(2).getRef());
149
150        final List<ResourceREF> testListTwo = jdbcAnnotationDao.getAnnotationREFs(new ArrayList<Number>());
151        assertEquals(0, testListTwo.size());
152
153        final List<ResourceREF> testListThree = jdbcAnnotationDao.getAnnotationREFs(null);
154        assertEquals(null, testListThree);
155
156    }
157
158    /**
159     *
160     * Test of getAnnotationID method, of class JdbcAnnotationDao. Integer
161     * getAnnotationID(AnnotationIdentifier externalID)
162     */
163    @Test
164    public void getInternalID() throws SQLException {
165        System.out.println("test getInternalID");
166
167        final Number annotaionId = jdbcAnnotationDao.getInternalID(new AnnotationIdentifier(TestBackendConstants._TEST_ANNOT_2_EXT));
168        assertEquals(2, annotaionId.intValue());
169
170        final Number annotaionIdNE = jdbcAnnotationDao.getInternalID(new AnnotationIdentifier(TestBackendConstants._TEST_ANNOT_7_EXT_NOT_IN_DB));
171        assertEquals(null, annotaionIdNE);
172
173        final Number annotaionIdNull = jdbcAnnotationDao.getInternalID(null);
174        assertEquals(null, annotaionIdNull);
175    }
176
177    /**
178     *
179     * Test of getAnnotationWithoutSources method, of class JdbcAnnotationDao. Annotation
180     * getAnnotation(Number annotationlID)
181     */
182    @Test
183    public void getAnnotationWithoutSources() throws SQLException {
184        System.out.println("test getAnnotationWithoutSources");
185
186        /// dummy test
187        final Annotation annotaionNull = jdbcAnnotationDao.getAnnotationWithoutSources(null);
188        assertEquals(null, annotaionNull);
189        ////
190
191        final Number testAnnotationID = 2;
192        final Annotation annotation = jdbcAnnotationDao.getAnnotationWithoutSources(testAnnotationID);
193        assertEquals(TestBackendConstants._TEST_ANNOT_2_HEADLINE, annotation.getHeadline());
194        assertEquals(String.valueOf(TestBackendConstants._TEST_ANNOT_2_OWNER), annotation.getOwner().getRef());
195        assertEquals(TestBackendConstants._TEST_ANNOT_2_BODY, annotation.getBody().getAny().get(0)); // when the body is elaborated it may be changed
196        assertEquals(TestBackendConstants._TEST_ANNOT_2_EXT, annotation.getURI());
197        assertEquals(TestBackendConstants._TEST_ANNOT_2_TIME_STAMP, annotation.getTimeStamp().toString());
198    }
199
200    /**
201     * Test of deletAnnotation method, of class JdbcAnnotationDao.
202     */
203     /**
204     *
205     * @param annotationId
206     * @return removed annotation rows (should be 1)
207     */
208    @Test
209    public void testDeleteAnnotation() throws SQLException {
210        System.out.println("deleteAnnotation"); 
211       
212        // to provide integrity, first delete rows in the joint tables
213        jdbcAnnotationDao.deleteAllAnnotationSource(5);
214        jdbcAnnotationDao.deleteAnnotationPrincipalPermissions(5);
215       
216        assertEquals(1, jdbcAnnotationDao.deleteAnnotation(5));
217        assertEquals(0, jdbcAnnotationDao.deleteAnnotation(5));
218    }
219
220    /**
221     * Test of addAnnotation method, of class JdbcAnnotationDao.
222     */
223    @Test
224    public void testAddAnnotation() throws SQLException {
225        System.out.println("test_addAnnotation ");
226
227        final Annotation annotationToAdd = testInstances.getAnnotationToAdd();// existing sources
228        assertEquals(null, annotationToAdd.getURI());
229        assertEquals(null, annotationToAdd.getTimeStamp());
230       
231        Number newAnnotationID = jdbcAnnotationDao.addAnnotation(annotationToAdd, 5);
232        assertEquals(6, newAnnotationID);
233       
234        // checking
235        Annotation addedAnnotation= jdbcAnnotationDao.getAnnotationWithoutSources(6);
236        assertFalse(null == addedAnnotation.getURI());
237        assertFalse(null == addedAnnotation.getTimeStamp());
238        assertEquals(Integer.toString(5), addedAnnotation.getOwner().getRef());
239        assertEquals(annotationToAdd.getBody().getAny().get(0), addedAnnotation.getBody().getAny().get(0)); // TODO: to be changed after serialization is fixed
240        assertEquals(annotationToAdd.getHeadline(), addedAnnotation.getHeadline());
241    }
242
243 
244
245    /**
246     * testing public List<Number> retrieveAnnotationList(List<Number> sourceIDs);
247   
248    **/
249    @Test   
250    public void testRetrieveAnnotationList() {
251        System.out.println("test retrieveAnnotationlist");
252        List<Number> sources = new ArrayList<Number>();
253        sources.add(1);
254        sources.add(2);
255        List<Number> result = jdbcAnnotationDao.retrieveAnnotationList(sources);
256        assertEquals (2, result.size());
257        assertEquals(2, result.get(0));
258        assertEquals(3, result.get(1));
259    }
260   
261    //////////////////////////////////
262   
263   
264    @Test   
265    public void testGetExternalID() {
266        System.out.println("getExternalID");
267
268        final AnnotationIdentifier externalId = jdbcAnnotationDao.getExternalID(2);
269        assertEquals(new AnnotationIdentifier(TestBackendConstants._TEST_ANNOT_2_EXT), externalId);
270
271
272        final AnnotationIdentifier externalIdThree = jdbcAnnotationDao.getExternalID(null);
273        assertEquals(null, externalIdThree.getUUID());
274
275    }
276   
277   
278    /** test
279     * public List<Number> getFilteredAnnotationIDs(String link, String text, String access, String namespace, UserIdentifier owner, Timestamp after, Timestamp before) {
280  **/
281   
282    @Test   
283    public void testGetFilteredAnnotationIDs(){
284        System.out.println(" test getFilteredAnnotationIDs");
285       
286       
287        //////////////////////////////////////////
288        // TEST 1
289        //final String link = "nl.wikipedia.org";
290        final List<Number> annotationIDs = new ArrayList<Number>();
291        annotationIDs.add(2);
292        annotationIDs.add(3);
293       
294        List<Number> result_1 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, null, null, null, null, null, null);       
295        assertEquals(2, result_1.size());
296        assertEquals(2, result_1.get(0));
297        assertEquals(3, result_1.get(1));
298       
299       
300        List<Number> result_2 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, "some html", null, null, null, null, null);       
301        assertEquals(2, result_2.size());
302        assertEquals(2, result_2.get(0));
303        assertEquals(3, result_2.get(1));
304       
305       
306       
307        List<Number> result_3 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, "some html", null, null, 3, null, null);       
308        assertEquals(1, result_3.size());
309        assertEquals(2, result_3.get(0));
310       
311       
312        Timestamp after = new Timestamp(0); 
313        Timestamp before = new Timestamp(System.currentTimeMillis()); 
314        List<Number> result_4 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, "some html", null, null, 3, after, before);       
315        assertEquals(1, result_4.size());
316        assertEquals(2, result_4.get(0));
317       
318       
319        Timestamp after_1 = new Timestamp(System.currentTimeMillis()); // no annotations added after "now"       
320        List<Number> result_5 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, "some html", null, null, 3, after_1, null);       
321        assertEquals(0, result_5.size());
322       
323       
324    }
325   
326    //////////////////////////////////
327    @Test
328    public void testUpdateBody() throws SQLException{
329        System.out.println("test UpdateAnnotationBody");
330        String serializedNewBody = "new body";
331        int result = jdbcAnnotationDao.updateBody(2, serializedNewBody);
332        assertEquals(1, result);
333        Annotation updatedAnnotation= jdbcAnnotationDao.getAnnotationWithoutSources(2);
334        assertEquals(serializedNewBody, Helpers.serializeBody(updatedAnnotation.getBody()));
335    }
336
337   
338    // public List<Map<Number, String>> retrievePermissions(Number annotationId)
339   
340    @Test
341    public void testRetrievePermissions (){
342        // VALUES (2, 3, 'owner');
343        //VALUES (2, 4, 'writer');
344        //VALUES (2, 5, 'reader');
345        System.out.println("test Permissions");
346        List<Map<Number, String>> result = jdbcAnnotationDao.retrievePermissions(2);
347        assertEquals(3, result.size());
348        assertEquals("owner", result.get(0).get(3));
349        assertEquals("writer", result.get(1).get(4));
350        assertEquals("reader", result.get(2).get(5));
351       
352       
353    }
354}
Note: See TracBrowser for help on using the repository browser.