<commit_before>(ns com.sixsq.slipstream.ssclj.resources.connector-test-utils
  (:require
    [clojure.test :refer :all]
    [clojure.data.json :as json]
    [peridot.core :refer :all]

    [com.sixsq.slipstream.ssclj.resources.lifecycle-test-utils :as ltu]
    [com.sixsq.slipstream.ssclj.app.params :as p]
    [com.sixsq.slipstream.ssclj.app.routes :as routes]
    [com.sixsq.slipstream.ssclj.middleware.authn-info-header :refer [authn-info-header]]
    [com.sixsq.slipstream.ssclj.resources.common.crud :as crud]
    [com.sixsq.slipstream.ssclj.resources.common.debug-utils :as du]
    [com.sixsq.slipstream.ssclj.resources.common.schema :as c]
    [com.sixsq.slipstream.ssclj.resources.common.utils :as u]
    [com.sixsq.slipstream.ssclj.resources.connector :as con]
    [com.sixsq.slipstream.ssclj.resources.connector-template :as ct]
    ))

(def base-uri (str p/service-context (u/de-camelcase con/resource-name)))
(def tpl-base-uri (str p/service-context (u/de-camelcase ct/resource-name)))

(defn new-instance-name
  [cloud-service-type]
  (str cloud-service-type "-" (System/currentTimeMillis)))

(defn ring-app
  []
  (ltu/make-ring-app (ltu/concat-routes [(routes/get-main-routes)])))

(defn strip-unwanted-attrs
  [m]
  (let [unwanted #{:id :resourceURI :acl :operations
                   :created :updated :name :description}]
    (into {} (remove #(unwanted (first %)) m))))

;;
;; Tests.
;;

(defn connector-lifecycle
  [cloud-service-type]
  (let [href           (str ct/resource-url "/" cloud-service-type)
        template-url   (str p/service-context ct/resource-url "/" cloud-service-type)
        resp           (-> (session (ring-app))
                           (content-type "application/json")
                           (header authn-info-header "root ADMIN")
                           (request template-url)
                           (ltu/body->edn)
                           (ltu/is-status 200))
        template       (get-in resp [:response :body])
        valid-create   {:connectorTemplate (-> template
                                               strip-unwanted-attrs
                                               (assoc :endpoint "http://example.com")
                                               (assoc :instanceName (new-instance-name cloud-service-type)))}
        href-create    {:connectorTemplate {:href         href
                                            :endpoint     "http://google.com"
                                            :instanceName (new-instance-name cloud-service-type)}}
        invalid-create (assoc-in valid-create [:connectorTemplate :invalid] "BAD")]

    ;; admin create with invalid template fails
    (-> (session (ring-app))
        (content-type "application/json")
        (header authn-info-header "root ADMIN")
        (request base-uri
                 :request-method :post
                 :body (json/write-str invalid-create))
        (ltu/body->edn)
        (ltu/is-status 400))

    ;; full connector lifecycle as administrator should work
    (let [uri     (-> (session (ring-app))
                      (content-type "application/json")
                      (header authn-info-header "root ADMIN")
                      (request base-uri
                               :request-method :post
                               :body (json/write-str valid-create))
                      (ltu/body->edn)
                      (ltu/is-status 201)<commit_msg>some connectors don't use :endpoint parameter.<commit_after>(ns com.sixsq.slipstream.ssclj.resources.connector-test-utils
  (:require
    [clojure.test :refer :all]
    [clojure.data.json :as json]
    [peridot.core :refer :all]

    [com.sixsq.slipstream.ssclj.resources.lifecycle-test-utils :as ltu]
    [com.sixsq.slipstream.ssclj.app.params :as p]
    [com.sixsq.slipstream.ssclj.app.routes :as routes]
    [com.sixsq.slipstream.ssclj.middleware.authn-info-header :refer [authn-info-header]]
    [com.sixsq.slipstream.ssclj.resources.common.crud :as crud]
    [com.sixsq.slipstream.ssclj.resources.common.debug-utils :as du]
    [com.sixsq.slipstream.ssclj.resources.common.schema :as c]
    [com.sixsq.slipstream.ssclj.resources.common.utils :as u]
    [com.sixsq.slipstream.ssclj.resources.connector :as con]
    [com.sixsq.slipstream.ssclj.resources.connector-template :as ct]
    ))

(def base-uri (str p/service-context (u/de-camelcase con/resource-name)))
(def tpl-base-uri (str p/service-context (u/de-camelcase ct/resource-name)))

(defn new-instance-name
  [cloud-service-type]
  (str cloud-service-type "-" (System/currentTimeMillis)))

(defn ring-app
  []
  (ltu/make-ring-app (ltu/concat-routes [(routes/get-main-routes)])))

(defn strip-unwanted-attrs
  [m]
  (let [unwanted #{:id :resourceURI :acl :operations
                   :created :updated :name :description}]
    (into {} (remove #(unwanted (first %)) m))))

;;
;; Tests.
;;

(defn connector-lifecycle
  [cloud-service-type]
  (let [href           (str ct/resource-url "/" cloud-service-type)
        template-url   (str p/service-context ct/resource-url "/" cloud-service-type)
        resp           (-> (session (ring-app))
                           (content-type "application/json")
                           (header authn-info-header "root ADMIN")
                           (request template-url)
                           (ltu/body->edn)
                           (ltu/is-status 200))
        template       (get-in resp [:response :body])
        valid-create   {:connectorTemplate (-> template
                                               strip-unwanted-attrs
                                               (assoc :instanceName (new-instance-name cloud-service-type)))}
        href-create    {:connectorTemplate {:href         href
                                            :instanceName (new-instance-name cloud-service-type)}}
        invalid-create (assoc-in valid-create [:connectorTemplate :invalid] "BAD")]

    ;; admin create with invalid template fails
    (-> (session (ring-app))
        (content-type "application/json")
        (header authn-info-header "root ADMIN")
        (request base-uri
                 :request-method :post
                 :body (json/write-str invalid-create))
        (ltu/body->edn)
        (ltu/is-status 400))

    ;; full connector lifecycle as administrator should work
    (let [uri     (-> (session (ring-app))
                      (content-type "application/json")
                      (header authn-info-header "root ADMIN")
                      (request base-uri
                               :request-method :post
                               :body (json/write-str valid-create))
                      (ltu/body->edn)
                      (ltu/is-status 201)
{'fraction_non_alphanumeric': 0.13097528488974397, 'fraction_numerical': 0.0026639040994524197, 'mean_word_length': 1.8745214802211825, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6442663', 'n_tokens_mistral': 2094, 'n_tokens_neox': 2032, 'n_words': 385}