Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
include the metadata in the message-payload
- Loading branch information
1 parent
77b81ec
commit e1ed887
Showing
12 changed files
with
124 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,35 @@ | ||
(ns ziggurat.header-transformer-test | ||
(:require [clojure.test :refer :all] | ||
[ziggurat.header-transformer :refer :all]) | ||
(:import [org.apache.kafka.streams.processor ProcessorContext] | ||
[org.apache.kafka.common.header.internals RecordHeaders RecordHeader])) | ||
(:require [clojure.test :refer [deftest is testing]] | ||
[ziggurat.header-transformer :refer [create]]) | ||
(:import [org.apache.kafka.common.header.internals RecordHeaders RecordHeader] | ||
[org.apache.kafka.streams.processor ProcessorContext])) | ||
|
||
(deftest header-transformer-test | ||
(testing "transforms value with passed headers" | ||
(let [headers (RecordHeaders. (list (RecordHeader. "key" (byte-array (map byte "value"))))) | ||
context (reify ProcessorContext | ||
(headers [_] headers)) | ||
transformer (create) | ||
_ (.init transformer context) | ||
(let [headers (RecordHeaders. (list (RecordHeader. "key" (byte-array (map byte "value"))))) | ||
topic "topic" | ||
timestamp 1234567890 | ||
partition 1 | ||
context (reify ProcessorContext | ||
(headers [_] headers) | ||
(topic [_] topic) | ||
(timestamp [_] timestamp) | ||
(partition [_] partition)) | ||
transformer (create) | ||
_ (.init transformer context) | ||
transformed-val (.transform transformer "val")] | ||
(is (= {:value "val" :headers headers} transformed-val)))) | ||
(is (= {:value "val" :headers headers :metadata {:topic topic :timestamp timestamp :partition partition}} transformed-val)))) | ||
|
||
(testing "transforms value with nil headers when not passed" | ||
(let [context (reify ProcessorContext | ||
(headers [_] nil)) | ||
transformer (create) | ||
_ (.init transformer context) | ||
(let [topic "topic" | ||
timestamp 1234567890 | ||
partition 1 | ||
context (reify ProcessorContext | ||
(headers [_] nil) | ||
(topic [_] topic) | ||
(timestamp [_] timestamp) | ||
(partition [_] partition)) | ||
transformer (create) | ||
_ (.init transformer context) | ||
transformed-val (.transform transformer "val")] | ||
(is (= {:value "val" :headers nil} transformed-val))))) | ||
(is (= {:value "val" :headers nil :metadata {:topic topic :timestamp timestamp :partition partition}} transformed-val))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,66 @@ | ||
(ns ziggurat.middleware.json-test | ||
(:require [clojure.test :refer :all] | ||
[cheshire.core :refer [generate-string]] | ||
[ziggurat.middleware.json :refer [parse-json]] | ||
(:require [cheshire.core :refer [generate-string]] | ||
[clojure.test :refer [deftest is join-fixtures testing use-fixtures]] | ||
[ziggurat.fixtures :as fix] | ||
[ziggurat.metrics :as metrics])) | ||
[ziggurat.metrics :as metrics] | ||
[ziggurat.middleware.json :refer [parse-json]])) | ||
|
||
(use-fixtures :once (join-fixtures [fix/mount-only-config | ||
fix/silence-logging])) | ||
|
||
(deftest parse-json-test | ||
(testing "Given a handler function (without passing key-fn), parse-json should call that function on after deserializing the string to JSON object." | ||
(let [handler-fn-called? (atom false) | ||
message {:a "A" | ||
json-message {:a "A" | ||
:b "B"} | ||
topic-entity-name "test" | ||
handler-fn (fn [msg] | ||
(if (= msg message) | ||
handler-fn (fn [{:keys [message metadata]}] | ||
(when (and (= json-message message) | ||
(= (:topic metadata) "topic") | ||
(= (:timestamp metadata) 1234567890) | ||
(= (:partition metadata) 1)) | ||
(reset! handler-fn-called? true)))] | ||
((parse-json handler-fn topic-entity-name) (generate-string message)) | ||
((parse-json handler-fn topic-entity-name) {:message (generate-string json-message) :metadata {:topic "topic" :timestamp 1234567890 :partition 1}}) | ||
(is (true? @handler-fn-called?)))) | ||
(testing "Given a handler function and key-fn as false, parse-json should call that function on after | ||
deserializing the string without coercing the keys to keywords." | ||
(let [handler-fn-called? (atom false) | ||
message {:a "A" | ||
:b "B"} | ||
expected-output {"a" "A" "b" "B"} | ||
json-message {"a" "A" "b" "B"} | ||
topic-entity-name "test" | ||
handler-fn (fn [msg] | ||
(if (= msg expected-output) | ||
handler-fn (fn [{:keys [message metadata]}] | ||
(when (and (= json-message message) | ||
(= (:topic metadata) "topic") | ||
(= (:timestamp metadata) 1234567890) | ||
(= (:partition metadata) 1)) | ||
(reset! handler-fn-called? true)))] | ||
((parse-json handler-fn topic-entity-name false) (generate-string message)) | ||
((parse-json handler-fn topic-entity-name false) {:message (generate-string json-message) :metadata {:topic "topic" :timestamp 1234567890 :partition 1}}) | ||
(is (true? @handler-fn-called?)))) | ||
(testing "Given a handler function and a key-fn, parse-json should call that function after | ||
deserializing the string by applying key-fn to keys." | ||
(let [handler-fn-called? (atom false) | ||
key-fn (fn [k] (str k "-modified")) | ||
message {"a" "A" | ||
json-message {"a" "A" | ||
"b" "B"} | ||
expected-output {"a-modified" "A" "b-modified" "B"} | ||
topic-entity-name "test" | ||
handler-fn (fn [msg] | ||
(if (= msg expected-output) | ||
handler-fn (fn [{:keys [message metadata]}] | ||
(is (= {"a-modified" "A", "b-modified" "B"} message)) | ||
(when (and (= {"a-modified" "A", "b-modified" "B"} message) | ||
(= (:topic metadata) "topic") | ||
(= (:timestamp metadata) 1234567890) | ||
(= (:partition metadata) 1)) | ||
(reset! handler-fn-called? true)))] | ||
((parse-json handler-fn topic-entity-name key-fn) (generate-string message)) | ||
((parse-json handler-fn topic-entity-name key-fn) {:message (generate-string json-message) :metadata {:topic "topic" :timestamp 1234567890 :partition 1}}) | ||
(is (true? @handler-fn-called?)))) | ||
(testing "Should report metrics when JSON deserialization fails" | ||
(let [handler-fn-called? (atom false) | ||
metric-reporter-called? (atom false) | ||
topic-entity-name "test" | ||
message "{\"foo\":\"bar" | ||
handler-fn (fn [msg] | ||
(if (nil? msg) | ||
json-message "{\"foo\":\"bar" | ||
handler-fn (fn [{:keys [message _]}] | ||
(when (nil? message) | ||
(reset! handler-fn-called? true)))] | ||
(with-redefs [metrics/multi-ns-increment-count (fn [_ _ _] | ||
(reset! metric-reporter-called? true))] | ||
((parse-json handler-fn topic-entity-name true) message)) | ||
((parse-json handler-fn topic-entity-name true) {:message json-message :metadata {:topic "topic" :timestamp 1234567890 :partition 1}})) | ||
(is (true? @handler-fn-called?)) | ||
(is (true? @metric-reporter-called?))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.