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

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

test commit after disaster

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