|
19 | 19 |
|
20 | 20 | package org.elasticsearch.action.admin.indices.analyze;
|
21 | 21 |
|
| 22 | +import org.elasticsearch.common.bytes.BytesReference; |
| 23 | +import org.elasticsearch.common.xcontent.ToXContent; |
| 24 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 25 | +import org.elasticsearch.common.xcontent.XContentHelper; |
22 | 26 | import org.elasticsearch.common.xcontent.XContentParser;
|
| 27 | +import org.elasticsearch.common.xcontent.json.JsonXContent; |
23 | 28 | import org.elasticsearch.test.AbstractStreamableXContentTestCase;
|
24 | 29 |
|
25 | 30 | import java.io.IOException;
|
|
30 | 35 | import java.util.Map;
|
31 | 36 | import java.util.function.Predicate;
|
32 | 37 |
|
| 38 | +import static org.hamcrest.Matchers.equalTo; |
| 39 | + |
33 | 40 | public class AnalyzeResponseTests extends AbstractStreamableXContentTestCase<AnalyzeResponse> {
|
34 | 41 |
|
35 | 42 | @Override
|
@@ -112,4 +119,31 @@ private AnalyzeResponse.AnalyzeToken randomToken() {
|
112 | 119 | }
|
113 | 120 | return new AnalyzeResponse.AnalyzeToken(token, position, startOffset, endOffset, posLength, type, extras);
|
114 | 121 | }
|
| 122 | + |
| 123 | + public void testNullResponseToXContent() throws IOException { |
| 124 | + DetailAnalyzeResponse.CharFilteredText[] charfilters = null; |
| 125 | + |
| 126 | + String name = "test_tokens_null"; |
| 127 | + AnalyzeResponse.AnalyzeToken[] tokens = null; |
| 128 | + DetailAnalyzeResponse.AnalyzeTokenList tokenizer = null; |
| 129 | + |
| 130 | + |
| 131 | + DetailAnalyzeResponse.AnalyzeTokenList tokenfiltersItem = new DetailAnalyzeResponse.AnalyzeTokenList(name, tokens); |
| 132 | + DetailAnalyzeResponse.AnalyzeTokenList[] tokenfilters = {tokenfiltersItem}; |
| 133 | + |
| 134 | + DetailAnalyzeResponse detail = new DetailAnalyzeResponse(charfilters, tokenizer, tokenfilters); |
| 135 | + |
| 136 | + AnalyzeResponse response = new AnalyzeResponse(null, detail); |
| 137 | + try (XContentBuilder builder = JsonXContent.contentBuilder()) { |
| 138 | + response.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 139 | + Map<String, Object> converted = XContentHelper.convertToMap(BytesReference.bytes(builder), false, builder.contentType()).v2(); |
| 140 | + List<Map<String, Object>> tokenfiltersValue = (List<Map<String, Object>>) ((Map<String, Object>) |
| 141 | + converted.get("detail")).get("tokenfilters"); |
| 142 | + List<Map<String, Object>> nullTokens = (List<Map<String, Object>>) tokenfiltersValue.get(0).get("tokens"); |
| 143 | + String nameValue = (String) tokenfiltersValue.get(0).get("name"); |
| 144 | + assertThat(nullTokens.size(), equalTo(0)); |
| 145 | + assertThat(name, equalTo(nameValue)); |
| 146 | + } |
| 147 | + |
| 148 | + } |
115 | 149 | }
|
0 commit comments