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