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

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

Unit test HSQL database is cleaned up (obsolete rows are removed), UNit tests are refactored (hairy code is removed, a lot of test backend constants). Existed unit tests work.

File size: 15.7 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 = 1;
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(1));
98        assertEquals(0, jdbcAnnotationDao.deleteAllAnnotationTarget(1));
99    }
100   
101    ///////////////////////////////////////////
102    @Test
103    public void testDeleteAnnotationPrinciplePermissions() throws SQLException{
104        System.out.println("test deleteAllAnnotationTargets");
105        int result = jdbcAnnotationDao.deleteAnnotationPrincipalPermissions(1);
106        assertEquals(2, result);
107        assertEquals(0, jdbcAnnotationDao.deleteAnnotationPrincipalPermissions(1));
108    }
109   
110    ///////////////////////////////////////////
111    @Test
112    public void testAddAnnotationPrinciplePermission() throws SQLException{
113        System.out.println("test addAnnotationTargets");
114        int result = jdbcAnnotationDao.addAnnotationPrincipalPermission(1, 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,3));
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("Sagrada Famiglia", annotationInfos.get(0).getHeadline());
140//        assertEquals(String.valueOf(TestBackendConstants._TEST_ANNOT_2_OWNER), annotationInfos.get(0).getOwnerRef());
141//        assertEquals(TestBackendConstants._TEST_SERVLET_URI_annotations +"00000000-0000-0000-0000-000000000021",
142//           annotationInfos.get(0).getRef());         
143//        //assertEquals(TestBackendConstants._TEST_ANNOT_1_TARGETS, annotationInfos.get(0).getTargetTargets());
144//
145//        assertEquals("Gaudi", annotationInfos.get(1).getHeadline());
146//        assertEquals(String.valueOf(TestBackendConstants._TEST_ANNOT_3_OWNER), annotationInfos.get(1).getOwnerRef());
147//        assertEquals(TestBackendConstants._TEST_SERVLET_URI_annotations + "00000000-0000-0000-0000-000000000022",
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 + "00000000-0000-0000-0000-000000000023",
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(1);
175        annotIds.add(2);
176        annotIds.add(3);
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+"00000000-0000-0000-0000-000000000021", testList.get(0));
182        assertEquals(TestBackendConstants._TEST_SERVLET_URI_annotations+"00000000-0000-0000-0000-000000000022", testList.get(1));
183        assertEquals(TestBackendConstants._TEST_SERVLET_URI_annotations+"00000000-0000-0000-0000-000000000023", 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("00000000-0000-0000-0000-000000000021"));
203        assertEquals(1, annotaionId.intValue());
204
205        final Number annotaionIdNE = jdbcAnnotationDao.getInternalID(UUID.fromString("00000000-0000-0000-0000-0000000000cc"));
206        assertEquals(null, annotaionIdNE);
207
208       
209    }
210   
211      /**
212     * Test of getInternalIDFromURI method,
213     * public Number getInternalIDFromURI(UUID externalID);
214     */
215    @Test
216    public void testGetInternalIDFRomURI() {
217        System.out.println("test getInternalIDFromURI");
218        jdbcAnnotationDao.setServiceURI(TestBackendConstants._TEST_SERVLET_URI_annotations);
219        String uri = TestBackendConstants._TEST_SERVLET_URI_annotations+"00000000-0000-0000-0000-000000000021";
220        Number result = jdbcAnnotationDao.getInternalIDFromURI(uri);
221        assertEquals(1, result.intValue());
222    }
223
224    /**
225     *
226     * Test of getAnnotationWithoutTargetsAndPermissions method, of class JdbcAnnotationDao. Annotation
227     * getAnnotation(Number annotationlID)
228     */
229    @Test
230    public void getAnnotationWithoutTargetsAndPermissions() throws SQLException, DatatypeConfigurationException {
231        System.out.println("test getAnnotationWithoutTargets");
232        jdbcAnnotationDao.setServiceURI(TestBackendConstants._TEST_SERVLET_URI_annotations);
233        final Annotation result= jdbcAnnotationDao.getAnnotationWithoutTargetsAndPermissions(1);
234       
235        assertEquals("Sagrada Famiglia", result.getHeadline());
236        assertEquals("<html><body>some html 1</body></html>", result.getBody().getTextBody().getBody()); 
237        assertEquals("text/html", result.getBody().getTextBody().getMimeType()); 
238        assertEquals(TestBackendConstants._TEST_SERVLET_URI_annotations+
239                "00000000-0000-0000-0000-000000000021", result.getURI());
240        assertEquals(DatatypeFactory.newInstance().newXMLGregorianCalendar("2013-08-12T09:25:00.383000Z"), result.getLastModified());
241    }
242
243    /**
244     * Test of deletAnnotation method, of class JdbcAnnotationDao.
245     */
246     /**
247     *
248     * @param annotationId
249     * @return removed annotation rows (should be 1)
250     */
251    @Test
252    public void testDeleteAnnotation() throws SQLException {
253        System.out.println("deleteAnnotation"); 
254       
255        // to provide integrity, first delete rows in the joint tables
256        jdbcAnnotationDao.deleteAllAnnotationTarget(4);
257        jdbcAnnotationDao.deleteAnnotationPrincipalPermissions(4);
258       
259        assertEquals(1, jdbcAnnotationDao.deleteAnnotation(4));
260        assertEquals(0, jdbcAnnotationDao.deleteAnnotation(4));
261    }
262
263    /**
264     * Test of addAnnotation method, of class JdbcAnnotationDao.
265     */
266    @Test 
267    public void testAddAnnotation() throws SQLException, Exception {
268        System.out.println("test_addAnnotation ");
269
270        final Annotation annotationToAdd = testInstances.getAnnotationToAdd();// existing Targets
271        assertEquals(null, annotationToAdd.getURI());
272        assertEquals(null, annotationToAdd.getLastModified());
273       
274        Number newAnnotationID = jdbcAnnotationDao.addAnnotation(annotationToAdd, 3);
275        assertEquals(5, newAnnotationID);
276       
277        // checking
278        Annotation addedAnnotation= jdbcAnnotationDao.getAnnotationWithoutTargetsAndPermissions(5);
279        assertFalse(null == addedAnnotation.getURI());
280        assertFalse(null == addedAnnotation.getLastModified());
281        assertEquals(annotationToAdd.getBody().getTextBody().getMimeType(), addedAnnotation.getBody().getTextBody().getMimeType());
282        assertEquals(annotationToAdd.getBody().getTextBody().getBody(), addedAnnotation.getBody().getTextBody().getBody()); 
283        assertEquals(annotationToAdd.getHeadline(), addedAnnotation.getHeadline());
284        System.out.println("creation time "+addedAnnotation.getLastModified());
285    }
286
287 
288
289    /**
290     * testing public List<Number> retrieveAnnotationList(List<Number> TargetIDs);
291   
292    **/
293    @Test   
294    public void testRetrieveAnnotationList() {
295        System.out.println("test retrieveAnnotationlist");
296        List<Number> targets = new ArrayList<Number>();
297        targets.add(1);
298        targets.add(2);
299        List<Number> result = jdbcAnnotationDao.retrieveAnnotationList(targets);
300        assertEquals (2, result.size());
301        assertEquals(1, result.get(0));
302        assertEquals(2, result.get(1));
303    }
304   
305    //////////////////////////////////
306   
307   
308    @Test   
309    public void testGetExternalID() {
310        System.out.println("getExternalID");
311
312        final UUID externalId = jdbcAnnotationDao.getExternalID(1);
313        assertEquals(UUID.fromString("00000000-0000-0000-0000-000000000021"), externalId);
314       
315        final UUID externalIdThree = jdbcAnnotationDao.getExternalID(null);
316        assertEquals(null, externalIdThree);
317
318    }
319   
320   
321    /** test
322     * public List<Number> getFilteredAnnotationIDs(String link, String text, String access, String namespace, UUID owner, Timestamp after, Timestamp before) {
323  **/
324   
325    @Test   
326    public void testGetFilteredAnnotationIDs(){
327        System.out.println(" test getFilteredAnnotationIDs");
328       
329       
330        //////////////////////////////////////////
331        // TEST 1
332        //final String link = "nl.wikipedia.org";
333        final List<Number> annotationIDs = new ArrayList<Number>();
334        annotationIDs.add(1);
335        annotationIDs.add(2);
336       
337        List<Number> result_1 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, null, null, null, null, null);       
338        assertEquals(2, result_1.size());
339        assertEquals(1, result_1.get(0));
340        assertEquals(2, result_1.get(1));
341       
342       
343        List<Number> result_2 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, null, "some html", null, null, null);       
344        assertEquals(2, result_2.size());
345        assertEquals(1, result_2.get(0));
346        assertEquals(2, result_2.get(1));
347       
348       
349             
350        final String after = (new Timestamp(0)).toString();
351        final String before = (new Timestamp(System.currentTimeMillis())).toString();
352 
353        List<Number> result_4 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, 1, "some html", null, after, before);       
354        assertEquals(1, result_4.size());
355        assertEquals(1, result_4.get(0));
356       
357        final String after_1 = (new Timestamp(System.currentTimeMillis())).toString();// no annotations added after "now"       
358        List<Number> result_5 = jdbcAnnotationDao.getFilteredAnnotationIDs(annotationIDs, 4, "some html", null, after_1, null);       
359        assertEquals(0, result_5.size());
360       
361       
362    }
363   
364    //////////////////////////////////
365   
366 
367   
368    // public List<Map<Number, String>> retrievePermissions(Number annotationId)
369   
370    @Test
371    public void testRetrievePermissions (){
372        System.out.println("test Permissions");
373        List<Map<Number, String>> result = jdbcAnnotationDao.getPermissions(1);
374        assertEquals(2, result.size());
375        assertEquals("writer", result.get(0).get(2));
376        assertEquals("reader", result.get(1).get(3));
377       
378       
379    }
380}
Note: See TracBrowser for help on using the repository browser.