|
21 | 21 | import java.nio.charset.StandardCharsets;
|
22 | 22 | import java.util.List;
|
23 | 23 | import java.util.stream.Collectors;
|
| 24 | +import java.util.stream.Stream; |
24 | 25 |
|
25 | 26 | import static org.mockito.Mockito.mock;
|
26 | 27 | import static org.mockito.Mockito.when;
|
@@ -246,6 +247,54 @@ public void testScalarObjectMismatchInNestedObject() throws IOException {
|
246 | 247 | assertEquals("{\"a\":{\"b\":{\"c\":\"10\",\"c.d\":\"20\"}}}", baos.toString(StandardCharsets.UTF_8));
|
247 | 248 | }
|
248 | 249 |
|
| 250 | + public void testSingleDotPath() throws IOException { |
| 251 | + // GIVEN |
| 252 | + final SortedSetDocValues dv = mock(SortedSetDocValues.class); |
| 253 | + final FlattenedFieldSyntheticWriterHelper writer = new FlattenedFieldSyntheticWriterHelper(new SortedSetSortedKeyedValues(dv)); |
| 254 | + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 255 | + final XContentBuilder builder = new XContentBuilder(XContentType.JSON.xContent(), baos); |
| 256 | + final List<byte[]> bytes = Stream.of("." + '\0' + "10").map(x -> x.getBytes(StandardCharsets.UTF_8)).toList(); |
| 257 | + when(dv.getValueCount()).thenReturn(Long.valueOf(bytes.size())); |
| 258 | + when(dv.docValueCount()).thenReturn(bytes.size()); |
| 259 | + for (int i = 0; i < bytes.size(); i++) { |
| 260 | + when(dv.nextOrd()).thenReturn((long) i); |
| 261 | + when(dv.lookupOrd(ArgumentMatchers.eq((long) i))).thenReturn(new BytesRef(bytes.get(i), 0, bytes.get(i).length)); |
| 262 | + } |
| 263 | + |
| 264 | + // WHEN |
| 265 | + builder.startObject(); |
| 266 | + writer.write(builder); |
| 267 | + builder.endObject(); |
| 268 | + builder.flush(); |
| 269 | + |
| 270 | + // THEN |
| 271 | + assertEquals("{\"\":{\"\":\"10\"}}", baos.toString(StandardCharsets.UTF_8)); |
| 272 | + } |
| 273 | + |
| 274 | + public void testTrailingDotsPath() throws IOException { |
| 275 | + // GIVEN |
| 276 | + final SortedSetDocValues dv = mock(SortedSetDocValues.class); |
| 277 | + final FlattenedFieldSyntheticWriterHelper writer = new FlattenedFieldSyntheticWriterHelper(new SortedSetSortedKeyedValues(dv)); |
| 278 | + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 279 | + final XContentBuilder builder = new XContentBuilder(XContentType.JSON.xContent(), baos); |
| 280 | + final List<byte[]> bytes = Stream.of("cat.." + '\0' + "10").map(x -> x.getBytes(StandardCharsets.UTF_8)).toList(); |
| 281 | + when(dv.getValueCount()).thenReturn(Long.valueOf(bytes.size())); |
| 282 | + when(dv.docValueCount()).thenReturn(bytes.size()); |
| 283 | + for (int i = 0; i < bytes.size(); i++) { |
| 284 | + when(dv.nextOrd()).thenReturn((long) i); |
| 285 | + when(dv.lookupOrd(ArgumentMatchers.eq((long) i))).thenReturn(new BytesRef(bytes.get(i), 0, bytes.get(i).length)); |
| 286 | + } |
| 287 | + |
| 288 | + // WHEN |
| 289 | + builder.startObject(); |
| 290 | + writer.write(builder); |
| 291 | + builder.endObject(); |
| 292 | + builder.flush(); |
| 293 | + |
| 294 | + // THEN |
| 295 | + assertEquals("{\"cat\":{\"\":{\"\":\"10\"}}}", baos.toString(StandardCharsets.UTF_8)); |
| 296 | + } |
| 297 | + |
249 | 298 | private class SortedSetSortedKeyedValues implements FlattenedFieldSyntheticWriterHelper.SortedKeyedValues {
|
250 | 299 | private final SortedSetDocValues dv;
|
251 | 300 | private int seen = 0;
|
|
0 commit comments