Search in sources :

Example 6 with DataServiceExecutor

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

the class CachedServiceTest method testAnswersQuery.

@Test
public void testAnswersQuery() throws Exception {
    String query = "SELECT ID, A FROM " + SERVICE_NAME + " WHERE B = 1";
    String limit = " LIMIT 20";
    String offset = " OFFSET 10";
    String groupBy = " GROUP BY A";
    DataServiceExecutor executor = dataServiceExecutor(query + limit + offset);
    CachedService complete = CachedService.complete(testData);
    CachedService partial = partial(executor);
    // An identical query always works
    assertThat(complete.answersQuery(executor), is(true));
    assertThat(partial.answersQuery(executor), is(true));
    // A partial will answer a query with fewer requested rows
    assertThat(partial.answersQuery(dataServiceExecutor(query + limit)), is(true));
    when(sqlTransGenerator.getRowLimit()).thenReturn(10);
    assertThat(partial.answersQuery(dataServiceExecutor(query)), is(true));
    when(sqlTransGenerator.getRowLimit()).thenReturn(0);
    assertThat(partial.answersQuery(dataServiceExecutor(query)), is(false));
    // Only complete sets will answer a GROUP BY query
    assertThat(partial.answersQuery(dataServiceExecutor(query + groupBy)), is(false));
    assertThat(complete.answersQuery(dataServiceExecutor(query + groupBy)), is(true));
    String distinctQuery = "SELECT DISTINCT ID FROM " + SERVICE_NAME + limit;
    assertThat(partial.answersQuery(dataServiceExecutor(distinctQuery)), is(false));
    assertThat(complete.answersQuery(dataServiceExecutor(distinctQuery)), is(true));
    String aggregateQuery = "SELECT count(*) FROM " + SERVICE_NAME;
    assertThat(partial.answersQuery(dataServiceExecutor(aggregateQuery)), is(false));
    assertThat(complete.answersQuery(dataServiceExecutor(aggregateQuery)), is(true));
}
Also used : DataServiceExecutor(org.pentaho.di.trans.dataservice.DataServiceExecutor) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Test(org.junit.Test)

Example 7 with DataServiceExecutor

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

the class ServiceCacheTest method testActivateObserve.

@Test
public void testActivateObserve() throws Exception {
    DataServiceExecutor executor = dataServiceExecutor("SELECT * FROM MOCK_SERVICE ORDER BY ID");
    CachedService.CacheKey key = CachedService.CacheKey.create(executor);
    CachedService cachedService = CachedService.complete(ImmutableList.<RowMetaAndData>of());
    ServiceObserver observer = mock(ServiceObserver.class);
    when(factory.createObserver(executor)).thenReturn(observer);
    when(observer.install()).thenReturn(Futures.immediateFuture(cachedService));
    when(cache.get(any(CachedService.CacheKey.class))).thenReturn(null);
    when(cache.putIfAbsent(key.withoutOrder(), cachedService)).thenReturn(true);
    assertThat(serviceCache.activate(executor, serviceStep), is(false));
    verify(cache).putIfAbsent(key.withoutOrder(), cachedService);
    verifyNoMoreInteractions(ignoreStubs(cache));
}
Also used : DataServiceExecutor(org.pentaho.di.trans.dataservice.DataServiceExecutor) Test(org.junit.Test)

Example 8 with DataServiceExecutor

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

the class ServiceCacheTest method testTimeToLiveCacheValid.

@Test
public void testTimeToLiveCacheValid() throws KettleException {
    CachedService existingCache = mock(CachedService.class);
    DataServiceExecutor executor = dataServiceExecutor("SELECT * FROM MOCK_SERVICE");
    // TTL of service cache == template config
    serviceCache.setTimeToLive(Long.toString(DEFAULT_TTL));
    CachedService.CacheKey key = CachedService.CacheKey.create(executor);
    when(cache.get(key)).thenReturn(existingCache);
    when(existingCache.answersQuery(executor)).thenReturn(true);
    assertThat(serviceCache.getAvailableCache(executor).get(key), equalTo(existingCache));
}
Also used : DataServiceExecutor(org.pentaho.di.trans.dataservice.DataServiceExecutor) Test(org.junit.Test)

Example 9 with DataServiceExecutor

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

the class ServiceCacheTest method testObserveUpdateExisting.

@Test
public void testObserveUpdateExisting() throws Exception {
    DataServiceExecutor executor = dataServiceExecutor("SELECT * FROM MOCK_SERVICE WHERE A = 2");
    CachedService.CacheKey key = CachedService.CacheKey.create(executor);
    CachedService existingCache = mock(CachedService.class);
    when(cache.get(key.withoutCondition())).thenReturn(null);
    when(cache.get(key)).thenReturn(existingCache);
    when(existingCache.answersQuery(executor)).thenReturn(false);
    ServiceObserver observer = mock(ServiceObserver.class);
    when(factory.createObserver(executor)).thenReturn(observer);
    CachedService cachedService = CachedService.complete(ImmutableList.<RowMetaAndData>of());
    when(observer.install()).thenReturn(Futures.immediateFuture(cachedService));
    when(cache.putIfAbsent(key, cachedService)).thenReturn(false);
    when(cache.replace(key, existingCache, cachedService)).thenReturn(true);
    assertThat(serviceCache.activate(executor, serviceStep), is(false));
    verify(cache).replace(key, existingCache, cachedService);
    verifyNoMoreInteractions(ignoreStubs(cache));
}
Also used : DataServiceExecutor(org.pentaho.di.trans.dataservice.DataServiceExecutor) Test(org.junit.Test)

Example 10 with DataServiceExecutor

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

the class ServiceCacheTest method testObserveWithoutCondition.

@Test
public void testObserveWithoutCondition() throws Exception {
    String select = "SELECT * FROM MOCK_SERVICE";
    String selectWithCondition = select + " WHERE A = 2";
    // These queries should always ignore WHERE conditions
    for (Map.Entry<String, List<PushDownOptimizationMeta>> testEntry : ImmutableMultimap.<String, List<PushDownOptimizationMeta>>builder().put(select, ImmutableList.of(serviceCacheOpt)).put(select, ImmutableList.of(serviceCacheOpt, otherOpt)).put(selectWithCondition, ImmutableList.of(serviceCacheOpt)).put(selectWithCondition, ImmutableList.<PushDownOptimizationMeta>of()).build().entries()) {
        dataServiceMeta.setPushDownOptimizationMeta(testEntry.getValue());
        DataServiceExecutor executor = dataServiceExecutor(testEntry.getKey());
        CachedService cachedService = CachedService.complete(ImmutableList.<RowMetaAndData>of());
        CachedService.CacheKey key = CachedService.CacheKey.create(executor).withoutCondition();
        ServiceObserver observer = mock(ServiceObserver.class);
        when(factory.createObserver(executor)).thenReturn(observer);
        when(observer.install()).thenReturn(Futures.immediateFuture(cachedService));
        when(cache.get(any(CachedService.CacheKey.class))).thenReturn(null);
        when(cache.putIfAbsent(key, cachedService)).thenReturn(true);
        assertThat(serviceCache.activate(executor, serviceStep), is(false));
        try {
            verify(cache).putIfAbsent(key, cachedService);
            verify(cache).getConfiguration(CompleteConfiguration.class);
            verifyNoMoreInteractions(ignoreStubs(cache));
        } catch (AssertionError e) {
            throw new AssertionError(testEntry.toString(), e);
        }
        reset((Cache<CachedService.CacheKey, CachedService>) cache);
    }
}
Also used : DataServiceExecutor(org.pentaho.di.trans.dataservice.DataServiceExecutor) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

DataServiceExecutor (org.pentaho.di.trans.dataservice.DataServiceExecutor)23 Test (org.junit.Test)19 SQL (org.pentaho.di.core.sql.SQL)6 HashMap (java.util.HashMap)4 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)4 KettleException (org.pentaho.di.core.exception.KettleException)4 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)3 StepListener (org.pentaho.di.trans.step.StepListener)3 StepMeta (org.pentaho.di.trans.step.StepMeta)3 IMetaStore (org.pentaho.metastore.api.IMetaStore)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Executor (java.util.concurrent.Executor)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)2 ValueMetaInteger (org.pentaho.di.core.row.value.ValueMetaInteger)2 RowProducer (org.pentaho.di.trans.RowProducer)2 DataServiceContext (org.pentaho.di.trans.dataservice.DataServiceContext)2