summaryrefslogtreecommitdiff
path: root/lib/config_test.go
blob: 6f4e854fe356d61d1e9c085ba615f41ecb104b08 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package lib

import (
	"encoding/json"
	"testing"
)

func TestRegisterInputConfigCreator(t *testing.T) {
	// Test registering a new input config creator
	testID := "test_input_config_creator_" + t.Name()
	fn := func(action Action, data json.RawMessage) (InputConverter, error) {
		return nil, nil
	}

	err := RegisterInputConfigCreator(testID, fn)
	if err != nil {
		t.Fatalf("RegisterInputConfigCreator failed: %v", err)
	}

	// Test registering duplicate
	err = RegisterInputConfigCreator(testID, fn)
	if err == nil {
		t.Error("RegisterInputConfigCreator should return error for duplicate")
	}
}

func TestRegisterInputConfigCreator_CaseInsensitive(t *testing.T) {
	testID := "TEST_INPUT_CONFIG_CASE_" + t.Name()
	fn := func(action Action, data json.RawMessage) (InputConverter, error) {
		return nil, nil
	}

	err := RegisterInputConfigCreator(testID, fn)
	if err != nil {
		t.Fatalf("RegisterInputConfigCreator failed: %v", err)
	}

	// Try to register with lowercase
	err = RegisterInputConfigCreator("test_input_config_case_"+t.Name(), fn)
	if err == nil {
		t.Error("RegisterInputConfigCreator should be case-insensitive")
	}
}

func TestCreateInputConfig_NotFound(t *testing.T) {
	_, err := createInputConfig("nonexistent_input_config", ActionAdd, nil)
	if err == nil {
		t.Error("createInputConfig should return error for unknown type")
	}
}

func TestRegisterOutputConfigCreator(t *testing.T) {
	// Test registering a new output config creator
	testID := "test_output_config_creator_" + t.Name()
	fn := func(action Action, data json.RawMessage) (OutputConverter, error) {
		return nil, nil
	}

	err := RegisterOutputConfigCreator(testID, fn)
	if err != nil {
		t.Fatalf("RegisterOutputConfigCreator failed: %v", err)
	}

	// Test registering duplicate
	err = RegisterOutputConfigCreator(testID, fn)
	if err == nil {
		t.Error("RegisterOutputConfigCreator should return error for duplicate")
	}
}

func TestRegisterOutputConfigCreator_CaseInsensitive(t *testing.T) {
	testID := "TEST_OUTPUT_CONFIG_CASE_" + t.Name()
	fn := func(action Action, data json.RawMessage) (OutputConverter, error) {
		return nil, nil
	}

	err := RegisterOutputConfigCreator(testID, fn)
	if err != nil {
		t.Fatalf("RegisterOutputConfigCreator failed: %v", err)
	}

	// Try to register with lowercase
	err = RegisterOutputConfigCreator("test_output_config_case_"+t.Name(), fn)
	if err == nil {
		t.Error("RegisterOutputConfigCreator should be case-insensitive")
	}
}

func TestCreateOutputConfig_NotFound(t *testing.T) {
	_, err := createOutputConfig("nonexistent_output_config", ActionOutput, nil)
	if err == nil {
		t.Error("createOutputConfig should return error for unknown type")
	}
}

// MockInputConverter for testing
type mockInputConverter struct {
	typeName    string
	action      Action
	description string
}

func (m *mockInputConverter) GetType() string        { return m.typeName }
func (m *mockInputConverter) GetAction() Action      { return m.action }
func (m *mockInputConverter) GetDescription() string { return m.description }
func (m *mockInputConverter) Input(c Container) (Container, error) {
	return c, nil
}

// MockOutputConverter for testing
type mockOutputConverter struct {
	typeName    string
	action      Action
	description string
}

func (m *mockOutputConverter) GetType() string        { return m.typeName }
func (m *mockOutputConverter) GetAction() Action      { return m.action }
func (m *mockOutputConverter) GetDescription() string { return m.description }
func (m *mockOutputConverter) Output(c Container) error {
	return nil
}

func TestInputConvConfigUnmarshalJSON(t *testing.T) {
	// Register a mock input config creator
	testType := "mock_input_" + t.Name()
	RegisterInputConfigCreator(testType, func(action Action, data json.RawMessage) (InputConverter, error) {
		return &mockInputConverter{
			typeName: testType,
			action:   action,
		}, nil
	})

	// Test valid unmarshal
	jsonData := []byte(`{"type":"` + testType + `","action":"add","args":{}}`)
	var config inputConvConfig
	err := json.Unmarshal(jsonData, &config)
	if err != nil {
		t.Fatalf("UnmarshalJSON failed: %v", err)
	}

	if config.iType != testType {
		t.Errorf("config.iType = %s, want %s", config.iType, testType)
	}
	if config.action != ActionAdd {
		t.Errorf("config.action = %s, want %s", config.action, ActionAdd)
	}
}

func TestInputConvConfigUnmarshalJSON_InvalidAction(t *testing.T) {
	jsonData := []byte(`{"type":"sometype","action":"invalid_action","args":{}}`)
	var config inputConvConfig
	err := json.Unmarshal(jsonData, &config)
	if err == nil {
		t.Error("UnmarshalJSON should fail for invalid action")
	}
}

func TestInputConvConfigUnmarshalJSON_InvalidJSON(t *testing.T) {
	jsonData := []byte(`{invalid json}`)
	var config inputConvConfig
	err := json.Unmarshal(jsonData, &config)
	if err == nil {
		t.Error("UnmarshalJSON should fail for invalid JSON")
	}
}

func TestInputConvConfigUnmarshalJSON_UnknownType(t *testing.T) {
	jsonData := []byte(`{"type":"unknown_type_123","action":"add","args":{}}`)
	var config inputConvConfig
	err := json.Unmarshal(jsonData, &config)
	if err == nil {
		t.Error("UnmarshalJSON should fail for unknown type")
	}
}

func TestOutputConvConfigUnmarshalJSON(t *testing.T) {
	// Register a mock output config creator
	testType := "mock_output_" + t.Name()
	RegisterOutputConfigCreator(testType, func(action Action, data json.RawMessage) (OutputConverter, error) {
		return &mockOutputConverter{
			typeName: testType,
			action:   action,
		}, nil
	})

	// Test valid unmarshal
	jsonData := []byte(`{"type":"` + testType + `","action":"output","args":{}}`)
	var config outputConvConfig
	err := json.Unmarshal(jsonData, &config)
	if err != nil {
		t.Fatalf("UnmarshalJSON failed: %v", err)
	}

	if config.iType != testType {
		t.Errorf("config.iType = %s, want %s", config.iType, testType)
	}
	if config.action != ActionOutput {
		t.Errorf("config.action = %s, want %s", config.action, ActionOutput)
	}
}

func TestOutputConvConfigUnmarshalJSON_DefaultAction(t *testing.T) {
	// Register a mock output config creator
	testType := "mock_output_default_" + t.Name()
	RegisterOutputConfigCreator(testType, func(action Action, data json.RawMessage) (OutputConverter, error) {
		return &mockOutputConverter{
			typeName: testType,
			action:   action,
		}, nil
	})

	// Test unmarshal without action (should default to "output")
	jsonData := []byte(`{"type":"` + testType + `","args":{}}`)
	var config outputConvConfig
	err := json.Unmarshal(jsonData, &config)
	if err != nil {
		t.Fatalf("UnmarshalJSON failed: %v", err)
	}

	if config.action != ActionOutput {
		t.Errorf("config.action = %s, want %s (default)", config.action, ActionOutput)
	}
}

func TestOutputConvConfigUnmarshalJSON_InvalidAction(t *testing.T) {
	jsonData := []byte(`{"type":"sometype","action":"invalid_action","args":{}}`)
	var config outputConvConfig
	err := json.Unmarshal(jsonData, &config)
	if err == nil {
		t.Error("UnmarshalJSON should fail for invalid action")
	}
}

func TestOutputConvConfigUnmarshalJSON_InvalidJSON(t *testing.T) {
	jsonData := []byte(`{invalid json}`)
	var config outputConvConfig
	err := json.Unmarshal(jsonData, &config)
	if err == nil {
		t.Error("UnmarshalJSON should fail for invalid JSON")
	}
}

func TestOutputConvConfigUnmarshalJSON_UnknownType(t *testing.T) {
	jsonData := []byte(`{"type":"unknown_type_456","action":"output","args":{}}`)
	var config outputConvConfig
	err := json.Unmarshal(jsonData, &config)
	if err == nil {
		t.Error("UnmarshalJSON should fail for unknown type")
	}
}

func TestConfigStruct(t *testing.T) {
	// Register mock converters for this test
	inputType := "config_test_input_" + t.Name()
	outputType := "config_test_output_" + t.Name()

	RegisterInputConfigCreator(inputType, func(action Action, data json.RawMessage) (InputConverter, error) {
		return &mockInputConverter{
			typeName: inputType,
			action:   action,
		}, nil
	})

	RegisterOutputConfigCreator(outputType, func(action Action, data json.RawMessage) (OutputConverter, error) {
		return &mockOutputConverter{
			typeName: outputType,
			action:   action,
		}, nil
	})

	// Test unmarshaling full config
	jsonData := []byte(`{
		"input": [
			{"type":"` + inputType + `","action":"add","args":{}}
		],
		"output": [
			{"type":"` + outputType + `","action":"output","args":{}}
		]
	}`)

	var cfg config
	err := json.Unmarshal(jsonData, &cfg)
	if err != nil {
		t.Fatalf("UnmarshalJSON failed: %v", err)
	}

	if len(cfg.Input) != 1 {
		t.Errorf("len(cfg.Input) = %d, want 1", len(cfg.Input))
	}
	if len(cfg.Output) != 1 {
		t.Errorf("len(cfg.Output) = %d, want 1", len(cfg.Output))
	}
}