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

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

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

File size: 16.6 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.schema.Annotation;
23import eu.dasish.annotation.schema.Permission;
24import java.sql.SQLException;
25import java.sql.Timestamp;
26import java.util.ArrayList;
27import java.util.List;
28import java.util.Map;
29import java.util.UUID;
30import javax.xml.datatype.DatatypeConfigurationException;
31import javax.xml.datatype.DatatypeFactory;
32import static org.junit.Assert.*;
33import org.junit.Ignore;
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(TestBackendConstants._TEST_SERVLET_URI);
51   
52     /**
53     * Test of stringURItoExternalID method
54     * public String stringURItoExternalID(String uri);
55     */
56    @Test
57    public void testStringURItoExternalID() {
58        System.out.println("test stringURItoExternalID");
59        jdbcAnnotationDao.setServiceURI(TestBackendConstants._TEST_SERVLET_URI_annotations);
60        String randomUUID = UUID.randomUUID().toString();
61        String uri = TestBackendConstants._TEST_SERVLET_URI_annotations + randomUUID;
62        String externalID = jdbcAnnotationDao.stringURItoExternalID(uri);
63        assertEquals(randomUUID, externalID);
64    }
65   
66    /**
67     * Test of externalIDtoURI method
68     * public String externalIDtoURI(String externalID);
69     */
70    @Test
71    public void testExternalIDtoURI() {
72        System.out.println("test stringURItoExternalID");
73        jdbcAnnotationDao.setServiceURI(TestBackendConstants._TEST_SERVLET_URI_annotations);
74        String randomUUID = UUID.randomUUID().toString();
75        String uri = TestBackendConstants._TEST_SERVLET_URI_annotations+randomUUID;
76        String uriResult = jdbcAnnotationDao.externalIDtoURI(randomUUID);
77        assertEquals(uri, uriResult);
78    }
79
80     /**
81     * Test of retrieveTargetIDs method, of class JdbcAnnotationDao.
82     */
83    @Test
84    public void testRetrieveTargetIDs() {
85        System.out.println("retrieveTargetIDs");
86        Number annotationID = 2;
87        List<Number> result = jdbcAnnotationDao.retrieveTargetIDs(annotationID);
88        assertEquals(2, result.size());
89        assertEquals(1, result.get(0));
90        assertEquals(2, result.get(1));
91    }
92   
93    ///////////////////////////////////////////
94    @Test
95    public void testDeleteAllAnnotationTarget() throws SQLException{
96        System.out.println("test deleteAllAnnotationTargets");
97        assertEquals(2, jdbcAnnotationDao.deleteAllAnnotationTarget(2));
98        assertEquals(0, jdbcAnnotationDao.deleteAllAnnotationTarget(2));
99    }
100   
101    ///////////////////////////////////////////
102    @Test
103    public void testDeleteAnnotationPrinciplePermissions() throws SQLException{
104        System.out.println("test deleteAllAnnotationTargets");
105        int result = jdbcAnnotationDao.deleteAnnotationPrincipalPermissions(2);
106        assertEquals(3, result);
107        assertEquals(0, jdbcAnnotationDao.deleteAnnotationPrincipalPermissions(2));
108    }
109   
110    ///////////////////////////////////////////
111    @Test
112    public void testAddAnnotationPrinciplePermission() throws SQLException{
113        System.out.println("test addAnnotationTargets");
114        int result = jdbcAnnotationDao.addAnnotationPrincipalPermission(2, 1, Permission.READER);
115        assertEquals(1, result);
116    }
117   
118    ///////////////////////////////////////////
119    @Test
120    public void testAddAnnotationTarget() throws SQLException{
121        System.out.println("test addAnnotationTargetPair");
122        assertEquals(1, jdbcAnnotationDao.addAnnotationTarget(1,2));
123    }
124   
125    ////////////////////////////////
126   
127//    @Test
128//    public void testGetAnnotationInfos() {
129//        System.out.println("getAnnotationInfos");
130//        List<Number> annotIds = new ArrayList<Number>();
131//        annotIds.add(2);
132//        annotIds.add(3);
133//        annotIds.add(4);
134//
135//        jdbcAnnotationDao.setServiceURI(TestBackendConstants._TEST_SERVLET_URI_annotations);
136//        final List<AnnotationInfo> annotationInfos = jdbcAnnotationDao.getAnnotationInfos(annotIds);
137//        assertEquals(3, annotationInfos.size());
138//
139//        assertEquals(TestBackendConstants._TEST_ANNOT_2_HEADLINE, annotationInfos.get(0).getHeadline());
140//        assertEquals(String.valueOf(TestBackendConstants._TEST_ANNOT_2_OWNER), annotationInfos.get(0).getOwnerRef());
141//        assertEquals(TestBackendConstants._TEST_SERVLET_URI_annotations +TestBackendConstants._TEST_ANNOT_2_EXT,
142//           annotationInfos.get(0).getRef());         
143//        //assertEquals(TestBackendConstants._TEST_ANNOT_1_TARGETS, annotationInfos.get(0).getTargetTargets());
144//
145//        assertEquals(TestBackendConstants._TEST_ANNOT_3_HEADLINE, annotationInfos.get(1).getHeadline());
146//        assertEquals(String.valueOf(TestBackendConstants._TEST_ANNOT_3_OWNER), annotationInfos.get(1).getOwnerRef());
147//        assertEquals(TestBackendConstants._TEST_SERVLET_URI_annotations + TestBackendConstants._TEST_ANNOT_3_EXT,
148//           annotationInfos.get(1).getRef());
149//        //assertEquals(TestBackendConstants._TEST_ANNOT_2_TARGETS, annotationInfos.get(1).getTargetTargets());
150//
151//        assertEquals(TestBackendConstants._TEST_ANNOT_4_HEADLINE, annotationInfos.get(2).getHeadline());
152//        assertEquals(String.valueOf(TestBackendConstants._TEST_ANNOT_4_OWNER), annotationInfos.get(2).getOwnerRef());
153//        assertEquals(TestBackendConstants._TEST_SERVLET_URI_annotations + TestBackendConstants._TEST_ANNOT_4_EXT,
154//           annotationInfos.get(2).getRef());
155//        //assertEquals(TestBackendConstants._TEST_ANNOT_3_TARGETS, annotationInfos.get(2).getTargetTargets());
156//
157//        final List<AnnotationInfo> annotationInfosNull = jdbcAnnotationDao.getAnnotationInfos(null);
158//        assertEquals(null, annotationInfosNull);
159//
160//        final List<AnnotationInfo> annotationInfosZeroSize = jdbcAnnotationDao.getAnnotationInfos(new ArrayList<Number>());
161//        assertEquals(0, annotationInfosZeroSize.size());
162//
163//
164//    }
165
166    /**
167     * Test of getAnnotationREFs method, of class JdbcAnnotationDao.
168     * List<ReTargetREF> getAnnotationREFs(List<Number> annotationIDs)
169     */
170    @Test
171    public void testGetAnnotationREFs() {
172        System.out.println("getAnnotationREFs");
173        List<Number> annotIds = new ArrayList<Number>();
174        annotIds.add(2);
175        annotIds.add(3);
176        annotIds.add(4);
177
178        jdbcAnnotationDao.setServiceURI(TestBackendConstants._TEST_SERVLET_URI_annotations);
179        final List<String> testList = jdbcAnnotationDao.getAnnotationREFs(annotIds);
180        assertEquals(3, testList.size());
181        assertEquals(TestBackendConstants._TEST_SERVLET_URI_annotations+TestBackendConstants._TEST_ANNOT_2_EXT, testList.get(0));
182        assertEquals(TestBackendConstants._TEST_SERVLET_URI_annotations+TestBackendConstants._TEST_ANNOT_3_EXT, testList.get(1));
183        assertEquals(TestBackendConstants._TEST_SERVLET_URI_annotations+TestBackendConstants._TEST_ANNOT_4_EXT, testList.get(2));
184
185        final List<String> testListTwo = jdbcAnnotationDao.getAnnotationREFs(new ArrayList<Number>());
186        assertEquals(0, testListTwo.size());
187
188        final List<String> testListThree = jdbcAnnotationDao.getAnnotationREFs(null);
189        assertEquals(null, testListThree);
190
191    }
192
193    /**
194     *
195     * Test of getAnnotationID method, of class JdbcAnnotationDao. Integer
196     * getAnnotationID(UUID externalID)
197     */
198    @Test
199    public void getInternalID() throws SQLException {
200        System.out.println("test getInternalID");
201
202        final Number annotaionId = jdbcAnnotationDao.getInternalID(UUID.fromString(TestBackendConstants._TEST_ANNOT_2_EXT));
203        assertEquals(2, annotaionId.intValue());
204
205        final Number annotaionIdNE = jdbcAnnotationDao.getInternalID(UUID.fromString(TestBackendConstants._TEST_ANNOT_7_EXT_NOT_IN_DB));
206        assertEquals(null, annotaionIdNE);
207
208        final Number annotaionIdNull = jdbcAnnotationDao.getInternalID(null);
209        assertEquals(null, annotaionIdNull);
210    }
211   
212      /**
213     * Test of getInternalIDFromURI method,
214     * public Number getInternalIDFromURI(UUID externalID);
215     */
216    @Test
217    public void testGetInternalIDFRomURI() {
218        System.out.println("test getInternalIDFromURI");
219        jdbcAnnotationDao.setServiceURI(TestBackendConstants._TEST_SERVLET_URI_annotations);
220        String uri = TestBackendConstants._TEST_SERVLET_URI_annotations+TestBackendConstants._TEST_ANNOT_2_EXT;
221        Number result = jdbcAnnotationDao.getInternalIDFromURI(uri);
222        assertEquals(2, result.intValue());
223    }
224
225    /**
226     *
227     * Test of getAnnotationWithoutTargetsAndPermissions method, of class JdbcAnnotationDao. Annotation
228     * getAnnotation(Number annotationlID)
229     */
230    @Test
231    public void getAnnotationWithoutTargetsAndPermissions() throws SQLException, DatatypeConfigurationException {
232        System.out.println("test getAnnotationWithoutTargets");
233        jdbcAnnotationDao.setServiceURI(TestBackendConstants._TEST_SERVLET_URI_annotations);
234        final Map<Annotation, Number> result= jdbcAnnotationDao.getAnnotationWithoutTargetsAndPermissions(2);
235        Annotation[] annotations = new Annotation[1];
236        result.keySet().toArray(annotations);
237       
238        assertEquals(TestBackendConstants._TEST_ANNOT_2_HEADLINE, annotations[0].getHeadline());
239        assertEquals(3, result.get(annotations[0]));
240        assertEquals(TestBackendConstants._TEST_ANNOT_2_BODY, annotations[0].getBody().getTextBody().getValue()); 
241        assertEquals(TestBackendConstants._TEST_BODY_MIMETYPE_HTML, annotations[0].getBody().getTextBody().getMimeType()); 
242        assertEquals(TestBackendConstants._TEST_SERVLET_URI_annotations+
243                TestBackendConstants._TEST_ANNOT_2_EXT, annotations[0].getURI());
244        assertEquals(DatatypeFactory.newInstance().newXMLGregorianCalendar(TestBackendConstants._TEST_ANNOT_2_TIME_STAMP), annotations[0].getLastModified());
245    }
246
247    /**
248     * Test of deletAnnotation method, of class JdbcAnnotationDao.
249     */
250     /**
251     *
252     * @param annotationId
253     * @return removed annotation rows (should be 1)
254     */
255    @Test
256    public void testDeleteAnnotation() throws SQLException {
257        System.out.println("deleteAnnotation"); 
258       
259        // to provide integrity, first delete rows in the joint tables
260        jdbcAnnotationDao.deleteAllAnnotationTarget(5);
261        jdbcAnnotationDao.deleteAnnotationPrincipalPermissions(5);
262       
263        assertEquals(1, jdbcAnnotationDao.deleteAnnotation(5));
264        assertEquals(0, jdbcAnnotationDao.deleteAnnotation(5));
265    }
266
267    /**
268     * Test of addAnnotation method, of class JdbcAnnotationDao.
269     */
270    @Test 
271    public void testAddAnnotation() throws SQLException, Exception {
272        System.out.println("test_addAnnotation ");
273
274        final Annotation annotationToAdd = testInstances.getAnnotationToAdd();// existing Targets
275        assertEquals(null, annotationToAdd.getURI());
276        assertEquals(null, annotationToAdd.getLastModified());
277       
278        Number newAnnotationID = jdbcAnnotationDao.addAnnotation(annotationToAdd, 5);
279        assertEquals(6, newAnnotationID);
280       
281        // checking
282        Map<Annotation, Number> getResult= jdbcAnnotationDao.getAnnotationWithoutTargetsAndPermissions(6);
283        Annotation[] annotations = new Annotation[1];
284        getResult.keySet().toArray(annotations);
285        Annotation addedAnnotation = annotations[0];
286        assertFalse(null == addedAnnotation.getURI());
287        assertFalse(null == addedAnnotation.getLastModified());
288        assertEquals(5, getResult.get(addedAnnotation));
289        assertEquals(annotationToAdd.getBody().getTextBody().getMimeType(), addedAnnotation.getBody().getTextBody().getMimeType());
290        assertEquals(annotationToAdd.getBody().getTextBody().getValue(), addedAnnotation.getBody().getTextBody().getValue()); 
291        assertEquals(annotationToAdd.getHeadline(), addedAnnotation.getHeadline());
292        System.out.println("creation time "+addedAnnotation.getLastModified());
293    }
294
295 
296
297    /**
298     * testing public List<Number> retrieveAnnotationList(List<Number> TargetIDs);
299   
300    **/
301    @Test   
302    public void testRetrieveAnnotationList() {
303        System.out.println("test retrieveAnnotationlist");
304        List<Number> Targets = new ArrayList<Number>();
305        Targets.add(1);
306        Targets.add(2);
307        List<Number> result = jdbcAnnotationDao.retrieveAnnotationList(Targets);
308        assertEquals (2, result.size());
309        assertEquals(2, result.get(0));
310        assertEquals(3, result.get(1));
311    }
312   
313    //////////////////////////////////
314   
315   
316    @Test   
317    public void testGetExternalID() {
318        System.out.println("getExternalID");
319
320        final UUID externalId = jdbcAnnotationDao.getExternalID(2);
321        assertEquals(UUID.fromString(TestBackendConstants._TEST_ANNOT_2_EXT), externalId);
322       
323        final UUID externalIdThree = jdbcAnnotationDao.getExternalID(null);
324        assertEquals(null, externalIdThree);
325
326    }
327   
328   
329    /** test
330     * public List<Number> getFilteredAnnotationIDs(String link, String text, String access, String namespace, UUID owner, Timestamp after, Timestamp before) {
331  **/
332   
333    @Test   
334    public void testGetFilteredAnnotationIDs(){
335        System.out.println(" test getFilteredAnnotationIDs");
336       
337       
338        //////////////////////////////////////////
339        // TEST 1
340        //final String link = "nl.wikipedia.org";
341        final List<Number> annotationIDs = new ArrayList<Number>();
342        annotationIDs.add(2);
343        annotationIDs.add(3);
344       
345        List<Number> result_1 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, null, null, null, null, null);       
346        assertEquals(2, result_1.size());
347        assertEquals(2, result_1.get(0));
348        assertEquals(3, result_1.get(1));
349       
350       
351        List<Number> result_2 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, "some html", null, null, null, null);       
352        assertEquals(2, result_2.size());
353        assertEquals(2, result_2.get(0));
354        assertEquals(3, result_2.get(1));
355       
356       
357       
358        List<Number> result_3 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, "some html", null, 3, null, null);       
359        assertEquals(1, result_3.size());
360        assertEquals(2, result_3.get(0));
361       
362       
363        Timestamp after = new Timestamp(0); 
364        Timestamp before = new Timestamp(System.currentTimeMillis()); 
365        List<Number> result_4 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, "some html", null, 3, after, before);       
366        assertEquals(1, result_4.size());
367        assertEquals(2, result_4.get(0));
368       
369       
370        Timestamp after_1 = new Timestamp(System.currentTimeMillis()); // no annotations added after "now"       
371        List<Number> result_5 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, "some html", null, 3, after_1, null);       
372        assertEquals(0, result_5.size());
373       
374       
375    }
376   
377    //////////////////////////////////
378   
379 
380   
381    // public List<Map<Number, String>> retrievePermissions(Number annotationId)
382   
383    @Test
384    public void testRetrievePermissions (){
385        System.out.println("test Permissions");
386        List<Map<Number, String>> result = jdbcAnnotationDao.getPermissions(2);
387        assertEquals(3, result.size());
388        assertEquals("owner", result.get(0).get(3));
389        assertEquals("writer", result.get(1).get(4));
390        assertEquals("reader", result.get(2).get(5));
391       
392       
393    }
394}
Note: See TracBrowser for help on using the repository browser.