| margaretha | 541b8cc | 2018-01-10 13:02:46 +0100 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.controller; |
| Michael Hanl | a471397 | 2016-02-17 15:20:22 +0100 | [diff] [blame] | 2 | |
| margaretha | 1a6d020 | 2017-02-16 18:09:39 +0100 | [diff] [blame] | 3 | import static org.junit.Assert.assertEquals; |
| margaretha | 1a6d020 | 2017-02-16 18:09:39 +0100 | [diff] [blame] | 4 | import static org.junit.Assert.assertNotEquals; |
| 5 | import static org.junit.Assert.assertNotNull; |
| 6 | import static org.junit.Assert.assertTrue; |
| 7 | |
| margaretha | 7e16d6f | 2017-04-18 18:01:59 +0200 | [diff] [blame] | 8 | import java.io.IOException; |
| margaretha | 1a6d020 | 2017-02-16 18:09:39 +0100 | [diff] [blame] | 9 | |
| margaretha | 7e16d6f | 2017-04-18 18:01:59 +0200 | [diff] [blame] | 10 | import org.apache.http.HttpStatus; |
| margaretha | 0ba34ef | 2017-02-21 14:19:05 +0100 | [diff] [blame] | 11 | import org.junit.Assert; |
| Michael Hanl | a471397 | 2016-02-17 15:20:22 +0100 | [diff] [blame] | 12 | import org.junit.Ignore; |
| 13 | import org.junit.Test; |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 14 | import org.springframework.beans.factory.annotation.Autowired; |
| Michael Hanl | a471397 | 2016-02-17 15:20:22 +0100 | [diff] [blame] | 15 | |
| margaretha | 7e16d6f | 2017-04-18 18:01:59 +0200 | [diff] [blame] | 16 | import com.fasterxml.jackson.core.JsonProcessingException; |
| margaretha | 1a6d020 | 2017-02-16 18:09:39 +0100 | [diff] [blame] | 17 | import com.fasterxml.jackson.databind.JsonNode; |
| 18 | import com.sun.jersey.api.client.ClientResponse; |
| Michael Hanl | 9a24216 | 2016-06-03 11:18:06 +0200 | [diff] [blame] | 19 | |
| margaretha | 56e8e55 | 2017-12-05 16:31:21 +0100 | [diff] [blame] | 20 | import de.ids_mannheim.korap.authentication.http.HttpAuthorizationHandler; |
| margaretha | 1a6d020 | 2017-02-16 18:09:39 +0100 | [diff] [blame] | 21 | import de.ids_mannheim.korap.config.Attributes; |
| margaretha | 0e8f4e7 | 2018-04-05 14:11:52 +0200 | [diff] [blame] | 22 | import de.ids_mannheim.korap.constant.TokenType; |
| margaretha | 1a6d020 | 2017-02-16 18:09:39 +0100 | [diff] [blame] | 23 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| margaretha | 0ba34ef | 2017-02-21 14:19:05 +0100 | [diff] [blame] | 24 | import de.ids_mannheim.korap.handlers.ResourceDao; |
| margaretha | 0ba34ef | 2017-02-21 14:19:05 +0100 | [diff] [blame] | 25 | import de.ids_mannheim.korap.resources.KustvaktResource; |
| margaretha | 0ba34ef | 2017-02-21 14:19:05 +0100 | [diff] [blame] | 26 | import de.ids_mannheim.korap.user.User; |
| margaretha | 1a6d020 | 2017-02-16 18:09:39 +0100 | [diff] [blame] | 27 | import de.ids_mannheim.korap.utils.JsonUtils; |
| margaretha | 541b8cc | 2018-01-10 13:02:46 +0100 | [diff] [blame] | 28 | import de.ids_mannheim.korap.web.FastJerseyTest; |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 29 | |
| Michael Hanl | a471397 | 2016-02-17 15:20:22 +0100 | [diff] [blame] | 30 | /** |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 31 | * @author hanl, margaretha |
| Michael Hanl | a471397 | 2016-02-17 15:20:22 +0100 | [diff] [blame] | 32 | * @date 14/01/2016 |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 33 | * @update 24/04/2017 |
| Michael Hanl | a471397 | 2016-02-17 15:20:22 +0100 | [diff] [blame] | 34 | */ |
| margaretha | f18298b | 2017-09-14 22:14:32 +0200 | [diff] [blame] | 35 | @Ignore |
| 36 | @Deprecated |
| margaretha | 541b8cc | 2018-01-10 13:02:46 +0100 | [diff] [blame] | 37 | public class ResourceControllerTest extends FastJerseyTest { |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 38 | |
| margaretha | 4b5c141 | 2017-11-15 20:55:04 +0100 | [diff] [blame] | 39 | @Autowired |
| 40 | HttpAuthorizationHandler handler; |
| 41 | |
| Michael Hanl | 9a24216 | 2016-06-03 11:18:06 +0200 | [diff] [blame] | 42 | // create a simple test collection for user kustvakt, otherwise test fails |
| Michael Hanl | a471397 | 2016-02-17 15:20:22 +0100 | [diff] [blame] | 43 | @Test |
| Michael Hanl | 9a24216 | 2016-06-03 11:18:06 +0200 | [diff] [blame] | 44 | @Ignore |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 45 | public void testStats () throws KustvaktException{ |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 46 | ClientResponse response = resource().path(getAPIVersion()) |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 47 | .path("collection") |
| 48 | .header(Attributes.AUTHORIZATION, |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 49 | handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015")) |
| Michael Hanl | a471397 | 2016-02-17 15:20:22 +0100 | [diff] [blame] | 50 | .get(ClientResponse.class); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 51 | assertEquals(ClientResponse.Status.OK.getStatusCode(), |
| 52 | response.getStatus()); |
| Michael Hanl | a471397 | 2016-02-17 15:20:22 +0100 | [diff] [blame] | 53 | |
| 54 | JsonNode node = JsonUtils.readTree(response.getEntity(String.class)); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 55 | assertNotNull(node); |
| 56 | assertNotEquals(0, node.size()); |
| Michael Hanl | 9a24216 | 2016-06-03 11:18:06 +0200 | [diff] [blame] | 57 | String id = node.path(1).path("id").asText(); |
| Michael Hanl | a471397 | 2016-02-17 15:20:22 +0100 | [diff] [blame] | 58 | |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 59 | response = resource().path(getAPIVersion()).path("collection").path(id) |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 60 | .path("stats") |
| 61 | .header(Attributes.AUTHORIZATION, |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 62 | handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015")) |
| Michael Hanl | a471397 | 2016-02-17 15:20:22 +0100 | [diff] [blame] | 63 | .get(ClientResponse.class); |
| 64 | |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 65 | assertEquals(ClientResponse.Status.OK.getStatusCode(), |
| 66 | response.getStatus()); |
| Michael Hanl | a471397 | 2016-02-17 15:20:22 +0100 | [diff] [blame] | 67 | node = JsonUtils.readTree(response.getEntity(String.class)); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 68 | assertNotNull(node); |
| Michael Hanl | 9a24216 | 2016-06-03 11:18:06 +0200 | [diff] [blame] | 69 | assertNotNull(node); |
| Michael Hanl | a471397 | 2016-02-17 15:20:22 +0100 | [diff] [blame] | 70 | int docs = node.path("documents").asInt(); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 71 | assertNotEquals(0, docs); |
| Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 72 | assertTrue(docs < 15); |
| Michael Hanl | a471397 | 2016-02-17 15:20:22 +0100 | [diff] [blame] | 73 | } |
| 74 | |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 75 | |
| Michael Hanl | a471397 | 2016-02-17 15:20:22 +0100 | [diff] [blame] | 76 | @Test |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 77 | 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, |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 82 | handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015")) |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 83 | .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, |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 107 | handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015")) |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 108 | .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, |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 130 | handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015")) |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 131 | .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, |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 152 | handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015")) |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 153 | .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, |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 175 | handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015")) |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 176 | .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, |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 211 | handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015")) |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 212 | .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, |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 245 | handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015")) |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 246 | .post(ClientResponse.class); |
| 247 | |
| 248 | assertEquals(ClientResponse.Status.BAD_REQUEST.getStatusCode(), |
| 249 | response.getStatus()); |
| 250 | |
| 251 | String ent = response.getEntity(String.class); |
| margaretha | 23aae22 | 2017-12-22 15:08:23 +0100 | [diff] [blame] | 252 | // System.out.println(ent); |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 253 | 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, |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 270 | handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015")) |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 271 | .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") |
| margaretha | 1a6d020 | 2017-02-16 18:09:39 +0100 | [diff] [blame] | 303 | .queryParam("name", "Goethe") |
| 304 | .queryParam("description", "Goethe corpus") |
| 305 | .header(Attributes.AUTHORIZATION, |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 306 | handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015")) |
| margaretha | 1a6d020 | 2017-02-16 18:09:39 +0100 | [diff] [blame] | 307 | .post(ClientResponse.class); |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 308 | |
| margaretha | 1a6d020 | 2017-02-16 18:09:39 +0100 | [diff] [blame] | 309 | assertEquals(ClientResponse.Status.OK.getStatusCode(), |
| 310 | response.getStatus()); |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 311 | |
| margaretha | 1a6d020 | 2017-02-16 18:09:39 +0100 | [diff] [blame] | 312 | String ent = response.getEntity(String.class); |
| margaretha | 1a6d020 | 2017-02-16 18:09:39 +0100 | [diff] [blame] | 313 | 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()); |
| margaretha | 3595ffd | 2017-02-24 13:56:29 +0100 | [diff] [blame] | 318 | |
| 319 | String id = node.path("id").asText(); |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 320 | |
| margaretha | 0ba34ef | 2017-02-21 14:19:05 +0100 | [diff] [blame] | 321 | // check if the resource is in the db |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 322 | ResourceDao<?> dao = new ResourceDao<>( |
| 323 | helper().getContext().getPersistenceClient()); |
| 324 | assertEquals("sqlite", |
| 325 | helper().getContext().getPersistenceClient().getDatabase()); |
| Michael Hanl | a471397 | 2016-02-17 15:20:22 +0100 | [diff] [blame] | 326 | |
| margaretha | 0ba34ef | 2017-02-21 14:19:05 +0100 | [diff] [blame] | 327 | assertNotEquals(0, dao.size()); |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 328 | KustvaktResource res = dao.findbyId(id, User.UserFactory.getDemoUser()); |
| margaretha | 0ba34ef | 2017-02-21 14:19:05 +0100 | [diff] [blame] | 329 | assertNotNull(res); |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 330 | Assert.assertEquals("Goethe", res.getName().toString()); |
| 331 | |
| margaretha | 0c47c65 | 2017-04-19 18:44:40 +0200 | [diff] [blame] | 332 | // no update resource service |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 333 | response = resource().path(getAPIVersion()).path("virtualcollection") |
| 334 | .path(id).queryParam("name", "Goethe") |
| margaretha | fc2040a | 2017-04-18 12:07:23 +0200 | [diff] [blame] | 335 | .header(Attributes.AUTHORIZATION, |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 336 | handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015")) |
| margaretha | fc2040a | 2017-04-18 12:07:23 +0200 | [diff] [blame] | 337 | .post(ClientResponse.class); |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 338 | |
| margaretha | 7e16d6f | 2017-04-18 18:01:59 +0200 | [diff] [blame] | 339 | assertEquals(HttpStatus.SC_BAD_REQUEST, response.getStatus()); |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 340 | |
| margaretha | 61471cc | 2017-04-20 18:42:23 +0200 | [diff] [blame] | 341 | node = JsonUtils.readTree(response.getEntity(String.class)); |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 342 | assertEquals("[No change has found.]", |
| margaretha | 61471cc | 2017-04-20 18:42:23 +0200 | [diff] [blame] | 343 | node.get("errors").get(0).get(2).asText()); |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 344 | |
| margaretha | fc2040a | 2017-04-18 12:07:23 +0200 | [diff] [blame] | 345 | // update resource service |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 346 | response = resource().path(getAPIVersion()).path("virtualcollection") |
| 347 | .path(id).queryParam("name", "Goethe collection") |
| margaretha | fc2040a | 2017-04-18 12:07:23 +0200 | [diff] [blame] | 348 | .header(Attributes.AUTHORIZATION, |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 349 | handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015")) |
| margaretha | fc2040a | 2017-04-18 12:07:23 +0200 | [diff] [blame] | 350 | .post(ClientResponse.class); |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 351 | |
| margaretha | 7e16d6f | 2017-04-18 18:01:59 +0200 | [diff] [blame] | 352 | assertEquals(ClientResponse.Status.OK.getStatusCode(), |
| 353 | response.getStatus()); |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 354 | |
| 355 | res = dao.findbyId(id, User.UserFactory.getDemoUser()); |
| margaretha | fc2040a | 2017-04-18 12:07:23 +0200 | [diff] [blame] | 356 | assertNotNull(res); |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 357 | Assert.assertEquals("Goethe collection", res.getName().toString()); |
| 358 | |
| 359 | |
| margaretha | 0ba34ef | 2017-02-21 14:19:05 +0100 | [diff] [blame] | 360 | // delete resource service |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 361 | response = resource().path(getAPIVersion()).path("virtualcollection") |
| margaretha | 3595ffd | 2017-02-24 13:56:29 +0100 | [diff] [blame] | 362 | .path(id) |
| margaretha | 0ba34ef | 2017-02-21 14:19:05 +0100 | [diff] [blame] | 363 | .header(Attributes.AUTHORIZATION, |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 364 | handler.createBasicAuthorizationHeaderValue("kustvakt", "kustvakt2015")) |
| margaretha | 0ba34ef | 2017-02-21 14:19:05 +0100 | [diff] [blame] | 365 | .delete(ClientResponse.class); |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 366 | |
| margaretha | 0ba34ef | 2017-02-21 14:19:05 +0100 | [diff] [blame] | 367 | assertEquals(ClientResponse.Status.OK.getStatusCode(), |
| 368 | response.getStatus()); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 369 | |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 370 | // 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 Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 377 | } |
| 378 | |
| margaretha | 50b331e | 2017-04-25 18:05:16 +0200 | [diff] [blame] | 379 | |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 380 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 381 | public void initMethod () throws KustvaktException { |
| margaretha | e7badb3 | 2017-06-22 15:16:00 +0200 | [diff] [blame] | 382 | // helper().runBootInterfaces(); |
| Michael Hanl | daf8660 | 2016-05-12 14:31:52 +0200 | [diff] [blame] | 383 | } |
| Michael Hanl | a471397 | 2016-02-17 15:20:22 +0100 | [diff] [blame] | 384 | } |