forked from jenkinsci/configuration-as-code-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseConfiguratorTest.java
More file actions
487 lines (354 loc) · 15.8 KB
/
BaseConfiguratorTest.java
File metadata and controls
487 lines (354 loc) · 15.8 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package io.jenkins.plugins.casc;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import hudson.util.PersistedList;
import io.jenkins.plugins.casc.model.Mapping;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;
import org.junit.Test;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.Beta;
import org.kohsuke.accmod.restrictions.NoExternalUse;
public class BaseConfiguratorTest {
public static class Animal {}
public static class Dog extends Animal {}
public static class Cat extends Animal {}
public static class Vehicle {}
public static class Car extends Vehicle {}
public static class FakeSecret {}
public static class A_Shape {}
public static class B_Polygon extends A_Shape {}
public static class C_Square extends B_Polygon {}
public interface Z_Interface {}
public static class A_Concrete {}
public static class V_Class {}
public static class W_Class {}
public static class X_Class {}
public static class Y_Class {}
public static class Z_Class {}
@SuppressWarnings("unused")
public static class DummyTarget {
private String[] items;
private Set<String> stringSet;
private SortedSet<String> sortedStringSet;
public String getStandard() {
return null;
}
public void setStandard(String val) {}
public Car getRide() {
return null;
}
public void setRide(Vehicle val) {}
public void setRide(Car val) {}
public void setRide(Object val) {}
public void setRide() {}
public Animal getPet() {
return null;
}
public void setPet(Object pet) {}
public void setPet(Animal pet) {}
public Animal getAmbiguous() {
return null;
}
public void setAmbiguous(Dog dog) {}
public void setAmbiguous(Cat cat) {}
public void setWriteOnly(String val) {}
public FakeSecret getMismatchedToken() {
return null;
}
public void setMismatchedToken(String val) {}
public String[] getItems() {
return items;
}
public void setItems(String[] items) {
this.items = items;
}
public Set<String> getStringSet() {
return stringSet;
}
public void setStringSet(Set<String> stringSet) {
this.stringSet = stringSet;
}
public SortedSet<String> getSortedStringSet() {
return sortedStringSet;
}
public void setSortedStringSet(SortedSet<String> sortedStringSet) {
this.sortedStringSet = sortedStringSet;
}
public List<String> getArrayFallback() {
return null;
}
public void setArrayFallback(String val) {}
public void setArrayFallback(String[] val) {}
public FakeSecret getShapeSubtype() {
return null;
}
public void setShapeSubtype(A_Shape val) {}
public void setShapeSubtype(B_Polygon val) {}
public void setShapeSubtype(C_Square val) {}
public Object getConcreteWins() {
return null;
}
public void setConcreteWins(Z_Interface val) {}
public void setConcreteWins(A_Concrete val) {}
public Object getReverseAlphabetical() {
return null;
}
public void setReverseAlphabetical(Z_Class val) {}
public void setReverseAlphabetical(Y_Class val) {}
public void setReverseAlphabetical(X_Class val) {}
public void setReverseAlphabetical(W_Class val) {}
public void setReverseAlphabetical(V_Class val) {}
public Integer getPrimitiveSetter() {
return null;
}
public void setPrimitiveSetter(int val) {}
public int getWrapperSetter() {
return 0;
}
public void setWrapperSetter(Integer val) {}
public Boolean getPrimitiveBoolean() {
return null;
}
public void setPrimitiveBoolean(boolean val) {}
public Long getPrimitiveLong() {
return null;
}
public void setPrimitiveLong(long val) {}
public Double getPrimitiveDouble() {
return null;
}
public void setPrimitiveDouble(double val) {}
public Float getPrimitiveFloat() {
return null;
}
public void setPrimitiveFloat(float val) {}
public Byte getPrimitiveByte() {
return null;
}
public void setPrimitiveByte(byte val) {}
public Character getPrimitiveChar() {
return null;
}
public void setPrimitiveChar(char val) {}
public Short getPrimitiveShort() {
return null;
}
public void setPrimitiveShort(short val) {}
public void getVoidEdgeCase() {}
public void setVoidEdgeCase(String val) {}
@Restricted(NoExternalUse.class)
public PersistedList<String> getRestrictedList() {
return null;
}
public String getRestrictedSetter() {
return null;
}
@Restricted(Beta.class)
public void setRestrictedSetter(String val) {}
}
public static class DummyConfigurator extends BaseConfigurator<DummyTarget> {
@Override
public Class<DummyTarget> getTarget() {
return DummyTarget.class;
}
@Override
protected DummyTarget instance(Mapping mapping, ConfigurationContext context) {
return new DummyTarget();
}
}
@SuppressWarnings("unused")
public static class NoGetterTarget {
public void setCompletelyMissing(String val) {}
public void setRequiresParam(String val) {}
public String getRequiresParam(String param) {
return param;
}
public void setFakeBoolean(String val) {}
public String isFakeBoolean() {
return "I am a String, not a boolean";
}
}
public static class NoGetterConfigurator extends BaseConfigurator<NoGetterTarget> {
@Override
public Class<NoGetterTarget> getTarget() {
return NoGetterTarget.class;
}
@Override
protected NoGetterTarget instance(Mapping mapping, ConfigurationContext context) {
return new NoGetterTarget();
}
}
@Test
public void testDescribeResolvesBestSetters() {
DummyConfigurator configurator = new DummyConfigurator();
Set<Attribute<DummyTarget, ?>> attributes = configurator.describe();
Map<String, Class<?>> resolvedAttributes =
attributes.stream().collect(Collectors.toMap(Attribute::getName, attr -> (Class<?>) attr.getType()));
assertEquals("Should discover exactly 24 configurable properties", 24, resolvedAttributes.size());
assertEquals("Standard setter should resolve to String", String.class, resolvedAttributes.get("standard"));
assertEquals(
"Exact match should win over subclasses and superclasses", Car.class, resolvedAttributes.get("ride"));
assertEquals(
"Most specific compatible type should win over Object", Animal.class, resolvedAttributes.get("pet"));
assertEquals(
"Alphabetical fallback should deterministically choose Cat over Dog",
Cat.class,
resolvedAttributes.get("ambiguous"));
assertEquals(
"Disjoint getter/setter types should fallback to the available setter parameter type",
String.class,
resolvedAttributes.get("mismatchedToken"));
assertEquals(
"Array types should be resolved to their component type",
String.class,
resolvedAttributes.get("items"));
assertEquals(
"When no exact match exists between getters and setters, array setters should be preferred",
String.class,
resolvedAttributes.get("arrayFallback"));
assertEquals(
"When multiple setters exist in an inheritance hierarchy, the most specific subtype should win",
C_Square.class,
resolvedAttributes.get("shapeSubtype"));
assertEquals(
"Concrete class should win over interface when both are candidates",
A_Concrete.class,
resolvedAttributes.get("concreteWins"));
assertEquals(
"Alphabetical fallback should resolve to the alphabetically first class",
V_Class.class,
resolvedAttributes.get("reverseAlphabetical"));
assertEquals(
"Should map primitive setter with wrapper getter",
int.class,
resolvedAttributes.get("primitiveSetter"));
assertEquals(
"Should map wrapper setter with primitive getter",
Integer.class,
resolvedAttributes.get("wrapperSetter"));
assertEquals(boolean.class, resolvedAttributes.get("primitiveBoolean"));
assertEquals(long.class, resolvedAttributes.get("primitiveLong"));
assertEquals(double.class, resolvedAttributes.get("primitiveDouble"));
assertEquals(float.class, resolvedAttributes.get("primitiveFloat"));
assertEquals(byte.class, resolvedAttributes.get("primitiveByte"));
assertEquals(char.class, resolvedAttributes.get("primitiveChar"));
assertEquals(short.class, resolvedAttributes.get("primitiveShort"));
assertEquals(String.class, resolvedAttributes.get("voidEdgeCase"));
Attribute<DummyTarget, ?> itemsAttr = attributes.stream()
.filter(a -> a.getName().equals("items"))
.findFirst()
.orElseThrow(() -> new AssertionError("items attribute not found"));
assertTrue("items attribute should be marked as multiple", itemsAttr.isMultiple());
Attribute<DummyTarget, ?> restrictedListAttr = attributes.stream()
.filter(a -> a.getName().equals("restrictedList"))
.findFirst()
.orElseThrow(() -> new AssertionError("restrictedList attribute not found"));
assertTrue(
"restrictedList attribute should contain NoExternalUse restriction",
Arrays.asList(restrictedListAttr.getRestrictions()).contains(NoExternalUse.class));
Attribute<DummyTarget, ?> restrictedSetterAttr = attributes.stream()
.filter(a -> a.getName().equals("restrictedSetter"))
.findFirst()
.orElseThrow(() -> new AssertionError("restrictedSetter attribute not found"));
assertTrue(
"restrictedSetter attribute should contain Beta restriction",
Arrays.asList(restrictedSetterAttr.getRestrictions()).contains(Beta.class));
assertEquals(
Set.of(
"standard",
"ride",
"pet",
"ambiguous",
"mismatchedToken",
"items",
"stringSet",
"sortedStringSet",
"arrayFallback",
"shapeSubtype",
"concreteWins",
"reverseAlphabetical",
"primitiveSetter",
"wrapperSetter",
"primitiveBoolean",
"primitiveLong",
"primitiveDouble",
"primitiveFloat",
"primitiveByte",
"primitiveChar",
"primitiveShort",
"voidEdgeCase",
"restrictedList",
"restrictedSetter"),
resolvedAttributes.keySet());
}
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
public void testCollectionToArrayConversion() throws Exception {
DummyConfigurator configurator = new DummyConfigurator();
Set<Attribute<DummyTarget, ?>> attributes = configurator.describe();
Attribute<DummyTarget, ?> itemsAttr = attributes.stream()
.filter(a -> a.getName().equals("items"))
.findFirst()
.orElseThrow(() -> new AssertionError("items attribute not found"));
DummyTarget target = new DummyTarget();
List<String> inputCollection = Arrays.asList("foo", "bar", "baz");
((Attribute) itemsAttr).setValue(target, inputCollection);
String[] result = target.getItems();
assertNotNull("Array should have been set", result);
assertEquals("Array should have the same size as the collection", 3, result.length);
assertArrayEquals(new String[] {"foo", "bar", "baz"}, result);
}
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
public void testCollectionToSetConversions() throws Exception {
DummyConfigurator configurator = new DummyConfigurator();
Set<Attribute<DummyTarget, ?>> attributes = configurator.describe();
Attribute<DummyTarget, ?> setAttr = attributes.stream()
.filter(a -> a.getName().equals("stringSet"))
.findFirst()
.orElseThrow(() -> new AssertionError("stringSet attribute not found"));
Attribute<DummyTarget, ?> sortedSetAttr = attributes.stream()
.filter(a -> a.getName().equals("sortedStringSet"))
.findFirst()
.orElseThrow(() -> new AssertionError("sortedStringSet attribute not found"));
DummyTarget target = new DummyTarget();
List<String> inputCollection = Arrays.asList("foo", "bar", "baz");
((Attribute) setAttr).setValue(target, inputCollection);
((Attribute) sortedSetAttr).setValue(target, inputCollection);
Set<String> setResult = target.getStringSet();
assertNotNull("Set should have been populated", setResult);
assertEquals("Set should be converted to LinkedHashSet", LinkedHashSet.class, setResult.getClass());
assertEquals("Set should have 3 items", 3, setResult.size());
SortedSet<String> sortedSetResult = target.getSortedStringSet();
assertNotNull("SortedSet should have been populated", sortedSetResult);
assertEquals("SortedSet should be converted to TreeSet", TreeSet.class, sortedSetResult.getClass());
assertEquals("SortedSet should have 3 items", 3, sortedSetResult.size());
}
@Test
public void testFindGetterReturnsNullForMissingOrInvalidGetters() {
NoGetterConfigurator configurator = new NoGetterConfigurator();
Set<Attribute<NoGetterTarget, ?>> attributes = configurator.describe();
assertThat("Properties without valid getters should yield no attributes", attributes, empty());
}
@Test
public void testResolveBestSetterBranchCoverage() throws Exception {
DummyConfigurator configurator = new DummyConfigurator();
Method setObj = DummyTarget.class.getMethod("setPet", Object.class);
Method setAnimal = DummyTarget.class.getMethod("setPet", Animal.class);
List<Method> orderedMethods = Arrays.asList(setObj, setAnimal);
Method best = configurator.resolveBestSetter(orderedMethods, null);
assertEquals("Should upgrade bestType and resolve to the more specific Animal setter", setAnimal, best);
}
}