Skip to content

Commit

Permalink
Add unit tests for Connections sorting
Browse files Browse the repository at this point in the history
- Renamed source and target Relationship fields to source_entity and
target_entity. source was in conflict with an existing entity field.
- Removed the specification sort field for Indicators. It's an union of several
schemas (ThreatBrainSpecification, SnortSpecification, ...), there is no common
field to sort on it.
  • Loading branch information
msprunck committed Jun 14, 2017
1 parent bcd734b commit 094265d
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 172 deletions.
22 changes: 13 additions & 9 deletions src/ctia/schemas/graphql/helpers.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[walk :refer [stringify-keys]]]
[clojure.tools.logging :as log]
[clojure.walk :as walk])
(:import graphql.GraphQL
(:import [graphql GraphQL GraphQLException]
[graphql.schema
DataFetcher GraphQLArgument GraphQLArgument$Builder GraphQLEnumType
GraphQLFieldDefinition GraphQLInputObjectType
Expand Down Expand Up @@ -111,7 +111,7 @@

(defn debug
[msg value]
(log/debugf msg (pr-str value)))
(log/debug msg (pr-str value)))

(defn fn->data-fetcher
"Converts a function that takes 3 parameters (context, args and value)
Expand Down Expand Up @@ -318,10 +318,14 @@
^String query
^String operation-name
^java.util.Map variables]
(let [result (.execute graphql
query
operation-name
nil
(->java (or variables {})))]
{:data (->clj (.getData result))
:errors (.getErrors result)}))
(try
(let [result (.execute graphql
query
operation-name
nil
(->java (or variables {})))]
{:data (->clj (.getData result))
:errors (.getErrors result)})
(catch GraphQLException e
(log/error e)
{:errors [(.getMessage e)]})))
3 changes: 2 additions & 1 deletion src/ctia/schemas/graphql/observable.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
:resolve (fn [_ _ src]
(observable-verdict (select-keys src [:type :value])))}
:judgements {:type judgement/JudgementConnectionType
:args pagination/connection-arguments
:args (into pagination/connection-arguments
judgement/judgement-order-arg)
:resolve (fn [_ args src]
(resolvers/search-judgements-by-observable
(select-keys src [:type :value])
Expand Down
7 changes: 3 additions & 4 deletions src/ctia/schemas/graphql/pagination.clj
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,13 @@
{:first (s/maybe s/Int)
:last (s/maybe s/Int)
:after (s/maybe Cursor)
:before (s/maybe Cursor)
:orderBy [{:field s/Str
:direction (s/enum "ASC" "DESC")}]})))
:before (s/maybe Cursor)})))

(s/defschema PagingParams
(st/merge
{:limit s/Int
:offset s/Int}
:offset s/Int
(s/optional-key :sort_by) s/Str}
PagingDirection))

(s/defschema Edge
Expand Down
27 changes: 14 additions & 13 deletions src/ctia/schemas/graphql/relationship.clj
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@
(def relation-fields
(merge
(g/non-nulls
{:source {:type Entity
:resolve (fn [_ args src]
(log/debug "Source resolver" args src)
(let [ref (:source_ref src)
entity-type (ref->entity-type ref)]
(entity-by-id entity-type ref)))}
:target {:type Entity
:resolve (fn [_ args src]
(log/debug "Target resolver" args src)
(let [ref (:target_ref src)
entity-type (ref->entity-type ref)]
(entity-by-id entity-type ref)))}})))
{:source_entity {:type Entity
:resolve (fn [_ args src]
(log/debug "Source resolver" args src)
(let [ref (:source_ref src)
entity-type (ref->entity-type ref)]
(entity-by-id entity-type ref)))}
:target_entity {:type Entity
:resolve (fn [_ args src]
(log/debug "Target resolver" args src)
(let [ref (:target_ref src)
entity-type (ref->entity-type ref)]
(entity-by-id entity-type ref)))}})))

(def RelationshipType
(let [{:keys [fields name description]}
Expand Down Expand Up @@ -105,5 +105,6 @@
{:type Scalars/GraphQLString
:description (str "restrict to Relationships whose target is of the "
"specified CTIM entity type.")}}
p/connection-arguments)
p/connection-arguments
relationship-order-arg)
:resolve search-relationships}})
2 changes: 1 addition & 1 deletion src/ctia/schemas/graphql/sorting.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{s/Keyword s/Any}
(st/optional-keys
{:orderBy [{:field s/Str
:direction (s/enum "ASC" "DESC")}]})))
:direction (s/enum "asc" "desc")}]})))

(def OrderDirection
(g/enum
Expand Down
8 changes: 6 additions & 2 deletions src/ctia/schemas/sorting.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
(ns ctia.schemas.sorting)

;; These are the fields that are sortable, per entity. We place them
;; here since they are used by more than one entity's routes. For
;; isntance, the indicator route needs to know how to sort sightings
;; for the `ctia/indicator/:ID/sighting` handler
;;
(def base-entity-sort-fields [:id :schema_version :revision
:timestamp :language :tlp])

Expand Down Expand Up @@ -41,8 +46,7 @@
(concat default-entity-sort-fields
[:indicator_type
:likely_impact
:confidence
:specification]))
:confidence]))

(def relationship-sort-fields
(concat default-entity-sort-fields
Expand Down
Loading

0 comments on commit 094265d

Please sign in to comment.