|
4 | 4 | */ |
5 | 5 | package org.opensearch.neuralsearch.query.ext; |
6 | 6 |
|
7 | | -import java.io.IOException; |
8 | | -import java.util.List; |
9 | | - |
10 | 7 | import org.opensearch.common.io.stream.BytesStreamOutput; |
| 8 | +import org.opensearch.common.xcontent.XContentType; |
11 | 9 | import org.opensearch.core.ParseField; |
| 10 | +import org.opensearch.core.common.bytes.BytesReference; |
12 | 11 | import org.opensearch.core.common.io.stream.NamedWriteableRegistry; |
13 | 12 | import org.opensearch.core.common.io.stream.StreamInput; |
| 13 | +import org.opensearch.core.xcontent.MediaType; |
14 | 14 | import org.opensearch.core.xcontent.NamedXContentRegistry; |
| 15 | +import org.opensearch.core.xcontent.ToXContentObject; |
| 16 | +import org.opensearch.core.xcontent.XContentBuilder; |
15 | 17 | import org.opensearch.core.xcontent.XContentParser; |
16 | 18 | import org.opensearch.search.SearchExtBuilder; |
17 | 19 | import org.opensearch.test.OpenSearchTestCase; |
18 | 20 |
|
| 21 | +import java.io.ByteArrayOutputStream; |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.List; |
| 24 | + |
19 | 25 | public class SemanticHighlighterExtBuilderTests extends OpenSearchTestCase { |
20 | 26 |
|
21 | 27 | @Override |
@@ -57,6 +63,14 @@ public void testRoundTripStreamingFalse() throws IOException { |
57 | 63 | roundTrip(false); |
58 | 64 | } |
59 | 65 |
|
| 66 | + public void testRoundTripXContentTrue() throws IOException { |
| 67 | + roundTripXContent(true); |
| 68 | + } |
| 69 | + |
| 70 | + public void testRoundTripXContentFalse() throws IOException { |
| 71 | + roundTripXContent(false); |
| 72 | + } |
| 73 | + |
60 | 74 | public void testParseBooleanTrue() throws IOException { |
61 | 75 | SemanticHighlighterExtBuilder result = parseValue("true"); |
62 | 76 | assertTrue(result.isEnabled()); |
@@ -97,6 +111,28 @@ private void roundTrip(boolean value) throws IOException { |
97 | 111 | } |
98 | 112 | } |
99 | 113 |
|
| 114 | + private void roundTripXContent(boolean value) throws IOException { |
| 115 | + SemanticHighlighterExtBuilder original = new SemanticHighlighterExtBuilder(value); |
| 116 | + MediaType xContentType = randomFrom(XContentType.values()); |
| 117 | + |
| 118 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 119 | + XContentBuilder builder = xContentType.contentBuilder(outputStream); |
| 120 | + builder.startObject(); |
| 121 | + original.toXContent(builder, ToXContentObject.EMPTY_PARAMS); |
| 122 | + builder.endObject(); |
| 123 | + BytesReference originalBytes = BytesReference.bytes(builder); |
| 124 | + |
| 125 | + try (XContentParser parser = this.createParser(xContentType.xContent(), originalBytes)) { |
| 126 | + assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); |
| 127 | + assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken()); |
| 128 | + assertEquals(SemanticHighlighterExtBuilder.NAME, parser.currentName()); |
| 129 | + parser.nextToken(); // advance to the value |
| 130 | + SearchExtBuilder deserialized = parser.namedObject(SearchExtBuilder.class, SemanticHighlighterExtBuilder.NAME, null); |
| 131 | + assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken()); |
| 132 | + assertEquals(original, deserialized); |
| 133 | + } |
| 134 | + } |
| 135 | + |
100 | 136 | private SemanticHighlighterExtBuilder parseValue(String json) throws IOException { |
101 | 137 | XContentParser parser = createParser(org.opensearch.common.xcontent.XContentType.JSON.xContent(), json); |
102 | 138 | // advance past START_TOKEN to the value |
|
0 commit comments