1
1
package loader_test
2
2
3
3
import (
4
+ "encoding/json"
4
5
"fmt"
5
6
"os"
6
7
"testing"
@@ -40,10 +41,10 @@ func TestLoadConfigUnreg(t *testing.T) {
40
41
41
42
func TestLoadConfig (t * testing.T ) {
42
43
43
- loader .Register ("aTypeOfSource" , func () loader.Builder [Source ] { return & srcConfigA {} })
44
- loader .Register ("sourceThatCanB" , func () loader.Builder [Source ] { return & srcConfigB {} })
45
- loader .Register ("aTypeOfDest" , func () loader.Builder [Destination ] { return & dstConfigA {} })
46
- loader .Register ("bAllThatUCanB" , func () loader.Builder [Destination ] { return & dstConfigB {} })
44
+ loader .Register ("aTypeOfSource" , func () loader.Builder [Source ] { return & srcConfigA {Type : "aTypeOfSource" } })
45
+ loader .Register ("sourceThatCanB" , func () loader.Builder [Source ] { return & srcConfigB {Type : "sourceThatCanB" } })
46
+ loader .Register ("aTypeOfDest" , func () loader.Builder [Destination ] { return & dstConfigA {Type : "aTypeOfDest" } })
47
+ loader .Register ("bAllThatUCanB" , func () loader.Builder [Destination ] { return & dstConfigB {Type : "bAllThatUCanB" } })
47
48
48
49
tests := []struct {
49
50
name string
@@ -81,12 +82,12 @@ func TestLoadConfig(t *testing.T) {
81
82
expected : Config {
82
83
Name : "jimmy" ,
83
84
Sources : []loader.Loader [Source ]{
84
- {& srcConfigA {"localhost" }},
85
- {& srcConfigB {"gym" }},
85
+ {& srcConfigA {Type : "aTypeOfSource" , Host : "localhost" }},
86
+ {& srcConfigB {Type : "sourceThatCanB" , Topic : "gym" }},
86
87
},
87
88
Destinations : []loader.Loader [Destination ]{
88
- {& dstConfigA {"localhost" }},
89
- {& dstConfigB {"output" }},
89
+ {& dstConfigA {Type : "aTypeOfDest" , Host : "localhost" }},
90
+ {& dstConfigB {Type : "bAllThatUCanB" , Topic : "output" }},
90
91
},
91
92
},
92
93
err : false ,
@@ -151,6 +152,7 @@ type srcB struct{ topic string }
151
152
func (s * srcB ) Recv () (string , error ) { return s .topic , nil }
152
153
153
154
type srcConfigA struct {
155
+ Type string `json:"type"`
154
156
Host string `json:"host"`
155
157
}
156
158
@@ -159,6 +161,7 @@ func (c *srcConfigA) Configure() (Source, error) {
159
161
}
160
162
161
163
type srcConfigB struct {
164
+ Type string `json:"type"`
162
165
Topic string `json:"topic"`
163
166
}
164
167
@@ -175,17 +178,57 @@ type dstB struct{ topic string }
175
178
func (s * dstB ) Send (string ) error { return nil }
176
179
177
180
type dstConfigA struct {
178
- Host string
181
+ Type string `json:"type"`
182
+ Host string `json:"host"`
179
183
}
180
184
181
185
func (c * dstConfigA ) Configure () (Destination , error ) {
182
186
return & dstA {c .Host }, nil
183
187
}
184
188
185
189
type dstConfigB struct {
186
- Topic string
190
+ Type string `json:"type"`
191
+ Topic string `json:"topic"`
187
192
}
188
193
189
194
func (c * dstConfigB ) Configure () (Destination , error ) {
190
195
return & dstB {c .Topic }, nil
191
196
}
197
+
198
+ func TestMarshalJSON (t * testing.T ) {
199
+ // Register test types if not already registered from other tests
200
+ loader .Register ("testSource" , func () loader.Builder [Source ] { return & srcConfigA {Type : "testSource" } })
201
+
202
+ tests := []struct {
203
+ name string
204
+ loader loader.Loader [Source ]
205
+ expected string
206
+ }{
207
+ {
208
+ name : "marshal source config" ,
209
+ loader : loader.Loader [Source ]{& srcConfigA {Type : "testSource" , Host : "test-host" }},
210
+ expected : `{"type":"testSource","host":"test-host"}` ,
211
+ },
212
+ }
213
+
214
+ for _ , test := range tests {
215
+ t .Run (test .name , func (t * testing.T ) {
216
+ // Marshal the loader
217
+ data , err := json .Marshal (test .loader )
218
+ assert .NoError (t , err )
219
+
220
+ // Compare with expected JSON string
221
+ assert .JSONEq (t , test .expected , string (data ))
222
+
223
+ // Try unmarshaling back to verify round-trip
224
+ var newLoader loader.Loader [Source ]
225
+ err = json .Unmarshal (data , & newLoader )
226
+ assert .NoError (t , err )
227
+
228
+ // Verify the unmarshaled object produces the same JSON
229
+ newData , err := json .Marshal (newLoader )
230
+ assert .NoError (t , err )
231
+ assert .JSONEq (t , test .expected , string (newData ))
232
+ })
233
+ }
234
+ }
0 commit comments