use of org.opensearch.common.xcontent.XContentType in project OpenSearch by opensearch-project.
the class SearchTemplateResponseTests method testSourceToXContent.
public void testSourceToXContent() throws IOException {
SearchTemplateResponse response = new SearchTemplateResponse();
XContentBuilder source = XContentFactory.jsonBuilder().startObject().startObject("query").startObject("terms").field("status", new String[] { "pending", "published" }).endObject().endObject().endObject();
response.setSource(BytesReference.bytes(source));
XContentType contentType = randomFrom(XContentType.values());
XContentBuilder expectedResponse = XContentFactory.contentBuilder(contentType).startObject().startObject("template_output").startObject("query").startObject("terms").field("status", new String[] { "pending", "published" }).endObject().endObject().endObject().endObject();
XContentBuilder actualResponse = XContentFactory.contentBuilder(contentType);
response.toXContent(actualResponse, ToXContent.EMPTY_PARAMS);
assertToXContentEquivalent(BytesReference.bytes(expectedResponse), BytesReference.bytes(actualResponse), contentType);
}
use of org.opensearch.common.xcontent.XContentType in project OpenSearch by opensearch-project.
the class SearchTemplateResponseTests method testSearchResponseToXContent.
public void testSearchResponseToXContent() throws IOException {
SearchHit hit = new SearchHit(1, "id", Collections.emptyMap(), Collections.emptyMap());
hit.score(2.0f);
SearchHit[] hits = new SearchHit[] { hit };
InternalSearchResponse internalSearchResponse = new InternalSearchResponse(new SearchHits(hits, new TotalHits(100, TotalHits.Relation.EQUAL_TO), 1.5f), null, null, null, false, null, 1);
SearchResponse searchResponse = new SearchResponse(internalSearchResponse, null, 0, 0, 0, 0, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY);
SearchTemplateResponse response = new SearchTemplateResponse();
response.setResponse(searchResponse);
XContentType contentType = randomFrom(XContentType.values());
XContentBuilder expectedResponse = XContentFactory.contentBuilder(contentType).startObject().field("took", 0).field("timed_out", false).startObject("_shards").field("total", 0).field("successful", 0).field("skipped", 0).field("failed", 0).endObject().startObject("hits").startObject("total").field("value", 100).field("relation", "eq").endObject().field("max_score", 1.5F).startArray("hits").startObject().field("_id", "id").field("_score", 2.0F).endObject().endArray().endObject().endObject();
XContentBuilder actualResponse = XContentFactory.contentBuilder(contentType);
response.toXContent(actualResponse, ToXContent.EMPTY_PARAMS);
assertToXContentEquivalent(BytesReference.bytes(expectedResponse), BytesReference.bytes(actualResponse), contentType);
}
use of org.opensearch.common.xcontent.XContentType in project OpenSearch by opensearch-project.
the class DiscountedCumulativeGainTests method testXContentParsingIsNotLenient.
public void testXContentParsingIsNotLenient() throws IOException {
DiscountedCumulativeGain testItem = createTestItem();
XContentType xContentType = randomFrom(XContentType.values());
BytesReference originalBytes = toShuffledXContent(testItem, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());
BytesReference withRandomFields = insertRandomFields(xContentType, originalBytes, null, random());
try (XContentParser parser = createParser(xContentType.xContent(), withRandomFields)) {
parser.nextToken();
parser.nextToken();
XContentParseException exception = expectThrows(XContentParseException.class, () -> DiscountedCumulativeGain.fromXContent(parser));
assertThat(exception.getMessage(), containsString("[dcg] unknown field"));
}
}
use of org.opensearch.common.xcontent.XContentType in project OpenSearch by opensearch-project.
the class ExpectedReciprocalRankTests method testXContentParsingIsNotLenient.
public void testXContentParsingIsNotLenient() throws IOException {
ExpectedReciprocalRank testItem = createTestItem();
XContentType xContentType = randomFrom(XContentType.values());
BytesReference originalBytes = toShuffledXContent(testItem, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());
BytesReference withRandomFields = insertRandomFields(xContentType, originalBytes, null, random());
try (XContentParser parser = createParser(xContentType.xContent(), withRandomFields)) {
parser.nextToken();
parser.nextToken();
XContentParseException exception = expectThrows(XContentParseException.class, () -> DiscountedCumulativeGain.fromXContent(parser));
assertThat(exception.getMessage(), containsString("[dcg] unknown field"));
}
}
use of org.opensearch.common.xcontent.XContentType in project OpenSearch by opensearch-project.
the class RatedRequestsTests method testXContentParsingIsNotLenient.
public void testXContentParsingIsNotLenient() throws IOException {
RatedRequest testItem = createTestItem(randomBoolean());
XContentType xContentType = randomFrom(XContentType.values());
BytesReference originalBytes = toShuffledXContent(testItem, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());
BytesReference withRandomFields = insertRandomFields(xContentType, originalBytes, null, random());
try (XContentParser parser = createParser(xContentType.xContent(), withRandomFields)) {
Throwable exception = expectThrows(XContentParseException.class, () -> RatedRequest.fromXContent(parser));
if (exception.getCause() != null) {
assertThat(exception.getMessage(), containsString("[request] failed to parse field"));
exception = exception.getCause();
}
assertThat(exception.getMessage(), containsString("unknown field"));
}
}
Aggregations