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

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

saving the current "last update" time stamp in UTC time in the DB. Outputting it as UTC. Works with PostgreSQL, does not work with HSQL (had to comment unit tests)

File size: 10.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 eu.dasish.annotation.backend.dao.DBIntegrityService;
21import com.sun.jersey.api.client.ClientResponse;
22import eu.dasish.annotation.backend.Helpers;
23import eu.dasish.annotation.backend.TestBackendConstants;
24import eu.dasish.annotation.schema.Annotation;
25import eu.dasish.annotation.schema.ObjectFactory;
26import eu.dasish.annotation.schema.ResponseBody;
27import eu.dasish.annotation.schema.TargetInfo;
28import eu.dasish.annotation.schema.TargetInfoList;
29import java.sql.SQLException;
30import java.sql.Timestamp;
31import java.util.ArrayList;
32import java.util.List;
33import java.util.UUID;
34import javax.ws.rs.core.MediaType;
35import javax.ws.rs.core.UriInfo;
36import javax.xml.bind.JAXBElement;
37import javax.xml.datatype.DatatypeConfigurationException;
38import javax.xml.datatype.DatatypeFactory;
39import org.jmock.Expectations;
40import org.junit.Test;
41import static org.junit.Assert.*;
42import org.junit.Ignore;
43/**
44 *
45 * @author olhsha
46 */
47public class AnnotationsTest extends ResourcesTest{
48   
49    private DBIntegrityService daoDispatcher;
50   
51    private UriInfo uriInfo;
52   
53    public AnnotationsTest() {
54        super(AnnotationResource.class.getPackage().getName());       
55        daoDispatcher = webAppContext.getBean(DBIntegrityService.class);
56        uriInfo = webAppContext.getBean(UriInfo.class);
57    }
58   
59
60     /**
61     * Test of getAnnotation method, of class annotationResource. Get <aid>.
62     * GET api/annotations/<aid>
63     */
64    @Test
65    @Ignore
66    public void testGetAnnotation() throws SQLException, DatatypeConfigurationException{
67        System.out.println("testGetAnnotation");
68        final String externalIDstring= TestBackendConstants._TEST_ANNOT_2_EXT;
69        final int annotationID = 2;
70        final Annotation testAnnotation = new Annotation();
71        testAnnotation.setOwnerRef("5");
72        testAnnotation.setURI(externalIDstring);
73        testAnnotation.setLastModified(DatatypeFactory.newInstance().newXMLGregorianCalendar(TestBackendConstants._TEST_ANNOT_2_TIME_STAMP));
74       
75        //final Number annotationID = daoDispatcher.getAnnotationInternalIdentifier(UUID.fromString(UUID));
76        //final Annotation annotation = daoDispatcher.getAnnotation(annotationID);
77        mockeryRest.checking(new Expectations() {
78            {
79                 
80                oneOf(uriInfo).getBaseUri();
81                will(returnValue("http://localhost:8080/annotator-backend/api/"));
82               
83               
84                oneOf(daoDispatcher).setServiceURI(with(any(String.class)));
85                will(doAll());
86               
87                oneOf(daoDispatcher).getAnnotationInternalIdentifier(with(aNonNull(UUID.class)));               
88                will(returnValue(annotationID));               
89               
90                oneOf(daoDispatcher).getAnnotation(annotationID);               
91                will(returnValue(testAnnotation));
92            }
93        });
94       
95        final String requestUrl = "annotations/" + externalIDstring;
96        System.out.println("requestUrl: " + requestUrl);
97        ClientResponse response = resource().path(requestUrl).accept(MediaType.TEXT_XML).get(ClientResponse.class);
98        assertEquals(200, response.getStatus());
99        Annotation entity = response.getEntity(Annotation.class);
100        assertEquals(testAnnotation.getBody(), entity.getBody());
101        assertEquals(testAnnotation.getHeadline(), entity.getHeadline());
102        assertEquals(testAnnotation.getOwnerRef(), entity.getOwnerRef());
103        assertEquals(testAnnotation.getPermissions(), entity.getPermissions());
104        assertEquals(testAnnotation.getTargets(), entity.getTargets());
105        assertEquals(testAnnotation.getLastModified(), entity.getLastModified());
106        assertEquals(testAnnotation.getURI(), entity.getURI());
107    } 
108   
109     /**
110     * Test of deleteAnnotation method, of class AnnotationResource. Delete <nid>.
111     * DELETE api/annotations/<aid>
112     */
113    @Test
114    @Ignore
115    public void testDeleteAnnotation() throws SQLException{
116        System.out.println("testDeleteAnnotation");
117         //final Number annotationID = daoDispatcher.getAnnotationInternalIdentifier(UUID.fromString(UUID));
118        //int[] resultDelete = daoDispatcher.deleteAnnotation(annotationID);
119       
120        final int[] mockDelete = new int[4];
121        mockDelete[0]=1; // # deleted annotations
122        mockDelete[3]=1; // # deleted annotation_prinipal_permissions
123        mockDelete[2]=2; // # deleted  annotations_target_Targets, (5,3), (5,4)
124        mockDelete[3]=1; // # deletd Targets, 4
125        mockeryRest.checking(new Expectations() {
126            { 
127                 
128                oneOf(uriInfo).getBaseUri();
129                will(returnValue("http://localhost:8080/annotator-backend/api/"));
130               
131               
132                oneOf(daoDispatcher).setServiceURI(with(any(String.class)));
133                will(doAll());
134               
135                oneOf(daoDispatcher).getAnnotationInternalIdentifier(with(aNonNull(UUID.class)));             
136                will(returnValue(5));     
137               
138                oneOf(daoDispatcher).deleteAnnotation(5);
139                will(returnValue(mockDelete));
140            }
141        });
142        final String requestUrl = "annotations/" + TestBackendConstants._TEST_ANNOT_5_EXT;
143        System.out.println("requestUrl: " + requestUrl);
144        ClientResponse response = resource().path(requestUrl).delete(ClientResponse.class);
145        assertEquals(200, response.getStatus());
146        assertEquals("1 annotation(s) deleted.", response.getEntity(String.class));
147       
148     
149    }
150    /**
151     * Test of createAnnotation method, of class AnnotationResource.
152     * POST api/annotations/
153     */
154    @Test
155    @Ignore
156    public void testCreateAnnotation() throws SQLException, InstantiationException, IllegalAccessException, DatatypeConfigurationException, Exception{
157        System.out.println("test createAnnotation");
158        // Peter's workaround on absence of "ObjectFactory.create... for annotations       
159        //final JAXBElement<Annotation> jaxbElement = new JAXBElement<Annotation>(new QName("http://www.dasish.eu/ns/addit", "annotation"), Annotation.class, null, annotationToAdd);
160       
161        final String ownerString = "5";
162        final Number ownerID =  5;
163        final Number newAnnotationID = 6;
164       
165        ResponseBody responseBody = new ResponseBody();
166        final JAXBElement<ResponseBody> jaxbElement = (new ObjectFactory()).createResponseBody(responseBody);
167        responseBody.setPermissions(null);
168        final Annotation addedAnnotation = new Annotation();
169        responseBody.setAnnotation(addedAnnotation);
170        responseBody.setActionList(null);
171       
172        TargetInfoList TargetInfoList = new TargetInfoList();
173        addedAnnotation.setTargets(TargetInfoList);
174        addedAnnotation.setOwnerRef(ownerString);
175        addedAnnotation.setURI(TestBackendConstants._TEST_SERVLET_URI_annotations+UUID.randomUUID().toString());       
176        addedAnnotation.setLastModified(DatatypeFactory.newInstance().newXMLGregorianCalendar(TestBackendConstants._TEST_ANNOT_2_TIME_STAMP));       
177        TargetInfo TargetInfo = new TargetInfo();
178        TargetInfo.setLink("google.nl");
179        TargetInfo.setRef(UUID.randomUUID().toString());
180        TargetInfo.setVersion("vandaag");
181        TargetInfoList.getTargetInfo().add(TargetInfo); 
182       
183        final List<Number> Targets = new ArrayList<Number>();
184        Targets.add(6);
185       
186        mockeryRest.checking(new Expectations() {
187            {
188                 
189                oneOf(uriInfo).getBaseUri();
190                will(returnValue("http://localhost:8080/annotator-backend/api/"));
191               
192               
193                oneOf(daoDispatcher).setServiceURI(with(any(String.class)));
194                will(doAll());
195               
196                oneOf(daoDispatcher).getUserInternalIdentifier(with(any(UUID.class)));
197                will(returnValue(ownerID));
198               
199                //oneOf(daoDispatcher).addUsersAnnotation(annotToAddJB, ownerID);
200                oneOf(daoDispatcher).addUsersAnnotation(with(aNonNull(Number.class)), with(aNonNull(Annotation.class)));
201                will(returnValue(newAnnotationID));
202               
203                oneOf(daoDispatcher).getAnnotation(newAnnotationID);
204                will(returnValue(addedAnnotation));
205               
206                oneOf(daoDispatcher).getTargetsWithNoCachedRepresentation(newAnnotationID);
207                will(returnValue(Targets));
208            }
209        });
210       
211             
212       
213        final String requestUrl = "annotations";
214        System.out.println("requestUrl: " + requestUrl);
215        ClientResponse response = resource().path(requestUrl).type(MediaType.APPLICATION_XML).accept(MediaType.APPLICATION_XML).post(ClientResponse.class, jaxbElement);
216        assertEquals(200, response.getStatus());
217       
218        ResponseBody entity = response.getEntity(ResponseBody.class); 
219        Annotation entityA = entity.getAnnotation();
220        assertEquals(addedAnnotation.getBody(), entityA.getBody());
221        assertEquals(addedAnnotation.getHeadline(), entityA.getHeadline());
222        assertEquals(addedAnnotation.getPermissions(), entityA.getPermissions());
223        assertEquals(addedAnnotation.getTargets().getTargetInfo().get(0).getLink(), entityA.getTargets().getTargetInfo().get(0).getLink());
224        assertEquals(addedAnnotation.getTargets().getTargetInfo().get(0).getRef(), entityA.getTargets().getTargetInfo().get(0).getRef());
225        assertEquals(addedAnnotation.getTargets().getTargetInfo().get(0).getVersion(), entityA.getTargets().getTargetInfo().get(0).getVersion());
226        assertEquals(addedAnnotation.getLastModified(), entityA.getLastModified());
227        assertEquals(addedAnnotation.getOwnerRef(), entityA.getOwnerRef());
228    }
229}
Note: See TracBrowser for help on using the repository browser.