 (ns java-s3-tests.test.client
   (:import java.security.MessageDigest
            org.apache.commons.codec.binary.Hex
-           com.amazonaws.services.s3.model.AmazonS3Exception
-           com.amazonaws.services.s3.model.AccessControlList
-           com.amazonaws.services.s3.model.CanonicalGrantee
-           com.amazonaws.services.s3.model.GroupGrantee
-           com.amazonaws.services.s3.model.Grant
-           com.amazonaws.services.s3.model.Permission
-           com.amazonaws.services.s3.model.PutObjectRequest
-           com.amazonaws.services.s3.model.CreateBucketRequest
-           com.amazonaws.services.s3.model.ObjectMetadata
-           com.amazonaws.services.s3.transfer.TransferManager
-           com.amazonaws.services.s3.transfer.TransferManagerConfiguration)
+           com.amazonaws.services.s3.model.AmazonS3Exception)
 
   (:require [aws.sdk.s3 :as s3])
   (:require [java-s3-tests.user-creation :as user-creation])
+  (:require [clojure.tools.logging :as log])
   (:use midje.sweet))
 
 (def ^:internal riak-cs-host-with-protocol "http://localhost")

(defn etag-suffix [etag]
  (subs etag (- (count etag) 2)))

(defn create-manager [c]
  (TransferManager. c))

(defn configure-manager [tm]
  (let [tm-config (.getConfiguration tm)]
    (.setMultipartUploadThreshold tm-config 19)
    (.setMinimumUploadPartSize tm-config 10)
    (.setConfiguration tm tm-config)))

(defn create-and-configure-manager [c]
  (let [tm (create-manager c)]
    (configure-manager tm)
    tm))

(defn upload-file [tm bucket-name object-name file-name]
  (let [f (clojure.java.io/file file-name)
        u (.upload tm bucket-name object-name f)]
    (.waitForCompletion u)
    (.delete f)))

(fact "bogus creds raises an exception"
      (let [bogus-client
            (s3/client "foo"
                       "bar"
                       {:endpoint (str "http://localhost:"
                                       (get-riak-cs-port-str))})]
        (s3/list-buckets bogus-client))
      => (throws AmazonS3Exception))

(fact "new users have no buckets"
        (with-random-client c
          (s3/list-buckets c))
        => [])

(let [bucket-name (random-string)]
  (fact "creating a bucket should list
        one bucket in list buckets"
        (with-random-client c
          (do (s3/create-bucket c bucket-name)
            ((comp :name first) (s3/list-buckets c))))
        => bucket-name))

(let [bucket-name (random-string)
      object-name (random-string)]
  (fact "simple put works"
        (with-random-client c
          (do (s3/create-bucket c bucket-name)
            (s3/put-object c bucket-name object-name
                           "contents")))
        => truthy))

(let [bucket-name (random-string)

      value "this is the value!"]
  (fact "the value received during GET is the same
        as the object that was PUT"
        (with-random-cred c
          (do (s3/create-bucket c bucket-name)
              (s3/put-object c bucket-name object-name
                             value)
              ((comp slurp :content) (s3/get-object c bucket-name object-name))))
        => value))

(let [bucket-name (random-string)
