package com.marklogic.contentpump;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.After;
import org.junit.Test;

import com.marklogic.contentpump.utilities.OptionsFileUtil;
import com.marklogic.xcc.ResultSequence;

public class TestDistributedImportDocs {
    @After
    public void tearDown() {
        Utils.closeSession();
    }
    
    @Test
    public void testImportMixedDocs() throws Exception {
        String cmd = "IMPORT -password admin -username admin -host localhost"
            + " -input_file_path " + Constants.TEST_PATH.toUri() + "/wiki"
            + " -port 5275 -output_uri_replace wiki,'wiki1'"
            + " -output_uri_prefix test/"
            + " -output_collections test,ML"
            + " -fastload true"
            + " -hadoop_conf_dir " + Constants.HADOOP_CONF_DIR;
        String[] args = cmd.split(" ");
        assertFalse(args.length == 0);

        Utils.clearDB("xcc://admin:admin@localhost:5275", "Documents");

        String[] expandedArgs = null;
        expandedArgs = OptionsFileUtil.expandArguments(args);
        Utils.prepareDistributedMode();
        ContentPump.runCommand(expandedArgs);

        ResultSequence result = Utils.runQuery(
            "xcc://admin:admin@localhost:5275",
            "fn:count(fn:collection(\"ML\"))");
        assertTrue(result.hasNext());
        assertEquals("93", result.next().asString());
        Utils.closeSession();
    }
    
    @Test
    public void testImportText() throws Exception {
        String cmd = "IMPORT -password admin -username admin -host localhost"
            + " -input_file_path " + Constants.TEST_PATH.toUri() + "/wiki/AbacuS"
            + " -port 5275 -output_uri_prefix ABC"
            + " -output_collections test,ML -document_type text"
            + " -hadoop_conf_dir " + Constants.HADOOP_CONF_DIR;
        String[] args = cmd.split(" ");
        assertFalse(args.length == 0);

        Utils.clearDB("xcc://admin:admin@localhost:5275", "Documents");

        String[] expandedArgs = null;
        expandedArgs = OptionsFileUtil.expandArguments(args);
        Utils.prepareDistributedMode();
        ContentPump.runCommand(expandedArgs);

        ResultSequence result = Utils.runQuery(
            "xcc://admin:admin@localhost:5275", "fn:count(fn:collection())");
        assertTrue(result.hasNext());
        assertEquals("1", result.next().asString());
        Utils.closeSession();
    }
    
    @Test
    public void testImportMixedDocsZipMultiplewriter() throws Exception {
        String cmd = 
            "IMPORT -password admin -username admin -host localhost -port 5275"
            + " -input_file_path " + Constants.TEST_PATH.toUri() + "/zips"
            + " -thread_count_per_split 3"
            + " -hadoop_conf_dir " + Constants.HADOOP_CONF_DIR
            + " -input_compressed -input_compression_codec zip"
            + " -output_collections test,ML";
        String[] args = cmd.split(" ");
        assertFalse(args.length == 0);

        Utils.clearDB("xcc://admin:admin@localhost:5275", "Documents");

        String[] expandedArgs = null;
        expandedArgs = OptionsFileUtil.expandArguments(args);
        Utils.prepareDistributedMode();
        ContentPump.runCommand(expandedArgs);

        ResultSequence result = Utils.runQuery(
            "xcc://admin:admin@localhost:5275",
            "fn:count(fn:collection(\"test\"))");
        assertTrue(result.hasNext());
        assertEquals("93", result.next().asString());
        Utils.closeSession();
    }
 
}

@@ -1230,32 +1230,1303 @@
 expandedArgs);%0A%0A
+        ResultSequence result = Utils.runQuery(%0A            %22xcc://admin:admin@localhost:5275%22,%0A            %22fn:count(fn:collection(%5C%22ML%5C%22))%22);%0A        assertTrue(result.hasNext());%0A        assertEquals(%2293%22, result.next().asString());%0A        Utils.closeSession();%0A    %7D%0A    %0A    @Test%0A    public void testImportTransformMixedDocs() throws Exception %7B%0A        Utils.prepareModule(%22xcc://admin:admin@localhost:5275%22);%0A        String cmd = %22IMPORT -password admin -username admin -host localhost%22%0A            + %22 -input_file_path %22 + Constants.TEST_PATH.toUri() + %22/wiki%22%0A            + %22 -port 5275 -output_uri_replace wiki,'wiki1'%22%0A            + %22 -output_uri_prefix test/%22%0A            + %22 -output_collections test,ML%22%0A            + %22 -fastload true%22%0A            + %22 -transform_module /lc.xqy%22%0A            + %22 -transform_namespace http://marklogic.com/module_invoke%22%0A            + %22 -hadoop_conf_dir %22 + Constants.HADOOP_CONF_DIR;%0A        String%5B%5D args = cmd.split(%22 %22);%0A        assertFalse(args.length == 0);%0A%0A        Utils.clearDB(%22xcc://admin:admin@localhost:5275%22, %22Documents%22);%0A%0A        String%5B%5D expandedArgs = null;%0A        expandedArgs = OptionsFileUtil.expandArguments(args);%0A        Utils.prepareDistributedMode();%0A        ContentPump.runCommand(expandedArgs);%0A%0A
         ResultSe
@@ -3006,16 +3006,20 @@
 i/AbacuS
+.xml
 %22%0A      

test case