<commit_before>(ns com.puppetlabs.puppetdb.test.http.v2.facts
  (:require [com.puppetlabs.puppetdb.scf.storage :as scf-store]
            [com.puppetlabs.http :as pl-http]
            [cheshire.core :as json]
            [clojure.java.jdbc :as sql]
            [com.puppetlabs.puppetdb.http.server :as server]
            [clojure.java.io :as io])
  (:use clojure.test
        ring.mock.request
        [com.puppetlabs.puppetdb.fixtures]
        [com.puppetlabs.puppetdb.examples]
        [clj-time.core :only [now]]
        [com.puppetlabs.jdbc :only (with-transacted-connection)]))

(use-fixtures :each with-test-db with-http-app)

(def c-t pl-http/json-response-content-type)

(defn get-request
  "Return a GET request against path, suitable as an argument to a ring
  app. Params supported are content-type and query-string."
  ([path] (get-request path {}))
  ([path params]
   (let [request (request :get path params)]
     (update-in request [:headers] assoc "Accept" c-t))))

(defn get-response
  ([]      (get-response nil))
  ([query] (*app* (get-request "/v2/facts" query))))

(deftest fact-queries
  (let [facts1 {"domain" "testing.com"
                "hostname" "foo1"
                "kernel" "Linux"
                "operatingsystem" "Debian"
                "some_version" "1.3.7+build.11.e0f985a"
                "uptime_seconds" "4000"}
        facts2 {"domain" "testing.com"
                "hostname" "foo2"
                "kernel" "Linux"
                "operatingsystem" "RedHat"
                "uptime_seconds" "6000"}
        facts3 {"domain" "testing.com"
                "hostname" "foo3"
                "kernel" "Darwin"
                "operatingsystem" "Darwin"}
        facts4 {"domain" "testing.com"
                "hostname" "foo4"
                "kernel" "Linux"
                "operatingsystem" "RedHat"
                "uptime_seconds" "6000"}]
    (with-transacted-connection *db*
      (scf-store/add-certname! "foo1")
      (scf-store/add-certname! "foo2")
      (scf-store/add-certname! "foo3")
      (scf-store/add-certname! "foo4")
      (scf-store/add-facts! "foo1" facts1 (now))
      (scf-store/add-facts! "foo2" facts2 (now))
      (scf-store/add-facts! "foo3" facts3 (now))
      (scf-store/add-facts! "foo4" facts3 (now))
      (scf-store/deactivate-node! "foo4"))

    (testing "query without param should not fail"
      (let [response (get-response)
            body     (get response :body "null")]
        (is (= 200 (:status response)))))

    (testing "fact queries"
      (testing "well-formed queries"
        (doseq [[query result] {nil
                                [{:certname "foo1" :name "domain" :value "testing.com"}
                                 {:certname "foo1" :name "hostname" :value "foo1"}
                                 {:certname "foo1" :name "kernel" :value "Linux"}
                                 {:certname "foo1" :name "operatingsystem" :value "Debian"}
                                 {:certname "foo1" :name "some_version" :value "1.3.7+build.11.e0f985a"}
                                 {:certname "foo1" :name "uptime_seconds" :value "4000"}
                                 {:certname "foo2" :name "domain" :value "testing.com"}
                                 {:certname "foo2" :name "hostname" :value "foo2"}
                                 {:certname "foo2" :name "kernel" :value "Linux"}
                                 {:certname "foo2" :name "operatingsystem" :value "RedHat"}
                                 {:certname "foo2" :name "uptime_seconds" :value "6000"}
                                 {:certname "foo3" :name "domain" :value "testing.com"}
                                 {:certname "foo3" :name "hostname" :value "foo3"}
                                 {:certname "foo3" :name "kernel" :value "Darwin"}
                                 {:certname "foo3" :name "operatingsystem" :value "Darwin"}]

                                ["=" "name" "domain"]
                                [{:certname "foo1" :name "domain" :value "testing.com"}
                                 {:certname "foo2" :name "domain" :value "testing.com"}
                                 {:certname "foo3" :name "domain" :value "testing.com"}]<commit_msg>Fix for the current unit test hanging issue.<commit_after>(ns com.puppetlabs.puppetdb.test.http.v2.facts
  (:require [com.puppetlabs.puppetdb.scf.storage :as scf-store]
            [com.puppetlabs.http :as pl-http]
            [cheshire.core :as json]
            [clojure.java.jdbc :as sql]
            [com.puppetlabs.puppetdb.http.server :as server]
            [clojure.java.io :as io])
  (:use clojure.test
        ring.mock.request
        [com.puppetlabs.puppetdb.fixtures]
        [com.puppetlabs.puppetdb.examples]
        [clj-time.core :only [now]]
        [com.puppetlabs.jdbc :only (with-transacted-connection)]))

(use-fixtures :each with-test-db with-http-app)

(def c-t pl-http/json-response-content-type)

(defn get-request
  "Return a GET request against path, suitable as an argument to a ring
  app. Params supported are content-type and query-string."
  ([path] (get-request path {}))
  ([path params]
   (let [request (request :get path params)]
     (update-in request [:headers] assoc "Accept" c-t))))

(defn get-response
  ([]      (get-response nil))
  ([query] (*app* (get-request "/v2/facts" query))))

(deftest fact-queries
  (let [facts1 {"domain" "testing.com"
                "hostname" "foo1"
                "kernel" "Linux"
                "operatingsystem" "Debian"
                "some_version" "1.3.7+build.11.e0f985a"
                "uptime_seconds" "4000"}
        facts2 {"domain" "testing.com"
                "hostname" "foo2"
                "kernel" "Linux"
                "operatingsystem" "RedHat"
                "uptime_seconds" "6000"}
        facts3 {"domain" "testing.com"
                "hostname" "foo3"
                "kernel" "Darwin"
                "operatingsystem" "Darwin"}
        facts4 {"domain" "testing.com"
                "hostname" "foo4"
                "kernel" "Linux"
                "operatingsystem" "RedHat"
                "uptime_seconds" "6000"}]
    (with-transacted-connection *db*
      (scf-store/add-certname! "foo1")
      (scf-store/add-certname! "foo2")
      (scf-store/add-certname! "foo3")
      (scf-store/add-certname! "foo4")
      (scf-store/add-facts! "foo1" facts1 (now))
      (scf-store/add-facts! "foo2" facts2 (now))
      (scf-store/add-facts! "foo3" facts3 (now))
      (scf-store/add-facts! "foo4" facts3 (now))
      (scf-store/deactivate-node! "foo4"))

    (testing "query without param should not fail"
      (let [response (get-response)
            body     (get response :body "null")]
        (slurp body)
        (is (= 200 (:status response)))))

    (testing "fact queries"
      (testing "well-formed queries"
        (doseq [[query result] {nil
                                [{:certname "foo1" :name "domain" :value "testing.com"}
                                 {:certname "foo1" :name "hostname" :value "foo1"}
                                 {:certname "foo1" :name "kernel" :value "Linux"}
                                 {:certname "foo1" :name "operatingsystem" :value "Debian"}
                                 {:certname "foo1" :name "some_version" :value "1.3.7+build.11.e0f985a"}
                                 {:certname "foo1" :name "uptime_seconds" :value "4000"}
                                 {:certname "foo2" :name "domain" :value "testing.com"}
                                 {:certname "foo2" :name "hostname" :value "foo2"}
                                 {:certname "foo2" :name "kernel" :value "Linux"}
                                 {:certname "foo2" :name "operatingsystem" :value "RedHat"}
                                 {:certname "foo2" :name "uptime_seconds" :value "6000"}
                                 {:certname "foo3" :name "domain" :value "testing.com"}
                                 {:certname "foo3" :name "hostname" :value "foo3"}
                                 {:certname "foo3" :name "kernel" :value "Darwin"}
                                 {:certname "foo3" :name "operatingsystem" :value "Darwin"}]

                                ["=" "name" "domain"]
                                [{:certname "foo1" :name "domain" :value "testing.com"}
                                 {:certname "foo2" :name "domain" :value "testing.com"}
                                 {:certname "foo3" :name "domain" :value "testing.com"}]
{'fraction_non_alphanumeric': 0.14220612292591728, 'fraction_numerical': 0.019864454311755084, 'mean_word_length': 1.7111181501425403, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1557603', 'n_tokens_mistral': 2840, 'n_tokens_neox': 2700, 'n_words': 610}