MDXBaseComponent extends ComponentBase implements IDataComponent, IPreparedComponent { protected boolean runQuery( final IPentahoConnection localConnection, final String rawQuery ) { try { if ( localConnection == null ) { error( Messages.getInstance().getErrorString( "MDXBaseComponent.ERROR_0008_NO_CONNECTION" ) ); return false; } if ( !localConnection.initialized() ) { error( Messages.getInstance().getErrorString( "MDXBaseComponent.ERROR_0008_NO_CONNECTION" ) ); return false; } if ( rawQuery == null ) { error( Messages.getInstance().getErrorString( "MDXBaseComponent.ERROR_0001_QUERY_NOT_SPECIFIED", getActionName() ) ); return false; } if ( ComponentBase.debug ) { debug( Messages.getInstance().getString( "MDXBaseComponent.DEBUG_RUNNING_QUERY", rawQuery ) ); } IPentahoResultSet resultSet = localConnection.executeQuery( rawQuery ); if ( resultSet != null && resultSet instanceof MDXResultSet ) { boolean formattedCellValues = false; if ( isDefinedInput( FORMATTED_CELL_VALUES ) ) { formattedCellValues = getInputBooleanValue( FORMATTED_CELL_VALUES, false ); } ( (MDXResultSet) resultSet ).setFormattedCellValues( formattedCellValues ); } rSet = resultSet; if ( resultSet != null ) { MdxQueryAction mdxQueryAction = (MdxQueryAction) getActionDefinition(); IActionOutput actionOutput = mdxQueryAction.getOutputResultSet(); if ( actionOutput != null ) { actionOutput.setValue( resultSet ); } return true; } else { error( Messages.getInstance().getErrorString( "MDXBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName() ) ); localConnection.close(); return false; } } catch ( Exception e ) { error( Messages.getInstance().getErrorString( "MDXBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName() ), e ); } return false; }  @Override abstract boolean validateSystemSettings(); @Override abstract Log getLogger(); IPentahoResultSet getResultSet(); @Override void done(); void dispose(); IPentahoConnection shareConnection(); IPentahoResultSet executePrepared( final Map preparedParams ); IPentahoConnection getDatasourceConnection(); @Override boolean init(); static final String FORMATTED_CELL_VALUES; }
@Test public void testRunQuery_nullConnection() throws Exception { assertFalse( mdxBaseComponent.runQuery( null, "select * from table" ) ); }
@Test public void testRunQuery_connectionNotInit() throws Exception { assertFalse( mdxBaseComponent.runQuery( conn, "select * from table" ) ); }
@Test public void testRunQuery_nullQuery() throws Exception { when( conn.initialized() ).thenReturn( true ); assertFalse( mdxBaseComponent.runQuery( conn, null ) ); }
@Test public void testRunQuery() throws Exception { String query = "select * from table"; when( conn.executeQuery( query ) ).thenReturn( resultSet ); when( conn.initialized() ).thenReturn( true ); mdxBaseComponent.setActionDefinition( queryAction ); when( queryAction.getOutputResultSet() ).thenReturn( outputResultSet ); assertTrue( mdxBaseComponent.runQuery( conn, query ) ); verify( outputResultSet ).setValue( resultSet ); }
@Test public void testRunQuery_nullResultSet() throws Exception { String query = "select * from table"; when( conn.executeQuery( query ) ).thenReturn( null ); when( conn.initialized() ).thenReturn( true ); assertFalse( mdxBaseComponent.runQuery( conn, query ) ); verify( conn ).close(); }
@Test public void testRunQuery_mdxResultSet() throws Exception { String query = "select * from table"; when( conn.executeQuery( query ) ).thenReturn( mdxResultSet ); when( conn.initialized() ).thenReturn( true ); mdxBaseComponent.setRuntimeContext( runtimeContext ); HashSet<String> inputs = new HashSet<>(); inputs.add( MDXBaseComponent.FORMATTED_CELL_VALUES ); when( runtimeContext.getInputNames() ).thenReturn( inputs ); mdxBaseComponent.setActionDefinition( queryAction ); when( queryAction.getOutputResultSet() ).thenReturn( outputResultSet ); assertTrue( mdxBaseComponent.runQuery( conn, query ) ); verify( outputResultSet ).setValue( mdxResultSet ); }