X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/8a2035547ad8bf6abad6a4a03bb0b59211a00932..8afc85aabb9563da4de17b0b5f7d4fe574e9ad8d:/sdk/R/tests/testthat/test-RESTService.R diff --git a/sdk/R/tests/testthat/test-RESTService.R b/sdk/R/tests/testthat/test-RESTService.R index d4f3c2c1c0..859b6180f3 100644 --- a/sdk/R/tests/testthat/test-RESTService.R +++ b/sdk/R/tests/testthat/test-RESTService.R @@ -1,3 +1,7 @@ +# Copyright (C) The Arvados Authors. All rights reserved. +# +# SPDX-License-Identifier: Apache-2.0 + source("fakes/FakeArvados.R") source("fakes/FakeHttpRequest.R") source("fakes/FakeHttpParser.R") @@ -31,264 +35,6 @@ test_that("getWebDavHostName returns webDAV host name properly", { expect_that("https://myWebDavServer.com", equals(REST$getWebDavHostName())) }) -test_that("getResource calls REST service properly", { - - serverResponse <- NULL - resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc" - expectedURL <- paste0("https://host/arvados/v1/collections/", resourceUUID) - - httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse) - - REST <- RESTService$new("token", "host", - httpRequest, FakeHttpParser$new(), - 0, "webDavHost") - - REST$getResource("collections", resourceUUID) - - expect_that(httpRequest$URLIsProperlyConfigured, is_true()) - expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true()) - expect_that(httpRequest$numberOfGETRequests, equals(1)) -}) - -test_that("getResource parses server response", { - - resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc" - httpParser <- FakeHttpParser$new() - REST <- RESTService$new("token", "host", - FakeHttpRequest$new(), httpParser, - 0, "webDavHost") - - REST$getResource("collections", resourceUUID) - - expect_that(httpParser$parserCallCount, equals(1)) -}) - -test_that("getResource raises exception if response contains errors field", { - - resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc" - serverResponse <- list(errors = 404) - - REST <- RESTService$new("token", "host", - FakeHttpRequest$new(NULL, serverResponse), - FakeHttpParser$new(), - 0, "webDavHost") - - expect_that(REST$getResource("collections", resourceUUID), throws_error("404", fixed = TRUE)) -}) - -test_that("listResources calls REST service properly", { - - serverResponse <- NULL - expectedURL <- paste0("https://host/arvados/v1/collections") - httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse) - - REST <- RESTService$new("token", "host", - httpRequest, FakeHttpParser$new(), - 0, "webDavHost") - - REST$listResources("collections") - - expect_that(httpRequest$URLIsProperlyConfigured, is_true()) - expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true()) - expect_that(httpRequest$numberOfGETRequests, equals(1)) -}) - -test_that("listResources parses server response", { - - httpParser <- FakeHttpParser$new() - REST <- RESTService$new("token", "host", - FakeHttpRequest$new(), httpParser, - 0, "webDavHost") - - REST$listResources("collections") - - expect_that(httpParser$parserCallCount, equals(1)) -}) - -test_that("listResources raises exception if response contains errors field", { - - serverResponse <- list(errors = 404) - - REST <- RESTService$new("token", "host", - FakeHttpRequest$new(NULL, serverResponse), - FakeHttpParser$new(), - 0, "webDavHost") - - expect_that(REST$listResources("collections"), throws_error("404", fixed = TRUE)) -}) - -test_that("fetchAllItems always returns all resource items from server", { - - expectedURL <- NULL - serverResponse <- list(items_available = 8, - items = list("collection1", - "collection2", - "collection3", - "collection4", - "collection5", - "collection6", - "collection7", - "collection8")) - - httpParser <- FakeHttpParser$new() - httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse) - httpRequest$serverMaxElementsPerRequest <- 3 - - REST <- RESTService$new("token", "host", - httpRequest, httpParser, - 0, "webDavHost") - - result <- REST$fetchAllItems(NULL, NULL) - - expect_that(length(result), equals(8)) - expect_that(httpRequest$numberOfGETRequests, equals(3)) - expect_that(httpParser$parserCallCount, equals(3)) -}) - -test_that("deleteResource calls REST service properly", { - - serverResponse <- NULL - resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc" - expectedURL <- paste0("https://host/arvados/v1/collections/", resourceUUID) - - httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse) - - REST <- RESTService$new("token", "host", - httpRequest, FakeHttpParser$new(), - 0, "webDavHost") - - REST$deleteResource("collections", resourceUUID) - - expect_that(httpRequest$URLIsProperlyConfigured, is_true()) - expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true()) - expect_that(httpRequest$numberOfDELETERequests, equals(1)) -}) - -test_that("deleteCollection parses server response", { - - resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc" - httpParser <- FakeHttpParser$new() - REST <- RESTService$new("token", "host", - FakeHttpRequest$new(), httpParser, - 0, "webDavHost") - - REST$deleteResource("collections", resourceUUID) - - expect_that(httpParser$parserCallCount, equals(1)) -}) - -test_that("deleteCollection raises exception if response contains errors field", { - - resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc" - serverResponse <- list(errors = 404) - - REST <- RESTService$new("token", "host", - FakeHttpRequest$new(NULL, serverResponse), - FakeHttpParser$new(), - 0, "webDavHost") - - expect_that(REST$deleteResource("collections", resourceUUID), throws_error("404", fixed = TRUE)) -}) - -test_that("updateResource calls REST service properly", { - - serverResponse <- NULL - resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc" - expectedURL <- paste0("https://host/arvados/v1/collections/", resourceUUID) - newResourceContent <- list(newName = "Brand new shiny name") - - httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse) - - REST <- RESTService$new("token", "host", - httpRequest, FakeHttpParser$new(), - 0, "webDavHost") - - REST$updateResource("collections", resourceUUID, newResourceContent) - - expect_that(httpRequest$URLIsProperlyConfigured, is_true()) - expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true()) - expect_that(httpRequest$JSONEncodedBodyIsProvided, is_true()) - expect_that(httpRequest$numberOfPUTRequests, equals(1)) -}) - -test_that("updateResource parses server response", { - - newResourceContent <- list(newName = "Brand new shiny name") - resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc" - httpParser <- FakeHttpParser$new() - REST <- RESTService$new("token", "host", - FakeHttpRequest$new(), httpParser, - 0, "webDavHost") - - REST$updateResource("collections", resourceUUID, newResourceContent) - - expect_that(httpParser$parserCallCount, equals(1)) -}) - -test_that("updateResource raises exception if response contains errors field", { - - resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc" - serverResponse <- list(errors = 404) - newResourceContent <- list(newName = "Brand new shiny name") - REST <- RESTService$new("token", "host", - FakeHttpRequest$new(NULL, serverResponse), - FakeHttpParser$new(), - 0, "webDavHost") - - expect_that(REST$updateResource("collections", resourceUUID, newResourceContent), - throws_error("404", fixed = TRUE)) -}) - -test_that("createResource calls REST service properly", { - - resourceContent <- list(name = "My favorite collection") - serverResponse <- NULL - resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc" - expectedURL <- "https://host/arvados/v1/collections" - newResourceContent <- list(newName = "Brand new shiny name") - - httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse) - - REST <- RESTService$new("token", "host", - httpRequest, FakeHttpParser$new(), - 0, "webDavHost") - - REST$createResource("collections", resourceContent) - - expect_that(httpRequest$URLIsProperlyConfigured, is_true()) - expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true()) - expect_that(httpRequest$JSONEncodedBodyIsProvided, is_true()) - expect_that(httpRequest$numberOfPOSTRequests, equals(1)) -}) - -test_that("createResource parses server response", { - - resourceContent <- list(newName = "Brand new shiny name") - resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc" - httpParser <- FakeHttpParser$new() - REST <- RESTService$new("token", "host", - FakeHttpRequest$new(), httpParser, - 0, "webDavHost") - - REST$createResource("collections", resourceContent) - - expect_that(httpParser$parserCallCount, equals(1)) -}) - -test_that("createResource raises exception if response contains errors field", { - - resourceUUID <- "aaaaa-j7d0g-ccccccccccccccc" - serverResponse <- list(errors = 404) - resourceContent <- list(newName = "Brand new shiny name") - REST <- RESTService$new("token", "host", - FakeHttpRequest$new(NULL, serverResponse), - FakeHttpParser$new(), - 0, "webDavHost") - - expect_that(REST$createResource("collections", resourceContent), - throws_error("404", fixed = TRUE)) -}) - test_that("create calls REST service properly", { uuid <- "aaaaa-j7d0g-ccccccccccccccc"