/* This file is part of VoltDB.
 * Copyright (C) 2008-2013 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.groovy;

import org.voltdb.BackendTarget;
import org.voltdb.LegacyHashinator;
import org.voltdb.TheHashinator;
import org.voltdb.VoltTable;
import org.voltdb.client.Client;
import org.voltdb.client.ClientResponse;
import org.voltdb.compiler.VoltProjectBuilder;
import org.voltdb.regressionsuites.LocalCluster;
import org.voltdb.regressionsuites.MultiConfigSuiteBuilder;
import org.voltdb.regressionsuites.RegressionSuite;

public class TestGroovyDeployment extends RegressionSuite {

    static final String SCHEMA = "CREATE TABLE MAMMA_MIA (\n" +
            "  MAMMA INTEGER NOT NULL,\n" +
            "  MIA VARCHAR(32) NOT NULL,\n" +
            "  CONSTRAINT PK_MAMMA_MIA PRIMARY KEY(MAMMA)\n" +
            ")\n" +
            ";\n" +
            "PARTITION TABLE MAMMA_MIA ON COLUMN MAMMA\n" +
            ";\n" +
            "\n" +
            "CREATE PROCEDURE voltdb.groovy.example.AddMamma AS ###\n" +
            "  addMamma = new SQLStmt('INSERT INTO MAMMA_MIA (MAMMA,MIA) VALUES (?,?);')\n" +
            "  transactOn = { int mammaId, String miaName ->\n" +
            "    voltQueueSQL(addMamma, mammaId, miaName)\n" +
            "    voltExecuteSQL(true)\n" +
            "  }\n" +
            "### LANGUAGE GROOVY;\n" +
            "PARTITION PROCEDURE AddMamma ON TABLE MAMMA_MIA COLUMN MAMMA\n" +
            ";\n" +
            "\n" +
            "CREATE PROCEDURE voltdb.groovy.example.GetMamma AS ###\n" +
            "  getMamma = new SQLStmt('SELECT MAMMA,MIA FROM MAMMA_MIA WHERE MAMMA = ?')\n" +
            "  transactOn = { int mammaId ->\n" +
            "    voltQueueSQL(getMamma,mammaId)\n" +
            "    voltExecuteSQL(true)\n" +
            "  }\n" +
            "### LANGUAGE GROOVY;\n" +
            "PARTITION PROCEDURE GetMamma ON TABLE MAMMA_MIA COLUMN MAMMA\n" +
            ";\n";

    public TestGroovyDeployment(String name) {
        super(name);
    }

    public void testGroovyProcedureInvocation() throws Exception {
        Client client = getClient();
        ClientResponse cr = null;

        cr = client.callProcedure("AddMamma", 1, "Una");
        assertTrue(cr.getStatus() == ClientResponse.SUCCESS);
        cr = client.callProcedure("AddMamma", 2, "Due");
        assertTrue(cr.getStatus() == ClientResponse.SUCCESS);

        cr = client.callProcedure("GetMamma", 2);
        assertEquals(ClientResponse.SUCCESS, cr.getStatus());
        VoltTable result = cr.getResults()[0];
        assertEquals(1, result.getRowCount());
        assertTrue(result.advanceRow());
        assertEquals("Due",result.getString(1));
    }

    static public junit.framework.Test suite() throws Exception
    {
        TheHashinator.initialize(LegacyHashinator.class, LegacyHashinator.getConfigureBytes(2));
        LocalCluster config;

        final MultiConfigSuiteBuilder builder =
            new MultiConfigSuiteBuilder(TestGroovyDeployment.class);

        VoltProjectBuilder project = new VoltProjectBuilder();
        project.addLiteralSchema(SCHEMA);

        /*
         * compile the catalog all tests start with
         */
        config = new LocalCluster("groovy-ddl-cluster-rep.jar", 2, 1, 0,
                BackendTarget.NATIVE_EE_JNI, LocalCluster.FailureState.ALL_RUNNING, true, false, true, null);
        boolean compile = config.compile(project);
        assertTrue(compile);
        config.setHasLocalServer(false);
        config.setMaxHeap(256);
        builder.addServerConfig(config);

        return builder;
    }

}

@@ -1358,16 +1358,90 @@
 sponse;%0A
+import org.voltdb.compiler.JvmProbe;%0Aimport org.voltdb.compiler.Language;%0A
 import o
@@ -1471,32 +1471,32 @@
 ProjectBuilder;%0A
-
 import org.voltd
@@ -3049,16 +3049,284 @@
 %22;%5Cn%22;%0A%0A
+    static final String NON_JAVA_7_SCHEMA = %22CREATE TABLE MAMMA_MIA (%5Cn%22 +%0A            %22  MAMMA INTEGER NOT NULL,%5Cn%22 +%0A            %22  MIA VARCHAR(32) NOT NULL,%5Cn%22 +%0A            %22  CONSTRAINT PK_MAMMA_MIA PRIMARY KEY(MAMMA)%5Cn%22 +%0A            %22)%5Cn%22 +%0A            %22;%5Cn%22;%0A%0A
     publ
@@ -3459,16 +3459,97 @@
 ption %7B%0A
+        if (!JvmProbe.mayLoad(Language.GROOVY)) %7B%0A            return;%0A        %7D%0A%0A
         
@@ -4544,41 +4544,178 @@
     
-project.addLiteralSchema(SCHEMA);
+if (JvmProbe.mayLoad(Language.GROOVY)) %7B%0A            project.addLiteralSchema(SCHEMA);%0A        %7D else %7B%0A            project.addLiteralSchema(NON_JAVA_7_SCHEMA);%0A        %7D
 %0A%0A  

make sure that TestGroovyDeployment runs only on Java 7