package com.github.obourgain.elasticsearch.http.handler.admin.indices.delete;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Collections;
import org.assertj.core.api.Assertions;
import org.elasticsearch.action.admin.cluster.node.info.NodeInfo;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.test.ElasticsearchIntegrationTest;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.github.obourgain.elasticsearch.http.client.HttpClient;
import com.github.obourgain.elasticsearch.http.response.admin.indices.delete.DeleteIndexResponse;

@ElasticsearchIntegrationTest.ClusterScope(transportClientRatio = 1, numClientNodes = 1, numDataNodes = 1, scope = ElasticsearchIntegrationTest.Scope.SUITE)
public class DeleteIndexActionHandlerTest extends ElasticsearchIntegrationTest {

    private HttpClient httpClient;

    @Override
    protected Settings nodeSettings(int nodeOrdinal) {
        return ImmutableSettings.settingsBuilder()
                .put("action.destructive_requires_name", false)
                .put(super.nodeSettings(nodeOrdinal)).build();
    }

    @Before
    public void setUpClient() throws IOException, InterruptedException, NoSuchFieldException, IllegalAccessException {
        NodeInfo[] nodes = admin().cluster().nodesInfo(Requests.nodesInfoRequest()).actionGet().getNodes();
        Assert.assertThat(nodes.length, Matchers.greaterThanOrEqualTo(1));

        TransportAddress transportAddress = nodes[0].getHttp().getAddress().publishAddress();
        Assert.assertEquals(InetSocketTransportAddress.class, transportAddress.getClass());
        InetSocketTransportAddress inetSocketTransportAddress = (InetSocketTransportAddress) transportAddress;
        InetSocketAddress socketAddress = inetSocketTransportAddress.address();

        String url = String.format("http://%s:%d", socketAddress.getHostName(), socketAddress.getPort());
        httpClient = new HttpClient(Collections.singleton(url));
    }

    @Test
    public void should_delete_index() throws Exception {
        Assertions.assertThat(indexExists("the_index")).isTrue();

        DeleteIndexResponse response = httpClient.admin().indices().deleteIndex(Requests.deleteIndexRequest("the_index")).get();

        Assertions.assertThat(response.isAcknowledged()).isTrue();
        Assertions.assertThat(response.getError()).isNull();
        Assertions.assertThat(response.getStatus()).isEqualTo(200);
        Assertions.assertThat(indexExists("the_index")).isFalse();
    }

    @Test
    public void should_delete_all_indices_for_wildcard() throws Exception {
        createIndex("test1");
        createIndex("test2");

        DeleteIndexResponse response = httpClient.admin().indices().deleteIndex(Requests.deleteIndexRequest("_all")).get();

        Assertions.assertThat(response.isAcknowledged()).isTrue();
        Assertions.assertThat(response.getError()).isNull();
        Assertions.assertThat(response.getStatus()).isEqualTo(200);
        Assertions.assertThat(indexExists("test1")).isFalse();
        Assertions.assertThat(indexExists("test2")).isFalse();
    }

    @Test
    public void should_fail_on_missing_index() throws Exception {
        Assertions.assertThat(indexExists("the_index")).isTrue();

        DeleteIndexResponse response = httpClient.admin().indices().deleteIndex(Requests.deleteIndexRequest("doesnotexists")).get();

        Assertions.assertThat(response.isAcknowledged()).isFalse();
        Assertions.assertThat(response.getError()).contains("IndexMissingException");
        Assertions.assertThat(response.getError()).contains("doesnotexists");
        Assertions.assertThat(response.getStatus()).isEqualTo(404);
    }
}
@@ -629,98 +629,320 @@
 org.
-hamcrest.Matchers;%0Aimport org.junit.Assert;%0Aimport org.junit.Before;%0Aimport org.junit.Test
+elasticsearch.test.ElasticsearchThreadFilter;%0Aimport org.hamcrest.Matchers;%0Aimport org.junit.After;%0Aimport org.junit.Assert;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0Aimport com.carrotsearch.randomizedtesting.annotations.ThreadLeakFilters;%0Aimport com.github.obourgain.elasticsearch.http.RxNettyThreadFilter
 ;%0Aim
@@ -1104,16 +1104,130 @@
 ponse;%0A%0A
+@ThreadLeakFilters(defaultFilters = true, filters = %7BElasticsearchThreadFilter.class, RxNettyThreadFilter.class%7D)%0A
 @Elastic
@@ -1677,16 +1677,59 @@
  false)%0A
+                .put(%22http.enabled%22, true)%0A
         
@@ -2653,16 +2653,161 @@
 n(url));
+%0A%0A        createIndex(%22the_index%22);%0A        ensureSearchable(%22the_index%22);%0A    %7D%0A%0A    @After%0A    public void stop() %7B%0A        httpClient.close();
 %0A    %7D%0A%0A

fix tests