source: DASISH/t5.6/backend/annotator-backend/branches/Jersey-2.12-adaptation/annotator-backend/src/test/java/eu/dasish/annotation/backend/rest/AnnotationsTest.java @ 5745

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

bug in AnnotationTest?: httpSevletRequest = null;

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