<commit_before>(ns cmr.mock-echo.api.urs
  "Defines routes for mocking URS"
  (:require [compojure.core :refer :all]
            [cheshire.core :as json]
            [clojure.data.xml :as x]
            [cmr.common.xml :as cx]
            [cmr.common.mime-types :as mt]
            [clojure.string :as str]
            [cmr.common.services.errors :as errors]
            [cmr.mock-echo.data.urs-db :as urs-db]
            [clojure.data.codec.base64 :as b64]
            [ring.util.response :as rsp]))

(defn get-user
  "Processes a request to get a user."
  [context name]
  (if (urs-db/user-exists? context name)
    (let [{:keys [username password email affiliation studyArea country phone firstName lastName
                  organization userType address1 address2 address3 city state zip]}
          (urs-db/get-user context name)]
      {:status 200 :body (str
                          "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>
                           <user>
                            <accountCreatedDate>2003-08-24T00:00:00Z</accountCreatedDate>
                            <accountStatus>ACTIVE</accountStatus>"
                            (if address1 (str "<address1>" address1 "</address1>"))
                            (if address2 (str "<address2>" address2 "</address2>"))
                            (if address3 (str "<address3>" address3 "</address3>"))
                            (if affiliation (str "<affiliation>" affiliation "</affiliation>"))
                            (if city (str "<city>" city "</city>"))
                            (if country (str "<country>" country "</country>"))
                            "<emailAddress>" email "</emailAddress>"
                            (if firstName (str "<firstName>" firstName "</firstName>"))
                            (if lastName (str "<lastName>" lastName "</lastName>"))
                            (if state (str "<state>" state "</state>"))
                            (if studyArea (str "<studyArea>" studyArea "</studyArea>"))
                            (if organization (str "<organization>" organization "</organization>"))
                            (if phone (str "<phone>" phone "</phone>"))
                            "<userName>" username "</userName>"
                            (if userType (str "<userType>" userType "</userType>"))
                            (if zip (str "<zip>" zip "</zip>"))
                          "</user>")
       :headers {"content-type" mt/xml}})
    {:status 404 :body "Not found.\n"}))

(defn create-users-json
  "Processes a request to create multiple users."
  [context body]
  (println "JSON BODY = " body)
  (let [users (json/decode body true)]
    (urs-db/create-users context users)))

(defn parse-user-request-xml
  "Parses a login request into a map of username and password"<commit_msg>CMR-2920 fix http status for create user with json<commit_after>(ns cmr.mock-echo.api.urs
  "Defines routes for mocking URS"
  (:require [compojure.core :refer :all]
            [cheshire.core :as json]
            [clojure.data.xml :as x]
            [cmr.common.xml :as cx]
            [cmr.common.mime-types :as mt]
            [clojure.string :as str]
            [cmr.common.services.errors :as errors]
            [cmr.mock-echo.data.urs-db :as urs-db]
            [clojure.data.codec.base64 :as b64]
            [ring.util.response :as rsp]))

(defn get-user
  "Processes a request to get a user."
  [context name]
  (if (urs-db/user-exists? context name)
    (let [{:keys [username password email affiliation studyArea country phone firstName lastName
                  organization userType address1 address2 address3 city state zip]}
          (urs-db/get-user context name)]
      {:status 200 :body (str
                          "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>
                           <user>
                            <accountCreatedDate>2003-08-24T00:00:00Z</accountCreatedDate>
                            <accountStatus>ACTIVE</accountStatus>"
                            (if address1 (str "<address1>" address1 "</address1>"))
                            (if address2 (str "<address2>" address2 "</address2>"))
                            (if address3 (str "<address3>" address3 "</address3>"))
                            (if affiliation (str "<affiliation>" affiliation "</affiliation>"))
                            (if city (str "<city>" city "</city>"))
                            (if country (str "<country>" country "</country>"))
                            "<emailAddress>" email "</emailAddress>"
                            (if firstName (str "<firstName>" firstName "</firstName>"))
                            (if lastName (str "<lastName>" lastName "</lastName>"))
                            (if state (str "<state>" state "</state>"))
                            (if studyArea (str "<studyArea>" studyArea "</studyArea>"))
                            (if organization (str "<organization>" organization "</organization>"))
                            (if phone (str "<phone>" phone "</phone>"))
                            "<userName>" username "</userName>"
                            (if userType (str "<userType>" userType "</userType>"))
                            (if zip (str "<zip>" zip "</zip>"))
                          "</user>")
       :headers {"content-type" mt/xml}})
    {:status 404 :body "Not found.\n"}))

(defn create-users-json
  "Processes a request to create multiple users."
  [context body]
  (println "JSON BODY = " body)
  (let [users (json/decode body true)]
    (urs-db/create-users context users)
    {:status 201}))

(defn parse-user-request-xml
  "Parses a login request into a map of username and password"
{'fraction_non_alphanumeric': 0.13797885248743283, 'fraction_numerical': 0.01577396429190501, 'mean_word_length': 1.6812267657992566, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 2, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1876482', 'n_tokens_mistral': 1585, 'n_tokens_neox': 1529, 'n_words': 451}