Search in sources :

Example 61 with CswRecordCollection

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

the class TestGetRecordsMessageBodyReader method testGetMultipleMetacardsWithForeignText.

// verifies UTF-8 encoding configured properly when XML includes foreign text with special characters
@Test
public void testGetMultipleMetacardsWithForeignText() throws Exception {
    List<Metacard> inputMetacards = new ArrayList<>();
    MetacardImpl metacard = new MetacardImpl(BasicTypes.BASIC_METACARD);
    inputMetacards.add(metacard);
    CswRecordCollection collection = new CswRecordCollection();
    collection.setCswRecords(inputMetacards);
    when(mockProvider.unmarshal(any(), any())).thenReturn(collection);
    CswSourceConfiguration config = new CswSourceConfiguration(encryptionService);
    config.setMetacardCswMappings(DefaultCswRecordMap.getCswToMetacardAttributeNames());
    config.setOutputSchema(CswConstants.CSW_OUTPUT_SCHEMA);
    GetRecordsMessageBodyReader reader = new GetRecordsMessageBodyReader(mockProvider, config);
    InputStream is = TestGetRecordsMessageBodyReader.class.getResourceAsStream("/geomaticsGetRecordsResponse.xml");
    MultivaluedMap<String, String> httpHeaders = new MultivaluedHashMap<>();
    CswRecordCollection cswRecords = reader.readFrom(CswRecordCollection.class, null, null, null, httpHeaders, is);
    List<Metacard> metacards = cswRecords.getCswRecords();
    assertThat(metacards, contains(metacard));
}
Also used : CswSourceConfiguration(org.codice.ddf.spatial.ogc.csw.catalog.common.CswSourceConfiguration) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Metacard(ddf.catalog.data.Metacard) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) MetacardImpl(ddf.catalog.data.impl.MetacardImpl) Test(org.junit.Test)

Example 62 with CswRecordCollection

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

the class GetRecordsResponseConverterTest method testMarshalRecordCollectionById.

@Ignore
@Test
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) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) JAXBElement(javax.xml.bind.JAXBElement) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 63 with CswRecordCollection

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

the class GetRecordsResponseConverterTest method testMarshalRecordCollectionGetFull.

@Ignore
@Test
public void testMarshalRecordCollectionGetFull() 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.FULL);
    query.setElementSetName(set);
    ObjectFactory objectFactory = new ObjectFactory();
    getRecords.setAbstractQuery(objectFactory.createAbstractQuery(query));
    CswRecordCollection collection = createCswRecordCollection(getRecords, totalResults);
    collection.setElementSetType(ElementSetType.FULL);
    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.FULL));
    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.FULL));
    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) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.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(org.junit.Ignore) Test(org.junit.Test)

Example 64 with CswRecordCollection

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

the class GetRecordsResponseConverterTest method createCswRecordCollection.

private CswRecordCollection createCswRecordCollection(GetRecordsType request, int resultCount) {
    CswRecordCollection collection = new CswRecordCollection();
    int first = 1;
    int last = 2;
    if (request != null) {
        first = request.getStartPosition().intValue();
        int next = request.getMaxRecords().intValue() + first;
        last = next - 1;
        if (last >= resultCount) {
            last = resultCount;
        }
    }
    int returned = last - first + 1;
    collection.setCswRecords(createMetacardList(first, last));
    collection.setNumberOfRecordsMatched(resultCount);
    collection.setNumberOfRecordsReturned(returned);
    return collection;
}
Also used : CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)

Example 65 with CswRecordCollection

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

the class GetRecordsResponseConverterTest method getRecords.

private void getRecords(final int maxRecords, final int startPosition, final int totalResults, final int expectedNext, final int expectedReturn) throws JAXBException, UnsupportedEncodingException {
    XStream xstream = createXStream(CswConstants.GET_RECORDS_RESPONSE);
    GetRecordsType query = new GetRecordsType();
    query.setMaxRecords(BigInteger.valueOf(maxRecords));
    query.setStartPosition(BigInteger.valueOf(startPosition));
    CswRecordCollection collection = createCswRecordCollection(query, totalResults);
    collection.setStartPosition(startPosition);
    String xml = xstream.toXML(collection);
    JAXBElement<GetRecordsResponseType> jaxb = (JAXBElement<GetRecordsResponseType>) getJaxBContext().createUnmarshaller().unmarshal(new ByteArrayInputStream(xml.getBytes("UTF-8")));
    GetRecordsResponseType response = jaxb.getValue();
    assertThat(response.getSearchResults().getNumberOfRecordsMatched().intValue(), is(totalResults));
    assertThat(response.getSearchResults().getNumberOfRecordsReturned().intValue(), is(expectedReturn));
    assertThat(response.getSearchResults().getAbstractRecord().size(), is(expectedReturn));
    assertThat(response.getSearchResults().getNextRecord().intValue(), is(expectedNext));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) XStream(com.thoughtworks.xstream.XStream) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) GetRecordsType(net.opengis.cat.csw.v_2_0_2.GetRecordsType) GetRecordsResponseType(net.opengis.cat.csw.v_2_0_2.GetRecordsResponseType) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) JAXBElement(javax.xml.bind.JAXBElement)

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