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.rest; |
---|
19 | |
---|
20 | import com.sun.jersey.api.client.ClientResponse; |
---|
21 | import com.sun.jersey.api.client.WebResource; |
---|
22 | import com.sun.jersey.api.client.WebResource.Builder; |
---|
23 | import com.sun.jersey.core.util.Base64; |
---|
24 | import com.sun.jersey.spi.spring.container.servlet.SpringServlet; |
---|
25 | import com.sun.jersey.test.framework.AppDescriptor; |
---|
26 | import com.sun.jersey.test.framework.JerseyTest; |
---|
27 | import com.sun.jersey.test.framework.WebAppDescriptor; |
---|
28 | import eu.dasish.annotation.backend.Helpers; |
---|
29 | import eu.dasish.annotation.backend.NotInDataBaseException; |
---|
30 | import eu.dasish.annotation.backend.TestInstances; |
---|
31 | import eu.dasish.annotation.backend.dao.impl.JdbcResourceDaoTest; |
---|
32 | import eu.dasish.annotation.schema.Access; |
---|
33 | import eu.dasish.annotation.schema.Annotation; |
---|
34 | import eu.dasish.annotation.schema.AnnotationBody; |
---|
35 | import eu.dasish.annotation.schema.AnnotationBody.TextBody; |
---|
36 | import eu.dasish.annotation.schema.AnnotationBody.XmlBody; |
---|
37 | import eu.dasish.annotation.schema.ObjectFactory; |
---|
38 | import eu.dasish.annotation.schema.ResponseBody; |
---|
39 | import java.io.FileNotFoundException; |
---|
40 | import java.io.IOException; |
---|
41 | import java.net.URISyntaxException; |
---|
42 | import javax.ws.rs.core.HttpHeaders; |
---|
43 | import javax.ws.rs.core.MediaType; |
---|
44 | import javax.xml.bind.JAXBElement; |
---|
45 | import javax.xml.parsers.ParserConfigurationException; |
---|
46 | import org.junit.Test; |
---|
47 | import static org.junit.Assert.*; |
---|
48 | import org.junit.Before; |
---|
49 | import org.junit.Ignore; |
---|
50 | import org.junit.runner.RunWith; |
---|
51 | import org.springframework.beans.factory.annotation.Autowired; |
---|
52 | import org.springframework.dao.DataAccessException; |
---|
53 | import org.springframework.jdbc.core.JdbcTemplate; |
---|
54 | import org.springframework.test.context.ContextConfiguration; |
---|
55 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
---|
56 | import org.springframework.web.context.ContextLoaderListener; |
---|
57 | import org.springframework.web.context.request.RequestContextListener; |
---|
58 | import org.w3c.dom.Element; |
---|
59 | import org.xml.sax.SAXException; |
---|
60 | |
---|
61 | /** |
---|
62 | * |
---|
63 | * @author olhsha |
---|
64 | */ |
---|
65 | @RunWith(value = SpringJUnit4ClassRunner.class) |
---|
66 | @ContextConfiguration({"/spring-test-config/dataSource.xml"}) |
---|
67 | public class AnnotationsTest extends JerseyTest { |
---|
68 | |
---|
69 | @Autowired |
---|
70 | private JdbcTemplate jdbcTemplate; |
---|
71 | |
---|
72 | String _relativePath = "/backend/api"; |
---|
73 | |
---|
74 | |
---|
75 | @Override |
---|
76 | protected AppDescriptor configure() { |
---|
77 | return new WebAppDescriptor.Builder(AnnotationResource.class.getPackage().getName()) |
---|
78 | .servletClass(SpringServlet.class) |
---|
79 | .contextParam("contextConfigLocation", getApplicationContextFile()) |
---|
80 | .addFilter(DummySecurityFilter.class, "DummySecurityFilter") |
---|
81 | .requestListenerClass(RequestContextListener.class) |
---|
82 | .contextListenerClass(ContextLoaderListener.class) |
---|
83 | .contextPath("/backend").servletPath("/api") |
---|
84 | .build(); |
---|
85 | |
---|
86 | } |
---|
87 | |
---|
88 | private String getApplicationContextFile() { |
---|
89 | // sorry for the duplication, but JerseyTest is not aware of |
---|
90 | // @ContextConfiguration |
---|
91 | return "classpath:spring-config/componentscan.xml, classpath:spring-config/notebookDao.xml, classpath:spring-config/annotationDao.xml, classpath:spring-config/principalDao.xml, classpath:spring-config/targetDao.xml, classpath:spring-config/cachedRepresentationDao.xml, classpath:spring-config/dbDispatcher.xml, classpath:spring-config/jaxbMarshallerFactory.xml, classpath:spring-config/jaxbUnmarshallerFactory.xml, classpath:spring-test-config/dataSource.xml"; |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | |
---|
96 | @Override |
---|
97 | @Before |
---|
98 | public void setUp() throws DataAccessException, FileNotFoundException, URISyntaxException, Exception { |
---|
99 | super.setUp(); |
---|
100 | jdbcTemplate.execute("DROP SCHEMA PUBLIC CASCADE"); |
---|
101 | // consume the DashishAnnotatorCreate sql script to create the database |
---|
102 | jdbcTemplate.execute(JdbcResourceDaoTest.getNormalisedSql()); |
---|
103 | jdbcTemplate.execute(JdbcResourceDaoTest.getTestDataInsertSql()); |
---|
104 | |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | @Override |
---|
109 | public void tearDown() throws Exception { |
---|
110 | super.tearDown(); |
---|
111 | } |
---|
112 | /** |
---|
113 | * Test of getAnnotation method, of class annotationResource. Get <aid>. GET |
---|
114 | * api/annotations/<aid> |
---|
115 | */ |
---|
116 | @Test |
---|
117 | public void testGetAnnotation() throws NotInDataBaseException, IOException { |
---|
118 | |
---|
119 | // Authentication |
---|
120 | Builder responseBuilderAu = getAuthenticatedResource(resource().path("authentication/login")).accept(MediaType.TEXT_HTML); |
---|
121 | ClientResponse responseAu = responseBuilderAu.get(ClientResponse.class); |
---|
122 | assertEquals(200, responseAu.getStatus()); |
---|
123 | |
---|
124 | // Getting annotation |
---|
125 | System.out.println("testGetAnnotation"); |
---|
126 | final Annotation testAnnotation = (new TestInstances(_relativePath)).getAnnotationOne(); |
---|
127 | |
---|
128 | Builder responseBuilder = getAuthenticatedResource(resource().path("annotations/00000000-0000-0000-0000-000000000021")).accept(MediaType.TEXT_XML); |
---|
129 | ClientResponse response = responseBuilder.get(ClientResponse.class); |
---|
130 | |
---|
131 | |
---|
132 | assertEquals(200, response.getStatus()); |
---|
133 | Annotation entity = response.getEntity(Annotation.class); |
---|
134 | assertEquals(testAnnotation.getBody().getTextBody().getBody(), entity.getBody().getTextBody().getBody()); |
---|
135 | assertEquals(testAnnotation.getHeadline(), entity.getHeadline()); |
---|
136 | assertEquals(testAnnotation.getOwnerHref(), entity.getOwnerHref()); |
---|
137 | assertEquals(Access.WRITE, entity.getPermissions().getPublic()); |
---|
138 | assertEquals(3, entity.getPermissions().getPermission().size()); |
---|
139 | assertEquals("write", entity.getPermissions().getPermission().get(0).getLevel().value()); |
---|
140 | assertEquals(_relativePath+"/principals/00000000-0000-0000-0000-000000000112", entity.getPermissions().getPermission().get(0).getPrincipalHref()); |
---|
141 | assertEquals("read", entity.getPermissions().getPermission().get(1).getLevel().value()); |
---|
142 | assertEquals(_relativePath+"/principals/00000000-0000-0000-0000-000000000113", entity.getPermissions().getPermission().get(1).getPrincipalHref()); |
---|
143 | assertEquals("read", entity.getPermissions().getPermission().get(1).getLevel().value()); |
---|
144 | assertEquals(_relativePath+"/principals/00000000-0000-0000-0000-000000000221", entity.getPermissions().getPermission().get(2).getPrincipalHref()); |
---|
145 | assertEquals(2, entity.getTargets().getTargetInfo().size()); |
---|
146 | assertEquals(_relativePath+"/targets/00000000-0000-0000-0000-000000000031", entity.getTargets().getTargetInfo().get(0).getHref()); |
---|
147 | assertEquals(_relativePath+"/targets/00000000-0000-0000-0000-000000000032", entity.getTargets().getTargetInfo().get(1).getHref()); |
---|
148 | assertEquals(testAnnotation.getLastModified(), entity.getLastModified()); |
---|
149 | assertEquals(_relativePath+"/annotations/00000000-0000-0000-0000-000000000021", entity.getHref()); |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * Test of deleteAnnotation method, of class AnnotationResource. Delete |
---|
154 | * <nid>. DELETE api/annotations/<aid> |
---|
155 | */ |
---|
156 | @Test |
---|
157 | public void testDeleteAnnotation() { |
---|
158 | |
---|
159 | |
---|
160 | // Authentication |
---|
161 | Builder responseBuilderAu = getAuthenticatedResource(resource().path("authentication/login")).accept(MediaType.TEXT_HTML); |
---|
162 | ClientResponse responseAu = responseBuilderAu.get(ClientResponse.class); |
---|
163 | assertEquals(200, responseAu.getStatus()); |
---|
164 | |
---|
165 | // Deleting annotation |
---|
166 | System.out.println("testDeleteAnnotation"); |
---|
167 | String externalIDstring = "00000000-0000-0000-0000-000000000024"; |
---|
168 | final String requestUrl = "annotations/" + externalIDstring; |
---|
169 | System.out.println("requestUrl: " + requestUrl); |
---|
170 | |
---|
171 | Builder responseBuilder = getAuthenticatedResource(resource().path(requestUrl)).accept(MediaType.TEXT_XML); |
---|
172 | ClientResponse response = responseBuilder.delete(ClientResponse.class); |
---|
173 | assertEquals(200, response.getStatus()); |
---|
174 | assertEquals("1 annotation(s) is(are) deleted.", response.getEntity(String.class)); |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | * Test of createAnnotation method, of class AnnotationResource. POST |
---|
179 | * api/annotations/ |
---|
180 | */ |
---|
181 | @Test |
---|
182 | public void testCreateAnnotation() throws NotInDataBaseException, IOException{ |
---|
183 | |
---|
184 | |
---|
185 | // Authentication |
---|
186 | Builder responseBuilderAu = getAuthenticatedResource(resource().path("authentication/login")).accept(MediaType.TEXT_HTML); |
---|
187 | ClientResponse responseAu = responseBuilderAu.get(ClientResponse.class); |
---|
188 | assertEquals(200, responseAu.getStatus()); |
---|
189 | |
---|
190 | |
---|
191 | // Adding annotation |
---|
192 | System.out.println("test createAnnotation"); |
---|
193 | System.out.println("POST "+resource().getURI().toString()+"annotations/"); |
---|
194 | |
---|
195 | final Annotation annotationToAdd = (new TestInstances(_relativePath)).getAnnotationToAdd(); |
---|
196 | final JAXBElement<Annotation> jaxbElement = (new ObjectFactory()).createAnnotation(annotationToAdd); |
---|
197 | |
---|
198 | Builder responseBuilder = getAuthenticatedResource(resource().path("annotations/")).type(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML); |
---|
199 | ClientResponse response = responseBuilder.post(ClientResponse.class, jaxbElement); |
---|
200 | assertEquals(200, response.getStatus()); |
---|
201 | |
---|
202 | ResponseBody entity = response.getEntity(ResponseBody.class); |
---|
203 | Annotation entityA = entity.getAnnotation(); |
---|
204 | assertEquals("<html><body>some html 3</body></html>", entityA.getBody().getTextBody().getBody()); |
---|
205 | assertEquals("text/plain", entityA.getBody().getTextBody().getMimeType()); |
---|
206 | assertEquals("Annotation to add to test DAO", entityA.getHeadline()); |
---|
207 | assertEquals(1, entityA.getPermissions().getPermission().size()); |
---|
208 | assertEquals(Access.ALL, entityA.getPermissions().getPermission().get(0).getLevel()); |
---|
209 | assertEquals(_relativePath + "/principals/00000000-0000-0000-0000-000000000111", entityA.getPermissions().getPermission().get(0).getPrincipalHref()); |
---|
210 | assertEquals(Access.WRITE, entityA.getPermissions().getPublic()); |
---|
211 | assertEquals(_relativePath + "/principals/00000000-0000-0000-0000-000000000113", entityA.getOwnerHref()); |
---|
212 | assertEquals("http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia#de_Opdracht", entityA.getTargets().getTargetInfo().get(0).getLink()); |
---|
213 | assertEquals(_relativePath+ "/targets/00000000-0000-0000-0000-000000000031", entityA.getTargets().getTargetInfo().get(0).getHref()); |
---|
214 | assertEquals("version 1.0", entityA.getTargets().getTargetInfo().get(0).getVersion()); |
---|
215 | |
---|
216 | } |
---|
217 | |
---|
218 | |
---|
219 | |
---|
220 | |
---|
221 | @Test |
---|
222 | public void testUpdateAnnotation() throws NotInDataBaseException, IOException{ |
---|
223 | |
---|
224 | |
---|
225 | // Authentication |
---|
226 | Builder responseBuilderAu = getAuthenticatedResource(resource().path("authentication/login")).accept(MediaType.TEXT_HTML); |
---|
227 | ClientResponse responseAu = responseBuilderAu.get(ClientResponse.class); |
---|
228 | assertEquals(200, responseAu.getStatus()); |
---|
229 | |
---|
230 | |
---|
231 | // updating annotation |
---|
232 | System.out.println("test updateAnnotation"); |
---|
233 | System.out.println("PUT "+resource().getURI().toString()+"annotations/00000000-0000-0000-0000-000000000021"); |
---|
234 | |
---|
235 | Annotation annotation = (new TestInstances(_relativePath)).getAnnotationOne(); |
---|
236 | annotation.getPermissions().setPublic(Access.READ); |
---|
237 | annotation.setHeadline("updated annotation 1"); |
---|
238 | annotation.getPermissions().getPermission().get(1).setLevel(Access.WRITE); |
---|
239 | AnnotationBody ab = new AnnotationBody(); |
---|
240 | TextBody tb = new TextBody(); |
---|
241 | ab.setTextBody(tb); |
---|
242 | tb.setMimeType("text/plain"); |
---|
243 | tb.setBody("some text body l"); |
---|
244 | annotation.setBody(ab); |
---|
245 | final JAXBElement<Annotation> jaxbElement = (new ObjectFactory()).createAnnotation(annotation); |
---|
246 | |
---|
247 | Builder responseBuilder = getAuthenticatedResource(resource().path("annotations/00000000-0000-0000-0000-000000000021")).type(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML); |
---|
248 | ClientResponse response = responseBuilder.put(ClientResponse.class, jaxbElement); |
---|
249 | assertEquals(200, response.getStatus()); |
---|
250 | |
---|
251 | ResponseBody entity = response.getEntity(ResponseBody.class); |
---|
252 | Annotation entityA = entity.getAnnotation(); |
---|
253 | assertEquals("some text body l", entityA.getBody().getTextBody().getBody()); |
---|
254 | assertEquals("text/plain", entityA.getBody().getTextBody().getMimeType()); |
---|
255 | assertEquals("updated annotation 1", entityA.getHeadline()); |
---|
256 | assertEquals(3, entityA.getPermissions().getPermission().size()); |
---|
257 | assertEquals(Access.READ, entityA.getPermissions().getPublic()); |
---|
258 | assertEquals(Access.WRITE, entityA.getPermissions().getPermission().get(0).getLevel()); |
---|
259 | assertEquals(Access.WRITE, entityA.getPermissions().getPermission().get(1).getLevel()); |
---|
260 | assertEquals(Access.READ, entityA.getPermissions().getPermission().get(2).getLevel()); |
---|
261 | // assertEquals(_relativePath + "/principals/00000000-0000-0000-0000-000000000111", entityA.getOwnerHref()); |
---|
262 | // assertEquals("http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia#de_Opdracht", entityA.getTargets().getTargetInfo().get(0).getLink()); |
---|
263 | // assertEquals(_relativePath+ "/targets/00000000-0000-0000-0000-000000000031", entityA.getTargets().getTargetInfo().get(0).getHref()); |
---|
264 | // |
---|
265 | } |
---|
266 | |
---|
267 | @Test |
---|
268 | public void testUpdateAnnotationBody() throws NotInDataBaseException, IOException, ParserConfigurationException, SAXException{ |
---|
269 | |
---|
270 | |
---|
271 | // Authentication |
---|
272 | Builder responseBuilderAu = getAuthenticatedResource(resource().path("authentication/login")).accept(MediaType.TEXT_HTML); |
---|
273 | ClientResponse responseAu = responseBuilderAu.get(ClientResponse.class); |
---|
274 | assertEquals(200, responseAu.getStatus()); |
---|
275 | |
---|
276 | |
---|
277 | // updating annotation |
---|
278 | System.out.println("test updateAnnotation"); |
---|
279 | System.out.println("PUT "+resource().getURI().toString()+"annotations/00000000-0000-0000-0000-000000000021/body"); |
---|
280 | AnnotationBody ab = new AnnotationBody(); |
---|
281 | XmlBody xmlb = new XmlBody(); |
---|
282 | ab.setXmlBody(xmlb); |
---|
283 | xmlb.setMimeType("text/xml"); |
---|
284 | String testXml = "<span style=\"background-color:rgb(0,0,153);color:rgb(255,255,255);border: thick solid rgb(0, 0, 153);\">test</span>"; |
---|
285 | Element el = Helpers.stringToElement(testXml); |
---|
286 | xmlb.setAny(el); |
---|
287 | final JAXBElement<AnnotationBody> jaxbElement = (new ObjectFactory()).createAnnotationBody(ab); |
---|
288 | |
---|
289 | |
---|
290 | Builder responseBuilder = getAuthenticatedResource(resource().path("annotations/00000000-0000-0000-0000-000000000021/body")).type(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML); |
---|
291 | ClientResponse response = responseBuilder.put(ClientResponse.class, jaxbElement); |
---|
292 | assertEquals(200, response.getStatus()); |
---|
293 | |
---|
294 | ResponseBody entity = response.getEntity(ResponseBody.class); |
---|
295 | Annotation entityA = entity.getAnnotation(); |
---|
296 | assertEquals("test", entityA.getBody().getXmlBody().getAny().getTextContent()); |
---|
297 | assertEquals("span", entityA.getBody().getXmlBody().getAny().getNodeName()); |
---|
298 | assertEquals("background-color:rgb(0,0,153);color:rgb(255,255,255);border: thick solid rgb(0, 0, 153);", entityA.getBody().getXmlBody().getAny().getAttribute("style")); |
---|
299 | |
---|
300 | assertEquals("text/xml", entityA.getBody().getXmlBody().getMimeType()); |
---|
301 | assertEquals("Sagrada Famiglia", entityA.getHeadline()); |
---|
302 | assertEquals(3, entityA.getPermissions().getPermission().size()); |
---|
303 | assertEquals(Access.WRITE , entityA.getPermissions().getPublic()); |
---|
304 | |
---|
305 | |
---|
306 | assertEquals(_relativePath + "/principals/00000000-0000-0000-0000-000000000111", entityA.getOwnerHref()); |
---|
307 | assertEquals("http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia#de_Opdracht", entityA.getTargets().getTargetInfo().get(0).getLink()); |
---|
308 | assertEquals(_relativePath+ "/targets/00000000-0000-0000-0000-000000000031", entityA.getTargets().getTargetInfo().get(0).getHref()); |
---|
309 | |
---|
310 | } |
---|
311 | |
---|
312 | @Test |
---|
313 | public void testUpdateAnnotationHeadline() throws IOException{ |
---|
314 | |
---|
315 | |
---|
316 | // Authentication |
---|
317 | Builder responseBuilderAu = getAuthenticatedResource(resource().path("authentication/login")).accept(MediaType.TEXT_HTML); |
---|
318 | ClientResponse responseAu = responseBuilderAu.get(ClientResponse.class); |
---|
319 | assertEquals(200, responseAu.getStatus()); |
---|
320 | |
---|
321 | |
---|
322 | // updating annotation |
---|
323 | System.out.println("test updateAnnotationHeadline"); |
---|
324 | System.out.println("PUT "+resource().getURI().toString()+"annotations/00000000-0000-0000-0000-000000000021/headline"); |
---|
325 | String newHeadline = "new Headline"; |
---|
326 | Builder responseBuilder = getAuthenticatedResource(resource().path("annotations/00000000-0000-0000-0000-000000000021/headline")).type(MediaType.TEXT_PLAIN).accept(MediaType.APPLICATION_XML); |
---|
327 | ClientResponse response = responseBuilder.put(ClientResponse.class, newHeadline); |
---|
328 | assertEquals(200, response.getStatus()); |
---|
329 | |
---|
330 | ResponseBody entity = response.getEntity(ResponseBody.class); |
---|
331 | Annotation entityA = entity.getAnnotation(); |
---|
332 | assertEquals("new Headline", entityA.getHeadline()); |
---|
333 | } |
---|
334 | |
---|
335 | protected Builder getAuthenticatedResource(WebResource resource) { |
---|
336 | return resource.header(HttpHeaders.AUTHORIZATION, "Basic " + new String(Base64.encode(DummyPrincipal.DUMMY_PRINCIPAL.getName()+":alicepassword"))); |
---|
337 | } |
---|
338 | } |
---|