/* This file is part of VoltDB.
 * Copyright (C) 2008-2014 VoltDB Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

package org.voltdb;

import java.util.Random;

import junit.framework.TestCase;

import org.voltdb.VoltDB.Configuration;
import org.voltdb.client.Client;
import org.voltdb.client.ClientFactory;
import org.voltdb.client.ClientResponse;
import org.voltdb.compiler.VoltProjectBuilder;
import org.voltdb.utils.Encoder;
import org.voltdb.utils.MiscUtils;

public class TestVarBinaryPartition extends TestCase {

    private String pathToCatalog;
    private String pathToDeployment;
    private ServerThread localServer;
    private VoltDB.Configuration config;
    private VoltProjectBuilder builder;
    private Client client;

    @Override
    protected void setUp() throws Exception
    {
        super.setUp();
    }


    public void testPartitionAndInsert () throws Exception {
        String my_schema =
                "create table BLAH (" +
                "clm_varinary varbinary(128) default '00' not null," +
                "clm_smallint smallint default 0 not null, " +
                ");";

            pathToCatalog = Configuration.getPathToCatalogForTest("csv.jar");
            pathToDeployment = Configuration.getPathToCatalogForTest("csv.xml");
            builder = new VoltProjectBuilder();

            builder.addLiteralSchema(my_schema);
            builder.addPartitionInfo("BLAH", "clm_varinary");
            //builder.addStmtProcedure("Insert", "INSERT into BLAH values (?, ?, ?, ?, ?, ?, ?);");
            boolean success = builder.compile(pathToCatalog, 2, 1, 0);
            assertTrue(success);
            MiscUtils.copyFile(builder.getPathToDeployment(), pathToDeployment);
            config = new VoltDB.Configuration();
            config.m_pathToCatalog = pathToCatalog;
            config.m_pathToDeployment = pathToDeployment;
            localServer = new ServerThread(config);
            client = null;

            localServer.start();
            localServer.waitForInitialization();

            client = ClientFactory.createClient();
            client.createConnection("localhost");

            ClientResponse resp = client.callProcedure("@AdHoc", "INSERT INTO BLAH VALUES ('22',1);" );
            assert( resp.getResults().length == 1 );
            resp = client.callProcedure("@AdHoc", "INSERT INTO BLAH VALUES ('80',3);" );
            assert( resp.getResults().length == 1 );
            resp = client.callProcedure("@AdHoc", "INSERT INTO BLAH VALUES ('8081828384858687888990',4);" );
            assert( resp.getResults().length == 1 );

            Random rand = new Random();
            for( int i = 0; i < 1000; i++ ){
                byte[] b = new byte[rand.nextInt(128)];
                rand.nextBytes(b);
                String hexString = Encoder.hexEncode(b);
                resp = client.callProcedure("@AdHoc", "INSERT INTO BLAH VALUES ("+"'"+hexString+"'"+",5);" );
                assert( resp.getResults().length == 1 );
            }
    }
}

@@ -3186,16 +3186,34 @@
 esponse 
+resp;%0A            
 resp = c
@@ -3277,17 +3277,16 @@
 22',1);%22
- 
 );%0A     
@@ -3290,33 +3290,41 @@
           assert
-(
+Equals(1,
  resp.getResults
@@ -3321,37 +3321,42 @@
 getResults()
-.length == 1 
+%5B0%5D.asScalarLong()
 );%0A         
@@ -3445,33 +3445,41 @@
           assert
-(
+Equals(1,
  resp.getResults
@@ -3476,37 +3476,42 @@
 getResults()
-.length == 1 
+%5B0%5D.asScalarLong()
 );%0A         
@@ -3620,33 +3620,41 @@
           assert
-(
+Equals(1,
  resp.getResults
@@ -3655,29 +3655,34 @@
 esults()
-.length == 1 
+%5B0%5D.asScalarLong()
 );%0A%0A    
@@ -3786,16 +3786,20 @@
 byte%5B%5D b
+ytes
  = new b
@@ -3854,19 +3854,222 @@
 tBytes(b
+ytes
 );%0A
+                // Just to mix things up, alternate methods of INSERT among%0A                // literal hex string, hex string parameter, and byte%5B%5D parameter.%0A                if ( i %25 2 == 0 ) %7B%0A    
         
@@ -4114,19 +4114,71 @@
 Encode(b
+ytes
 );%0A
+                    if ( i %25 4 == 0 ) %7B%0A        
         
@@ -4253,35 +4253,317 @@
 ES (
-%22+%22'%22+hexString+%22'%22+%22,5);%22 
+'%22 + hexString + %22',5);%22 );%0A                    %7D else %7B%0A                        resp = client.callProcedure(%22@AdHoc%22, %22INSERT INTO BLAH VALUES (?,5);%22, hexString);%0A                    %7D%0A                %7D else %7B%0A                    resp = client.callProcedure(%22@AdHoc%22, %22INSERT INTO BLAH VALUES (?,5);%22, bytes
 );%0A 
@@ -4580,63 +4580,249 @@
     
+ 
- assert( resp.getResults().length == 1 );%0A            %7D
+%7D%0A                assertEquals(1, resp.getResults()%5B0%5D.asScalarLong());%0A            %7D%0A            resp =  client.callProcedure(%22@AdHoc%22, %22SELECT COUNT(*) FROM BLAH;%22);%0A            assertEquals(3 + 1000, resp.getResults()%5B0%5D.asScalarLong());
 %0A   

Test options for varbinary insert