Search in sources :

Example 56 with QueryResponse

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;
}
Also used : QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) QueryResponse(ddf.catalog.operation.QueryResponse) ArrayList(java.util.ArrayList) ResultImpl(ddf.catalog.data.impl.ResultImpl) ArrayDeque(java.util.ArrayDeque) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result)

Example 57 with QueryResponse

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));
}
Also used : Metacard(ddf.catalog.data.Metacard) QueryResponse(ddf.catalog.operation.QueryResponse) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) CatalogFramework(ddf.catalog.CatalogFramework) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Result(ddf.catalog.data.Result) Test(org.junit.Test)

Example 58 with QueryResponse

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;
    });
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) CreateRequest(ddf.catalog.operation.CreateRequest) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) QueryResponse(ddf.catalog.operation.QueryResponse) CatalogFramework(ddf.catalog.CatalogFramework) CommandSession(org.apache.felix.service.command.CommandSession) Session(org.apache.karaf.shell.api.console.Session) Before(org.junit.Before)

Example 59 with QueryResponse

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;
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) Serializable(java.io.Serializable) QueryRequest(ddf.catalog.operation.QueryRequest) HashMap(java.util.HashMap) UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) BinaryContent(ddf.catalog.data.BinaryContent) FederationException(ddf.catalog.federation.FederationException) QueryResponse(ddf.catalog.operation.QueryResponse) Response(javax.ws.rs.core.Response) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) QueryRequestImpl(ddf.catalog.operation.impl.QueryRequestImpl) QueryResponse(ddf.catalog.operation.QueryResponse)

Example 60 with QueryResponse

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));
}
Also used : QueryRequest(ddf.catalog.operation.QueryRequest) QueryResponse(ddf.catalog.operation.QueryResponse) Tag(io.micrometer.core.instrument.Tag) Test(org.junit.Test)

Aggregations

QueryResponse (ddf.catalog.operation.QueryResponse)187 QueryRequest (ddf.catalog.operation.QueryRequest)130 Test (org.junit.Test)113 Metacard (ddf.catalog.data.Metacard)91 Result (ddf.catalog.data.Result)85 ArrayList (java.util.ArrayList)73 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)70 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)55 QueryImpl (ddf.catalog.operation.impl.QueryImpl)48 QueryResponseImpl (ddf.catalog.operation.impl.QueryResponseImpl)43 FederationException (ddf.catalog.federation.FederationException)40 ResultImpl (ddf.catalog.data.impl.ResultImpl)39 UnsupportedQueryException (ddf.catalog.source.UnsupportedQueryException)39 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)33 Filter (org.opengis.filter.Filter)33 HashMap (java.util.HashMap)31 Serializable (java.io.Serializable)30 Source (ddf.catalog.source.Source)28 HashSet (java.util.HashSet)28 CatalogFramework (ddf.catalog.CatalogFramework)26