public void testMustRewrite() throws Exception {
    String templateString = "{ \"file\": \"storedTemplate\" ,\"params\":{\"template\":\"all\" } } ";

    XContentParser templateSourceParser =
        XContentFactory.xContent(templateString).createParser(templateString);
    context.reset();
    templateSourceParser.nextToken();
    try {
      TemplateQueryBuilder.fromXContent(context.newParseContext(templateSourceParser))
          .toQuery(context);
      fail();
    } catch (UnsupportedOperationException ex) {
      assertEquals("this query must be rewritten first", ex.getMessage());
    }
  }
  public void testParserCanExtractTemplateNames() throws Exception {
    String templateString = "{ \"file\": \"storedTemplate\" ,\"params\":{\"template\":\"all\" } } ";

    XContentParser templateSourceParser =
        XContentFactory.xContent(templateString).createParser(templateString);
    context.reset();
    templateSourceParser.nextToken();

    Query query =
        QueryBuilder.rewriteQuery(
                TemplateQueryBuilder.fromXContent(context.newParseContext(templateSourceParser)),
                context)
            .toQuery(context);
    assertTrue("Parsing template query failed.", query instanceof MatchAllDocsQuery);
  }
  public void testParser() throws IOException {
    String templateString =
        "{" + "\"query\":{\"match_{{template}}\": {}}," + "\"params\":{\"template\":\"all\"}" + "}";

    XContentParser templateSourceParser =
        XContentFactory.xContent(templateString).createParser(templateString);
    context.reset();
    templateSourceParser.nextToken();

    Query query =
        QueryBuilder.rewriteQuery(
                TemplateQueryBuilder.fromXContent(context.newParseContext(templateSourceParser)),
                context)
            .toQuery(context);
    assertTrue("Parsing template query failed.", query instanceof MatchAllDocsQuery);
  }
  public void testParseTemplateAsSingleStringWithConditionalClause() throws IOException {
    String templateString =
        "{"
            + "  \"inline\" : \"{ \\\"match_{{#use_it}}{{template}}{{/use_it}}\\\":{} }\","
            + "  \"params\":{"
            + "    \"template\":\"all\","
            + "    \"use_it\": true"
            + "  }"
            + "}";
    XContentParser templateSourceParser =
        XContentFactory.xContent(templateString).createParser(templateString);
    context.reset();

    Query query =
        QueryBuilder.rewriteQuery(
                TemplateQueryBuilder.fromXContent(context.newParseContext(templateSourceParser)),
                context)
            .toQuery(context);
    assertTrue("Parsing template query failed.", query instanceof MatchAllDocsQuery);
  }
  /**
   * Test that the template query parser can parse and evaluate template expressed as a single
   * string but still it expects only the query specification (thus this test should fail with
   * specific exception).
   */
  public void testParseTemplateFailsToParseCompleteQueryAsSingleString() throws IOException {
    String templateString =
        "{"
            + "  \"inline\" : \"{ \\\"size\\\": \\\"{{size}}\\\", \\\"query\\\":{\\\"match_all\\\":{}}}\","
            + "  \"params\":{"
            + "    \"size\":2"
            + "  }\n"
            + "}";

    XContentParser templateSourceParser =
        XContentFactory.xContent(templateString).createParser(templateString);
    context.reset();

    try {
      TemplateQueryBuilder.fromXContent(context.newParseContext(templateSourceParser))
          .rewrite(context);
      fail("Expected ParsingException");
    } catch (ParsingException e) {
      assertThat(e.getMessage(), containsString("query malformed, no field after start_object"));
    }
  }