source: DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/rest/AnnotationsTest.java @ 5774

Last change on this file since 5774 was 5774, checked in by olhsha@mpi.nl, 10 years ago

updating annotations: if the current user is not owner then the permission part is ignored

File size: 17.5 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.rest;
19
20import com.sun.jersey.api.client.ClientResponse;
21import com.sun.jersey.api.client.WebResource;
22import com.sun.jersey.api.client.WebResource.Builder;
23import com.sun.jersey.core.util.Base64;
24import com.sun.jersey.spi.spring.container.servlet.SpringServlet;
25import com.sun.jersey.test.framework.AppDescriptor;
26import com.sun.jersey.test.framework.JerseyTest;
27import com.sun.jersey.test.framework.WebAppDescriptor;
28import eu.dasish.annotation.backend.Helpers;
29import eu.dasish.annotation.backend.NotInDataBaseException;
30import eu.dasish.annotation.backend.TestInstances;
31import eu.dasish.annotation.backend.dao.impl.JdbcResourceDaoTest;
32import eu.dasish.annotation.schema.Access;
33import eu.dasish.annotation.schema.Annotation;
34import eu.dasish.annotation.schema.AnnotationBody;
35import eu.dasish.annotation.schema.AnnotationBody.TextBody;
36import eu.dasish.annotation.schema.AnnotationBody.XmlBody;
37import eu.dasish.annotation.schema.ObjectFactory;
38import eu.dasish.annotation.schema.ResponseBody;
39import java.io.FileNotFoundException;
40import java.io.IOException;
41import java.net.URISyntaxException;
42import javax.ws.rs.core.HttpHeaders;
43import javax.ws.rs.core.MediaType;
44import javax.xml.bind.JAXBElement;
45import javax.xml.parsers.ParserConfigurationException;
46import org.junit.Test;
47import static org.junit.Assert.*;
48import org.junit.Before;
49import org.junit.Ignore;
50import org.junit.runner.RunWith;
51import org.springframework.beans.factory.annotation.Autowired;
52import org.springframework.dao.DataAccessException;
53import org.springframework.jdbc.core.JdbcTemplate;
54import org.springframework.test.context.ContextConfiguration;
55import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
56import org.springframework.web.context.ContextLoaderListener;
57import org.springframework.web.context.request.RequestContextListener;
58import org.w3c.dom.Element;
59import org.xml.sax.SAXException;
60
61/**
62 *
63 * @author olhsha
64 */
65@RunWith(value = SpringJUnit4ClassRunner.class)
66@ContextConfiguration({"/spring-test-config/dataSource.xml"})
67public 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(0, entityA.getPermissions().getPermission().size());
208        assertEquals(Access.WRITE, entityA.getPermissions().getPublic());
209        assertEquals(_relativePath + "/principals/00000000-0000-0000-0000-000000000113", entityA.getOwnerHref());
210        assertEquals("http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia#de_Opdracht", entityA.getTargets().getTargetInfo().get(0).getLink());
211        assertEquals(_relativePath+ "/targets/00000000-0000-0000-0000-000000000031", entityA.getTargets().getTargetInfo().get(0).getHref());
212        assertEquals("version 1.0", entityA.getTargets().getTargetInfo().get(0).getVersion());
213       
214    }
215   
216   
217   
218   
219    @Test
220    public void testUpdateAnnotation() throws NotInDataBaseException, IOException{
221       
222         
223        // Authentication 
224        Builder responseBuilderAu = getAuthenticatedResource(resource().path("authentication/login")).accept(MediaType.TEXT_HTML);       
225        ClientResponse responseAu = responseBuilderAu.get(ClientResponse.class); 
226        assertEquals(200, responseAu.getStatus());
227       
228       
229        // updating annotation
230        System.out.println("test updateAnnotation");
231        System.out.println("PUT "+resource().getURI().toString()+"annotations/00000000-0000-0000-0000-000000000021");
232               
233        Annotation annotation = (new TestInstances(_relativePath)).getAnnotationOne();
234        annotation.getPermissions().setPublic(Access.READ);
235        annotation.setHeadline("updated annotation 1");
236        annotation.getPermissions().getPermission().get(1).setLevel(Access.WRITE);
237        AnnotationBody ab = new AnnotationBody();
238        TextBody tb = new TextBody();
239        ab.setTextBody(tb);
240        tb.setMimeType("text/plain");
241        tb.setBody("some text body l");
242        annotation.setBody(ab);
243        final JAXBElement<Annotation> jaxbElement = (new ObjectFactory()).createAnnotation(annotation);
244       
245        Builder responseBuilder = getAuthenticatedResource(resource().path("annotations/00000000-0000-0000-0000-000000000021")).type(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML);       
246        ClientResponse response = responseBuilder.put(ClientResponse.class, jaxbElement);
247        assertEquals(200, response.getStatus());
248       
249        ResponseBody entity = response.getEntity(ResponseBody.class);       
250        Annotation entityA = entity.getAnnotation();
251        assertEquals("some text body l", entityA.getBody().getTextBody().getBody());
252        assertEquals("text/plain", entityA.getBody().getTextBody().getMimeType());
253        assertEquals("updated annotation 1", entityA.getHeadline());
254        assertEquals(3, entityA.getPermissions().getPermission().size());
255//        assertEquals(Access.READ, entityA.getPermissions().getPublic());
256//        assertEquals(_relativePath + "/principals/00000000-0000-0000-0000-000000000111", entityA.getOwnerHref());
257//        assertEquals("http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia#de_Opdracht", entityA.getTargets().getTargetInfo().get(0).getLink());
258//        assertEquals(_relativePath+ "/targets/00000000-0000-0000-0000-000000000031", entityA.getTargets().getTargetInfo().get(0).getHref());
259//       
260    }
261   
262    @Test
263    public void testUpdateAnnotationBody() throws NotInDataBaseException, IOException, ParserConfigurationException, SAXException{
264       
265         
266        // Authentication 
267        Builder responseBuilderAu = getAuthenticatedResource(resource().path("authentication/login")).accept(MediaType.TEXT_HTML);       
268        ClientResponse responseAu = responseBuilderAu.get(ClientResponse.class); 
269        assertEquals(200, responseAu.getStatus());
270       
271       
272        // updating annotation
273        System.out.println("test updateAnnotation");
274        System.out.println("PUT "+resource().getURI().toString()+"annotations/00000000-0000-0000-0000-000000000021/body");
275        AnnotationBody ab = new AnnotationBody();
276        XmlBody xmlb = new XmlBody();
277        ab.setXmlBody(xmlb);
278        xmlb.setMimeType("text/xml");
279        String testXml = "<span style=\"background-color:rgb(0,0,153);color:rgb(255,255,255);border: thick solid rgb(0, 0, 153);\">test</span>";
280        Element el = Helpers.stringToElement(testXml);
281        xmlb.setAny(el);
282        final JAXBElement<AnnotationBody> jaxbElement = (new ObjectFactory()).createAnnotationBody(ab);
283       
284       
285        Builder responseBuilder = getAuthenticatedResource(resource().path("annotations/00000000-0000-0000-0000-000000000021/body")).type(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML);       
286        ClientResponse response = responseBuilder.put(ClientResponse.class, jaxbElement);
287        assertEquals(200, response.getStatus());
288       
289        ResponseBody entity = response.getEntity(ResponseBody.class);       
290        Annotation entityA = entity.getAnnotation();
291        assertEquals("test", entityA.getBody().getXmlBody().getAny().getTextContent());
292        assertEquals("span", entityA.getBody().getXmlBody().getAny().getNodeName());
293        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"));
294       
295        assertEquals("text/xml", entityA.getBody().getXmlBody().getMimeType());
296        assertEquals("Sagrada Famiglia", entityA.getHeadline());
297        assertEquals(3, entityA.getPermissions().getPermission().size());
298        assertEquals(Access.WRITE , entityA.getPermissions().getPublic());
299       
300     
301        assertEquals(_relativePath + "/principals/00000000-0000-0000-0000-000000000111", entityA.getOwnerHref());
302        assertEquals("http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia#de_Opdracht", entityA.getTargets().getTargetInfo().get(0).getLink());
303        assertEquals(_relativePath+ "/targets/00000000-0000-0000-0000-000000000031", entityA.getTargets().getTargetInfo().get(0).getHref());
304       
305    }
306   
307    @Test
308    public void testUpdateAnnotationHeadline() throws IOException{
309       
310         
311        // Authentication 
312        Builder responseBuilderAu = getAuthenticatedResource(resource().path("authentication/login")).accept(MediaType.TEXT_HTML);       
313        ClientResponse responseAu = responseBuilderAu.get(ClientResponse.class); 
314        assertEquals(200, responseAu.getStatus());
315       
316       
317        // updating annotation
318        System.out.println("test updateAnnotationHeadline");
319        System.out.println("PUT "+resource().getURI().toString()+"annotations/00000000-0000-0000-0000-000000000021/headline");
320        String newHeadline = "new Headline";
321        Builder responseBuilder = getAuthenticatedResource(resource().path("annotations/00000000-0000-0000-0000-000000000021/headline")).type(MediaType.TEXT_PLAIN).accept(MediaType.APPLICATION_XML);       
322        ClientResponse response = responseBuilder.put(ClientResponse.class, newHeadline);
323        assertEquals(200, response.getStatus());
324       
325        ResponseBody entity = response.getEntity(ResponseBody.class);       
326        Annotation entityA = entity.getAnnotation();
327        assertEquals("new Headline", entityA.getHeadline());
328    }
329   
330    protected Builder getAuthenticatedResource(WebResource resource) {
331        return resource.header(HttpHeaders.AUTHORIZATION, "Basic "  + new String(Base64.encode(DummyPrincipal.DUMMY_PRINCIPAL.getName()+":olhapassword")));
332    }
333}
Note: See TracBrowser for help on using the repository browser.