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 eu.dasish.annotation.backend.ForbiddenException; |
---|
21 | import eu.dasish.annotation.backend.Helpers; |
---|
22 | import eu.dasish.annotation.backend.NotInDataBaseException; |
---|
23 | import eu.dasish.annotation.backend.Resource; |
---|
24 | import eu.dasish.annotation.backend.dao.DBDispatcher; |
---|
25 | import eu.dasish.annotation.backend.TestInstances; |
---|
26 | import eu.dasish.annotation.schema.Access; |
---|
27 | import eu.dasish.annotation.schema.Action; |
---|
28 | import eu.dasish.annotation.schema.ActionList; |
---|
29 | import eu.dasish.annotation.schema.Annotation; |
---|
30 | import eu.dasish.annotation.schema.ObjectFactory; |
---|
31 | import eu.dasish.annotation.schema.ResponseBody; |
---|
32 | import eu.dasish.annotation.schema.AnnotationActionName; |
---|
33 | import eu.dasish.annotation.schema.AnnotationBody; |
---|
34 | import eu.dasish.annotation.schema.AnnotationBody.TextBody; |
---|
35 | import java.io.IOException; |
---|
36 | import javax.xml.bind.JAXBElement; |
---|
37 | import org.jmock.Expectations; |
---|
38 | import org.jmock.Mockery; |
---|
39 | import org.junit.Test; |
---|
40 | import static org.junit.Assert.*; |
---|
41 | import org.junit.runner.RunWith; |
---|
42 | import org.springframework.beans.factory.annotation.Autowired; |
---|
43 | import org.springframework.test.context.ContextConfiguration; |
---|
44 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
---|
45 | import java.util.UUID; |
---|
46 | import org.springframework.mock.web.MockHttpServletRequest; |
---|
47 | |
---|
48 | /** |
---|
49 | * |
---|
50 | * @author olhsha |
---|
51 | */ |
---|
52 | @RunWith(value = SpringJUnit4ClassRunner.class) |
---|
53 | @ContextConfiguration(locations = {"/spring-test-config/mockeryRest.xml", "/spring-test-config/mockDBDispatcher.xml", |
---|
54 | "/spring-config/jaxbMarshallerFactory.xml", "/spring-config/jaxbUnmarshallerFactory.xml"}) |
---|
55 | public class AnnotationResourceTest { |
---|
56 | |
---|
57 | @Autowired |
---|
58 | private Mockery mockeryRest; |
---|
59 | @Autowired |
---|
60 | private DBDispatcher mockDbDispatcher; |
---|
61 | @Autowired |
---|
62 | private AnnotationResource annotationResource; |
---|
63 | private MockHttpServletRequest mockRequest; |
---|
64 | |
---|
65 | public AnnotationResourceTest() { |
---|
66 | mockRequest = new MockHttpServletRequest(); |
---|
67 | } |
---|
68 | |
---|
69 | // public Number getPrincipalID() throws IOException { |
---|
70 | // dbIntegrityService.setServiceURI(this.getRelativeServiceURI()); |
---|
71 | // verboseOutput = new VerboseOutput(httpServletResponse, loggerServer); |
---|
72 | // String remotePrincipal = httpServletRequest.getRemotePrincipal(); |
---|
73 | // if (remotePrincipal != null) { |
---|
74 | // if (!remotePrincipal.equals(anonym)) { |
---|
75 | // final Number principalID = dbIntegrityService.getPrincipalInternalIDFromRemoteID(remotePrincipal); |
---|
76 | // if (principalID != null) { |
---|
77 | // return principalID; |
---|
78 | // } |
---|
79 | // verboseOutput.REMOTE_PRINCIPAL_NOT_FOUND(remotePrincipal, dbIntegrityService.getDataBaseAdmin().getDisplayName(), dbIntegrityService.getDataBaseAdmin().getEMail()); |
---|
80 | // return null; |
---|
81 | // } |
---|
82 | // } |
---|
83 | // |
---|
84 | // verboseOutput.NOT_LOGGED_IN(dbIntegrityService.getDataBaseAdmin().getDisplayName(), dbIntegrityService.getDataBaseAdmin().getEMail()); |
---|
85 | // return null; |
---|
86 | // |
---|
87 | // } |
---|
88 | @Test |
---|
89 | public void testGetAnnotation() throws NotInDataBaseException, IOException { |
---|
90 | System.out.println("getAnnotation"); |
---|
91 | final String externalIDstring = "00000000-0000-0000-0000-000000000021"; |
---|
92 | final Annotation expectedAnnotation = (new TestInstances("/api")).getAnnotationOne(); |
---|
93 | annotationResource.setHttpServletRequest(mockRequest); |
---|
94 | mockRequest.setRemoteUser("alice@mail.domain"); |
---|
95 | mockRequest.setContextPath("/backend"); |
---|
96 | mockRequest.setServletPath("/api"); |
---|
97 | mockeryRest.checking(new Expectations() { |
---|
98 | { |
---|
99 | |
---|
100 | oneOf(mockDbDispatcher).setResourcesPaths("/backend/api"); |
---|
101 | |
---|
102 | oneOf(mockDbDispatcher).getPrincipalInternalIDFromRemoteID("alice@mail.domain"); |
---|
103 | will(returnValue(3)); |
---|
104 | |
---|
105 | oneOf(mockDbDispatcher).getResourceInternalIdentifier(with(aNonNull(UUID.class)), with(aNonNull((Resource.class)))); |
---|
106 | will(returnValue(1)); |
---|
107 | |
---|
108 | |
---|
109 | oneOf(mockDbDispatcher).canDo(Access.READ, 3, 1, Resource.ANNOTATION); |
---|
110 | will(returnValue(true)); |
---|
111 | |
---|
112 | oneOf(mockDbDispatcher).getAnnotation(1); |
---|
113 | will(returnValue(expectedAnnotation)); |
---|
114 | } |
---|
115 | }); |
---|
116 | |
---|
117 | |
---|
118 | JAXBElement<Annotation> result = annotationResource.getAnnotation(externalIDstring); |
---|
119 | assertTrue(expectedAnnotation.equals(result.getValue())); |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * Test of deleteAnnotation method, of class AnnotationResource. |
---|
124 | */ |
---|
125 | @Test |
---|
126 | public void testDeleteAnnotation() throws NotInDataBaseException, IOException { |
---|
127 | System.out.println("deleteAnnotation"); |
---|
128 | |
---|
129 | final int[] mockDelete = new int[4]; |
---|
130 | mockDelete[0] = 1; // # deleted annotations |
---|
131 | mockDelete[3] = 1; // # deleted annotation_prinipal_accesss |
---|
132 | mockDelete[2] = 2; // # deleted annotations_target_Targets, (4,3), (4,4) |
---|
133 | mockDelete[3] = 1; // # deletd Targets, 4 |
---|
134 | |
---|
135 | annotationResource.setHttpServletRequest(mockRequest); |
---|
136 | mockRequest.setRemoteUser("alice@mail.domain"); |
---|
137 | mockRequest.setContextPath("/backend"); |
---|
138 | mockRequest.setServletPath("/api"); |
---|
139 | |
---|
140 | mockeryRest.checking(new Expectations() { |
---|
141 | { |
---|
142 | oneOf(mockDbDispatcher).setResourcesPaths("/backend/api"); |
---|
143 | |
---|
144 | oneOf(mockDbDispatcher).getPrincipalInternalIDFromRemoteID("alice@mail.domain"); |
---|
145 | will(returnValue(3)); |
---|
146 | |
---|
147 | oneOf(mockDbDispatcher).getResourceInternalIdentifier(with(aNonNull(UUID.class)), with(aNonNull((Resource.class)))); |
---|
148 | will(returnValue(4)); |
---|
149 | |
---|
150 | |
---|
151 | oneOf(mockDbDispatcher).getAnnotationOwnerID(4); |
---|
152 | will(returnValue(3)); |
---|
153 | |
---|
154 | oneOf(mockDbDispatcher).canDo(Access.ALL, 3, 4, Resource.ANNOTATION); |
---|
155 | will(returnValue(true)); |
---|
156 | |
---|
157 | oneOf(mockDbDispatcher).deleteAnnotation(4); |
---|
158 | will(returnValue(mockDelete)); |
---|
159 | } |
---|
160 | }); |
---|
161 | |
---|
162 | |
---|
163 | String result = annotationResource.deleteAnnotation("00000000-0000-0000-0000-000000000024"); |
---|
164 | assertEquals("1 annotation(s) is(are) deleted.", result); |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * Test of createAnnotation method, of class AnnotationResource. |
---|
169 | */ |
---|
170 | @Test |
---|
171 | public void testCreateAnnotation() throws IOException, NotInDataBaseException { |
---|
172 | System.out.println("test createAnnotation"); |
---|
173 | |
---|
174 | final Annotation annotationToAdd = (new TestInstances("/api")).getAnnotationToAdd(); |
---|
175 | final Number newAnnotationID = 5; |
---|
176 | |
---|
177 | |
---|
178 | final Annotation addedAnnotation = (new ObjectFactory()).createAnnotation(annotationToAdd).getValue(); |
---|
179 | String externalId = Helpers.generateUUID().toString(); |
---|
180 | addedAnnotation.setId(externalId); |
---|
181 | addedAnnotation.setHref("/backend/api/annotations/" + externalId); |
---|
182 | addedAnnotation.setOwnerHref("/backend/api/principals/00000000-0000-0000-0000-000000000113"); |
---|
183 | |
---|
184 | final ResponseBody mockEnvelope = new ResponseBody(); |
---|
185 | final Action action = new Action(); |
---|
186 | final ActionList actionList = new ActionList(); |
---|
187 | mockEnvelope.setAnnotation(addedAnnotation); |
---|
188 | mockEnvelope.setActionList(actionList); |
---|
189 | actionList.getAction().add(action); |
---|
190 | action.setMessage(AnnotationActionName.CREATE_CACHED_REPRESENTATION.value()); |
---|
191 | action.setObject("/backend/api/targets/00000000-0000-0000-0000-000000000036"); |
---|
192 | |
---|
193 | annotationResource.setHttpServletRequest(mockRequest); |
---|
194 | mockRequest.setRemoteUser("alice@mail.domain"); |
---|
195 | mockRequest.setContextPath("/backend"); |
---|
196 | mockRequest.setServletPath("/api"); |
---|
197 | mockeryRest.checking(new Expectations() { |
---|
198 | { |
---|
199 | |
---|
200 | oneOf(mockDbDispatcher).setResourcesPaths("/backend/api"); |
---|
201 | |
---|
202 | oneOf(mockDbDispatcher).getPrincipalInternalIDFromRemoteID("alice@mail.domain"); |
---|
203 | will(returnValue(3)); |
---|
204 | |
---|
205 | oneOf(mockDbDispatcher).addPrincipalsAnnotation(3, annotationToAdd); |
---|
206 | will(returnValue(newAnnotationID)); |
---|
207 | |
---|
208 | oneOf(mockDbDispatcher).getAnnotation(newAnnotationID); |
---|
209 | will(returnValue(addedAnnotation)); |
---|
210 | |
---|
211 | oneOf(mockDbDispatcher).makeAnnotationResponseEnvelope(newAnnotationID); |
---|
212 | will(returnValue(mockEnvelope)); |
---|
213 | |
---|
214 | } |
---|
215 | }); |
---|
216 | |
---|
217 | |
---|
218 | |
---|
219 | JAXBElement<ResponseBody> result = annotationResource.createAnnotation(annotationToAdd); |
---|
220 | Annotation newAnnotation = result.getValue().getAnnotation(); |
---|
221 | String actionName = result.getValue().getActionList().getAction().get(0).getMessage(); |
---|
222 | assertEquals(addedAnnotation.getOwnerHref(), newAnnotation.getOwnerHref()); |
---|
223 | assertEquals(addedAnnotation.getId(), newAnnotation.getId()); |
---|
224 | assertEquals(addedAnnotation.getHref(), newAnnotation.getHref()); |
---|
225 | assertEquals(addedAnnotation.getHeadline(), newAnnotation.getHeadline()); |
---|
226 | assertEquals(addedAnnotation.getTargets(), newAnnotation.getTargets()); |
---|
227 | assertEquals(addedAnnotation.getLastModified(), newAnnotation.getLastModified()); |
---|
228 | assertEquals(addedAnnotation.getBody(), newAnnotation.getBody()); |
---|
229 | assertEquals(AnnotationActionName.CREATE_CACHED_REPRESENTATION.value(), actionName); |
---|
230 | assertEquals(Access.WRITE, addedAnnotation.getPermissions().getPublic()); |
---|
231 | } |
---|
232 | |
---|
233 | @Test |
---|
234 | public void testUpdateAnnotation() throws NotInDataBaseException, IOException, ForbiddenException{ |
---|
235 | System.out.println("test updateAnnotation"); |
---|
236 | |
---|
237 | final Annotation annotation = (new TestInstances("/backend/api")).getAnnotationOne(); |
---|
238 | annotation.getPermissions().setPublic(Access.READ); |
---|
239 | annotation.setHeadline("updated annotation 1"); |
---|
240 | annotation.getPermissions().getPermission().get(1).setLevel(Access.WRITE); |
---|
241 | AnnotationBody ab = new AnnotationBody(); |
---|
242 | TextBody tb = new TextBody(); |
---|
243 | ab.setTextBody(tb); |
---|
244 | tb.setMimeType("text/plain"); |
---|
245 | tb.setBody("some text body l"); |
---|
246 | annotation.setBody(ab); |
---|
247 | |
---|
248 | final ResponseBody mockEnvelope = new ResponseBody(); |
---|
249 | final ActionList actionList = new ActionList(); |
---|
250 | mockEnvelope.setAnnotation(annotation); |
---|
251 | mockEnvelope.setActionList(actionList); |
---|
252 | |
---|
253 | annotationResource.setHttpServletRequest(mockRequest); |
---|
254 | mockRequest.setRemoteUser("twagoo@mpi.nl"); |
---|
255 | mockRequest.setContextPath("/backend"); |
---|
256 | mockRequest.setServletPath("/api"); |
---|
257 | final UUID externalId = UUID.fromString("00000000-0000-0000-0000-000000000021"); |
---|
258 | |
---|
259 | // Number annotationID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.ANNOTATION); |
---|
260 | // if (principalID.equals(dbIntegrityService.getAnnotationOwnerID(annotationID)) || dbIntegrityService.getTypeOfPrincipalAccount(principalID).equals(admin)) { |
---|
261 | // int updatedRows = dbIntegrityService.updateAnnotation(annotation); |
---|
262 | // return new ObjectFactory().createResponseBody(dbIntegrityService.makeAnnotationResponseEnvelope(annotationID)); |
---|
263 | |
---|
264 | |
---|
265 | mockeryRest.checking(new Expectations() { |
---|
266 | { |
---|
267 | oneOf(mockDbDispatcher).setResourcesPaths("/backend/api"); |
---|
268 | |
---|
269 | oneOf(mockDbDispatcher).getPrincipalInternalIDFromRemoteID("twagoo@mpi.nl"); |
---|
270 | will(returnValue(1)); |
---|
271 | |
---|
272 | oneOf(mockDbDispatcher).getResourceInternalIdentifier(externalId, Resource.ANNOTATION); |
---|
273 | will(returnValue(1)); |
---|
274 | |
---|
275 | oneOf(mockDbDispatcher).getAnnotationOwnerID(1); |
---|
276 | will(returnValue(1)); |
---|
277 | |
---|
278 | oneOf(mockDbDispatcher).canDo(Access.WRITE, 1, 1, Resource.ANNOTATION); |
---|
279 | will(returnValue(true)); |
---|
280 | |
---|
281 | oneOf(mockDbDispatcher).updateAnnotation(annotation, "twagoo@mpi.nl"); |
---|
282 | will(returnValue(1)); |
---|
283 | |
---|
284 | oneOf(mockDbDispatcher).makeAnnotationResponseEnvelope(1); |
---|
285 | will(returnValue(mockEnvelope)); |
---|
286 | |
---|
287 | } |
---|
288 | }); |
---|
289 | |
---|
290 | |
---|
291 | |
---|
292 | JAXBElement<ResponseBody> result = annotationResource.updateAnnotation("00000000-0000-0000-0000-000000000021", annotation); |
---|
293 | Annotation newAnnotation = result.getValue().getAnnotation(); |
---|
294 | assertEquals(annotation.getOwnerHref(), newAnnotation.getOwnerHref()); |
---|
295 | assertEquals(annotation.getId(), newAnnotation.getId()); |
---|
296 | assertEquals(annotation.getHref(), newAnnotation.getHref()); |
---|
297 | assertEquals("updated annotation 1", newAnnotation.getHeadline()); |
---|
298 | assertEquals("text/plain", newAnnotation.getBody().getTextBody().getMimeType()); |
---|
299 | assertEquals("some text body l", newAnnotation.getBody().getTextBody().getBody()); |
---|
300 | assertEquals(Access.WRITE, annotation.getPermissions().getPermission().get(1).getLevel()); |
---|
301 | assertEquals(Access.READ, annotation.getPermissions().getPublic()); |
---|
302 | } |
---|
303 | |
---|
304 | @Test |
---|
305 | public void testUpdateAnnotationBody() throws NotInDataBaseException, IOException{ |
---|
306 | System.out.println("test updateAnnotationBody"); |
---|
307 | |
---|
308 | Annotation annotation = (new TestInstances("/backend/api")).getAnnotationOne(); |
---|
309 | |
---|
310 | final AnnotationBody ab = new AnnotationBody(); |
---|
311 | TextBody tb = new TextBody(); |
---|
312 | ab.setTextBody(tb); |
---|
313 | tb.setMimeType("text/plain"); |
---|
314 | tb.setBody("some text body l"); |
---|
315 | annotation.setBody(ab); |
---|
316 | |
---|
317 | final ResponseBody mockEnvelope = new ResponseBody(); |
---|
318 | final ActionList actionList = new ActionList(); |
---|
319 | mockEnvelope.setAnnotation(annotation); |
---|
320 | mockEnvelope.setActionList(actionList); |
---|
321 | |
---|
322 | annotationResource.setHttpServletRequest(mockRequest); |
---|
323 | mockRequest.setRemoteUser("twagoo@mpi.nl"); |
---|
324 | mockRequest.setContextPath("/backend"); |
---|
325 | mockRequest.setServletPath("/api"); |
---|
326 | final UUID externalId = UUID.fromString("00000000-0000-0000-0000-000000000021"); |
---|
327 | |
---|
328 | |
---|
329 | //final Number annotationID = dbIntegrityService.getResourceInternalIdentifier(UUID.fromString(externalIdentifier), Resource.ANNOTATION); |
---|
330 | // (dbIntegrityService.canDo(Access.WRITE, principalID, annotationID)) { |
---|
331 | // int updatedRows = dbIntegrityService.updateAnnotationBody(annotationID, annotationBody); |
---|
332 | // return new ObjectFactory().createResponseBody(dbIntegrityService.makeAnnotationResponseEnvelope(annotationID)); |
---|
333 | |
---|
334 | mockeryRest.checking(new Expectations() { |
---|
335 | { |
---|
336 | |
---|
337 | oneOf(mockDbDispatcher).setResourcesPaths("/backend/api"); |
---|
338 | |
---|
339 | oneOf(mockDbDispatcher).getPrincipalInternalIDFromRemoteID("twagoo@mpi.nl"); |
---|
340 | will(returnValue(1)); |
---|
341 | |
---|
342 | |
---|
343 | oneOf(mockDbDispatcher).getResourceInternalIdentifier(externalId, Resource.ANNOTATION); |
---|
344 | will(returnValue(1)); |
---|
345 | |
---|
346 | oneOf(mockDbDispatcher).canDo(Access.WRITE, 1, 1, Resource.ANNOTATION); |
---|
347 | will(returnValue(true)); |
---|
348 | |
---|
349 | |
---|
350 | oneOf(mockDbDispatcher).updateAnnotationBody(1, ab); |
---|
351 | will(returnValue(1)); |
---|
352 | |
---|
353 | oneOf(mockDbDispatcher).makeAnnotationResponseEnvelope(1); |
---|
354 | will(returnValue(mockEnvelope)); |
---|
355 | |
---|
356 | } |
---|
357 | }); |
---|
358 | |
---|
359 | |
---|
360 | |
---|
361 | JAXBElement<ResponseBody> result = annotationResource.updateAnnotationBody("00000000-0000-0000-0000-000000000021", ab); |
---|
362 | Annotation newAnnotation = result.getValue().getAnnotation(); |
---|
363 | assertEquals("text/plain", newAnnotation.getBody().getTextBody().getMimeType()); |
---|
364 | assertEquals("some text body l", newAnnotation.getBody().getTextBody().getBody()); |
---|
365 | } |
---|
366 | |
---|
367 | @Test |
---|
368 | public void testUpdateAnnotationHeadline() throws NotInDataBaseException, IOException{ |
---|
369 | System.out.println("test updateAnnotationHeadline"); |
---|
370 | |
---|
371 | Annotation annotation = (new TestInstances("/api")).getAnnotationOne(); |
---|
372 | |
---|
373 | final String newHeadline = "new Headline"; |
---|
374 | annotation.setHeadline(newHeadline); |
---|
375 | |
---|
376 | final ResponseBody mockEnvelope = new ResponseBody(); |
---|
377 | final ActionList actionList = new ActionList(); |
---|
378 | mockEnvelope.setAnnotation(annotation); |
---|
379 | mockEnvelope.setActionList(actionList); |
---|
380 | |
---|
381 | annotationResource.setHttpServletRequest(mockRequest); |
---|
382 | mockRequest.setRemoteUser("twagoo@mpi.nl"); |
---|
383 | mockRequest.setContextPath("/backend"); |
---|
384 | mockRequest.setServletPath("/api"); |
---|
385 | final UUID externalId = UUID.fromString("00000000-0000-0000-0000-000000000021"); |
---|
386 | |
---|
387 | |
---|
388 | mockeryRest.checking(new Expectations() { |
---|
389 | { |
---|
390 | oneOf(mockDbDispatcher).setResourcesPaths("/backend/api"); |
---|
391 | |
---|
392 | oneOf(mockDbDispatcher).getPrincipalInternalIDFromRemoteID("twagoo@mpi.nl"); |
---|
393 | will(returnValue(1)); |
---|
394 | |
---|
395 | |
---|
396 | oneOf(mockDbDispatcher).getResourceInternalIdentifier(externalId, Resource.ANNOTATION); |
---|
397 | will(returnValue(1)); |
---|
398 | |
---|
399 | oneOf(mockDbDispatcher).canDo(Access.WRITE, 1, 1, Resource.ANNOTATION); |
---|
400 | will(returnValue(true)); |
---|
401 | |
---|
402 | |
---|
403 | oneOf(mockDbDispatcher).updateAnnotationHeadline(1, newHeadline); |
---|
404 | will(returnValue(1)); |
---|
405 | |
---|
406 | oneOf(mockDbDispatcher).makeAnnotationResponseEnvelope(1); |
---|
407 | will(returnValue(mockEnvelope)); |
---|
408 | |
---|
409 | } |
---|
410 | }); |
---|
411 | |
---|
412 | |
---|
413 | JAXBElement<ResponseBody> result = annotationResource.updateAnnotationHeadline("00000000-0000-0000-0000-000000000021", newHeadline); |
---|
414 | Annotation newAnnotation = result.getValue().getAnnotation(); |
---|
415 | assertEquals("new Headline", newAnnotation.getHeadline()); |
---|
416 | } |
---|
417 | |
---|
418 | |
---|
419 | |
---|
420 | } |
---|