use of org.pentaho.di.core.Condition in project pdi-dataservice-server-plugin by pentaho.
the class DataServiceExecutorTest method testQueryWithParams.
@Test
public void testQueryWithParams() throws Exception {
String sql = "SELECT * FROM " + DATA_SERVICE_NAME + " WHERE PARAMETER('foo') = 'bar' AND PARAMETER('baz') = 'bop'";
final SQL theSql = new SQL(sql);
DataServiceExecutor executor = new DataServiceExecutor.Builder(theSql, dataService, context).serviceTrans(serviceTrans).sqlTransGenerator(sqlTransGenerator).genTrans(genTrans).parameters(ImmutableMap.of("BUILD_PARAM", "TRUE")).build();
List<Condition> conditions = theSql.getWhereCondition().getCondition().getChildren();
assertEquals(2, conditions.size());
for (Condition condition : conditions) {
// verifies that each of the parameter conditions have their left and right valuename
// set to null after executor initialization. This prevents failure due to non-existent
// fieldnames being present.
assertNull(condition.getLeftValuename());
assertNull(condition.getRightValuename());
}
assertThat(executor.getParameters(), equalTo((Map<String, String>) ImmutableMap.of("baz", "bop", "foo", "bar", "BUILD_PARAM", "TRUE")));
// Late parameter modification is okay
executor.getParameters().put("AFTER_BUILD", "TRUE");
// Parameters should not be set on the trans until execute
verify(serviceTrans, never()).setParameterValue(anyString(), anyString());
executor.executeQuery();
// verify that the parameter values were correctly extracted from the WHERE and applied
verify(serviceTrans).setParameterValue("foo", "bar");
verify(serviceTrans).setParameterValue("baz", "bop");
verify(serviceTrans).setParameterValue("BUILD_PARAM", "TRUE");
verify(serviceTrans).setParameterValue("AFTER_BUILD", "TRUE");
}
use of org.pentaho.di.core.Condition in project pdi-dataservice-server-plugin by pentaho.
the class ParameterGenerationTest method testConditionMapping.
@Test
public void testConditionMapping() throws Exception {
Condition condition, verify;
// In examples below, fields A, B, and C are mapped
// ( A & Z ) -> ( A )
condition = newCondition("A_src");
condition.addCondition(newCondition(AND, "Z"));
assertNotNull(condition.toString(), verify = paramGen.mapConditionFields(condition));
assertEquals("A_tgt", verify.getLeftValuename());
assertEquals(0, verify.getChildren().size());
// ( A & ( B & C ) ) -> ( A & ( B & C ) )
condition = newCondition("A_src");
condition.addCondition(newCondition(AND, "B_src"));
condition.getCondition(1).addCondition(newCondition(AND, "C_src"));
assertNotNull(condition.toString(), verify = paramGen.mapConditionFields(condition));
assertEquals("A_tgt", verify.getCondition(0).getLeftValuename());
assertEquals(AND, verify.getCondition(1).getOperator());
assertEquals("B_tgt", verify.getCondition(1).getCondition(0).getLeftValuename());
assertEquals(AND, verify.getCondition(1).getCondition(1).getOperator());
assertEquals("C_tgt", verify.getCondition(1).getCondition(1).getLeftValuename());
// ( ( A & B ) | ( A & C ) ) -> ( ( A & B ) | ( A & C ) )
condition = new Condition();
condition.addCondition(newCondition("A_src"));
condition.getCondition(0).addCondition(newCondition(AND, "B_src"));
condition.addCondition(newCondition(OR, "A_src"));
condition.getCondition(1).addCondition(newCondition(AND, "C_src"));
assertNotNull(condition.toString(), verify = paramGen.mapConditionFields(condition));
assertEquals("A_tgt", verify.getCondition(0).getCondition(0).getLeftValuename());
assertEquals(AND, verify.getCondition(0).getCondition(1).getOperator());
assertEquals("B_tgt", verify.getCondition(0).getCondition(1).getLeftValuename());
assertEquals(OR, verify.getCondition(1).getOperator());
assertEquals("A_tgt", verify.getCondition(1).getCondition(0).getLeftValuename());
assertEquals(AND, verify.getCondition(1).getCondition(1).getOperator());
assertEquals("C_tgt", verify.getCondition(1).getCondition(1).getLeftValuename());
// ( ( A | B ) & ( A | C ) ) -> ( ( A | B ) & ( A | C ) )
condition = new Condition();
condition.addCondition(newCondition("A_src"));
condition.getCondition(0).addCondition(newCondition(OR, "B_src"));
condition.addCondition(newCondition(AND, "A_src"));
condition.getCondition(1).addCondition(newCondition(OR, "C_src"));
assertNotNull(condition.toString(), verify = paramGen.mapConditionFields(condition));
assertEquals("A_tgt", verify.getCondition(0).getCondition(0).getLeftValuename());
assertEquals(OR, verify.getCondition(0).getCondition(1).getOperator());
assertEquals("B_tgt", verify.getCondition(0).getCondition(1).getLeftValuename());
assertEquals(AND, verify.getCondition(1).getOperator());
assertEquals("A_tgt", verify.getCondition(1).getCondition(0).getLeftValuename());
assertEquals(OR, verify.getCondition(1).getCondition(1).getOperator());
assertEquals("C_tgt", verify.getCondition(1).getCondition(1).getLeftValuename());
// ( ( A | B ) & ( C | Z ) & ( X & Y ) ) -> ( A | B )
condition = new Condition();
condition.addCondition(newCondition("A_src"));
condition.getCondition(0).addCondition(newCondition(OR, "B_src"));
condition.addCondition(newCondition(AND, "C_src"));
condition.getCondition(1).addCondition(newCondition(OR, "Z"));
condition.addCondition(newCondition(AND, "X"));
condition.getCondition(2).addCondition(newCondition(AND, "Y"));
assertNotNull(condition.toString(), verify = paramGen.mapConditionFields(condition));
assertEquals("A_tgt", verify.getCondition(0).getCondition(0).getLeftValuename());
assertEquals(OR, verify.getCondition(0).getCondition(1).getOperator());
assertEquals("B_tgt", verify.getCondition(0).getCondition(1).getLeftValuename());
assertEquals(1, verify.getChildren().size());
// ( A & ( B | Z ) ) -> A
condition = newCondition("A_src");
condition.addCondition(newCondition(AND, "B_src"));
condition.getCondition(1).addCondition(newCondition(OR, "Z"));
assertNotNull(condition.toString(), verify = paramGen.mapConditionFields(condition));
assertEquals("A_tgt", verify.getLeftValuename());
assertEquals(0, verify.getChildren().size());
// ( Z || A ) -> none
condition = newCondition("Z");
condition.addCondition(newCondition(OR, "A_src"));
assertEquals(condition.toString(), null, paramGen.mapConditionFields(condition));
// ( A || Z ) -> none
condition = newCondition("A_src");
condition.addCondition(newCondition(OR, "Z"));
assertNull(condition.toString(), paramGen.mapConditionFields(condition));
// !( A & Z ) -> none
condition = newCondition("A_src");
condition.addCondition(newCondition(AND, "Z"));
condition.negate();
assertNull(condition.toString(), paramGen.mapConditionFields(condition));
// !( A || Z ) -> !A
condition = newCondition("A_src");
condition.addCondition(newCondition(OR, "Z"));
condition.negate();
assertNotNull(condition.toString(), verify = paramGen.mapConditionFields(condition));
assertEquals("A_tgt", verify.getLeftValuename());
assertEquals(0, verify.getChildren().size());
assertTrue(verify.isNegated());
}
use of org.pentaho.di.core.Condition in project pdi-dataservice-server-plugin by pentaho.
the class ParameterGenerationTest method testPreview.
@Test
public void testPreview() throws KettleValueException, PushDownOptimizationException {
Condition condition = newCondition("A_src", "A_value");
SQL query = mockSql(condition);
when(executor.getSql()).thenReturn(query);
OptimizationImpactInfo optImpact = mock(OptimizationImpactInfo.class);
when(service.preview(any(Condition.class), same(paramGen), same(stepInterface))).thenReturn(optImpact);
assertEquals(optImpact, paramGen.preview(executor, stepInterface));
ArgumentCaptor<Condition> pushDownCaptor = ArgumentCaptor.forClass(Condition.class);
verify(service).preview(pushDownCaptor.capture(), same(paramGen), same(stepInterface));
Condition verify = pushDownCaptor.getValue();
assertEquals("A_tgt", verify.getLeftValuename());
assertEquals("A_value", verify.getRightExactString());
}
use of org.pentaho.di.core.Condition in project pdi-dataservice-server-plugin by pentaho.
the class ParameterGenerationTest method testOperatorHandlingWithPartiallyApplicableCondition.
@Test
public void testOperatorHandlingWithPartiallyApplicableCondition() throws Exception {
// Verifies no dangling AND/OR operators are left around after unmapped fields
// have been removed.
Condition condition, verify;
// In examples below, fields A, B, and C are mapped
// ( Z & A ) -> ( A )
condition = newCondition("Z");
condition.addCondition(newCondition(AND, "A_src"));
assertNotNull(condition.toString(), verify = paramGen.mapConditionFields(condition));
assertEquals("A_tgt", verify.getLeftValuename());
assertEquals(0, verify.getChildren().size());
assertEquals("Only one predicate, should be OPERATOR_NONE", Condition.OPERATOR_NONE, verify.getOperator());
// ( Z & ( A | B ) ) -> ( A | B )
condition = newCondition("Z");
condition.addCondition(newCondition(AND, "A_src"));
condition.getCondition(1).addCondition(newCondition(OR, "B_src"));
assertNotNull(condition.toString(), paramGen.mapConditionFields(condition));
// ( Z & A & B ) -> ( A & B )
// 3 flat conditions
condition = newCondition("Z");
condition.addCondition(newCondition(AND, "A_src"));
condition.addCondition(newCondition(AND, "B_src"));
assertNotNull(condition.toString(), paramGen.mapConditionFields(condition));
// ( B & ( Z & A ) ) -> ( B & A )
// unmapped condition as first child in nested expression
condition = newCondition("B_src");
condition.addCondition(newCondition(AND, "Z"));
condition.getCondition(1).addCondition(newCondition(AND, "A_src"));
assertNotNull(condition.toString(), paramGen.mapConditionFields(condition));
// !( Z || A || B ) -> !( A || B )
// 3 flat conditions, negated
condition = newCondition("Z");
condition.addCondition(newCondition(OR, "A_src"));
condition.addCondition(newCondition(OR, "B_src"));
condition.negate();
assertNotNull(condition.toString(), verify = paramGen.mapConditionFields(condition));
assertEquals(2, verify.getChildren().size());
assertEquals("A_tgt", verify.getCondition(0).getLeftValuename());
assertEquals(OR, verify.getCondition(1).getOperator());
assertEquals("B_tgt", verify.getCondition(1).getLeftValuename());
assertTrue(verify.isNegated());
}
use of org.pentaho.di.core.Condition in project pdi-dataservice-server-plugin by pentaho.
the class TableInputParameterGenerationTest method testPreview.
@Test
public void testPreview() throws KettleValueException, PushDownOptimizationException {
ParameterGeneration param = factory.createPushDown();
param.setParameterName("param");
Condition employeeFilter = newCondition("fooField", "barValue");
when(stepInterface.getStepMeta()).thenReturn(mock(StepMeta.class));
TableInputMeta mockTableInput = mock(TableInputMeta.class);
String origQuery = "SELECT * FROM TABLE WHERE ${param}";
when(mockTableInput.getSQL()).thenReturn("SELECT * FROM TABLE WHERE ${param}");
when(stepInterface.getStepMeta().getStepMetaInterface()).thenReturn(mockTableInput);
when(stepInterface.getStepname()).thenReturn("testStepName");
OptimizationImpactInfo impact = service.preview(employeeFilter, param, stepInterface);
assertThat(impact.getQueryBeforeOptimization(), equalTo(origQuery));
assertThat(impact.getStepName(), equalTo("testStepName"));
assertThat(impact.getQueryAfterOptimization(), equalTo("Parameterized SQL: SELECT * FROM TABLE WHERE fooField = ? {1: barValue}"));
assertTrue(impact.isModified());
}
Aggregations