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

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

lintegrity unit test reconstructed so it does not mock any more. getAnnotation works (the others are "ignored"). Needs refactoring (the subdirectory with beans and DummySecurityFilter? class.

File size: 8.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.schema.Target;
22import eu.dasish.annotation.schema.TargetInfo;
23import java.sql.SQLException;
24import java.util.ArrayList;
25import java.util.List;
26import java.util.Map;
27import java.util.UUID;
28import org.junit.Test;
29import static org.junit.Assert.*;
30import org.junit.Ignore;
31import org.junit.runner.RunWith;
32import org.springframework.beans.factory.annotation.Autowired;
33import org.springframework.test.context.ContextConfiguration;
34import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
35
36/**
37 *
38 * @author olhsha
39 */
40@RunWith(SpringJUnit4ClassRunner.class)
41@ContextConfiguration({"/spring-test-config/dataSource.xml", "/spring-config/TargetDao.xml"})
42public class JdbcTargetDaoTest extends JdbcResourceDaoTest {
43
44    @Autowired
45    JdbcTargetDao jdbcTargetDao;
46   
47     /**
48     * Test of stringURItoExternalID method
49     * public String stringURItoExternalID(String uri);
50     */
51    @Test
52    public void testStringURItoExternalID() {
53        System.out.println("test stringURItoExternalID");
54        jdbcTargetDao.setServiceURI(TestBackendConstants._TEST_SERVLET_URI_Targets);
55        String randomUUID = UUID.randomUUID().toString();
56        String uri = TestBackendConstants._TEST_SERVLET_URI_Targets + randomUUID;
57        String externalID = jdbcTargetDao.stringURItoExternalID(uri);
58        assertEquals(randomUUID, externalID);
59    }
60   
61    /**
62     * Test of externalIDtoURI method
63     * public String externalIDtoURI(String externalID);
64     */
65    @Test
66    public void testExternalIDtoURI() {
67        System.out.println("test stringURItoExternalID");
68        jdbcTargetDao.setServiceURI(TestBackendConstants._TEST_SERVLET_URI_Targets);
69        String randomUUID = UUID.randomUUID().toString();
70        String uri = TestBackendConstants._TEST_SERVLET_URI_Targets+randomUUID;
71        String uriResult = jdbcTargetDao.externalIDtoURI(randomUUID);
72        assertEquals(uri, uriResult);
73    }
74
75    /**
76     * Test of getExternalID method, of class JdbcTargetDao.
77     */
78    @Test
79    public void testGetExternalID() {
80        System.out.println("getExternalID");
81        Number internalID = 1;
82        UUID result = jdbcTargetDao.getExternalID(internalID);
83        assertEquals(TestBackendConstants._TEST_Target_1_EXT_ID, result.toString());
84    }
85
86    /**
87     * Test of getInternalID method, of class JdbcTargetDao.
88     */
89    @Test
90    public void testGetInternalId() {
91        System.out.println("getInternalId");
92        UUID externalID = UUID.fromString(TestBackendConstants._TEST_Target_1_EXT_ID);
93        Number expResult = 1;
94        Number result = jdbcTargetDao.getInternalID(externalID);
95        assertEquals(expResult, result);
96    }
97   
98    /**
99     * Test of getInternalIDFromURI method,
100     * public Number getInternalIDFromURI(UUID externalID);
101     */
102    @Test
103    public void testGetInternalIDFRomURI() {
104        System.out.println("test getInternalIDFromURI");
105        jdbcTargetDao.setServiceURI(TestBackendConstants._TEST_SERVLET_URI_Targets);
106        String uri = TestBackendConstants._TEST_SERVLET_URI_Targets+TestBackendConstants._TEST_Target_1_EXT_ID;
107        Number result = jdbcTargetDao.getInternalIDFromURI(uri);
108        assertEquals(1, result.intValue());
109    }
110   
111
112    /**
113     * Test of getTarget method, of class JdbcTargetDao.
114     */
115    @Test
116    public void testGetTarget() {
117        System.out.println("getTarget");
118        jdbcTargetDao.setServiceURI(TestBackendConstants._TEST_SERVLET_URI_Targets);
119        Target result = jdbcTargetDao.getTarget(1);
120        assertEquals(TestBackendConstants._TEST_SERVLET_URI_Targets+TestBackendConstants._TEST_Target_1_EXT_ID, result.getURI());
121        assertEquals(TestBackendConstants._TEST_Target_1_LINK, result.getLink());
122        assertEquals(TestBackendConstants._TEST_Target_1_VERSION, result.getVersion());
123        assertEquals("#de_Opdracht", result.getFragmentDescriptor());
124        // TODO :add time stamp test
125       
126    }
127
128    /**
129     * Test of deleteTarget method, of class JdbcTargetDao.
130     */
131    @Test
132    public void testDeleteTarget() {
133        System.out.println("deleteTarget");
134        // test 1
135        // remove the rows from the joint table to keep integrity
136        int result = jdbcTargetDao.deleteTarget(1); //the Target is in use, should not be deleted
137        assertEquals(0, result); 
138
139        // test 2
140        int resultTwo = jdbcTargetDao.deleteTarget(6);// the Target will be deleted because it is not referred by any annotation
141        assertEquals(1, resultTwo); 
142    }
143   
144 
145
146    /**
147     * Test of addTargetCachedRepresentation method, of class JdbcTargetDao.
148     */
149    @Test
150    public void testAddTargetCachedRepresentation() throws SQLException{
151       System.out.println("test addTargetCachedRepresentation");
152       assertEquals(1, jdbcTargetDao.addTargetCachedRepresentation(6, 7, "#firstrow"));
153       // content test
154       Map<Number, String> pairs = jdbcTargetDao.getCachedRepresentationFragmentPairs(6) ;
155       assertEquals(1, pairs.size());
156       assertEquals("#firstrow", pairs.get(7));
157    }
158   
159    /**
160     * Test of addTarget method, of class JdbcTargetDao.
161     */
162    @Test
163    public void testAddTarget() throws SQLException {
164        System.out.println("addTarget");
165
166        String link = "http://www.sagradafamilia.cat/";
167        Target freshTarget = new Target();
168        freshTarget.setLink(link);
169        freshTarget.setVersion(TestBackendConstants._TEST_Target_1_VERSION);
170        freshTarget.setLastModified(null);
171       
172        Number result = jdbcTargetDao.addTarget(freshTarget);
173        assertEquals(8, result);
174        // detailed checking
175        Target addedTarget = jdbcTargetDao.getTarget(result);
176        assertEquals(link, addedTarget.getLink());
177        assertEquals(TestBackendConstants._TEST_Target_1_VERSION, addedTarget.getVersion());
178        assertTrue(addedTarget.getURI().startsWith(TestBackendConstants._TEST_SERVLET_URI_Targets));
179    }
180
181    /**
182     * Test of getTargetInfos method, of class JdbcTargetDao.
183     */
184    @Test
185    public void testGetTargetInfos() {
186        System.out.println("getTargetInfos");       
187        jdbcTargetDao.setServiceURI(TestBackendConstants._TEST_SERVLET_URI_Targets);
188        List<Number> test = new ArrayList<Number>();
189        test.add(1);
190        test.add(2);
191        List<TargetInfo> result = jdbcTargetDao.getTargetInfos(test);
192        assertEquals(2, result.size());
193        assertEquals(TestBackendConstants._TEST_SERVLET_URI_Targets+TestBackendConstants._TEST_Target_1_EXT_ID, result.get(0).getRef());
194        assertEquals(TestBackendConstants._TEST_SERVLET_URI_Targets+TestBackendConstants._TEST_Target_2_EXT_ID, result.get(1).getRef());
195        assertEquals(TestBackendConstants._TEST_Target_1_VERSION, result.get(0).getVersion());
196        assertEquals(TestBackendConstants._TEST_Target_2_VERSION, result.get(1).getVersion());
197        assertEquals(TestBackendConstants._TEST_Target_1_LINK, result.get(0).getLink());
198        assertEquals(TestBackendConstants._TEST_Target_2_LINK, result.get(1).getLink());
199
200    }
201
202    /**
203     * test public List<Number> getTargetsForLink(String link)
204     *
205     *
206     */
207    @Test
208    public void tesGetTargetsReferringTo() {
209        System.out.println(" test getTargetsReferringTo");
210
211        String substring = "http://nl.wikipedia.org";
212        List<Number> result = jdbcTargetDao.getTargetsReferringTo(substring);
213        assertEquals(2, result.size());
214        assertEquals(1, result.get(0));
215        assertEquals(2, result.get(1));
216    }
217
218    /* Test of getCachedRepresentations method, of class JdbcTargetDao.
219     */
220    @Test
221    public void testGetCachedRepresentations() {
222        System.out.println("test getCachedRepresentations");
223        Number TargetID = 1;
224        List<Number> result = jdbcTargetDao.getCachedRepresentations(TargetID);
225        assertEquals(1, result.get(0));
226        assertEquals(2, result.get(1));
227    }
228}
Note: See TracBrowser for help on using the repository browser.