1 source("fakes/FakeArvados.R")
2 source("fakes/FakeHttpRequest.R")
3 source("fakes/FakeHttpParser.R")
5 context("REST service")
7 test_that("getWebDavHostName calls REST service properly", {
9 expectedURL <- "https://host/discovery/v1/apis/arvados/v1/rest"
10 serverResponse <- list(keepWebServiceUrl = "https://myWebDavServer.com")
11 httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
13 REST <- RESTService$new("token", "host", NULL,
14 httpRequest, FakeHttpParser$new())
16 REST$getWebDavHostName()
18 expect_that(httpRequest$URLIsProperlyConfigured, is_true())
19 expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
20 expect_that(httpRequest$numberOfGETRequests, equals(1))
23 test_that("getWebDavHostName returns webDAV host name properly", {
25 serverResponse <- list(keepWebServiceUrl = "https://myWebDavServer.com")
26 httpRequest <- FakeHttpRequest$new(expectedURL = NULL, serverResponse)
28 REST <- RESTService$new("token", "host", NULL,
29 httpRequest, FakeHttpParser$new())
31 expect_that("https://myWebDavServer.com", equals(REST$getWebDavHostName()))
34 test_that("getResource calls REST service properly", {
36 serverResponse <- NULL
37 resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
38 expectedURL <- paste0("https://host/arvados/v1/collections/", resourceUUID)
40 httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
42 REST <- RESTService$new("token", "host", "webDavHost",
43 httpRequest, FakeHttpParser$new())
45 REST$getResource("collections", resourceUUID)
47 expect_that(httpRequest$URLIsProperlyConfigured, is_true())
48 expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
49 expect_that(httpRequest$numberOfGETRequests, equals(1))
52 test_that("getResource parses server response", {
54 resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
55 httpParser <- FakeHttpParser$new()
56 REST <- RESTService$new("token", "host", "webDavHost",
57 FakeHttpRequest$new(), httpParser)
59 REST$getResource("collections", resourceUUID)
61 expect_that(httpParser$parserCallCount, equals(1))
64 test_that("getResource raises exception if response contains errors field", {
66 resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
67 serverResponse <- list(errors = 404)
69 REST <- RESTService$new("token", "host", "webDavHost",
70 FakeHttpRequest$new(NULL, serverResponse),
73 expect_that(REST$getResource("collections", resourceUUID), throws_error(404))
76 test_that("listResources calls REST service properly", {
78 serverResponse <- NULL
79 expectedURL <- paste0("https://host/arvados/v1/collections")
80 httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
82 REST <- RESTService$new("token", "host", "webDavHost",
83 httpRequest, FakeHttpParser$new())
85 REST$listResources("collections")
87 expect_that(httpRequest$URLIsProperlyConfigured, is_true())
88 expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
89 expect_that(httpRequest$numberOfGETRequests, equals(1))
92 test_that("listResources parses server response", {
94 httpParser <- FakeHttpParser$new()
95 REST <- RESTService$new("token", "host", "webDavHost",
96 FakeHttpRequest$new(), httpParser)
98 REST$listResources("collections")
100 expect_that(httpParser$parserCallCount, equals(1))
103 test_that("listResources raises exception if response contains errors field", {
105 serverResponse <- list(errors = 404)
107 REST <- RESTService$new("token", "host", "webDavHost",
108 FakeHttpRequest$new(NULL, serverResponse),
109 FakeHttpParser$new())
111 expect_that(REST$listResources("collections"), throws_error(404))
114 test_that("fetchAllItems always returns all resource items from server", {
117 serverResponse <- list(items_available = 8,
118 items = list("collection1",
127 httpParser <- FakeHttpParser$new()
128 httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
129 httpRequest$serverMaxElementsPerRequest <- 3
131 REST <- RESTService$new("token", "host", "webDavHost",
132 httpRequest, httpParser)
134 result <- REST$fetchAllItems(NULL, NULL)
136 expect_that(length(result), equals(8))
137 expect_that(httpRequest$numberOfGETRequests, equals(3))
138 expect_that(httpParser$parserCallCount, equals(3))
141 test_that("deleteResource calls REST service properly", {
143 serverResponse <- NULL
144 resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
145 expectedURL <- paste0("https://host/arvados/v1/collections/", resourceUUID)
147 httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
149 REST <- RESTService$new("token", "host", "webDavHost",
150 httpRequest, FakeHttpParser$new())
152 REST$deleteResource("collections", resourceUUID)
154 expect_that(httpRequest$URLIsProperlyConfigured, is_true())
155 expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
156 expect_that(httpRequest$numberOfDELETERequests, equals(1))
159 test_that("deleteCollection parses server response", {
161 resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
162 httpParser <- FakeHttpParser$new()
163 REST <- RESTService$new("token", "host", "webDavHost",
164 FakeHttpRequest$new(), httpParser)
166 REST$deleteResource("collections", resourceUUID)
168 expect_that(httpParser$parserCallCount, equals(1))
171 test_that("deleteCollection raises exception if response contains errors field", {
173 resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
174 serverResponse <- list(errors = 404)
176 REST <- RESTService$new("token", "host", "webDavHost",
177 FakeHttpRequest$new(NULL, serverResponse),
178 FakeHttpParser$new())
180 expect_that(REST$deleteResource("collections", resourceUUID), throws_error(404))
183 test_that("updateResource calls REST service properly", {
185 serverResponse <- NULL
186 resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
187 expectedURL <- paste0("https://host/arvados/v1/collections/", resourceUUID)
188 newResourceContent <- list(newName = "Brand new shiny name")
190 httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
192 REST <- RESTService$new("token", "host", "webDavHost",
193 httpRequest, FakeHttpParser$new())
195 REST$updateResource("collections", resourceUUID, newResourceContent)
197 expect_that(httpRequest$URLIsProperlyConfigured, is_true())
198 expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
199 expect_that(httpRequest$JSONEncodedBodyIsProvided, is_true())
200 expect_that(httpRequest$numberOfPUTRequests, equals(1))
203 test_that("updateResource parses server response", {
205 newResourceContent <- list(newName = "Brand new shiny name")
206 resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
207 httpParser <- FakeHttpParser$new()
208 REST <- RESTService$new("token", "host", "webDavHost",
209 FakeHttpRequest$new(), httpParser)
211 REST$updateResource("collections", resourceUUID, newResourceContent)
213 expect_that(httpParser$parserCallCount, equals(1))
216 test_that("updateResource raises exception if response contains errors field", {
218 resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
219 serverResponse <- list(errors = 404)
220 newResourceContent <- list(newName = "Brand new shiny name")
221 REST <- RESTService$new("token", "host", "webDavHost",
222 FakeHttpRequest$new(NULL, serverResponse),
223 FakeHttpParser$new())
225 expect_that(REST$updateResource("collections", resourceUUID, newResourceContent),
229 test_that("createResource calls REST service properly", {
231 resourceContent <- list(name = "My favorite collection")
232 serverResponse <- NULL
233 resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
234 expectedURL <- "https://host/arvados/v1/collections"
235 newResourceContent <- list(newName = "Brand new shiny name")
237 httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
239 REST <- RESTService$new("token", "host", "webDavHost",
240 httpRequest, FakeHttpParser$new())
242 REST$createResource("collections", resourceContent)
244 expect_that(httpRequest$URLIsProperlyConfigured, is_true())
245 expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
246 expect_that(httpRequest$JSONEncodedBodyIsProvided, is_true())
247 expect_that(httpRequest$numberOfPOSTRequests, equals(1))
250 test_that("createResource parses server response", {
252 resourceContent <- list(newName = "Brand new shiny name")
253 resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
254 httpParser <- FakeHttpParser$new()
255 REST <- RESTService$new("token", "host", "webDavHost",
256 FakeHttpRequest$new(), httpParser)
258 REST$createResource("collections", resourceContent)
260 expect_that(httpParser$parserCallCount, equals(1))
263 test_that("createResource raises exception if response contains errors field", {
265 resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
266 serverResponse <- list(errors = 404)
267 resourceContent <- list(newName = "Brand new shiny name")
268 REST <- RESTService$new("token", "host", "webDavHost",
269 FakeHttpRequest$new(NULL, serverResponse),
270 FakeHttpParser$new())
272 expect_that(REST$createResource("collections", resourceContent),
276 test_that("create calls REST service properly", {
278 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
279 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
280 fakeHttp <- FakeHttpRequest$new(expectedURL)
281 fakeHttpParser <- FakeHttpParser$new()
283 REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
284 fakeHttp, fakeHttpParser)
286 REST$create("file", uuid)
288 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
289 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
290 expect_that(fakeHttp$numberOfPUTRequests, equals(1))
293 test_that("create raises exception if server response code is not between 200 and 300", {
295 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
297 response$status_code <- 404
298 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
300 REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
301 fakeHttp, HttpParser$new())
303 expect_that(REST$create("file", uuid),
304 throws_error("Server code: 404"))
307 test_that("delete calls REST service properly", {
309 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
310 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
311 fakeHttp <- FakeHttpRequest$new(expectedURL)
312 fakeHttpParser <- FakeHttpParser$new()
314 REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
315 fakeHttp, fakeHttpParser)
317 REST$delete("file", uuid)
319 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
320 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
321 expect_that(fakeHttp$numberOfDELETERequests, equals(1))
324 test_that("delete raises exception if server response code is not between 200 and 300", {
326 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
328 response$status_code <- 404
329 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
331 REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
332 fakeHttp, HttpParser$new())
334 expect_that(REST$delete("file", uuid),
335 throws_error("Server code: 404"))
338 test_that("move calls REST service properly", {
340 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
341 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
342 fakeHttp <- FakeHttpRequest$new(expectedURL)
343 fakeHttpParser <- FakeHttpParser$new()
345 REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
346 fakeHttp, fakeHttpParser)
348 REST$move("file", "newDestination/file", uuid)
350 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
351 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
352 expect_that(fakeHttp$requestHeaderContainsDestinationField, is_true())
353 expect_that(fakeHttp$numberOfMOVERequests, equals(1))
356 test_that("move raises exception if server response code is not between 200 and 300", {
358 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
360 response$status_code <- 404
361 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
363 REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
364 fakeHttp, HttpParser$new())
366 expect_that(REST$move("file", "newDestination/file", uuid),
367 throws_error("Server code: 404"))
370 test_that("getCollectionContent retreives correct content from WebDAV server", {
372 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
373 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc"
374 returnContent <- list()
375 returnContent$status_code <- 200
376 returnContent$content <- c("animal", "animal/dog", "ball")
378 fakeHttp <- FakeHttpRequest$new(expectedURL, returnContent)
380 REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
381 fakeHttp, FakeHttpParser$new())
383 returnResult <- REST$getCollectionContent(uuid)
384 returnedContentMatchExpected <- all.equal(returnResult,
385 c("animal", "animal/dog", "ball"))
387 expect_that(returnedContentMatchExpected, is_true())
388 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
391 test_that("getCollectionContent raises exception if server returns empty response", {
393 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
395 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
397 REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
398 fakeHttp, FakeHttpParser$new())
400 expect_that(REST$getCollectionContent(uuid),
401 throws_error("Response is empty, request may be misconfigured"))
404 test_that("getCollectionContent parses server response", {
406 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
407 fakeHttpParser <- FakeHttpParser$new()
408 REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
409 FakeHttpRequest$new(), fakeHttpParser)
411 REST$getCollectionContent(uuid)
413 expect_that(fakeHttpParser$parserCallCount, equals(1))
416 test_that("getCollectionContent raises exception if server returns empty response", {
418 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
420 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
422 REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
423 fakeHttp, FakeHttpParser$new())
425 expect_that(REST$getCollectionContent(uuid),
426 throws_error("Response is empty, request may be misconfigured"))
429 test_that(paste("getCollectionContent raises exception if server",
430 "response code is not between 200 and 300"), {
432 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
434 response$status_code <- 404
435 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
437 REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
438 fakeHttp, HttpParser$new())
440 expect_that(REST$getCollectionContent(uuid),
441 throws_error("Server code: 404"))
445 test_that("getResourceSize calls REST service properly", {
447 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
448 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
450 response$status_code <- 200
451 response$content <- c(6, 2, 931, 12003)
452 fakeHttp <- FakeHttpRequest$new(expectedURL, response)
454 REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
455 fakeHttp, FakeHttpParser$new())
457 returnResult <- REST$getResourceSize("file", uuid)
458 returnedContentMatchExpected <- all.equal(returnResult,
461 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
462 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
463 expect_that(returnedContentMatchExpected, is_true())
466 test_that("getResourceSize raises exception if server returns empty response", {
468 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
470 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
472 REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
473 fakeHttp, FakeHttpParser$new())
475 expect_that(REST$getResourceSize("file", uuid),
476 throws_error("Response is empty, request may be misconfigured"))
479 test_that(paste("getResourceSize raises exception if server",
480 "response code is not between 200 and 300"), {
482 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
484 response$status_code <- 404
485 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
487 REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
488 fakeHttp, HttpParser$new())
490 expect_that(REST$getResourceSize("file", uuid),
491 throws_error("Server code: 404"))
494 test_that("getResourceSize parses server response", {
496 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
497 fakeHttpParser <- FakeHttpParser$new()
498 REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
499 FakeHttpRequest$new(), fakeHttpParser)
501 REST$getResourceSize("file", uuid)
503 expect_that(fakeHttpParser$parserCallCount, equals(1))
506 test_that("read calls REST service properly", {
508 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
509 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
510 serverResponse <- list()
511 serverResponse$status_code <- 200
512 serverResponse$content <- "file content"
514 fakeHttp <- FakeHttpRequest$new(expectedURL, serverResponse)
516 REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
517 fakeHttp, FakeHttpParser$new())
519 returnResult <- REST$read("file", uuid, "text", 1024, 512)
521 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
522 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
523 expect_that(fakeHttp$requestHeaderContainsRangeField, is_true())
524 expect_that(returnResult, equals("file content"))
527 test_that("read raises exception if server response code is not between 200 and 300", {
529 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
531 response$status_code <- 404
532 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
534 REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
535 fakeHttp, HttpParser$new())
537 expect_that(REST$read("file", uuid),
538 throws_error("Server code: 404"))
541 test_that("read raises exception if contentType is not valid", {
543 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
544 fakeHttp <- FakeHttpRequest$new()
546 REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
547 fakeHttp, HttpParser$new())
549 expect_that(REST$read("file", uuid, "some invalid content type"),
550 throws_error("Invalid contentType. Please use text or raw."))
553 test_that("read parses server response", {
555 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
556 fakeHttpParser <- FakeHttpParser$new()
557 REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
558 FakeHttpRequest$new(), fakeHttpParser)
560 REST$read("file", uuid, "text", 1024, 512)
562 expect_that(fakeHttpParser$parserCallCount, equals(1))
565 test_that("write calls REST service properly", {
567 fileContent <- "new file content"
568 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
569 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
570 fakeHttp <- FakeHttpRequest$new(expectedURL)
572 REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
573 fakeHttp, FakeHttpParser$new())
575 REST$write("file", uuid, fileContent, "text/html")
577 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
578 expect_that(fakeHttp$requestBodyIsProvided, is_true())
579 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
580 expect_that(fakeHttp$requestHeaderContainsContentTypeField, is_true())
583 test_that("write raises exception if server response code is not between 200 and 300", {
585 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
586 fileContent <- "new file content"
588 response$status_code <- 404
589 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
591 REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
592 fakeHttp, HttpParser$new())
594 expect_that(REST$write("file", uuid, fileContent, "text/html"),
595 throws_error("Server code: 404"))