package com.github.terma.gigaspacewebconsole.provider.executor.database;

import com.github.terma.gigaspacewebconsole.core.ExecuteRequest;
import com.github.terma.gigaspacewebconsole.core.ObjectExecuteResponseStream;
import com.github.terma.gigaspacewebconsole.core.config.Config;
import com.github.terma.gigaspacewebconsole.provider.executor.DatabaseExecutor;
import junit.framework.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Collections;

public class DatabaseExecutorTest {

    @BeforeClass
    public static void before() {
        System.setProperty(Config.CONFIG_PATH_SYSTEM_PROPERTY, Config.NONE);
    }

    @Test
    public void shouldAllowExecute() throws Exception {
        ExecuteRequest request = new ExecuteRequest();
        request.url = "jdbc:h2:mem:execute";
        request.driver = "org.h2.Driver";
        request.sql = "create table A (id int);";

        ObjectExecuteResponseStream responseStream = new ObjectExecuteResponseStream();
        DatabaseExecutor.INSTANCE.execute(request, responseStream);

        Assert.assertEquals(1, responseStream.getColumns().size());
        Assert.assertEquals(1, responseStream.getData().size());
    }

    @Test
    public void shouldAllowQuery() throws Exception {
        ExecuteRequest request = new ExecuteRequest();
        request.url = "jdbc:h2:mem:query;DB_CLOSE_DELAY=-1";
        request.driver = "org.h2.Driver";

        request.sql = "create table A (id int);";
        DatabaseExecutor.INSTANCE.execute(request, new ObjectExecuteResponseStream());

        request.sql = "insert into A values (12);";
        DatabaseExecutor.INSTANCE.execute(request, new ObjectExecuteResponseStream());

        request.sql = "select * from A";
        ObjectExecuteResponseStream responseStream = new ObjectExecuteResponseStream();
        DatabaseExecutor.INSTANCE.execute(request, responseStream);

        Assert.assertEquals(1, responseStream.getColumns().size());
        Assert.assertEquals(1, responseStream.getData().size());
        Assert.assertEquals(Collections.singletonList("12"), responseStream.getData().get(0));
    }

}

@@ -385,16 +385,41 @@
 Assert;%0A
+import org.junit.Before;%0A
 import o
@@ -532,16 +532,53 @@
 Test %7B%0A%0A
+    private ExecuteRequest request;%0A%0A
     @Bef
@@ -701,36 +701,38 @@
 E);%0A    %7D%0A%0A    @
-Test
+Before
 %0A    public void
@@ -736,70 +736,33 @@
 oid 
-shouldAllowExecute() throws Exception %7B%0A        ExecuteRequest
+createRequest() %7B%0A       
  req
@@ -833,16 +833,55 @@
 execute%22
+ + Math.random() + %22;DB_CLOSE_DELAY=-1%22
 ;%0A      
@@ -916,16 +916,89 @@
 river%22;%0A
+    %7D%0A%0A    @Test%0A    public void shouldAllowExecute() throws Exception %7B%0A
         
@@ -1405,167 +1405,8 @@
 n %7B%0A
-        ExecuteRequest request = new ExecuteRequest();%0A        request.url = %22jdbc:h2:mem:query;DB_CLOSE_DELAY=-1%22;%0A        request.driver = %22org.h2.Driver%22;%0A%0A
     
@@ -2112,10 +2112,546 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void shouldSupportMultipleQueries() throws Exception %7B%0A        request.sql = %22create table A (id int);%5Cnselect * from A;%22;%0A%0A        ObjectExecuteResponseStream responseStream = new ObjectExecuteResponseStream();%0A        DatabaseExecutor.INSTANCE.execute(request, responseStream);%0A%0A        Assert.assertEquals(1, responseStream.getColumns().size());%0A        Assert.assertEquals(1, responseStream.getData().size());%0A        Assert.assertEquals(Collections.singletonList(%220%22), responseStream.getData().get(0));%0A    %7D%0A%0A
 %7D%0A

Check that H2 support multi query in same query.