Search in sources :

Example 31 with Condition

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");
}
Also used : Condition(org.pentaho.di.core.Condition) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Matchers.anyString(org.mockito.Matchers.anyString) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) SQL(org.pentaho.di.core.sql.SQL) Test(org.junit.Test)

Example 32 with Condition

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());
}
Also used : SQLCondition(org.pentaho.di.core.sql.SQLCondition) Condition(org.pentaho.di.core.Condition) Test(org.junit.Test)

Example 33 with Condition

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());
}
Also used : SQLCondition(org.pentaho.di.core.sql.SQLCondition) Condition(org.pentaho.di.core.Condition) OptimizationImpactInfo(org.pentaho.di.trans.dataservice.optimization.OptimizationImpactInfo) SQL(org.pentaho.di.core.sql.SQL) Test(org.junit.Test)

Example 34 with Condition

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());
}
Also used : SQLCondition(org.pentaho.di.core.sql.SQLCondition) Condition(org.pentaho.di.core.Condition) Test(org.junit.Test)

Example 35 with Condition

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());
}
Also used : Condition(org.pentaho.di.core.Condition) ParameterGenerationTest.newCondition(org.pentaho.di.trans.dataservice.optimization.paramgen.ParameterGenerationTest.newCondition) OptimizationImpactInfo(org.pentaho.di.trans.dataservice.optimization.OptimizationImpactInfo) JUnitMatchers.containsString(org.junit.matchers.JUnitMatchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) StepMeta(org.pentaho.di.trans.step.StepMeta) TableInputMeta(org.pentaho.di.trans.steps.tableinput.TableInputMeta) Test(org.junit.Test)

Aggregations

Condition (org.pentaho.di.core.Condition)42 Test (org.junit.Test)14 Matchers.anyString (org.mockito.Matchers.anyString)6 RowMeta (org.pentaho.di.core.row.RowMeta)6 SQLCondition (org.pentaho.di.core.sql.SQLCondition)6 StepMeta (org.pentaho.di.trans.step.StepMeta)6 KettleException (org.pentaho.di.core.exception.KettleException)5 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)5 SQL (org.pentaho.di.core.sql.SQL)5 ParameterGenerationTest.newCondition (org.pentaho.di.trans.dataservice.optimization.paramgen.ParameterGenerationTest.newCondition)5 KettleStepException (org.pentaho.di.core.exception.KettleStepException)4 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)4 ValueMetaAndData (org.pentaho.di.core.row.ValueMetaAndData)4 JUnitMatchers.containsString (org.junit.matchers.JUnitMatchers.containsString)3 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2