|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.core.util; |
| 10 | + |
| 11 | +import org.opensearch.OpenSearchParseException; |
| 12 | +import org.opensearch.test.OpenSearchTestCase; |
| 13 | +import org.junit.Before; |
| 14 | + |
| 15 | +import java.util.Arrays; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +import static org.hamcrest.Matchers.equalTo; |
| 20 | + |
| 21 | +public class ConfigurationUtilsTests extends OpenSearchTestCase { |
| 22 | + private Map<String, Object> config; |
| 23 | + |
| 24 | + @Before |
| 25 | + public void setConfig() { |
| 26 | + config = new HashMap<>(); |
| 27 | + config.put("foo", "bar"); |
| 28 | + config.put("boolVal", true); |
| 29 | + config.put("null", null); |
| 30 | + config.put("arr", Arrays.asList("1", "2", "3")); |
| 31 | + config.put("ip", "127.0.0.1"); |
| 32 | + config.put("num", 1); |
| 33 | + config.put("double", 1.0); |
| 34 | + } |
| 35 | + |
| 36 | + public void testReadStringProperty() { |
| 37 | + String val = ConfigurationUtils.readStringProperty(config, "foo"); |
| 38 | + assertThat(val, equalTo("bar")); |
| 39 | + String val1 = ConfigurationUtils.readStringProperty(config, "foo1", "none"); |
| 40 | + assertThat(val1, equalTo("none")); |
| 41 | + try { |
| 42 | + ConfigurationUtils.readStringProperty(config, "foo1", null); |
| 43 | + } catch (OpenSearchParseException e) { |
| 44 | + assertThat(e.getMessage(), equalTo("[foo1] required property is missing")); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public void testOptionalReadStringProperty() { |
| 49 | + String val = ConfigurationUtils.readOptionalStringProperty(config, "foo"); |
| 50 | + assertThat(val, equalTo("bar")); |
| 51 | + String val1 = ConfigurationUtils.readOptionalStringProperty(config, "foo1"); |
| 52 | + assertThat(val, equalTo("bar")); |
| 53 | + assertThat(val1, equalTo(null)); |
| 54 | + } |
| 55 | + |
| 56 | + public void testReadStringPropertyInvalidType() { |
| 57 | + try { |
| 58 | + ConfigurationUtils.readStringProperty(config, "arr"); |
| 59 | + } catch (OpenSearchParseException e) { |
| 60 | + assertThat(e.getMessage(), equalTo("[arr] property isn't a string, but of type [java.util.Arrays$ArrayList]")); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public void testReadBooleanProperty() { |
| 65 | + Boolean val = ConfigurationUtils.readBooleanProperty(config, "boolVal", false); |
| 66 | + assertThat(val, equalTo(true)); |
| 67 | + } |
| 68 | + |
| 69 | + public void testReadNullBooleanProperty() { |
| 70 | + Boolean val = ConfigurationUtils.readBooleanProperty(config, "null", false); |
| 71 | + assertThat(val, equalTo(false)); |
| 72 | + } |
| 73 | + |
| 74 | + public void testReadBooleanPropertyInvalidType() { |
| 75 | + try { |
| 76 | + ConfigurationUtils.readBooleanProperty(config, "arr", true); |
| 77 | + } catch (OpenSearchParseException e) { |
| 78 | + assertThat(e.getMessage(), equalTo("[arr] property isn't a boolean, but of type [java.util.Arrays$ArrayList]")); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public void testReadStringOrIntProperty() { |
| 83 | + String val1 = ConfigurationUtils.readStringOrIntProperty(config, "foo", null); |
| 84 | + String val2 = ConfigurationUtils.readStringOrIntProperty(config, "num", null); |
| 85 | + assertThat(val1, equalTo("bar")); |
| 86 | + assertThat(val2, equalTo("1")); |
| 87 | + } |
| 88 | + |
| 89 | + public void testOptionalReadStringOrIntProperty() { |
| 90 | + String val1 = ConfigurationUtils.readOptionalStringOrIntProperty(config, "foo"); |
| 91 | + String val2 = ConfigurationUtils.readOptionalStringOrIntProperty(config, "num"); |
| 92 | + String val3 = ConfigurationUtils.readOptionalStringOrIntProperty(config, "num1"); |
| 93 | + assertThat(val1, equalTo("bar")); |
| 94 | + assertThat(val2, equalTo("1")); |
| 95 | + assertThat(val3, equalTo(null)); |
| 96 | + } |
| 97 | + |
| 98 | + public void testReadIntProperty() { |
| 99 | + int val = ConfigurationUtils.readIntProperty(config, "num", null); |
| 100 | + assertThat(val, equalTo(1)); |
| 101 | + try { |
| 102 | + ConfigurationUtils.readIntProperty(config, "foo", 2); |
| 103 | + } catch (OpenSearchParseException e) { |
| 104 | + assertThat(e.getMessage(), equalTo("[foo] property cannot be converted to an int [bar]")); |
| 105 | + } |
| 106 | + try { |
| 107 | + ConfigurationUtils.readIntProperty(config, "foo1", 2); |
| 108 | + } catch (OpenSearchParseException e) { |
| 109 | + assertThat(e.getMessage(), equalTo("required property is missing")); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + public void testReadDoubleProperty() { |
| 114 | + double val = ConfigurationUtils.readDoubleProperty(config, "double"); |
| 115 | + assertThat(val, equalTo(1.0)); |
| 116 | + try { |
| 117 | + ConfigurationUtils.readDoubleProperty(config, "foo"); |
| 118 | + } catch (OpenSearchParseException e) { |
| 119 | + assertThat(e.getMessage(), equalTo("[foo] property cannot be converted to a double [bar]")); |
| 120 | + } |
| 121 | + try { |
| 122 | + ConfigurationUtils.readDoubleProperty(config, "foo1"); |
| 123 | + } catch (OpenSearchParseException e) { |
| 124 | + assertThat(e.getMessage(), equalTo("[foo1] required property is missing")); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public void testReadStringOrIntPropertyInvalidType() { |
| 129 | + try { |
| 130 | + ConfigurationUtils.readStringOrIntProperty(config, "arr", null); |
| 131 | + } catch (OpenSearchParseException e) { |
| 132 | + assertThat(e.getMessage(), equalTo("[arr] property isn't a string or int, but of type [java.util.Arrays$ArrayList]")); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments