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.TestInstances; |
---|
29 | import eu.dasish.annotation.backend.dao.impl.JdbcResourceDaoTest; |
---|
30 | import eu.dasish.annotation.schema.Annotation; |
---|
31 | import eu.dasish.annotation.schema.AnnotationBody; |
---|
32 | import eu.dasish.annotation.schema.AnnotationBody.TextBody; |
---|
33 | import eu.dasish.annotation.schema.ObjectFactory; |
---|
34 | import eu.dasish.annotation.schema.ResponseBody; |
---|
35 | import eu.dasish.annotation.schema.TargetInfo; |
---|
36 | import eu.dasish.annotation.schema.TargetInfoList; |
---|
37 | import java.io.FileNotFoundException; |
---|
38 | import java.net.URISyntaxException; |
---|
39 | import java.sql.SQLException; |
---|
40 | import java.util.UUID; |
---|
41 | import javax.ws.rs.core.HttpHeaders; |
---|
42 | import javax.ws.rs.core.MediaType; |
---|
43 | import javax.xml.bind.JAXBElement; |
---|
44 | import javax.xml.datatype.DatatypeConfigurationException; |
---|
45 | import javax.xml.datatype.DatatypeFactory; |
---|
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 | |
---|
59 | /** |
---|
60 | * |
---|
61 | * @author olhsha |
---|
62 | */ |
---|
63 | @RunWith(value = SpringJUnit4ClassRunner.class) |
---|
64 | @ContextConfiguration({"/spring-test-config/dataSource.xml"}) |
---|
65 | public class AnnotationsTest extends JerseyTest { |
---|
66 | |
---|
67 | @Autowired |
---|
68 | private JdbcTemplate jdbcTemplate; |
---|
69 | |
---|
70 | |
---|
71 | |
---|
72 | |
---|
73 | @Override |
---|
74 | protected AppDescriptor configure() { |
---|
75 | return new WebAppDescriptor.Builder(AnnotationResource.class.getPackage().getName()) |
---|
76 | .servletClass(SpringServlet.class) |
---|
77 | .contextParam("contextConfigLocation", getApplicationContextFile()) |
---|
78 | .addFilter(DummySecurityFilter.class, "DummySecurityFilter") |
---|
79 | .requestListenerClass(RequestContextListener.class) |
---|
80 | .contextListenerClass(ContextLoaderListener.class) |
---|
81 | .build(); |
---|
82 | |
---|
83 | } |
---|
84 | |
---|
85 | private String getApplicationContextFile() { |
---|
86 | // sorry for the duplication, but JerseyTest is not aware of |
---|
87 | // @ContextConfiguration |
---|
88 | return "classpath:spring-config/componentscan.xml, classpath:spring-config/notebookDao.xml, classpath:spring-config/annotationDao.xml, classpath:spring-config/userDao.xml, classpath:spring-config/targetDao.xml, classpath:spring-config/cachedRepresentationDao.xml, classpath:spring-config/dbIntegrityService.xml, classpath:spring-config/jaxbMarshallerFactory.xml, classpath:spring-test-config/dataSource.xml"; |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | |
---|
93 | @Override |
---|
94 | @Before |
---|
95 | public void setUp() throws DataAccessException, FileNotFoundException, URISyntaxException, Exception { |
---|
96 | super.setUp(); |
---|
97 | jdbcTemplate.execute("DROP SCHEMA PUBLIC CASCADE"); |
---|
98 | // consume the DashishAnnotatorCreate sql script to create the database |
---|
99 | jdbcTemplate.execute(JdbcResourceDaoTest.getNormalisedSql()); |
---|
100 | jdbcTemplate.execute(JdbcResourceDaoTest.getTestDataInsertSql()); |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | @Override |
---|
105 | public void tearDown() throws Exception { |
---|
106 | super.tearDown(); |
---|
107 | } |
---|
108 | /** |
---|
109 | * Test of getAnnotation method, of class annotationResource. Get <aid>. GET |
---|
110 | * api/annotations/<aid> |
---|
111 | */ |
---|
112 | @Test |
---|
113 | public void testGetAnnotation() throws SQLException, DatatypeConfigurationException { |
---|
114 | |
---|
115 | // Authentication |
---|
116 | Builder responseBuilderAu = getAuthenticatedResource(resource().path("authentication/login")).accept(MediaType.TEXT_XML); |
---|
117 | ClientResponse responseAu = responseBuilderAu.get(ClientResponse.class); |
---|
118 | assertEquals(200, responseAu.getStatus()); |
---|
119 | |
---|
120 | // Getting annotation |
---|
121 | System.out.println("testGetAnnotation"); |
---|
122 | final String externalIDstring = "00000000-0000-0000-0000-000000000021"; |
---|
123 | final Annotation testAnnotation = (new TestInstances(resource().getURI().toString())).getAnnotationOne(); |
---|
124 | |
---|
125 | final String requestUrl = "annotations/" + externalIDstring; |
---|
126 | System.out.println("requestUrl: " + requestUrl); |
---|
127 | |
---|
128 | Builder responseBuilder = getAuthenticatedResource(resource().path(requestUrl)).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.getOwnerRef(), entity.getOwnerRef()); |
---|
137 | assertEquals(3, entity.getPermissions().getUserWithPermission().size()); |
---|
138 | assertEquals("writer", entity.getPermissions().getUserWithPermission().get(0).getPermission().value()); |
---|
139 | assertEquals(resource().getURI()+"users/"+"00000000-0000-0000-0000-000000000112", entity.getPermissions().getUserWithPermission().get(0).getRef()); |
---|
140 | assertEquals("reader", entity.getPermissions().getUserWithPermission().get(1).getPermission().value()); |
---|
141 | assertEquals(resource().getURI()+"users/"+"00000000-0000-0000-0000-000000000113", entity.getPermissions().getUserWithPermission().get(1).getRef()); |
---|
142 | assertEquals("reader", entity.getPermissions().getUserWithPermission().get(1).getPermission().value()); |
---|
143 | assertEquals(resource().getURI()+"users/"+"00000000-0000-0000-0000-000000000221", entity.getPermissions().getUserWithPermission().get(2).getRef()); |
---|
144 | assertEquals(2, entity.getTargets().getTargetInfo().size()); |
---|
145 | assertEquals(resource().getURI().toString()+"targets/"+"00000000-0000-0000-0000-000000000031", entity.getTargets().getTargetInfo().get(0).getRef()); |
---|
146 | assertEquals(resource().getURI().toString()+"targets/"+"00000000-0000-0000-0000-000000000032", entity.getTargets().getTargetInfo().get(1).getRef()); |
---|
147 | assertEquals(testAnnotation.getLastModified(), entity.getLastModified()); |
---|
148 | assertEquals(resource().getURI()+requestUrl, entity.getURI()); |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * Test of deleteAnnotation method, of class AnnotationResource. Delete |
---|
153 | * <nid>. DELETE api/annotations/<aid> |
---|
154 | */ |
---|
155 | @Test |
---|
156 | public void testDeleteAnnotation() throws SQLException { |
---|
157 | |
---|
158 | |
---|
159 | // Authentication |
---|
160 | Builder responseBuilderAu = getAuthenticatedResource(resource().path("authentication/login")).accept(MediaType.TEXT_XML); |
---|
161 | ClientResponse responseAu = responseBuilderAu.get(ClientResponse.class); |
---|
162 | assertEquals(200, responseAu.getStatus()); |
---|
163 | |
---|
164 | // Deleting annotation |
---|
165 | System.out.println("testDeleteAnnotation"); |
---|
166 | String externalIDstring = "00000000-0000-0000-0000-000000000024"; |
---|
167 | final String requestUrl = "annotations/" + externalIDstring; |
---|
168 | System.out.println("requestUrl: " + requestUrl); |
---|
169 | |
---|
170 | Builder responseBuilder = getAuthenticatedResource(resource().path(requestUrl)).accept(MediaType.TEXT_XML); |
---|
171 | ClientResponse response = responseBuilder.delete(ClientResponse.class); |
---|
172 | assertEquals(200, response.getStatus()); |
---|
173 | assertEquals("1 annotation(s) deleted.", response.getEntity(String.class)); |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
177 | * Test of createAnnotation method, of class AnnotationResource. POST |
---|
178 | * api/annotations/ |
---|
179 | */ |
---|
180 | @Test |
---|
181 | public void testCreateAnnotation() throws SQLException, InstantiationException, IllegalAccessException, DatatypeConfigurationException, Exception { |
---|
182 | |
---|
183 | |
---|
184 | // Authentication |
---|
185 | Builder responseBuilderAu = getAuthenticatedResource(resource().path("authentication/login")).accept(MediaType.TEXT_XML); |
---|
186 | ClientResponse responseAu = responseBuilderAu.get(ClientResponse.class); |
---|
187 | assertEquals(200, responseAu.getStatus()); |
---|
188 | |
---|
189 | |
---|
190 | // Adding annotation |
---|
191 | System.out.println("test createAnnotation"); |
---|
192 | System.out.println("POST "+resource().getURI().toString()+"annotations/"); |
---|
193 | final String ownerString = resource().getURI().toString()+"users/"+"00000000-0000-0000-0000-000000000113"; |
---|
194 | final Annotation annotationToAdd = new Annotation(); |
---|
195 | final JAXBElement<Annotation> jaxbElement = (new ObjectFactory()).createAnnotation(annotationToAdd); |
---|
196 | annotationToAdd.setPermissions(null); |
---|
197 | annotationToAdd.setOwnerRef(ownerString); |
---|
198 | annotationToAdd.setURI(resource().getURI().toString()+"annotations/"+ UUID.randomUUID().toString()); |
---|
199 | annotationToAdd.setLastModified(DatatypeFactory.newInstance().newXMLGregorianCalendar("2013-08-12T09:25:00.383000Z")); |
---|
200 | |
---|
201 | TargetInfoList targetInfoList = new TargetInfoList(); |
---|
202 | annotationToAdd.setTargets(targetInfoList); |
---|
203 | TargetInfo targetInfo = new TargetInfo(); |
---|
204 | targetInfo.setLink("http://nl.wikipedia.org/wiki/Viktor_Janoekovytsj#Biografie"); |
---|
205 | targetInfo.setRef(resource().getURI().toString()+"targets/"+UUID.randomUUID().toString()); |
---|
206 | targetInfo.setVersion("5 apr 2013 om 18:42"); |
---|
207 | targetInfoList.getTargetInfo().add(targetInfo); |
---|
208 | |
---|
209 | AnnotationBody annotationBody = new AnnotationBody(); |
---|
210 | annotationBody.setXmlBody(null); |
---|
211 | TextBody textBody = new TextBody(); |
---|
212 | textBody.setMimeType("plain/text"); |
---|
213 | textBody.setBody("yanuk - zek"); |
---|
214 | annotationBody.setTextBody(textBody); |
---|
215 | annotationToAdd.setBody(annotationBody); |
---|
216 | |
---|
217 | Builder responseBuilder = getAuthenticatedResource(resource().path("annotations/")).type(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML); |
---|
218 | ClientResponse response = responseBuilder.post(ClientResponse.class, jaxbElement); |
---|
219 | assertEquals(200, response.getStatus()); |
---|
220 | |
---|
221 | ResponseBody entity = response.getEntity(ResponseBody.class); |
---|
222 | Annotation entityA = entity.getAnnotation(); |
---|
223 | assertEquals(annotationToAdd.getBody().getTextBody().getBody(), entityA.getBody().getTextBody().getBody()); |
---|
224 | assertEquals(annotationToAdd.getBody().getTextBody().getMimeType(), entityA.getBody().getTextBody().getMimeType()); |
---|
225 | assertEquals(annotationToAdd.getHeadline(), entityA.getHeadline()); |
---|
226 | assertEquals(0, entityA.getPermissions().getUserWithPermission().size()); |
---|
227 | assertEquals(annotationToAdd.getOwnerRef(), entityA.getOwnerRef()); |
---|
228 | assertEquals(annotationToAdd.getTargets().getTargetInfo().get(0).getLink(), entityA.getTargets().getTargetInfo().get(0).getLink()); |
---|
229 | // new ref is generated |
---|
230 | //assertEquals(annotationToAdd.getTargets().getTargetInfo().get(0).getRef(), entityA.getTargets().getTargetInfo().get(0).getRef()); |
---|
231 | assertEquals(annotationToAdd.getTargets().getTargetInfo().get(0).getVersion(), entityA.getTargets().getTargetInfo().get(0).getVersion()); |
---|
232 | //last modified is updated by the server |
---|
233 | //assertEquals(annotationToAdd.getLastModified(), entityA.getLastModified()); |
---|
234 | assertEquals(annotationToAdd.getOwnerRef(), entityA.getOwnerRef()); |
---|
235 | } |
---|
236 | |
---|
237 | |
---|
238 | |
---|
239 | protected Builder getAuthenticatedResource(WebResource resource) { |
---|
240 | return resource.header(HttpHeaders.AUTHORIZATION, "Basic " + new String(Base64.encode(DummyPrincipal.DUMMY_PRINCIPAL.getName()+":olhapassword"))); |
---|
241 | } |
---|
242 | |
---|
243 | |
---|
244 | } |
---|