Search in sources :

Example 56 with CswRecordCollection

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

the class TestGetRecordsResponseConverter method testMarshalRecordCollectionById.

@Ignore
public void testMarshalRecordCollectionById() throws UnsupportedEncodingException, JAXBException {
    final int totalResults = 2;
    XStream xstream = createXStream(CswConstants.GET_RECORD_BY_ID_RESPONSE);
    CswRecordCollection collection = createCswRecordCollection(null, totalResults);
    collection.setById(true);
    ArgumentCaptor<MarshallingContext> captor = ArgumentCaptor.forClass(MarshallingContext.class);
    String xml = xstream.toXML(collection);
    // Verify the context arguments were set correctly
    verify(mockProvider, times(totalResults)).marshal(any(Object.class), any(HierarchicalStreamWriter.class), captor.capture());
    MarshallingContext context = captor.getValue();
    assertThat(context, not(nullValue()));
    assertThat(context.get(CswConstants.OUTPUT_SCHEMA_PARAMETER), is(CswConstants.CSW_OUTPUT_SCHEMA));
    assertThat(context.get(CswConstants.ELEMENT_SET_TYPE), is(nullValue()));
    assertThat(context.get(CswConstants.ELEMENT_NAMES), is(nullValue()));
    JAXBElement<GetRecordByIdResponseType> jaxb = (JAXBElement<GetRecordByIdResponseType>) getJaxBContext().createUnmarshaller().unmarshal(new ByteArrayInputStream(xml.getBytes("UTF-8")));
    GetRecordByIdResponseType response = jaxb.getValue();
    // Assert the GetRecordsResponse elements and attributes
    assertThat(response, not(nullValue()));
}
Also used : GetRecordByIdResponseType(net.opengis.cat.csw.v_2_0_2.GetRecordByIdResponseType) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) XStream(com.thoughtworks.xstream.XStream) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) Matchers.anyString(org.mockito.Matchers.anyString) JAXBElement(javax.xml.bind.JAXBElement) Ignore(jdk.nashorn.internal.ir.annotations.Ignore)

Example 57 with CswRecordCollection

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

the class TestGetRecordsResponseConverter method testMarshalRecordCollectionGetSummary.

@Ignore
public void testMarshalRecordCollectionGetSummary() throws UnsupportedEncodingException, JAXBException {
    final int totalResults = 5;
    XStream xstream = createXStream(CswConstants.GET_RECORDS_RESPONSE);
    GetRecordsType getRecords = new GetRecordsType();
    QueryType query = new QueryType();
    ElementSetNameType set = new ElementSetNameType();
    set.setValue(ElementSetType.SUMMARY);
    query.setElementSetName(set);
    ObjectFactory objectFactory = new ObjectFactory();
    getRecords.setAbstractQuery(objectFactory.createAbstractQuery(query));
    CswRecordCollection collection = createCswRecordCollection(getRecords, totalResults);
    collection.setElementSetType(ElementSetType.SUMMARY);
    ArgumentCaptor<MarshallingContext> captor = ArgumentCaptor.forClass(MarshallingContext.class);
    String xml = xstream.toXML(collection);
    // Verify the context arguments were set correctly
    verify(mockProvider, times(totalResults)).marshal(any(Object.class), any(HierarchicalStreamWriter.class), captor.capture());
    MarshallingContext context = captor.getValue();
    assertThat(context, not(nullValue()));
    assertThat(context.get(CswConstants.OUTPUT_SCHEMA_PARAMETER), is(CswConstants.CSW_OUTPUT_SCHEMA));
    assertThat(context.get(CswConstants.ELEMENT_SET_TYPE), is(ElementSetType.SUMMARY));
    JAXBElement<GetRecordsResponseType> jaxb = (JAXBElement<GetRecordsResponseType>) getJaxBContext().createUnmarshaller().unmarshal(new ByteArrayInputStream(xml.getBytes("UTF-8")));
    GetRecordsResponseType response = jaxb.getValue();
    // Assert the GetRecordsResponse elements and attributes
    assertThat(response, not(nullValue()));
    SearchResultsType resultsType = response.getSearchResults();
    assertThat(resultsType, not(nullValue()));
    assertThat(resultsType.getElementSet(), is(ElementSetType.SUMMARY));
    assertThat(resultsType.getNumberOfRecordsMatched().intValue(), is(totalResults));
    assertThat(resultsType.getNumberOfRecordsReturned().intValue(), is(totalResults));
    assertThat(resultsType.getRecordSchema(), is(CswConstants.CSW_OUTPUT_SCHEMA));
}
Also used : HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) XStream(com.thoughtworks.xstream.XStream) SearchResultsType(net.opengis.cat.csw.v_2_0_2.SearchResultsType) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) Matchers.anyString(org.mockito.Matchers.anyString) JAXBElement(javax.xml.bind.JAXBElement) ObjectFactory(net.opengis.cat.csw.v_2_0_2.ObjectFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) ElementSetNameType(net.opengis.cat.csw.v_2_0_2.ElementSetNameType) GetRecordsResponseType(net.opengis.cat.csw.v_2_0_2.GetRecordsResponseType) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) QueryType(net.opengis.cat.csw.v_2_0_2.QueryType) Ignore(jdk.nashorn.internal.ir.annotations.Ignore)

Example 58 with CswRecordCollection

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

the class TestCswEndpoint method testPostGetRecordsGmdCswOutputSchema.

@Test
public void testPostGetRecordsGmdCswOutputSchema() 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(GmdConstants.GMD_NAMESPACE, GmdConstants.GMD_LOCAL_NAME, GmdConstants.GMD_PREFIX));
    query.setTypeNames(typeNames);
    QueryConstraintType constraint = new QueryConstraintType();
    constraint.setCqlText(GMD_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);
    when(catalogFramework.query(argument.capture())).thenReturn(getQueryResponse());
    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) 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 59 with CswRecordCollection

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

the class TestCswEndpoint method testPostGetRecordById.

@Test
public void testPostGetRecordById() throws CswException, FederationException, SourceUnavailableException, UnsupportedQueryException {
    final GetRecordByIdType getRecordByIdType = new GetRecordByIdType();
    getRecordByIdType.setId(Collections.singletonList("123,456"));
    getRecordByIdType.setOutputFormat(MediaType.APPLICATION_XML);
    getRecordByIdType.setOutputSchema(CswConstants.CSW_OUTPUT_SCHEMA);
    final Metacard metacard1 = new MetacardImpl();
    final Metacard metacard2 = new MetacardImpl();
    final List<Result> mockResults = Arrays.asList(new ResultImpl(metacard1), new ResultImpl(metacard2));
    final QueryResponse queryResponse = new QueryResponseImpl(null, mockResults, mockResults.size());
    doReturn(queryResponse).when(catalogFramework).query(any(QueryRequest.class));
    final CswRecordCollection cswRecordCollection = csw.getRecordById(getRecordByIdType, null);
    verifyCswRecordCollection(cswRecordCollection, metacard1, metacard2);
    // "summary" is the default if none is specified in the request.
    assertThat(cswRecordCollection.getElementSetType(), is(ElementSetType.SUMMARY));
}
Also used : Metacard(ddf.catalog.data.Metacard) QueryResponseImpl(ddf.catalog.operation.impl.QueryResponseImpl) QueryRequest(ddf.catalog.operation.QueryRequest) QueryResponse(ddf.catalog.operation.QueryResponse) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) ResultImpl(ddf.catalog.data.impl.ResultImpl) GetRecordByIdType(net.opengis.cat.csw.v_2_0_2.GetRecordByIdType) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Result(ddf.catalog.data.Result) Test(org.junit.Test)

Example 60 with CswRecordCollection

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

the class TestGetRecordsMessageBodyReader method testPartialContentResponseHandling.

@Test
public void testPartialContentResponseHandling() throws Exception {
    CswSourceConfiguration config = new CswSourceConfiguration(encryptionService);
    config.setMetacardCswMappings(DefaultCswRecordMap.getCswToMetacardAttributeNames());
    config.setOutputSchema(CswConstants.CSW_OUTPUT_SCHEMA);
    GetRecordsMessageBodyReader reader = new GetRecordsMessageBodyReader(mockProvider, config);
    String sampleData = "SampleData";
    byte[] data = sampleData.getBytes();
    ByteArrayInputStream dataInputStream = new ByteArrayInputStream(data);
    MultivaluedMap<String, String> httpHeaders = new MultivaluedHashMap<>();
    httpHeaders.add(CswConstants.PRODUCT_RETRIEVAL_HTTP_HEADER, "TRUE");
    httpHeaders.add(HttpHeaders.CONTENT_DISPOSITION, String.format("inline; filename=ResourceName"));
    httpHeaders.add(HttpHeaders.CONTENT_RANGE, String.format("bytes 1-%d/%d", sampleData.length() - 1, sampleData.length()));
    MediaType mediaType = new MediaType("text", "plain");
    CswRecordCollection cswRecords = reader.readFrom(CswRecordCollection.class, null, null, mediaType, httpHeaders, dataInputStream);
    Resource resource = cswRecords.getResource();
    // assert that the CswRecordCollection properly extracted the bytes skipped from the Partial Content response
    assertThat(cswRecords.getResourceProperties().get(GetRecordsMessageBodyReader.BYTES_SKIPPED), is(1L));
    // assert that the input stream has not been skipped at this stage. Since AbstractCswSource has the number
    // of bytes that was attempted to be skipped, the stream must be aligned there instead.
    assertThat(resource.getByteArray(), is(data));
}
Also used : CswSourceConfiguration(org.codice.ddf.spatial.ogc.csw.catalog.common.CswSourceConfiguration) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) Resource(ddf.catalog.resource.Resource) MediaType(javax.ws.rs.core.MediaType) 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