RuntimeKieServerDataSetProvider implements DataSetProvider { protected List<List> performQuery(RemoteDataSetDef def, ConsoleDataSetLookup lookup, QueryFilterSpec filterSpec) { KieServerConnectionInfo connectionInfo = connectionInfoProvider.verifiedConnectionInfo(def); int pageNumber = lookup.getRowOffset() / lookup.getNumberOfRows(); if (lookup.testMode() || connectionInfo.isReplaceQuery()) { QueryDefinition queryDefinition = QueryDefinition.builder() .name(lookup.getDataSetUUID()) .source(def.getDataSource()) .target(def.getQueryTarget()) .expression(def.getDbSQL()) .build(); QueryDefinition registered = queryClient.replaceQuery(connectionInfo, queryDefinition); if (registered.getColumns() != null) { addColumnsToDefinition(def, registered); } try { return queryClient.query(connectionInfo, lookup.getDataSetUUID(), filterSpec, pageNumber, lookup.getNumberOfRows()); } catch (Exception e) { queryClient.unregisterQuery(connectionInfo, lookup.getDataSetUUID()); throw new RuntimeException(e); } } else { if (def.getColumns() != null && def.getColumns().isEmpty()) { QueryDefinition query = queryClient.getQuery(connectionInfo, def.getUUID()); addColumnsToDefinition(def, query); } return queryClient.query(connectionInfo, lookup.getDataSetUUID(), filterSpec, pageNumber, lookup.getNumberOfRows()); } }  @Override DataSetProviderType getType(); @Override DataSetMetadata getDataSetMetadata(DataSetDef def); @Override DataSet lookupDataSet(DataSetDef def,
                                 DataSetLookup lookup); @Override boolean isDataSetOutdated(DataSetDef def); static final DataSetProviderType TYPE; }
@Test public void testPerformQueryTestMode() { QueryFilterSpec filterSpec = new QueryFilterSpec(); ConsoleDataSetLookup dataSetLookup = Mockito.mock(ConsoleDataSetLookup.class); when(dataSetLookup.testMode()).thenReturn(true); when(dataSetLookup.getNumberOfRows()).thenReturn(10); when(dataSetLookup.getRowOffset()).thenReturn(1); when(dataSetLookup.getDataSetUUID()).thenReturn(""); when(kieServerConnectionInfoProvider.verifiedConnectionInfo(dataSetDef)).thenReturn(connectionInfo); when(queryClient.replaceQuery(eq(connectionInfo), any())).thenReturn(definition); kieServerDataSetProvider.performQuery(dataSetDef, dataSetLookup, filterSpec); verify(dataSetLookup, times(1)).testMode(); verify(queryClient).replaceQuery(eq(connectionInfo), any()); verify(queryClient).query(eq(connectionInfo), anyString(), any(QueryFilterSpec.class), anyInt(), anyInt()); }
@Test public void testPerformQueryWithReplace() { QueryFilterSpec filterSpec = new QueryFilterSpec(); ConsoleDataSetLookup dataSetLookup = Mockito.mock(ConsoleDataSetLookup.class); when(dataSetLookup.testMode()).thenReturn(true); when(dataSetLookup.getNumberOfRows()).thenReturn(10); when(dataSetLookup.getRowOffset()).thenReturn(1); when(dataSetLookup.getDataSetUUID()).thenReturn(""); when(kieServerConnectionInfoProvider.verifiedConnectionInfo(dataSetDef)).thenReturn(connectionInfoWithQueryReplace); when(queryClient.replaceQuery(eq(connectionInfoWithQueryReplace), any())).thenReturn(definition); kieServerDataSetProvider.performQuery(dataSetDef, dataSetLookup, filterSpec); verify(dataSetLookup, times(1)).testMode(); verify(queryClient).replaceQuery(eq(connectionInfoWithQueryReplace), any()); verify(queryClient).query(eq(connectionInfoWithQueryReplace), anyString(), any(QueryFilterSpec.class), anyInt(), anyInt()); }
@Test public void testPerformQueryRegularMode() { QueryFilterSpec filterSpec = new QueryFilterSpec(); ConsoleDataSetLookup dataSetLookup = Mockito.mock(ConsoleDataSetLookup.class); when(dataSetLookup.testMode()).thenReturn(false); when(dataSetLookup.getNumberOfRows()).thenReturn(10); when(dataSetLookup.getRowOffset()).thenReturn(1); when(dataSetLookup.getDataSetUUID()).thenReturn(""); when(queryClient.replaceQuery(eq(connectionInfo), any())).thenReturn(definition); when(queryClient.getQuery(eq(connectionInfo), any())).thenReturn(definition); when(kieServerConnectionInfoProvider.verifiedConnectionInfo(dataSetDef)).thenReturn(connectionInfo); kieServerDataSetProvider.performQuery(dataSetDef, dataSetLookup, filterSpec); verify(dataSetLookup, times(1)).testMode(); verify(queryClient, times(0)).replaceQuery(eq(connectionInfo), any()); verify(queryClient).query(eq(connectionInfo), anyString(), any(QueryFilterSpec.class), anyInt(), anyInt()); }