|
28 | 28 | import java.nio.charset.StandardCharsets;
|
29 | 29 | import java.util.HashMap;
|
30 | 30 | import java.util.Map;
|
| 31 | +import java.util.Set; |
31 | 32 | import java.util.function.Predicate;
|
32 | 33 |
|
33 | 34 | import static org.hamcrest.Matchers.anEmptyMap;
|
| 35 | +import static org.hamcrest.Matchers.contains; |
34 | 36 | import static org.hamcrest.Matchers.equalTo;
|
35 | 37 | import static org.hamcrest.Matchers.not;
|
36 | 38 | import static org.hamcrest.Matchers.sameInstance;
|
@@ -143,6 +145,41 @@ public void testGetVersion() {
|
143 | 145 | }
|
144 | 146 | }
|
145 | 147 |
|
| 148 | + @SuppressWarnings("unchecked") |
| 149 | + public void testMapKeyOrderingRoundTrip() throws IOException { |
| 150 | + // make up two random keys |
| 151 | + String key1 = randomAlphaOfLength(10); |
| 152 | + String key2 = randomValueOtherThan(key1, () -> randomAlphaOfLength(10)); |
| 153 | + // stick them as mappings onto themselves in the _meta of a pipeline configuration |
| 154 | + // this happens to use the _meta as a convenient map to test that the ordering of the key sets is the same |
| 155 | + String configJson = Strings.format(""" |
| 156 | + {"description": "blah", "_meta" : {"foo": "bar", "%s": "%s", "%s": "%s"}}""", key1, key1, key2, key2); |
| 157 | + PipelineConfiguration configuration = new PipelineConfiguration( |
| 158 | + "1", |
| 159 | + new BytesArray(configJson.getBytes(StandardCharsets.UTF_8)), |
| 160 | + XContentType.JSON |
| 161 | + ); |
| 162 | + |
| 163 | + // serialize it to bytes |
| 164 | + XContentType xContentType = randomFrom(XContentType.values()); |
| 165 | + final BytesReference bytes; |
| 166 | + try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) { |
| 167 | + configuration.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 168 | + bytes = BytesReference.bytes(builder); |
| 169 | + } |
| 170 | + |
| 171 | + // deserialize it back |
| 172 | + ContextParser<Void, PipelineConfiguration> parser = PipelineConfiguration.getParser(); |
| 173 | + XContentParser xContentParser = xContentType.xContent() |
| 174 | + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, bytes.streamInput()); |
| 175 | + PipelineConfiguration parsed = parser.parse(xContentParser, null); |
| 176 | + |
| 177 | + // make sure the _meta key sets are in the same order |
| 178 | + Set<String> keys1 = ((Map<String, Object>) configuration.getConfig().get("_meta")).keySet(); |
| 179 | + Set<String> keys2 = ((Map<String, Object>) parsed.getConfig().get("_meta")).keySet(); |
| 180 | + assertThat(keys1, contains(keys2.toArray(new String[0]))); |
| 181 | + } |
| 182 | + |
146 | 183 | @Override
|
147 | 184 | protected PipelineConfiguration createTestInstance() {
|
148 | 185 | BytesArray config;
|
|
0 commit comments