Search in sources :

Example 86 with SourceResponse

use of ddf.catalog.operation.SourceResponse in project ddf by codice.

the class TestXmlResponseQueueTransformer method testEmptySourceResponse.

/**
     * @throws CatalogTransformerException
     */
@Test
public void testEmptySourceResponse() throws CatalogTransformerException, IOException, XpathException, SAXException {
    // given
    transformer.setThreshold(-1);
    SourceResponse response = new SourceResponseImpl(null, Collections.<Result>emptyList());
    // when
    BinaryContent binaryContent = transformer.transform(response, null);
    // then
    String output = new String(binaryContent.getByteArray());
    LOGGER.info(output);
    assertXpathEvaluatesTo("", "/mc:metacards", output);
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) SourceResponseImpl(ddf.catalog.operation.impl.SourceResponseImpl) Matchers.anyString(org.mockito.Matchers.anyString) BinaryContent(ddf.catalog.data.BinaryContent) Test(org.junit.Test)

Example 87 with SourceResponse

use of ddf.catalog.operation.SourceResponse in project ddf by codice.

the class TestXmlResponseQueueTransformer method testNoId.

@Test
public void testNoId() throws CatalogTransformerException, IOException, XpathException, SAXException {
    // given
    transformer.setThreshold(2);
    SourceResponse response = givenSourceResponse(DEFAULT_SOURCE_ID, null);
    // when
    BinaryContent binaryContent = transformer.transform(response, null);
    // then
    assertThat(binaryContent.getMimeType(), is(mimeType));
    byte[] bytes = binaryContent.getByteArray();
    String output = new String(bytes);
    print(output, verboseDebug);
    assertXpathEvaluatesTo(DEFAULT_SOURCE_ID, "/mc:metacards/mc:metacard/mc:source", output);
    assertXpathNotExists("/mc:metacards/mc:metacard/@gml:id", output);
    verifyDefaults("1", output);
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) Matchers.anyString(org.mockito.Matchers.anyString) BinaryContent(ddf.catalog.data.BinaryContent) Test(org.junit.Test)

Example 88 with SourceResponse

use of ddf.catalog.operation.SourceResponse in project ddf by codice.

the class TestXmlResponseQueueTransformer method testMetacardTypeNameNull.

/**
     * No {@link MetacardType} name should use the default name.
     *
     * @throws CatalogTransformerException
     * @throws IOException
     * @throws SAXException
     * @throws XpathException
     */
@Test
public void testMetacardTypeNameNull() throws CatalogTransformerException, IOException, XpathException, SAXException {
    // given
    transformer.setThreshold(2);
    SourceResponse response = givenMetacardTypeName(null);
    // when
    BinaryContent binaryContent = transformer.transform(response, null);
    // then
    assertThat(binaryContent.getMimeType(), is(mimeType));
    byte[] bytes = binaryContent.getByteArray();
    String output = new String(bytes);
    print(output, verboseDebug);
    assertXpathEvaluatesTo(DEFAULT_TYPE_NAME, "/mc:metacards/mc:metacard/mc:type", output);
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) Matchers.anyString(org.mockito.Matchers.anyString) BinaryContent(ddf.catalog.data.BinaryContent) Test(org.junit.Test)

Example 89 with SourceResponse

use of ddf.catalog.operation.SourceResponse in project ddf by codice.

the class TestXmlResponseQueueTransformer method testCompareSerialToFork.

@Test
public void testCompareSerialToFork() throws IOException, CatalogTransformerException, MimeTypeParseException {
    SourceResponse response = givenSourceResponse(new MetacardStub("source1", "id1"), new MetacardStub("source2", "id2"), new MetacardStub("source3", "id3"), new MetacardStub("source4", "id4"));
    PrintWriterProvider pwp = new PrintWriterProviderImpl();
    MetacardMarshaller mcm = new MetacardMarshallerImpl(parser, pwp);
    XmlResponseQueueTransformer serialXform = new XmlResponseQueueTransformer(parser, FJP, pwp, mcm, getMimeType());
    serialXform.setThreshold(2);
    XmlResponseQueueTransformer forkXForm = new XmlResponseQueueTransformer(parser, FJP, pwp, mcm, getMimeType());
    forkXForm.setThreshold(10);
    BinaryContent serialBc = serialXform.transform(response, null);
    BinaryContent forkBc = forkXForm.transform(response, null);
    String serialOutput = new String(serialBc.getByteArray());
    String forkOutput = new String(forkBc.getByteArray());
    // There are expected whitespace differences between the outputs.
    // This is an overly aggressive conversion; a better test would be to unmarshal the
    // xml metacards back into Metacard instances and compare equality.
    assertEquals(serialOutput.replaceAll("\\s", ""), forkOutput.replaceAll("\\s", ""));
}
Also used : MetacardMarshallerImpl(ddf.catalog.transformer.xml.MetacardMarshallerImpl) PrintWriterProvider(ddf.catalog.transformer.api.PrintWriterProvider) SourceResponse(ddf.catalog.operation.SourceResponse) MetacardMarshaller(ddf.catalog.transformer.api.MetacardMarshaller) PrintWriterProviderImpl(ddf.catalog.transformer.xml.PrintWriterProviderImpl) XmlResponseQueueTransformer(ddf.catalog.transformer.xml.XmlResponseQueueTransformer) Matchers.anyString(org.mockito.Matchers.anyString) BinaryContent(ddf.catalog.data.BinaryContent) Test(org.junit.Test)

Example 90 with SourceResponse

use of ddf.catalog.operation.SourceResponse in project ddf by codice.

the class SortedQueryMonitor method run.

@Override
public void run() {
    SortBy sortBy = query.getSortBy();
    // Prepare the Comparators that we will use
    Comparator<Result> coreComparator = CachingFederationStrategy.DEFAULT_COMPARATOR;
    if (sortBy != null && sortBy.getPropertyName() != null) {
        PropertyName sortingProp = sortBy.getPropertyName();
        String sortType = sortingProp.getPropertyName();
        SortOrder sortOrder = (sortBy.getSortOrder() == null) ? SortOrder.DESCENDING : sortBy.getSortOrder();
        LOGGER.debug("Sorting type: {}", sortType);
        LOGGER.debug("Sorting order: {}", sortBy.getSortOrder());
        // Temporal searches are currently sorted by the effective time
        if (Metacard.EFFECTIVE.equals(sortType) || Result.TEMPORAL.equals(sortType)) {
            coreComparator = new TemporalResultComparator(sortOrder);
        } else if (Result.DISTANCE.equals(sortType)) {
            coreComparator = new DistanceResultComparator(sortOrder);
        } else if (Result.RELEVANCE.equals(sortType)) {
            coreComparator = new RelevanceResultComparator(sortOrder);
        }
    }
    List<Result> resultList = new ArrayList<>();
    long totalHits = 0;
    Set<ProcessingDetails> processingDetails = returnResults.getProcessingDetails();
    Map<String, Serializable> returnProperties = returnResults.getProperties();
    HashMap<String, Long> hitsPerSource = new HashMap<>();
    for (int i = futures.size(); i > 0; i--) {
        String sourceId = "Unknown Source";
        QueryRequest queryRequest = null;
        SourceResponse sourceResponse = null;
        try {
            Future<SourceResponse> future;
            if (query.getTimeoutMillis() < 1) {
                future = completionService.take();
            } else {
                future = completionService.poll(getTimeRemaining(deadline), TimeUnit.MILLISECONDS);
                if (future == null) {
                    timeoutRemainingSources(processingDetails);
                    break;
                }
            }
            queryRequest = futures.remove(future);
            sourceId = getSourceIdFromRequest(queryRequest);
            sourceResponse = future.get();
            if (sourceResponse == null) {
                LOGGER.debug("Source {} returned null response", sourceId);
                executePostFederationQueryPluginsWithSourceError(queryRequest, sourceId, new NullPointerException(), processingDetails);
            } else {
                sourceResponse = executePostFederationQueryPlugins(sourceResponse, queryRequest);
                resultList.addAll(sourceResponse.getResults());
                long hits = sourceResponse.getHits();
                totalHits += hits;
                hitsPerSource.merge(sourceId, hits, (l1, l2) -> l1 + l2);
                Map<String, Serializable> properties = sourceResponse.getProperties();
                returnProperties.putAll(properties);
            }
        } catch (InterruptedException e) {
            if (queryRequest != null) {
                // First, add interrupted processing detail for this source
                LOGGER.debug("Search interrupted for {}", sourceId);
                executePostFederationQueryPluginsWithSourceError(queryRequest, sourceId, e, processingDetails);
            }
            // Then add the interrupted exception for the remaining sources
            interruptRemainingSources(processingDetails, e);
            break;
        } catch (ExecutionException e) {
            LOGGER.info("Couldn't get results from completed federated query. {}, {}", sourceId, Exceptions.getFullMessage(e), e);
            executePostFederationQueryPluginsWithSourceError(queryRequest, sourceId, e, processingDetails);
        }
    }
    returnProperties.put("hitsPerSource", hitsPerSource);
    LOGGER.debug("All sources finished returning results: {}", resultList.size());
    returnResults.setHits(totalHits);
    if (CachingFederationStrategy.INDEX_QUERY_MODE.equals(request.getPropertyValue(CachingFederationStrategy.QUERY_MODE))) {
        QueryResponse result = cachingFederationStrategy.queryCache(request);
        returnResults.addResults(result.getResults(), true);
    } else {
        returnResults.addResults(sortedResults(resultList, coreComparator), true);
    }
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) SortBy(org.opengis.filter.sort.SortBy) ArrayList(java.util.ArrayList) Result(ddf.catalog.data.Result) ProcessingDetails(ddf.catalog.operation.ProcessingDetails) PluginExecutionException(ddf.catalog.plugin.PluginExecutionException) ExecutionException(java.util.concurrent.ExecutionException) PropertyName(org.opengis.filter.expression.PropertyName) QueryRequest(ddf.catalog.operation.QueryRequest) SourceResponse(ddf.catalog.operation.SourceResponse) SortOrder(org.opengis.filter.sort.SortOrder) TemporalResultComparator(ddf.catalog.util.impl.TemporalResultComparator) QueryResponse(ddf.catalog.operation.QueryResponse) RelevanceResultComparator(ddf.catalog.util.impl.RelevanceResultComparator) DistanceResultComparator(ddf.catalog.util.impl.DistanceResultComparator)

Aggregations

SourceResponse (ddf.catalog.operation.SourceResponse)199 Test (org.junit.Test)146 QueryRequestImpl (ddf.catalog.operation.impl.QueryRequestImpl)99 QueryImpl (ddf.catalog.operation.impl.QueryImpl)95 Result (ddf.catalog.data.Result)79 Metacard (ddf.catalog.data.Metacard)73 Filter (org.opengis.filter.Filter)58 QueryRequest (ddf.catalog.operation.QueryRequest)52 BinaryContent (ddf.catalog.data.BinaryContent)40 ResultImpl (ddf.catalog.data.impl.ResultImpl)29 SourceResponseImpl (ddf.catalog.operation.impl.SourceResponseImpl)25 ArrayList (java.util.ArrayList)25 HashMap (java.util.HashMap)24 Matchers.containsString (org.hamcrest.Matchers.containsString)24 Matchers.anyString (org.mockito.Matchers.anyString)21 MetacardImpl (ddf.catalog.data.impl.MetacardImpl)20 Serializable (java.io.Serializable)20 MetacardTransformer (ddf.catalog.transform.MetacardTransformer)19 SortByImpl (ddf.catalog.filter.impl.SortByImpl)18 Map (java.util.Map)16