use of ddf.catalog.operation.QueryResponse in project ddf by codice.
the class RemoveCommandTest method getQueryResponseBatch.
private List<QueryResponse> getQueryResponseBatch(int batchSize, int total) {
Queue<Result> results = new ArrayDeque<>();
for (int i = 1; i <= total; i++) {
MetacardImpl metacard = new MetacardImpl();
metacard.setId(i + "");
results.add(new ResultImpl(metacard));
}
List<QueryResponse> queryResponses = new ArrayList<>();
while (!results.isEmpty()) {
List<Result> batchList = new ArrayList<>();
for (int i = 0; i < batchSize; i++) {
Result result = results.poll();
if (result == null) {
break;
}
batchList.add(result);
}
queryResponses.add(new QueryResponseImpl(null, batchList, total));
}
// Add one empty response list to the end
queryResponses.add(new QueryResponseImpl(null, Collections.emptyList(), 0));
return queryResponses;
}
use of ddf.catalog.operation.QueryResponse in project ddf by codice.
the class InspectCommandTest method testMultivaluedAttr.
@Test
public void testMultivaluedAttr() throws Exception {
Metacard metacard = getMockMultiValuedMetacard();
// mock out catalog framework
QueryResponse mockQueryResponse = mock(QueryResponse.class);
Result mockResult = mock(Result.class);
doReturn(metacard).when(mockResult).getMetacard();
doReturn(ImmutableList.of(mockResult)).when(mockQueryResponse).getResults();
CatalogFramework mockCatalogFramework = mock(CatalogFramework.class);
doReturn(mockQueryResponse).when(mockCatalogFramework).query(any());
InspectCommand command = new InspectCommand();
command.catalogFramework = mockCatalogFramework;
command.id = "id";
command.filterBuilder = new GeotoolsFilterBuilder();
command.executeWithSubject();
String output = consoleOutput.getOutput();
assertThat(output, containsString(VALUE_1));
assertThat(output, containsString(VALUE_2));
assertThat(output, containsString(VALUE_3));
}
use of ddf.catalog.operation.QueryResponse in project ddf by codice.
the class ReplicateCommandTest method setUp.
@Before
public void setUp() throws Exception {
catalogFramework = mock(CatalogFramework.class);
replicateCommand = new ReplicateCommand();
final Session session = mock(Session.class);
when(session.readLine(anyString(), isNull())).thenReturn("sourceId1");
replicateCommand.session = session;
replicateCommand.catalogFramework = catalogFramework;
replicateCommand.filterBuilder = new GeotoolsFilterBuilder();
when(mockSession.getKeyboard()).thenReturn(mockIS);
when(catalogFramework.getSourceIds()).thenReturn(SOURCE_IDS);
when(catalogFramework.query(isA(QueryRequest.class))).thenAnswer(invocation -> {
Object[] args = invocation.getArguments();
QueryRequest request = (QueryRequest) args[0];
QueryResponse mockQueryResponse = mock(QueryResponse.class);
when(mockQueryResponse.getHits()).thenReturn(Long.valueOf(HITS));
when(mockQueryResponse.getResults()).thenReturn(getResultList(Math.min(replicateCommand.batchSize, HITS - request.getQuery().getStartIndex() + 1)));
return mockQueryResponse;
});
when(catalogFramework.create(isA(CreateRequest.class))).thenAnswer(invocation -> {
Object[] args = invocation.getArguments();
CreateRequest request = (CreateRequest) args[0];
when(mockCreateResponse.getCreatedMetacards()).thenReturn(request.getMetacards());
return mockCreateResponse;
});
}
use of ddf.catalog.operation.QueryResponse in project ddf by codice.
the class OpenSearchEndpoint method executeQuery.
/**
* Executes the OpenSearchQuery and formulates the response
*
* @param format - of the results in the response
* @param query - the query to execute
* @param ui -the ui information to use to format the results
* @return the response on the query
*/
private Response executeQuery(String format, OpenSearchQuery query, UriInfo ui, Map<String, Serializable> properties) {
Response response = null;
String queryFormat = format;
MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
List<String> subscriptionList = queryParams.get(Constants.SUBSCRIPTION_KEY);
LOGGER.trace("Attempting to execute query: {}", query);
try {
Map<String, Serializable> arguments = new HashMap<>();
String organization = framework.getOrganization();
String url = ui.getRequestUri().toString();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("organization: {}", organization);
LOGGER.trace("url: {}", url);
}
arguments.put("organization", organization);
arguments.put("url", url);
// interval
if (CollectionUtils.isNotEmpty(subscriptionList)) {
String subscription = subscriptionList.get(0);
LOGGER.trace("Subscription: {}", subscription);
arguments.put(Constants.SUBSCRIPTION_KEY, subscription);
List<String> intervalList = queryParams.get(UPDATE_QUERY_INTERVAL);
if (CollectionUtils.isNotEmpty(intervalList)) {
arguments.put(UPDATE_QUERY_INTERVAL, intervalList.get(0));
}
}
if (StringUtils.isEmpty(queryFormat)) {
queryFormat = DEFAULT_FORMAT;
}
if (query.getFilter() != null) {
QueryRequest queryRequest = new QueryRequestImpl(query, query.isEnterprise(), query.getSiteIds(), properties);
QueryResponse queryResponse;
LOGGER.trace("Sending query");
queryResponse = framework.query(queryRequest);
// pass in the format for the transform
BinaryContent content = framework.transform(queryResponse, queryFormat, arguments);
response = Response.ok(content.getInputStream(), content.getMimeTypeValue()).build();
} else {
// No query was specified
QueryRequest queryRequest = new QueryRequestImpl(query, query.isEnterprise(), query.getSiteIds(), null);
// Create a dummy QueryResponse with zero results
QueryResponseImpl queryResponseQueue = new QueryResponseImpl(queryRequest, new ArrayList<>(), 0);
// pass in the format for the transform
BinaryContent content = framework.transform(queryResponseQueue, queryFormat, arguments);
if (null != content) {
response = Response.ok(content.getInputStream(), content.getMimeTypeValue()).build();
}
}
} catch (UnsupportedQueryException ce) {
LOGGER.debug("Unsupported query", ce);
response = Response.status(Response.Status.BAD_REQUEST).entity(wrapStringInPreformattedTags("Unsupported query")).build();
} catch (CatalogTransformerException e) {
LOGGER.debug("Error transforming response", e);
response = Response.serverError().entity(wrapStringInPreformattedTags("Error transforming response")).build();
} catch (FederationException e) {
LOGGER.debug("Error executing query", e);
response = Response.serverError().entity(wrapStringInPreformattedTags("Error executing query")).build();
} catch (SourceUnavailableException e) {
LOGGER.debug("Error executing query because the underlying source was unavailable.", e);
response = Response.serverError().entity(wrapStringInPreformattedTags("Error executing query because the underlying source was unavailable.")).build();
} catch (RuntimeException e) {
// Account for any runtime exceptions and send back a server error
// this prevents full stacktraces returning to the client
// this allows for a graceful server error to be returned
LOGGER.debug("RuntimeException on executing query", e);
response = Response.serverError().entity(wrapStringInPreformattedTags("RuntimeException on executing query")).build();
}
return response;
}
use of ddf.catalog.operation.QueryResponse in project ddf by codice.
the class CatalogMetricsTest method catalogPostQueryLatencyMetric.
@Test
public void catalogPostQueryLatencyMetric() throws Exception {
Iterable<Tag> tags = Tags.of("successful", "true");
QueryRequest request = mock(QueryRequest.class);
QueryResponse response = mock(QueryResponse.class);
when(response.getRequest()).thenReturn(request);
when(request.getPropertyValue("metrics.catalog.operation.start")).thenReturn(System.currentTimeMillis() - 1000);
catalogMetrics.process(response);
assertThat(meterRegistry.summary("ddf.catalog.query.latency", tags).count(), is(1L));
assertThat(meterRegistry.summary("ddf.catalog.query.latency", tags).max(), greaterThanOrEqualTo(1000.0));
}
Aggregations