(ns document-storage.postgres.caching-test
  (:require [clojure.test :refer :all]
            [document-storage.core :as ds]
            [document-storage.postgres.core :as dspostgres]
            [clojure.java.io :as io]
            [fipp.edn :refer [pprint] :rename {pprint fipp}]
            [document-storage.protocol :as dsprot]
            [environ.core :refer [env]]
            [document-storage.protocol-test :as protocol-tests]
            [document-storage.postgres.core-test :refer [ds-atom storage-fixture]]
            [document-storage.postgres.documents :as pgdocs]
            [document-storage.testdata :refer [generate-document]]
            [clj-helper.string :refer [get-unique-id]]))



(use-fixtures :once storage-fixture)

(deftest test-repository-caching
  (testing "check if documents are cached and reset cache will remove cached documents"
    (let [repo1name (str (gensym "r1"))
          repo2name (str (gensym "r2"))]
      (.create-repository @ds-atom {:name repo1name
                                    :caching true
                                    :versioning true})
      (.create-repository @ds-atom {:name repo2name
                                    :caching true
                                    :versioning true})
      (let [docname1 (get-unique-id "tdoc")
            new-document1 (generate-document)
            docname2 (get-unique-id "tdoc")
            new-document2 (generate-document)]
        ;;;Create Testdocument in each repository
        (.save-document @ds-atom docname1 new-document1 repo1name)
        (.save-document @ds-atom docname2 new-document2 repo2name)
        (let [last-version1 (pgdocs/get-last-document-version @ds-atom docname1 repo1name)
              last-version2 (pgdocs/get-last-document-version @ds-atom docname2 repo2name)]
          ;;; Check if inital versions are not cached
          (is (nil? (pgdocs/get-cached-document @ds-atom repo1name docname1 last-version1)) "Initial documents are not cached")
          (is (nil? (pgdocs/get-cached-document @ds-atom repo2name docname2 last-version2)) "Initial documents are not cached")
          ;;; Load doc1 and check if its cached - doc2 should not be cached
          (.load-document @ds-atom docname1 repo1name)
          (is (not (nil? (pgdocs/get-cached-document @ds-atom repo1name docname1 last-version1))) "Documents should be cached after being loaded at least once")
          (is (nil? (pgdocs/get-cached-document @ds-atom repo2name docname2 last-version2)) "Unloaded docs are not cached")
          ;;; Now load doc1 and check if its also cached
          (.load-document @ds-atom docname2 repo2name)
          (is (not (nil? (pgdocs/get-cached-document @ds-atom repo2name docname2 last-version2))) "Documents should be cached after being loaded at least once")

          ;;; Reset repository cache and check if its only reset the chosen repository cache
          (.reset-repository-cache @ds-atom repo1name)
          (is (nil? (pgdocs/get-cached-document @ds-atom repo1name docname1 last-version1)) "Documents should not be cached after repository-cache reset")
          (is (not (nil? (pgdocs/get-cached-document @ds-atom repo2name docname2 last-version2))) "Documents should be cached if another repository is reset")
          ;;; Reset complete cache and check if it cleared the remaining cache for repo2
          (.reset-cache @ds-atom)
          (is (nil? (pgdocs/get-cached-document @ds-atom repo2name docname2 last-version2)) "Documents should not be cached if the whole cache was reset")))
      (.delete-repository @ds-atom repo1name)
      (.delete-repository @ds-atom repo2name))))