/*
 * Licensed to Crate under one or more contributor license agreements.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.  Crate licenses this file
 * to you under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.  You may
 * obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * However, if you have executed another commercial license agreement
 * with Crate these terms will supersede the license and you may use the
 * software solely pursuant to the terms of the relevant commercial
 * agreement.
 */

package io.crate.integrationtests;

import com.google.common.collect.Sets;
import io.crate.action.sql.SQLAction;
import io.crate.action.sql.SQLRequest;
import io.crate.testing.SQLTransportExecutor;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.Strings;
import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.test.disruption.NetworkPartition;
import org.elasticsearch.test.disruption.NetworkUnresponsivePartition;
import org.junit.Test;

import java.util.*;

import static org.hamcrest.Matchers.*;

@ESIntegTestCase.ClusterScope(minNumDataNodes = 2)
public class SysNodeResiliencyIntegrationTest extends SQLTransportIntegrationTest {

    /**
     * Test that basic information from cluster state is used if a sys node
     * request is timing out
     */
    @Test
    public void testTimingOutNode() throws Exception {
        final List<String> nodes = Arrays.asList(internalCluster().getNodeNames());
        final String unluckyNode = randomFrom(nodes);
        final Set<String> luckyNodes = new HashSet<>(nodes);
        luckyNodes.remove(unluckyNode);

        NetworkPartition partition
            = new NetworkUnresponsivePartition(luckyNodes, Sets.newHashSet(unluckyNode), getRandom());
        setDisruptionScheme(partition);
        partition.startDisrupting();

        SQLRequest request = new SQLRequest("select version, hostname, id, name from sys.nodes where name = ?", new Object[]{unluckyNode});
        Client client = internalCluster().client(randomFrom(luckyNodes.toArray(Strings.EMPTY_ARRAY)));
        assert client != null;
        response = client.execute(SQLAction.INSTANCE, request).actionGet(SQLTransportExecutor.REQUEST_TIMEOUT);

        assertThat(response.rowCount(), is(1L));
        assertThat(response.rows()[0][0], is(nullValue()));
        assertThat(response.rows()[0][1], is(nullValue()));
        assertThat(response.rows()[0][2], is(notNullValue()));
        assertThat((String)response.rows()[0][3], is(unluckyNode));
    }
}

@@ -1271,16 +1271,57 @@
 trings;%0A
+import org.elasticsearch.plugins.Plugin;%0A
 import o
@@ -1359,16 +1359,16 @@
 stCase;%0A
-
 import o
@@ -1481,32 +1481,94 @@
 nsivePartition;%0A
+import org.elasticsearch.test.transport.MockTransportService;%0A
 import org.junit
@@ -1685,16 +1685,42 @@
 odes = 2
+, transportClientRatio = 0
 )%0Apublic
@@ -1798,16 +1798,291 @@
 Test %7B%0A%0A
+    @Override%0A    protected Collection%3CClass%3C? extends Plugin%3E%3E nodePlugins() %7B%0A        ArrayList%3CClass%3C? extends Plugin%3E%3E nodePlugins = new ArrayList%3C%3E(super.nodePlugins());%0A        nodePlugins.add(MockTransportService.TestPlugin.class);%0A        return nodePlugins;%0A    %7D%0A%0A%0A
     /**%0A

Fix flaky testTimingOutNode