 (ns cmr.system-int-test.ingest.collection-validation-test
   "CMR Ingest validation integration tests"
-  (:require [clojure.test :refer :all]
-            [cmr.system-int-test.utils.ingest-util :as ingest]
-            [cmr.system-int-test.data2.collection :as dc]
-            [cmr.system-int-test.data2.granule :as dg]
-            [cmr.system-int-test.data2.core :as d]
-            [cmr.common.mime-types :as mt]
-            [cmr.umm.umm-collection :as c]
-            [cmr.umm.umm-spatial :as umm-s]
-            [cmr.common.util :refer [are2]]
-            [cmr.common-app.test.side-api :as side]
-            [cmr.spatial.polygon :as poly]
-            [cmr.spatial.point :as p]
-            [cmr.spatial.line-string :as l]
-            [cmr.spatial.mbr :as m]
-            [cmr.ingest.config :as icfg]
-            [cmr.ingest.services.messages :as msg]
-            [cmr.common.mime-types :as mime-types]
-            [cmr.system-int-test.utils.index-util :as index]
-            [cmr.system-int-test.utils.search-util :as search]
-            [clojure.java.io :as io]))
+  (:require 
+    [clojure.test :refer :all]
+    [cmr.system-int-test.utils.ingest-util :as ingest]
+    [cmr.system-int-test.data2.collection :as dc]
+    [cmr.system-int-test.data2.granule :as dg]
+    [cmr.system-int-test.data2.core :as d]
+    [cmr.common.mime-types :as mt]
+    [cmr.umm.umm-collection :as c]
+    [cmr.umm.umm-spatial :as umm-s]
+    [cmr.common.util :refer [are2]]
+    [cmr.common.util :as u :refer [are3]]
+    [cmr.common-app.test.side-api :as side]
+    [cmr.spatial.polygon :as poly]
+    [cmr.spatial.point :as p]
+    [cmr.spatial.line-string :as l]
+    [cmr.spatial.mbr :as m]
+    [cmr.ingest.config :as icfg]
+    [cmr.ingest.services.messages :as msg]
+    [cmr.common.mime-types :as mime-types]
+    [cmr.system-int-test.utils.index-util :as index]
+    [cmr.system-int-test.utils.search-util :as search]
+    [clojure.java.io :as io]))
 
 (use-fixtures :each (ingest/reset-fixture {"provguid1" "PROV1" "provguid2" "PROV2"}))
 

     (is (= {:status 422
             :errors [{:path field-path
                       :errors errors}]}
            (select-keys response [:status :errors]))))))

(defn assert-invalid-keywords

    (assert-valid {:product-specific-attributes [(dc/psa {:name "bool1" :data-type :boolean :value true})
                                                 (dc/psa {:name "bool2" :data-type :boolean :value true})]}))

  ;; testing enabling umm json schema validation through Cmr-Validation-Umm-C header
  (let [coll-attr {:product-specific-attributes
                   [(dc/psa {:name "bool1" :data-type :boolean :value true})
                    (dc/psa {:name "bool2" :data-type :boolean :value true})]}]
    (side/eval-form `(icfg/set-return-umm-json-validation-errors! false))
    (do (are3 [coll-attributes error options]
              (do (assert-invalid-schema coll-attributes error options)
                  (assert-valid coll-attributes))
             
              "Set Cmr-Validate-Umm-C header to true - schema validation error is returned"
              coll-attr
              "object has missing required properties ([\"ProcessingLevel\",\"RelatedUrls\",\"ScienceKeywords\",\"SpatialExtent\"])"
              {:validate-umm-c true})

        (are3 [coll-attributes options] 
              (do (assert-valid coll-attributes options)
                  (assert-valid coll-attributes))

              "Set Cmr-Validate-Umm-C header to false - schema validation error is not returned"  
              coll-attr
              {:allow-failure? true :validate-umm-c false} 

              "Do not set Cmr-Validate-Umm-C header - schema validation error is not returned"
              coll-attr
              {:allow-failure? true})))     

  ;; testing enabling umm-spec validation through Cmr-Validation-Umm-C header
  (let [coll-attr {:processing-level-id "1"
                   :science-keywords [(dc/science-keyword {:category "upcase"
                                                           :topic "Cool"
                                                           :term "Mild"})]
                   :spatial-coverage (dc/spatial {:gsr :cartesian})
                   :related-urls [(dc/related-url {:type "type" :url "http://www.foo.com"})]
                   :product-specific-attributes
                   [(dc/psa {:name "bool" :data-type :boolean :value true})
                    (dc/psa {:name "bool" :data-type :boolean :value true})]}]
    (side/eval-form `(icfg/set-return-umm-json-validation-errors! false))
    (side/eval-form `(icfg/set-return-umm-spec-validation-errors! false))
    (are3 [coll-attributes field-path error options]
          (assert-invalid coll-attributes field-path error options)

          "Set Cmr-Validate-Umm-C header to true - schema validation passed, umm-spec validation error is returned"
          coll-attr
          ["AdditionalAttributes"]
          ["Additional Attributes must be unique. This contains duplicates named [bool]."]
          {:validate-umm-c true}

          "Set Cmr-Validate-Umm-C header to false - schema validation passed, umm-spec validation error is not returned"
          coll-attr
          ["ProductSpecificAttributes"]
          ["Product Specific Attributes must be unique. This contains duplicates named [bool]."]
          {:validate-umm-c false}
           
          "Do not set Cmr-Validate-Umm-C header - schema validation passed, umm-spec validation error is not returned"
          coll-attr
          ["ProductSpecificAttributes"]
          ["Product Specific Attributes must be unique. This contains duplicates named [bool]."]       
          nil))

  (side/eval-form `(icfg/set-return-umm-spec-validation-errors! true))

