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

Last change on this file since 4671 was 4671, checked in by olhsha, 10 years ago

debugging and testing

File size: 12.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.TestInstances;
29import eu.dasish.annotation.backend.dao.impl.JdbcResourceDaoTest;
30import eu.dasish.annotation.schema.Annotation;
31import eu.dasish.annotation.schema.AnnotationBody;
32import eu.dasish.annotation.schema.AnnotationBody.TextBody;
33import eu.dasish.annotation.schema.ObjectFactory;
34import eu.dasish.annotation.schema.ResponseBody;
35import eu.dasish.annotation.schema.TargetInfo;
36import eu.dasish.annotation.schema.TargetInfoList;
37import java.io.FileNotFoundException;
38import java.net.URISyntaxException;
39import java.sql.SQLException;
40import java.util.UUID;
41import javax.ws.rs.core.HttpHeaders;
42import javax.ws.rs.core.MediaType;
43import javax.xml.bind.JAXBElement;
44import javax.xml.datatype.DatatypeConfigurationException;
45import javax.xml.datatype.DatatypeFactory;
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;
58
59/**
60 *
61 * @author olhsha
62 */
63@RunWith(value = SpringJUnit4ClassRunner.class)
64@ContextConfiguration({"/spring-test-config/dataSource.xml"})
65public 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}
Note: See TracBrowser for help on using the repository browser.