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));
}
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));
}
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));
}
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));
}
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);
}
}
Aggregations