token <- Sys.getenv("ARVADOS_API_TOKEN");
if(host == "" | token == "")
- stop("Please provide host name and authentification token or set
- ARVADOS_API_HOST and ARVADOS_API_TOKEN environmental variables.")
+ stop(paste0("Please provide host name and authentification token",
+ " or set ARVADOS_API_HOST and ARVADOS_API_TOKEN",
+ " environmental variables."))
version <- "v1"
host <- paste0("https://", host, "/arvados/", version, "/")
private$httpParser <- HttpParser$new()
private$token <- token
private$host <- host
+ private$rawHost <- host_name
},
getToken = function() private$token,
getHostName = function() private$host,
+ getHttpClient = function() private$http,
+ setHttpClient = function(newClient) private$http <- newClient,
+
+ getHttpParser = function() private$httpParser,
+ setHttpParser = function(newParser) private$httpParser <- newParser,
+
getWebDavHostName = function()
{
if(is.null(private$webDavHostName))
{
- discoveryDocumentURL <- paste0("https://", host,
+ discoveryDocumentURL <- paste0("https://", private$rawHost,
"/discovery/v1/apis/arvados/v1/rest")
headers <- list(Authorization = paste("OAuth2", private$token))
token = NULL,
host = NULL,
+ rawHost = NULL,
webDavHostName = NULL,
http = NULL,
httpParser = NULL,
--- /dev/null
+FakeHttpRequest <- R6::R6Class(
+
+ "FakeHttpRequest",
+
+ public = list(
+
+ content = NULL,
+ expectedURL = NULL,
+ URLIsProperlyConfigured = NULL,
+ requestHeaderContainsAuthorizationField = NULL,
+
+ numberOfGETRequests = NULL,
+ numberOfDELETERequests = NULL,
+
+ initialize = function(expectedURL = NULL, serverResponse = NULL)
+ {
+ self$content <- serverResponse
+ self$expectedURL <- expectedURL
+ self$requestHeaderContainsAuthorizationField <- FALSE
+ self$URLIsProperlyConfigured <- FALSE
+
+ self$numberOfGETRequests <- 0
+ self$numberOfDELETERequests <- 0
+ },
+
+ GET = function(url, headers = NULL, queryFilters = NULL, limit = NULL, offset = NULL)
+ {
+ private$validateURL(url)
+ private$validateHeaders(headers)
+ self$numberOfGETRequests <- self$numberOfGETRequests + 1
+
+ self$content
+ },
+
+ PUT = function(url, headers = NULL, body = NULL,
+ queryFilters = NULL, limit = NULL, offset = NULL)
+ {
+ self$content
+ },
+
+ POST = function(url, headers = NULL, body = NULL,
+ queryFilters = NULL, limit = NULL, offset = NULL)
+ {
+ self$content
+ },
+
+ DELETE = function(url, headers = NULL, body = NULL,
+ queryFilters = NULL, limit = NULL, offset = NULL)
+ {
+ private$validateURL(url)
+ private$validateHeaders(headers)
+ self$numberOfDELETERequests <- self$numberOfDELETERequests + 1
+ self$content
+ },
+
+ PROPFIND = function(url, headers = NULL)
+ {
+ self$content
+ },
+
+ MOVE = function(url, headers = NULL)
+ {
+ self$content
+ }
+ ),
+
+ private = list(
+
+ validateURL = function(url)
+ {
+ if(!is.null(self$expectedURL) && url == self$expectedURL)
+ self$URLIsProperlyConfigured <- TRUE
+ },
+
+ validateHeaders = function(headers)
+ {
+ if(!is.null(headers$Authorization))
+ self$requestHeaderContainsAuthorizationField <- TRUE
+ }
+ ),
+
+ cloneable = FALSE
+)
context("Arvados API")
-test_that("Arvados constructor will use environment variables if no parameters are passed to it", {
+source("fakes/FakeHttpRequest.R")
+source("fakes/FakeHttpParser.R")
+
+test_that("Constructor will use environment variables if no parameters are passed to it", {
Sys.setenv(ARVADOS_API_HOST = "environment_api_host")
Sys.setenv(ARVADOS_API_TOKEN = "environment_api_token")
equals(arv$getToken()))
})
-test_that("Arvados constructor preferes constructor fields over environment variables", {
+test_that("Constructor preferes constructor fields over environment variables", {
Sys.setenv(ARVADOS_API_HOST = "environment_api_host")
Sys.setenv(ARVADOS_API_TOKEN = "environment_api_token")
expect_that("constructor_api_token",
equals(arv$getToken()))
})
+
+test_that("Constructor raises exception if fields and environment variables are not provided", {
+
+ expect_that(Arvados$new(),
+ throws_error(paste0("Please provide host name and authentification token",
+ " or set ARVADOS_API_HOST and ARVADOS_API_TOKEN",
+ " environmental variables.")))
+})
+
+test_that("getWebDavHostName calls REST service properly", {
+
+ hostName <- "hostName"
+ token <- "token"
+ arv <- Arvados$new(token, hostName)
+
+ serverResponse <- list(keepWebServiceUrl = "https://myWebDavServer.com")
+ expectedURL <- paste0("https://", hostName,
+ "/discovery/v1/apis/arvados/v1/rest")
+
+ httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
+ arv$setHttpClient(httpRequest)
+ arv$setHttpParser(FakeHttpParser$new())
+
+ webDAVHostName <- arv$getWebDavHostName()
+
+ expect_that(httpRequest$URLIsProperlyConfigured, is_true())
+ expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
+ expect_that(httpRequest$numberOfGETRequests, equals(1))
+})
+
+test_that("getWebDavHostName returns webDAV host name properly", {
+
+ arv <- Arvados$new("token", "hostName")
+
+ serverResponse <- list(keepWebServiceUrl = "https://myWebDavServer.com")
+
+ httpRequest <- FakeHttpRequest$new(expectedURL = NULL, serverResponse)
+ arv$setHttpClient(httpRequest)
+ arv$setHttpParser(FakeHttpParser$new())
+
+ expect_that("https://myWebDavServer.com", equals(arv$getWebDavHostName()))
+})
+
+test_that("getCollection calls REST service properly", {
+
+ arv <- Arvados$new("token", "hostName")
+
+ serverResponse <- NULL
+ collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
+ expectedURL <- paste0(arv$getHostName(), "collections/", collectionUUID)
+
+ httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
+ arv$setHttpClient(httpRequest)
+ arv$setHttpParser(FakeHttpParser$new())
+
+ arv$getCollection(collectionUUID)
+
+ expect_that(httpRequest$URLIsProperlyConfigured, is_true())
+ expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
+ expect_that(httpRequest$numberOfGETRequests, equals(1))
+})
+
+test_that("getCollection parses server response", {
+
+ arv <- Arvados$new("token", "hostName")
+
+ httpParser <- FakeHttpParser$new()
+ arv$setHttpParser(httpParser)
+ arv$setHttpClient(FakeHttpRequest$new())
+
+ collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
+ arv$getCollection(collectionUUID)
+
+ expect_that(httpParser$parserCallCount, equals(1))
+})
+
+test_that("getCollection raises exception if response contains errors field", {
+
+ arv <- Arvados$new("token", "hostName")
+
+ serverResponse <- list(errors = 404)
+ arv$setHttpClient(FakeHttpRequest$new(NULL, serverResponse))
+ arv$setHttpParser(FakeHttpParser$new())
+
+ collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
+
+ expect_that(arv$getCollection(collectionUUID),
+ throws_error(404))
+})
+
+test_that("listCollections calls REST service properly", {
+
+ arv <- Arvados$new("token", "hostName")
+
+ serverResponse <- NULL
+ expectedURL <- paste0(arv$getHostName(), "collections")
+
+ httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
+ arv$setHttpClient(httpRequest)
+ arv$setHttpParser(FakeHttpParser$new())
+
+ arv$listCollections()
+
+ expect_that(httpRequest$URLIsProperlyConfigured, is_true())
+ expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
+ expect_that(httpRequest$numberOfGETRequests, equals(1))
+})
+
+test_that("listCollections parses server response", {
+
+ arv <- Arvados$new("token", "hostName")
+
+ httpParser <- FakeHttpParser$new()
+ arv$setHttpParser(httpParser)
+ arv$setHttpClient(FakeHttpRequest$new())
+
+ arv$listCollections()
+
+ expect_that(httpParser$parserCallCount, equals(1))
+})
+
+test_that("listCollections raises exception if response contains errors field", {
+
+ arv <- Arvados$new("token", "hostName")
+
+ serverResponse <- list(errors = 404)
+ expectedURL <- NULL
+ arv$setHttpClient(FakeHttpRequest$new(expectedURL, serverResponse))
+ arv$setHttpParser(FakeHttpParser$new())
+
+ expect_that(arv$listCollections(),
+ throws_error(404))
+})
+
+test_that("deleteCollection calls REST service properly", {
+
+ arv <- Arvados$new("token", "hostName")
+
+ serverResponse <- NULL
+ collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
+ expectedURL <- paste0(arv$getHostName(), "collections/", collectionUUID)
+
+ httpRequest <- FakeHttpRequest$new(expectedURL, serverResponse)
+ arv$setHttpClient(httpRequest)
+ arv$setHttpParser(FakeHttpParser$new())
+
+ arv$deleteCollection(collectionUUID)
+
+ expect_that(httpRequest$URLIsProperlyConfigured, is_true())
+ expect_that(httpRequest$requestHeaderContainsAuthorizationField, is_true())
+ expect_that(httpRequest$numberOfDELETERequests, equals(1))
+})
+
+test_that("deleteCollection parses server response", {
+
+ arv <- Arvados$new("token", "hostName")
+
+ httpParser <- FakeHttpParser$new()
+ arv$setHttpParser(httpParser)
+ arv$setHttpClient(FakeHttpRequest$new())
+
+ collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
+ arv$deleteCollection(collectionUUID)
+
+ expect_that(httpParser$parserCallCount, equals(1))
+})
+
+test_that("getCollection raises exception if response contains errors field", {
+
+ arv <- Arvados$new("token", "hostName")
+
+ serverResponse <- list(errors = 404)
+ arv$setHttpClient(FakeHttpRequest$new(NULL, serverResponse))
+ arv$setHttpParser(FakeHttpParser$new())
+
+ collectionUUID <- "aaaaa-j7d0g-ccccccccccccccc"
+
+ expect_that(arv$deleteCollection(collectionUUID),
+ throws_error(404))
+})