MongodbInputParameterGeneration implements ParameterGenerationService { @Override public OptimizationImpactInfo preview( Condition pushDownCondition, ParameterGeneration parameterGeneration, StepInterface stepInterface ) { OptimizationImpactInfo impactInfo = new OptimizationImpactInfo( stepInterface.getStepname() ); try { String jsonQuery = getJsonQuery( stepInterface ); impactInfo.setQueryBeforeOptimization( jsonQuery ); if ( pushDownCondition == null ) { impactInfo.setModified( false ); return impactInfo; } String predicate = getMongodbPredicate( pushDownCondition, getFieldMappings( stepInterface ) ).asFilterCriteria(); String modifiedQuery = parameterGeneration.setQueryParameter( jsonQuery, predicate ); if ( !modifiedQuery.equals( jsonQuery ) ) { impactInfo.setQueryAfterOptimization( modifiedQuery ); impactInfo.setModified( true ); } } catch ( KettleException e ) { log.logDetailed( String.format( "Unable to optimize step '%s'", stepInterface.getStepname() ) ); impactInfo.setModified( false ); impactInfo.setErrorMsg( e ); } return impactInfo; } MongodbInputParameterGeneration( ValueMetaResolver resolver );  MongodbInputParameterGeneration(); @Override void pushDown( Condition condition, ParameterGeneration parameterGeneration, StepInterface stepInterface ); @Override String getParameterDefault(); @Override OptimizationImpactInfo preview( Condition pushDownCondition,
                                         ParameterGeneration parameterGeneration, StepInterface stepInterface );  }
@Test public void testPreviewWithModification() { OptimizationImpactInfo impact = preview( condition, parameterGeneration, stepInterface ); assertThat( impact.getStepName(), equalTo( MOCK_STEPNAME ) ); assertThat( impact.getQueryBeforeOptimization(), equalTo( TEST_JSON_QUERY ) ); assertThat( impact.getQueryAfterOptimization(), equalTo( "after optimization" ) ); assertTrue( impact.isModified() ); assertThat( impact.getErrorMsg(), equalTo( "" ) ); }
@Test public void testPreviewWithoutModification() { when( parameterGeneration.getParameterName() ).thenReturn( "DifferentParamName" ); when( parameterGeneration.setQueryParameter( TEST_JSON_QUERY, "mockedFilter" ) ) .thenReturn( TEST_JSON_QUERY ); OptimizationImpactInfo impact = preview( condition, parameterGeneration, stepInterface ); assertThat( impact.getStepName(), equalTo( MOCK_STEPNAME ) ); assertThat( impact.getQueryBeforeOptimization(), equalTo( TEST_JSON_QUERY ) ); assertThat( impact.getQueryAfterOptimization(), equalTo( "" ) ); assertFalse( impact.isModified() ); assertThat( impact.getErrorMsg(), equalTo( "" ) ); }
@Test public void testPreviewWithNoQuery() throws KettleException { when( stepInterface.getStepMeta().getXML() ).thenReturn( "<empty/>" ); when( parameterGeneration.setQueryParameter( "", "mockedFilter" ) ) .thenReturn( "" ); when( parameterGeneration.getParameterName() ).thenReturn( "paramName" ); OptimizationImpactInfo impact = preview( condition, parameterGeneration, stepInterface ); assertThat( impact.getStepName(), equalTo( MOCK_STEPNAME ) ); assertThat( impact.getQueryBeforeOptimization(), equalTo( "<no query>" ) ); assertThat( impact.getQueryAfterOptimization(), equalTo( "" ) ); assertFalse( impact.isModified() ); assertThat( impact.getErrorMsg(), equalTo( "" ) ); }
@Test public void testPreviewWithConversionFailure() throws KettleException { when( mongodbPredicate.asFilterCriteria() ).thenThrow( new PushDownOptimizationException( "FAILURE" ) ); OptimizationImpactInfo impact = preview( condition, parameterGeneration, stepInterface ); assertThat( impact.getStepName(), equalTo( MOCK_STEPNAME ) ); assertThat( impact.getQueryBeforeOptimization(), equalTo( TEST_JSON_QUERY ) ); assertFalse( impact.isModified() ); assertThat( impact.getErrorMsg(), containsString( "FAILURE" ) ); }
@Test public void testQueryBeforeOptimizationWithConversionFailure() throws KettleException { when( mongodbPredicate.asFilterCriteria() ).thenThrow( new PushDownOptimizationException( "FAILURE" ) ); OptimizationImpactInfo impact = preview( condition, parameterGeneration, stepInterface ); assertThat( impact.getQueryBeforeOptimization(), equalTo( TEST_JSON_QUERY ) ); }