blob: 6a0bb6428eab54a9dedfae6e0c598e28863aef62 [file] [log] [blame]
margaretha541b8cc2018-01-10 13:02:46 +01001package de.ids_mannheim.korap.web.controller;
Michael Hanla4713972016-02-17 15:20:22 +01002
margaretha1a6d0202017-02-16 18:09:39 +01003import static org.junit.Assert.assertEquals;
margaretha1a6d0202017-02-16 18:09:39 +01004import static org.junit.Assert.assertNotEquals;
5import static org.junit.Assert.assertNotNull;
6import static org.junit.Assert.assertTrue;
7
margaretha7e16d6f2017-04-18 18:01:59 +02008import java.io.IOException;
margaretha1a6d0202017-02-16 18:09:39 +01009
margaretha7e16d6f2017-04-18 18:01:59 +020010import org.apache.http.HttpStatus;
margaretha0ba34ef2017-02-21 14:19:05 +010011import org.junit.Assert;
Michael Hanla4713972016-02-17 15:20:22 +010012import org.junit.Ignore;
13import org.junit.Test;
margaretha4b5c1412017-11-15 20:55:04 +010014import org.springframework.beans.factory.annotation.Autowired;
Michael Hanla4713972016-02-17 15:20:22 +010015
margaretha7e16d6f2017-04-18 18:01:59 +020016import com.fasterxml.jackson.core.JsonProcessingException;
margaretha1a6d0202017-02-16 18:09:39 +010017import com.fasterxml.jackson.databind.JsonNode;
18import com.sun.jersey.api.client.ClientResponse;
Michael Hanl9a242162016-06-03 11:18:06 +020019
margaretha56e8e552017-12-05 16:31:21 +010020import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler;
margaretha1a6d0202017-02-16 18:09:39 +010021import de.ids_mannheim.korap.config.Attributes;
margaretha0e8f4e72018-04-05 14:11:52 +020022import de.ids_mannheim.korap.constant.TokenType;
margaretha1a6d0202017-02-16 18:09:39 +010023import de.ids_mannheim.korap.exceptions.KustvaktException;
margaretha0ba34ef2017-02-21 14:19:05 +010024import de.ids_mannheim.korap.handlers.ResourceDao;
margaretha0ba34ef2017-02-21 14:19:05 +010025import de.ids_mannheim.korap.resources.KustvaktResource;
margaretha0ba34ef2017-02-21 14:19:05 +010026import de.ids_mannheim.korap.user.User;
margaretha1a6d0202017-02-16 18:09:39 +010027import de.ids_mannheim.korap.utils.JsonUtils;
margaretha541b8cc2018-01-10 13:02:46 +010028import de.ids_mannheim.korap.web.FastJerseyTest;
Michael Hanldaf86602016-05-12 14:31:52 +020029
Michael Hanla4713972016-02-17 15:20:22 +010030/**
margaretha50b331e2017-04-25 18:05:16 +020031 * @author hanl, margaretha
Michael Hanla4713972016-02-17 15:20:22 +010032 * @date 14/01/2016
margaretha50b331e2017-04-25 18:05:16 +020033 * @update 24/04/2017
Michael Hanla4713972016-02-17 15:20:22 +010034 */
margarethaf18298b2017-09-14 22:14:32 +020035@Ignore
36@Deprecated
margaretha541b8cc2018-01-10 13:02:46 +010037public class ResourceControllerTest extends FastJerseyTest {
margaretha50b331e2017-04-25 18:05:16 +020038
margaretha4b5c1412017-11-15 20:55:04 +010039 @Autowired
40 HttpAuthorizationHandler handler;
41
Michael Hanl9a242162016-06-03 11:18:06 +020042 // create a simple test collection for user kustvakt, otherwise test fails
Michael Hanla4713972016-02-17 15:20:22 +010043 @Test
Michael Hanl9a242162016-06-03 11:18:06 +020044 @Ignore
margaretha894a7d72017-11-08 19:24:20 +010045 public void testStats () throws KustvaktException{
margaretha50b331e2017-04-25 18:05:16 +020046 ClientResponse response = resource().path(getAPIVersion())
Michael Hanl8abaf9e2016-05-23 16:46:35 +020047 .path("collection")
48 .header(Attributes.AUTHORIZATION,
margaretha2afb97d2017-12-07 19:18:44 +010049 handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015"))
Michael Hanla4713972016-02-17 15:20:22 +010050 .get(ClientResponse.class);
Michael Hanldaf86602016-05-12 14:31:52 +020051 assertEquals(ClientResponse.Status.OK.getStatusCode(),
52 response.getStatus());
Michael Hanla4713972016-02-17 15:20:22 +010053
54 JsonNode node = JsonUtils.readTree(response.getEntity(String.class));
Michael Hanl8abaf9e2016-05-23 16:46:35 +020055 assertNotNull(node);
56 assertNotEquals(0, node.size());
Michael Hanl9a242162016-06-03 11:18:06 +020057 String id = node.path(1).path("id").asText();
Michael Hanla4713972016-02-17 15:20:22 +010058
margaretha50b331e2017-04-25 18:05:16 +020059 response = resource().path(getAPIVersion()).path("collection").path(id)
Michael Hanl8abaf9e2016-05-23 16:46:35 +020060 .path("stats")
61 .header(Attributes.AUTHORIZATION,
margaretha2afb97d2017-12-07 19:18:44 +010062 handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015"))
Michael Hanla4713972016-02-17 15:20:22 +010063 .get(ClientResponse.class);
64
Michael Hanldaf86602016-05-12 14:31:52 +020065 assertEquals(ClientResponse.Status.OK.getStatusCode(),
66 response.getStatus());
Michael Hanla4713972016-02-17 15:20:22 +010067 node = JsonUtils.readTree(response.getEntity(String.class));
Michael Hanl8abaf9e2016-05-23 16:46:35 +020068 assertNotNull(node);
Michael Hanl9a242162016-06-03 11:18:06 +020069 assertNotNull(node);
Michael Hanla4713972016-02-17 15:20:22 +010070 int docs = node.path("documents").asInt();
Michael Hanldaf86602016-05-12 14:31:52 +020071 assertNotEquals(0, docs);
Michael Hanl00b64e02016-05-24 20:24:27 +020072 assertTrue(docs < 15);
Michael Hanla4713972016-02-17 15:20:22 +010073 }
74
margaretha50b331e2017-04-25 18:05:16 +020075
Michael Hanla4713972016-02-17 15:20:22 +010076 @Test
margaretha50b331e2017-04-25 18:05:16 +020077 public void testOwnerUpdateVirtualCollection () throws KustvaktException {
78 ClientResponse response = resource().path(getAPIVersion())
79 .path("virtualcollection").path("GOE-VC") // persistent id
80 .queryParam("name", "Goethe collection")
81 .header(Attributes.AUTHORIZATION,
margaretha2afb97d2017-12-07 19:18:44 +010082 handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015"))
margaretha50b331e2017-04-25 18:05:16 +020083 .post(ClientResponse.class);
84
85 assertEquals(ClientResponse.Status.OK.getStatusCode(),
86 response.getStatus());
87
88 ResourceDao<?> dao = new ResourceDao<>(
89 helper().getContext().getPersistenceClient());
90 assertEquals("sqlite",
91 helper().getContext().getPersistenceClient().getDatabase());
92
93 KustvaktResource res = dao.findbyId("GOE-VC",
94 User.UserFactory.getDemoUser());
95 assertNotNull(res);
96 assertEquals("Goethe collection", res.getName().toString());
97
98 }
99
100
101 @Test
102 public void testOwnerUpdateCorpus () throws KustvaktException {
103 ClientResponse response = resource().path(getAPIVersion())
104 .path("corpus").path("GOE") // persistent id
105 .queryParam("name", "Goethe corpus")
106 .header(Attributes.AUTHORIZATION,
margaretha2afb97d2017-12-07 19:18:44 +0100107 handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015"))
margaretha50b331e2017-04-25 18:05:16 +0200108 .post(ClientResponse.class);
109
110 assertEquals(ClientResponse.Status.OK.getStatusCode(),
111 response.getStatus());
112
113 ResourceDao<?> dao = new ResourceDao<>(
114 helper().getContext().getPersistenceClient());
115
116 KustvaktResource res = dao.findbyId("GOE",
117 User.UserFactory.getDemoUser());
118 assertNotNull(res);
119 assertEquals("Goethe corpus", res.getName().toString());
120
121 }
122
123
124 @Test
125 public void testOwnerUpdateFoundry () throws KustvaktException {
126 ClientResponse response = resource().path(getAPIVersion())
127 .path("foundry").path("malt") // persistent id
128 .queryParam("name", "malt parser")
129 .header(Attributes.AUTHORIZATION,
margaretha2afb97d2017-12-07 19:18:44 +0100130 handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015"))
margaretha50b331e2017-04-25 18:05:16 +0200131 .post(ClientResponse.class);
132
133 assertEquals(ClientResponse.Status.OK.getStatusCode(),
134 response.getStatus());
135
136 ResourceDao<?> dao = new ResourceDao<>(
137 helper().getContext().getPersistenceClient());
138
139 KustvaktResource res = dao.findbyId("malt",
140 User.UserFactory.getDemoUser());
141 assertNotNull(res);
142 assertEquals("malt parser", res.getName().toString());
143
144 }
145
146
147 @Test
148 public void testOwnerUpdateLayer () throws KustvaktException {
149 ClientResponse response = resource().path(getAPIVersion()).path("layer")
150 .path("mate/d").queryParam("name", "Mate dependency")
151 .header(Attributes.AUTHORIZATION,
margaretha2afb97d2017-12-07 19:18:44 +0100152 handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015"))
margaretha50b331e2017-04-25 18:05:16 +0200153 .post(ClientResponse.class);
154
155 assertEquals(ClientResponse.Status.OK.getStatusCode(),
156 response.getStatus());
157
158 ResourceDao<?> dao = new ResourceDao<>(
159 helper().getContext().getPersistenceClient());
160
161 KustvaktResource res = dao.findbyId("mate/d",
162 User.UserFactory.getDemoUser());
163 assertNotNull(res);
164 assertEquals("Mate dependency", res.getName().toString());
165
166 }
167
168
169 @Test
170 public void testOwnerUpdateUnexistingCorpus () throws KustvaktException {
171 ClientResponse response = resource().path(getAPIVersion())
172 .path("corpus").path("GOEC") // persistent id
173 .queryParam("name", "Goethe corpus")
174 .header(Attributes.AUTHORIZATION,
margaretha2afb97d2017-12-07 19:18:44 +0100175 handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015"))
margaretha50b331e2017-04-25 18:05:16 +0200176 .post(ClientResponse.class);
177
178 assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(),
179 response.getStatus());
180
181 JsonNode node = JsonUtils.readTree(response.getEntity(String.class));
182 assertEquals("Resource not found!",
183 node.get("errors").get(0).get(1).asText());
184
185 }
186
187
188 @Test
189 public void testUpdateUnauthorized () throws KustvaktException {
190 ClientResponse response = resource().path(getAPIVersion())
191 .path("corpus").path("GOE") // persistent id
192 .queryParam("name", "Goethe corpus").post(ClientResponse.class);
193
194 assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(),
195 response.getStatus());
196
197 JsonNode node = JsonUtils.readTree(response.getEntity(String.class));
198 assertEquals("Permission denied for resource id GOE for the user.",
199 node.get("errors").get(0).get(1).asText());
200
201 }
202
203
204 @Test
205 public void testStoreNewVirtualCollection () throws KustvaktException {
206 ClientResponse response = resource().path(getAPIVersion())
207 .path("virtualcollection").queryParam("filter", "false")
208 .queryParam("name", "Brown")
209 .queryParam("description", "Brown corpus")
210 .header(Attributes.AUTHORIZATION,
margaretha2afb97d2017-12-07 19:18:44 +0100211 handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015"))
margaretha50b331e2017-04-25 18:05:16 +0200212 .post(ClientResponse.class);
213
214 assertEquals(ClientResponse.Status.OK.getStatusCode(),
215 response.getStatus());
216
217 String ent = response.getEntity(String.class);
218 JsonNode node = JsonUtils.readTree(ent);
219 assertEquals("Brown", node.path("name").asText());
220 assertEquals("Brown corpus", node.path("description").asText());
221
222 String id = node.path("id").asText();
223
224 // check if the resource is in the db
225 ResourceDao<?> dao = new ResourceDao<>(
226 helper().getContext().getPersistenceClient());
227 assertEquals("sqlite",
228 helper().getContext().getPersistenceClient().getDatabase());
229
230 assertNotEquals(0, dao.size());
231 KustvaktResource res = dao.findbyId(id, User.UserFactory.getDemoUser());
232 assertEquals("Brown", res.getName().toString());
233
234 }
235
236
237 @Test
238 public void testStoreUnsupportedType () throws KustvaktException {
239
240 ClientResponse response = resource().path(getAPIVersion())
241 .path("corpus").queryParam("filter", "false")
242 .queryParam("name", "Brown")
243 .queryParam("description", "Brown corpus")
244 .header(Attributes.AUTHORIZATION,
margaretha2afb97d2017-12-07 19:18:44 +0100245 handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015"))
margaretha50b331e2017-04-25 18:05:16 +0200246 .post(ClientResponse.class);
247
248 assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(),
249 response.getStatus());
250
251 String ent = response.getEntity(String.class);
margaretha23aae222017-12-22 15:08:23 +0100252// System.out.println(ent);
margaretha50b331e2017-04-25 18:05:16 +0200253 JsonNode node = JsonUtils.readTree(ent);
254 assertEquals(402, node.at("/errors/0/0").asInt());
255 assertEquals("Unsupported operation for the given resource type.",
256 node.at("/errors/0/1").asText());
257 }
258
259
260 @Test
261 public void testStoreNewVirtualCollectionFromExistingCollection ()
262 throws KustvaktException {
263 ClientResponse response = resource().path(getAPIVersion())
264 .path("virtualcollection").queryParam("filter", "true")
265 .queryParam("ref", "WPD15-VC")
266 .queryParam("name", "Wikipedia collection")
267 .queryParam("query", "author ~ Asdert")
268 .queryParam("description", "Wikipedia subcorpus from Asdert")
269 .header(Attributes.AUTHORIZATION,
margaretha2afb97d2017-12-07 19:18:44 +0100270 handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015"))
margaretha50b331e2017-04-25 18:05:16 +0200271 .post(ClientResponse.class);
272
273 assertEquals(ClientResponse.Status.OK.getStatusCode(),
274 response.getStatus());
275
276 String ent = response.getEntity(String.class);
277 JsonNode node = JsonUtils.readTree(ent);
278 String id = node.path("id").asText();
279
280 assertEquals("Wikipedia collection", node.path("name").asText());
281 assertEquals("Wikipedia subcorpus from Asdert",
282 node.path("description").asText());
283 node = node.at("/data/collection/operands/1");
284 assertEquals("author", node.at("/key").asText());
285 assertEquals("Asdert", node.at("/value").asText());
286
287 // check if the resource is in the db
288 ResourceDao<?> dao = new ResourceDao<>(
289 helper().getContext().getPersistenceClient());
290 KustvaktResource res = dao.findbyId(id, User.UserFactory.getDemoUser());
291 assertEquals("Wikipedia collection", res.getName().toString());
292 }
293
294
295 // EM: The test covers multiple ordered operations dealing with
296 // the same resource (store followed by update followed by delete).
297 @Test
298 public void testVirtualCollectionStoreUpdateDelete ()
299 throws KustvaktException, JsonProcessingException, IOException {
300 // resource store service
301 ClientResponse response = resource().path(getAPIVersion())
302 .path("virtualcollection").queryParam("filter", "false")
margaretha1a6d0202017-02-16 18:09:39 +0100303 .queryParam("name", "Goethe")
304 .queryParam("description", "Goethe corpus")
305 .header(Attributes.AUTHORIZATION,
margaretha2afb97d2017-12-07 19:18:44 +0100306 handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015"))
margaretha1a6d0202017-02-16 18:09:39 +0100307 .post(ClientResponse.class);
margaretha50b331e2017-04-25 18:05:16 +0200308
margaretha1a6d0202017-02-16 18:09:39 +0100309 assertEquals(ClientResponse.Status.OK.getStatusCode(),
310 response.getStatus());
margaretha50b331e2017-04-25 18:05:16 +0200311
margaretha1a6d0202017-02-16 18:09:39 +0100312 String ent = response.getEntity(String.class);
margaretha1a6d0202017-02-16 18:09:39 +0100313 JsonNode node = JsonUtils.readTree(ent);
314 assertNotNull(node);
315 assertTrue(node.isObject());
316 assertEquals("Goethe", node.path("name").asText());
317 assertEquals("Goethe corpus", node.path("description").asText());
margaretha3595ffd2017-02-24 13:56:29 +0100318
319 String id = node.path("id").asText();
margaretha50b331e2017-04-25 18:05:16 +0200320
margaretha0ba34ef2017-02-21 14:19:05 +0100321 // check if the resource is in the db
margaretha50b331e2017-04-25 18:05:16 +0200322 ResourceDao<?> dao = new ResourceDao<>(
323 helper().getContext().getPersistenceClient());
324 assertEquals("sqlite",
325 helper().getContext().getPersistenceClient().getDatabase());
Michael Hanla4713972016-02-17 15:20:22 +0100326
margaretha0ba34ef2017-02-21 14:19:05 +0100327 assertNotEquals(0, dao.size());
margaretha50b331e2017-04-25 18:05:16 +0200328 KustvaktResource res = dao.findbyId(id, User.UserFactory.getDemoUser());
margaretha0ba34ef2017-02-21 14:19:05 +0100329 assertNotNull(res);
margaretha50b331e2017-04-25 18:05:16 +0200330 Assert.assertEquals("Goethe", res.getName().toString());
331
margaretha0c47c652017-04-19 18:44:40 +0200332 // no update resource service
margaretha50b331e2017-04-25 18:05:16 +0200333 response = resource().path(getAPIVersion()).path("virtualcollection")
334 .path(id).queryParam("name", "Goethe")
margarethafc2040a2017-04-18 12:07:23 +0200335 .header(Attributes.AUTHORIZATION,
margaretha2afb97d2017-12-07 19:18:44 +0100336 handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015"))
margarethafc2040a2017-04-18 12:07:23 +0200337 .post(ClientResponse.class);
margaretha50b331e2017-04-25 18:05:16 +0200338
margaretha7e16d6f2017-04-18 18:01:59 +0200339 assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatus());
margaretha50b331e2017-04-25 18:05:16 +0200340
margaretha61471cc2017-04-20 18:42:23 +0200341 node = JsonUtils.readTree(response.getEntity(String.class));
margaretha50b331e2017-04-25 18:05:16 +0200342 assertEquals("[No change has found.]",
margaretha61471cc2017-04-20 18:42:23 +0200343 node.get("errors").get(0).get(2).asText());
margaretha50b331e2017-04-25 18:05:16 +0200344
margarethafc2040a2017-04-18 12:07:23 +0200345 // update resource service
margaretha50b331e2017-04-25 18:05:16 +0200346 response = resource().path(getAPIVersion()).path("virtualcollection")
347 .path(id).queryParam("name", "Goethe collection")
margarethafc2040a2017-04-18 12:07:23 +0200348 .header(Attributes.AUTHORIZATION,
margaretha2afb97d2017-12-07 19:18:44 +0100349 handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015"))
margarethafc2040a2017-04-18 12:07:23 +0200350 .post(ClientResponse.class);
margaretha50b331e2017-04-25 18:05:16 +0200351
margaretha7e16d6f2017-04-18 18:01:59 +0200352 assertEquals(ClientResponse.Status.OK.getStatusCode(),
353 response.getStatus());
margaretha50b331e2017-04-25 18:05:16 +0200354
355 res = dao.findbyId(id, User.UserFactory.getDemoUser());
margarethafc2040a2017-04-18 12:07:23 +0200356 assertNotNull(res);
margaretha50b331e2017-04-25 18:05:16 +0200357 Assert.assertEquals("Goethe collection", res.getName().toString());
358
359
margaretha0ba34ef2017-02-21 14:19:05 +0100360 // delete resource service
margaretha50b331e2017-04-25 18:05:16 +0200361 response = resource().path(getAPIVersion()).path("virtualcollection")
margaretha3595ffd2017-02-24 13:56:29 +0100362 .path(id)
margaretha0ba34ef2017-02-21 14:19:05 +0100363 .header(Attributes.AUTHORIZATION,
margaretha2afb97d2017-12-07 19:18:44 +0100364 handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015"))
margaretha0ba34ef2017-02-21 14:19:05 +0100365 .delete(ClientResponse.class);
margaretha50b331e2017-04-25 18:05:16 +0200366
margaretha0ba34ef2017-02-21 14:19:05 +0100367 assertEquals(ClientResponse.Status.OK.getStatusCode(),
368 response.getStatus());
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200369
margaretha50b331e2017-04-25 18:05:16 +0200370 // check if the resource is *not* in the db anymore
371 dao = new ResourceDao<>(helper().getContext().getPersistenceClient());
372 assertEquals("sqlite",
373 helper().getContext().getPersistenceClient().getDatabase());
374
375 res = dao.findbyId(id, User.UserFactory.getDemoUser());
376 assertEquals(null, res);
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200377 }
378
margaretha50b331e2017-04-25 18:05:16 +0200379
Michael Hanldaf86602016-05-12 14:31:52 +0200380 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200381 public void initMethod () throws KustvaktException {
margarethae7badb32017-06-22 15:16:00 +0200382// helper().runBootInterfaces();
Michael Hanldaf86602016-05-12 14:31:52 +0200383 }
Michael Hanla4713972016-02-17 15:20:22 +0100384}