Search in sources :

Example 71 with CswRecordCollection

use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.

the class CswRecordCollectionMessageBodyWriterTest method testWriteToWithSchema.

@Test
public void testWriteToWithSchema() throws WebApplicationException, IOException, JAXBException, CatalogTransformerException {
    CswRecordCollectionMessageBodyWriter writer = new CswRecordCollectionMessageBodyWriter(mockManager);
    when(mockManager.getTransformerBySchema(anyString())).thenReturn(mockTransformer);
    ArgumentCaptor<Map> captor = ArgumentCaptor.forClass(Map.class);
    when(mockTransformer.transform(any(SourceResponse.class), any(Map.class))).thenReturn(mockContent);
    when(mockContent.getInputStream()).thenReturn(new ByteArrayInputStream("bytes".getBytes()));
    CswRecordCollection collection = createCswRecordCollection(6);
    collection.setNumberOfRecordsMatched(22);
    collection.setNumberOfRecordsReturned(6);
    final String exampleSchema = "http://example.com/schema";
    collection.setOutputSchema(exampleSchema);
    collection.setById(true);
    collection.setResultType(ResultType.HITS);
    collection.setSourceResponse(mock(SourceResponse.class));
    QName example = new QName("example");
    collection.setElementName(Arrays.asList(example));
    collection.setElementSetType(ElementSetType.BRIEF);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    writer.writeTo(collection, null, null, null, null, null, stream);
    verify(mockTransformer).transform(any(SourceResponse.class), captor.capture());
    Map arguments = captor.getValue();
    assertThat(arguments.get(CswConstants.WRITE_NAMESPACES), is(false));
    assertThat(arguments.get(CswConstants.OUTPUT_SCHEMA_PARAMETER), is(exampleSchema));
    assertThat(arguments.get(CswConstants.RESULT_TYPE_PARAMETER), is(ResultType.HITS));
    assertThat(arguments.get(CswConstants.IS_BY_ID_QUERY), is(true));
    assertThat(arguments.get(CswConstants.ELEMENT_SET_TYPE), is(ElementSetType.BRIEF));
    assertThat(((Object[]) arguments.get(CswConstants.ELEMENT_NAMES))[0], is(example));
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) QName(javax.xml.namespace.QName) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Map(java.util.Map) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Test(org.junit.Test)

Example 72 with CswRecordCollection

use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.

the class CswRecordCollectionMessageBodyWriterTest method testWriteToWithMimeType.

@Test
public void testWriteToWithMimeType() throws WebApplicationException, IOException, JAXBException, CatalogTransformerException {
    CswRecordCollectionMessageBodyWriter writer = new CswRecordCollectionMessageBodyWriter(mockManager);
    when(mockManager.getTransformerByMimeType(any(String.class))).thenReturn(mockTransformer);
    when(mockTransformer.transform(any(SourceResponse.class), any(Map.class))).thenReturn(mockContent);
    when(mockContent.getInputStream()).thenReturn(new ByteArrayInputStream("bytes".getBytes()));
    CswRecordCollection collection = createCswRecordCollection(6);
    collection.setNumberOfRecordsMatched(22);
    collection.setNumberOfRecordsReturned(6);
    collection.setById(true);
    collection.setResultType(ResultType.RESULTS);
    collection.setMimeType(MediaType.APPLICATION_JSON);
    collection.setSourceResponse(mock(SourceResponse.class));
    QName example = new QName("example");
    collection.setElementName(Arrays.asList(example));
    collection.setElementSetType(ElementSetType.BRIEF);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    writer.writeTo(collection, null, null, null, null, null, stream);
    verify(mockManager, times(1)).getTransformerByMimeType(any(String.class));
// TODO - assert lookup by mime type
// TODO failure case
}
Also used : SourceResponse(ddf.catalog.operation.SourceResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) QName(javax.xml.namespace.QName) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Map(java.util.Map) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Test(org.junit.Test)

Example 73 with CswRecordCollection

use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.

the class CswEndpointTest method testPostGetRecordsResults.

@Test
public void testPostGetRecordsResults() throws CswException, UnsupportedQueryException, SourceUnavailableException, FederationException {
    GetRecordsType grr = createDefaultPostRecordsRequest();
    grr.setResultType(ResultType.RESULTS);
    QueryType query = new QueryType();
    List<QName> typeNames = new ArrayList<>();
    typeNames.add(new QName(CswConstants.CSW_OUTPUT_SCHEMA, VALID_TYPE, VALID_PREFIX));
    query.setTypeNames(typeNames);
    QueryConstraintType constraint = new QueryConstraintType();
    constraint.setCqlText(CQL_CONTEXTUAL_LIKE_QUERY);
    query.setConstraint(constraint);
    ElementSetNameType esnt = new ElementSetNameType();
    esnt.setValue(ElementSetType.SUMMARY);
    query.setElementSetName(esnt);
    JAXBElement<QueryType> jaxbQuery = new JAXBElement<>(cswQnameOutPutSchema, QueryType.class, query);
    grr.setAbstractQuery(jaxbQuery);
    final String exampleSchema = CswConstants.CSW_OUTPUT_SCHEMA;
    grr.setOutputSchema(exampleSchema);
    final String exampleMime = "application/xml";
    grr.setOutputFormat(exampleMime);
    CswRecordCollection collection = csw.getRecords(grr);
    assertThat(collection.getMimeType(), is(exampleMime));
    assertThat(collection.getOutputSchema(), is(exampleSchema));
    assertThat(collection.getSourceResponse(), notNullValue());
    assertThat(collection.getResultType(), is(ResultType.RESULTS));
    assertThat(collection.getElementSetType(), is(ElementSetType.SUMMARY));
}
Also used : QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) ElementSetNameType(net.opengis.cat.csw.v_2_0_2.ElementSetNameType) JAXBElement(javax.xml.bind.JAXBElement) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) QueryType(net.opengis.cat.csw.v_2_0_2.QueryType) QueryConstraintType(net.opengis.cat.csw.v_2_0_2.QueryConstraintType) Test(org.junit.Test)

Example 74 with CswRecordCollection

use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.

the class CswEndpoint method getRecordById.

@Override
@POST
@Consumes({ MediaType.TEXT_XML, MediaType.APPLICATION_XML })
@Produces({ MediaType.TEXT_XML, MediaType.APPLICATION_XML })
public CswRecordCollection getRecordById(GetRecordByIdType request, @HeaderParam(CswConstants.RANGE_HEADER) String rangeValue) throws CswException {
    if (request == null) {
        throw new CswException("GetRecordByIdRequest request is null");
    }
    String outputFormat = request.getOutputFormat();
    String outputSchema = request.getOutputSchema();
    validator.validateOutputFormat(outputFormat, mimeTypeTransformerManager);
    validator.validateOutputSchema(outputSchema, schemaTransformerManager);
    List<String> ids = request.getId();
    if (!ids.isEmpty()) {
        String id = ids.get(0);
        // Check if the request wants to retrieve a product.
        if (isProductRetrieval(ids, outputFormat, outputSchema)) {
            LOGGER.debug("{} is attempting to retrieve product for: {}", request.getService(), id);
            try {
                return queryProductById(id, rangeValue);
            } catch (UnsupportedQueryException e) {
                throw new CswException(String.format(ERROR_ID_PRODUCT_RETRIEVAL, id), e);
            }
        }
        LOGGER.debug("{} is attempting to retrieve records: {}", request.getService(), ids);
        CswRecordCollection response = queryById(ids, outputSchema);
        response.setOutputSchema(outputSchema);
        if (request.isSetElementSetName() && request.getElementSetName().getValue() != null) {
            response.setElementSetType(request.getElementSetName().getValue());
        } else {
            response.setElementSetType(ElementSetType.SUMMARY);
        }
        LOGGER.debug("{} successfully retrieved record(s): {}", request.getService(), request.getId());
        return response;
    } else {
        throw new CswException("A GetRecordById Query must contain an ID.", CswConstants.MISSING_PARAMETER_VALUE, "id");
    }
}
Also used : UnsupportedQueryException(ddf.catalog.source.UnsupportedQueryException) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 75 with CswRecordCollection

use of org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection in project ddf by codice.

the class CswEndpointTest method testPostRetrieveProductGetRecordByIdWithNoMimeType.

@Test
public void testPostRetrieveProductGetRecordByIdWithNoMimeType() throws IOException, ResourceNotFoundException, ResourceNotSupportedException, CswException {
    final GetRecordByIdType getRecordByIdType = new GetRecordByIdType();
    getRecordByIdType.setOutputFormat(MediaType.APPLICATION_OCTET_STREAM);
    getRecordByIdType.setOutputSchema(OCTET_STREAM_OUTPUT_SCHEMA);
    getRecordByIdType.setId(Collections.singletonList("123"));
    setUpMocksForProductRetrieval(false);
    CswRecordCollection cswRecordCollection = csw.getRecordById(getRecordByIdType, null);
    assertThat(cswRecordCollection.getResource(), is(notNullValue()));
    assertThat(cswRecordCollection.getResource().getMimeType().toString(), is(MediaType.APPLICATION_OCTET_STREAM));
}
Also used : CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) GetRecordByIdType(net.opengis.cat.csw.v_2_0_2.GetRecordByIdType) Test(org.junit.Test)

Aggregations

CswRecordCollection (org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)84 Test (org.junit.Test)56 ByteArrayInputStream (java.io.ByteArrayInputStream)37 GetRecordsType (net.opengis.cat.csw.v_2_0_2.GetRecordsType)29 JAXBElement (javax.xml.bind.JAXBElement)25 QueryType (net.opengis.cat.csw.v_2_0_2.QueryType)21 XStream (com.thoughtworks.xstream.XStream)20 Metacard (ddf.catalog.data.Metacard)20 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)20 Matchers.anyString (org.mockito.Matchers.anyString)20 ArrayList (java.util.ArrayList)18 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)15 QName (javax.xml.namespace.QName)15 GetRecordsResponseType (net.opengis.cat.csw.v_2_0_2.GetRecordsResponseType)15 Resource (ddf.catalog.resource.Resource)14 ElementSetNameType (net.opengis.cat.csw.v_2_0_2.ElementSetNameType)14 ObjectFactory (net.opengis.cat.csw.v_2_0_2.ObjectFactory)13 SearchResultsType (net.opengis.cat.csw.v_2_0_2.SearchResultsType)13 MarshallingContext (com.thoughtworks.xstream.converters.MarshallingContext)12 HierarchicalStreamWriter (com.thoughtworks.xstream.io.HierarchicalStreamWriter)12