use of ddf.catalog.operation.impl.QueryResponseImpl in project ddf by codice.
the class RegistryIdPostIngestPluginTest method testInit.
@Test
public void testInit() throws Exception {
Metacard metacard = getDefaultMetacard();
QueryResponseImpl response = new QueryResponseImpl(null, Collections.singletonList(new ResultImpl(metacard)), 1L);
when(security.runAsAdminWithException(any(PrivilegedExceptionAction.class))).thenAnswer(invocation -> ((PrivilegedExceptionAction) invocation.getArguments()[0]).run());
when(security.runWithSubjectOrElevate(any(Callable.class))).thenAnswer(invocation -> ((Callable) invocation.getArguments()[0]).call());
when(framework.query(any(QueryRequest.class))).thenReturn(response);
registryIdPostIngestPlugin.init();
assertThat(registryIdPostIngestPlugin.getRegistryIds().size(), equalTo(1));
assertThat(registryIdPostIngestPlugin.getRegistryIds().iterator().next(), equalTo("regId"));
}
use of ddf.catalog.operation.impl.QueryResponseImpl in project ddf by codice.
the class Search method failSource.
public synchronized void failSource(String sourceId, Exception cause) {
QueryResponseImpl failedResponse = new QueryResponseImpl(null);
failedResponse.closeResultQueue();
failedResponse.setHits(0);
failedResponse.getProcessingDetails().add(new ProcessingDetailsImpl(sourceId, cause));
failedResponse.getProperties().put("elapsed", -1L);
updateStatus(sourceId, failedResponse);
}
use of ddf.catalog.operation.impl.QueryResponseImpl in project ddf by codice.
the class FederationStrategyTest method testFederateTwoSourcesOffsetTwoPageSizeThree.
/**
* Verify that a modified version of the mockQuery passed is used by the sources.
* <p>
* Special results handling done by OffsetResultsHandler.
*/
@Test
public void testFederateTwoSourcesOffsetTwoPageSizeThree() throws Exception {
LOGGER.debug("testFederate_TwoSources_OffsetTwo_PageSizeThree()");
// Test Setup
Query mockQuery = mock(QueryImpl.class);
// Offset of 2
when(mockQuery.getStartIndex()).thenReturn(2);
// Page size of 3
when(mockQuery.getPageSize()).thenReturn(3);
QueryRequest queryRequest = mock(QueryRequest.class);
when(queryRequest.getQuery()).thenReturn(mockQuery);
ArgumentCaptor<QueryRequest> argument1 = ArgumentCaptor.forClass(QueryRequest.class);
ArgumentCaptor<QueryRequest> argument2 = ArgumentCaptor.forClass(QueryRequest.class);
/**
* When using a modified mockQuery to mockQuery the sources, the desired offset and page size are
* NOT used. So, the results returned by each source start at index 1 and end at (offset +
* pageSize - 1).
*
* Number of results returned by each source = offset + pageSize - 1 4 = 2 + 3 - 1
*/
Result mockSource1Result1 = mock(Result.class);
Result mockSource1Result2 = mock(Result.class);
Result mockSource1Result3 = mock(Result.class);
Result mockSource1Result4 = mock(Result.class);
SourceResponse mockSource1Response = mock(SourceResponse.class);
List<Result> mockSource1Results = Arrays.asList(mockSource1Result1, mockSource1Result2, mockSource1Result3, mockSource1Result4);
when(mockSource1Response.getResults()).thenReturn(mockSource1Results);
Source mockSource1 = mock(Source.class);
when(mockSource1.query(any(QueryRequest.class))).thenReturn(mockSource1Response);
when(mockSource1.getId()).thenReturn("####### MOCK SOURCE 1.3 #######");
/**
* When using a modified mockQuery to mockQuery the sources, the desired offset and page size are
* NOT used. So, the results returned by each source start at index 1 and end at (offset +
* pageSize - 1).
*
* Number of results returned by each source = offset + pageSize - 1 4 = 2 + 3 - 1
*/
Result mockSource2Result1 = mock(Result.class);
Result mockSource2Result2 = mock(Result.class);
Result mockSource2Result3 = mock(Result.class);
Result mockSource2Result4 = mock(Result.class);
SourceResponse mockSource2Response = mock(SourceResponse.class);
List<Result> mockSource2Results = Arrays.asList(mockSource2Result1, mockSource2Result2, mockSource2Result3, mockSource2Result4);
when(mockSource2Response.getResults()).thenReturn(mockSource2Results);
Source mockSource2 = mock(Source.class);
when(mockSource2.query(any(QueryRequest.class))).thenReturn(mockSource2Response);
when(mockSource2.getId()).thenReturn("####### MOCK SOURCE 2.3 #######");
// Two sources
List<Source> sources = new ArrayList<Source>(2);
sources.add(mockSource1);
sources.add(mockSource2);
Result mockSortedResult1 = mock(Result.class);
Result mockSortedResult2 = mock(Result.class);
Result mockSortedResult3 = mock(Result.class);
Result mockSortedResult4 = mock(Result.class);
Result mockSortedResult5 = mock(Result.class);
Result mockSortedResult6 = mock(Result.class);
Result mockSortedResult7 = mock(Result.class);
Result mockSortedResult8 = mock(Result.class);
List<Result> mockSortedResults = Arrays.asList(mockSortedResult1, mockSortedResult2, mockSortedResult3, mockSortedResult4, mockSortedResult5, mockSortedResult6, mockSortedResult7, mockSortedResult8);
QueryResponseImpl mockOriginalResults = Mockito.mock(QueryResponseImpl.class);
// Return true for the number of mockSortedResults
Mockito.when(mockOriginalResults.hasMoreResults()).thenReturn(true, true, true, true, true, true, true, true, false);
Mockito.when(mockOriginalResults.getResults()).thenReturn(mockSortedResults);
// Returns the sorted results from both sources (4 + 4 = 8)
Mockito.when(mockOriginalResults.take()).thenReturn(mockSortedResult1, mockSortedResult2, mockSortedResult3, mockSortedResult4, mockSortedResult5, mockSortedResult6, mockSortedResult7, mockSortedResult8);
QueryResponseImpl offsetResultQueue = new QueryResponseImpl(queryRequest, null);
PowerMockito.whenNew(QueryResponseImpl.class).withArguments(queryRequest, (Map<String, Serializable>) null).thenReturn(mockOriginalResults, offsetResultQueue);
SortedFederationStrategy strategy = new SortedFederationStrategy(executor, new ArrayList<PreFederatedQueryPlugin>(), new ArrayList<PostFederatedQueryPlugin>());
// Run Test
QueryResponse federatedResponse = strategy.federate(sources, queryRequest);
// Make sure we've finished running the test
killAndWaitForExecutor();
// Verification
assertNotNull(federatedResponse);
verify(mockSource1).query(argument1.capture());
// The modified query should have a start index of 1 and an end index of offset + pageSize -
// 1
assertEquals(1, argument1.getValue().getQuery().getStartIndex());
assertEquals(4, argument1.getValue().getQuery().getPageSize());
verify(mockSource2).query(argument2.capture());
assertThat(mockQuery, not(argument2.getValue().getQuery()));
// The modified query should have a start index of 1 and an end index of offset + pageSize -
// 1
assertEquals(1, argument2.getValue().getQuery().getStartIndex());
assertEquals(4, argument2.getValue().getQuery().getPageSize());
/**
* Verify three results (page size) are returned. The sorted results returned by the sources
* do NOT have the offset and page size taken into account, so the offset and page size are
* applied to the sorted results in the OffsetResultHandler.
*
* Offset of 2 (start at result 2) and page size of 3 (end at result 4).
*/
LOGGER.debug("mockSortedResult1: {}", mockSortedResult1);
LOGGER.debug("mockSortedResult2: {}", mockSortedResult2);
LOGGER.debug("mockSortedResult3: {}", mockSortedResult3);
LOGGER.debug("mockSortedResult4: {}", mockSortedResult4);
assertEquals(3, federatedResponse.getResults().size());
assertEquals(mockSortedResult2, federatedResponse.getResults().get(0));
assertEquals(mockSortedResult3, federatedResponse.getResults().get(1));
assertEquals(mockSortedResult4, federatedResponse.getResults().get(2));
for (Result result : federatedResponse.getResults()) {
LOGGER.debug("federated response result: {}", result);
}
}
use of ddf.catalog.operation.impl.QueryResponseImpl in project ddf by codice.
the class FifoFederationStrategy method federate.
@Override
public QueryResponse federate(List<Source> sources, final QueryRequest queryRequest) {
Query originalQuery = queryRequest.getQuery();
int offset = originalQuery.getStartIndex();
// limit offset to max value
if (offset > this.maxStartIndex) {
offset = this.maxStartIndex;
}
final int pageSize = originalQuery.getPageSize();
QueryResponseImpl queryResponse = new QueryResponseImpl(queryRequest, null);
Map<Source, Future<SourceResponse>> futures = new HashMap<Source, Future<SourceResponse>>();
Query modifiedQuery = getModifiedQuery(originalQuery, sources.size(), offset, pageSize);
QueryRequest modifiedQueryRequest = new QueryRequestImpl(modifiedQuery, queryRequest.isEnterprise(), queryRequest.getSourceIds(), queryRequest.getProperties());
executeSourceQueries(sources, futures, modifiedQueryRequest);
int resultsToSkip = 0;
if (offset > 1 && sources.size() > 1) {
resultsToSkip = offset - 1;
}
queryExecutorService.submit(new FifoQueryMonitor(queryExecutorService, futures, queryResponse, modifiedQueryRequest.getQuery(), resultsToSkip));
return executePostFederationPlugins(queryResponse);
}
use of ddf.catalog.operation.impl.QueryResponseImpl in project ddf by codice.
the class SolrMetacardClientImpl method query.
@Override
public SourceResponse query(QueryRequest request) throws UnsupportedQueryException {
if (request == null || request.getQuery() == null) {
return new QueryResponseImpl(request, new ArrayList<Result>(), true, 0L);
}
SolrQuery query = getSolrQuery(request, filterDelegateFactory.newInstance(resolver));
long totalHits;
List<Result> results = new ArrayList<>();
try {
QueryResponse solrResponse = client.query(query, SolrRequest.METHOD.POST);
totalHits = solrResponse.getResults().getNumFound();
SolrDocumentList docs = solrResponse.getResults();
for (SolrDocument doc : docs) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("SOLR DOC: {}", doc.getFieldValue(Metacard.ID + SchemaFields.TEXT_SUFFIX));
}
ResultImpl tmpResult;
try {
tmpResult = createResult(doc);
} catch (MetacardCreationException e) {
throw new UnsupportedQueryException("Could not create metacard(s).", e);
}
results.add(tmpResult);
}
} catch (SolrServerException | IOException | SolrException e) {
throw new UnsupportedQueryException("Could not complete solr query.", e);
}
SourceResponse sourceResponse = new SourceResponseImpl(request, results, totalHits);
return sourceResponse;
}
Aggregations