public void testFromXContent() throws IOException {
    SearchSortValues sortValues = createTestItem();
    XContentType xcontentType = randomFrom(XContentType.values());
    XContentBuilder builder = XContentFactory.contentBuilder(xcontentType);
    if (randomBoolean()) {
      builder.prettyPrint();
    }
    builder
        .startObject(); // we need to wrap xContent output in proper object to create a parser for
    // it
    builder = sortValues.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.endObject();

    XContentParser parser = createParser(builder);
    parser
        .nextToken(); // skip to the elements field name token, fromXContent advances from there if
    // called from ourside
    parser.nextToken();
    if (sortValues.sortValues().length > 0) {
      SearchSortValues parsed = SearchSortValues.fromXContent(parser);
      assertToXContentEquivalent(
          builder.bytes(), toXContent(parsed, xcontentType, true), xcontentType);
      parser.nextToken();
    }
    assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
    assertNull(parser.nextToken());
  }
 // https://github.com/elasticsearch/elasticsearch/issues/7240
 public void testEmptyBooleanQuery() throws Exception {
   XContentBuilder contentBuilder =
       XContentFactory.contentBuilder(randomFrom(XContentType.values()));
   BytesReference query =
       contentBuilder.startObject().startObject("bool").endObject().endObject().bytes();
   Query parsedQuery = parseQuery(query).toQuery(createShardContext());
   assertThat(parsedQuery, Matchers.instanceOf(MatchAllDocsQuery.class));
 }
示例#3
0
 /**
  * Returns a random source containing a random number of fields, objects and array, with maximum
  * depth 5.
  *
  * @param random Random generator
  */
 public static BytesReference randomSource(Random random) {
   // the source can be stored in any format and eventually converted when retrieved depending on
   // the format of the response
   XContentType xContentType = RandomPicks.randomFrom(random, XContentType.values());
   try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType)) {
     builder.startObject();
     addFields(random, builder, 0);
     builder.endObject();
     return builder.bytes();
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
 public void testFromXContent() throws Exception {
   SliceBuilder sliceBuilder = randomSliceBuilder();
   XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
   if (randomBoolean()) {
     builder.prettyPrint();
   }
   builder.startObject();
   sliceBuilder.innerToXContent(builder);
   builder.endObject();
   XContentParser parser = XContentHelper.createParser(shuffleXContent(builder).bytes());
   QueryParseContext context =
       new QueryParseContext(indicesQueriesRegistry, parser, ParseFieldMatcher.STRICT);
   SliceBuilder secondSliceBuilder = SliceBuilder.fromXContent(context);
   assertNotSame(sliceBuilder, secondSliceBuilder);
   assertEquals(sliceBuilder, secondSliceBuilder);
   assertEquals(sliceBuilder.hashCode(), secondSliceBuilder.hashCode());
 }
 /**
  * Test that creates new smoothing model from a random test smoothing model and checks both for
  * equality
  */
 public void testFromXContent() throws IOException {
   SmoothingModel testModel = createTestModel();
   XContentBuilder contentBuilder =
       XContentFactory.contentBuilder(randomFrom(XContentType.values()));
   if (randomBoolean()) {
     contentBuilder.prettyPrint();
   }
   contentBuilder.startObject();
   testModel.innerToXContent(contentBuilder, ToXContent.EMPTY_PARAMS);
   contentBuilder.endObject();
   XContentParser parser = XContentHelper.createParser(shuffleXContent(contentBuilder).bytes());
   QueryParseContext context =
       new QueryParseContext(new IndicesQueriesRegistry(), parser, ParseFieldMatcher.STRICT);
   parser.nextToken(); // go to start token, real parsing would do that in the outer element parser
   SmoothingModel parsedModel = fromXContent(context);
   assertNotSame(testModel, parsedModel);
   assertEquals(testModel, parsedModel);
   assertEquals(testModel.hashCode(), parsedModel.hashCode());
 }
  /**
   * creates random suggestion builder, renders it to xContent and back to new instance that should
   * be equal to original
   */
  public void testFromXContent() throws IOException {
    Suggesters suggesters = new Suggesters(Collections.emptyMap());
    QueryParseContext context = new QueryParseContext(null);
    context.parseFieldMatcher(new ParseFieldMatcher(Settings.EMPTY));
    for (int runs = 0; runs < NUMBER_OF_RUNS; runs++) {
      SuggestBuilder suggestBuilder = createTestModel();
      XContentBuilder xContentBuilder =
          XContentFactory.contentBuilder(randomFrom(XContentType.values()));
      if (randomBoolean()) {
        xContentBuilder.prettyPrint();
      }
      suggestBuilder.toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS);
      XContentParser parser = XContentHelper.createParser(xContentBuilder.bytes());
      context.reset(parser);

      SuggestBuilder secondSuggestBuilder = SuggestBuilder.fromXContent(context, suggesters);
      assertNotSame(suggestBuilder, secondSuggestBuilder);
      assertEquals(suggestBuilder, secondSuggestBuilder);
      assertEquals(suggestBuilder.hashCode(), secondSuggestBuilder.hashCode());
    }
  }
 /**
  * Generic test that creates new AggregatorFactory from the test AggregatorFactory and checks both
  * for equality and asserts equality on the two queries.
  */
 public void testFromXContent() throws IOException {
   AF testAgg = createTestAggregatorFactory();
   AggregatorFactories.Builder factoriesBuilder =
       AggregatorFactories.builder().skipResolveOrder().addPipelineAggregator(testAgg);
   logger.info("Content string: {}", factoriesBuilder);
   XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
   if (randomBoolean()) {
     builder.prettyPrint();
   }
   factoriesBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
   XContentBuilder shuffled = shuffleXContent(builder);
   XContentParser parser =
       XContentFactory.xContent(shuffled.bytes()).createParser(shuffled.bytes());
   QueryParseContext parseContext =
       new QueryParseContext(queriesRegistry, parser, parseFieldMatcher);
   String contentString = factoriesBuilder.toString();
   logger.info("Content string: {}", contentString);
   assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
   assertSame(XContentParser.Token.FIELD_NAME, parser.nextToken());
   assertEquals(testAgg.getName(), parser.currentName());
   assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
   assertSame(XContentParser.Token.FIELD_NAME, parser.nextToken());
   assertEquals(testAgg.type(), parser.currentName());
   assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
   PipelineAggregationBuilder newAgg =
       aggParsers
           .pipelineParser(testAgg.getWriteableName(), ParseFieldMatcher.STRICT)
           .parse(testAgg.getName(), parseContext);
   assertSame(XContentParser.Token.END_OBJECT, parser.currentToken());
   assertSame(XContentParser.Token.END_OBJECT, parser.nextToken());
   assertSame(XContentParser.Token.END_OBJECT, parser.nextToken());
   assertNull(parser.nextToken());
   assertNotNull(newAgg);
   assertNotSame(newAgg, testAgg);
   assertEquals(testAgg, newAgg);
   assertEquals(testAgg.hashCode(), newAgg.hashCode());
 }