// Path: src/main/java/com/basho/riak/presto/SplitTask.java
// public class SplitTask {
//     private final String host;
//     private final OtpErlangTuple task; // OtpErlangObject[] a = {vnode, filterVnodes};
// 
// 
//     public SplitTask(String node, OtpErlangTuple task) {
//         this.host = node2host(node);
//         this.task = task;
//     }
// 
//     // fromString(String)
//     public SplitTask(String data)
//             throws OtpErlangDecodeException, DecoderException {
// 
//         byte[] binary = Base64.decodeBase64(Hex.decodeHex(data.toCharArray()));
//         task = (OtpErlangTuple) binary2term(binary);
//         // task = {vnode, filterVnodes}
//         OtpErlangTuple vnode = (OtpErlangTuple) task.elementAt(0);
//         // vnode = {index, node}
//         OtpErlangAtom erlangNode = (OtpErlangAtom) vnode.elementAt(1);
//         // "dev" @ "127.0.0.1"
//         this.host = node2host(erlangNode.atomValue());
//     }
// 
//     private String node2host(String node) {
//         String[] s = node.split("@");
//         return s[1];
//     }
// 
//     // @doc hostname without port number: Riak doesn't care about port number.
//     public String getHost() {
//         return host;
//     }
// 
//     public String toString() {
//         byte[] binary = term2binary(task);
//         byte[] b = Base64.encodeBase64(binary);
//         return Hex.encodeHexString(b);
//     }
// 
//     public byte[] term2binary(OtpErlangObject o) {
//         OtpOutputStream oos = new OtpOutputStream();
//         oos.write_any(o);
//         return oos.toByteArray();
//     }
// 
//     public OtpErlangObject binary2term(byte[] data)
//             throws OtpErlangDecodeException {
//         OtpInputStream ois = new OtpInputStream(data);
//         return ois.read_any();
//     }
// 
//     public OtpErlangTuple getTask() {
//         return this.task;
//     }
// 
//     public OtpErlangList fetchAllData(DirectConnection conn, String schemaName, String tableName)
//             throws OtpErlangDecodeException, OtpAuthException, OtpErlangExit {
//         OtpErlangTuple t = (OtpErlangTuple) task;
//         OtpErlangTuple vnode = (OtpErlangTuple) t.elementAt(0);
//         OtpErlangList filterVnodes = (OtpErlangList) t.elementAt(1);
// 
//         try {
//             OtpErlangList riakObjects = conn.processSplit(schemaName.getBytes(), tableName.getBytes(), vnode, filterVnodes);
//             //System.out.println(riakObjects);
//             return riakObjects;
//         } catch (java.io.IOException e) {
//             System.err.println(e);
//         }
//         return new OtpErlangList();
//     }
// 
//     public OtpErlangList fetchViaIndex(DirectConnection conn, String schemaName, String tableName,
//                                        OtpErlangTuple query)
//             throws OtpErlangDecodeException, OtpAuthException, OtpErlangExit {
//         OtpErlangTuple t = (OtpErlangTuple) task;
//         OtpErlangTuple vnode = (OtpErlangTuple) t.elementAt(0);
//         OtpErlangList filterVnodes = (OtpErlangList) t.elementAt(1);
// 
//         try {
//             OtpErlangTuple result = conn.processSplitIndex(schemaName.getBytes(), tableName.getBytes(), vnode,
//                     filterVnodes, query);
//             OtpErlangList riakObjects = (OtpErlangList) result.elementAt(1);
//             //System.out.println(riakObjects);
//             return riakObjects;
//         } catch (java.io.IOException e) {
//             System.err.println(e);
//         }
//         return new OtpErlangList();
//     }
// 
// }


import com.basho.riak.presto.SplitTask;
import com.ericsson.otp.erlang.OtpErlangAtom;
import com.ericsson.otp.erlang.OtpErlangDecodeException;
import com.ericsson.otp.erlang.OtpErlangObject;
import com.ericsson.otp.erlang.OtpErlangTuple;
import com.ericsson.otp.erlang.OtpErlangLong;
import com.google.common.annotations.VisibleForTesting;
import org.apache.commons.codec.DecoderException;
import org.junit.Test;
import static org.junit.Assert.*;
package com.basho.riak.presto;

/**
 * Created by kuenishi on 14/03/29.
 */
public class TestSplitTask {

    @Test
    public void testPasses() {
        String expected = "Hello, JUnit!";
        String hello = "Hello, JUnit!";
        assertEquals(hello, expected);
    }

    @Test
    public void testEncode()
            throws OtpErlangDecodeException, DecoderException
    {
        OtpErlangObject[] list = {new OtpErlangLong(12),
                new OtpErlangAtom("dev@127.0.0.1")};

        OtpErlangObject[] list2 = {new OtpErlangTuple(list),
                new OtpErlangAtom("dev@1.1.1.1")};
        OtpErlangTuple t = new OtpErlangTuple(list2);

// Path: src/main/java/com/basho/riak/presto/SplitTask.java
// public class SplitTask {
//     private final String host;
//     private final OtpErlangTuple task; // OtpErlangObject[] a = {vnode, filterVnodes};
// 
// 
//     public SplitTask(String node, OtpErlangTuple task) {
//         this.host = node2host(node);
//         this.task = task;
//     }
// 
//     // fromString(String)
//     public SplitTask(String data)
//             throws OtpErlangDecodeException, DecoderException {
// 
//         byte[] binary = Base64.decodeBase64(Hex.decodeHex(data.toCharArray()));
//         task = (OtpErlangTuple) binary2term(binary);
//         // task = {vnode, filterVnodes}
//         OtpErlangTuple vnode = (OtpErlangTuple) task.elementAt(0);
//         // vnode = {index, node}
//         OtpErlangAtom erlangNode = (OtpErlangAtom) vnode.elementAt(1);
//         // "dev" @ "127.0.0.1"
//         this.host = node2host(erlangNode.atomValue());
//     }
// 
//     private String node2host(String node) {
//         String[] s = node.split("@");
//         return s[1];
//     }
// 
//     // @doc hostname without port number: Riak doesn't care about port number.
//     public String getHost() {
//         return host;
//     }
// 
//     public String toString() {
//         byte[] binary = term2binary(task);
//         byte[] b = Base64.encodeBase64(binary);
//         return Hex.encodeHexString(b);
//     }
// 
//     public byte[] term2binary(OtpErlangObject o) {
//         OtpOutputStream oos = new OtpOutputStream();
//         oos.write_any(o);
//         return oos.toByteArray();
//     }
// 
//     public OtpErlangObject binary2term(byte[] data)
//             throws OtpErlangDecodeException {
//         OtpInputStream ois = new OtpInputStream(data);
//         return ois.read_any();
//     }
// 
//     public OtpErlangTuple getTask() {
//         return this.task;
//     }
// 
//     public OtpErlangList fetchAllData(DirectConnection conn, String schemaName, String tableName)
//             throws OtpErlangDecodeException, OtpAuthException, OtpErlangExit {
//         OtpErlangTuple t = (OtpErlangTuple) task;
//         OtpErlangTuple vnode = (OtpErlangTuple) t.elementAt(0);
//         OtpErlangList filterVnodes = (OtpErlangList) t.elementAt(1);
// 
//         try {
//             OtpErlangList riakObjects = conn.processSplit(schemaName.getBytes(), tableName.getBytes(), vnode, filterVnodes);
//             //System.out.println(riakObjects);
//             return riakObjects;
//         } catch (java.io.IOException e) {
//             System.err.println(e);
//         }
//         return new OtpErlangList();
//     }
// 
//     public OtpErlangList fetchViaIndex(DirectConnection conn, String schemaName, String tableName,
//                                        OtpErlangTuple query)
//             throws OtpErlangDecodeException, OtpAuthException, OtpErlangExit {
//         OtpErlangTuple t = (OtpErlangTuple) task;
//         OtpErlangTuple vnode = (OtpErlangTuple) t.elementAt(0);
//         OtpErlangList filterVnodes = (OtpErlangList) t.elementAt(1);
// 
//         try {
//             OtpErlangTuple result = conn.processSplitIndex(schemaName.getBytes(), tableName.getBytes(), vnode,
//                     filterVnodes, query);
//             OtpErlangList riakObjects = (OtpErlangList) result.elementAt(1);
//             //System.out.println(riakObjects);
//             return riakObjects;
//         } catch (java.io.IOException e) {
//             System.err.println(e);
//         }
//         return new OtpErlangList();
//     }
// 
// }


// Path: src/test/java/com/basho/riak/presto/TestSplitTask.java
import com.basho.riak.presto.SplitTask;
import com.ericsson.otp.erlang.OtpErlangAtom;
import com.ericsson.otp.erlang.OtpErlangDecodeException;
import com.ericsson.otp.erlang.OtpErlangObject;
import com.ericsson.otp.erlang.OtpErlangTuple;
import com.ericsson.otp.erlang.OtpErlangLong;
import com.google.common.annotations.VisibleForTesting;
import org.apache.commons.codec.DecoderException;
import org.junit.Test;
import static org.junit.Assert.*;

package com.basho.riak.presto;

/**
 * Created by kuenishi on 14/03/29.
 */
public class TestSplitTask {

    @Test
    public void testPasses() {
        String expected = "Hello, JUnit!";
        String hello = "Hello, JUnit!";
        assertEquals(hello, expected);
    }

    @Test
    public void testEncode()
            throws OtpErlangDecodeException, DecoderException
    {
        OtpErlangObject[] list = {new OtpErlangLong(12),
                new OtpErlangAtom("dev@127.0.0.1")};

        OtpErlangObject[] list2 = {new OtpErlangTuple(list),
                new OtpErlangAtom("dev@1.1.1.1")};
        OtpErlangTuple t = new OtpErlangTuple(list2);

        SplitTask splitTask = new SplitTask("dev@127.0.0.1", t);