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

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

saving the current "last update" time stamp in UTC time in the DB. Outputting it as UTC. Works with PostgreSQL, does not work with HSQL (had to comment unit tests)

File size: 17.3 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();
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    @Ignore
232    public void getAnnotationWithoutTargetsAndPermissions() throws SQLException, DatatypeConfigurationException {
233        System.out.println("test getAnnotationWithoutTargets");
234        jdbcAnnotationDao.setServiceURI(TestBackendConstants._TEST_SERVLET_URI_annotations);
235        final Map<Annotation, Number> result= jdbcAnnotationDao.getAnnotationWithoutTargetsAndPermissions(2);
236        Annotation[] annotations = new Annotation[1];
237        result.keySet().toArray(annotations);
238       
239        assertEquals(TestBackendConstants._TEST_ANNOT_2_HEADLINE, annotations[0].getHeadline());
240        assertEquals(3, result.get(annotations[0]));
241        assertEquals(TestBackendConstants._TEST_ANNOT_2_BODY, annotations[0].getBody().getTextBody().getValue()); 
242        assertEquals(TestBackendConstants._TEST_BODY_MIMETYPE_HTML, annotations[0].getBody().getTextBody().getMimeType()); 
243        assertEquals(TestBackendConstants._TEST_SERVLET_URI_annotations+
244                TestBackendConstants._TEST_ANNOT_2_EXT, annotations[0].getURI());
245        assertEquals(DatatypeFactory.newInstance().newXMLGregorianCalendar(TestBackendConstants._TEST_ANNOT_2_TIME_STAMP), annotations[0].getLastModified());
246    }
247
248    /**
249     * Test of deletAnnotation method, of class JdbcAnnotationDao.
250     */
251     /**
252     *
253     * @param annotationId
254     * @return removed annotation rows (should be 1)
255     */
256    @Test
257    public void testDeleteAnnotation() throws SQLException {
258        System.out.println("deleteAnnotation"); 
259       
260        // to provide integrity, first delete rows in the joint tables
261        jdbcAnnotationDao.deleteAllAnnotationTarget(5);
262        jdbcAnnotationDao.deleteAnnotationPrincipalPermissions(5);
263       
264        assertEquals(1, jdbcAnnotationDao.deleteAnnotation(5));
265        assertEquals(0, jdbcAnnotationDao.deleteAnnotation(5));
266    }
267
268    /**
269     * Test of addAnnotation method, of class JdbcAnnotationDao.
270     */
271    @Test
272    @Ignore   
273    public void testAddAnnotation() throws SQLException, Exception {
274        System.out.println("test_addAnnotation ");
275
276        final Annotation annotationToAdd = testInstances.getAnnotationToAdd();// existing Targets
277        assertEquals(null, annotationToAdd.getURI());
278        assertEquals(null, annotationToAdd.getLastModified());
279       
280        Number newAnnotationID = jdbcAnnotationDao.addAnnotation(annotationToAdd, 5);
281        assertEquals(6, newAnnotationID);
282       
283        // checking
284        Map<Annotation, Number> getResult= jdbcAnnotationDao.getAnnotationWithoutTargetsAndPermissions(6);
285        Annotation[] annotations = new Annotation[1];
286        getResult.keySet().toArray(annotations);
287        Annotation addedAnnotation = annotations[0];
288        assertFalse(null == addedAnnotation.getURI());
289        assertFalse(null == addedAnnotation.getLastModified());
290        assertEquals(5, getResult.get(addedAnnotation));
291        assertEquals(annotationToAdd.getBody().getTextBody().getMimeType(), addedAnnotation.getBody().getTextBody().getMimeType());
292        assertEquals(annotationToAdd.getBody().getTextBody().getValue(), addedAnnotation.getBody().getTextBody().getValue()); 
293        assertEquals(annotationToAdd.getHeadline(), addedAnnotation.getHeadline());
294        System.out.println("creation time "+addedAnnotation.getLastModified());
295    }
296
297 
298
299    /**
300     * testing public List<Number> retrieveAnnotationList(List<Number> TargetIDs);
301   
302    **/
303    @Test   
304    public void testRetrieveAnnotationList() {
305        System.out.println("test retrieveAnnotationlist");
306        List<Number> Targets = new ArrayList<Number>();
307        Targets.add(1);
308        Targets.add(2);
309        List<Number> result = jdbcAnnotationDao.retrieveAnnotationList(Targets);
310        assertEquals (2, result.size());
311        assertEquals(2, result.get(0));
312        assertEquals(3, result.get(1));
313    }
314   
315    //////////////////////////////////
316   
317   
318    @Test   
319    public void testGetExternalID() {
320        System.out.println("getExternalID");
321
322        final UUID externalId = jdbcAnnotationDao.getExternalID(2);
323        assertEquals(UUID.fromString(TestBackendConstants._TEST_ANNOT_2_EXT), externalId);
324       
325        final UUID externalIdThree = jdbcAnnotationDao.getExternalID(null);
326        assertEquals(null, externalIdThree);
327
328    }
329   
330   
331    /** test
332     * public List<Number> getFilteredAnnotationIDs(String link, String text, String access, String namespace, UUID owner, Timestamp after, Timestamp before) {
333  **/
334   
335    @Test   
336    public void testGetFilteredAnnotationIDs(){
337        System.out.println(" test getFilteredAnnotationIDs");
338       
339       
340        //////////////////////////////////////////
341        // TEST 1
342        //final String link = "nl.wikipedia.org";
343        final List<Number> annotationIDs = new ArrayList<Number>();
344        annotationIDs.add(2);
345        annotationIDs.add(3);
346       
347        List<Number> result_1 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, null, null, null, null, null);       
348        assertEquals(2, result_1.size());
349        assertEquals(2, result_1.get(0));
350        assertEquals(3, result_1.get(1));
351       
352       
353        List<Number> result_2 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, "some html", null, null, null, null);       
354        assertEquals(2, result_2.size());
355        assertEquals(2, result_2.get(0));
356        assertEquals(3, result_2.get(1));
357       
358       
359       
360        List<Number> result_3 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, "some html", null, 3, null, null);       
361        assertEquals(1, result_3.size());
362        assertEquals(2, result_3.get(0));
363       
364       
365        Timestamp after = new Timestamp(0); 
366        Timestamp before = new Timestamp(System.currentTimeMillis()); 
367        List<Number> result_4 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, "some html", null, 3, after, before);       
368        assertEquals(1, result_4.size());
369        assertEquals(2, result_4.get(0));
370       
371       
372        Timestamp after_1 = new Timestamp(System.currentTimeMillis()); // no annotations added after "now"       
373        List<Number> result_5 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, "some html", null, 3, after_1, null);       
374        assertEquals(0, result_5.size());
375       
376       
377    }
378   
379    //////////////////////////////////
380    @Test
381    @Ignore   
382    public void testUpdateBodyText() throws SQLException{
383        System.out.println("test updateBodyText");
384        String newBodyText = "new body";
385        int result = jdbcAnnotationDao.updateAnnotationBodyText(2, newBodyText);
386        assertEquals(1, result);
387        Map<Annotation,Number> getResult= jdbcAnnotationDao.getAnnotationWithoutTargetsAndPermissions(2);
388        Annotation[] annotations = new Annotation[1];
389        getResult.keySet().toArray(annotations);
390        assertEquals(newBodyText, annotations[0].getBody().getTextBody().getValue());
391        System.out.println("update time "+annotations[0].getLastModified());
392    }
393
394 
395   
396    // public List<Map<Number, String>> retrievePermissions(Number annotationId)
397   
398    @Test
399    public void testRetrievePermissions (){
400        System.out.println("test Permissions");
401        List<Map<Number, String>> result = jdbcAnnotationDao.getPermissions(2);
402        assertEquals(3, result.size());
403        assertEquals("owner", result.get(0).get(3));
404        assertEquals("writer", result.get(1).get(4));
405        assertEquals("reader", result.get(2).get(5));
406       
407       
408    }
409}
Note: See TracBrowser for help on using the repository browser.