Merge branch 'master' of git.curoverse.com:arvados into 11876-r-sdk
[arvados.git] / sdk / R / tests / testthat / test-RESTService.R
1 source("fakes/FakeArvados.R")
2 source("fakes/FakeHttpRequest.R")
3 source("fakes/FakeHttpParser.R")
4
5 context("REST service")
6
7 test_that("getWebDavHostName calls REST service properly", {
8
9     expectedURL <- "https://host/discovery/v1/apis/arvados/v1/rest"
10     serverResponse <- list(keepWebServiceUrl = "https://myWebDavServer.com")
11     httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
12
13     REST <- RESTService$new("token", "host", NULL,
14                             httpRequest, FakeHttpParser$new())
15
16     REST$getWebDavHostName()
17
18     expect_that(httpRequest$URLIsProperlyConfigured, is_true())
19     expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
20     expect_that(httpRequest$numberOfGETRequests, equals(1))
21 }) 
22
23 test_that("getWebDavHostName returns webDAV host name properly", {
24
25     serverResponse <- list(keepWebServiceUrl = "https://myWebDavServer.com")
26     httpRequest <- FakeHttpRequest$new(expectedURL = NULL, serverResponse)
27
28     REST <- RESTService$new("token", "host", NULL,
29                             httpRequest, FakeHttpParser$new())
30
31     expect_that("https://myWebDavServer.com", equals(REST$getWebDavHostName())) 
32 }) 
33
34 test_that("getResource calls REST service properly", {
35
36     serverResponse <- NULL
37     resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
38     expectedURL    <- paste0("https://host/arvados/v1/collections/", resourceUUID)
39
40     httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
41
42     REST <- RESTService$new("token", "host", "webDavHost",
43                             httpRequest, FakeHttpParser$new())
44
45     REST$getResource("collections", resourceUUID)
46
47     expect_that(httpRequest$URLIsProperlyConfigured, is_true())
48     expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
49     expect_that(httpRequest$numberOfGETRequests, equals(1))
50 }) 
51
52 test_that("getResource parses server response", {
53
54     resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
55     httpParser <- FakeHttpParser$new()
56     REST <- RESTService$new("token", "host", "webDavHost",
57                             FakeHttpRequest$new(), httpParser)
58
59     REST$getResource("collections", resourceUUID)
60
61     expect_that(httpParser$parserCallCount, equals(1))
62 }) 
63
64 test_that("getResource raises exception if response contains errors field", {
65
66     resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
67     serverResponse <- list(errors = 404)
68
69     REST <- RESTService$new("token", "host", "webDavHost",
70                             FakeHttpRequest$new(NULL, serverResponse),
71                             FakeHttpParser$new())
72     
73     expect_that(REST$getResource("collections", resourceUUID), throws_error(404))
74 }) 
75
76 test_that("listResources calls REST service properly", {
77
78     serverResponse <- NULL
79     expectedURL    <- paste0("https://host/arvados/v1/collections")
80     httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
81
82     REST <- RESTService$new("token", "host", "webDavHost",
83                             httpRequest, FakeHttpParser$new())
84
85     REST$listResources("collections")
86
87     expect_that(httpRequest$URLIsProperlyConfigured, is_true())
88     expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
89     expect_that(httpRequest$numberOfGETRequests, equals(1))
90 }) 
91
92 test_that("listResources parses server response", {
93
94     httpParser <- FakeHttpParser$new()
95     REST <- RESTService$new("token", "host", "webDavHost",
96                             FakeHttpRequest$new(), httpParser)
97
98     REST$listResources("collections")
99
100     expect_that(httpParser$parserCallCount, equals(1))
101 }) 
102
103 test_that("listResources raises exception if response contains errors field", {
104
105     serverResponse <- list(errors = 404)
106
107     REST <- RESTService$new("token", "host", "webDavHost",
108                             FakeHttpRequest$new(NULL, serverResponse),
109                             FakeHttpParser$new())
110     
111     expect_that(REST$listResources("collections"), throws_error(404))
112 }) 
113
114 test_that("fetchAllItems always returns all resource items from server", {
115
116     expectedURL <- NULL
117     serverResponse <- list(items_available = 8,
118                            items = list("collection1",
119                                         "collection2",
120                                         "collection3",
121                                         "collection4",
122                                         "collection5",
123                                         "collection6",
124                                         "collection7",
125                                         "collection8"))
126
127     httpParser <- FakeHttpParser$new()
128     httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
129     httpRequest$serverMaxElementsPerRequest <- 3
130
131     REST <- RESTService$new("token", "host", "webDavHost",
132                             httpRequest, httpParser)
133
134     result <- REST$fetchAllItems(NULL, NULL)
135
136     expect_that(length(result), equals(8))
137     expect_that(httpRequest$numberOfGETRequests, equals(3))
138     expect_that(httpParser$parserCallCount, equals(3))
139 }) 
140
141 test_that("deleteResource calls REST service properly", {
142
143     serverResponse <- NULL
144     resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
145     expectedURL    <- paste0("https://host/arvados/v1/collections/", resourceUUID)
146
147     httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
148
149     REST <- RESTService$new("token", "host", "webDavHost",
150                             httpRequest, FakeHttpParser$new())
151
152     REST$deleteResource("collections", resourceUUID)
153
154     expect_that(httpRequest$URLIsProperlyConfigured, is_true())
155     expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
156     expect_that(httpRequest$numberOfDELETERequests, equals(1))
157 }) 
158
159 test_that("deleteCollection parses server response", {
160
161     resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
162     httpParser <- FakeHttpParser$new()
163     REST <- RESTService$new("token", "host", "webDavHost",
164                             FakeHttpRequest$new(), httpParser)
165
166     REST$deleteResource("collections", resourceUUID)
167
168     expect_that(httpParser$parserCallCount, equals(1))
169 }) 
170
171 test_that("deleteCollection raises exception if response contains errors field", {
172
173     resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc"
174     serverResponse <- list(errors = 404)
175
176     REST <- RESTService$new("token", "host", "webDavHost",
177                             FakeHttpRequest$new(NULL, serverResponse),
178                             FakeHttpParser$new())
179     
180     expect_that(REST$deleteResource("collections", resourceUUID), throws_error(404))
181 }) 
182
183 test_that("updateResource calls REST service properly", {
184
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")
189
190     httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
191
192     REST <- RESTService$new("token", "host", "webDavHost",
193                             httpRequest, FakeHttpParser$new())
194
195     REST$updateResource("collections", resourceUUID, newResourceContent)
196
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))
201 }) 
202
203 test_that("updateResource parses server response", {
204
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)
210
211     REST$updateResource("collections", resourceUUID, newResourceContent)
212
213     expect_that(httpParser$parserCallCount, equals(1))
214 }) 
215
216 test_that("updateResource raises exception if response contains errors field", {
217
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())
224     
225     expect_that(REST$updateResource("collections", resourceUUID, newResourceContent),
226                 throws_error(404))
227 }) 
228
229 test_that("createResource calls REST service properly", {
230
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")
236
237     httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
238
239     REST <- RESTService$new("token", "host", "webDavHost",
240                             httpRequest, FakeHttpParser$new())
241
242     REST$createResource("collections", resourceContent)
243
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))
248 }) 
249
250 test_that("createResource parses server response", {
251
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)
257
258     REST$createResource("collections", resourceContent)
259
260     expect_that(httpParser$parserCallCount, equals(1))
261 }) 
262
263 test_that("createResource raises exception if response contains errors field", {
264
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())
271     
272     expect_that(REST$createResource("collections", resourceContent),
273                 throws_error(404))
274 }) 
275
276 test_that("create calls REST service properly", {
277
278     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
279     expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
280     fakeHttp <- FakeHttpRequest$new(expectedURL)
281     fakeHttpParser <- FakeHttpParser$new()
282
283     REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
284                             fakeHttp, fakeHttpParser)
285
286     REST$create("file", uuid)
287
288     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
289     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
290     expect_that(fakeHttp$numberOfPUTRequests, equals(1))
291 }) 
292
293 test_that("create raises exception if server response code is not between 200 and 300", {
294
295     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
296     response <- list()
297     response$status_code <- 404
298     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
299
300     REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
301                             fakeHttp, HttpParser$new())
302
303     expect_that(REST$create("file", uuid),
304                 throws_error("Server code: 404"))
305 }) 
306
307 test_that("delete calls REST service properly", {
308
309     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
310     expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
311     fakeHttp <- FakeHttpRequest$new(expectedURL)
312     fakeHttpParser <- FakeHttpParser$new()
313
314     REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
315                             fakeHttp, fakeHttpParser)
316
317     REST$delete("file", uuid)
318
319     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
320     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
321     expect_that(fakeHttp$numberOfDELETERequests, equals(1))
322 }) 
323
324 test_that("delete raises exception if server response code is not between 200 and 300", {
325
326     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
327     response <- list()
328     response$status_code <- 404
329     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
330
331     REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
332                             fakeHttp, HttpParser$new())
333
334     expect_that(REST$delete("file", uuid),
335                 throws_error("Server code: 404"))
336 }) 
337
338 test_that("move calls REST service properly", {
339
340     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
341     expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
342     fakeHttp <- FakeHttpRequest$new(expectedURL)
343     fakeHttpParser <- FakeHttpParser$new()
344
345     REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
346                             fakeHttp, fakeHttpParser)
347
348     REST$move("file", "newDestination/file", uuid)
349
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))
354 }) 
355
356 test_that("move raises exception if server response code is not between 200 and 300", {
357
358     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
359     response <- list()
360     response$status_code <- 404
361     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
362
363     REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
364                             fakeHttp, HttpParser$new())
365
366     expect_that(REST$move("file", "newDestination/file", uuid),
367                 throws_error("Server code: 404"))
368 }) 
369
370 test_that("getCollectionContent retreives correct content from WebDAV server", {
371
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")
377
378     fakeHttp <- FakeHttpRequest$new(expectedURL, returnContent)
379
380     REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
381                             fakeHttp, FakeHttpParser$new())
382
383     returnResult <- REST$getCollectionContent(uuid)
384     returnedContentMatchExpected <- all.equal(returnResult,
385                                               c("animal", "animal/dog", "ball"))
386
387     expect_that(returnedContentMatchExpected, is_true())
388     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
389 }) 
390
391 test_that("getCollectionContent raises exception if server returns empty response", {
392
393     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
394     response <- ""
395     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
396
397     REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
398                             fakeHttp, FakeHttpParser$new())
399
400     expect_that(REST$getCollectionContent(uuid),
401                 throws_error("Response is empty, request may be misconfigured"))
402 }) 
403
404 test_that("getCollectionContent parses server response", {
405
406     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
407     fakeHttpParser <- FakeHttpParser$new()
408     REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
409                             FakeHttpRequest$new(), fakeHttpParser)
410
411     REST$getCollectionContent(uuid)
412
413     expect_that(fakeHttpParser$parserCallCount, equals(1))
414 }) 
415
416 test_that("getCollectionContent raises exception if server returns empty response", {
417
418     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
419     response <- ""
420     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
421
422     REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
423                             fakeHttp, FakeHttpParser$new())
424
425     expect_that(REST$getCollectionContent(uuid),
426                 throws_error("Response is empty, request may be misconfigured"))
427 }) 
428
429 test_that(paste("getCollectionContent raises exception if server",
430                 "response code is not between 200 and 300"), {
431
432     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
433     response <- list()
434     response$status_code <- 404
435     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
436
437     REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
438                             fakeHttp, HttpParser$new())
439
440     expect_that(REST$getCollectionContent(uuid),
441                 throws_error("Server code: 404"))
442 }) 
443
444
445 test_that("getResourceSize calls REST service properly", {
446
447     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
448     expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
449     response <- list()
450     response$status_code <- 200
451     response$content <- c(6, 2, 931, 12003)
452     fakeHttp <- FakeHttpRequest$new(expectedURL, response)
453
454     REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
455                             fakeHttp, FakeHttpParser$new())
456
457     returnResult <- REST$getResourceSize("file", uuid)
458     returnedContentMatchExpected <- all.equal(returnResult,
459                                               c(6, 2, 931, 12003))
460
461     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
462     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
463     expect_that(returnedContentMatchExpected, is_true())
464 }) 
465
466 test_that("getResourceSize raises exception if server returns empty response", {
467
468     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
469     response <- ""
470     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
471
472     REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
473                             fakeHttp, FakeHttpParser$new())
474
475     expect_that(REST$getResourceSize("file", uuid),
476                 throws_error("Response is empty, request may be misconfigured"))
477 }) 
478
479 test_that(paste("getResourceSize raises exception if server",
480                 "response code is not between 200 and 300"), {
481
482     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
483     response <- list()
484     response$status_code <- 404
485     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
486
487     REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
488                             fakeHttp, HttpParser$new())
489
490     expect_that(REST$getResourceSize("file", uuid),
491                 throws_error("Server code: 404"))
492 }) 
493
494 test_that("getResourceSize parses server response", {
495
496     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
497     fakeHttpParser <- FakeHttpParser$new()
498     REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
499                             FakeHttpRequest$new(), fakeHttpParser)
500
501     REST$getResourceSize("file", uuid)
502
503     expect_that(fakeHttpParser$parserCallCount, equals(1))
504 }) 
505
506 test_that("read calls REST service properly", {
507
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"
513
514     fakeHttp <- FakeHttpRequest$new(expectedURL, serverResponse)
515
516     REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
517                             fakeHttp, FakeHttpParser$new())
518
519     returnResult <- REST$read("file", uuid, "text", 1024, 512)
520
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"))
525 }) 
526
527 test_that("read raises exception if server response code is not between 200 and 300", {
528
529     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
530     response <- list()
531     response$status_code <- 404
532     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
533
534     REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
535                             fakeHttp, HttpParser$new())
536
537     expect_that(REST$read("file", uuid),
538                 throws_error("Server code: 404"))
539 }) 
540
541 test_that("read raises exception if contentType is not valid", {
542
543     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
544     fakeHttp <- FakeHttpRequest$new()
545
546     REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
547                             fakeHttp, HttpParser$new())
548
549     expect_that(REST$read("file", uuid, "some invalid content type"),
550                 throws_error("Invalid contentType. Please use text or raw."))
551 }) 
552
553 test_that("read parses server response", {
554
555     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
556     fakeHttpParser <- FakeHttpParser$new()
557     REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
558                             FakeHttpRequest$new(), fakeHttpParser)
559
560     REST$read("file", uuid, "text", 1024, 512)
561
562     expect_that(fakeHttpParser$parserCallCount, equals(1))
563 }) 
564
565 test_that("write calls REST service properly", {
566
567     fileContent <- "new file content" 
568     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
569     expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
570     fakeHttp <- FakeHttpRequest$new(expectedURL)
571
572     REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
573                             fakeHttp, FakeHttpParser$new())
574
575     REST$write("file", uuid, fileContent, "text/html")
576
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())
581 }) 
582
583 test_that("write raises exception if server response code is not between 200 and 300", {
584
585     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
586     fileContent <- "new file content" 
587     response <- list()
588     response$status_code <- 404
589     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
590
591     REST <- RESTService$new("token", "https://host/", "https://webDavHost/",
592                             fakeHttp, HttpParser$new())
593
594     expect_that(REST$write("file", uuid, fileContent, "text/html"),
595                 throws_error("Server code: 404"))
596 })