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 | */ |
---|
18 | package eu.dasish.annotation.backend.dao.impl; |
---|
19 | |
---|
20 | import eu.dasish.annotation.backend.TestBackendConstants; |
---|
21 | import eu.dasish.annotation.schema.Target; |
---|
22 | import eu.dasish.annotation.schema.TargetInfo; |
---|
23 | import java.sql.SQLException; |
---|
24 | import java.util.ArrayList; |
---|
25 | import java.util.List; |
---|
26 | import java.util.Map; |
---|
27 | import java.util.UUID; |
---|
28 | import org.junit.Test; |
---|
29 | import static org.junit.Assert.*; |
---|
30 | import org.junit.Ignore; |
---|
31 | import org.junit.runner.RunWith; |
---|
32 | import org.springframework.beans.factory.annotation.Autowired; |
---|
33 | import org.springframework.test.context.ContextConfiguration; |
---|
34 | import 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"}) |
---|
42 | public 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 | @Ignore |
---|
164 | public void testAddTarget() throws SQLException { |
---|
165 | System.out.println("addTarget"); |
---|
166 | |
---|
167 | String link = "http://www.sagradafamilia.cat/"; |
---|
168 | Target freshTarget = new Target(); |
---|
169 | freshTarget.setLink(link); |
---|
170 | freshTarget.setVersion(TestBackendConstants._TEST_Target_1_VERSION); |
---|
171 | freshTarget.setLastModified(null); |
---|
172 | |
---|
173 | Number result = jdbcTargetDao.addTarget(freshTarget); |
---|
174 | assertEquals(8, result); |
---|
175 | // detailed checking |
---|
176 | Target addedTarget = jdbcTargetDao.getTarget(result); |
---|
177 | assertEquals(link, addedTarget.getLink()); |
---|
178 | assertEquals(TestBackendConstants._TEST_Target_1_VERSION, addedTarget.getVersion()); |
---|
179 | assertTrue(addedTarget.getURI().startsWith(TestBackendConstants._TEST_SERVLET_URI_Targets)); |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * Test of getTargetInfos method, of class JdbcTargetDao. |
---|
184 | */ |
---|
185 | @Test |
---|
186 | public void testGetTargetInfos() { |
---|
187 | System.out.println("getTargetInfos"); |
---|
188 | jdbcTargetDao.setServiceURI(TestBackendConstants._TEST_SERVLET_URI_Targets); |
---|
189 | List<Number> test = new ArrayList<Number>(); |
---|
190 | test.add(1); |
---|
191 | test.add(2); |
---|
192 | List<TargetInfo> result = jdbcTargetDao.getTargetInfos(test); |
---|
193 | assertEquals(2, result.size()); |
---|
194 | assertEquals(TestBackendConstants._TEST_SERVLET_URI_Targets+TestBackendConstants._TEST_Target_1_EXT_ID, result.get(0).getRef()); |
---|
195 | assertEquals(TestBackendConstants._TEST_SERVLET_URI_Targets+TestBackendConstants._TEST_Target_2_EXT_ID, result.get(1).getRef()); |
---|
196 | assertEquals(TestBackendConstants._TEST_Target_1_VERSION, result.get(0).getVersion()); |
---|
197 | assertEquals(TestBackendConstants._TEST_Target_2_VERSION, result.get(1).getVersion()); |
---|
198 | assertEquals(TestBackendConstants._TEST_Target_1_LINK, result.get(0).getLink()); |
---|
199 | assertEquals(TestBackendConstants._TEST_Target_2_LINK, result.get(1).getLink()); |
---|
200 | |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | * test public List<Number> getTargetsForLink(String link) |
---|
205 | * |
---|
206 | * |
---|
207 | */ |
---|
208 | @Test |
---|
209 | public void tesGetTargetsReferringTo() { |
---|
210 | System.out.println(" test getTargetsReferringTo"); |
---|
211 | |
---|
212 | String substring = "http://nl.wikipedia.org"; |
---|
213 | List<Number> result = jdbcTargetDao.getTargetsReferringTo(substring); |
---|
214 | assertEquals(2, result.size()); |
---|
215 | assertEquals(1, result.get(0)); |
---|
216 | assertEquals(2, result.get(1)); |
---|
217 | } |
---|
218 | |
---|
219 | /* Test of getCachedRepresentations method, of class JdbcTargetDao. |
---|
220 | */ |
---|
221 | @Test |
---|
222 | public void testGetCachedRepresentations() { |
---|
223 | System.out.println("test getCachedRepresentations"); |
---|
224 | Number TargetID = 1; |
---|
225 | List<Number> result = jdbcTargetDao.getCachedRepresentations(TargetID); |
---|
226 | assertEquals(1, result.get(0)); |
---|
227 | assertEquals(2, result.get(1)); |
---|
228 | } |
---|
229 | } |
---|