Search in sources :

Example 1 with StepListener

use of org.pentaho.di.trans.step.StepListener in project pdi-dataservice-server-plugin by pentaho.

the class DataServiceExecutorTest method testExecuteQueryNoResults.

@Test
public void testExecuteQueryNoResults() throws Exception {
    SQL sql = new SQL("SELECT * FROM " + DATA_SERVICE_NAME);
    StepInterface serviceStep = serviceTrans.findRunThread(DATA_SERVICE_STEP);
    StepInterface resultStep = genTrans.findRunThread(RESULT_STEP_NAME);
    when(serviceTrans.getTransMeta().listParameters()).thenReturn(new String[0]);
    PushDownOptimizationMeta optimization = mock(PushDownOptimizationMeta.class);
    when(optimization.isEnabled()).thenReturn(true);
    dataService.getPushDownOptimizationMeta().add(optimization);
    IMetaStore metastore = mock(IMetaStore.class);
    DataServiceExecutor executor = new DataServiceExecutor.Builder(sql, dataService, context).serviceTrans(serviceTrans).sqlTransGenerator(sqlTransGenerator).genTrans(genTrans).metastore(metastore).build();
    ArgumentCaptor<String> objectIds = ArgumentCaptor.forClass(String.class);
    verify(serviceTrans).setContainerObjectId(objectIds.capture());
    when(serviceTrans.getContainerObjectId()).thenReturn(objectIds.getValue());
    verify(genTrans).setContainerObjectId(objectIds.capture());
    when(genTrans.getContainerObjectId()).thenReturn(objectIds.getValue());
    verify(serviceTrans).setMetaStore(metastore);
    verify(genTrans).setMetaStore(metastore);
    RowProducer sqlTransRowProducer = mock(RowProducer.class);
    when(genTrans.addRowProducer(INJECTOR_STEP_NAME, 0)).thenReturn(sqlTransRowProducer);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    // Start Execution
    executor.executeQuery(new DataOutputStream(outputStream));
    // Check header was written
    assertThat(outputStream.size(), greaterThan(0));
    outputStream.reset();
    InOrder genTransStartup = inOrder(genTrans, resultStep);
    InOrder serviceTransStartup = inOrder(optimization, serviceTrans, serviceStep);
    ArgumentCaptor<RowListener> listenerArgumentCaptor = ArgumentCaptor.forClass(RowListener.class);
    ArgumentCaptor<StepListener> resultStepListener = ArgumentCaptor.forClass(StepListener.class);
    ArgumentCaptor<TransListener> transListenerCaptor = ArgumentCaptor.forClass(TransListener.class);
    genTransStartup.verify(genTrans).addTransListener(transListenerCaptor.capture());
    genTransStartup.verify(genTrans).addRowProducer(INJECTOR_STEP_NAME, 0);
    genTransStartup.verify(resultStep).addStepListener(resultStepListener.capture());
    genTransStartup.verify(resultStep).addRowListener(listenerArgumentCaptor.capture());
    RowListener clientRowListener = listenerArgumentCaptor.getValue();
    genTransStartup.verify(genTrans).startThreads();
    serviceTransStartup.verify(optimization).activate(executor);
    serviceTransStartup.verify(serviceStep).addRowListener(listenerArgumentCaptor.capture());
    serviceTransStartup.verify(serviceTrans).startThreads();
    // Verify linkage
    RowListener serviceRowListener = listenerArgumentCaptor.getValue();
    assertNotNull(serviceRowListener);
    // Push row from service to sql Trans
    RowMetaInterface rowMeta = genTrans.getTransMeta().getStepFields(RESULT_STEP_NAME);
    doReturn(true).when(serviceTrans).isRunning();
    resultStepListener.getValue().stepFinished(genTrans, resultStep.getStepMeta(), resultStep);
    verify(serviceTrans).stopAll();
    // Verify Service Trans finished
    ArgumentCaptor<StepListener> serviceStepListener = ArgumentCaptor.forClass(StepListener.class);
    verify(serviceStep).addStepListener(serviceStepListener.capture());
    serviceStepListener.getValue().stepFinished(serviceTrans, serviceStep.getStepMeta(), serviceStep);
    verify(sqlTransRowProducer).finished();
    // finish transformation, so that the listener runs
    transListenerCaptor.getValue().transFinished(genTrans);
    InOrder writeRows = inOrder(rowMeta);
    ArgumentCaptor<DataOutputStream> streamCaptor = ArgumentCaptor.forClass(DataOutputStream.class);
    writeRows.verify(rowMeta).writeMeta(streamCaptor.capture());
    DataOutputStream dataOutputStream = streamCaptor.getValue();
    writeRows.verify(rowMeta, times(0)).writeData(same(dataOutputStream), argThat(arrayWithSize(1)));
    writeRows.verifyNoMoreInteractions();
    executor.waitUntilFinished();
    verify(serviceTrans).waitUntilFinished();
    verify(genTrans).waitUntilFinished();
}
Also used : RowProducer(org.pentaho.di.trans.RowProducer) InOrder(org.mockito.InOrder) DataOutputStream(java.io.DataOutputStream) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Matchers.anyString(org.mockito.Matchers.anyString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IMetaStore(org.pentaho.metastore.api.IMetaStore) RowListener(org.pentaho.di.trans.step.RowListener) SQL(org.pentaho.di.core.sql.SQL) StepInterface(org.pentaho.di.trans.step.StepInterface) PushDownOptimizationMeta(org.pentaho.di.trans.dataservice.optimization.PushDownOptimizationMeta) TransListener(org.pentaho.di.trans.TransListener) StepListener(org.pentaho.di.trans.step.StepListener) Test(org.junit.Test)

Example 2 with StepListener

use of org.pentaho.di.trans.step.StepListener in project pdi-dataservice-server-plugin by pentaho.

the class DataServiceExecutorTest method testExecuteQuery.

@Test
public void testExecuteQuery() throws Exception {
    SQL sql = new SQL("SELECT * FROM " + DATA_SERVICE_NAME);
    StepInterface serviceStep = serviceTrans.findRunThread(DATA_SERVICE_STEP);
    StepInterface resultStep = genTrans.findRunThread(RESULT_STEP_NAME);
    when(serviceTrans.getTransMeta().listParameters()).thenReturn(new String[0]);
    PushDownOptimizationMeta optimization = mock(PushDownOptimizationMeta.class);
    when(optimization.isEnabled()).thenReturn(true);
    dataService.getPushDownOptimizationMeta().add(optimization);
    IMetaStore metastore = mock(IMetaStore.class);
    DataServiceExecutor executor = new DataServiceExecutor.Builder(sql, dataService, context).serviceTrans(serviceTrans).sqlTransGenerator(sqlTransGenerator).genTrans(genTrans).metastore(metastore).build();
    ArgumentCaptor<String> objectIds = ArgumentCaptor.forClass(String.class);
    verify(serviceTrans).setContainerObjectId(objectIds.capture());
    when(serviceTrans.getContainerObjectId()).thenReturn(objectIds.getValue());
    verify(genTrans).setContainerObjectId(objectIds.capture());
    when(genTrans.getContainerObjectId()).thenReturn(objectIds.getValue());
    verify(serviceTrans).setMetaStore(metastore);
    verify(genTrans).setMetaStore(metastore);
    RowProducer sqlTransRowProducer = mock(RowProducer.class);
    when(genTrans.addRowProducer(INJECTOR_STEP_NAME, 0)).thenReturn(sqlTransRowProducer);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    // Start Execution
    executor.executeQuery(new DataOutputStream(outputStream));
    // Check header was written
    assertThat(outputStream.size(), greaterThan(0));
    outputStream.reset();
    InOrder genTransStartup = inOrder(genTrans, resultStep);
    InOrder serviceTransStartup = inOrder(optimization, serviceTrans, serviceStep);
    ArgumentCaptor<RowListener> listenerArgumentCaptor = ArgumentCaptor.forClass(RowListener.class);
    ArgumentCaptor<StepListener> resultStepListener = ArgumentCaptor.forClass(StepListener.class);
    ArgumentCaptor<TransListener> transListenerCaptor = ArgumentCaptor.forClass(TransListener.class);
    genTransStartup.verify(genTrans).addTransListener(transListenerCaptor.capture());
    genTransStartup.verify(genTrans).addRowProducer(INJECTOR_STEP_NAME, 0);
    genTransStartup.verify(resultStep).addStepListener(resultStepListener.capture());
    genTransStartup.verify(resultStep).addRowListener(listenerArgumentCaptor.capture());
    RowListener clientRowListener = listenerArgumentCaptor.getValue();
    genTransStartup.verify(genTrans).startThreads();
    serviceTransStartup.verify(optimization).activate(executor);
    serviceTransStartup.verify(serviceStep).addRowListener(listenerArgumentCaptor.capture());
    serviceTransStartup.verify(serviceTrans).startThreads();
    // Verify linkage
    RowListener serviceRowListener = listenerArgumentCaptor.getValue();
    assertNotNull(serviceRowListener);
    // Push row from service to sql Trans
    RowMetaInterface rowMeta = genTrans.getTransMeta().getStepFields(RESULT_STEP_NAME);
    Object[] data;
    for (int i = 0; i < 50; i++) {
        data = new Object[] { i };
        Object[] dataClone = { i };
        when(rowMeta.cloneRow(data)).thenReturn(dataClone);
        serviceRowListener.rowWrittenEvent(rowMeta, data);
        verify(sqlTransRowProducer).putRowWait(same(rowMeta), and(eq(dataClone), not(same(data))), any(Long.class), any(TimeUnit.class));
        verify(rowMeta).cloneRow(data);
    }
    doReturn(true).when(serviceTrans).isRunning();
    resultStepListener.getValue().stepFinished(genTrans, resultStep.getStepMeta(), resultStep);
    verify(serviceTrans).stopAll();
    // Verify Service Trans finished
    ArgumentCaptor<StepListener> serviceStepListener = ArgumentCaptor.forClass(StepListener.class);
    verify(serviceStep).addStepListener(serviceStepListener.capture());
    serviceStepListener.getValue().stepFinished(serviceTrans, serviceStep.getStepMeta(), serviceStep);
    verify(sqlTransRowProducer).finished();
    // Push row from service to sql Trans
    for (int i = 0; i < 50; i++) {
        Object[] row = { i };
        clientRowListener.rowWrittenEvent(rowMeta, row);
    }
    transListenerCaptor.getValue().transFinished(genTrans);
    InOrder writeRows = inOrder(rowMeta);
    ArgumentCaptor<DataOutputStream> streamCaptor = ArgumentCaptor.forClass(DataOutputStream.class);
    writeRows.verify(rowMeta).writeMeta(streamCaptor.capture());
    DataOutputStream dataOutputStream = streamCaptor.getValue();
    writeRows.verify(rowMeta, times(50)).writeData(same(dataOutputStream), argThat(arrayWithSize(1)));
    writeRows.verifyNoMoreInteractions();
    executor.waitUntilFinished();
    verify(serviceTrans).waitUntilFinished();
    verify(genTrans).waitUntilFinished();
}
Also used : RowProducer(org.pentaho.di.trans.RowProducer) DataOutputStream(java.io.DataOutputStream) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Matchers.anyString(org.mockito.Matchers.anyString) RowListener(org.pentaho.di.trans.step.RowListener) StepInterface(org.pentaho.di.trans.step.StepInterface) PushDownOptimizationMeta(org.pentaho.di.trans.dataservice.optimization.PushDownOptimizationMeta) TimeUnit(java.util.concurrent.TimeUnit) InOrder(org.mockito.InOrder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IMetaStore(org.pentaho.metastore.api.IMetaStore) SQL(org.pentaho.di.core.sql.SQL) TransListener(org.pentaho.di.trans.TransListener) StepListener(org.pentaho.di.trans.step.StepListener) Test(org.junit.Test)

Example 3 with StepListener

use of org.pentaho.di.trans.step.StepListener in project pdi-dataservice-server-plugin by pentaho.

the class CachedServiceTest method testObserveCancelled.

@Test
public void testObserveCancelled() throws Exception {
    DataServiceExecutor executor = dataServiceExecutor(BASE_QUERY);
    ServiceObserver observer = new ServiceObserver(executor);
    observer.install();
    assertThat(executor.getListenerMap().get(DataServiceExecutor.ExecutionPoint.READY), hasItem(observer));
    ListenableFuture<CachedService> failedObserver = new ServiceObserver(executor).install();
    try {
        failedObserver.get(1, TimeUnit.SECONDS);
        fail("Expecting an IllegalStateException");
    } catch (ExecutionException e) {
        assertThat(e.getCause(), instanceOf(IllegalStateException.class));
    }
    assertThat(observer.isDone(), is(false));
    ArgumentCaptor<StepListener> stepListener = ArgumentCaptor.forClass(StepListener.class);
    observer.run();
    verify(serviceStep).addStepListener(stepListener.capture());
    when(genTrans.getErrors()).thenReturn(1);
    stepListener.getValue().stepFinished(serviceTrans, mock(StepMeta.class), serviceStep);
    try {
        observer.get(1, TimeUnit.SECONDS);
        fail("Expecting an KettleException");
    } catch (ExecutionException e) {
        assertThat(e.getCause(), instanceOf(KettleException.class));
    }
}
Also used : DataServiceExecutor(org.pentaho.di.trans.dataservice.DataServiceExecutor) StepListener(org.pentaho.di.trans.step.StepListener) ExecutionException(java.util.concurrent.ExecutionException) StepMeta(org.pentaho.di.trans.step.StepMeta) Test(org.junit.Test)

Example 4 with StepListener

use of org.pentaho.di.trans.step.StepListener in project pdi-dataservice-server-plugin by pentaho.

the class CachedServiceTest method testObserve.

@Test
public void testObserve() throws Exception {
    DataServiceExecutor executor = dataServiceExecutor(BASE_QUERY);
    ServiceObserver observer = new ServiceObserver(executor);
    assertThat(observer.install(), sameInstance((ListenableFuture<CachedService>) observer));
    assertThat(executor.getListenerMap().get(DataServiceExecutor.ExecutionPoint.READY), hasItem(observer));
    observer.run();
    ArgumentCaptor<RowListener> rowListener = ArgumentCaptor.forClass(RowListener.class);
    ArgumentCaptor<StepListener> stepListener = ArgumentCaptor.forClass(StepListener.class);
    verify(serviceStep).addRowListener(rowListener.capture());
    verify(serviceStep).addStepListener(stepListener.capture());
    assertThat(observer.isDone(), is(false));
    for (RowMetaAndData metaAndData : testData) {
        rowListener.getValue().rowWrittenEvent(metaAndData.getRowMeta(), metaAndData.getData());
    }
    stepListener.getValue().stepFinished(serviceTrans, mock(StepMeta.class), serviceStep);
    assertThat(observer.isDone(), is(true));
    assertThat(observer.get().getRowMetaAndData(), equalTo((Iterable<RowMetaAndData>) testData));
    assertThat(observer.get().isComplete(), is(true));
}
Also used : RowMetaAndData(org.pentaho.di.core.RowMetaAndData) FluentIterable(com.google.common.collect.FluentIterable) DataServiceExecutor(org.pentaho.di.trans.dataservice.DataServiceExecutor) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) StepListener(org.pentaho.di.trans.step.StepListener) StepMeta(org.pentaho.di.trans.step.StepMeta) RowListener(org.pentaho.di.trans.step.RowListener) Test(org.junit.Test)

Example 5 with StepListener

use of org.pentaho.di.trans.step.StepListener in project pdi-dataservice-server-plugin by pentaho.

the class CachedServiceTest method testObservePartial.

@Test
public void testObservePartial() throws Exception {
    DataServiceExecutor executor = dataServiceExecutor(BASE_QUERY + " LIMIT 20");
    ServiceObserver observer = new ServiceObserver(executor);
    observer.install();
    assertThat(executor.getListenerMap().get(DataServiceExecutor.ExecutionPoint.READY), hasItem(observer));
    observer.run();
    ArgumentCaptor<RowListener> rowListener = ArgumentCaptor.forClass(RowListener.class);
    ArgumentCaptor<StepListener> stepListener = ArgumentCaptor.forClass(StepListener.class);
    verify(serviceStep).addRowListener(rowListener.capture());
    verify(serviceStep).addStepListener(stepListener.capture());
    assertThat(observer.isDone(), is(false));
    for (RowMetaAndData metaAndData : FluentIterable.from(testData)) {
        rowListener.getValue().rowWrittenEvent(metaAndData.getRowMeta(), metaAndData.getData());
    }
    when(serviceStep.isStopped()).thenReturn(true);
    stepListener.getValue().stepFinished(serviceTrans, mock(StepMeta.class), serviceStep);
    assertThat(observer.isDone(), is(true));
    CachedService cachedService = observer.get(5, TimeUnit.SECONDS);
    assertThat(cachedService.isComplete(), is(false));
    assertThat(cachedService.getRowMetaAndData(), equalTo(testData));
}
Also used : RowMetaAndData(org.pentaho.di.core.RowMetaAndData) DataServiceExecutor(org.pentaho.di.trans.dataservice.DataServiceExecutor) StepListener(org.pentaho.di.trans.step.StepListener) StepMeta(org.pentaho.di.trans.step.StepMeta) RowListener(org.pentaho.di.trans.step.RowListener) Test(org.junit.Test)

Aggregations

StepListener (org.pentaho.di.trans.step.StepListener)6 Test (org.junit.Test)5 RowListener (org.pentaho.di.trans.step.RowListener)4 StepMeta (org.pentaho.di.trans.step.StepMeta)4 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)3 DataServiceExecutor (org.pentaho.di.trans.dataservice.DataServiceExecutor)3 StepInterface (org.pentaho.di.trans.step.StepInterface)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 InOrder (org.mockito.InOrder)2 Matchers.anyString (org.mockito.Matchers.anyString)2 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)2 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)2 SQL (org.pentaho.di.core.sql.SQL)2 RowProducer (org.pentaho.di.trans.RowProducer)2 TransListener (org.pentaho.di.trans.TransListener)2 PushDownOptimizationMeta (org.pentaho.di.trans.dataservice.optimization.PushDownOptimizationMeta)2 IMetaStore (org.pentaho.metastore.api.IMetaStore)2 FluentIterable (com.google.common.collect.FluentIterable)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1