Search in sources :

Example 96 with XContentType

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);
}
Also used : XContentType(org.opensearch.common.xcontent.XContentType) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 97 with XContentType

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);
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) XContentType(org.opensearch.common.xcontent.XContentType) SearchHit(org.opensearch.search.SearchHit) SearchHits(org.opensearch.search.SearchHits) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 98 with XContentType

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"));
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) XContentType(org.opensearch.common.xcontent.XContentType) XContentParser(org.opensearch.common.xcontent.XContentParser) XContentParseException(org.opensearch.common.xcontent.XContentParseException)

Example 99 with XContentType

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"));
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) XContentType(org.opensearch.common.xcontent.XContentType) XContentParser(org.opensearch.common.xcontent.XContentParser) XContentParseException(org.opensearch.common.xcontent.XContentParseException)

Example 100 with XContentType

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"));
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) XContentType(org.opensearch.common.xcontent.XContentType) XContentParser(org.opensearch.common.xcontent.XContentParser)

Aggregations

XContentType (org.opensearch.common.xcontent.XContentType)155 BytesReference (org.opensearch.common.bytes.BytesReference)102 XContentParser (org.opensearch.common.xcontent.XContentParser)92 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)44 Map (java.util.Map)34 IOException (java.io.IOException)32 HashMap (java.util.HashMap)32 ToXContent (org.opensearch.common.xcontent.ToXContent)22 List (java.util.List)18 IndexRequest (org.opensearch.action.index.IndexRequest)17 ArrayList (java.util.ArrayList)15 Predicate (java.util.function.Predicate)14 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)14 XContentTestUtils.insertRandomFields (org.opensearch.test.XContentTestUtils.insertRandomFields)12 Matchers.containsString (org.hamcrest.Matchers.containsString)11 DocWriteRequest (org.opensearch.action.DocWriteRequest)11 XContentHelper.toXContent (org.opensearch.common.xcontent.XContentHelper.toXContent)11 Collections (java.util.Collections)10 DeleteRequest (org.opensearch.action.delete.DeleteRequest)10 OpenSearchAssertions.assertToXContentEquivalent (org.opensearch.test.hamcrest.OpenSearchAssertions.assertToXContentEquivalent)10