-
Notifications
You must be signed in to change notification settings - Fork 87
/
ExampleKafkaStreamsSchemaRegistrySpec.scala
73 lines (62 loc) · 2.88 KB
/
ExampleKafkaStreamsSchemaRegistrySpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package net.manub.embeddedkafka.schemaregistry.streams
import net.manub.embeddedkafka.Codecs._
import net.manub.embeddedkafka.ConsumerExtensions._
import net.manub.embeddedkafka.TestAvroClass
import net.manub.embeddedkafka.schemaregistry.avro.Codecs._
import net.manub.embeddedkafka.schemaregistry.{
EmbeddedKafkaConfigWithSchemaRegistry,
_
}
import org.apache.kafka.common.serialization._
import org.apache.kafka.streams.StreamsBuilder
import org.apache.kafka.streams.kstream.{Consumed, KStream, Produced}
import org.scalatest.{Matchers, WordSpec}
class ExampleKafkaStreamsSchemaRegistrySpec
extends WordSpec
with Matchers
with EmbeddedKafkaStreamsWithSchemaRegistryAllInOne {
implicit val config: EmbeddedKafkaConfigWithSchemaRegistry =
EmbeddedKafkaConfigWithSchemaRegistry(kafkaPort = 7000,
zooKeeperPort = 7001,
schemaRegistryPort = 7002)
val (inTopic, outTopic) = ("in", "out")
val stringSerde: Serde[String] = Serdes.String()
val avroSerde: Serde[TestAvroClass] = serdeFrom[TestAvroClass]
"A Kafka streams test using Schema Registry" should {
"be easy to run with streams and consumer lifecycle management" in {
val streamBuilder = new StreamsBuilder
val stream: KStream[String, TestAvroClass] =
streamBuilder.stream(inTopic, Consumed.`with`(stringSerde, avroSerde))
stream.to(outTopic, Produced.`with`(stringSerde, avroSerde))
runStreams(Seq(inTopic, outTopic), streamBuilder.build()) {
publishToKafka(inTopic, "hello", TestAvroClass("world"))
publishToKafka(inTopic, "foo", TestAvroClass("bar"))
publishToKafka(inTopic, "baz", TestAvroClass("yaz"))
withConsumer[String, TestAvroClass, Unit] { consumer =>
val consumedMessages: Stream[(String, TestAvroClass)] =
consumer.consumeLazily(outTopic)
consumedMessages.take(2) should be(
Seq("hello" -> TestAvroClass("world"),
"foo" -> TestAvroClass("bar")))
consumedMessages.drop(2).head should be("baz" -> TestAvroClass("yaz"))
}
}
}
"allow support creating custom consumers" in {
val streamBuilder = new StreamsBuilder
val stream: KStream[String, TestAvroClass] =
streamBuilder.stream(inTopic, Consumed.`with`(stringSerde, avroSerde))
stream.to(outTopic, Produced.`with`(stringSerde, avroSerde))
runStreams(Seq(inTopic, outTopic), streamBuilder.build()) {
publishToKafka(inTopic, "hello", TestAvroClass("world"))
publishToKafka(inTopic, "foo", TestAvroClass("bar"))
val consumer = newConsumer[String, TestAvroClass]()
consumer
.consumeLazily[(String, TestAvroClass)](outTopic)
.take(2) should be(
Seq("hello" -> TestAvroClass("world"), "foo" -> TestAvroClass("bar")))
consumer.close()
}
}
}
}