Skip to content

Commit

Permalink
[PDI-15453] JobEntryWaitForSQL Wrong Error Count
Browse files Browse the repository at this point in the history
Belt and Suspenders due to PDI-15437
Test Cases added
  • Loading branch information
Matt Tucker committed Jul 12, 2016
1 parent acdcabe commit 4ac4ac5
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 14 deletions.
1 change: 1 addition & 0 deletions build.xml
Expand Up @@ -891,6 +891,7 @@
<test todir="${testreports.xml.dir}" name="org.pentaho.di.job.entries.ftpsget.FTPSConnectionTest" haltonerror="false"/>
<test todir="${testreports.xml.dir}" name="org.pentaho.di.job.entries.ftpsget.JobEntryFTPSGetTest" haltonerror="false"/>
<test todir="${testreports.xml.dir}" name="org.pentaho.di.job.entries.ftpput.JobEntryFTPPUTTest" haltonerror="false"/>
<test todir="${testreports.xml.dir}" name="org.pentaho.di.job.entries.waitforsql.JobEntryWaitForSQLTest" haltonerror="false"/>

<!-- Some extra things to run -->
<test todir="${testreports.xml.dir}" name="org.pentaho.di.job.entries.copyfiles.CopyFilesTest"
Expand Down
Expand Up @@ -22,9 +22,6 @@

package org.pentaho.di.job.entries.waitforsql;

import org.pentaho.di.job.entry.validator.AndValidator;
import org.pentaho.di.job.entry.validator.JobEntryValidatorUtils;

import java.util.ArrayList;
import java.util.List;

Expand All @@ -45,6 +42,8 @@
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.job.entry.JobEntryBase;
import org.pentaho.di.job.entry.JobEntryInterface;
import org.pentaho.di.job.entry.validator.AndValidator;
import org.pentaho.di.job.entry.validator.JobEntryValidatorUtils;
import org.pentaho.di.repository.ObjectId;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.resource.ResourceEntry;
Expand Down Expand Up @@ -226,6 +225,11 @@ public String getMaximumTimeout() {
return maximumTimeout;
}

/**
* Set how long the job entry may test the connection for a success result
*
* @param maximumTimeout Number of seconds to wait for success
*/
public void setMaximumTimeout( String maximumTimeout ) {
this.maximumTimeout = maximumTimeout;
}
Expand Down Expand Up @@ -338,6 +342,22 @@ public boolean isUnconditional() {
return false;
}

// Visible for testing purposes
protected void checkConnection() throws KettleDatabaseException {
// check connection
// connect and disconnect
Database dbchecked = null;
try {
dbchecked = new Database( this, connection );
dbchecked.shareVariablesWith( this );
dbchecked.connect( parentJob.getTransactionId(), null );
} finally {
if ( dbchecked != null ) {
dbchecked.disconnect();
}
}
}

@Override
public Result execute( Result previousResult, int nr ) {
Result result = previousResult;
Expand Down Expand Up @@ -381,16 +401,7 @@ public Result execute( Result previousResult, int nr ) {
try {
// check connection
// connect and disconnect
Database dbchecked = null;
try {
dbchecked = new Database( this, connection );
dbchecked.shareVariablesWith( this );
dbchecked.connect( parentJob.getTransactionId(), null );
} finally {
if ( dbchecked != null ) {
dbchecked.disconnect();
}
}
checkConnection();

// starttime (in seconds)
long timeStart = System.currentTimeMillis() / 1000;
Expand Down Expand Up @@ -487,7 +498,7 @@ public Result execute( Result previousResult, int nr ) {
return result;
}

private boolean SQLDataOK( Result result, long nrRowsLimit, String realSchemaName, String realTableName,
protected boolean SQLDataOK( Result result, long nrRowsLimit, String realSchemaName, String realTableName,
String customSQL ) throws KettleException {
String countStatement = null;
long rowsCount = 0;
Expand Down
128 changes: 128 additions & 0 deletions test/org/pentaho/di/job/entries/waitforsql/JobEntryWaitForSQLTest.java
@@ -0,0 +1,128 @@
/*! ******************************************************************************
*
* Pentaho Data Integration
*
* Copyright (C) 2002-2016 by Pentaho : http://www.pentaho.com
*
*******************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/

package org.pentaho.di.job.entries.waitforsql;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyString;
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.when;

import java.util.UUID;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.Result;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.logging.LogChannelInterface;
import org.pentaho.di.job.Job;

public class JobEntryWaitForSQLTest {

DatabaseMeta mockDbMeta;
Job parentJob;
LogChannelInterface parentJobLogChannel;

@BeforeClass
public static void setUpBeforeClass() throws KettleException {
KettleEnvironment.init( false );
}

@Before
public void setup() {
mockDbMeta = mock( DatabaseMeta.class );
parentJob = spy( new Job() );
when( parentJob.isStopped() ).thenReturn( false );
}

@Test
public void testGoodResult() throws KettleException {
JobEntryWaitForSQL entry = spy( new JobEntryWaitForSQL() );
doReturn( true ).when( entry ).SQLDataOK( any( Result.class ), anyLong(), anyString(), anyString(), anyString() );
doNothing().when( entry ).checkConnection();

entry.setDatabase( mockDbMeta );
entry.successCondition = JobEntryWaitForSQL.SUCCESS_CONDITION_ROWS_COUNT_GREATER;
entry.rowsCountValue = "0";
entry.setMaximumTimeout( "1" ); // Seconds
entry.setCheckCycleTime( "1" ); // Seconds
entry.tablename = UUID.randomUUID().toString();
entry.setParentJob( parentJob );

Result result = entry.execute( new Result(), 0 );
assertNotNull( result );
assertTrue( result.getResult() );
assertEquals( 0, result.getNrErrors() );
}

@Test
public void testBadResult() throws KettleException {
JobEntryWaitForSQL entry = spy( new JobEntryWaitForSQL() );
doReturn( false ).when( entry ).SQLDataOK( any( Result.class ), anyLong(), anyString(), anyString(), anyString() );
doNothing().when( entry ).checkConnection();

entry.setDatabase( mockDbMeta );
entry.successCondition = JobEntryWaitForSQL.SUCCESS_CONDITION_ROWS_COUNT_GREATER;
entry.rowsCountValue = "0";
entry.setMaximumTimeout( "1" ); // Seconds
entry.setCheckCycleTime( "1" ); // Seconds
entry.tablename = UUID.randomUUID().toString();
entry.setParentJob( parentJob );

Result result = entry.execute( new Result(), 0 );
assertNotNull( result );
assertFalse( result.getResult() );
assertEquals( 1, result.getNrErrors() );
}

@Test
public void testSuccessOnTimeout() throws KettleException {
JobEntryWaitForSQL entry = spy( new JobEntryWaitForSQL() );
doReturn( false ).when( entry ).SQLDataOK( any( Result.class ), anyLong(), anyString(), anyString(), anyString() );
doNothing().when( entry ).checkConnection();

entry.setDatabase( mockDbMeta );
entry.successCondition = JobEntryWaitForSQL.SUCCESS_CONDITION_ROWS_COUNT_GREATER;
entry.rowsCountValue = "0";
entry.setMaximumTimeout( "1" ); // Seconds
entry.setCheckCycleTime( "1" ); // Seconds
entry.tablename = UUID.randomUUID().toString();
entry.setSuccessOnTimeout( true );
entry.setParentJob( parentJob );

Result result = entry.execute( new Result(), 0 );
assertNotNull( result );
assertTrue( result.getResult() );
assertEquals( 0, result.getNrErrors() );
}
}

0 comments on commit 4ac4ac5

Please sign in to comment.