@Test
  public void testParseClearScrollRequestWithInvalidJsonThrowsException() throws Exception {
    ClearScrollRequest clearScrollRequest = new ClearScrollRequest();

    try {
      RestClearScrollAction.buildFromContent(new BytesArray("{invalid_json}"), clearScrollRequest);
      fail("expected parseContent failure");
    } catch (Exception e) {
      assertThat(e, instanceOf(IllegalArgumentException.class));
      assertThat(e.getMessage(), equalTo("Failed to parse request body"));
    }
  }
  @Test
  public void testParseClearScrollRequestWithUnknownParamThrowsException() throws Exception {
    BytesReference invalidContent =
        XContentFactory.jsonBuilder()
            .startObject()
            .array("scroll_id", "value_1", "value_2")
            .field("unknown", "keyword")
            .endObject()
            .bytes();
    ClearScrollRequest clearScrollRequest = new ClearScrollRequest();

    try {
      RestClearScrollAction.buildFromContent(invalidContent, clearScrollRequest);
      fail("expected parseContent failure");
    } catch (Exception e) {
      assertThat(e, instanceOf(IllegalArgumentException.class));
      assertThat(e.getMessage(), startsWith("Unknown parameter [unknown]"));
    }
  }
  @Test
  public void testStrictAliasParsingInIndicesCreatedViaTemplates() throws Exception {
    // Indexing into a should succeed, because the field mapping for field 'field' is defined in the
    // test mapping.
    client()
        .admin()
        .indices()
        .preparePutTemplate("template1")
        .setTemplate("a*")
        .setOrder(0)
        .addMapping("test", "field", "type=string")
        .addAlias(new Alias("alias1").filter(termFilter("field", "value")))
        .get();
    // Indexing into b should succeed, because the field mapping for field 'field' is defined in the
    // _default_ mapping and the test type exists.
    client()
        .admin()
        .indices()
        .preparePutTemplate("template2")
        .setTemplate("b*")
        .setOrder(0)
        .addMapping("_default_", "field", "type=string")
        .addMapping("test")
        .addAlias(new Alias("alias2").filter(termFilter("field", "value")))
        .get();
    // Indexing into c should succeed, because the field mapping for field 'field' is defined in the
    // _default_ mapping.
    client()
        .admin()
        .indices()
        .preparePutTemplate("template3")
        .setTemplate("c*")
        .setOrder(0)
        .addMapping("_default_", "field", "type=string")
        .addAlias(new Alias("alias3").filter(termFilter("field", "value")))
        .get();
    // Indexing into d index should fail, since there is field with name 'field' in the mapping
    client()
        .admin()
        .indices()
        .preparePutTemplate("template4")
        .setTemplate("d*")
        .setOrder(0)
        .addAlias(new Alias("alias4").filter(termFilter("field", "value")))
        .get();

    client().prepareIndex("a1", "test", "test").setSource("{}").get();
    BulkResponse response =
        client().prepareBulk().add(new IndexRequest("a2", "test", "test").source("{}")).get();
    assertThat(response.hasFailures(), is(false));
    assertThat(response.getItems()[0].isFailed(), equalTo(false));
    assertThat(response.getItems()[0].getIndex(), equalTo("a2"));
    assertThat(response.getItems()[0].getType(), equalTo("test"));
    assertThat(response.getItems()[0].getId(), equalTo("test"));
    assertThat(response.getItems()[0].getVersion(), equalTo(1l));

    client().prepareIndex("b1", "test", "test").setSource("{}").get();
    response =
        client().prepareBulk().add(new IndexRequest("b2", "test", "test").source("{}")).get();
    assertThat(response.hasFailures(), is(false));
    assertThat(response.getItems()[0].isFailed(), equalTo(false));
    assertThat(response.getItems()[0].getIndex(), equalTo("b2"));
    assertThat(response.getItems()[0].getType(), equalTo("test"));
    assertThat(response.getItems()[0].getId(), equalTo("test"));
    assertThat(response.getItems()[0].getVersion(), equalTo(1l));

    client().prepareIndex("c1", "test", "test").setSource("{}").get();
    response =
        client().prepareBulk().add(new IndexRequest("c2", "test", "test").source("{}")).get();
    assertThat(response.hasFailures(), is(false));
    assertThat(response.getItems()[0].isFailed(), equalTo(false));
    assertThat(response.getItems()[0].getIndex(), equalTo("c2"));
    assertThat(response.getItems()[0].getType(), equalTo("test"));
    assertThat(response.getItems()[0].getId(), equalTo("test"));
    assertThat(response.getItems()[0].getVersion(), equalTo(1l));

    try {
      client().prepareIndex("d1", "test", "test").setSource("{}").get();
      fail();
    } catch (Exception e) {
      assertThat(
          ExceptionsHelper.unwrapCause(e), instanceOf(ElasticsearchIllegalArgumentException.class));
      assertThat(e.getMessage(), containsString("failed to parse filter for alias [alias4]"));
    }
    response =
        client().prepareBulk().add(new IndexRequest("d2", "test", "test").source("{}")).get();
    assertThat(response.hasFailures(), is(true));
    assertThat(response.getItems()[0].isFailed(), equalTo(true));
    assertThat(
        response.getItems()[0].getFailureMessage(),
        containsString("failed to parse filter for alias [alias4]"));
  }