[ARVADOS] updated: 1.1.2-184-gf1bde50

Git user git at public.curoverse.com
Thu Jan 18 09:32:01 EST 2018


Summary of changes:
 sdk/R/R/ArvadosFile.R                        |   4 +-
 sdk/R/R/RESTService.R                        |  14 +-
 sdk/R/R/Subcollection.R                      |   4 +-
 sdk/R/tests/testthat/fakes/FakeArvados.R     |  35 ++++
 sdk/R/tests/testthat/fakes/FakeHttpParser.R  |   2 +
 sdk/R/tests/testthat/fakes/FakeHttpRequest.R |  27 ++-
 sdk/R/tests/testthat/fakes/FakeRESTService.R |   1 -
 sdk/R/tests/testthat/test-RESTService.R      | 237 +++++++++++++++++++++++++++
 8 files changed, 308 insertions(+), 16 deletions(-)
 create mode 100644 sdk/R/tests/testthat/fakes/FakeArvados.R
 create mode 100644 sdk/R/tests/testthat/test-RESTService.R

       via  f1bde50ee2e8b8fa8533518006f09546a62f7a87 (commit)
      from  af17604c5a93830380fb50db93ce543926c116cf (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.


commit f1bde50ee2e8b8fa8533518006f09546a62f7a87
Author: Fuad Muhic <fmuhic at capeannenterprises.com>
Date:   Thu Jan 18 15:31:02 2018 +0100

    Added unit tests for RESTService class
    
    Arvados-DCO-1.1-Signed-off-by: Fuad Muhic <fmuhic at capeannenterprises.com>

diff --git a/sdk/R/R/ArvadosFile.R b/sdk/R/R/ArvadosFile.R
index bacbb74..0cbecd8 100644
--- a/sdk/R/R/ArvadosFile.R
+++ b/sdk/R/R/ArvadosFile.R
@@ -32,8 +32,8 @@ ArvadosFile <- R6::R6Class(
 
             REST <- private$collection$getRESTService()
 
-            fileSize <- REST$getResourceSize(private$collection$uuid,
-                                             self$getRelativePath())
+            fileSize <- REST$getResourceSize(self$getRelativePath(),
+                                             private$collection$uuid)
 
             fileSize
         },
diff --git a/sdk/R/R/RESTService.R b/sdk/R/R/RESTService.R
index de232ad..8cb75f0 100644
--- a/sdk/R/R/RESTService.R
+++ b/sdk/R/R/RESTService.R
@@ -30,14 +30,12 @@ RESTService <- R6::R6Class(
             if(serverResponse$status_code < 200 || serverResponse$status_code >= 300)
                 stop(paste("Server code:", serverResponse$status_code))
 
-            print(paste("File deleted:", relativePath))
+            paste("File deleted:", relativePath)
         },
 
         move = function(from, to, uuid)
         {
-            #Todo Do we need this URLencode?
-            collectionURL <- URLencode(paste0(private$api$getWebDavHostName(), "c=",
-                                              uuid, "/"))
+            collectionURL <- paste0(private$api$getWebDavHostName(), "c=", uuid, "/")
             fromURL <- paste0(collectionURL, from)
             toURL <- paste0(collectionURL, to)
 
@@ -60,11 +58,14 @@ RESTService <- R6::R6Class(
 
             response <- private$http$PROPFIND(collectionURL, headers)
 
+            if(all(response == ""))
+                stop("Response is empty, reques may be misconfigured")
+
             parsedResponse <- private$httpParser$parseWebDAVResponse(response, collectionURL)
             parsedResponse[-1]
         },
 
-        getResourceSize = function(uuid, relativePath)
+        getResourceSize = function(relativePath, uuid)
         {
             collectionURL <- URLencode(paste0(private$api$getWebDavHostName(),
                                               "c=", uuid))
@@ -138,7 +139,6 @@ RESTService <- R6::R6Class(
         http       = NULL,
         httpParser = NULL,
 
-
         createNewFile = function(relativePath, uuid, contentType)
         {
             fileURL <- paste0(private$api$getWebDavHostName(), "c=",
@@ -152,7 +152,7 @@ RESTService <- R6::R6Class(
             if(serverResponse$status_code < 200 || serverResponse$status_code >= 300)
                 stop(paste("Server code:", serverResponse$status_code))
 
-            print(paste("File created:", relativePath))
+            paste("File created:", relativePath)
         }
     ),
 
diff --git a/sdk/R/R/Subcollection.R b/sdk/R/R/Subcollection.R
index 5babc66..801cf69 100644
--- a/sdk/R/R/Subcollection.R
+++ b/sdk/R/R/Subcollection.R
@@ -127,8 +127,8 @@ Subcollection <- R6::R6Class(
 
             REST <- private$collection$getRESTService()
 
-            fileSizes <- REST$getResourceSize(private$collection$uuid,
-                                              paste0(self$getRelativePath(), "/"))
+            fileSizes <- REST$getResourceSize(paste0(self$getRelativePath(), "/"),
+                                              private$collection$uuid)
             return(sum(fileSizes))
         },
 
diff --git a/sdk/R/tests/testthat/fakes/FakeArvados.R b/sdk/R/tests/testthat/fakes/FakeArvados.R
new file mode 100644
index 0000000..5886ff7
--- /dev/null
+++ b/sdk/R/tests/testthat/fakes/FakeArvados.R
@@ -0,0 +1,35 @@
+FakeArvados <- R6::R6Class(
+
+    "FakeArvados",
+
+    public = list(
+
+        token      = NULL,
+        host       = NULL,
+        webdavHost = NULL,
+        http       = NULL,
+        httpParser = NULL,
+        REST       = NULL,
+
+        initialize = function(token      = NULL,
+                              host       = NULL,
+                              webdavHost = NULL,
+                              http       = NULL,
+                              httpParser = NULL)
+        {
+            self$token      <- token
+            self$host       <- host
+            self$webdavHost <- webdavHost
+            self$http       <- http
+            self$httpParser <- httpParser
+        },
+
+        getToken    = function() self$token,
+        getHostName = function() self$host,
+        getHttpClient = function() self$http,
+        getHttpParser = function() self$httpParser,
+        getWebDavHostName = function() self$webdavHost
+    ),
+
+    cloneable = FALSE
+)
diff --git a/sdk/R/tests/testthat/fakes/FakeHttpParser.R b/sdk/R/tests/testthat/fakes/FakeHttpParser.R
index 8252497..479cc0d 100644
--- a/sdk/R/tests/testthat/fakes/FakeHttpParser.R
+++ b/sdk/R/tests/testthat/fakes/FakeHttpParser.R
@@ -19,11 +19,13 @@ FakeHttpParser <- R6::R6Class(
 
         parseWebDAVResponse = function(response, uri)
         {
+            self$parserCallCount <- self$parserCallCount + 1
             response
         },
 
         extractFileSizeFromWebDAVResponse = function(response, uri)    
         {
+            self$parserCallCount <- self$parserCallCount + 1
             response
         }
     )
diff --git a/sdk/R/tests/testthat/fakes/FakeHttpRequest.R b/sdk/R/tests/testthat/fakes/FakeHttpRequest.R
index 168e4ec..7571d5f 100644
--- a/sdk/R/tests/testthat/fakes/FakeHttpRequest.R
+++ b/sdk/R/tests/testthat/fakes/FakeHttpRequest.R
@@ -12,29 +12,40 @@ FakeHttpRequest <- R6::R6Class(
         expectedQueryFilters                    = NULL,
         queryFiltersAreCorrect                  = NULL,
         requestHeaderContainsAuthorizationField = NULL,
+        requestHeaderContainsDestinationField   = NULL,
         JSONEncodedBodyIsProvided               = NULL,
 
         numberOfGETRequests    = NULL,
         numberOfDELETERequests = NULL,
         numberOfPUTRequests    = NULL,
         numberOfPOSTRequests   = NULL,
+        numberOfMOVERequests   = NULL,
 
-        initialize = function(expectedURL = NULL,
-                              serverResponse = NULL,
-                              expectedFilters = NULL)
+        initialize = function(expectedURL      = NULL,
+                              serverResponse   = NULL,
+                              expectedFilters  = NULL)
         {
-            self$content <- serverResponse
+            if(is.null(serverResponse))
+            {
+                self$content <- list()
+                self$content$status_code <- 200
+            }
+            else
+                self$content <- serverResponse
+
             self$expectedURL <- expectedURL
             self$URLIsProperlyConfigured <- FALSE
             self$expectedQueryFilters <- expectedFilters
             self$queryFiltersAreCorrect <- FALSE
             self$requestHeaderContainsAuthorizationField <- FALSE
+            self$requestHeaderContainsDestinationField <- FALSE
             self$JSONEncodedBodyIsProvided <- FALSE
 
             self$numberOfGETRequests <- 0
             self$numberOfDELETERequests <- 0
             self$numberOfPUTRequests <- 0
             self$numberOfPOSTRequests <- 0
+            self$numberOfMOVERequests <- 0
 
             self$serverMaxElementsPerRequest <- 5
         },
@@ -87,11 +98,16 @@ FakeHttpRequest <- R6::R6Class(
 
         PROPFIND = function(url, headers = NULL)
         {
+            private$validateURL(url)
+            private$validateHeaders(headers)
             self$content
         },
 
         MOVE = function(url, headers = NULL)
         {
+            private$validateURL(url)
+            private$validateHeaders(headers)
+            self$numberOfMOVERequests <- self$numberOfMOVERequests + 1
             self$content
         }
     ),
@@ -108,6 +124,9 @@ FakeHttpRequest <- R6::R6Class(
         {
             if(!is.null(headers$Authorization))
                 self$requestHeaderContainsAuthorizationField <- TRUE
+
+            if(!is.null(headers$Destination))
+                self$requestHeaderContainsDestinationField <- TRUE
         },
 
         validateBody = function(body)
diff --git a/sdk/R/tests/testthat/fakes/FakeRESTService.R b/sdk/R/tests/testthat/fakes/FakeRESTService.R
index 3baea0b..a69cd58 100644
--- a/sdk/R/tests/testthat/fakes/FakeRESTService.R
+++ b/sdk/R/tests/testthat/fakes/FakeRESTService.R
@@ -33,7 +33,6 @@ FakeRESTService <- R6::R6Class(
         create = function(files, uuid)
         {
             self$createCallCount <- self$createCallCount + 1
-
             self$returnContent
         },
 
diff --git a/sdk/R/tests/testthat/test-RESTService.R b/sdk/R/tests/testthat/test-RESTService.R
new file mode 100644
index 0000000..10c62ce
--- /dev/null
+++ b/sdk/R/tests/testthat/test-RESTService.R
@@ -0,0 +1,237 @@
+source("fakes/FakeArvados.R")
+
+context("REST service")
+
+test_that("create calls REST service properly", {
+
+    expectedURL <- "https:/webdavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
+    fakeHttp <- FakeHttpRequest$new(expectedURL)
+    fakeHttpParser <- FakeHttpParser$new()
+
+    arv <- FakeArvados$new("token",
+                           "https:/host/",
+                           "https:/webdavHost/",
+                           fakeHttp,
+                           fakeHttpParser)
+
+    REST <- RESTService$new(arv)
+
+    uuid <- "aaaaa-j7d0g-ccccccccccccccc"
+    REST$create("file", uuid)
+
+    expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
+    expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
+    expect_that(fakeHttp$numberOfPUTRequests, equals(1))
+}) 
+
+test_that("create raises exception if error code is not between 200 and 300", {
+
+    response <- list()
+    response$status_code <- 404
+    fakeHttp <- FakeHttpRequest$new(serverResponse = response)
+
+    arv <- FakeArvados$new("token",
+                           "https:/host/",
+                           "https:/webdavHost/",
+                           fakeHttp,
+                           FakeHttpParser$new())
+
+    uuid <- "aaaaa-j7d0g-ccccccccccccccc"
+    REST <- RESTService$new(arv)
+
+    expect_that(REST$create("file", uuid),
+                throws_error("Server code: 404"))
+}) 
+
+test_that("delete calls REST service properly", {
+
+    expectedURL <- "https:/webdavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
+    fakeHttp <- FakeHttpRequest$new(expectedURL)
+    fakeHttpParser <- FakeHttpParser$new()
+
+    arv <- FakeArvados$new("token",
+                           "https:/host/",
+                           "https:/webdavHost/",
+                           fakeHttp,
+                           fakeHttpParser)
+
+    REST <- RESTService$new(arv)
+
+    uuid <- "aaaaa-j7d0g-ccccccccccccccc"
+    REST$delete("file", uuid)
+
+    expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
+    expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
+    expect_that(fakeHttp$numberOfDELETERequests, equals(1))
+}) 
+
+test_that("delete raises exception if error code is not between 200 and 300", {
+
+    response <- list()
+    response$status_code <- 404
+    fakeHttp <- FakeHttpRequest$new(serverResponse = response)
+
+    arv <- FakeArvados$new("token",
+                           "https:/host/",
+                           "https:/webdavHost/",
+                           fakeHttp,
+                           FakeHttpParser$new())
+
+    uuid <- "aaaaa-j7d0g-ccccccccccccccc"
+    REST <- RESTService$new(arv)
+
+    expect_that(REST$delete("file", uuid),
+                throws_error("Server code: 404"))
+}) 
+
+test_that("move calls REST service properly", {
+
+    expectedURL <- "https:/webdavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
+    fakeHttp <- FakeHttpRequest$new(expectedURL)
+    fakeHttpParser <- FakeHttpParser$new()
+
+    arv <- FakeArvados$new("token",
+                           "https:/host/",
+                           "https:/webdavHost/",
+                           fakeHttp,
+                           fakeHttpParser)
+
+    REST <- RESTService$new(arv)
+
+    uuid <- "aaaaa-j7d0g-ccccccccccccccc"
+    REST$move("file", "newDestination/file", uuid)
+
+    expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
+    expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
+    expect_that(fakeHttp$requestHeaderContainsDestinationField, is_true())
+    expect_that(fakeHttp$numberOfMOVERequests, equals(1))
+}) 
+
+test_that("move raises exception if error code is not between 200 and 300", {
+
+    response <- list()
+    response$status_code <- 404
+    fakeHttp <- FakeHttpRequest$new(serverResponse = response)
+
+    arv <- FakeArvados$new("token",
+                           "https:/host/",
+                           "https:/webdavHost/",
+                           fakeHttp,
+                           FakeHttpParser$new())
+
+    uuid <- "aaaaa-j7d0g-ccccccccccccccc"
+    REST <- RESTService$new(arv)
+
+    expect_that(REST$move("file", "newDestination/file", uuid),
+                throws_error("Server code: 404"))
+}) 
+
+test_that("getCollectionContent retreives correct content from WebDAV server", {
+
+    expectedURL <- "https:/webdavHost/c=aaaaa-j7d0g-ccccccccccccccc"
+
+    # WevDAV server always return collection name as first entry in result array,
+    # so getCollectionContern need to filter it 
+    returnContent <- c("aaaaa-j7d0g-ccccccccccccccc", 
+                       "animal", "animal/dog", "ball")
+    expectedContent <- c("animal", "animal/dog", "ball")
+
+    fakeHttp <- FakeHttpRequest$new(expectedURL, returnContent)
+    fakeHttpParser <- FakeHttpParser$new()
+
+    arv <- FakeArvados$new("token",
+                           "https:/host/",
+                           "https:/webdavHost/",
+                           fakeHttp,
+                           fakeHttpParser)
+
+    REST <- RESTService$new(arv)
+
+    uuid <- "aaaaa-j7d0g-ccccccccccccccc"
+    returnResult <- REST$getCollectionContent(uuid)
+    returnedContentMatchExpected <- all.equal(returnResult, expectedContent)
+
+    expect_that(returnedContentMatchExpected, is_true())
+    expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
+}) 
+
+test_that("getCollectionContent raises exception if server returns empty response", {
+
+    response <- ""
+    fakeHttp <- FakeHttpRequest$new(serverResponse = response)
+
+    arv <- FakeArvados$new("token",
+                           "https:/host/",
+                           "https:/webdavHost/",
+                           fakeHttp,
+                           FakeHttpParser$new())
+
+    uuid <- "aaaaa-j7d0g-ccccccccccccccc"
+    REST <- RESTService$new(arv)
+
+    expect_that(REST$getCollectionContent(uuid),
+                throws_error("Response is empty, reques may be misconfigured"))
+}) 
+
+test_that("getCollectionContent parses server response", {
+
+    fakeHttp <- FakeHttpRequest$new()
+    fakeHttpParser <- FakeHttpParser$new()
+
+    arv <- FakeArvados$new("token",
+                           "https:/host/",
+                           "https:/webdavHost/",
+                           fakeHttp,
+                           fakeHttpParser)
+
+    REST <- RESTService$new(arv)
+
+    uuid <- "aaaaa-j7d0g-ccccccccccccccc"
+    REST$getCollectionContent(uuid)
+
+    expect_that(fakeHttpParser$parserCallCount, equals(1))
+}) 
+
+test_that("getResourceSize calls REST service properly", {
+
+    expectedURL <- "https:/webdavHost/c=aaaaa-j7d0g-ccccccccccccccc/file"
+    expectedContent <- c("6", "2", "931", "12003")
+    fakeHttp <- FakeHttpRequest$new(expectedURL, expectedContent)
+    fakeHttpParser <- FakeHttpParser$new()
+
+    arv <- FakeArvados$new("token",
+                           "https:/host/",
+                           "https:/webdavHost/",
+                           fakeHttp,
+                           fakeHttpParser)
+
+    REST <- RESTService$new(arv)
+
+    uuid <- "aaaaa-j7d0g-ccccccccccccccc"
+    returnResult <- REST$getResourceSize("file", uuid)
+    returnedContentMatchExpected <- all.equal(returnResult,
+                                              as.numeric(expectedContent))
+
+    expect_that(fakeHttp$URLIsProperlyConfigured, is_true())
+    expect_that(fakeHttp$requestHeaderContainsAuthorizationField, is_true())
+    expect_that(returnedContentMatchExpected, is_true())
+}) 
+
+test_that("getResourceSize parses server response", {
+
+    fakeHttp <- FakeHttpRequest$new()
+    fakeHttpParser <- FakeHttpParser$new()
+
+    arv <- FakeArvados$new("token",
+                           "https:/host/",
+                           "https:/webdavHost/",
+                           fakeHttp,
+                           fakeHttpParser)
+
+    REST <- RESTService$new(arv)
+
+    uuid <- "aaaaa-j7d0g-ccccccccccccccc"
+    REST$getResourceSize("file", uuid)
+
+    expect_that(fakeHttpParser$parserCallCount, equals(1))
+}) 

-----------------------------------------------------------------------


hooks/post-receive
-- 




More information about the arvados-commits mailing list