/**
 * Copyright (C) 2011 Akiban Technologies Inc.
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses.
 */

package com.akiban.cserver.itests.bugs.bug687225;

import java.nio.ByteBuffer;
import java.util.List;

import com.akiban.cserver.InvalidOperationException;
import com.akiban.cserver.RowData;
import com.akiban.cserver.api.GenericInvalidOperationException;
import com.akiban.cserver.api.common.TableId;
import com.akiban.cserver.api.dml.scan.CursorId;
import com.akiban.cserver.api.dml.scan.LegacyRowOutput;
import com.akiban.cserver.api.dml.scan.LegacyScanRequest;
import com.akiban.cserver.api.dml.scan.NewRow;
import com.akiban.cserver.api.dml.scan.ScanAllRequest;
import com.akiban.cserver.api.dml.scan.ScanRequest;
import com.akiban.cserver.api.dml.scan.WrappingRowOutput;
import com.akiban.cserver.itests.ApiTestBase;
import com.akiban.cserver.message.ScanRowsRequest;
import com.akiban.cserver.message.ScanRowsResponse;
import com.akiban.cserver.store.RowCollector;

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


public final class TruncateTableIT extends ApiTestBase {
    @Test
    public void basicTruncate() throws InvalidOperationException {
        TableId tId = createTable("test", "t", "id int key");

        expectRowCount(tId, 0);
        final int rowCount = 5;
        for(int i = 1; i <= rowCount; ++i) {
            dml().writeRow(session, createNewRow(tId, i));
        }
        expectRowCount(tId, rowCount);

        dml().truncateTable(session, tId);

        // Check that we can still get the rows
        List<NewRow> rows = scanAll(new ScanAllRequest(tId, null));
        assertEquals("Rows scanned", 0, rows.size());
    }
    
    @Test(expected=GenericInvalidOperationException.class)
    public void bugTestCase() throws InvalidOperationException {
        TableId tId = createTable("test", "t", "id int NOT NULL, pid int NOT NULL, PRIMARY KEY(id), UNIQUE KEY pid(pid)");

        expectRowCount(tId, 0);
        dml().writeRow(session, createNewRow(tId, 1, 1));
        dml().writeRow(session, createNewRow(tId, 2, 2));
        expectRowCount(tId, 2);

        dml().truncateTable(session, tId);
        
        int indexId = ddl().getAIS(session).getTable("test", "t").getIndex("pid").getIndexId();
        int scanFlags = RowCollector.SCAN_FLAGS_START_AT_EDGE | RowCollector.SCAN_FLAGS_END_AT_EDGE;
        
        // Exception currently thrown during dml.doScan: Corrupt RowData at {1,(long)1}
        List<NewRow> rows = scanAll(new ScanAllRequest(tId, null, indexId, scanFlags));
        assertEquals("Rows scanned", 0, rows.size());
    }
}

@@ -1,11 +1,10 @@
 /*
-*
 %0A * Copy
@@ -704,60 +704,8 @@
 5;%0A%0A
-import java.nio.ByteBuffer;%0Aimport java.util.List;%0A%0A
 impo
@@ -757,43 +757,8 @@
 on;%0A
-import com.akiban.cserver.RowData;%0A
 impo
@@ -851,544 +851,122 @@
 api.
-common.TableId;%0Aimport com.akiban.cserver.api.dml.scan.CursorId;%0Aimport com.akiban.cserver.api.dml.scan.LegacyRowOutput;%0Aimport com.akiban.cserver.api.dml.scan.LegacyScanRequest;%0Aimport com.akiban.cserver.api.dml.scan.NewRow;%0Aimport com.akiban.cserver.api.dml.scan.ScanAllRequest;%0Aimport com.akiban.cserver.api.dml.scan.ScanRequest;%0Aimport com.akiban.cserver.api.dml.scan.WrappingRowOutput;%0Aimport com.akiban.cserver.itests.ApiTestBase;%0Aimport com.akiban.cserver.message.ScanRowsRequest;%0Aimport com.akiban.cserver.message.ScanRowsRespon
+dml.scan.NewRow;%0Aimport com.akiban.cserver.api.dml.scan.ScanAllRequest;%0Aimport com.akiban.cserver.itests.ApiTestBa
 se;%0A
@@ -1011,17 +1011,16 @@
 lector;%0A
-%0A
 import o
@@ -1030,24 +1030,49 @@
 junit.Test;%0A
+%0Aimport java.util.List;%0A%0A
 import stati
@@ -1105,17 +1105,16 @@
 quals;%0A%0A
-%0A
 public f
@@ -1243,33 +1243,33 @@
 n %7B%0A        
-TableId t
+int table
 Id = createT
@@ -1318,32 +1318,36 @@
 expectRowCount(t
+able
 Id, 0);%0A        
@@ -1461,24 +1461,28 @@
 eateNewRow(t
+able
 Id, i));%0A   
@@ -1508,24 +1508,28 @@
 ctRowCount(t
+able
 Id, rowCount
@@ -1566,24 +1566,28 @@
 e(session, t
+able
 Id);%0A%0A      
@@ -1676,32 +1676,36 @@
 ScanAllRequest(t
+able
 Id, null));%0A    
@@ -1901,17 +1901,17 @@
     
-TableId t
+int table
 Id =
@@ -2033,24 +2033,28 @@
 ctRowCount(t
+able
 Id, 0);%0A    
@@ -2091,24 +2091,28 @@
 eateNewRow(t
+able
 Id, 1, 1));%0A
@@ -2157,16 +2157,20 @@
 NewRow(t
+able
 Id, 2, 2
@@ -2197,16 +2197,20 @@
 wCount(t
+able
 Id, 2);%0A
@@ -2248,16 +2248,20 @@
 ssion, t
+able
 Id);%0A   
@@ -2616,16 +2616,20 @@
 equest(t
+able
 Id, null

Update test for FooId removal