-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathSettingTests.java
1772 lines (1535 loc) · 84.7 KB
/
SettingTests.java
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
package org.opensearch.common.settings;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LogEvent;
import org.opensearch.cluster.metadata.IndexMetadata;
import org.opensearch.common.collect.Tuple;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.common.settings.AbstractScopedSettings.SettingUpdater;
import org.opensearch.common.settings.Setting.ByteSizeValueParser;
import org.opensearch.common.settings.Setting.DoubleParser;
import org.opensearch.common.settings.Setting.FloatParser;
import org.opensearch.common.settings.Setting.IntegerParser;
import org.opensearch.common.settings.Setting.LongParser;
import org.opensearch.common.settings.Setting.MemorySizeValueParser;
import org.opensearch.common.settings.Setting.MinMaxTimeValueParser;
import org.opensearch.common.settings.Setting.MinTimeValueParser;
import org.opensearch.common.settings.Setting.Property;
import org.opensearch.common.settings.Setting.RegexValidator;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.common.io.stream.BytesStreamInput;
import org.opensearch.core.common.unit.ByteSizeUnit;
import org.opensearch.core.common.unit.ByteSizeValue;
import org.opensearch.index.IndexSettings;
import org.opensearch.monitor.jvm.JvmInfo;
import org.opensearch.test.MockLogAppender;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.test.junit.annotations.TestLogging;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.opensearch.index.IndexSettingsTests.newIndexMeta;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.hasToString;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
public class SettingTests extends OpenSearchTestCase {
public void testGet() {
Setting<Boolean> booleanSetting = Setting.boolSetting("foo.bar", false, Property.Dynamic, Property.NodeScope);
assertFalse(booleanSetting.get(Settings.EMPTY));
assertFalse(booleanSetting.get(Settings.builder().put("foo.bar", false).build()));
assertTrue(booleanSetting.get(Settings.builder().put("foo.bar", true).build()));
}
public void testByteSizeSetting() {
final Setting<ByteSizeValue> byteSizeValueSetting = Setting.byteSizeSetting(
"a.byte.size",
new ByteSizeValue(1024),
Property.Dynamic,
Property.NodeScope
);
assertFalse(byteSizeValueSetting.isGroupSetting());
final ByteSizeValue byteSizeValue = byteSizeValueSetting.get(Settings.EMPTY);
assertThat(byteSizeValue.getBytes(), equalTo(1024L));
}
public void testByteSizeSettingMinValue() {
final Setting<ByteSizeValue> byteSizeValueSetting = Setting.byteSizeSetting(
"a.byte.size",
new ByteSizeValue(100, ByteSizeUnit.MB),
new ByteSizeValue(20_000_000, ByteSizeUnit.BYTES),
new ByteSizeValue(Integer.MAX_VALUE, ByteSizeUnit.BYTES)
);
final long value = 20_000_000 - randomIntBetween(1, 1024);
final Settings settings = Settings.builder().put("a.byte.size", value + "b").build();
final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> byteSizeValueSetting.get(settings));
final String expectedMessage = "failed to parse value [" + value + "b] for setting [a.byte.size], must be >= [20000000b]";
assertThat(e, hasToString(containsString(expectedMessage)));
}
public void testByteSizeSettingMaxValue() {
final Setting<ByteSizeValue> byteSizeValueSetting = Setting.byteSizeSetting(
"a.byte.size",
new ByteSizeValue(100, ByteSizeUnit.MB),
new ByteSizeValue(16, ByteSizeUnit.MB),
new ByteSizeValue(Integer.MAX_VALUE, ByteSizeUnit.BYTES)
);
final long value = (1L << 31) - 1 + randomIntBetween(1, 1024);
final Settings settings = Settings.builder().put("a.byte.size", value + "b").build();
final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> byteSizeValueSetting.get(settings));
final String expectedMessage = "failed to parse value [" + value + "b] for setting [a.byte.size], must be <= [2147483647b]";
assertThat(e, hasToString(containsString(expectedMessage)));
}
public void testByteSizeSettingValidation() {
final Setting<ByteSizeValue> byteSizeValueSetting = Setting.byteSizeSetting(
"a.byte.size",
s -> "2048b",
Property.Dynamic,
Property.NodeScope
);
final ByteSizeValue byteSizeValue = byteSizeValueSetting.get(Settings.EMPTY);
assertThat(byteSizeValue.getBytes(), equalTo(2048L));
AtomicReference<ByteSizeValue> value = new AtomicReference<>(null);
ClusterSettings.SettingUpdater<ByteSizeValue> settingUpdater = byteSizeValueSetting.newUpdater(value::set, logger);
final IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> settingUpdater.apply(Settings.builder().put("a.byte.size", 12).build(), Settings.EMPTY)
);
assertThat(e, hasToString(containsString("illegal value can't update [a.byte.size] from [2048b] to [12]")));
assertNotNull(e.getCause());
assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
final IllegalArgumentException cause = (IllegalArgumentException) e.getCause();
final String expected = "failed to parse setting [a.byte.size] with value [12] as a size in bytes: unit is missing or unrecognized";
assertThat(cause, hasToString(containsString(expected)));
assertTrue(settingUpdater.apply(Settings.builder().put("a.byte.size", "12b").build(), Settings.EMPTY));
assertThat(value.get(), equalTo(new ByteSizeValue(12)));
}
public void testMemorySize() {
Setting<ByteSizeValue> memorySizeValueSetting = Setting.memorySizeSetting(
"a.byte.size",
new ByteSizeValue(1024),
Property.Dynamic,
Property.NodeScope
);
assertFalse(memorySizeValueSetting.isGroupSetting());
ByteSizeValue memorySizeValue = memorySizeValueSetting.get(Settings.EMPTY);
assertEquals(memorySizeValue.getBytes(), 1024);
memorySizeValueSetting = Setting.memorySizeSetting("a.byte.size", s -> "2048b", Property.Dynamic, Property.NodeScope);
memorySizeValue = memorySizeValueSetting.get(Settings.EMPTY);
assertEquals(memorySizeValue.getBytes(), 2048);
memorySizeValueSetting = Setting.memorySizeSetting("a.byte.size", "50%", Property.Dynamic, Property.NodeScope);
assertFalse(memorySizeValueSetting.isGroupSetting());
memorySizeValue = memorySizeValueSetting.get(Settings.EMPTY);
assertEquals(memorySizeValue.getBytes(), JvmInfo.jvmInfo().getMem().getHeapMax().getBytes() * 0.5, 1.0);
memorySizeValueSetting = Setting.memorySizeSetting("a.byte.size", s -> "25%", Property.Dynamic, Property.NodeScope);
memorySizeValue = memorySizeValueSetting.get(Settings.EMPTY);
assertEquals(memorySizeValue.getBytes(), JvmInfo.jvmInfo().getMem().getHeapMax().getBytes() * 0.25, 1.0);
AtomicReference<ByteSizeValue> value = new AtomicReference<>(null);
ClusterSettings.SettingUpdater<ByteSizeValue> settingUpdater = memorySizeValueSetting.newUpdater(value::set, logger);
try {
settingUpdater.apply(Settings.builder().put("a.byte.size", 12).build(), Settings.EMPTY);
fail("no unit");
} catch (IllegalArgumentException ex) {
assertThat(ex, hasToString(containsString("illegal value can't update [a.byte.size] from [25%] to [12]")));
assertNotNull(ex.getCause());
assertThat(ex.getCause(), instanceOf(IllegalArgumentException.class));
final IllegalArgumentException cause = (IllegalArgumentException) ex.getCause();
final String expected =
"failed to parse setting [a.byte.size] with value [12] as a size in bytes: unit is missing or unrecognized";
assertThat(cause, hasToString(containsString(expected)));
}
assertTrue(settingUpdater.apply(Settings.builder().put("a.byte.size", "12b").build(), Settings.EMPTY));
assertEquals(new ByteSizeValue(12), value.get());
assertTrue(settingUpdater.apply(Settings.builder().put("a.byte.size", "20%").build(), Settings.EMPTY));
assertEquals(new ByteSizeValue((long) (JvmInfo.jvmInfo().getMem().getHeapMax().getBytes() * 0.2)), value.get());
}
public void testMemorySizeWithFallbackValue() {
Setting<ByteSizeValue> fallbackSetting = Setting.memorySizeSetting("a.byte.size", "20%", Property.Dynamic, Property.NodeScope);
Setting<ByteSizeValue> memorySizeSetting = Setting.memorySizeSetting(
"b.byte.size",
fallbackSetting,
Property.Dynamic,
Property.NodeScope
);
AtomicReference<ByteSizeValue> value = new AtomicReference<>(null);
ClusterSettings.SettingUpdater<ByteSizeValue> settingUpdater = memorySizeSetting.newUpdater(value::set, logger);
ByteSizeValue memorySizeValue = memorySizeSetting.get(Settings.EMPTY);
assertEquals(memorySizeValue.getBytes(), JvmInfo.jvmInfo().getMem().getHeapMax().getBytes() * 0.2, 1.0);
assertTrue(settingUpdater.apply(Settings.builder().put("a.byte.size", "30%").build(), Settings.EMPTY));
// If value=getHeapMax()*0.3 is bigger than 2gb, and is bigger than Integer.MAX_VALUE,
// then (long)((int) value) will lose precision.
assertEquals(new ByteSizeValue((long) (JvmInfo.jvmInfo().getMem().getHeapMax().getBytes() * 0.3)), value.get());
assertTrue(settingUpdater.apply(Settings.builder().put("b.byte.size", "40%").build(), Settings.EMPTY));
assertEquals(new ByteSizeValue((long) (JvmInfo.jvmInfo().getMem().getHeapMax().getBytes() * 0.4)), value.get());
}
public void testSimpleUpdate() {
Setting<Boolean> booleanSetting = Setting.boolSetting("foo.bar", false, Property.Dynamic, Property.NodeScope);
AtomicReference<Boolean> atomicBoolean = new AtomicReference<>(null);
ClusterSettings.SettingUpdater<Boolean> settingUpdater = booleanSetting.newUpdater(atomicBoolean::set, logger);
Settings build = Settings.builder().put("foo.bar", false).build();
settingUpdater.apply(build, Settings.EMPTY);
assertNull(atomicBoolean.get());
build = Settings.builder().put("foo.bar", true).build();
settingUpdater.apply(build, Settings.EMPTY);
assertTrue(atomicBoolean.get());
// try update bogus value
build = Settings.builder().put("foo.bar", "I am not a boolean").build();
try {
settingUpdater.apply(build, Settings.EMPTY);
fail("not a boolean");
} catch (IllegalArgumentException ex) {
assertThat(ex, hasToString(containsString("illegal value can't update [foo.bar] from [false] to [I am not a boolean]")));
assertNotNull(ex.getCause());
assertThat(ex.getCause(), instanceOf(IllegalArgumentException.class));
final IllegalArgumentException cause = (IllegalArgumentException) ex.getCause();
assertThat(
cause,
hasToString(containsString("Failed to parse value [I am not a boolean] as only [true] or [false] are allowed."))
);
}
}
public void testSimpleUpdateOfFilteredSetting() {
Setting<Boolean> booleanSetting = Setting.boolSetting("foo.bar", false, Property.Dynamic, Property.Filtered);
AtomicReference<Boolean> atomicBoolean = new AtomicReference<>(null);
ClusterSettings.SettingUpdater<Boolean> settingUpdater = booleanSetting.newUpdater(atomicBoolean::set, logger);
// try update bogus value
Settings build = Settings.builder().put("foo.bar", "I am not a boolean").build();
IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> settingUpdater.apply(build, Settings.EMPTY));
assertThat(ex, hasToString(equalTo("java.lang.IllegalArgumentException: illegal value can't update [foo.bar]")));
assertNull(ex.getCause());
}
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/33135")
public void testValidateStringSetting() {
Settings settings = Settings.builder().putList("foo.bar", Arrays.asList("bla-a", "bla-b")).build();
Setting<String> stringSetting = Setting.simpleString("foo.bar", Property.NodeScope);
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> stringSetting.get(settings));
assertEquals("Found list type value for setting [foo.bar] but but did not expect a list for it.", e.getMessage());
}
private static final Setting<String> FOO_BAR_SETTING = new Setting<>(
"foo.bar",
"foobar",
Function.identity(),
new FooBarValidator(),
Property.Dynamic,
Property.NodeScope
);
private static final Setting<String> BAZ_QUX_SETTING = Setting.simpleString("baz.qux", Property.NodeScope);
private static final Setting<String> QUUX_QUUZ_SETTING = Setting.simpleString("quux.quuz", Property.NodeScope);
static class FooBarValidator implements Setting.Validator<String> {
public static boolean invokedInIsolation;
public static boolean invokedWithDependencies;
@Override
public void validate(final String value) {
invokedInIsolation = true;
assertThat(value, equalTo("foo.bar value"));
}
@Override
public void validate(final String value, final Map<Setting<?>, Object> settings) {
invokedWithDependencies = true;
assertTrue(settings.keySet().contains(BAZ_QUX_SETTING));
assertThat(settings.get(BAZ_QUX_SETTING), equalTo("baz.qux value"));
assertTrue(settings.keySet().contains(QUUX_QUUZ_SETTING));
assertThat(settings.get(QUUX_QUUZ_SETTING), equalTo("quux.quuz value"));
}
@Override
public Iterator<Setting<?>> settings() {
final List<Setting<?>> settings = Arrays.asList(BAZ_QUX_SETTING, QUUX_QUUZ_SETTING);
return settings.iterator();
}
}
// the purpose of this test is merely to ensure that a validator is invoked with the appropriate values
public void testValidator() {
final Settings settings = Settings.builder()
.put("foo.bar", "foo.bar value")
.put("baz.qux", "baz.qux value")
.put("quux.quuz", "quux.quuz value")
.build();
FOO_BAR_SETTING.get(settings);
assertTrue(FooBarValidator.invokedInIsolation);
assertTrue(FooBarValidator.invokedWithDependencies);
}
public void testRegexValidator() throws Exception {
// A regex that matches one or more digits
String expectedRegex = "\\d+";
Pattern expectedPattern = Pattern.compile(expectedRegex);
RegexValidator regexValidator = new RegexValidator(expectedRegex);
RegexValidator regexValidatorMatcherFalse = new RegexValidator(expectedRegex, false);
// Test that the pattern is correctly initialized
assertNotNull(expectedPattern);
assertNotNull(regexValidator.getPattern());
assertEquals(expectedPattern.pattern(), regexValidator.getPattern().pattern());
// Test that checks the pattern and isMatching with the set value false parameters are working correctly during initialization
assertNotNull(regexValidatorMatcherFalse);
assertNotNull(regexValidatorMatcherFalse.getPattern());
assertEquals(expectedPattern.pattern(), regexValidatorMatcherFalse.getPattern().pattern());
// Test throw an exception when the value does not match
final RegexValidator finalValidator = new RegexValidator(expectedRegex);
assertThrows(IllegalArgumentException.class, () -> finalValidator.validate("foo"));
try {
regexValidator.validate("123");
} catch (IllegalArgumentException e) {
fail("Expected validate() to not throw an exception, but it threw " + e);
}
// Test throws an exception when the value matches
final RegexValidator finalValidatorFalse = new RegexValidator(expectedRegex);
assertThrows(IllegalArgumentException.class, () -> finalValidatorFalse.validate(expectedRegex));
try {
regexValidatorMatcherFalse.validate(expectedRegex);
} catch (IllegalArgumentException e) {
fail("Expected validate() to not throw an exception, but it threw " + e);
}
try (BytesStreamOutput out = new BytesStreamOutput()) {
regexValidator.writeTo(out);
out.flush();
try (BytesStreamInput in = new BytesStreamInput(BytesReference.toBytes(out.bytes()))) {
regexValidator = new RegexValidator(in);
assertEquals(expectedPattern.pattern(), regexValidator.getPattern().pattern());
// Test that validate() throws an exception for invalid input
final RegexValidator newFinalValidator = new RegexValidator(expectedRegex);
assertThrows(IllegalArgumentException.class, () -> newFinalValidator.validate("foo"));
// Test that validate() does not throw an exception for valid input
try {
regexValidator.validate("123");
} catch (IllegalArgumentException e) {
fail("Expected validate() to not throw an exception, but it threw " + e);
}
}
}
}
public void testValidatorForFilteredStringSetting() {
final Setting<String> filteredStringSetting = new Setting<>("foo.bar", "foobar", Function.identity(), value -> {
throw new SettingsException("validate always fails");
}, Property.Filtered);
final Settings settings = Settings.builder().put(filteredStringSetting.getKey(), filteredStringSetting.getKey() + " value").build();
final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> filteredStringSetting.get(settings));
assertThat(e, hasToString(containsString("Failed to parse value for setting [" + filteredStringSetting.getKey() + "]")));
assertThat(e.getCause(), instanceOf(SettingsException.class));
assertThat(e.getCause(), hasToString(containsString("validate always fails")));
}
public void testFilteredFloatSetting() {
final IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> Setting.floatSetting("foo", 42.0f, 43.0f, Property.Filtered)
);
assertThat(e, hasToString(containsString("Failed to parse value for setting [foo] must be >= 43.0")));
}
public void testFilteredDoubleSetting() {
final IllegalArgumentException e1 = expectThrows(
IllegalArgumentException.class,
() -> Setting.doubleSetting("foo", 42.0, 43.0, Property.Filtered)
);
assertThat(e1, hasToString(containsString("Failed to parse value for setting [foo] must be >= 43.0")));
final IllegalArgumentException e2 = expectThrows(
IllegalArgumentException.class,
() -> Setting.doubleSetting("foo", 45.0, 43.0, 44.0, Property.Filtered)
);
assertThat(e2, hasToString(containsString("Failed to parse value for setting [foo] must be <= 44.0")));
}
public void testFilteredIntSetting() {
final IllegalArgumentException e1 = expectThrows(
IllegalArgumentException.class,
() -> Setting.intSetting("foo", 42, 43, 44, Property.Filtered)
);
assertThat(e1, hasToString(containsString("Failed to parse value for setting [foo] must be >= 43")));
final IllegalArgumentException e2 = expectThrows(
IllegalArgumentException.class,
() -> Setting.intSetting("foo", 45, 43, 44, Property.Filtered)
);
assertThat(e2, hasToString(containsString("Failed to parse value for setting [foo] must be <= 44")));
}
public void testFilteredLongSetting() {
final IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> Setting.longSetting("foo", 42L, 43L, Property.Filtered)
);
assertThat(e, hasToString(containsString("Failed to parse value for setting [foo] must be >= 43")));
}
public void testFilteredTimeSetting() {
final IllegalArgumentException e1 = expectThrows(
IllegalArgumentException.class,
() -> Setting.timeSetting("foo", TimeValue.timeValueHours(1), TimeValue.timeValueHours(2), Property.Filtered)
);
assertThat(e1, hasToString(containsString("failed to parse value for setting [foo], must be >= [2h]")));
final IllegalArgumentException e2 = expectThrows(
IllegalArgumentException.class,
() -> Setting.timeSetting(
"foo",
TimeValue.timeValueHours(4),
TimeValue.timeValueHours(2),
TimeValue.timeValueHours(3),
Property.Filtered
)
);
assertThat(e2, hasToString(containsString("failed to parse value for setting [foo], must be <= [3h]")));
final Setting minSetting = Setting.timeSetting("foo", TimeValue.timeValueHours(3), TimeValue.timeValueHours(2), Property.Filtered);
final Settings minSettings = Settings.builder().put("foo", "not a time value").build();
final IllegalArgumentException e3 = expectThrows(IllegalArgumentException.class, () -> minSetting.get(minSettings));
assertThat(e3, hasToString(containsString("failed to parse value for setting [foo] as a time value")));
assertNull(e3.getCause());
final Setting maxSetting = Setting.timeSetting(
"foo",
TimeValue.timeValueHours(3),
TimeValue.timeValueHours(2),
TimeValue.timeValueHours(4),
Property.Filtered
);
final Settings maxSettings = Settings.builder().put("foo", "not a time value").build();
final IllegalArgumentException e4 = expectThrows(IllegalArgumentException.class, () -> maxSetting.get(maxSettings));
assertThat(e4, hasToString(containsString("failed to parse value for setting [foo] as a time value")));
assertNull(e4.getCause());
}
public void testFilteredBooleanSetting() {
Setting setting = Setting.boolSetting("foo", false, Property.Filtered);
final Settings settings = Settings.builder().put("foo", "not a boolean value").build();
final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> setting.get(settings));
assertThat(e, hasToString(containsString("Failed to parse value for setting [foo]")));
assertNull(e.getCause());
}
public void testUpdateNotDynamic() {
Setting<Boolean> booleanSetting = Setting.boolSetting("foo.bar", false, Property.NodeScope);
assertFalse(booleanSetting.isGroupSetting());
AtomicReference<Boolean> atomicBoolean = new AtomicReference<>(null);
try {
booleanSetting.newUpdater(atomicBoolean::set, logger);
fail("not dynamic");
} catch (IllegalStateException ex) {
assertEquals("setting [foo.bar] is not dynamic", ex.getMessage());
}
}
public void testUpdaterIsIsolated() {
Setting<Boolean> booleanSetting = Setting.boolSetting("foo.bar", false, Property.Dynamic, Property.NodeScope);
AtomicReference<Boolean> ab1 = new AtomicReference<>(null);
AtomicReference<Boolean> ab2 = new AtomicReference<>(null);
ClusterSettings.SettingUpdater<Boolean> settingUpdater = booleanSetting.newUpdater(ab1::set, logger);
settingUpdater.apply(Settings.builder().put("foo.bar", true).build(), Settings.EMPTY);
assertTrue(ab1.get());
assertNull(ab2.get());
}
public void testDefault() {
TimeValue defaultValue = TimeValue.timeValueMillis(randomIntBetween(0, 1000000));
Setting<TimeValue> setting = Setting.positiveTimeSetting("my.time.value", defaultValue, Property.NodeScope);
assertFalse(setting.isGroupSetting());
String aDefault = setting.getDefaultRaw(Settings.EMPTY);
assertEquals(defaultValue.millis() + "ms", aDefault);
assertEquals(defaultValue.millis(), setting.get(Settings.EMPTY).millis());
assertEquals(defaultValue, setting.getDefault(Settings.EMPTY));
Setting<String> secondaryDefault = new Setting<>(
"foo.bar",
(s) -> s.get("old.foo.bar", "some_default"),
Function.identity(),
Property.NodeScope
);
assertEquals("some_default", secondaryDefault.get(Settings.EMPTY));
assertEquals("42", secondaryDefault.get(Settings.builder().put("old.foo.bar", 42).build()));
Setting<String> secondaryDefaultViaSettings = new Setting<>("foo.bar", secondaryDefault, Function.identity(), Property.NodeScope);
assertEquals("some_default", secondaryDefaultViaSettings.get(Settings.EMPTY));
assertEquals("42", secondaryDefaultViaSettings.get(Settings.builder().put("old.foo.bar", 42).build()));
// It gets more complicated when there are two settings objects....
Settings hasFallback = Settings.builder().put("foo.bar", "o").build();
Setting<String> fallsback = new Setting<>("foo.baz", secondaryDefault, Function.identity(), Property.NodeScope);
assertEquals("o", fallsback.get(hasFallback));
assertEquals("some_default", fallsback.get(Settings.EMPTY));
assertEquals("some_default", fallsback.get(Settings.EMPTY, Settings.EMPTY));
assertEquals("o", fallsback.get(Settings.EMPTY, hasFallback));
assertEquals("o", fallsback.get(hasFallback, Settings.EMPTY));
assertEquals("a", fallsback.get(Settings.builder().put("foo.bar", "a").build(), Settings.builder().put("foo.bar", "b").build()));
}
public void testComplexType() {
AtomicReference<ComplexType> ref = new AtomicReference<>(null);
Setting<ComplexType> setting = new Setting<>("foo.bar", (s) -> "", (s) -> new ComplexType(s), Property.Dynamic, Property.NodeScope);
assertFalse(setting.isGroupSetting());
ref.set(setting.get(Settings.EMPTY));
ComplexType type = ref.get();
ClusterSettings.SettingUpdater<ComplexType> settingUpdater = setting.newUpdater(ref::set, logger);
assertFalse(settingUpdater.apply(Settings.EMPTY, Settings.EMPTY));
assertSame("no update - type has not changed", type, ref.get());
// change from default
assertTrue(settingUpdater.apply(Settings.builder().put("foo.bar", "2").build(), Settings.EMPTY));
assertNotSame("update - type has changed", type, ref.get());
assertEquals("2", ref.get().foo);
// change back to default...
assertTrue(settingUpdater.apply(Settings.EMPTY, Settings.builder().put("foo.bar", "2").build()));
assertNotSame("update - type has changed", type, ref.get());
assertEquals("", ref.get().foo);
}
public void testType() {
Setting<Integer> integerSetting = Setting.intSetting("foo.int.bar", 1, Property.Dynamic, Property.NodeScope);
assertThat(integerSetting.hasNodeScope(), is(true));
assertThat(integerSetting.hasIndexScope(), is(false));
integerSetting = Setting.intSetting("foo.int.bar", 1, Property.Dynamic, Property.IndexScope);
assertThat(integerSetting.hasIndexScope(), is(true));
assertThat(integerSetting.hasNodeScope(), is(false));
}
public void testGroups() {
AtomicReference<Settings> ref = new AtomicReference<>(null);
Setting<Settings> setting = Setting.groupSetting("foo.bar.", Property.Dynamic, Property.NodeScope);
assertTrue(setting.isGroupSetting());
ClusterSettings.SettingUpdater<Settings> settingUpdater = setting.newUpdater(ref::set, logger);
Settings currentInput = Settings.builder()
.put("foo.bar.1.value", "1")
.put("foo.bar.2.value", "2")
.put("foo.bar.3.value", "3")
.build();
Settings previousInput = Settings.EMPTY;
assertTrue(settingUpdater.apply(currentInput, previousInput));
assertNotNull(ref.get());
Settings settings = ref.get();
Map<String, Settings> asMap = settings.getAsGroups();
assertEquals(3, asMap.size());
assertEquals(asMap.get("1").get("value"), "1");
assertEquals(asMap.get("2").get("value"), "2");
assertEquals(asMap.get("3").get("value"), "3");
previousInput = currentInput;
currentInput = Settings.builder().put("foo.bar.1.value", "1").put("foo.bar.2.value", "2").put("foo.bar.3.value", "3").build();
Settings current = ref.get();
assertFalse(settingUpdater.apply(currentInput, previousInput));
assertSame(current, ref.get());
previousInput = currentInput;
currentInput = Settings.builder().put("foo.bar.1.value", "1").put("foo.bar.2.value", "2").build();
// now update and check that we got it
assertTrue(settingUpdater.apply(currentInput, previousInput));
assertNotSame(current, ref.get());
asMap = ref.get().getAsGroups();
assertEquals(2, asMap.size());
assertEquals(asMap.get("1").get("value"), "1");
assertEquals(asMap.get("2").get("value"), "2");
previousInput = currentInput;
currentInput = Settings.builder().put("foo.bar.1.value", "1").put("foo.bar.2.value", "4").build();
// now update and check that we got it
assertTrue(settingUpdater.apply(currentInput, previousInput));
assertNotSame(current, ref.get());
asMap = ref.get().getAsGroups();
assertEquals(2, asMap.size());
assertEquals(asMap.get("1").get("value"), "1");
assertEquals(asMap.get("2").get("value"), "4");
assertTrue(setting.match("foo.bar.baz"));
assertFalse(setting.match("foo.baz.bar"));
ClusterSettings.SettingUpdater<Settings> predicateSettingUpdater = setting.newUpdater(ref::set, logger, (s) -> assertFalse(true));
try {
predicateSettingUpdater.apply(
Settings.builder().put("foo.bar.1.value", "1").put("foo.bar.2.value", "2").build(),
Settings.EMPTY
);
fail("not accepted");
} catch (IllegalArgumentException ex) {
assertEquals(ex.getMessage(), "illegal value can't update [foo.bar.] from [{}] to [{\"1.value\":\"1\",\"2.value\":\"2\"}]");
}
}
public void testGroupSettingFallback() {
Setting<Settings> fallbackSetting = Setting.groupSetting("old.bar.", Property.Dynamic, Property.NodeScope);
assertFalse(fallbackSetting.exists(Settings.EMPTY));
Setting<Settings> groupSetting = Setting.groupSetting("new.bar.", fallbackSetting, Property.Dynamic, Property.NodeScope);
assertFalse(groupSetting.exists(Settings.EMPTY));
Settings values = Settings.builder().put("old.bar.1.value", "value 1").put("old.bar.2.value", "value 2").build();
assertTrue(groupSetting.exists(values));
Map<String, Settings> asMap = groupSetting.get(values).getAsGroups();
assertEquals(2, asMap.size());
assertEquals("value 1", asMap.get("1").get("value"));
assertEquals("value 2", asMap.get("2").get("value"));
}
public void testFilteredGroups() {
AtomicReference<Settings> ref = new AtomicReference<>(null);
Setting<Settings> setting = Setting.groupSetting("foo.bar.", Property.Filtered, Property.Dynamic);
ClusterSettings.SettingUpdater<Settings> predicateSettingUpdater = setting.newUpdater(ref::set, logger, (s) -> assertFalse(true));
IllegalArgumentException ex = expectThrows(
IllegalArgumentException.class,
() -> predicateSettingUpdater.apply(
Settings.builder().put("foo.bar.1.value", "1").put("foo.bar.2.value", "2").build(),
Settings.EMPTY
)
);
assertEquals("illegal value can't update [foo.bar.]", ex.getMessage());
}
public static class ComplexType {
final String foo;
public ComplexType(String foo) {
this.foo = foo;
}
}
public static class Composite {
private Integer b;
private Integer a;
public void set(Integer a, Integer b) {
this.a = a;
this.b = b;
}
public void validate(Integer a, Integer b) {
if (Integer.signum(a) != Integer.signum(b)) {
throw new IllegalArgumentException("boom");
}
}
}
public void testComposite() {
Composite c = new Composite();
Setting<Integer> a = Setting.intSetting("foo.int.bar.a", 1, Property.Dynamic, Property.NodeScope);
Setting<Integer> b = Setting.intSetting("foo.int.bar.b", 1, Property.Dynamic, Property.NodeScope);
ClusterSettings.SettingUpdater<Tuple<Integer, Integer>> settingUpdater = Setting.compoundUpdater(c::set, c::validate, a, b, logger);
assertFalse(settingUpdater.apply(Settings.EMPTY, Settings.EMPTY));
assertNull(c.a);
assertNull(c.b);
Settings build = Settings.builder().put("foo.int.bar.a", 2).build();
assertTrue(settingUpdater.apply(build, Settings.EMPTY));
assertEquals(2, c.a.intValue());
assertEquals(1, c.b.intValue());
Integer aValue = c.a;
assertFalse(settingUpdater.apply(build, build));
assertSame(aValue, c.a);
Settings previous = build;
build = Settings.builder().put("foo.int.bar.a", 2).put("foo.int.bar.b", 5).build();
assertTrue(settingUpdater.apply(build, previous));
assertEquals(2, c.a.intValue());
assertEquals(5, c.b.intValue());
// reset to default
assertTrue(settingUpdater.apply(Settings.EMPTY, build));
assertEquals(1, c.a.intValue());
assertEquals(1, c.b.intValue());
}
public void testCompositeValidator() {
Composite c = new Composite();
Setting<Integer> a = Setting.intSetting("foo.int.bar.a", 1, Property.Dynamic, Property.NodeScope);
Setting<Integer> b = Setting.intSetting("foo.int.bar.b", 1, Property.Dynamic, Property.NodeScope);
ClusterSettings.SettingUpdater<Tuple<Integer, Integer>> settingUpdater = Setting.compoundUpdater(c::set, c::validate, a, b, logger);
assertFalse(settingUpdater.apply(Settings.EMPTY, Settings.EMPTY));
assertNull(c.a);
assertNull(c.b);
Settings build = Settings.builder().put("foo.int.bar.a", 2).build();
assertTrue(settingUpdater.apply(build, Settings.EMPTY));
assertEquals(2, c.a.intValue());
assertEquals(1, c.b.intValue());
Integer aValue = c.a;
assertFalse(settingUpdater.apply(build, build));
assertSame(aValue, c.a);
Settings previous = build;
build = Settings.builder().put("foo.int.bar.a", 2).put("foo.int.bar.b", 5).build();
assertTrue(settingUpdater.apply(build, previous));
assertEquals(2, c.a.intValue());
assertEquals(5, c.b.intValue());
Settings invalid = Settings.builder().put("foo.int.bar.a", -2).put("foo.int.bar.b", 5).build();
IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> settingUpdater.apply(invalid, previous));
assertThat(exc.getMessage(), equalTo("boom"));
// reset to default
assertTrue(settingUpdater.apply(Settings.EMPTY, build));
assertEquals(1, c.a.intValue());
assertEquals(1, c.b.intValue());
}
public void testListSettingsDeprecated() {
final Setting<List<String>> deprecatedListSetting = Setting.listSetting(
"foo.deprecated",
Collections.singletonList("foo.deprecated"),
Function.identity(),
Property.Deprecated,
Property.NodeScope
);
final Setting<List<String>> nonDeprecatedListSetting = Setting.listSetting(
"foo.non_deprecated",
Collections.singletonList("foo.non_deprecated"),
Function.identity(),
Property.NodeScope
);
final Settings settings = Settings.builder()
.put("foo.deprecated", "foo.deprecated1,foo.deprecated2")
.put("foo.deprecated", "foo.non_deprecated1,foo.non_deprecated2")
.build();
deprecatedListSetting.get(settings);
nonDeprecatedListSetting.get(settings);
assertSettingDeprecationsAndWarnings(new Setting[] { deprecatedListSetting });
}
public void testListSettings() {
Setting<List<String>> listSetting = Setting.listSetting(
"foo.bar",
Arrays.asList("foo,bar"),
(s) -> s.toString(),
Property.Dynamic,
Property.NodeScope
);
List<String> value = listSetting.get(Settings.EMPTY);
assertFalse(listSetting.exists(Settings.EMPTY));
assertEquals(1, value.size());
assertEquals("foo,bar", value.get(0));
List<String> input = Arrays.asList("test", "test1, test2", "test", ",,,,");
Settings.Builder builder = Settings.builder().putList("foo.bar", input.toArray(new String[0]));
assertTrue(listSetting.exists(builder.build()));
value = listSetting.get(builder.build());
assertEquals(input.size(), value.size());
assertArrayEquals(value.toArray(new String[0]), input.toArray(new String[0]));
// try to parse this really annoying format
builder = Settings.builder();
for (int i = 0; i < input.size(); i++) {
builder.put("foo.bar." + i, input.get(i));
}
value = listSetting.get(builder.build());
assertEquals(input.size(), value.size());
assertArrayEquals(value.toArray(new String[0]), input.toArray(new String[0]));
assertTrue(listSetting.exists(builder.build()));
AtomicReference<List<String>> ref = new AtomicReference<>();
AbstractScopedSettings.SettingUpdater<List<String>> settingUpdater = listSetting.newUpdater(ref::set, logger);
assertTrue(settingUpdater.hasChanged(builder.build(), Settings.EMPTY));
settingUpdater.apply(builder.build(), Settings.EMPTY);
assertEquals(input.size(), ref.get().size());
assertArrayEquals(ref.get().toArray(new String[0]), input.toArray(new String[0]));
settingUpdater.apply(Settings.builder().putList("foo.bar", "123").build(), builder.build());
assertEquals(1, ref.get().size());
assertArrayEquals(ref.get().toArray(new String[0]), new String[] { "123" });
settingUpdater.apply(Settings.builder().put("foo.bar", "1,2,3").build(), Settings.builder().putList("foo.bar", "123").build());
assertEquals(3, ref.get().size());
assertArrayEquals(ref.get().toArray(new String[0]), new String[] { "1", "2", "3" });
settingUpdater.apply(Settings.EMPTY, Settings.builder().put("foo.bar", "1,2,3").build());
assertEquals(1, ref.get().size());
assertEquals("foo,bar", ref.get().get(0));
Setting<List<Integer>> otherSettings = Setting.listSetting(
"foo.bar",
Collections.emptyList(),
Integer::parseInt,
Property.Dynamic,
Property.NodeScope
);
List<Integer> defaultValue = otherSettings.get(Settings.EMPTY);
assertEquals(0, defaultValue.size());
List<Integer> intValues = otherSettings.get(Settings.builder().put("foo.bar", "0,1,2,3").build());
assertEquals(4, intValues.size());
for (int i = 0; i < intValues.size(); i++) {
assertEquals(i, intValues.get(i).intValue());
}
Setting<List<String>> settingWithFallback = Setting.listSetting(
"foo.baz",
listSetting,
Function.identity(),
Property.Dynamic,
Property.NodeScope
);
value = settingWithFallback.get(Settings.EMPTY);
assertEquals(1, value.size());
assertEquals("foo,bar", value.get(0));
value = settingWithFallback.get(Settings.builder().putList("foo.bar", "1", "2").build());
assertEquals(2, value.size());
assertEquals("1", value.get(0));
assertEquals("2", value.get(1));
value = settingWithFallback.get(Settings.builder().putList("foo.baz", "3", "4").build());
assertEquals(2, value.size());
assertEquals("3", value.get(0));
assertEquals("4", value.get(1));
value = settingWithFallback.get(Settings.builder().putList("foo.baz", "3", "4").putList("foo.bar", "1", "2").build());
assertEquals(2, value.size());
assertEquals("3", value.get(0));
assertEquals("4", value.get(1));
}
public void testListSettingAcceptsNumberSyntax() {
Setting<List<String>> listSetting = Setting.listSetting(
"foo.bar",
Arrays.asList("foo,bar"),
(s) -> s.toString(),
Property.Dynamic,
Property.NodeScope
);
List<String> input = Arrays.asList("test", "test1, test2", "test", ",,,,");
Settings.Builder builder = Settings.builder().putList("foo.bar", input.toArray(new String[0]));
// try to parse this really annoying format
for (String key : builder.keys()) {
assertTrue("key: " + key + " doesn't match", listSetting.match(key));
}
builder = Settings.builder().put("foo.bar", "1,2,3");
for (String key : builder.keys()) {
assertTrue("key: " + key + " doesn't match", listSetting.match(key));
}
assertFalse(listSetting.match("foo_bar"));
assertFalse(listSetting.match("foo_bar.1"));
assertTrue(listSetting.match("foo.bar"));
assertTrue(listSetting.match("foo.bar." + randomIntBetween(0, 10000)));
}
public void testDynamicKeySetting() {
Setting<Boolean> setting = Setting.prefixKeySetting("foo.", (key) -> Setting.boolSetting(key, false, Property.NodeScope));
assertTrue(setting.hasComplexMatcher());
assertTrue(setting.match("foo.bar"));
assertFalse(setting.match("foo"));
Setting<Boolean> concreteSetting = setting.getConcreteSetting("foo.bar");
assertTrue(concreteSetting.get(Settings.builder().put("foo.bar", "true").build()));
assertFalse(concreteSetting.get(Settings.builder().put("foo.baz", "true").build()));
try {
setting.getConcreteSetting("foo");
fail();
} catch (IllegalArgumentException ex) {
assertEquals("key [foo] must match [foo.] but didn't.", ex.getMessage());
}
}
public void testAffixKeySettingWithDynamicPrefix() {
Setting.AffixSetting<Boolean> setting = Setting.suffixKeySetting(
"enable",
(key) -> Setting.boolSetting(key, false, Property.NodeScope)
);
Setting<Boolean> concreteSetting = setting.getConcreteSettingForNamespace("foo.bar");
assertEquals("foo.bar.enable", concreteSetting.getKey());
IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> setting.getConcreteSettingForNamespace("foo."));
assertEquals("key [foo..enable] must match [*.enable] but didn't.", ex.getMessage());
}
public void testAffixKeySetting() {
Setting<Boolean> setting = Setting.affixKeySetting("foo.", "enable", (key) -> Setting.boolSetting(key, false, Property.NodeScope));
assertTrue(setting.hasComplexMatcher());
assertTrue(setting.match("foo.bar.enable"));
assertTrue(setting.match("foo.baz.enable"));
assertFalse(setting.match("foo.bar.baz.enable"));
assertFalse(setting.match("foo.bar"));
assertFalse(setting.match("foo.bar.baz.enabled"));
assertFalse(setting.match("foo"));
Setting<Boolean> concreteSetting = setting.getConcreteSetting("foo.bar.enable");
assertTrue(concreteSetting.get(Settings.builder().put("foo.bar.enable", "true").build()));
assertFalse(concreteSetting.get(Settings.builder().put("foo.baz.enable", "true").build()));
IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> setting.getConcreteSetting("foo"));
assertEquals("key [foo] must match [foo.*.enable] but didn't.", exc.getMessage());
exc = expectThrows(
IllegalArgumentException.class,
() -> Setting.affixKeySetting("foo", "enable", (key) -> Setting.boolSetting(key, false, Property.NodeScope))
);
assertEquals("prefix must end with a '.'", exc.getMessage());
Setting<List<String>> listAffixSetting = Setting.affixKeySetting(
"foo.",
"bar",
(key) -> Setting.listSetting(key, Collections.emptyList(), Function.identity(), Property.NodeScope)
);
assertTrue(listAffixSetting.hasComplexMatcher());
assertTrue(listAffixSetting.match("foo.test.bar"));
assertTrue(listAffixSetting.match("foo.test_1.bar"));
assertFalse(listAffixSetting.match("foo.buzz.baz.bar"));
assertFalse(listAffixSetting.match("foo.bar"));
assertFalse(listAffixSetting.match("foo.baz"));
assertFalse(listAffixSetting.match("foo"));
}
public void testAffixSettingNamespaces() {
Setting.AffixSetting<Boolean> setting = Setting.affixKeySetting(
"foo.",
"enable",
(key) -> Setting.boolSetting(key, false, Property.NodeScope)
);
Settings build = Settings.builder()
.put("foo.bar.enable", "true")
.put("foo.baz.enable", "true")
.put("foo.boom.enable", "true")
.put("something.else", "true")
.build();
Set<String> namespaces = setting.getNamespaces(build);
assertEquals(3, namespaces.size());
assertTrue(namespaces.contains("bar"));
assertTrue(namespaces.contains("baz"));
assertTrue(namespaces.contains("boom"));
}
public void testAffixAsMap() {
Setting.AffixSetting<String> setting = Setting.prefixKeySetting("foo.bar.", key -> Setting.simpleString(key, Property.NodeScope));
Settings build = Settings.builder().put("foo.bar.baz", 2).put("foo.bar.foobar", 3).build();
Map<String, String> asMap = setting.getAsMap(build);
assertEquals(2, asMap.size());
assertEquals("2", asMap.get("baz"));
assertEquals("3", asMap.get("foobar"));
setting = Setting.prefixKeySetting("foo.bar.", key -> Setting.simpleString(key, Property.NodeScope));
build = Settings.builder().put("foo.bar.baz", 2).put("foo.bar.foobar", 3).put("foo.bar.baz.deep", 45).build();
asMap = setting.getAsMap(build);
assertEquals(3, asMap.size());
assertEquals("2", asMap.get("baz"));
assertEquals("3", asMap.get("foobar"));
assertEquals("45", asMap.get("baz.deep"));
}
public void testGetAllConcreteSettings() {
Setting.AffixSetting<List<String>> listAffixSetting = Setting.affixKeySetting(