<commit_before>(ns buildviz.jenkins.sync-jobs-test
  (:require [buildviz.jenkins.sync-jobs :as sut]
            [buildviz.util.url :as url]
            [cheshire.core :as j]
            [clj-http.fake :as fake]
            [clj-time.core :as t]
            [clojure.test :refer :all]))

(defn- successful-json-response [body]
  (fn [_] {:status 200
           :body (j/generate-string body)}))

(defn- a-job [name]
  {:name name})

(defn- a-view [& jobs]
  [["http://jenkins:4321/api/json"
    (successful-json-response {:jobs jobs})]])

(defn- a-job-with-builds [job-name & builds]
  (let [job-builds [(format "http://jenkins:4321/job/%s/api/json?tree=allBuilds%%5Bnumber,timestamp,duration,result,actions%%5BlastBuiltRevision%%5BSHA1%%5D,remoteUrls,parameters%%5Bname,value%%5D,causes%%5BupstreamProject,upstreamBuild%%5D%%5D%%5D%%7B0,10%%7D"
                            job-name)
                    (successful-json-response {:allBuilds []})]]
    [job-builds]))


(defn- serve-up [& routes]
  (->> routes
       (mapcat identity)
       (into {})))

(def beginning-of-2016 (t/date-midnight 2016))


(deftest test-sync-jobs
  (testing "should handle no jobs "
    (let [store (atom [])]
      (fake/with-fake-routes-in-isolation (serve-up (a-view))
        (with-out-str (sut/sync-jobs (url/url "http://jenkins:4321") (url/url "http://buildviz:8010") beginning-of-2016)))
      (is (= []
             @store))))

  (testing "should handle no builds"
    (let [store (atom [])]
      (fake/with-fake-routes-in-isolation (serve-up (a-view (a-job "some_job"))
                                                    (a-job-with-builds "some_job"))
        (with-out-str (sut/sync-jobs (url/url "http://jenkins:4321") (url/url "http://buildviz:8010") beginning-of-2016)))
      (is (= []
             @store)))))<commit_msg>End-to-end test for simple import of Jenkins job<commit_after>(ns buildviz.jenkins.sync-jobs-test
  (:require [buildviz.jenkins.sync-jobs :as sut]
            [buildviz.util.url :as url]
            [cheshire.core :as j]
            [clj-http.fake :as fake]
            [clj-time
             [coerce :as tc]
             [core :as t]]
            [clojure.test :refer :all]))

(defn- successful-json-response [body]
  (fn [_] {:status 200
           :body (j/generate-string body)}))

(defn- a-job [name]
  {:name name})

(defn- a-view [& jobs]
  [["http://jenkins:4321/api/json"
    (successful-json-response {:jobs jobs})]])

(defn- a-job-with-builds [job-name & builds]
  (let [job-builds [(format "http://jenkins:4321/job/%s/api/json?tree=allBuilds%%5Bnumber,timestamp,duration,result,actions%%5BlastBuiltRevision%%5BSHA1%%5D,remoteUrls,parameters%%5Bname,value%%5D,causes%%5BupstreamProject,upstreamBuild%%5D%%5D%%5D%%7B0,10%%7D"
                            job-name)
                    (successful-json-response {:allBuilds builds})]
        test-results (map (fn [build]
                            [(format "http://jenkins:4321/job/%s/%s/testReport/api/json" job-name (:number build))
                             (fn [_] {:status 404 :body ""})]) builds)]
    (cons job-builds test-results)))

(defn- provide-buildviz-and-capture-puts [latest-build-start map-ref]
  [[#"http://buildviz:8010/builds/(.+)/(.+)"
    (fn [req]
      (swap! map-ref #(conj % [(:uri req)
                               (j/parse-string (slurp (:body req)) true)]))
      {:status 200 :body ""})]
   ["http://buildviz:8010/status"
    (successful-json-response (cond-> {}
                                latest-build-start (assoc :latestBuildStart (tc/to-long latest-build-start))))]])

(defn- serve-up [& routes]
  (->> routes
       (mapcat identity)
       (into {})))

(def beginning-of-2016 (t/date-midnight 2016))


(deftest test-sync-jobs
  (testing "should handle no jobs "
    (let [store (atom [])]
      (fake/with-fake-routes-in-isolation (serve-up (a-view))
        (with-out-str (sut/sync-jobs (url/url "http://jenkins:4321") (url/url "http://buildviz:8010") beginning-of-2016)))
      (is (= []
             @store))))

  (testing "should handle no builds"
    (let [store (atom [])]
      (fake/with-fake-routes-in-isolation (serve-up (a-view (a-job "some_job"))
                                                    (a-job-with-builds "some_job"))
        (with-out-str (sut/sync-jobs (url/url "http://jenkins:4321") (url/url "http://buildviz:8010") beginning-of-2016)))
      (is (= []
             @store))))

  (testing "should sync a simple build"
    (let [store (atom [])]
      (fake/with-fake-routes-in-isolation (serve-up (a-view (a-job "some_job"))
                                                    (a-job-with-builds "some_job" {:number "21"
                                                                                   :timestamp 1493201298062
                                                                                   :duration 10200
                                                                                   :result "SUCCESS"})
                                                    (provide-buildviz-and-capture-puts beginning-of-2016 store))
        (with-out-str (sut/sync-jobs (url/url "http://jenkins:4321") (url/url "http://buildviz:8010") beginning-of-2016)))
      (is (= [["/builds/some_job/21" {:start 1493201298062
                                      :end 1493201308262
                                      :outcome "pass"}]]
             @store)))))
{'fraction_non_alphanumeric': 0.17032662852924893, 'fraction_numerical': 0.036907178446207785, 'mean_word_length': 1.9202586206896552, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 21, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5537487', 'n_tokens_mistral': 1934, 'n_tokens_neox': 1778, 'n_words': 320}