source: DASISH/t5.6/backend/annotator-backend/tags/git-copy-censored/annotator-backend/src/test/java/eu/dasish/annotation/backend/rest/AnnotationsTest.java @ 6063

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

replacing olhsha@mpi.nl with alice@mail.domain in the code

File size: 18.0 KB
RevLine 
[4603]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;
[4903]28import eu.dasish.annotation.backend.Helpers;
29import eu.dasish.annotation.backend.NotInDataBaseException;
[4603]30import eu.dasish.annotation.backend.TestInstances;
31import eu.dasish.annotation.backend.dao.impl.JdbcResourceDaoTest;
[4865]32import eu.dasish.annotation.schema.Access;
[4603]33import eu.dasish.annotation.schema.Annotation;
34import eu.dasish.annotation.schema.AnnotationBody;
35import eu.dasish.annotation.schema.AnnotationBody.TextBody;
[4903]36import eu.dasish.annotation.schema.AnnotationBody.XmlBody;
[4603]37import eu.dasish.annotation.schema.ObjectFactory;
38import eu.dasish.annotation.schema.ResponseBody;
39import java.io.FileNotFoundException;
[4903]40import java.io.IOException;
[4603]41import java.net.URISyntaxException;
42import javax.ws.rs.core.HttpHeaders;
43import javax.ws.rs.core.MediaType;
44import javax.xml.bind.JAXBElement;
[4903]45import javax.xml.parsers.ParserConfigurationException;
[4603]46import org.junit.Test;
47import static org.junit.Assert.*;
48import org.junit.Before;
[5174]49import org.junit.Ignore;
[4603]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;
[4903]58import org.w3c.dom.Element;
59import org.xml.sax.SAXException;
[4603]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   
[5385]72    String _relativePath = "/backend/api";
[4603]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)
[5385]83                .contextPath("/backend").servletPath("/api")
[4603]84                .build();
85       
86    }
87   
88    private String getApplicationContextFile() {
89        // sorry for the duplication, but JerseyTest is not aware of
90        // @ContextConfiguration
[5145]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";
[4603]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());
[5385]104       
[4603]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
[4903]117    public void testGetAnnotation() throws NotInDataBaseException, IOException {
[4603]118       
119        // Authentication 
[4956]120        Builder responseBuilderAu = getAuthenticatedResource(resource().path("authentication/login")).accept(MediaType.TEXT_HTML);       
[4603]121        ClientResponse responseAu = responseBuilderAu.get(ClientResponse.class); 
122        assertEquals(200, responseAu.getStatus());
123       
124        // Getting annotation
125        System.out.println("testGetAnnotation");
[5385]126        final Annotation testAnnotation = (new TestInstances(_relativePath)).getAnnotationOne();       
[4603]127       
[5385]128        Builder responseBuilder = getAuthenticatedResource(resource().path("annotations/00000000-0000-0000-0000-000000000021")).accept(MediaType.TEXT_XML);       
[4603]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());
[5385]136        assertEquals(testAnnotation.getOwnerHref(), entity.getOwnerHref());
[4903]137        assertEquals(Access.WRITE, entity.getPermissions().getPublic());
[4800]138        assertEquals(3, entity.getPermissions().getPermission().size());
139        assertEquals("write", entity.getPermissions().getPermission().get(0).getLevel().value());
[5385]140        assertEquals(_relativePath+"/principals/00000000-0000-0000-0000-000000000112", entity.getPermissions().getPermission().get(0).getPrincipalHref()); 
[4800]141        assertEquals("read", entity.getPermissions().getPermission().get(1).getLevel().value()); 
[5385]142        assertEquals(_relativePath+"/principals/00000000-0000-0000-0000-000000000113", entity.getPermissions().getPermission().get(1).getPrincipalHref()); 
[4800]143        assertEquals("read", entity.getPermissions().getPermission().get(1).getLevel().value()); 
[5385]144        assertEquals(_relativePath+"/principals/00000000-0000-0000-0000-000000000221", entity.getPermissions().getPermission().get(2).getPrincipalHref()); 
[4603]145        assertEquals(2, entity.getTargets().getTargetInfo().size());
[5385]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());
[4603]148        assertEquals(testAnnotation.getLastModified(), entity.getLastModified());
[5385]149        assertEquals(_relativePath+"/annotations/00000000-0000-0000-0000-000000000021", entity.getHref());
[4603]150    }
151
152    /**
153     * Test of deleteAnnotation method, of class AnnotationResource. Delete
154     * <nid>. DELETE api/annotations/<aid>
155     */
156    @Test
[4903]157    public void testDeleteAnnotation() {
[4603]158       
159         
160        // Authentication 
[4956]161        Builder responseBuilderAu = getAuthenticatedResource(resource().path("authentication/login")).accept(MediaType.TEXT_HTML);       
[4603]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());
[5086]174        assertEquals("1 annotation(s) is(are) deleted.", response.getEntity(String.class));
[4603]175    }
176
177    /**
178     * Test of createAnnotation method, of class AnnotationResource. POST
179     * api/annotations/
180     */
181    @Test
[4903]182    public void testCreateAnnotation() throws NotInDataBaseException, IOException{
[4603]183       
184         
185        // Authentication 
[4956]186        Builder responseBuilderAu = getAuthenticatedResource(resource().path("authentication/login")).accept(MediaType.TEXT_HTML);       
[4603]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/");
[5385]194       
195        final Annotation annotationToAdd = (new TestInstances(_relativePath)).getAnnotationToAdd();
[4603]196        final JAXBElement<Annotation> jaxbElement = (new ObjectFactory()).createAnnotation(annotationToAdd);
[4903]197       
[4603]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();
[4903]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());
[5838]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());
[4865]210        assertEquals(Access.WRITE, entityA.getPermissions().getPublic());
[5385]211        assertEquals(_relativePath + "/principals/00000000-0000-0000-0000-000000000113", entityA.getOwnerHref());
[4903]212        assertEquals("http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia#de_Opdracht", entityA.getTargets().getTargetInfo().get(0).getLink());
[5385]213        assertEquals(_relativePath+ "/targets/00000000-0000-0000-0000-000000000031", entityA.getTargets().getTargetInfo().get(0).getHref());
[4903]214        assertEquals("version 1.0", entityA.getTargets().getTargetInfo().get(0).getVersion());
215       
[4603]216    }
217   
218   
219   
[4903]220   
221    @Test
222    public void testUpdateAnnotation() throws NotInDataBaseException, IOException{
223       
224         
225        // Authentication 
[4956]226        Builder responseBuilderAu = getAuthenticatedResource(resource().path("authentication/login")).accept(MediaType.TEXT_HTML);       
[4903]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");
[5385]234               
235        Annotation annotation = (new TestInstances(_relativePath)).getAnnotationOne();
[4903]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());
[5774]256        assertEquals(3, entityA.getPermissions().getPermission().size());
[5850]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());
[5774]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//       
[4903]265    }
266   
267    @Test
268    public void testUpdateAnnotationBody() throws NotInDataBaseException, IOException, ParserConfigurationException, SAXException{
269       
270         
271        // Authentication 
[4956]272        Builder responseBuilderAu = getAuthenticatedResource(resource().path("authentication/login")).accept(MediaType.TEXT_HTML);       
[4903]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());
[5385]304       
305     
306        assertEquals(_relativePath + "/principals/00000000-0000-0000-0000-000000000111", entityA.getOwnerHref());
[4903]307        assertEquals("http://nl.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia#de_Opdracht", entityA.getTargets().getTargetInfo().get(0).getLink());
[5385]308        assertEquals(_relativePath+ "/targets/00000000-0000-0000-0000-000000000031", entityA.getTargets().getTargetInfo().get(0).getHref());
[4903]309       
310    }
[5086]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    }
[4903]334   
[4603]335    protected Builder getAuthenticatedResource(WebResource resource) {
[6063]336        return resource.header(HttpHeaders.AUTHORIZATION, "Basic "  + new String(Base64.encode(DummyPrincipal.DUMMY_PRINCIPAL.getName()+":alicepassword")));
[4603]337    }
338}
Note: See TracBrowser for help on using the repository browser.