content: package com.alicloud.openservices.tablestore.model;
  
  import static org.junit.Assert.assertEquals;
  import static org.junit.Assert.assertTrue;
  
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Map;
  import java.util.TreeMap;
  import java.util.logging.Logger;
  
  import org.junit.AfterClass;
  import org.junit.Before;
  import org.junit.BeforeClass;
  import org.junit.Test;
  
  import com.alicloud.openservices.tablestore.SyncClientInterface;
  import com.alicloud.openservices.tablestore.TableStoreException;
  import com.alicloud.openservices.tablestore.common.OTSHelper;
  import com.alicloud.openservices.tablestore.common.Utils;
  import com.alicloud.openservices.tablestore.core.ErrorCode;
  import com.google.gson.JsonSyntaxException;
  
  public class UpdateRowTest {
      private static final int MILLISECONDS_UNTIL_TABLE_READY = 10 * 1000;
  
      private static final String tableName = "UpdateRowTest";
      private static SyncClientInterface client;
      private static Logger LOG = Logger.getLogger(BatchWriteTest.class.getName());
      
      @BeforeClass
      public static void classBefore() throws JsonSyntaxException, IOException {
          client = Utils.getOTSInstance();
      }
      
      @AfterClass
      public static void classAfter() {
      	client.shutdown();
      }
  
      @Before
      public void setup() throws Exception {
          OTSHelper.deleteAllTable(client);
      }   
  
      private void CreateTable(SyncClientInterface ots, String tableName, Map<String, PrimaryKeyType> pk) throws Exception {
          OTSHelper.createTable(ots, tableName, pk);
          Thread.sleep(MILLISECONDS_UNTIL_TABLE_READY);
      }
      
      /**
       * Insert a non-existent row, expected row existence: IGNORE
       * @throws Exception
       */
      @Test
      public void testUpdateNonExistRowWithIGnoreForInternal() throws Exception {
      	Map<String, PrimaryKeyType> pks = new TreeMap<String, PrimaryKeyType>();
          pks.put("PK1", PrimaryKeyType.STRING);
          pks.put("PK2", PrimaryKeyType.INTEGER);
          
      	CreateTable(client, tableName, pks);
      	
          PrimaryKey pk = PrimaryKeyBuilder.createPrimaryKeyBuilder()
                  .addPrimaryKeyColumn("PK1", PrimaryKeyValue.fromString("haha"))
                  .addPrimaryKeyColumn("PK2", PrimaryKeyValue.fromLong(1234))
                  .build();
          List<Column> columns = new ArrayList<Column>();
          columns.add(new Column("attr", ColumnValue.fromString("hello world")));
          columns.add(new Column("attr1", ColumnValue.fromString("hello world")));
          List<String> deletes = new ArrayList<String>();
          deletes.add("attr");
          OTSHelper.updateRow(client, tableName, pk, columns, deletes, null, RowExistenceExpectation.IGNORE);
          
          SingleRowQueryCriteria query = new SingleRowQueryCriteria(tableName, pk);
          query.setMaxVersions(1);
          GetRowResponse response = OTSHelper.getRow(client, query);
          Row row = response.getRow();
          assertEquals(row.getColumns().length, 1);
          assertEquals(row.getColumn("attr1").get(0).getValue().asString(), "hello world");
      }
      
      /**
       * Insert a non-existent row, expected row existence: exists, throw an error
       * @throws Exception
       */
      @Test
      public void testUpdateNonExistRowWithExpectExistForInternal() throws Exception {
      	Map<String, PrimaryKeyType> pks = new TreeMap<String, PrimaryKeyType>();
          pks.put("PK1", PrimaryKeyType.STRING);
          pks.put("PK2", PrimaryKeyType.INTEGER);
          
      	CreateTable(client, tableName, pks);
      	
          PrimaryKey pk = PrimaryKeyBuilder.createPrimaryKeyBuilder()
                  .addPrimaryKeyColumn("PK1", PrimaryKeyValue.fromString("haha"))
                  .addPrimaryKeyColumn("PK2", PrimaryKeyValue.fromLong(1234))
                  .build();
          List<Column> columns = new ArrayList<Column>();
          columns.add(new Column("attr", ColumnValue.fromString("hello world")));
          columns.add(new Column("attr1", ColumnValue.fromString("hello world")));
          List<String> deletes = new ArrayList<String>();
          deletes.add("attr");
          try {
              OTSHelper.updateRow(client, tableName, pk, columns, deletes, null, RowExistenceExpectation.EXPECT_EXIST);
          	assertTrue(false);
          } catch (TableStoreException e) {
          	assertEquals(ErrorCode.CONDITION_CHECK_FAIL, e.getErrorCode());
          }
      }
      
      /**
       * Insert a non-existent row, expected row existence: does not exist
       * @throws Exception
       */
      @Test
      public void testUpdateExistRowWithExpectNonExistForInternal() throws Exception {
      	Map<String, PrimaryKeyType> pks = new TreeMap<String, PrimaryKeyType>();
          pks.put("PK1", PrimaryKeyType.STRING);
          pks.put("PK2", PrimaryKeyType.INTEGER);
          
      	CreateTable(client, tableName, pks);
      	
          PrimaryKey pk = PrimaryKeyBuilder.createPrimaryKeyBuilder()
                  .addPrimaryKeyColumn("PK1", PrimaryKeyValue.fromString("haha"))
                  .addPrimaryKeyColumn("PK2", PrimaryKeyValue.fromLong(1234))
                  .build();
          List<Column> columns = new ArrayList<Column>();
          columns.add(new Column("attr", ColumnValue.fromString("hello world")));
          columns.add(new Column("attr1", ColumnValue.fromString("hello world")));
          List<String> deletes = new ArrayList<String>();
          deletes.add("attr");
          OTSHelper.updateRow(client, tableName, pk, columns, deletes, null, RowExistenceExpectation.EXPECT_NOT_EXIST);
          
          SingleRowQueryCriteria query = new SingleRowQueryCriteria(tableName, pk);
          query.setMaxVersions(1);
          GetRowResponse response = OTSHelper.getRow(client, query);
          Row row = response.getRow();
          assertEquals(row.getColumns().length, 1);
          assertEquals(row.getColumn("attr1").get(0).getValue().asString(), "hello world");
      }
      
      /**
       * Insert the existing row, expected row existence: IGNORE
       * @throws Exception
       */
      @Test
      public void testUpdateExistRowWithIGnoreForInternal() throws Exception {
      	Map<String, PrimaryKeyType> pks = new TreeMap<String, PrimaryKeyType>();
          pks.put("PK1", PrimaryKeyType.STRING);
          pks.put("PK2", PrimaryKeyType.INTEGER);
          
      	CreateTable(client, tableName, pks);
      	
          PrimaryKey pk = PrimaryKeyBuilder.createPrimaryKeyBuilder()
                  .addPrimaryKeyColumn("PK1", PrimaryKeyValue.fromString("haha"))
                  .addPrimaryKeyColumn("PK2", PrimaryKeyValue.fromLong(1234))
                  .build();
          List<Column> columns = new ArrayList<Column>();
          columns.add(new Column("attr", ColumnValue.fromString("hello world")));
          columns.add(new Column("attr1", ColumnValue.fromString("hello world")));
          List<String> deletes = new ArrayList<String>();
          deletes.add("attr");
          OTSHelper.updateRow(client, tableName, pk, columns, deletes, null, RowExistenceExpectation.IGNORE);
          
          // Write the same row
          OTSHelper.updateRow(client, tableName, pk, columns, deletes, null, RowExistenceExpectation.IGNORE);
          
          SingleRowQueryCriteria query = new SingleRowQueryCriteria(tableName, pk);
          query.setMaxVersions(1);
          GetRowResponse response = OTSHelper.getRow(client, query);
          Row row = response.getRow();
          assertEquals(row.getColumns().length, 1);
          assertEquals(row.getColumn("attr1").get(0).getValue().asString(), "hello world");
      }
      
      /**
       * Insert an existing row, expected row existence: exists
       * @throws Exception
       */
      @Test
      public void testUpdateExistRowWithExpectExistForInternal() throws Exception {
      	Map<String, PrimaryKeyType> pks = new TreeMap<String, PrimaryKeyType>();
          pks.put("PK1", PrimaryKeyType.STRING);
          pks.put("PK2", PrimaryKeyType.INTEGER);
          
      	CreateTable(client, tableName, pks);
      	
          PrimaryKey pk = PrimaryKeyBuilder.createPrimaryKeyBuilder()
                  .addPrimaryKeyColumn("PK1", PrimaryKeyValue.fromString("haha"))
                  .addPrimaryKeyColumn("PK2", PrimaryKeyValue.fromLong(1234))
                  .build();
          List<Column> columns = new ArrayList<Column>();
          columns.add(new Column("attr", ColumnValue.fromString("hello world")));
          columns.add(new Column("attr1", ColumnValue.fromString("hello world")));
          List<String> deletes = new ArrayList<String>();
          deletes.add("attr");
          OTSHelper.updateRow(client, tableName, pk, columns, deletes, null, RowExistenceExpectation.IGNORE);
          
          // Write to the same row
          OTSHelper.updateRow(client, tableName, pk, columns, deletes, null, RowExistenceExpectation.EXPECT_EXIST);
          
          SingleRowQueryCriteria query = new SingleRowQueryCriteria(tableName, pk);
          query.setMaxVersions(1);
          GetRowResponse response = OTSHelper.getRow(client, query);
          Row row = response.getRow();
          assertEquals(row.getColumns().length, 1);
          assertEquals(row.getColumn("attr1").get(0).getValue().asString(), "hello world");
      }
      
      /**
       * Insert an existing row, expected row existence: does not exist, throw error
       * @throws Exception
       */
      @Test
      public void testUpdateNonExistRowWithExpectNonExistForInternal() throws Exception {
      	Map<String, PrimaryKeyType> pks = new TreeMap<String, PrimaryKeyType>();
          pks.put("PK1", PrimaryKeyType.STRING);
          pks.put("PK2", PrimaryKeyType.INTEGER);
          
      	CreateTable(client, tableName, pks);
      	
          PrimaryKey pk = PrimaryKeyBuilder.createPrimaryKeyBuilder()
                  .addPrimaryKeyColumn("PK1", PrimaryKeyValue.fromString("haha"))
                  .addPrimaryKeyColumn("PK2", PrimaryKeyValue.fromLong(1234))
                  .build();
          List<Column> columns = new ArrayList<Column>();
          columns.add(new Column("attr", ColumnValue.fromString("hello world")));
          columns.add(new Column("attr1", ColumnValue.fromString("hello world")));
          List<String> deletes = new ArrayList<String>();
          deletes.add("attr");
          OTSHelper.updateRow(client, tableName, pk, columns, deletes, null, RowExistenceExpectation.IGNORE);
          
          // Write to the same row
          try {
              OTSHelper.updateRow(client, tableName, pk, columns, deletes, null, RowExistenceExpectation.EXPECT_NOT_EXIST);
          } catch (TableStoreException e) {
          	assertEquals(ErrorCode.CONDITION_CHECK_FAIL, e.getErrorCode());
          }
      }
  
  }
  