 ;; Test for mixed parameters
 (deftest mixed-paramter-types
   (is (thrown-with-msg? ExceptionInfo (tu/message->regex (a-msg/mixed-legacy-and-cmr-style-parameters-msg))
-                           (lp/process-legacy-psa {:attribute ["string,abc,xyz"
-                                                               {:name "PDQ"}
-                                                               {:type "string"}
-                                                               {:value "ABC"}]}
-                                                  (legacy-psa-map->legacy-psa-query
-                                                    {:name "PDQ" :type "string" :value "ABC"})))))
+                        (lp/process-legacy-psa {:attribute ["string,abc,xyz"
+                                                            {:name "PDQ"}
+                                                            {:type "string"}
+                                                            {:value "ABC"}]}
+                                               (legacy-psa-maps->legacy-psa-query
+                                                 [{:name "PDQ" :type "string" :value "ABC"}])))))
 
 
 

(ns cmr.search.test.services.legacy-parameters
  (:require [clojure.test :refer :all]
            [cmr.search.services.legacy-parameters :as lp]
            [clojure.test.check.generators :as gen]
            [clojure.test.check.properties :refer [for-all]]
            [ring.util.codec :as rc]
            [cmr.search.services.messages.attribute-messages :as a-msg]

  (import clojure.lang.ExceptionInfo))


(defn- legacy-psa-maps->legacy-psa-query
  "Convert a vector of maps of psa fields into a legacy psa url query string"
  [psa-maps]
  (reduce (fn [full-query psa-map]
            (let [{:keys [name type value minValue maxValue]} psa-map
                  [name type value minValue maxValue] (map #(when % (rc/url-encode %))
                                                           [name type value minValue maxValue])
                  query (format "attribute%%5B%%5D%%5Bname%%5D=%s&attribute%%5B%%5D%%5Btype%%5D=%s"
                                name
                                type)
                  query (if value
                          (str query "&attribute%5B%5D%5Bvalue%5D=" value)
                          (str query
                               (when minValue (str "&attribute%5B%5D%5BminValue%5D=" minValue))
                               (when maxValue (str "&attribute%5B%5D%5BmaxValue%5D=" maxValue))))]
              (if full-query
                (str full-query "&" query)
                query)))
          nil
          psa-maps))

(legacy-psa-maps->legacy-psa-query [{:name "name1" :value "value1" :type "string"}
                                    {:name "name2" :minValue "min" :type "string"}])


(defn- legacy-psa-maps->cmr-psa-query
  "Converts a vector of maps of psa fields into a string with several attributes as comma-separated
  values"
  [psa-maps]
  (map (fn [psa-map]
         (let [{:keys [name type value minValue maxValue]} psa-map
               [name type value minValue maxValue] (map #(when % (s/replace % "," "\\,"))
                                                        [name type value minValue maxValue])
               query (format "%s,%s" type name)
               query (if value
                       (str query "," value)
                       (str query "," minValue "," maxValue))]
           query))
       psa-maps))

;; NOTE - these don't generate valid psa fields, but that doesn't matter because we are only
;; testing that the legacy->cmr functionality reformats the query parameters correctly.
