@@ -46,17 +46,20 @@ func (suite *ConfigTestSuite) TestTopicRoute_ToWRPKafkaRoute() {
4646 name string
4747 topicRoute TopicRoute
4848 expected wrpkafka.TopicRoute
49+ expectError bool
4950 description string
5051 }{
5152 {
5253 name : "simple_route" ,
5354 topicRoute : TopicRoute {
5455 Topic : "events" ,
5556 Pattern : "event:.*" ,
57+ HashKey : "source" ,
5658 },
5759 expected : wrpkafka.TopicRoute {
5860 Topic : "events" ,
5961 Pattern : wrpkafka .Pattern ("event:.*" ),
62+ HashKey : wrpkafka.HashKey {Name : wrpkafka .HashKeySource },
6063 },
6164 description : "Should convert TopicRoute to wrpkafka.TopicRoute correctly" ,
6265 },
@@ -65,31 +68,64 @@ func (suite *ConfigTestSuite) TestTopicRoute_ToWRPKafkaRoute() {
6568 topicRoute : TopicRoute {
6669 Topic : "commands" ,
6770 Pattern : "mac:.*/command" ,
71+ HashKey : "metadata/hw-deviceid" ,
6872 },
6973 expected : wrpkafka.TopicRoute {
7074 Topic : "commands" ,
7175 Pattern : wrpkafka .Pattern ("mac:.*/command" ),
76+ HashKey : wrpkafka.HashKey {Name : wrpkafka .HashKeyMetadata , MetadataField : "hw-deviceid" },
7277 },
73- description : "Should handle complex routing patterns" ,
78+ description : "Should handle complex routing patterns with metadata hash key " ,
7479 },
7580 {
7681 name : "wildcard_route" ,
7782 topicRoute : TopicRoute {
7883 Topic : "all-messages" ,
7984 Pattern : ".*" ,
85+ HashKey : "none" ,
8086 },
8187 expected : wrpkafka.TopicRoute {
8288 Topic : "all-messages" ,
8389 Pattern : wrpkafka .Pattern (".*" ),
90+ HashKey : wrpkafka.HashKey {Name : wrpkafka .HashKeyNone },
91+ },
92+ description : "Should handle wildcard patterns with no hash key" ,
93+ },
94+ {
95+ name : "route_with_default_hash_key" ,
96+ topicRoute : TopicRoute {
97+ Topic : "events" ,
98+ Pattern : "event:.*" ,
99+ HashKey : "" , // Empty should default to metadata/hw-deviceid
100+ },
101+ expected : wrpkafka.TopicRoute {
102+ Topic : "events" ,
103+ Pattern : wrpkafka .Pattern ("event:.*" ),
104+ HashKey : wrpkafka.HashKey {Name : wrpkafka .HashKeyMetadata , MetadataField : "hw-deviceid" },
105+ },
106+ description : "Should default to metadata/hw-deviceid when hash_key is empty" ,
107+ },
108+ {
109+ name : "invalid_hash_key" ,
110+ topicRoute : TopicRoute {
111+ Topic : "events" ,
112+ Pattern : "event:.*" ,
113+ HashKey : "invalid" ,
84114 },
85- description : "Should handle wildcard patterns" ,
115+ expectError : true ,
116+ description : "Should return error for invalid hash key" ,
86117 },
87118 }
88119
89120 for _ , tt := range tests {
90121 suite .Run (tt .name , func () {
91- result := tt .topicRoute .ToWRPKafkaRoute ()
92- suite .Equal (tt .expected , result , tt .description )
122+ result , err := tt .topicRoute .ToWRPKafkaRoute ()
123+ if tt .expectError {
124+ suite .Error (err , tt .description )
125+ } else {
126+ suite .NoError (err , tt .description )
127+ suite .Equal (tt .expected , result , tt .description )
128+ }
93129 })
94130 }
95131}
@@ -100,33 +136,34 @@ func (suite *ConfigTestSuite) TestConfig_ToWRPKafkaRoutes() {
100136 name string
101137 config Config
102138 expected []wrpkafka.TopicRoute
139+ expectError bool
103140 description string
104141 }{
105142 {
106143 name : "single_route" ,
107144 config : Config {
108145 TopicRoutes : []TopicRoute {
109- {Topic : "events" , Pattern : "event:.*" },
146+ {Topic : "events" , Pattern : "event:.*" , HashKey : "source" },
110147 },
111148 },
112149 expected : []wrpkafka.TopicRoute {
113- {Topic : "events" , Pattern : wrpkafka .Pattern ("event:.*" )},
150+ {Topic : "events" , Pattern : wrpkafka .Pattern ("event:.*" ), HashKey : wrpkafka. HashKey { Name : wrpkafka . HashKeySource } },
114151 },
115152 description : "Should convert single route correctly" ,
116153 },
117154 {
118155 name : "multiple_routes" ,
119156 config : Config {
120157 TopicRoutes : []TopicRoute {
121- {Topic : "events" , Pattern : "event:.*" },
122- {Topic : "commands" , Pattern : "mac:.*/command" },
123- {Topic : "responses" , Pattern : ".*response.*" },
158+ {Topic : "events" , Pattern : "event:.*" , HashKey : "source" },
159+ {Topic : "commands" , Pattern : "mac:.*/command" , HashKey : "metadata/hw-deviceid" },
160+ {Topic : "responses" , Pattern : ".*response.*" , HashKey : "none" },
124161 },
125162 },
126163 expected : []wrpkafka.TopicRoute {
127- {Topic : "events" , Pattern : wrpkafka .Pattern ("event:.*" )},
128- {Topic : "commands" , Pattern : wrpkafka .Pattern ("mac:.*/command" )},
129- {Topic : "responses" , Pattern : wrpkafka .Pattern (".*response.*" )},
164+ {Topic : "events" , Pattern : wrpkafka .Pattern ("event:.*" ), HashKey : wrpkafka. HashKey { Name : wrpkafka . HashKeySource } },
165+ {Topic : "commands" , Pattern : wrpkafka .Pattern ("mac:.*/command" ), HashKey : wrpkafka. HashKey { Name : wrpkafka . HashKeyMetadata , MetadataField : "hw-deviceid" } },
166+ {Topic : "responses" , Pattern : wrpkafka .Pattern (".*response.*" ), HashKey : wrpkafka. HashKey { Name : wrpkafka . HashKeyNone } },
130167 },
131168 description : "Should convert multiple routes correctly" ,
132169 },
@@ -138,12 +175,27 @@ func (suite *ConfigTestSuite) TestConfig_ToWRPKafkaRoutes() {
138175 expected : []wrpkafka.TopicRoute {},
139176 description : "Should handle empty routes slice" ,
140177 },
178+ {
179+ name : "route_with_invalid_hash_key" ,
180+ config : Config {
181+ TopicRoutes : []TopicRoute {
182+ {Topic : "events" , Pattern : "event:.*" , HashKey : "invalid" },
183+ },
184+ },
185+ expectError : true ,
186+ description : "Should return error for invalid hash key" ,
187+ },
141188 }
142189
143190 for _ , tt := range tests {
144191 suite .Run (tt .name , func () {
145- result := tt .config .ToWRPKafkaRoutes ()
146- suite .Equal (tt .expected , result , tt .description )
192+ result , err := tt .config .ToWRPKafkaRoutes ()
193+ if tt .expectError {
194+ suite .Error (err , tt .description )
195+ } else {
196+ suite .NoError (err , tt .description )
197+ suite .Equal (tt .expected , result , tt .description )
198+ }
147199 })
148200 }
149201}
0 commit comments