Skip to content
Permalink
c2be41ed79
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
259 lines (243 sloc) 10 KB
(ns util-webdav.client-test
(:require [clojure.tools.logging :refer [info error]]
[clojure.string :as str]
[clojure.java.io :as io]
[clojure.test :refer [deftest is]]
[util-webdav.config :as config]
[de.mpg.shh.util-webdav.client :as client])
(:import [java.nio.file Files Paths StandardCopyOption]
[java.nio.file.attribute FileAttribute]
[java.io File IOException]))
(def dav-prefix (config/get-in [:test :dav-prefix] ""))
(def default-opts
{:base-url (config/get-in [:base-url])
:user-name (config/get-in [:user-name])
:password (config/get-in [:password])})
(defn temp-dir
[]
(try
(Files/createTempDirectory "clj-util-webdav" (into-array FileAttribute []))
(catch IOException ioe
(throw (ex-info "IOException creating tmp dir" {:cause (.getMessage ioe)})))))
(defn simple-dav-map
[m]
(dissoc m
:modified
:created))
(deftest conn
(let [conn (client/conn default-opts)
expected #{:sardine :base-url}]
(is (= (into #{} (keys conn)) expected))))
(deftest get-file-not-found-test
(let [conn (client/conn default-opts)
file-name "foo.txt"
result (try
(client/get-file conn file-name)
(catch Exception e
(ex-data e)))]
(is (= result {:cause "Unexpected response (404 Not Found)"}))))
(deftest get-file-test
(let [conn (client/conn default-opts)
file-name "foo.txt"
i-stream (io/input-stream (io/resource file-name))
result (try
(client/put-stream conn i-stream file-name)
(slurp (client/get-file conn file-name))
(catch Exception e
(ex-data e)))
_ (client/delete-file conn file-name)]
(is (= result "My dog spot\n"))))
(deftest put-file-test
(let [conn (client/conn default-opts)
file-name "foo.txt"
i-stream (io/input-stream (io/resource file-name))
tmp-dir-path (temp-dir)
tmp-file-path (Paths/get (.toString tmp-dir-path) (into-array String ["foo.txt"]))
result (try
(Files/copy i-stream tmp-file-path (into-array StandardCopyOption [StandardCopyOption/REPLACE_EXISTING]))
(client/put-file conn (.toFile tmp-file-path) file-name)
(catch Exception e
(error "caught exception e: " e)
(ex-data e)))
_ (client/delete-file conn file-name)]
(is (nil? result))))
(deftest put-stream-test
(let [conn (client/conn default-opts)
file-name "foo.txt"
i-stream (io/input-stream (io/resource file-name))
result (try
(client/put-stream conn i-stream file-name)
(catch Exception e
(error "caught exception e: " e)
(ex-data e)))
_ (client/delete-file conn file-name)]
(is (nil? result))))
(deftest create-directory-test
(let [conn (client/conn default-opts)
file-name "bar"
result (try
(client/create-directory conn file-name)
(catch Exception e
(error "caught exception e: " e)
(ex-data e)))
_ (client/delete-dir conn file-name)]
(is (or (nil? result)
(= result {:cause "Unexpected response (301 Moved Permanently)"})))))
(deftest list-dir-test
(let [conn (client/conn default-opts)
i-stream (io/input-stream (io/resource "foo.txt"))
_ (client/put-stream conn i-stream "foo.txt")
_ (client/create-directory conn "bar")
result (try
(vec (client/list-dir conn ""))
(catch Exception e
(error "caught exception e: " e)
(ex-data e)))
_ (client/delete-file conn "foo.txt")
_ (client/delete-dir conn "bar")
expected [{:file-name (str/replace dav-prefix #"/$" "")
:path (str "/" dav-prefix)
:relative-path ""
:dir? true
:content-type "httpd/unix-directory"
:content-length -1
:properties {}}
{:file-name "bar"
:path (str "/" dav-prefix "bar/")
:relative-path "bar"
:dir? true
:content-type "httpd/unix-directory"
:content-length -1
:properties {}}
{:file-name "foo.txt"
:path (str "/" dav-prefix "foo.txt")
:relative-path "foo.txt"
:dir? false
:content-type "text/plain"
:content-length 12
:properties {:apache.org.dav.props/executable false}}]]
(is (= (vec (sort-by :file-name expected)) (vec (sort-by :file-name (map simple-dav-map result)))))))
(deftest list-file-test
(let [conn (client/conn default-opts)
file-name "foo.txt"
i-stream (io/input-stream (io/resource file-name))
_ (client/put-stream conn i-stream file-name)
result (try
(client/list-file conn file-name)
(catch Exception e
(error "caught exception e: " e)
(ex-data e)))
_ (client/delete-file conn file-name)
expected {:file-name "foo.txt"
:path (str "/" dav-prefix "foo.txt")
:relative-path "foo.txt"
:dir? false
:content-type "text/plain"
:content-length 12
:properties {:apache.org.dav.props/executable false}}]
(is (= expected (simple-dav-map result)))))
(deftest delete-test
(let [conn (client/conn default-opts)
file-name "foo.txt"
i-stream (io/input-stream (io/resource file-name))
_ (client/put-stream conn i-stream file-name)
result (try
(client/delete-file conn file-name)
(catch Exception e
(error "caught exception e: " e)
(ex-data e)))]
(is (nil? result))))
(deftest assoc-props-test
(let [conn (client/conn default-opts)
file-name "foo.txt"
i-stream (io/input-stream (io/resource file-name))
_ (client/put-stream conn i-stream file-name)
props {:huricane/katrina true
:huricane/bob false}
_ (try
(client/assoc-props conn file-name props)
(catch Exception e
(error "caught exception e: " e)
(ex-data e)))
result (client/list-file conn file-name)
_ (client/delete-file conn "foo.txt")
expected {:file-name "foo.txt"
:path (str "/" dav-prefix "foo.txt")
:relative-path "foo.txt"
:dir? false
:content-type "text/plain"
:content-length 12
:properties {:apache.org.dav.props/executable false
:huricane/katrina true
:huricane/bob false}}]
(is (= expected (simple-dav-map result)))))
(deftest dissoc-props-test
(let [conn (client/conn default-opts)
file-name "foo.txt"
i-stream (io/input-stream (io/resource file-name))
_ (client/put-stream conn i-stream file-name)
props {:huricane/katrina true
:huricane/bob false}
_ (try
(client/assoc-props conn file-name props)
(catch Exception e
(error "caught exception e: " e)
(ex-data e)))
_ (try
(client/dissoc-props conn file-name #{:huricane/katrina})
(catch Exception e
(error "caught exception e: " e)
(ex-data e)))
result (try
(client/list-file conn file-name)
(catch Exception e
(error "caught exception e: " e)
(ex-data e)))
_ (client/delete-file conn "foo.txt")
expected {:file-name "foo.txt"
:path (str "/" dav-prefix "foo.txt")
:relative-path "foo.txt"
:dir? false
:content-type "text/plain"
:content-length 12
:properties {:apache.org.dav.props/executable false
:huricane/bob true}}]
(is (= expected (simple-dav-map result)))))
(deftest locktest
(let [conn (client/conn default-opts)
file-name "foo.txt"
i-stream (io/input-stream (io/resource file-name))
_ (client/put-stream conn i-stream file-name)
props {:count 0}
_ (client/assoc-props conn file-name props)
lock-token (try
(client/lock conn file-name)
(catch Exception e
(error "caught exception e: " e)
(ex-data e)))
already-locked (try
(client/lock conn file-name)
(catch Exception e
(ex-data e)))
_ (client/assoc-props conn file-name {:count 1} (str "(<" lock-token ">)"))
result-no-token (try
(client/assoc-props conn file-name {:count 2})
(catch Exception e
(ex-data e)))
result-unlock (try
(client/unlock conn file-name lock-token)
(catch Exception e
(error "caught exception e: " e)
(ex-data e)))
result-props (try
(client/list-file conn file-name)
(catch Exception e
(error "caught exception e: " e)
(ex-data e)))
_ (client/delete-file conn file-name)
]
(and (is (string? lock-token))
(is (= (:cause already-locked) "Unexpected response (423 Locked)"))
(is (= (:cause result-no-token) "Unexpected response (423 Locked)"))
(is (nil? result-unlock))
(is (= (-> result-props :properties :count) 1)))))