|
20 | 20 | package org.elasticsearch.action.support;
|
21 | 21 |
|
22 | 22 | import org.elasticsearch.Version;
|
| 23 | +import org.elasticsearch.action.support.IndicesOptions.Option; |
| 24 | +import org.elasticsearch.action.support.IndicesOptions.WildcardStates; |
| 25 | +import org.elasticsearch.common.bytes.BytesReference; |
23 | 26 | import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
24 | 27 | import org.elasticsearch.common.io.stream.StreamInput;
|
| 28 | +import org.elasticsearch.common.xcontent.NamedXContentRegistry; |
| 29 | +import org.elasticsearch.common.xcontent.ToXContent.MapParams; |
| 30 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 31 | +import org.elasticsearch.common.xcontent.XContentFactory; |
| 32 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 33 | +import org.elasticsearch.common.xcontent.XContentType; |
25 | 34 | import org.elasticsearch.test.ESTestCase;
|
26 | 35 | import org.elasticsearch.test.EqualsHashCodeTestUtils;
|
27 | 36 |
|
| 37 | +import java.io.IOException; |
| 38 | +import java.util.Arrays; |
| 39 | +import java.util.Collection; |
| 40 | +import java.util.Collections; |
| 41 | +import java.util.EnumSet; |
28 | 42 | import java.util.HashMap;
|
| 43 | +import java.util.List; |
29 | 44 | import java.util.Map;
|
30 | 45 |
|
31 | 46 | import static org.elasticsearch.test.VersionUtils.randomVersionBetween;
|
@@ -242,4 +257,71 @@ public void testEqualityAndHashCode() {
|
242 | 257 | allowAliasesToMulti, forbidClosed, ignoreAliases);
|
243 | 258 | });
|
244 | 259 | }
|
| 260 | + |
| 261 | + public void testFromMap() { |
| 262 | + IndicesOptions defaults = IndicesOptions.strictExpandOpen(); |
| 263 | + Collection<String> wildcardStates = randomBoolean() ? |
| 264 | + null : randomSubsetOf(Arrays.asList("open", "closed")); |
| 265 | + Boolean ignoreUnavailable = randomBoolean() ? null : randomBoolean(); |
| 266 | + Boolean allowNoIndices = randomBoolean() ? null : randomBoolean(); |
| 267 | + |
| 268 | + Map<String, Object> settings = new HashMap<>(); |
| 269 | + |
| 270 | + if (wildcardStates != null) { |
| 271 | + settings.put("expand_wildcards", wildcardStates); |
| 272 | + } |
| 273 | + |
| 274 | + if (ignoreUnavailable != null) { |
| 275 | + settings.put("ignore_unavailable", ignoreUnavailable); |
| 276 | + } |
| 277 | + |
| 278 | + if (allowNoIndices != null) { |
| 279 | + settings.put("allow_no_indices", allowNoIndices); |
| 280 | + } |
| 281 | + |
| 282 | + IndicesOptions fromMap = IndicesOptions.fromMap(settings, defaults); |
| 283 | + |
| 284 | + boolean open = wildcardStates != null ? wildcardStates.contains("open") : defaults.expandWildcardsOpen(); |
| 285 | + assertEquals(fromMap.expandWildcardsOpen(), open); |
| 286 | + boolean closed = wildcardStates != null ? wildcardStates.contains("closed") : defaults.expandWildcardsClosed(); |
| 287 | + assertEquals(fromMap.expandWildcardsClosed(), closed); |
| 288 | + |
| 289 | + assertEquals(fromMap.ignoreUnavailable(), ignoreUnavailable == null ? defaults.ignoreUnavailable() : ignoreUnavailable); |
| 290 | + assertEquals(fromMap.allowNoIndices(), allowNoIndices == null ? defaults.allowNoIndices() : allowNoIndices); |
| 291 | + } |
| 292 | + |
| 293 | + public void testToXContent() throws IOException { |
| 294 | + Collection<WildcardStates> wildcardStates = randomSubsetOf(Arrays.asList(WildcardStates.values())); |
| 295 | + Collection<Option> options = randomSubsetOf(Arrays.asList(Option.values())); |
| 296 | + |
| 297 | + IndicesOptions indicesOptions = new IndicesOptions( |
| 298 | + options.isEmpty() ? Option.NONE : EnumSet.copyOf(options), |
| 299 | + wildcardStates.isEmpty() ? WildcardStates.NONE : EnumSet.copyOf(wildcardStates)); |
| 300 | + |
| 301 | + XContentBuilder builder = XContentFactory.jsonBuilder(); |
| 302 | + builder.startObject(); |
| 303 | + indicesOptions.toXContent(builder, new MapParams(Collections.emptyMap())); |
| 304 | + builder.endObject(); |
| 305 | + XContentParser parser = XContentType.JSON.xContent().createParser( |
| 306 | + NamedXContentRegistry.EMPTY, null, BytesReference.bytes(builder).streamInput()); |
| 307 | + Map<String, Object> map = parser.mapOrdered(); |
| 308 | + |
| 309 | + boolean open = wildcardStates.contains(WildcardStates.OPEN); |
| 310 | + if (open) { |
| 311 | + assertTrue(((List)map.get("expand_wildcards")).contains("open")); |
| 312 | + } else { |
| 313 | + assertFalse(((List)map.get("expand_wildcards")).contains("open")); |
| 314 | + } |
| 315 | + boolean closed = wildcardStates.contains(WildcardStates.CLOSED); |
| 316 | + if (closed) { |
| 317 | + assertTrue(((List)map.get("expand_wildcards")).contains("closed")); |
| 318 | + } else { |
| 319 | + assertFalse(((List)map.get("expand_wildcards")).contains("closed")); |
| 320 | + } |
| 321 | + assertEquals(map.get("ignore_unavailable"), options.contains(Option.IGNORE_UNAVAILABLE)); |
| 322 | + assertEquals(map.get("allow_no_indices"), options.contains(Option.ALLOW_NO_INDICES)); |
| 323 | + assertEquals(map.get("forbid_aliases_to_multiple_indices"), options.contains(Option.FORBID_ALIASES_TO_MULTIPLE_INDICES)); |
| 324 | + assertEquals(map.get("forbid_closed_indices"), options.contains(Option.FORBID_CLOSED_INDICES)); |
| 325 | + assertEquals(map.get("ignore_aliases"), options.contains(Option.IGNORE_ALIASES)); |
| 326 | + } |
245 | 327 | }
|
0 commit comments