/*
 * JBoss, Home of Professional Open Source.
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * Modified by
 * @author  Data Virtuality GmbH
 * @date    $Date$
 *  
 */
// https://issues.jboss.org/browse/TEIID-2153
// https://issues.jboss.org/browse/TEIID-2171
// https://issues.jboss.org/browse/TEIID-2359
// https://issues.jboss.org/browse/TEIID-2149
// https://issues.jboss.org/browse/TEIID-2358
// https://issues.jboss.org/browse/TEIID-2410
// https://issues.jboss.org/browse/TEIID-2271
// https://issues.jboss.org/browse/TEIID-2467
// https://issues.jboss.org/browse/TEIID-2429
// https://issues.jboss.org/browse/TEIID-2363
// https://issues.jboss.org/browse/TEIID-2160
// https://issues.jboss.org/browse/TEIID-2461
// https://issues.jboss.org/browse/TEIID-2797
// https://issues.jboss.org/browse/TEIID-2326
// https://issues.jboss.org/browse/TEIID-1557
// https://issues.jboss.org/browse/TEIID-2626
// https://issues.jboss.org/browse/TEIID-2707
// https://issues.jboss.org/browse/TEIID-2900
// https://issues.jboss.org/browse/TEIID-2902
// https://issues.jboss.org/browse/TEIID-2457
// https://issues.jboss.org/browse/TEIID-2942
// https://github.com/teiid/teiid/commit/f840ed8283aad9cbe8d3019467a88d951d695659
// https://issues.jboss.org/browse/TEIID-2482
// https://issues.jboss.org/browse/TEIID-2346
// https://issues.jboss.org/browse/TEIID-2430
// https://issues.jboss.org/browse/TEIID-2731
// https://issues.jboss.org/browse/TEIID-2747
// https://issues.jboss.org/browse/TEIID-2946
package org.teiid.common.buffer;

import static org.junit.jupiter.api.Assertions.*;

import java.util.Arrays;
import java.util.List;

import javax.sql.rowset.serial.SerialClob;

import org.junit.jupiter.api.Test;
import org.teiid.common.buffer.BufferManager.TupleSourceType;
import org.teiid.common.buffer.TupleBuffer.TupleBufferTupleSource;
import org.teiid.core.types.ClobType;
import org.teiid.core.types.DataTypeManager;
import org.teiid.query.sql.symbol.ElementSymbol;

public class TestTupleBuffer {

	@Test public void testForwardOnly() throws Exception {
		ElementSymbol x = new ElementSymbol("x"); //$NON-NLS-1$
		x.setType(DataTypeManager.DefaultDataClasses.INTEGER);
		List<ElementSymbol> schema = Arrays.asList(x);
		TupleBuffer tb = BufferManagerFactory.getStandaloneBufferManager().createTupleBuffer(schema, "x", TupleSourceType.PROCESSOR); //$NON-NLS-1$ 
		tb.setForwardOnly(true);
		tb.addTuple(Arrays.asList(1));
		TupleBatch batch = tb.getBatch(1);
		assertTrue(!batch.getTerminationFlag());
		assertEquals(1, batch.getBeginRow());
		try {
			tb.getBatch(1);
			fail("expected exception"); //$NON-NLS-1$
		} catch (AssertionError e) {
			
		}
		tb.addTuple(Arrays.asList(1));
		tb.close();
		batch = tb.getBatch(2);
		assertTrue(batch.getTerminationFlag());
		assertEquals(2, batch.getBeginRow());
	}
	
	@Test public void testReverseIteration() throws Exception {
		ElementSymbol x = new ElementSymbol("x"); //$NON-NLS-1$
		x.setType(DataTypeManager.DefaultDataClasses.INTEGER);
		List<ElementSymbol> schema = Arrays.asList(x);
		TupleBuffer tb = BufferManagerFactory.getStandaloneBufferManager().createTupleBuffer(schema, "x", TupleSourceType.PROCESSOR); //$NON-NLS-1$ 
		tb.addTuple(Arrays.asList(1));
		tb.addTuple(Arrays.asList(2));
		TupleBufferTupleSource tbts = tb.createIndexedTupleSource();
		tbts.setReverse(true);
		assertTrue(tbts.hasNext());
		assertEquals(2, tbts.nextTuple().get(0));
		assertEquals(1, tbts.nextTuple().get(0));
		assertFalse(tbts.hasNext());
	}
	
	@Test public void testTruncate() throws Exception {
		ElementSymbol x = new ElementSymbol("x"); //$NON-NLS-1$
		x.setType(DataTypeManager.DefaultDataClasses.INTEGER);
		List<ElementSymbol> schema = Arrays.asList(x);
		TupleBuffer tb = BufferManagerFactory.getStandaloneBufferManager().createTupleBuffer(schema, "x", TupleSourceType.PROCESSOR); //$NON-NLS-1$
		tb.setBatchSize(2);
		for (int i = 0; i < 5; i++) {
			tb.addTuple(Arrays.asList(1));
		}
		TupleBatch batch = tb.getBatch(1);
		assertTrue(!batch.getTerminationFlag());
		assertEquals(2, batch.getEndRow());
		tb.close();
		assertEquals(5, tb.getManagedRowCount());
		tb.truncateTo(3);
		assertEquals(3, tb.getManagedRowCount());
		assertEquals(3, tb.getRowCount());
		batch = tb.getBatch(3);
		assertTrue(batch.getTerminationFlag());
		tb.truncateTo(2);
		assertEquals(2, tb.getManagedRowCount());
		assertEquals(2, tb.getRowCount());
		batch = tb.getBatch(2);
		assertTrue(batch.getTerminationFlag());
	}
	
	@Test public void testTruncatePartial() throws Exception {
		ElementSymbol x = new ElementSymbol("x"); //$NON-NLS-1$
		x.setType(DataTypeManager.DefaultDataClasses.INTEGER);
		List<ElementSymbol> schema = Arrays.asList(x);
		TupleBuffer tb = BufferManagerFactory.getStandaloneBufferManager().createTupleBuffer(schema, "x", TupleSourceType.PROCESSOR); //$NON-NLS-1$
		tb.setBatchSize(64);
		for (int i = 0; i < 65; i++) {
			tb.addTuple(Arrays.asList(1));
		}
		TupleBatch batch = tb.getBatch(1);
		assertTrue(!batch.getTerminationFlag());
		assertEquals(65, tb.getManagedRowCount());
		tb.truncateTo(3);
		assertEquals(3, tb.getManagedRowCount());
		assertEquals(3, tb.getRowCount());
		batch = tb.getBatch(3);
	}
	
	@Test public void testTruncatePartial1() throws Exception {
		ElementSymbol x = new ElementSymbol("x"); //$NON-NLS-1$
		x.setType(DataTypeManager.DefaultDataClasses.INTEGER);
		List<ElementSymbol> schema = Arrays.asList(x);
		TupleBuffer tb = BufferManagerFactory.getStandaloneBufferManager().createTupleBuffer(schema, "x", TupleSourceType.PROCESSOR); //$NON-NLS-1$
		tb.setBatchSize(128);
		for (int i = 0; i < 131; i++) {
			tb.addTuple(Arrays.asList(1));
		}
		tb.truncateTo(129);
		assertEquals(129, tb.getManagedRowCount());
		assertEquals(129, tb.getRowCount());
	}
	
	@Test public void testTruncateMultiple() throws Exception {
		ElementSymbol x = new ElementSymbol("x"); //$NON-NLS-1$
		x.setType(DataTypeManager.DefaultDataClasses.INTEGER);
		List<ElementSymbol> schema = Arrays.asList(x);
		TupleBuffer tb = BufferManagerFactory.getStandaloneBufferManager().createTupleBuffer(schema, "x", TupleSourceType.PROCESSOR); //$NON-NLS-1$
		tb.setBatchSize(16);
		for (int i = 0; i < 131; i++) {
			tb.addTuple(Arrays.asList(1));
		}
		tb.truncateTo(17);
		assertEquals(17, tb.getManagedRowCount());
		assertEquals(17, tb.getRowCount());
	}
	
	@Test public void testLobHandling() throws Exception {
		ElementSymbol x = new ElementSymbol("x"); //$NON-NLS-1$
		x.setType(DataTypeManager.DefaultDataClasses.CLOB);
		List<ElementSymbol> schema = Arrays.asList(x);
		TupleBuffer tb = BufferManagerFactory.getStandaloneBufferManager().createTupleBuffer(schema, "x", TupleSourceType.PROCESSOR); //$NON-NLS-1$
		tb.setInlineLobs(false);
		ClobType c = new ClobType(new SerialClob(new char[0]));
		TupleBatch batch = new TupleBatch(1, new List[] {Arrays.asList(c)});
		tb.addTupleBatch(batch, false);
		assertNotNull(tb.getLobReference(c.getReferenceStreamId()));
	}
	
}
