@@ -3,7 +3,9 @@ package config
33import (
44 "github.com/confluentinc/confluent-kafka-go/kafka"
55 "github.com/stretchr/testify/assert"
6+ "regexp"
67 "testing"
8+ "time"
79)
810
911func TestKafkaConfArgs (t * testing.T ) {
@@ -47,3 +49,104 @@ func TestKafkaConfArgs(t *testing.T) {
4749 })
4850 }
4951}
52+
53+ func TestTopicMappings (t * testing.T ) {
54+ tests := []struct {
55+ name string
56+ input string
57+ output TopicMappings
58+ err error
59+ }{
60+ {
61+ name : "Set parameters" ,
62+ input : "temperature=temperature, humidity=.*/humidity,brightness=.*brightness, temperature=^cool$" ,
63+ output : TopicMappings {
64+ Mappings : []TopicMapping {
65+ {
66+ Topic : "temperature" ,
67+ RegExp : regexp .MustCompile (`temperature` ),
68+ },
69+ {
70+ Topic : "humidity" ,
71+ RegExp : regexp .MustCompile (`.*/humidity` ),
72+ },
73+ {
74+ Topic : "brightness" ,
75+ RegExp : regexp .MustCompile (`.*brightness` ),
76+ },
77+ {
78+ Topic : "temperature" ,
79+ RegExp : regexp .MustCompile (`^cool$` ),
80+ },
81+ },
82+ },
83+ },
84+ }
85+ for _ , tc := range tests {
86+ t .Run (tc .name , func (t * testing.T ) {
87+ a := assert .New (t )
88+
89+ s := new (Server )
90+ err := s .MQTT .Publisher .Kafka .TopicMappings .Set (tc .input )
91+ a .Equal (tc .err , err )
92+ a .Equal (tc .output , s .MQTT .Publisher .Kafka .TopicMappings )
93+ })
94+ }
95+ }
96+
97+ func TestConfigValidation (t * testing.T ) {
98+ tests := []struct {
99+ name string
100+ factory func () * Server
101+ err error
102+ }{
103+ {
104+ name : "noop publisher" ,
105+ factory : func () * Server {
106+ s := new (Server )
107+ s .HTTP .ListenAddress = "localhost:9090"
108+ s .MQTT .ListenAddress = "localhost:1883"
109+ s .MQTT .Publisher .Name = Noop
110+ return s
111+ },
112+ },
113+ {
114+ name : "kafka publisher" ,
115+ factory : func () * Server {
116+ s := new (Server )
117+ s .HTTP .ListenAddress = "localhost:9090"
118+ s .MQTT .ListenAddress = "localhost:1883"
119+ s .MQTT .Publisher .Name = Kafka
120+ s .MQTT .Publisher .Kafka .BootstrapServers = "localhost:9092"
121+ return s
122+ },
123+ },
124+ {
125+ name : "kafka publisher with params" ,
126+ factory : func () * Server {
127+ s := new (Server )
128+ s .HTTP .ListenAddress = "localhost:9090"
129+ s .HTTP .GracePeriod = 10 * time .Second
130+ s .MQTT .ListenAddress = "localhost:1883"
131+ s .MQTT .GracePeriod = 1 * time .Second
132+ s .MQTT .ReadTimeout = 1 * time .Second
133+ s .MQTT .WriteTimeout = 1 * time .Second
134+ s .MQTT .IdleTimeout = 1 * time .Second
135+ s .MQTT .ReaderBufferSize = 256
136+ s .MQTT .WriterBufferSize = 256
137+ s .MQTT .Publisher .Name = Kafka
138+ s .MQTT .Publisher .Kafka .BootstrapServers = "localhost:9092"
139+ s .MQTT .Publisher .Kafka .GracePeriod = 10 * time .Second
140+ return s
141+ },
142+ },
143+ }
144+ for _ , tc := range tests {
145+ t .Run (tc .name , func (t * testing.T ) {
146+ input := tc .factory ()
147+ a := assert .New (t )
148+ err := input .Validate ()
149+ a .Equal (tc .err , err )
150+ })
151+ }
152+ }
0 commit comments