|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.xcontent.provider.json; |
| 11 | + |
| 12 | +import com.fasterxml.jackson.core.JsonFactory; |
| 13 | +import com.fasterxml.jackson.core.JsonParser; |
| 14 | +import com.fasterxml.jackson.core.JsonToken; |
| 15 | + |
| 16 | +import org.elasticsearch.core.CheckedConsumer; |
| 17 | +import org.elasticsearch.test.ESTestCase; |
| 18 | +import org.elasticsearch.xcontent.XBytesRef; |
| 19 | +import org.hamcrest.Matchers; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.nio.charset.StandardCharsets; |
| 23 | +import java.util.Arrays; |
| 24 | + |
| 25 | +public class ESUTF8StreamJsonParserTests extends ESTestCase { |
| 26 | + |
| 27 | + private void testParseJson(String input, CheckedConsumer<ESUTF8StreamJsonParser, IOException> test) throws IOException { |
| 28 | + JsonFactory factory = new ESJsonFactoryBuilder().build(); |
| 29 | + assertThat(factory, Matchers.instanceOf(ESJsonFactory.class)); |
| 30 | + |
| 31 | + JsonParser parser = factory.createParser(StandardCharsets.UTF_8.encode(input).array()); |
| 32 | + assertThat(parser, Matchers.instanceOf(ESUTF8StreamJsonParser.class)); |
| 33 | + test.accept((ESUTF8StreamJsonParser) parser); |
| 34 | + } |
| 35 | + |
| 36 | + private void assertTextRef(XBytesRef textRef, String expectedValue) { |
| 37 | + var data = Arrays.copyOfRange(textRef.bytes(), textRef.start(), textRef.end()); |
| 38 | + assertThat(data, Matchers.equalTo(StandardCharsets.UTF_8.encode(expectedValue).array())); |
| 39 | + } |
| 40 | + |
| 41 | + public void testGetValueAsByteRef() throws IOException { |
| 42 | + testParseJson("{\"foo\": \"bar\"}", parser -> { |
| 43 | + assertThat(parser.nextToken(), Matchers.equalTo(JsonToken.START_OBJECT)); |
| 44 | + assertThat(parser.nextFieldName(), Matchers.equalTo("foo")); |
| 45 | + assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING)); |
| 46 | + |
| 47 | + XBytesRef textRef = parser.getValueAsByteRef(); |
| 48 | + assertThat(textRef, Matchers.notNullValue()); |
| 49 | + assertThat(textRef.start(), Matchers.equalTo(9)); |
| 50 | + assertThat(textRef.end(), Matchers.equalTo(12)); |
| 51 | + assertTextRef(textRef, "bar"); |
| 52 | + |
| 53 | + assertThat(parser.getValueAsString(), Matchers.equalTo("bar")); |
| 54 | + assertThat(parser.getValueAsByteRef(), Matchers.nullValue()); |
| 55 | + |
| 56 | + assertThat(parser.nextToken(), Matchers.equalTo(JsonToken.END_OBJECT)); |
| 57 | + }); |
| 58 | + |
| 59 | + testParseJson("{\"foo\": \"bar\\\"baz\\\"\"}", parser -> { |
| 60 | + assertThat(parser.nextToken(), Matchers.equalTo(JsonToken.START_OBJECT)); |
| 61 | + assertThat(parser.nextFieldName(), Matchers.equalTo("foo")); |
| 62 | + assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING)); |
| 63 | + |
| 64 | + assertThat(parser.getValueAsByteRef(), Matchers.nullValue()); |
| 65 | + assertThat(parser.getValueAsString(), Matchers.equalTo("bar\"baz\"")); |
| 66 | + }); |
| 67 | + |
| 68 | + testParseJson("{\"foo\": \"bår\"}", parser -> { |
| 69 | + assertThat(parser.nextToken(), Matchers.equalTo(JsonToken.START_OBJECT)); |
| 70 | + assertThat(parser.nextFieldName(), Matchers.equalTo("foo")); |
| 71 | + assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING)); |
| 72 | + |
| 73 | + assertThat(parser.getValueAsByteRef(), Matchers.nullValue()); |
| 74 | + assertThat(parser.getValueAsString(), Matchers.equalTo("bår")); |
| 75 | + }); |
| 76 | + |
| 77 | + testParseJson("{\"foo\": [\"lorem\", \"ipsum\", \"dolor\"]}", parser -> { |
| 78 | + assertThat(parser.nextToken(), Matchers.equalTo(JsonToken.START_OBJECT)); |
| 79 | + assertThat(parser.nextFieldName(), Matchers.equalTo("foo")); |
| 80 | + assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.START_ARRAY)); |
| 81 | + |
| 82 | + assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING)); |
| 83 | + { |
| 84 | + XBytesRef textRef = parser.getValueAsByteRef(); |
| 85 | + assertThat(textRef, Matchers.notNullValue()); |
| 86 | + assertThat(textRef.start(), Matchers.equalTo(10)); |
| 87 | + assertThat(textRef.end(), Matchers.equalTo(15)); |
| 88 | + assertTextRef(textRef, "lorem"); |
| 89 | + } |
| 90 | + |
| 91 | + assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING)); |
| 92 | + { |
| 93 | + XBytesRef textRef = parser.getValueAsByteRef(); |
| 94 | + assertThat(textRef, Matchers.notNullValue()); |
| 95 | + assertThat(textRef.start(), Matchers.equalTo(19)); |
| 96 | + assertThat(textRef.end(), Matchers.equalTo(24)); |
| 97 | + assertTextRef(textRef, "ipsum"); |
| 98 | + } |
| 99 | + |
| 100 | + assertThat(parser.nextValue(), Matchers.equalTo(JsonToken.VALUE_STRING)); |
| 101 | + { |
| 102 | + XBytesRef textRef = parser.getValueAsByteRef(); |
| 103 | + assertThat(textRef, Matchers.notNullValue()); |
| 104 | + assertThat(textRef.start(), Matchers.equalTo(28)); |
| 105 | + assertThat(textRef.end(), Matchers.equalTo(33)); |
| 106 | + assertTextRef(textRef, "dolor"); |
| 107 | + } |
| 108 | + |
| 109 | + assertThat(parser.nextToken(), Matchers.equalTo(JsonToken.END_ARRAY)); |
| 110 | + assertThat(parser.nextToken(), Matchers.equalTo(JsonToken.END_OBJECT)); |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments