1
+ /*
2
+ * Copyright 2023 StreamThoughts.
3
+ *
4
+ * Licensed to the Apache Software Foundation (ASF) under one or more
5
+ * contributor license agreements. See the NOTICE file distributed with
6
+ * this work for additional information regarding copyright ownership.
7
+ * The ASF licenses this file to You under the Apache License, Version 2.0
8
+ * (the "License"); you may not use this file except in compliance with
9
+ * the License. You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+ package io .streamthoughts .kafka .connect .filepulse .config ;
20
+
21
+ import static io .streamthoughts .kafka .connect .filepulse .config .ExtractValueConfig .EXTRACT_TARGET_CONFIG ;
22
+ import static io .streamthoughts .kafka .connect .filepulse .config .ExtractValueConfig .REGEX_CONFIG ;
23
+ import static io .streamthoughts .kafka .connect .filepulse .config .ExtractValueConfig .REGEX_DEFAULT_VALUE_CONFIG ;
24
+ import static io .streamthoughts .kafka .connect .filepulse .config .ExtractValueConfig .REGEX_FIELD_CONFIG ;
25
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
26
+ import static org .junit .jupiter .api .Assertions .assertNull ;
27
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
28
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
29
+
30
+ import java .util .Map ;
31
+ import org .apache .kafka .common .config .ConfigException ;
32
+ import org .junit .jupiter .api .Test ;
33
+
34
+ class ExtractValueConfigTest {
35
+
36
+
37
+ @ Test
38
+ void when_all_fields_config_specified_config_ok () {
39
+ ExtractValueConfig config = new ExtractValueConfig (Map .of (
40
+ REGEX_FIELD_CONFIG , Fixture .fieldName ,
41
+ REGEX_CONFIG , Fixture .regex ,
42
+ REGEX_DEFAULT_VALUE_CONFIG , Fixture .defaultValue ,
43
+ EXTRACT_TARGET_CONFIG , Fixture .targetName
44
+ ));
45
+ assertEquals (Fixture .fieldName , config .getFieldName ());
46
+ assertEquals (Fixture .regex , config .pattern ().pattern ());
47
+ assertEquals (Fixture .defaultValue , config .getDefaultValue ());
48
+ assertEquals (Fixture .targetName , config .getTargetName ());
49
+ }
50
+
51
+ @ Test
52
+ void when_regex_field_config_missing_exception_expected () {
53
+ ConfigException configException = assertThrows (
54
+ ConfigException .class ,
55
+ () -> new ExtractValueConfig (Map .of (
56
+ REGEX_CONFIG , Fixture .regex ,
57
+ REGEX_DEFAULT_VALUE_CONFIG , Fixture .defaultValue )));
58
+ assertTrue (configException .getMessage ().contains (REGEX_FIELD_CONFIG ));
59
+ }
60
+
61
+ @ Test
62
+ void when_regex_config_missing_exception_expected () {
63
+ ConfigException configException = assertThrows (
64
+ ConfigException .class ,
65
+ () -> new ExtractValueConfig (Map .of (
66
+ REGEX_FIELD_CONFIG , Fixture .fieldName ,
67
+ REGEX_DEFAULT_VALUE_CONFIG , Fixture .defaultValue ))
68
+ );
69
+ assertTrue (configException .getMessage ().contains (REGEX_CONFIG ));
70
+ }
71
+
72
+ @ Test
73
+ void when_default_value_config_field_config_missing_null_expected () {
74
+ ExtractValueConfig config = new ExtractValueConfig (Map .of (
75
+ REGEX_FIELD_CONFIG , Fixture .fieldName ,
76
+ REGEX_CONFIG , Fixture .regex ));
77
+ assertEquals (Fixture .fieldName , config .getFieldName ());
78
+ assertEquals (Fixture .regex , config .pattern ().pattern ());
79
+ assertNull (config .getDefaultValue ());
80
+ assertNull (config .getTargetName ());
81
+ }
82
+
83
+ @ Test
84
+ void when_target_field_config_missing_null_expected () {
85
+ ExtractValueConfig config = new ExtractValueConfig (Map .of (
86
+ REGEX_FIELD_CONFIG , Fixture .fieldName ,
87
+ REGEX_CONFIG , Fixture .regex ,
88
+ REGEX_DEFAULT_VALUE_CONFIG , Fixture .defaultValue
89
+ ));
90
+ assertEquals (Fixture .fieldName , config .getFieldName ());
91
+ assertEquals (Fixture .regex , config .pattern ().pattern ());
92
+ assertEquals (Fixture .defaultValue , config .getDefaultValue ());
93
+ assertNull (config .getTargetName ());
94
+ }
95
+
96
+ interface Fixture {
97
+ String fieldName = "fieldA" ;
98
+ String regex = "[a-z]" ;
99
+ String defaultValue = "default" ;
100
+ String targetName = "targetA" ;
101
+ }
102
+ }
0 commit comments