     [cheshire.core :as json]
     [clojurewerkz.elastisch.rest :as esr]
     [clojurewerkz.elastisch.rest.document :as esd]
+    [cmr.common.util :refer [are3]]
     [cmr.system-int-test.utils.url-helper :as url]
     [cmr.system-int-test.utils.ingest-util :as ingest]
     [cmr.system-int-test.utils.index-util :as index]

                     [(ingest/reset-fixture)
                      autocomplete-fixture]))

(deftest autocomplete-response-test
  (testing "response form test"
     (let [response (search/get-autocomplete-suggestions "q=foo")

       (is (= "1" (:CMR-Hits headers))))))

(deftest autocomplete-functionality-test
  (testing "value search"
   (are3
    [query expected]
    (is (= expected (count (response-body->entries (query->json-response-body query)))))

    "partial value"
    "q=f" 1

    "full value match"
    "q=foo" 1

    "full value with extra value match"
    "q=foos" 0

    "case sensitivity test"
    "q=FOO" 1

    "no result test"
    "q=zee" 0

    "search with type filter"
    "q=foo&type[]=instrument" 1

    "search with multiple types filter"
    "q=b&type[]=instrument&type[]=platform&type[]=project&type[]=provider" 2

    "search with type filter with no results"
    "q=foo&type[]=platform" 0)))

(deftest autocomplete-usage-test
  (testing "invalid or missing query param tests"
   (are3
    [query]
    (is (= 400 (:status (search/get-autocomplete-suggestions query {:throw-exceptions false}))))

    "no query param provided"
    nil

    "blank query provided"
    "q=  "))

  (testing "page_size test"
   (are3
    [query expected cmr-hits]
    (do
      (let [response (search/get-autocomplete-suggestions query)
            headers (:headers response)
            body (json/parse-string (:body response) true)
            entry (:entry (:feed body))]
        (is (= (str cmr-hits) (:CMR-Hits headers)))
        (is (= expected (count entry)))))

    "page size 0"
    "q=*&page_size=0" 0 3

    "page size 1"
    "q=*&page_size=1" 1 3

    "page size 2"
    "q=*&page_size=2" 2 3

    "page size 3"
    "q=*&page_size=3" 3 3

    "page size 100"
    "q=*&page_size=100" 3 3))

  (testing "page_num should yield different 'first' results test"
   (let [page-one-entry (as-> (query->json-response-body "q=b&page_size=1") response
                              (first (response-body->entries response)))

         page-two-entry (as-> (query->json-response-body "q=b&page_size=1&page_num=1") response
                              (first (response-body->entries response)))]
     (is (not (= page-one-entry page-two-entry))))))
