Skip to content

Commit

Permalink
fix: fix failing unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
befc committed Nov 29, 2024
1 parent a37ddc4 commit 453a87d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ protected void initWithJNDI( final String jndiName ) {
}
}

private void initOther( Properties props ) {
void initOther( Properties props ) {
String connectionName = props.getProperty( IPentahoConnection.CONNECTION_NAME );
if ( connectionName != null && !connectionName.isEmpty() ) {
boolean isTestConnection = isTestConnection( connectionName );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,28 @@
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import org.pentaho.commons.connection.IPentahoConnection;
import org.pentaho.database.model.IDatabaseConnection;
import org.pentaho.platform.api.data.DBDatasourceServiceException;
import org.pentaho.platform.api.data.IDBDatasourceService;
import org.pentaho.platform.api.engine.ILogger;
import org.pentaho.platform.api.engine.IPentahoObjectFactory;
import org.pentaho.platform.api.engine.IPentahoSession;
import org.pentaho.platform.api.engine.ObjectFactoryException;
import org.pentaho.platform.engine.core.system.PentahoSystem;

import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.mockito.ArgumentMatcher;
Expand Down Expand Up @@ -87,21 +94,38 @@ public boolean matches( final Class<?> arg ) {

@Test
public void testConnect() {
// Create a spy of SQLConnection to verify method calls
SQLConnection sqlc = spy( new SQLConnection() );
Properties props;

props = new Properties();
// Mock the logger to avoid actual logging
ILogger logger = mock( ILogger.class );
doNothing().when( logger ).error( anyString(), any( Throwable.class ) );
sqlc.logger = logger;

// Test connection using JNDI name
Properties props = new Properties();
props.put( IPentahoConnection.JNDI_NAME_KEY, "test" );
assertTrue( "JNDI Test", sqlc.connect( props ) );
verify( sqlc ).initWithJNDI( "test" );
assertTrue( sqlc.initialized() );

// Test connection without JNDI name
props = new Properties();
doNothing().when( sqlc ).close();
doNothing().when( sqlc ).init( nullable( String.class ), nullable( String.class ), nullable( String.class ), nullable( String.class ) );
assertTrue( "NonPool Test", sqlc.connect( props ) );
verify( sqlc ).initOther( props );
assertTrue( sqlc.initialized() );

doNothing().when( sqlc ).initDataSource( nullable( IDatabaseConnection.class ), true );
// Test connection with a mock IDatabaseConnection
IDatabaseConnection mockDatabaseConnection = mock( IDatabaseConnection.class );
doNothing().when( sqlc ).initDataSource( eq( mockDatabaseConnection ), eq( false ) );

// Initialize the data source and test connection
props.put( IPentahoConnection.CONNECTION_NAME, "test" );
sqlc.initDataSource( mockDatabaseConnection, false );
assertTrue( "Pool Test", sqlc.connect( props ) );
verify( sqlc ).initDataSource( eq( mockDatabaseConnection ), eq( false ) );
assertTrue( sqlc.initialized() );
}
}

0 comments on commit 453a87d

Please sign in to comment.