             [cheshire.core :as cheshire]
             [cmr-system-int-test.ingest-util :as util]))
 
-;;; data related
 (def base-concept-attribs
   {:short-name "SN-Sedac88" 
    :version "Ver88"  
    :long-name "LongName Sedac88"
    :dataset-id "LarcDatasetId88"})
 
+;; Each test ingests and deletes this concept. 
 (defn concept
   "Creates a sample concept."
-  [provider-suffix-num]
+  [provider-num]
   {:concept-type :collection
    :native-id "nativeId1"
-   :provider-id (str "PROV" provider-suffix-num)
+   :provider-id (str "PROV88" provider-num)
    :metadata (util/metadata-xml base-concept-attribs)
-   :format "iso19115+xml"})
+   :format "echo10+xml"})
 
 (defn construct-ingest-rest-url
   "Construct ingest url based on concept."


;; Ingest a new concept with no revision-id.
(deftest concept-ingest-test
  (let [{:keys [status revision-id]} (ingest-concept :put (first *save-concept-list*))]
    (is (and (= status 200) (= revision-id 0)))))

;; Ingest same concept N times and verify it is in metadata db with revision id value 'N - 1'.
(deftest repeat-same-concept-ingest-test
  (let [N 4
        expected-revision-id (- N 1)
        last-revision-id (last (repeatedly N
                                           #(let [ {:keys [revision-id]} (ingest-concept :put (second *save-concept-list*))]
                                              revision-id)))]
    (is (= expected-revision-id last-revision-id))))

;; Verify concept is deleted from mdb and index.
(deftest save-concept-for-future-delete-test
  (let [{:keys [status revision-id]} (ingest-concept :put (first *delete-concept-list*))]
    (is (and (= status 200) (= revision-id 0)))))

(deftest delete-concept-test
  (let [{:keys [status]} (ingest-concept :delete (first *delete-concept-list*))]
    (is (= status 200))))

(deftest save-n-delete-test
  (save-concept-for-future-delete-test)
  (delete-concept-test))

(defn ingest-test-fixture 
  [f]
  (setup 2 1)
  (try
    (f)
    (finally (teardown))))

(deftest test-suite
  (concept-ingest-test)
  (repeat-same-concept-ingest-test)
  (save-n-delete-test))

(defn test-ns-hook []
  (ingest-test-fixture test-suite))


(comment
  (test-ns *ns*))




        ingest-rest-url (str "http://" host ":" port "/" ctx-part)]
    ingest-rest-url))

;;; operations
(defn ingest-concept
  "Ingest a concept and return a map with status, concept-id, and revision-id"
  [concept]
  (let [response (client/request
                   {:method :put
                    :url (construct-ingest-rest-url concept) 
                    :body  (:metadata concept) ;; (io/string-input-stream (:metadata concept))
                    :content-type (:format concept)
                    :accept :json
                    :throw-exceptions false})
        status (:status response)
        body (cheshire/parse-string (:body response))
        concept-id (get body "concept-id")
        revision-id (get body "revision-id")]
    {:status status :concept-id concept-id :revision-id revision-id :response response}))

(defn delete-concept
  "Delete a given concept."
  [concept]
  (let [response (client/request
                   {:method :delete
                    :url (construct-ingest-rest-url concept)
                    :accept :json
                    :throw-exceptions false})
        status (:status response)
        body (cheshire/parse-string (:body response))
        concept-id (get body "concept-id")
        revision-id (get body "revision-id")]
    {:status status :concept-id concept-id :revision-id revision-id :response response}))

;;; tests
;;; ensure metadata and ingest apps are accessable on ports 3001 and 3002 resp; 
