/*
 * Licensed to Crate.io GmbH ("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.primitives.Longs;
import io.crate.action.sql.SQLOperations;
import io.crate.testing.SQLTransportExecutor;
import org.elasticsearch.client.Client;
import org.elasticsearch.test.ESIntegTestCase;
import org.hamcrest.Matchers;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;

import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.is;

@ESIntegTestCase.ClusterScope(numDataNodes = 2, numClientNodes = 1)
public class BulkInsertOnClientNodeTest extends SQLIntegrationTestCase {

    public BulkInsertOnClientNodeTest() {
        super(new SQLTransportExecutor(
            new SQLTransportExecutor.ClientProvider() {

                private String nodeName;

                @Override
                public Client client() {
                    // make sure we use a client node (started with client=true)
                    Client client = internalCluster().client();
                    nodeName = client.settings().get("node.name");
                    return client;
                }

                @Override
                public String pgUrl() {
                    return null;
                }

                @Override
                public SQLOperations sqlOperations() {
                    return internalCluster().getInstance(SQLOperations.class, nodeName());
                }

                private String nodeName() {
                    if (nodeName == null) {
                        Client client= client();
                        nodeName = client.settings().get("name");
                    }
                    return nodeName;
                }
            }
        ));
    }

    @Test
    public void testInsertBulkDifferentTypesResultsInStreamingFailure() throws Exception {
        execute("create table test (id integer primary key) " +
                "clustered into 2 shards with (column_policy='dynamic', number_of_replicas=0)");
        List<Long> rowCounts = Longs.asList(
            execute("insert into test (id, value) values (?, ?)",
                    new Object[][]{
                        new Object[]{1, 1},                                 // use id 1 to ensure shard 0
                        new Object[]{3, new HashMap<String, Object>() {{    // use id 3 to ensure shard 1
                            put("foo", 127);
                        }}},
            }));
        assertThat(rowCounts.size(), is(2));
        assertThat(rowCounts, Matchers.anyOf(
            contains(1L, -2L),
            contains(-2L, 1L)
        ));
    }
}

@@ -1047,51 +1047,8 @@
 ;%0A%0A%0A
-import com.google.common.primitives.Longs;%0A
 impo
@@ -1268,24 +1268,49 @@
 unit.Test;%0A%0A
+import java.util.Arrays;%0A
 import java.
@@ -1345,16 +1345,52 @@
 il.List;
+%0Aimport java.util.stream.Collectors;
 %0A%0Aimport
@@ -3045,20 +3045,21 @@
 s = 
-Longs.asList
+Arrays.stream
 (%0A  
@@ -3130,36 +3130,32 @@
                 
-    
 new Object%5B%5D%5B%5D%7B%0A
@@ -3154,20 +3154,16 @@
 ct%5B%5D%5B%5D%7B%0A
-    
         
@@ -3268,36 +3268,32 @@
                 
-    
 new Object%5B%5D%7B3, 
@@ -3358,20 +3358,16 @@
 shard 1%0A
-    
         
@@ -3419,20 +3419,16 @@
         
-    
 %7D%7D%7D,%0A   
@@ -3436,17 +3436,58 @@
         
-%7D
+    %7D)).boxed().collect(Collectors.toList(
 ));%0A    

Remove Guava's primitives.Longs