Merge branch 'master' of git.curoverse.com:arvados into 13076-r-autogen-api
[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",
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",
29                             httpRequest, FakeHttpParser$new())
30
31     expect_that("https://myWebDavServer.com", equals(REST$getWebDavHostName())) 
32 }) 
33
34 test_that("fetchAllItems always returns all resource items from server", {
35
36     expectedURL <- NULL
37     serverResponse <- list(items_available = 8,
38                            items = list("collection1",
39                                         "collection2",
40                                         "collection3",
41                                         "collection4",
42                                         "collection5",
43                                         "collection6",
44                                         "collection7",
45                                         "collection8"))
46
47     httpParser <- FakeHttpParser$new()
48     httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
49     httpRequest$serverMaxElementsPerRequest <- 3
50
51     REST <- RESTService$new("token", "host", 
52                             httpRequest, httpParser,
53                             0, "webDavHost")
54
55     result <- REST$fetchAllItems(NULL, NULL)
56
57     expect_that(length(result), equals(8))
58     expect_that(httpRequest$numberOfGETRequests, equals(3))
59     expect_that(httpParser$parserCallCount, equals(3))
60 }) 
61
62 test_that("create calls REST service properly", {
63
64     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
65     expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
66     fakeHttp <- FakeHttpRequest$new(expectedURL)
67     fakeHttpParser <- FakeHttpParser$new()
68
69     REST <- RESTService$new("token", "https://host/",
70                             fakeHttp, fakeHttpParser,
71                             0, "https://webDavHost/")
72
73     REST$create("file", uuid)
74
75     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
76     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
77     expect_that(fakeHttp$numberOfPUTRequests, equals(1))
78 }) 
79
80 test_that("create raises exception if server response code is not between 200 and 300", {
81
82     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
83     response <- list()
84     response$status_code <- 404
85     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
86
87     REST <- RESTService$new("token", "https://host/", 
88                             fakeHttp, HttpParser$new(),
89                             0, "https://webDavHost/")
90
91     expect_that(REST$create("file", uuid),
92                 throws_error("Server code: 404"))
93 }) 
94
95 test_that("delete calls REST service properly", {
96
97     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
98     expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
99     fakeHttp <- FakeHttpRequest$new(expectedURL)
100     fakeHttpParser <- FakeHttpParser$new()
101
102     REST <- RESTService$new("token", "https://host/", 
103                             fakeHttp, fakeHttpParser,
104                             0, "https://webDavHost/")
105
106     REST$delete("file", uuid)
107
108     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
109     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
110     expect_that(fakeHttp$numberOfDELETERequests, equals(1))
111 }) 
112
113 test_that("delete raises exception if server response code is not between 200 and 300", {
114
115     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
116     response <- list()
117     response$status_code <- 404
118     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
119
120     REST <- RESTService$new("token", "https://host/",
121                             fakeHttp, HttpParser$new(),
122                             0, "https://webDavHost/")
123
124     expect_that(REST$delete("file", uuid),
125                 throws_error("Server code: 404"))
126 }) 
127
128 test_that("move calls REST service properly", {
129
130     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
131     expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
132     fakeHttp <- FakeHttpRequest$new(expectedURL)
133     fakeHttpParser <- FakeHttpParser$new()
134
135     REST <- RESTService$new("token", "https://host/", 
136                             fakeHttp, fakeHttpParser,
137                             0, "https://webDavHost/")
138
139     REST$move("file", "newDestination/file", uuid)
140
141     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
142     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
143     expect_that(fakeHttp$requestHeaderContainsDestinationField, is_true())
144     expect_that(fakeHttp$numberOfMOVERequests, equals(1))
145 }) 
146
147 test_that("move raises exception if server response code is not between 200 and 300", {
148
149     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
150     response <- list()
151     response$status_code <- 404
152     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
153
154     REST <- RESTService$new("token", "https://host/", 
155                             fakeHttp, HttpParser$new(),
156                             0, "https://webDavHost/")
157
158     expect_that(REST$move("file", "newDestination/file", uuid),
159                 throws_error("Server code: 404"))
160 }) 
161
162 test_that("getCollectionContent retreives correct content from WebDAV server", {
163
164     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
165     expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc"
166     returnContent <- list()
167     returnContent$status_code <- 200
168     returnContent$content <- c("animal", "animal/dog", "ball")
169
170     fakeHttp <- FakeHttpRequest$new(expectedURL, returnContent)
171
172     REST <- RESTService$new("token", "https://host/", 
173                             fakeHttp, FakeHttpParser$new(),
174                             0, "https://webDavHost/")
175
176     returnResult <- REST$getCollectionContent(uuid)
177     returnedContentMatchExpected <- all.equal(returnResult,
178                                               c("animal", "animal/dog", "ball"))
179
180     expect_that(returnedContentMatchExpected, is_true())
181     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
182 }) 
183
184 test_that("getCollectionContent raises exception if server returns empty response", {
185
186     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
187     response <- ""
188     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
189
190     REST <- RESTService$new("token", "https://host/", 
191                             fakeHttp, FakeHttpParser$new(),
192                             0, "https://webDavHost/")
193
194     expect_that(REST$getCollectionContent(uuid),
195                 throws_error("Response is empty, request may be misconfigured"))
196 }) 
197
198 test_that("getCollectionContent parses server response", {
199
200     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
201     fakeHttpParser <- FakeHttpParser$new()
202     REST <- RESTService$new("token", "https://host/", 
203                             FakeHttpRequest$new(), fakeHttpParser,
204                             0, "https://webDavHost/")
205
206     REST$getCollectionContent(uuid)
207
208     expect_that(fakeHttpParser$parserCallCount, equals(1))
209 }) 
210
211 test_that("getCollectionContent raises exception if server returns empty response", {
212
213     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
214     response <- ""
215     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
216
217     REST <- RESTService$new("token", "https://host/", 
218                             fakeHttp, FakeHttpParser$new(),
219                             0, "https://webDavHost/")
220
221     expect_that(REST$getCollectionContent(uuid),
222                 throws_error("Response is empty, request may be misconfigured"))
223 }) 
224
225 test_that(paste("getCollectionContent raises exception if server",
226                 "response code is not between 200 and 300"), {
227
228     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
229     response <- list()
230     response$status_code <- 404
231     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
232
233     REST <- RESTService$new("token", "https://host/", 
234                             fakeHttp, HttpParser$new(),
235                             0, "https://webDavHost/")
236
237     expect_that(REST$getCollectionContent(uuid),
238                 throws_error("Server code: 404"))
239 }) 
240
241
242 test_that("getResourceSize calls REST service properly", {
243
244     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
245     expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
246     response <- list()
247     response$status_code <- 200
248     response$content <- c(6, 2, 931, 12003)
249     fakeHttp <- FakeHttpRequest$new(expectedURL, response)
250
251     REST <- RESTService$new("token", "https://host/",
252                             fakeHttp, FakeHttpParser$new(),
253                             0, "https://webDavHost/")
254
255     returnResult <- REST$getResourceSize("file", uuid)
256     returnedContentMatchExpected <- all.equal(returnResult,
257                                               c(6, 2, 931, 12003))
258
259     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
260     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
261     expect_that(returnedContentMatchExpected, is_true())
262 }) 
263
264 test_that("getResourceSize raises exception if server returns empty response", {
265
266     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
267     response <- ""
268     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
269
270     REST <- RESTService$new("token", "https://host/", 
271                             fakeHttp, FakeHttpParser$new(),
272                             0, "https://webDavHost/")
273
274     expect_that(REST$getResourceSize("file", uuid),
275                 throws_error("Response is empty, request may be misconfigured"))
276 }) 
277
278 test_that(paste("getResourceSize raises exception if server",
279                 "response code is not between 200 and 300"), {
280
281     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
282     response <- list()
283     response$status_code <- 404
284     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
285
286     REST <- RESTService$new("token", "https://host/", 
287                             fakeHttp, HttpParser$new(),
288                             0, "https://webDavHost/")
289
290     expect_that(REST$getResourceSize("file", uuid),
291                 throws_error("Server code: 404"))
292 }) 
293
294 test_that("getResourceSize parses server response", {
295
296     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
297     fakeHttpParser <- FakeHttpParser$new()
298     REST <- RESTService$new("token", "https://host/", 
299                             FakeHttpRequest$new(), fakeHttpParser,
300                             0, "https://webDavHost/")
301
302     REST$getResourceSize("file", uuid)
303
304     expect_that(fakeHttpParser$parserCallCount, equals(1))
305 }) 
306
307 test_that("read calls REST service properly", {
308
309     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
310     expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
311     serverResponse <- list()
312     serverResponse$status_code <- 200
313     serverResponse$content <- "file content"
314
315     fakeHttp <- FakeHttpRequest$new(expectedURL, serverResponse)
316
317     REST <- RESTService$new("token", "https://host/", 
318                             fakeHttp, FakeHttpParser$new(),
319                             0, "https://webDavHost/")
320
321     returnResult <- REST$read("file", uuid, "text", 1024, 512)
322
323     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
324     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
325     expect_that(fakeHttp$requestHeaderContainsRangeField, is_true())
326     expect_that(returnResult, equals("file content"))
327 }) 
328
329 test_that("read raises exception if server response code is not between 200 and 300", {
330
331     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
332     response <- list()
333     response$status_code <- 404
334     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
335
336     REST <- RESTService$new("token", "https://host/", 
337                             fakeHttp, HttpParser$new(),
338                             0, "https://webDavHost/")
339
340     expect_that(REST$read("file", uuid),
341                 throws_error("Server code: 404"))
342 }) 
343
344 test_that("read raises exception if contentType is not valid", {
345
346     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
347     fakeHttp <- FakeHttpRequest$new()
348
349     REST <- RESTService$new("token", "https://host/", 
350                             fakeHttp, HttpParser$new(),
351                             0, "https://webDavHost/")
352
353     expect_that(REST$read("file", uuid, "some invalid content type"),
354                 throws_error("Invalid contentType. Please use text or raw."))
355 }) 
356
357 test_that("read parses server response", {
358
359     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
360     fakeHttpParser <- FakeHttpParser$new()
361     REST <- RESTService$new("token", "https://host/", 
362                             FakeHttpRequest$new(), fakeHttpParser,
363                             0, "https://webDavHost/")
364
365     REST$read("file", uuid, "text", 1024, 512)
366
367     expect_that(fakeHttpParser$parserCallCount, equals(1))
368 }) 
369
370 test_that("write calls REST service properly", {
371
372     fileContent <- "new file content" 
373     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
374     expectedURL <- "https://webDavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
375     fakeHttp <- FakeHttpRequest$new(expectedURL)
376
377     REST <- RESTService$new("token", "https://host/", 
378                             fakeHttp, FakeHttpParser$new(),
379                             0, "https://webDavHost/")
380
381     REST$write("file", uuid, fileContent, "text/html")
382
383     expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
384     expect_that(fakeHttp$requestBodyIsProvided, is_true())
385     expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
386     expect_that(fakeHttp$requestHeaderContainsContentTypeField, is_true())
387 }) 
388
389 test_that("write raises exception if server response code is not between 200 and 300", {
390
391     uuid <- "aaaaa-j7d0g-ccccccccccccccc"
392     fileContent <- "new file content" 
393     response <- list()
394     response$status_code <- 404
395     fakeHttp <- FakeHttpRequest$new(serverResponse = response)
396
397     REST <- RESTService$new("token", "https://host/",
398                             fakeHttp, HttpParser$new(),
399                             0, "https://webDavHost/")
400
401     expect_that(REST$write("file", uuid, fileContent, "text/html"),
402                 throws_error("Server code: 404"))
403 })