use of ddf.catalog.operation.QueryRequest in project ddf by codice.
the class CatalogFrameworkQueryTest method testBeforeQuery.
@Test
public void testBeforeQuery() {
Calendar beforeCal = Calendar.getInstance();
beforeCal.add(Calendar.YEAR, 4);
Calendar card1Exp = Calendar.getInstance();
card1Exp.add(Calendar.YEAR, 1);
Calendar card2Exp = Calendar.getInstance();
card2Exp.add(Calendar.YEAR, 3);
List<Metacard> metacards = new ArrayList<Metacard>();
MetacardImpl newCard1 = new MetacardImpl();
newCard1.setId(null);
newCard1.setExpirationDate(card1Exp.getTime());
metacards.add(newCard1);
MetacardImpl newCard2 = new MetacardImpl();
newCard2.setId(null);
newCard2.setExpirationDate(card2Exp.getTime());
metacards.add(newCard2);
String mcId1 = null;
String mcId2 = null;
CreateResponse createResponse = null;
try {
createResponse = framework.create(new CreateRequestImpl(metacards, null));
} catch (IngestException e1) {
fail();
} catch (SourceUnavailableException e1) {
fail();
}
assertEquals(createResponse.getCreatedMetacards().size(), metacards.size());
for (Metacard curCard : createResponse.getCreatedMetacards()) {
if (curCard.getExpirationDate().equals(card1Exp.getTime())) {
mcId1 = curCard.getId();
} else {
mcId2 = curCard.getId();
}
assertNotNull(curCard.getId());
}
FilterFactory filterFactory = new FilterFactoryImpl();
Instant beforeInstant = new DefaultInstant(new DefaultPosition(beforeCal.getTime()));
QueryImpl query = new QueryImpl(filterFactory.before(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(beforeInstant)));
QueryRequest queryReq = new QueryRequestImpl(query, false);
try {
QueryResponse response = framework.query(queryReq);
assertEquals("Expecting return 2 results.", 2, response.getHits());
} catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
LOGGER.error("Failure", e);
fail();
}
beforeInstant = new DefaultInstant(new DefaultPosition(card2Exp.getTime()));
query = new QueryImpl(filterFactory.before(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(beforeInstant)));
queryReq = new QueryRequestImpl(query, false);
try {
QueryResponse response = framework.query(queryReq);
assertEquals("Before filter should return 1 result", 1, response.getHits());
assertEquals("Before filter should return metacard[" + mcId1 + "]", mcId1, response.getResults().get(0).getMetacard().getId());
} catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
LOGGER.error("Failure", e);
fail();
}
beforeInstant = new DefaultInstant(new DefaultPosition(card1Exp.getTime()));
query = new QueryImpl(filterFactory.before(filterFactory.property(Metacard.EXPIRATION), filterFactory.literal(beforeInstant)));
queryReq = new QueryRequestImpl(query, false);
try {
QueryResponse response = framework.query(queryReq);
assertEquals("Before filter should return 0 results.", 0, response.getHits());
} catch (UnsupportedQueryException | SourceUnavailableException | FederationException e) {
LOGGER.error("Failure", e);
fail();
}
}
use of ddf.catalog.operation.QueryRequest in project ddf by codice.
the class FanoutCatalogFrameworkTest method testReplaceDistance.
@Test
public void testReplaceDistance() {
QueryRequest request = new QueryRequestImpl(null);
List<Result> results = new ArrayList<Result>();
MetacardImpl newCard1 = new MetacardImpl();
ResultImpl result1 = new ResultImpl(newCard1);
result1.setRelevanceScore(RELEVANCE_SCORE);
result1.setDistanceInMeters(DISTANCE_SCORE);
MetacardImpl newCard2 = new MetacardImpl();
ResultImpl result2 = new ResultImpl(newCard2);
result2.setRelevanceScore(RELEVANCE_SCORE);
result2.setDistanceInMeters(DISTANCE_SCORE);
results.add(result1);
results.add(result2);
QueryResponse response = new QueryResponseImpl(request, results, 2);
QueryResponse newResponse = framework.getQueryOperations().replaceSourceId(response);
assertNotNull(newResponse);
List<Result> newResults = newResponse.getResults();
assertNotNull(newResults);
assertEquals(2, newResults.size());
Metacard card = new MetacardImpl();
// Make sure the relevance and distance score was copied over
for (Result newResult : newResults) {
card = newResult.getMetacard();
assertNotNull(card);
assertEquals(RELEVANCE_SCORE, newResult.getRelevanceScore());
assertEquals(DISTANCE_SCORE, newResult.getDistanceInMeters());
}
}
use of ddf.catalog.operation.QueryRequest in project ddf by codice.
the class CachingFederationStrategyTest method testFederateQueryUpdateCacheNotBlocking.
@Test
public void testFederateQueryUpdateCacheNotBlocking() throws Exception {
properties.put(QUERY_MODE, UPDATE_QUERY_MODE);
QueryRequest fedQueryRequest = new QueryRequestImpl(mockQuery, properties);
Source mockSource = mock(Source.class);
when(mockSource.query(any(QueryRequest.class))).thenReturn(mockResponse);
doNothing().when(cacheBulkProcessor).add(cacheArgs.capture());
QueryResponse federateResponse = strategy.federate(Arrays.asList(mockSource), fedQueryRequest);
assertThat(requestArgumentCaptor.getValue().getPropertyValue(QUERY_MODE), is(UPDATE_QUERY_MODE));
verify(mockSource).query(any(QueryRequest.class));
verify(cache, times(0)).query(any(QueryRequest.class));
// CacheBulkProcessor.add() is called
verify(cacheBulkProcessor).add(cacheArgs.getValue());
verifyCacheUpdated();
assertThat(federateResponse.getRequest().getQuery(), is(requestArgumentCaptor.getValue().getQuery()));
}
use of ddf.catalog.operation.QueryRequest in project ddf by codice.
the class FanoutCatalogFrameworkTest method testReplaceRelevance.
@Test
public void testReplaceRelevance() {
QueryRequest request = new QueryRequestImpl(null);
List<Result> results = new ArrayList<Result>();
MetacardImpl newCard1 = new MetacardImpl();
ResultImpl result1 = new ResultImpl(newCard1);
result1.setRelevanceScore(RELEVANCE_SCORE);
MetacardImpl newCard2 = new MetacardImpl();
ResultImpl result2 = new ResultImpl(newCard2);
result2.setRelevanceScore(RELEVANCE_SCORE);
results.add(result1);
results.add(result2);
QueryResponse response = new QueryResponseImpl(request, results, 2);
QueryResponse newResponse = framework.getQueryOperations().replaceSourceId(response);
assertNotNull(newResponse);
List<Result> newResults = newResponse.getResults();
assertNotNull(newResponse);
assertEquals(2, newResults.size());
Metacard card = new MetacardImpl();
// Make sure the relevance score was copied over
for (Result newResult : newResults) {
card = newResult.getMetacard();
assertNotNull(card);
assertEquals(RELEVANCE_SCORE, newResult.getRelevanceScore());
}
}
use of ddf.catalog.operation.QueryRequest in project ddf by codice.
the class CatalogFrameworkImplTest method testPreQueryStopExecution.
@Test(expected = FederationException.class)
public void testPreQueryStopExecution() throws UnsupportedQueryException, FederationException, SourceUnavailableException {
SourcePoller poller = mock(SourcePoller.class);
when(poller.getCachedSource(isA(Source.class))).thenReturn(null);
MockMemoryProvider provider = new MockMemoryProvider("Provider", "Provider", "v1.0", "DDF", new HashSet<ContentType>(), true, new Date());
FederationStrategy federationStrategy = mock(FederationStrategy.class);
QueryRequest request = mock(QueryRequest.class);
when(request.getQuery()).thenReturn(mock(Query.class));
PreQueryPlugin stopQueryPlugin = new PreQueryPlugin() {
@Override
public QueryRequest process(QueryRequest input) throws PluginExecutionException, StopProcessingException {
throw new StopProcessingException("Testing that the framework will stop the query.");
}
};
FrameworkProperties frameworkProperties = new FrameworkProperties();
frameworkProperties.setSourcePoller(poller);
frameworkProperties.setPreQuery(Arrays.asList(stopQueryPlugin));
frameworkProperties.setFederationStrategy(federationStrategy);
frameworkProperties.setCatalogProviders(Collections.singletonList(provider));
CatalogFrameworkImpl framework = createFramework(frameworkProperties);
framework.query(request);
}
Aggregations