Skip to content

Commit

Permalink
fix test, generator should not produce nil or empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
macalimlim committed Jun 26, 2019
1 parent 7678c32 commit f99fb8f
Showing 1 changed file with 21 additions and 25 deletions.
46 changes: 21 additions & 25 deletions test/ziggurat/producer_test.clj
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
(ns ziggurat.producer-test
(:require [clojure.test :refer :all]
[ziggurat.streams :refer [start-streams stop-streams]]
[ziggurat.fixtures :as fix :refer [*producer-properties* *consumer-properties*]]
(:require [clojure.string :refer [blank?]]
[clojure.test :refer :all]
[clojure.test.check.generators :as gen]
[ziggurat.config :refer [ziggurat-config]]
[ziggurat.fixtures :as fix :refer [*producer-properties* *consumer-properties*]]
[ziggurat.producer :refer [producer-properties-map send kafka-producers]]
[clojure.test.check.generators :as gen])
(:import (org.apache.kafka.streams.integration.utils IntegrationTestUtils)
(org.apache.kafka.clients.producer KafkaProducer)))
[ziggurat.streams :refer [start-streams stop-streams]])
(:import (org.apache.kafka.clients.producer KafkaProducer)
(org.apache.kafka.streams.integration.utils IntegrationTestUtils)))

(use-fixtures :once fix/mount-only-config-and-producer)

Expand All @@ -21,46 +22,41 @@
:enabled [true :bool]}}}}})

(deftest send-data-with-topic-and-value-test
(with-redefs
[kafka-producers (hash-map :default (KafkaProducer. *producer-properties*))]
(let [topic (gen/generate gen/string-alphanumeric 10)
key "message"
value "Hello World!!"]
(with-redefs [kafka-producers (hash-map :default (KafkaProducer. *producer-properties*))]
(let [alphanum-gen (gen/such-that #(not (blank? %)) gen/string-alphanumeric)
topic (gen/generate alphanum-gen 10)
key "message"
value "Hello World!!"]
(send :default topic key value)
(let [result (IntegrationTestUtils/waitUntilMinKeyValueRecordsReceived *consumer-properties* topic 1 2000)]
(is (= value (.value (first result))))))))

(deftest send-data-with-topic-key-partition-and-value-test
(with-redefs
[kafka-producers (hash-map :default (KafkaProducer. *producer-properties*))]
(let [topic (gen/generate gen/string-alphanumeric 10)
key "message"
value "Hello World!!"
partition (int 0)]
(with-redefs [kafka-producers (hash-map :default (KafkaProducer. *producer-properties*))]
(let [alphanum-gen (gen/such-that #(not (blank? %)) gen/string-alphanumeric)
topic (gen/generate alphanum-gen 10)
key "message"
value "Hello World!!"
partition (int 0)]
(send :default topic partition key value)
(let [result (IntegrationTestUtils/waitUntilMinKeyValueRecordsReceived *consumer-properties* topic 1 2000)]
(is (= value (.value (first result))))))))

(deftest send-throws-exception-when-no-producers-are-configured
(with-redefs
[kafka-producers {}]
(with-redefs [kafka-producers {}]
(let [topic "test-topic"
key "message"
key "message"
value "Hello World!! from non-existant Kafka Producers"]
(is (not-empty (try (send :default topic key value)
(catch Exception e (ex-data e))))))))

(deftest producer-properties-map-is-empty-if-no-producers-configured
; Here ziggurat-config has been substituted with a custom map which
; does not have any valid producer configs.
(with-redefs
[ziggurat-config stream-router-config-without-producer]
(with-redefs [ziggurat-config stream-router-config-without-producer]
(is (empty? (producer-properties-map)))))

(deftest producer-properties-map-is-not-empty-if-producers-are-configured
; Here the config is read from config.test.edn which contains
; valid producer configs.
(is (seq (producer-properties-map))))



0 comments on commit f99fb8f

Please sign in to comment.