CachedService implements Serializable { public boolean answersQuery( DataServiceExecutor executor ) { SQL sql = executor.getSql(); if ( isComplete() ) { return true; } SQLFields selectFields = sql.getSelectFields(); SQLFields groupFields = sql.getGroupFields(); if ( selectFields.hasAggregates() || selectFields.isDistinct() || !groupFields.getFields().isEmpty() ) { if ( ranking.or( Integer.MAX_VALUE ) < Integer.MAX_VALUE ) { return false; } } boolean outRanks = true; if ( this.ranking.isPresent() ) { outRanks &= this.ranking.get() >= calculateRank( executor ); } if ( this.rankingServiceRows.isPresent() ) { outRanks &= this.rankingServiceRows.get() >= calculateServiceRowRank( executor ); } return outRanks; } private  CachedService( List<RowMetaAndData> rowMetaAndData, Optional<Integer> ranking,  Optional<Integer> ranking2 ); static CachedService complete( List<RowMetaAndData> rowMetaAndData ); static CachedService partial( List<RowMetaAndData> rowMetaAndData, DataServiceExecutor executor ); List<RowMetaAndData> getRowMetaAndData(); Optional<Integer> getRanking(); @Override String toString(); boolean isComplete(); boolean answersQuery( DataServiceExecutor executor );  }
@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 ); assertThat( complete.answersQuery( executor ), is( true ) ); assertThat( partial.answersQuery( executor ), is( true ) ); 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 ) ); 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 ) ); }