 (ns leela.blackbox.network.zmqserver-test
-  (:use     [clojure.test])
+  (:use     [clojure.test]
+            [clojure.tools.logging :only [trace debug info error]])
   (:require [leela.blackbox.f :as f]
             [leela.blackbox.storage.cassandra :as storage]
             [leela.blackbox.network.zmqserver :as server]))

        (is guid)))

    (truncate-n-test cluster "putname is idempotent"
      (is (= (server/handle-message cluster ["put" "name" "user" "tree" "kind" "foobar"]) (server/handle-message cluster ["put" "name" "user" "tree" "kind" "foobar"]))))

    (truncate-n-test cluster "getguid after putname"
      (is (= (server/handle-message cluster ["put" "name" "user" "tree" "kind" "foobar"]) (server/handle-message cluster ["get" "guid" "user" "tree" "kind" "foobar"]))))

    (truncate-n-test cluster "getname after putname"
      (let [[_ _ _ _ _ guid] (server/handle-message cluster ["put" "name" "user" "tree" "kind" "foobar"])]
        (is (= (server/handle-message cluster ["put" "name" "user" "tree" "kind" "foobar"]) (server/handle-message cluster ["get" "name" guid])))))))

(deftest test-zmqserver-handle-label-all-message
  (let [node (str (f/uuid-1))]
    (storage/with-connection [cluster ["127.0.0.1"] "leela"]

      (truncate-n-test cluster "getlabel-all with no data"
        (is (= (server/msg-label []) (server/handle-message cluster ["get" "label" "all" node]))))

      (truncate-n-test cluster "getlabel-all after putlabel"
        (server/handle-message cluster ["put"
                                        "label"
                                        node "0"
                                        node "1"
                                        node "2"])
        (is (= (server/msg-label ["0" "1" "2"]) (server/handle-message cluster ["get" "label" "all" node]))))

      (truncate-n-test cluster "getlabel-all pagination"
        (server/handle-message cluster ["put"
                                        "label"
                                        node "0"
                                        node "1"
                                        node "2"])
        (is (= (server/msg-label ["0"]) (server/handle-message cluster ["get" "label" "all" node "" "1"])))
        (is (= (server/msg-label ["1"]) (server/handle-message cluster ["get" "label" "all" node "1" "1"])))
        (is (= (server/msg-label ["2"]) (server/handle-message cluster ["get" "label" "all" node "2" "1"])))))))

(deftest test-zmqserver-handle-label-prefix-message
  (let [node (str (f/uuid-1))]
    (storage/with-connection [cluster ["127.0.0.1"] "leela"]

      (truncate-n-test cluster "getlabel-prefix with no data"
        (is (= (server/msg-label []) (server/handle-message cluster ["get" "label" "pre" node "1" "2"]))))

      (truncate-n-test cluster "getlabel-prefix after putlabel"
        (server/handle-message cluster ["put"
                                        "label"
                                        node "00"
                                        node "01"

     (storage/truncate-all ~cluster)
     ~@body))

(defn encode-str [data]
  (map #(f/str-to-bytes %) data))

(defmacro handle-message-macro [cluster msg]
  `(server/handle-message ~cluster ~cluster (encode-str ~msg)))

(deftest test-zmqserver-handle-put-name-msg
  (storage/with-connection [cluster ["127.0.0.1"] {}]
   (storage/use-attr-schema cluster "leela")
   (storage/use-graph-schema cluster "leela")
     (truncate-n-test cluster "putname returns guid"
      (let [[msg user tree kind name guid] (handle-message-macro cluster ["put" "name" "user" "tree" "kind" "foobar"])]
        (is (= "name" msg))
        (is (= "user" user))
        (is (= "tree" tree))
