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",
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",
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",
43 httpRequest, FakeHttpParser$new(),
46 REST$getResource("collections", resourceUUID)
48 expect_that(httpRequest$URLIsProperlyConfigured, is_true())
49 expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
50 expect_that(httpRequest$numberOfGETRequests, equals(1))
53 test_that("getResource parses server response", {
55 resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
56 httpParser <- FakeHttpParser$new()
57 REST <- RESTService$new("token", "host",
58 FakeHttpRequest$new(), httpParser,
61 REST$getResource("collections", resourceUUID)
63 expect_that(httpParser$parserCallCount, equals(1))
66 test_that("getResource raises exception if response contains errors field", {
68 resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
69 serverResponse <- list(errors = 404)
71 REST <- RESTService$new("token", "host",
72 FakeHttpRequest$new(NULL, serverResponse),
76 expect_that(REST$getResource("collections", resourceUUID), throws_error("404", fixed = TRUE))
79 test_that("listResources calls REST service properly", {
81 serverResponse <- NULL
82 expectedURL <- paste0("https://host/arvados/v1/collections")
83 httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
85 REST <- RESTService$new("token", "host",
86 httpRequest, FakeHttpParser$new(),
89 REST$listResources("collections")
91 expect_that(httpRequest$URLIsProperlyConfigured, is_true())
92 expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
93 expect_that(httpRequest$numberOfGETRequests, equals(1))
96 test_that("listResources parses server response", {
98 httpParser <- FakeHttpParser$new()
99 REST <- RESTService$new("token", "host",
100 FakeHttpRequest$new(), httpParser,
103 REST$listResources("collections")
105 expect_that(httpParser$parserCallCount, equals(1))
108 test_that("listResources raises exception if response contains errors field", {
110 serverResponse <- list(errors = 404)
112 REST <- RESTService$new("token", "host",
113 FakeHttpRequest$new(NULL, serverResponse),
114 FakeHttpParser$new(),
117 expect_that(REST$listResources("collections"), throws_error("404", fixed = TRUE))
120 test_that("fetchAllItems always returns all resource items from server", {
123 serverResponse <- list(items_available = 8,
124 items = list("collection1",
133 httpParser <- FakeHttpParser$new()
134 httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
135 httpRequest$serverMaxElementsPerRequest <- 3
137 REST <- RESTService$new("token", "host",
138 httpRequest, httpParser,
141 result <- REST$fetchAllItems(NULL, NULL)
143 expect_that(length(result), equals(8))
144 expect_that(httpRequest$numberOfGETRequests, equals(3))
145 expect_that(httpParser$parserCallCount, equals(3))
148 test_that("deleteResource calls REST service properly", {
150 serverResponse <- NULL
151 resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
152 expectedURL <- paste0("https://host/arvados/v1/collections/", resourceUUID)
154 httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
156 REST <- RESTService$new("token", "host",
157 httpRequest, FakeHttpParser$new(),
160 REST$deleteResource("collections", resourceUUID)
162 expect_that(httpRequest$URLIsProperlyConfigured, is_true())
163 expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
164 expect_that(httpRequest$numberOfDELETERequests, equals(1))
167 test_that("deleteCollection parses server response", {
169 resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
170 httpParser <- FakeHttpParser$new()
171 REST <- RESTService$new("token", "host",
172 FakeHttpRequest$new(), httpParser,
175 REST$deleteResource("collections", resourceUUID)
177 expect_that(httpParser$parserCallCount, equals(1))
180 test_that("deleteCollection raises exception if response contains errors field", {
182 resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
183 serverResponse <- list(errors = 404)
185 REST <- RESTService$new("token", "host",
186 FakeHttpRequest$new(NULL, serverResponse),
187 FakeHttpParser$new(),
190 expect_that(REST$deleteResource("collections", resourceUUID), throws_error("404", fixed = TRUE))
193 test_that("updateResource calls REST service properly", {
195 serverResponse <- NULL
196 resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
197 expectedURL <- paste0("https://host/arvados/v1/collections/", resourceUUID)
198 newResourceContent <- list(newName = "Brand new shiny name")
200 httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
202 REST <- RESTService$new("token", "host",
203 httpRequest, FakeHttpParser$new(),
206 REST$updateResource("collections", resourceUUID, newResourceContent)
208 expect_that(httpRequest$URLIsProperlyConfigured, is_true())
209 expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
210 expect_that(httpRequest$JSONEncodedBodyIsProvided, is_true())
211 expect_that(httpRequest$numberOfPUTRequests, equals(1))
214 test_that("updateResource parses server response", {
216 newResourceContent <- list(newName = "Brand new shiny name")
217 resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
218 httpParser <- FakeHttpParser$new()
219 REST <- RESTService$new("token", "host",
220 FakeHttpRequest$new(), httpParser,
223 REST$updateResource("collections", resourceUUID, newResourceContent)
225 expect_that(httpParser$parserCallCount, equals(1))
228 test_that("updateResource raises exception if response contains errors field", {
230 resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
231 serverResponse <- list(errors = 404)
232 newResourceContent <- list(newName = "Brand new shiny name")
233 REST <- RESTService$new("token", "host",
234 FakeHttpRequest$new(NULL, serverResponse),
235 FakeHttpParser$new(),
238 expect_that(REST$updateResource("collections", resourceUUID, newResourceContent),
239 throws_error("404", fixed = TRUE))
242 test_that("createResource calls REST service properly", {
244 resourceContent <- list(name = "My favorite collection")
245 serverResponse <- NULL
246 resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
247 expectedURL <- "https://host/arvados/v1/collections"
248 newResourceContent <- list(newName = "Brand new shiny name")
250 httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
252 REST <- RESTService$new("token", "host",
253 httpRequest, FakeHttpParser$new(),
256 REST$createResource("collections", resourceContent)
258 expect_that(httpRequest$URLIsProperlyConfigured, is_true())
259 expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
260 expect_that(httpRequest$JSONEncodedBodyIsProvided, is_true())
261 expect_that(httpRequest$numberOfPOSTRequests, equals(1))
264 test_that("createResource parses server response", {
266 resourceContent <- list(newName = "Brand new shiny name")
267 resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
268 httpParser <- FakeHttpParser$new()
269 REST <- RESTService$new("token", "host",
270 FakeHttpRequest$new(), httpParser,
273 REST$createResource("collections", resourceContent)
275 expect_that(httpParser$parserCallCount, equals(1))
278 test_that("createResource raises exception if response contains errors field", {
280 resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
281 serverResponse <- list(errors = 404)
282 resourceContent <- list(newName = "Brand new shiny name")
283 REST <- RESTService$new("token", "host",
284 FakeHttpRequest$new(NULL, serverResponse),
285 FakeHttpParser$new(),
288 expect_that(REST$createResource("collections", resourceContent),
289 throws_error("404", fixed = TRUE))
292 test_that("create calls REST service properly", {
294 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
295 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
296 fakeHttp <- FakeHttpRequest$new(expectedURL)
297 fakeHttpParser <- FakeHttpParser$new()
299 REST <- RESTService$new("token", "https://host/",
300 fakeHttp, fakeHttpParser,
301 0, "https://webDavHost/")
303 REST$create("file", uuid)
305 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
306 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
307 expect_that(fakeHttp$numberOfPUTRequests, equals(1))
310 test_that("create raises exception if server response code is not between 200 and 300", {
312 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
314 response$status_code <- 404
315 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
317 REST <- RESTService$new("token", "https://host/",
318 fakeHttp, HttpParser$new(),
319 0, "https://webDavHost/")
321 expect_that(REST$create("file", uuid),
322 throws_error("Server code: 404"))
325 test_that("delete calls REST service properly", {
327 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
328 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
329 fakeHttp <- FakeHttpRequest$new(expectedURL)
330 fakeHttpParser <- FakeHttpParser$new()
332 REST <- RESTService$new("token", "https://host/",
333 fakeHttp, fakeHttpParser,
334 0, "https://webDavHost/")
336 REST$delete("file", uuid)
338 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
339 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
340 expect_that(fakeHttp$numberOfDELETERequests, equals(1))
343 test_that("delete raises exception if server response code is not between 200 and 300", {
345 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
347 response$status_code <- 404
348 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
350 REST <- RESTService$new("token", "https://host/",
351 fakeHttp, HttpParser$new(),
352 0, "https://webDavHost/")
354 expect_that(REST$delete("file", uuid),
355 throws_error("Server code: 404"))
358 test_that("move calls REST service properly", {
360 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
361 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
362 fakeHttp <- FakeHttpRequest$new(expectedURL)
363 fakeHttpParser <- FakeHttpParser$new()
365 REST <- RESTService$new("token", "https://host/",
366 fakeHttp, fakeHttpParser,
367 0, "https://webDavHost/")
369 REST$move("file", "newDestination/file", uuid)
371 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
372 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
373 expect_that(fakeHttp$requestHeaderContainsDestinationField, is_true())
374 expect_that(fakeHttp$numberOfMOVERequests, equals(1))
377 test_that("move raises exception if server response code is not between 200 and 300", {
379 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
381 response$status_code <- 404
382 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
384 REST <- RESTService$new("token", "https://host/",
385 fakeHttp, HttpParser$new(),
386 0, "https://webDavHost/")
388 expect_that(REST$move("file", "newDestination/file", uuid),
389 throws_error("Server code: 404"))
392 test_that("getCollectionContent retreives correct content from WebDAV server", {
394 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
395 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc"
396 returnContent <- list()
397 returnContent$status_code <- 200
398 returnContent$content <- c("animal", "animal/dog", "ball")
400 fakeHttp <- FakeHttpRequest$new(expectedURL, returnContent)
402 REST <- RESTService$new("token", "https://host/",
403 fakeHttp, FakeHttpParser$new(),
404 0, "https://webDavHost/")
406 returnResult <- REST$getCollectionContent(uuid)
407 returnedContentMatchExpected <- all.equal(returnResult,
408 c("animal", "animal/dog", "ball"))
410 expect_that(returnedContentMatchExpected, is_true())
411 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
414 test_that("getCollectionContent raises exception if server returns empty response", {
416 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
418 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
420 REST <- RESTService$new("token", "https://host/",
421 fakeHttp, FakeHttpParser$new(),
422 0, "https://webDavHost/")
424 expect_that(REST$getCollectionContent(uuid),
425 throws_error("Response is empty, request may be misconfigured"))
428 test_that("getCollectionContent parses server response", {
430 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
431 fakeHttpParser <- FakeHttpParser$new()
432 REST <- RESTService$new("token", "https://host/",
433 FakeHttpRequest$new(), fakeHttpParser,
434 0, "https://webDavHost/")
436 REST$getCollectionContent(uuid)
438 expect_that(fakeHttpParser$parserCallCount, equals(1))
441 test_that("getCollectionContent raises exception if server returns empty response", {
443 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
445 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
447 REST <- RESTService$new("token", "https://host/",
448 fakeHttp, FakeHttpParser$new(),
449 0, "https://webDavHost/")
451 expect_that(REST$getCollectionContent(uuid),
452 throws_error("Response is empty, request may be misconfigured"))
455 test_that(paste("getCollectionContent raises exception if server",
456 "response code is not between 200 and 300"), {
458 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
460 response$status_code <- 404
461 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
463 REST <- RESTService$new("token", "https://host/",
464 fakeHttp, HttpParser$new(),
465 0, "https://webDavHost/")
467 expect_that(REST$getCollectionContent(uuid),
468 throws_error("Server code: 404"))
472 test_that("getResourceSize calls REST service properly", {
474 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
475 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
477 response$status_code <- 200
478 response$content <- c(6, 2, 931, 12003)
479 fakeHttp <- FakeHttpRequest$new(expectedURL, response)
481 REST <- RESTService$new("token", "https://host/",
482 fakeHttp, FakeHttpParser$new(),
483 0, "https://webDavHost/")
485 returnResult <- REST$getResourceSize("file", uuid)
486 returnedContentMatchExpected <- all.equal(returnResult,
489 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
490 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
491 expect_that(returnedContentMatchExpected, is_true())
494 test_that("getResourceSize raises exception if server returns empty response", {
496 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
498 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
500 REST <- RESTService$new("token", "https://host/",
501 fakeHttp, FakeHttpParser$new(),
502 0, "https://webDavHost/")
504 expect_that(REST$getResourceSize("file", uuid),
505 throws_error("Response is empty, request may be misconfigured"))
508 test_that(paste("getResourceSize raises exception if server",
509 "response code is not between 200 and 300"), {
511 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
513 response$status_code <- 404
514 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
516 REST <- RESTService$new("token", "https://host/",
517 fakeHttp, HttpParser$new(),
518 0, "https://webDavHost/")
520 expect_that(REST$getResourceSize("file", uuid),
521 throws_error("Server code: 404"))
524 test_that("getResourceSize parses server response", {
526 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
527 fakeHttpParser <- FakeHttpParser$new()
528 REST <- RESTService$new("token", "https://host/",
529 FakeHttpRequest$new(), fakeHttpParser,
530 0, "https://webDavHost/")
532 REST$getResourceSize("file", uuid)
534 expect_that(fakeHttpParser$parserCallCount, equals(1))
537 test_that("read calls REST service properly", {
539 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
540 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
541 serverResponse <- list()
542 serverResponse$status_code <- 200
543 serverResponse$content <- "file content"
545 fakeHttp <- FakeHttpRequest$new(expectedURL, serverResponse)
547 REST <- RESTService$new("token", "https://host/",
548 fakeHttp, FakeHttpParser$new(),
549 0, "https://webDavHost/")
551 returnResult <- REST$read("file", uuid, "text", 1024, 512)
553 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
554 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
555 expect_that(fakeHttp$requestHeaderContainsRangeField, is_true())
556 expect_that(returnResult, equals("file content"))
559 test_that("read raises exception if server response code is not between 200 and 300", {
561 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
563 response$status_code <- 404
564 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
566 REST <- RESTService$new("token", "https://host/",
567 fakeHttp, HttpParser$new(),
568 0, "https://webDavHost/")
570 expect_that(REST$read("file", uuid),
571 throws_error("Server code: 404"))
574 test_that("read raises exception if contentType is not valid", {
576 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
577 fakeHttp <- FakeHttpRequest$new()
579 REST <- RESTService$new("token", "https://host/",
580 fakeHttp, HttpParser$new(),
581 0, "https://webDavHost/")
583 expect_that(REST$read("file", uuid, "some invalid content type"),
584 throws_error("Invalid contentType. Please use text or raw."))
587 test_that("read parses server response", {
589 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
590 fakeHttpParser <- FakeHttpParser$new()
591 REST <- RESTService$new("token", "https://host/",
592 FakeHttpRequest$new(), fakeHttpParser,
593 0, "https://webDavHost/")
595 REST$read("file", uuid, "text", 1024, 512)
597 expect_that(fakeHttpParser$parserCallCount, equals(1))
600 test_that("write calls REST service properly", {
602 fileContent <- "new file content"
603 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
604 expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
605 fakeHttp <- FakeHttpRequest$new(expectedURL)
607 REST <- RESTService$new("token", "https://host/",
608 fakeHttp, FakeHttpParser$new(),
609 0, "https://webDavHost/")
611 REST$write("file", uuid, fileContent, "text/html")
613 expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
614 expect_that(fakeHttp$requestBodyIsProvided, is_true())
615 expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
616 expect_that(fakeHttp$requestHeaderContainsContentTypeField, is_true())
619 test_that("write raises exception if server response code is not between 200 and 300", {
621 uuid <- "aaaaa-j7d0g-ccccccccccccccc"
622 fileContent <- "new file content"
624 response$status_code <- 404
625 fakeHttp <- FakeHttpRequest$new(serverResponse = response)
627 REST <- RESTService$new("token", "https://host/",
628 fakeHttp, HttpParser$new(),
629 0, "https://webDavHost/")
631 expect_that(REST$write("file", uuid, fileContent, "text/html"),
632 throws_error("Server code: 404"))