package org.sfm.datastax;

import com.datastax.driver.core.*;
import com.datastax.driver.core.querybuilder.Batch;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import org.junit.Test;
import org.sfm.beans.DbObject;
import org.sfm.map.Mapper;
import org.sfm.reflect.TypeReference;

import java.lang.reflect.Type;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

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


public class SettableDataMapperTest extends AbstractDatastaxTest {

    public static final String QUERY = "insert into " +
            "dbobjects(id, name, email, creation_time, type_ordinal, type_name) " +
            "values(?, ?, ?, ?, ?, ?)";
    List<DbObject> dbObjects = Arrays.asList(DbObject.newInstance(), DbObject.newInstance());

    @Test
    public void testInsertDbObjects() throws Exception {
        testInSession(new Callback() {
            @Override
            public void call(Session session) throws Exception {

                PreparedStatement preparedStatement = session.prepare(QUERY);

                DatastaxBinder<DbObject> datastaxBinder = DatastaxMapperFactory.newInstance().mapFrom(DbObject.class);

                session.execute(datastaxBinder.mapTo(dbObjects.get(0), preparedStatement));
                checkObjectInserted(session, 0);

            }
        });
    }

    protected void checkObjectInserted(Session session, int i) {
        DbObject object = dbObjects.get(i);
        DatastaxMapper<DbObject> dbObjectDatastaxMapper = DatastaxMapperFactory.newInstance().mapTo(DbObject.class);
        BoundStatement boundStatement = session.prepare("select * from dbobjects where id = ?").bind(object.getId());
        ResultSet execute = session.execute(boundStatement);
        DbObject actual = dbObjectDatastaxMapper.iterator(execute).next();
        assertEquals(object, actual);
    }


    @Test
    public void testInsertDbObjectsBatch() throws Exception {
        testInSession(new Callback() {
            @Override
            public void call(Session session) throws Exception {


                Batch bs = QueryBuilder.batch();

                bs.add(new SimpleStatement(QUERY));
                bs.add(new SimpleStatement(QUERY));


                PreparedStatement preparedStatement = session.prepare(bs);

                DatastaxBinder<List<DbObject>> datastaxBinder = DatastaxMapperFactory.newInstance().disableAsm(true).mapFrom(new TypeReference<List<DbObject>>() {
                });

                Statement statement = datastaxBinder.mapTo(dbObjects, preparedStatement);

                statement.enableTracing();
                session.execute(statement);

                checkObjectInserted(session, 0);
                checkObjectInserted(session, 1);

            }
        });
    }

    @Test
    public void testUpdateDbObjectsBatch() throws Exception {
        testInSession(new Callback() {
            @Override
            public void call(Session session) throws Exception {


                PreparedStatement preparedStatement = session.prepare(QUERY);

                DatastaxBinder<DbObject> datastaxBinder = DatastaxMapperFactory.newInstance().mapFrom(DbObject.class);

                DbObject value = dbObjects.get(0);

                session.execute(datastaxBinder.mapTo(value, preparedStatement));
                checkObjectInserted(session, 0);

                PreparedStatement updateStatement = session.prepare("UPDATE dbobjects SET name = ? WHERE id = ?");
                value.setName("newname");

                session.execute(datastaxBinder.mapTo(value, updateStatement));

                checkObjectInserted(session, 0);
            }
        });
    }

}
@@ -220,16 +220,66 @@
 Object;%0A
+import org.sfm.datastax.beans.DbObjectsWithTuple;%0A
 import o
@@ -293,24 +293,24 @@
 map.Mapper;%0A
-
 import org.s
@@ -334,16 +334,46 @@
 ference;
+%0Aimport org.sfm.tuples.Tuple3;
 %0A%0Aimport
@@ -842,16 +842,16 @@
 ?, ?)%22;%0A
-
     List
@@ -933,24 +933,864 @@
 stance());%0A%0A
+%0A    @Test%0A    public void testUpdateDbObjectWithTuple() throws Exception %7B%0A        testInSession(new Callback() %7B%0A            @Override%0A            public void call(Session session) throws Exception %7B%0A                final DatastaxMapper%3CDbObjectsWithTuple%3E mapper = DatastaxMapperFactory.newInstance().mapTo(DbObjectsWithTuple.class);%0A%0A%0A                DbObjectsWithTuple value = mapper.iterator(session.execute(%22select id, t from dbobjects_tuple%22)).next();%0A%0A                PreparedStatement preparedStatement =  session.prepare(%22update dbobjects_tuple SET t  = ? where id = ?%22);%0A%0A%0A                DatastaxBinder%3CDbObjectsWithTuple%3E datastaxBinder = DatastaxMapperFactory.newInstance().mapFrom(DbObjectsWithTuple.class);%0A%0A                session.execute(datastaxBinder.mapTo(value, preparedStatement));%0A%0A            %7D%0A        %7D);%0A    %7D%0A%0A
     @Test%0A  

increase datastax coverage