|
3 | 3 | (:require |
4 | 4 | [clojure.test :refer [deftest is testing]] |
5 | 5 | [cmr.common.services.search.query-model :as qm] |
| 6 | + [cmr.elastic-utils.es-helper :as es-helper] |
6 | 7 | [cmr.elastic-utils.search.es-group-query-conditions :as gc] |
7 | 8 | [cmr.elastic-utils.search.es-index :as es-index])) |
8 | 9 |
|
| 10 | +(def clause-limit-message |
| 11 | + "The search is creating more clauses than allowed by CMR. Please narrow your search.") |
| 12 | + |
| 13 | +(defn- send-query-with-es-error |
| 14 | + [status body] |
| 15 | + (with-redefs [es-index/context->conn (constantly nil) |
| 16 | + es-helper/search (fn [& _] |
| 17 | + (throw (ex-info "Elasticsearch request failed" |
| 18 | + {:status status |
| 19 | + :body body})))] |
| 20 | + (#'es-index/do-send-with-retry |
| 21 | + {} |
| 22 | + {:index-name "test-index" :type-name "collection"} |
| 23 | + {} |
| 24 | + 1))) |
| 25 | + |
| 26 | +(deftest clause-limit-errors-are-payload-too-large |
| 27 | + (doseq [[description body] |
| 28 | + [["Elasticsearch 7 default clause limit error" |
| 29 | + "maxClauseCount is set to 1024"] |
| 30 | + ["Elasticsearch 7 configured clause limit error" |
| 31 | + "maxClauseCount is set to 4096"] |
| 32 | + ["Elasticsearch 8 clause limit error" |
| 33 | + (str "{\"error\":{\"root_cause\":[{\"type\":\"illegal_argument_exception\"," |
| 34 | + "\"reason\":\"Query rewrite failed: too many clauses\"}]},\"status\":400}")]]] |
| 35 | + (testing description |
| 36 | + (let [exception (try |
| 37 | + (send-query-with-es-error 400 body) |
| 38 | + nil |
| 39 | + (catch clojure.lang.ExceptionInfo e |
| 40 | + e))] |
| 41 | + (is (= :payload-too-large (:type (ex-data exception)))) |
| 42 | + (is (= [clause-limit-message] (:errors (ex-data exception)))))))) |
| 43 | + |
| 44 | +(deftest clause-limit-errors-require-bad-request-status |
| 45 | + (let [exception (try |
| 46 | + (send-query-with-es-error 500 "Query rewrite failed: too many clauses") |
| 47 | + nil |
| 48 | + (catch clojure.lang.ExceptionInfo e |
| 49 | + e))] |
| 50 | + (is (nil? (:type (ex-data exception)))) |
| 51 | + (is (= 500 (:status (ex-data (ex-cause exception))))))) |
| 52 | + |
9 | 53 | (deftest test-query->execution-params |
10 | 54 | (let [query->execution-params #'es-index/query->execution-params |
11 | 55 | condition (gc/or-conds (map #(qm/string-conditions :consortiums [%]) |
|
0 commit comments