// EXTENSION
// ISSUE: DVCORE-2440
// FIXED BY: Roman Pedchenko
// DATE: 2014.10.18
// ADOPTED FROM: https://issues.jboss.org/browse/TEIID-2176,https://issues.jboss.org/browse/TEIID-2177
/*
 * 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$
 *  
 */

package org.teiid.translator.jdbc;

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

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.teiid.dqp.internal.datamgr.FakeExecutionContextImpl;
import org.teiid.language.Command;
import org.teiid.metadata.RuntimeMetadata;
import org.teiid.translator.ExecutionContext;
import org.teiid.translator.ResultSetExecution;

@SuppressWarnings("nls")
public class TestJDBCDirectQueryExecution {
	
	@Test public void testSelectExecution() throws Exception {
		Command command = TranslationHelper.helpTranslate(TranslationHelper.BQT_VDB, "call native('select * from Source')"); //$NON-NLS-1$
		Connection connection = Mockito.mock(Connection.class);
		Statement stmt = Mockito.mock(Statement.class);
		ResultSet rs = Mockito.mock(ResultSet.class);
		ResultSetMetaData rsm = Mockito.mock(ResultSetMetaData.class);
		
		Mockito.when(stmt.getUpdateCount()).thenReturn(-1);
		Mockito.when(stmt.getResultSet()).thenReturn(rs);
		Mockito.when(rs.getMetaData()).thenReturn(rsm);
		Mockito.when(rsm.getColumnCount()).thenReturn(2);
		Mockito.when(connection.createStatement()).thenReturn(stmt); //$NON-NLS-1$
		Mockito.when(stmt.execute("select * from Source")).thenReturn(true);
		Mockito.when(rs.next()).thenReturn(true);
		Mockito.when(rs.getObject(1)).thenReturn(5);
		Mockito.when(rs.getObject(2)).thenReturn("five");
		Mockito.when(connection.getMetaData()).thenReturn(Mockito.mock(DatabaseMetaData.class));
		
		JDBCExecutionFactory ef = new JDBCExecutionFactory();
		ef.setSupportsNativeQueries(true);
		ResultSetExecution execution = (ResultSetExecution)ef.createExecution(command,  Mockito.mock(ExecutionContext.class), Mockito.mock(RuntimeMetadata.class), connection);
		execution.execute();
		assertArrayEquals(new Object[] {5, "five"}, (Object[])execution.next().get(0));
	}

	@Test public void testPrepareExecution() throws Exception {
		Command command = TranslationHelper.helpTranslate(TranslationHelper.BQT_VDB, "call native('select * from Source where e1 = ?', 2)"); //$NON-NLS-1$
		Connection connection = Mockito.mock(Connection.class);
		PreparedStatement stmt = Mockito.mock(PreparedStatement.class);
		ResultSet rs = Mockito.mock(ResultSet.class);
		ResultSetMetaData rsm = Mockito.mock(ResultSetMetaData.class);
		
		Mockito.when(stmt.getUpdateCount()).thenReturn(-1);
		Mockito.when(stmt.getResultSet()).thenReturn(rs);
		Mockito.when(stmt.execute()).thenReturn(true);
		Mockito.when(rs.getMetaData()).thenReturn(rsm);
		Mockito.when(rsm.getColumnCount()).thenReturn(2);
		Mockito.when(connection.prepareStatement("select * from Source where e1 = ?")).thenReturn(stmt); //$NON-NLS-1$
		Mockito.when(rs.next()).thenReturn(true);
		Mockito.when(rs.getObject(1)).thenReturn(5);
		Mockito.when(rs.getObject(2)).thenReturn("five");
		Mockito.when(connection.getMetaData()).thenReturn(Mockito.mock(DatabaseMetaData.class));
		
		JDBCExecutionFactory ef = new JDBCExecutionFactory();
		ef.setSupportsNativeQueries(true);
		ResultSetExecution execution = (ResultSetExecution)ef.createExecution(command,  new FakeExecutionContextImpl(), Mockito.mock(RuntimeMetadata.class), connection);
		execution.execute();
		Mockito.verify(stmt).setObject(1, 2);
		assertArrayEquals(new Object[] {5, "five"}, (Object[])execution.next().get(0));
	}
	
	@Test public void testPrepareUpdateCount() throws Exception {
		Command command = TranslationHelper.helpTranslate(TranslationHelper.BQT_VDB, "call native('update source set e1=? where e2 = ?', 2, 'foo')"); //$NON-NLS-1$
		Connection connection = Mockito.mock(Connection.class);
		PreparedStatement stmt = Mockito.mock(PreparedStatement.class);
		ResultSet rs = Mockito.mock(ResultSet.class);
		ResultSetMetaData rsm = Mockito.mock(ResultSetMetaData.class);
		
		Mockito.when(stmt.getUpdateCount()).thenReturn(-1);
		Mockito.when(stmt.getUpdateCount()).thenReturn(5);
		Mockito.when(stmt.execute()).thenReturn(false);
		Mockito.when(rs.getMetaData()).thenReturn(rsm);
		Mockito.when(rsm.getColumnCount()).thenReturn(2);
		Mockito.when(connection.prepareStatement("update source set e1=? where e2 = ?")).thenReturn(stmt); //$NON-NLS-1$
		Mockito.when(connection.getMetaData()).thenReturn(Mockito.mock(DatabaseMetaData.class));
		
		JDBCExecutionFactory ef = new JDBCExecutionFactory();
		ef.setSupportsNativeQueries(true);
		ResultSetExecution execution = (ResultSetExecution)ef.createExecution(command,  new FakeExecutionContextImpl(), Mockito.mock(RuntimeMetadata.class), connection);
		execution.execute();
		assertArrayEquals(new Object[] {5}, (Object[])execution.next().get(0));
	}	
}
// END EXTENSION
