-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathConfigModelTest.java
More file actions
163 lines (149 loc) · 8.3 KB
/
ConfigModelTest.java
File metadata and controls
163 lines (149 loc) · 8.3 KB
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
/*
* Copyright Strimzi authors.
* License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
*/
import io.strimzi.kafka.config.model.ConfigModel;
import io.strimzi.kafka.config.model.Type;
import org.junit.jupiter.api.Test;
import java.util.List;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
public class ConfigModelTest {
@Test
public void testStringValidation() {
ConfigModel cm = new ConfigModel();
cm.setType(Type.STRING);
assertThat(cm.validate("test", "dog"), is(emptyList()));
cm.setValues(asList("foo", "bar"));
assertThat(cm.validate("test", "foo"), is(emptyList()));
assertThat(cm.validate("test", "bar"), is(emptyList()));
assertThat(cm.validate("test", "baz"),
is(List.of("test has value 'baz' which is not one of the allowed values: [foo, bar]")));
cm.setValues(null);
cm.setPattern("foo|bar");
assertThat(cm.validate("test", "foo"), is(emptyList()));
assertThat(cm.validate("test", "bar"), is(emptyList()));
assertThat(cm.validate("test", "baz"),
is(List.of("test has value 'baz' which does not match the required pattern: foo|bar")));
}
@Test
public void testBooleanValidation() {
ConfigModel cm = new ConfigModel();
cm.setType(Type.BOOLEAN);
assertThat(cm.validate("test", "true"), is(emptyList()));
assertThat(cm.validate("test", "false"), is(emptyList()));
assertThat(cm.validate("test", "dog"), is(List.of("test has value 'dog' which is not a boolean")));
}
@Test
public void testShortValidation() {
ConfigModel cm = new ConfigModel();
cm.setType(Type.SHORT);
assertThat(cm.validate("test", "1"), is(emptyList()));
assertThat(cm.validate("test", Short.valueOf(Short.MAX_VALUE).toString()), is(emptyList()));
assertThat(cm.validate("test", Short.valueOf(Short.MIN_VALUE).toString()), is(emptyList()));
assertThat(cm.validate("test", Integer.valueOf((int) Short.MAX_VALUE + 1).toString()), is(List.of("test has value '32768' which is not a short")));
assertThat(cm.validate("test", Integer.valueOf((int) Short.MIN_VALUE - 1).toString()), is(List.of("test has value '-32769' which is not a short")));
cm.setMinimum(0);
assertThat(cm.validate("test", "-1"), is(List.of("test has value -1 which less than the minimum value 0")));
cm.setMaximum(1);
assertThat(cm.validate("test", "2"), is(List.of("test has value 2 which greater than the maximum value 1")));
}
@Test
public void testIntValidation() {
ConfigModel cm = new ConfigModel();
cm.setType(Type.INT);
assertThat(cm.validate("test", "1"), is(emptyList()));
assertThat(cm.validate("test", Integer.valueOf(Integer.MAX_VALUE).toString()), is(emptyList()));
assertThat(cm.validate("test", Integer.valueOf(Integer.MIN_VALUE).toString()), is(emptyList()));
assertThat(cm.validate("test", Long.valueOf((long) Integer.MAX_VALUE + 1L).toString()), is(List.of("test has value '2147483648' which is not an int")));
assertThat(cm.validate("test", Long.valueOf((long) Integer.MIN_VALUE - 1L).toString()), is(List.of("test has value '-2147483649' which is not an int")));
cm.setMinimum(0);
assertThat(cm.validate("test", "-1"), is(List.of("test has value -1 which less than the minimum value 0")));
cm.setMaximum(1);
assertThat(cm.validate("test", "2"), is(List.of("test has value 2 which greater than the maximum value 1")));
}
@Test
public void testIntPatternValidation() {
ConfigModel cm = new ConfigModel();
cm.setType(Type.INT);
cm.setPattern("1|2|3");
assertThat(cm.validate("test", "1"), is(emptyList()));
assertThat(cm.validate("test", "2"), is(emptyList()));
assertThat(cm.validate("test", "3"), is(emptyList()));
assertThat(cm.validate("test", "4"),
is(List.of("test has value '4' which does not match the required pattern: 1|2|3")));
}
@Test
public void testLongValidation() {
ConfigModel cm = new ConfigModel();
cm.setType(Type.LONG);
assertThat(cm.validate("test", "1"), is(emptyList()));
assertThat(cm.validate("test", Long.valueOf(Long.MAX_VALUE).toString()), is(emptyList()));
assertThat(cm.validate("test", Long.valueOf(Long.MIN_VALUE).toString()), is(emptyList()));
assertThat(cm.validate("test", "9223372036854775808"), is(List.of("test has value '9223372036854775808' which is not a long")));
assertThat(cm.validate("test", "-9223372036854775809"), is(List.of("test has value '-9223372036854775809' which is not a long")));
cm.setMinimum(0);
assertThat(cm.validate("test", "-1"), is(List.of("test has value -1 which less than the minimum value 0")));
cm.setMaximum(1);
assertThat(cm.validate("test", "2"), is(List.of("test has value 2 which greater than the maximum value 1")));
}
@Test
public void testDoubleValidation() {
ConfigModel cm = new ConfigModel();
cm.setType(Type.DOUBLE);
assertThat(cm.validate("test", "1"), is(emptyList()));
assertThat(cm.validate("test", "dog"), is(List.of("test has value 'dog' which is not a double")));
cm.setMinimum(0.0);
assertThat(cm.validate("test", "-0.1"), is(List.of("test has value -0.1 which less than the minimum value 0.0")));
cm.setMaximum(1.0);
assertThat(cm.validate("test", "1.1"), is(List.of("test has value 1.1 which greater than the maximum value 1.0")));
}
@Test
public void testListValidation() {
ConfigModel cm = new ConfigModel();
cm.setType(Type.LIST);
assertThat(cm.validate("test", "foo"), is(emptyList()));
assertThat(cm.validate("test", "foo,bar"), is(emptyList()));
cm.setItems(asList("foo", "bar"));
assertThat(cm.validate("test", "foo"), is(emptyList()));
assertThat(cm.validate("test", "foo,bar"), is(emptyList()));
assertThat(cm.validate("test", "foo,bar,baz"),
is(List.of("test contains values [baz] which are not in the allowed items [foo, bar]")));
assertThat(cm.validate("test", "foo, bar, baz"),
is(List.of("test contains values [baz] which are not in the allowed items [foo, bar]")));
assertThat(cm.validate("test", " foo , bar, baz "),
is(List.of("test contains values [baz] which are not in the allowed items [foo, bar]")));
// " foo , bar,," contains duplicate empty strings, so duplicate check triggers first
assertThat(cm.validate("test", " foo , bar,,"),
is(List.of("test contains duplicate values")));
// Test empty items list, should allow any values (Kafka 4.2.0+ behavior with ValidList.anyNonDuplicateValues)
cm.setItems(emptyList());
assertThat(cm.validate("test", "foo"), is(emptyList()));
assertThat(cm.validate("test", "foo,bar,baz"), is(emptyList()));
assertThat(cm.validate("test", "TLSv1.1,TLSv1.2,TLSv1.3"), is(emptyList()));
// Test duplicate values, should be rejected (Kafka 4.2.0+ behavior with ValidList.anyNonDuplicateValues rejects duplicates)
assertThat(cm.validate("test", "foo,bar,foo"),
is(List.of("test contains duplicate values")));
assertThat(cm.validate("test", "TLSv1.2,TLSv1.2"),
is(List.of("test contains duplicate values")));
// Test empty individual values, should be rejected (Kafka's ValidList rejects empty values)
assertThat(cm.validate("test", "foo,,bar"),
is(List.of("test contains empty values")));
assertThat(cm.validate("test", "TLSv1.2,,TLSv1.3"),
is(List.of("test contains empty values")));
}
@Test
public void testPasswordValidation() {
ConfigModel cm = new ConfigModel();
cm.setType(Type.PASSWORD);
assertThat(cm.validate("test", "whatever"), is(emptyList()));
}
@Test
public void testClassValidation() {
ConfigModel cm = new ConfigModel();
cm.setType(Type.CLASS);
assertThat(cm.validate("test", "org.example.Whatever"), is(emptyList()));
}
}