 (ns desdemona.plugins.swift-test
-  (:require [clojure.test :refer [deftest is]]
+  (:require [clojure.test :refer [deftest testing is]]
             [cheshire.core :as json]
             [camel-snake-kebab.core :refer [->kebab-case-keyword]]
             [clojure.java.io :as io]

    (is (= token got))))

(deftest authenticate-test
  "This test verifies that the authenticate function makes a request to the
  URL specified by the auth-url parameter, and includes the username and api-key
  in the expected JSON format."
  (http/with-fake-routes
    {"http://authurl.com" {:post (fn [req]
                                   (let [body (json/parse-stream (io/reader (req :body)) ->kebab-case-keyword)
                                         username (-> body :auth :rax-kskey:api-key-credentials :username)
                                         api-key (-> body :auth :rax-kskey:api-key-credentials :api-key)]
                                     {:status 200
                                      :headers {}
                                      :body (json/generate-string {:username username
                                                                   :api-key api-key})}))}}
    (let [result (swift/authenticate "http://authurl.com" "username" "apikey")]
      (is (= {:username "username" :api-key "apikey"} result)))))

(deftest create-container-test
  "This test verifies that the create-container function makes a request to the
  URL specified, with the container-name in the path, and includes the given
  auth token as a header."
  (http/with-fake-routes
    {"http://cf-url.com/containername" {:put (fn [req]
                                               (let [token (-> req :headers (get "X-Auth-Token"))]
                                                 (is (= "authtoken" token))
                                                 {:status 200
                                                  :headers {}
                                                  :body ""}))}}
    (let [result (swift/create-container "http://cf-url.com" "authtoken" "containername")]
      (is (= 200 result)))))

(deftest write-file-test
  "This test verifies that the write-file function makes a request to the URL
  specified, with the container-name and file-name in the path, that it includes
  the auth token as a header, and encodes the given contents as JSON."
  (http/with-fake-routes
    {"http://cf-url.com/containername/filename" {:put (fn [req]
                                                        (let [token (-> req :headers (get "X-Auth-Token"))
                                                              body (json/parse-stream (io/reader (req :body)) ->kebab-case-keyword)]
                                                          (is (= "authtoken" token))
                                                          (is (= {:contents 123} body))
                                                          {:status 200
                                                           :headers {}
                                                           :body ""}))}}
    (let [result (swift/write-file "http://cf-url.com" "authtoken" "containername" "filename" {:contents 123})]
      (is (= 200 result)))))

(deftest calculate-container-name-test
  (freeze-time (t/date-time 2016 6 8 4 15 45 401)

      (is (= {:onyx.core/written? true} result)))))

(deftest swift-write-rows-write-batch-empty-test
  (testing "This test verifies that a batch with no segments does not make any HTTP calls."
    (let [record (swift/->SwiftWriteRows "url" "username" "apikey")
          event {:onyx.core/results {:tree '()}}
          result (.write-batch record event)]
      (is (= {:onyx.core/written? true} result)))))

(deftest swift-write-rows-seal-resource-test
  (testing "This test verifies that seal-resource does nothing."
    (let [record (swift/->SwiftWriteRows "http://authurl.com" "username" "apikey")
          result (.seal-resource record :empty-event)]
      (is (= {} result)))))

(deftest write-batch-test
  (let [pipeline-data {:onyx.core/task-map {:swift/auth-url "authurl"
