source: DASISH/t5.6/backend/annotator-backend/trunk/annotator-backend/src/test/java/eu/dasish/annotation/backend/rest/AnnotationResourceTest.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 eu.dasish.annotation.backend.Helpers;
22import eu.dasish.annotation.backend.MockObjectsFactoryRest;
23import eu.dasish.annotation.backend.TestBackendConstants;
24import eu.dasish.annotation.backend.TestInstances;
25import eu.dasish.annotation.schema.AnnotationBody;
26import eu.dasish.annotation.schema.AnnotationBody.TextBody;
27import eu.dasish.annotation.schema.TargetInfo;
28import eu.dasish.annotation.schema.TargetInfoList;
29import eu.dasish.annotation.schema.Annotation;
30import eu.dasish.annotation.schema.ObjectFactory;
31import eu.dasish.annotation.schema.ResponseBody;
32import eu.dasish.annotation.schema.AnnotationActionName;
33import eu.dasish.annotation.schema.Permission;
34import java.io.IOException;
35import java.sql.SQLException;
36import javax.xml.bind.JAXBElement;
37import org.jmock.Expectations;
38import org.jmock.Mockery;
39import org.junit.Test;
40import static org.junit.Assert.*;
41import org.junit.runner.RunWith;
42import org.springframework.beans.factory.annotation.Autowired;
43import org.springframework.test.context.ContextConfiguration;
44import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
45import java.lang.InstantiationException;
46import java.net.URI;
47import java.sql.Timestamp;
48import java.util.ArrayList;
49import java.util.List;
50import java.util.UUID;
51import javax.servlet.ServletException;
52import javax.ws.rs.core.UriInfo;
53import javax.xml.bind.JAXBException;
54import javax.xml.datatype.DatatypeConfigurationException;
55import javax.xml.datatype.DatatypeFactory;
56import org.springframework.mock.web.MockHttpServletRequest;
57
58/**
59 *
60 * @author olhsha
61 */
62
63@RunWith(value = SpringJUnit4ClassRunner.class)
64@ContextConfiguration(locations = { "/spring-test-config/mockeryRest.xml", "/spring-test-config/mockDBIntegrityService.xml", 
65    "/spring-test-config/mockUriInfo.xml",
66    "/spring-config/jaxbMarshallerFactory.xml"})
67public class AnnotationResourceTest {
68   
69    @Autowired
70    private Mockery mockeryRest;     
71    @Autowired
72    private MockObjectsFactoryRest mockObjectFactory;
73    @Autowired
74    private DBIntegrityService mockDbIntegrityService;
75    @Autowired
76    private AnnotationResource annotationResource;
77    @Autowired
78    UriInfo mockUriInfo;
79   
80    private MockHttpServletRequest mockRequest;
81   
82    public AnnotationResourceTest() { 
83        mockRequest = new MockHttpServletRequest();
84        mockRequest.setRemoteUser("twan");
85       
86    }
87       
88   
89    /**
90     * Test of getAnnotation method, of class AnnotationResource.
91     */
92    @Test
93    public void testGetAnnotation() throws SQLException, JAXBException, Exception {
94        System.out.println("getAnnotation");
95        final String externalIDstring= TestBackendConstants._TEST_ANNOT_2_EXT;
96        final Annotation expectedAnnotation = (new TestInstances()).getAnnotationOne();       
97        annotationResource.setHttpServletRequest(mockRequest);       
98        annotationResource.setUriInfo(mockUriInfo);       
99       
100     
101        mockeryRest.checking(new Expectations() {
102            {
103               
104               oneOf(mockUriInfo).getBaseUri();
105               will(returnValue(URI.create("http://localhost:8080/annotator-backend/api/"))); 
106               
107               
108                oneOf(mockDbIntegrityService).setServiceURI(with(any(String.class)));
109                will(doAll());
110               
111                oneOf(mockDbIntegrityService).getUserInternalIDFromRemoteID("twan");
112                will(returnValue(3)); 
113               
114                oneOf(mockDbIntegrityService).getAnnotationInternalIdentifier(with(any(UUID.class)));               
115                will(returnValue(2));   
116               
117                oneOf(mockDbIntegrityService).getPermission(2, 3);
118                will(returnValue(Permission.OWNER)); 
119               
120                oneOf(mockDbIntegrityService).getAnnotation(2);               
121                will(returnValue(expectedAnnotation));
122            }
123        });
124       
125       
126        JAXBElement<Annotation> result = annotationResource.getAnnotation(externalIDstring);
127        assertEquals(expectedAnnotation, result.getValue());
128    }
129   
130    /**
131     * Test of deleteAnnotation method, of class AnnotationResource.
132     */
133    @Test
134    public void testDeleteAnnotation() throws SQLException, IOException {
135        System.out.println("deleteAnnotation");
136       
137        final int[] mockDelete = new int[4];
138        mockDelete[0]=1; // # deleted annotations
139        mockDelete[3]=1; // # deleted annotation_prinipal_permissions
140        mockDelete[2]=2; // # deleted  annotations_target_Targets, (5,3), (5,4)
141        mockDelete[3]=1; // # deletd Targets, 4
142       
143        annotationResource.setHttpServletRequest(mockRequest);
144        annotationResource.setUriInfo(mockUriInfo); 
145       
146        mockeryRest.checking(new Expectations() {
147            { 
148               oneOf(mockUriInfo).getBaseUri();
149               will(returnValue(URI.create("http://localhost:8080/annotator-backend/api/"))); 
150               
151             
152                oneOf(mockDbIntegrityService).getUserInternalIDFromRemoteID("twan");
153                will(returnValue(3)); 
154               
155                oneOf(mockDbIntegrityService).setServiceURI(with(any(String.class)));
156                will(doAll());
157               
158                oneOf(mockDbIntegrityService).getAnnotationInternalIdentifier(with(aNonNull(UUID.class)));             
159                will(returnValue(5));   
160               
161                oneOf(mockDbIntegrityService).getPermission(5, 3);
162                will(returnValue(Permission.OWNER)); 
163               
164                oneOf(mockDbIntegrityService).deleteAnnotation(5);
165                will(returnValue(mockDelete));
166            }
167        });
168       
169       
170        String result = annotationResource.deleteAnnotation(TestBackendConstants._TEST_ANNOT_5_EXT);
171        assertEquals("1 annotation(s) deleted.", result);
172    }
173   
174    /**
175     * Test of createAnnotation method, of class AnnotationResource.
176     */
177    @Test
178    public void testCreateAnnotation() throws SQLException, InstantiationException, IllegalAccessException, ServletException, DatatypeConfigurationException, Exception {
179        System.out.println("test createAnnotation");
180       
181        final Annotation annotationToAdd = new Annotation();
182        final Number newAnnotationID = 6;
183       
184        TargetInfoList TargetInfoList = new TargetInfoList();
185        annotationToAdd.setTargets(TargetInfoList);
186        annotationToAdd.setOwnerRef(null);     
187        annotationToAdd.setLastModified(DatatypeFactory.newInstance().newXMLGregorianCalendar(TestBackendConstants._TEST_ANNOT_2_TIME_STAMP));
188        annotationToAdd.setHeadline("headline");
189        annotationToAdd.setTargets(TargetInfoList);
190       
191       
192        AnnotationBody body   = new AnnotationBody();       
193        annotationToAdd.setBody(body);
194        TextBody textBody = new TextBody();
195        body.setTextBody(textBody);
196        textBody.setMimeType("text/plain");
197        textBody.setValue("blah");
198       
199        TargetInfo TargetInfo = new TargetInfo();
200        TargetInfo.setLink("google.nl");
201        TargetInfo.setRef(UUID.randomUUID().toString());
202        TargetInfo.setVersion("vandaag");
203       
204        final List<String> targets = new ArrayList<String>();
205        targets.add("http://localhost:8080/annotator-backend/api/targets/00000000-0000-0000-0000-000000000036");
206       
207        final Annotation addedAnnotation = (new ObjectFactory()).createAnnotation(annotationToAdd).getValue();
208        addedAnnotation.setURI("http://localhost:8080/annotator-backend/api/annotations/"+UUID.randomUUID().toString());
209        addedAnnotation.setOwnerRef("http://localhost:8080/annotator-backend/api/users/"+TestBackendConstants._TEST_USER_3_EXT_ID);
210     
211        annotationResource.setHttpServletRequest(mockRequest); 
212        annotationResource.setUriInfo(mockUriInfo); 
213       
214        mockeryRest.checking(new Expectations() {
215            {
216                oneOf(mockUriInfo).getBaseUri();
217                will(returnValue(URI.create("http://localhost:8080/annotator-backend/api/"))); 
218               
219               
220                oneOf(mockDbIntegrityService).setServiceURI(with(any(String.class)));
221                will(doAll());
222               
223                oneOf(mockDbIntegrityService).getUserInternalIDFromRemoteID("twan");
224                will(returnValue(3));
225               
226             
227                oneOf(mockDbIntegrityService).addUsersAnnotation(3, annotationToAdd);
228                will(returnValue(newAnnotationID));
229               
230                oneOf(mockDbIntegrityService).getAnnotation(newAnnotationID);
231                will(returnValue(addedAnnotation));
232               
233                oneOf(mockDbIntegrityService).getTargetsWithNoCachedRepresentation(newAnnotationID);
234                will(returnValue(targets));
235               
236          }
237        });
238       
239       
240       
241        JAXBElement<ResponseBody> result = annotationResource.createAnnotation(annotationToAdd);
242        Annotation newAnnotation = result.getValue().getAnnotation();
243        String actionName = result.getValue().getActionList().getAction().get(0).getMessage();
244        assertEquals(addedAnnotation.getOwnerRef(), newAnnotation.getOwnerRef());
245        assertEquals(addedAnnotation.getURI(), newAnnotation.getURI());
246        assertEquals(addedAnnotation.getHeadline(), newAnnotation.getHeadline());
247        assertEquals(addedAnnotation.getTargets(), newAnnotation.getTargets()); 
248        assertEquals(addedAnnotation.getLastModified(), newAnnotation.getLastModified());
249        assertEquals(addedAnnotation.getBody(), newAnnotation.getBody());
250        assertEquals(AnnotationActionName.CREATE_CACHED_REPRESENTATION.value(), actionName);
251    }
252}
Note: See TracBrowser for help on using the repository browser.