Search in sources :

Example 11 with UnmarshallingContext

use of com.thoughtworks.xstream.converters.UnmarshallingContext in project ddf by codice.

the class TestCswTransformProvider method testUnmarshalCopyPreservesNamespaces.

@Test
public void testUnmarshalCopyPreservesNamespaces() throws Exception {
    InputTransformer mockInputTransformer = mock(InputTransformer.class);
    when(mockInputManager.getTransformerBySchema(anyString())).thenReturn(mockInputTransformer);
    StaxDriver driver = new StaxDriver();
    driver.setRepairingNamespace(true);
    driver.getQnameMap().setDefaultNamespace(CswConstants.CSW_OUTPUT_SCHEMA);
    driver.getQnameMap().setDefaultPrefix(CswConstants.CSW_NAMESPACE_PREFIX);
    // Have to use XppReader in order to preserve the namespaces.
    HierarchicalStreamReader reader = new XppReader(new StringReader(getRecord()), XmlPullParserFactory.newInstance().newPullParser());
    CswTransformProvider provider = new CswTransformProvider(null, mockInputManager);
    UnmarshallingContext context = new TreeUnmarshaller(null, null, null, null);
    context.put(CswConstants.OUTPUT_SCHEMA_PARAMETER, "http://example.com/schema");
    ArgumentCaptor<InputStream> captor = ArgumentCaptor.forClass(InputStream.class);
    provider.unmarshal(reader, context);
    // Verify the context arguments were set correctly
    verify(mockInputTransformer, times(1)).transform(captor.capture());
    InputStream inStream = captor.getValue();
    String result = IOUtils.toString(inStream);
    XMLUnit.setIgnoreWhitespace(true);
    XMLAssert.assertXMLEqual(getRecord(), result);
}
Also used : StaxDriver(com.thoughtworks.xstream.io.xml.StaxDriver) XppReader(com.thoughtworks.xstream.io.xml.XppReader) InputStream(java.io.InputStream) StringReader(java.io.StringReader) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) UnmarshallingContext(com.thoughtworks.xstream.converters.UnmarshallingContext) Matchers.anyString(org.mockito.Matchers.anyString) InputTransformer(ddf.catalog.transform.InputTransformer) TreeUnmarshaller(com.thoughtworks.xstream.core.TreeUnmarshaller) Test(org.junit.Test)

Example 12 with UnmarshallingContext

use of com.thoughtworks.xstream.converters.UnmarshallingContext in project ddf by codice.

the class TestGetRecordsResponseConverter method testUnmarshalParseXmlNamespaces.

@Test
public void testUnmarshalParseXmlNamespaces() throws XmlPullParserException {
    XStream xstream = new XStream(new WstxDriver());
    xstream.setClassLoader(this.getClass().getClassLoader());
    xstream.registerConverter(new GetRecordsResponseConverter(mockProvider));
    xstream.alias("csw:GetRecordsResponse", CswRecordCollection.class);
    String xml = "<?xml version='1.0' encoding='UTF-8'?>" + "<csw:GetRecordsResponse " + "xmlns:dc=\"http://purl.org/dc/elements/1.1/\" " + "xmlns:dct=\"http://purl.org/dc/terms/\" " + "xmlns:ows=\"http://www.opengis.net/ows\" " + "xmlns:csw=\"http://www.opengis.net/cat/csw/2.0.2\" " + "version=\"2.0.2\"><csw:SearchStatus " + "timestamp=\"2014-11-11T10:53:32.152-06:00\"/>" + "<csw:SearchResults numberOfRecordsMatched=\"1\" " + "numberOfRecordsReturned=\"1\" nextRecord=\"0\" " + "recordSchema=\"http://www.opengis.net/cat/csw/2.0.2\">" + "<csw:Record>\n" + "<dc:identifier>0a2e1b1d2a3755e70a96d61e6bddbc5d</dc:identifier>" + "<dct:bibliographicCitation>0a2e1b1d2a3755e70a96d61e6bddbc5d</dct:bibliographicCitation>" + "<dc:title>US woman attacks Gauguin painting</dc:title>" + "<dct:alternative>US woman attacks Gauguin painting</dct:alternative>" + "<dc:type>video</dc:type><dc:date>2011-04-06T04:49:20.230-05:00</dc:date>" + "<dct:modified>2011-04-06T04:49:20.230-05:00</dct:modified>" + "<dct:created>2011-04-06T04:49:20.230-05:00</dct:created>" + "<dct:dateAccepted>2011-04-06T04:48:26.180-05:00</dct:dateAccepted>" + "<dct:dateCopyrighted>2011-04-06T04:48:26.180-05:00</dct:dateCopyrighted><" + "dct:dateSubmitted>2011-04-06T04:49:20.230-05:00</dct:dateSubmitted>" + "<dct:issued>2011-04-06T04:49:20.230-05:00</dct:issued>" + "<dc:publisher>ddf.distribution</dc:publisher>" + "<ows:BoundingBox crs=\"urn:x-ogc:def:crs:EPSG:6.11:4326\">\n" + "    <ows:LowerCorner>-50.5056430529222 84.0285103635943</ows:LowerCorner>" + "<ows:UpperCorner>-50.5056430529222 84.0285103635943</ows:UpperCorner>" + "</ows:BoundingBox></csw:Record><" + "/csw:SearchResults>" + "</csw:GetRecordsResponse>";
    InputStream inStream = IOUtils.toInputStream(xml);
    ArgumentCaptor<UnmarshallingContext> captor = ArgumentCaptor.forClass(UnmarshallingContext.class);
    HierarchicalStreamReader reader = new XppReader(new InputStreamReader(inStream), XmlPullParserFactory.newInstance().newPullParser());
    xstream.unmarshal(reader, null, null);
    IOUtils.closeQuietly(inStream);
    verify(mockProvider, times(1)).unmarshal(any(HierarchicalStreamReader.class), captor.capture());
    UnmarshallingContext context = captor.getValue();
    assertThat(context, notNullValue());
    assertThat(context.get(CswConstants.NAMESPACE_DECLARATIONS), instanceOf(Map.class));
    Map<String, String> namespaces = (Map) context.get(CswConstants.NAMESPACE_DECLARATIONS);
    assertThat(namespaces.get(CswConstants.XMLNS + CswConstants.NAMESPACE_DELIMITER + CswConstants.CSW_NAMESPACE_PREFIX), is(CswConstants.CSW_OUTPUT_SCHEMA));
    assertThat(namespaces.get(CswConstants.XMLNS + CswConstants.NAMESPACE_DELIMITER + CswConstants.DUBLIN_CORE_NAMESPACE_PREFIX), is(CswConstants.DUBLIN_CORE_SCHEMA));
    assertThat(namespaces.get(CswConstants.XMLNS + CswConstants.NAMESPACE_DELIMITER + CswConstants.DUBLIN_CORE_TERMS_NAMESPACE_PREFIX), is(CswConstants.DUBLIN_CORE_TERMS_SCHEMA));
    assertThat(namespaces.get(CswConstants.XMLNS + CswConstants.NAMESPACE_DELIMITER + CswConstants.OWS_NAMESPACE_PREFIX), is(CswConstants.OWS_NAMESPACE));
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) InputStreamReader(java.io.InputStreamReader) XStream(com.thoughtworks.xstream.XStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) XppReader(com.thoughtworks.xstream.io.xml.XppReader) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) UnmarshallingContext(com.thoughtworks.xstream.converters.UnmarshallingContext) Matchers.anyString(org.mockito.Matchers.anyString) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 13 with UnmarshallingContext

use of com.thoughtworks.xstream.converters.UnmarshallingContext in project ddf by codice.

the class TestCswTransformProvider method testUnmarshalCswRecordCoordinateOrder.

@Test
public void testUnmarshalCswRecordCoordinateOrder() throws Exception {
    when(mockInputManager.getTransformerBySchema(CswConstants.CSW_OUTPUT_SCHEMA)).thenReturn(mockCswRecordConverter);
    HierarchicalStreamReader reader = new WstxDriver().createReader(new StringReader(getRecord()));
    CswTransformProvider provider = new CswTransformProvider(null, mockInputManager);
    UnmarshallingContext context = new TreeUnmarshaller(null, null, null, null);
    context.put(CswConstants.AXIS_ORDER_PROPERTY, CswAxisOrder.LAT_LON);
    ArgumentCaptor<HierarchicalStreamReader> readerArgumentCaptor = ArgumentCaptor.forClass(HierarchicalStreamReader.class);
    ArgumentCaptor<UnmarshallingContext> unmarshallingContextArgumentCaptor = ArgumentCaptor.forClass(UnmarshallingContext.class);
    provider.unmarshal(reader, context);
    // Verify that CswRecordConverter unmarshal was called
    verify(mockCswRecordConverter, times(1)).unmarshal(readerArgumentCaptor.capture(), unmarshallingContextArgumentCaptor.capture());
    HierarchicalStreamReader hierarchicalStreamReader = readerArgumentCaptor.getValue();
    UnmarshallingContext unmarshallingContext = unmarshallingContextArgumentCaptor.getValue();
    // Verify that reader and context are passed to the CswRecordConverter correctly
    assertThat(hierarchicalStreamReader, is(reader));
    assertThat(unmarshallingContext, is(context));
    assertThat(context.get(CswConstants.AXIS_ORDER_PROPERTY), is(CswAxisOrder.LAT_LON));
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) StringReader(java.io.StringReader) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) UnmarshallingContext(com.thoughtworks.xstream.converters.UnmarshallingContext) TreeUnmarshaller(com.thoughtworks.xstream.core.TreeUnmarshaller) Test(org.junit.Test)

Example 14 with UnmarshallingContext

use of com.thoughtworks.xstream.converters.UnmarshallingContext in project ddf by codice.

the class TestGetRecordsMessageBodyReader method testConfigurationArguments.

@Test
public void testConfigurationArguments() throws Exception {
    CswSourceConfiguration config = new CswSourceConfiguration(encryptionService);
    config.setMetacardCswMappings(DefaultCswRecordMap.getCswToMetacardAttributeNames());
    config.setOutputSchema(CswConstants.CSW_OUTPUT_SCHEMA);
    config.setCswAxisOrder(CswAxisOrder.LAT_LON);
    config.putMetacardCswMapping(Core.THUMBNAIL, CswConstants.CSW_REFERENCES);
    config.putMetacardCswMapping(Core.RESOURCE_URI, CswConstants.CSW_SOURCE);
    GetRecordsMessageBodyReader reader = new GetRecordsMessageBodyReader(mockProvider, config);
    InputStream is = TestGetRecordsMessageBodyReader.class.getResourceAsStream("/getRecordsResponse.xml");
    MultivaluedMap<String, String> httpHeaders = new MultivaluedHashMap<>();
    ArgumentCaptor<UnmarshallingContext> captor = ArgumentCaptor.forClass(UnmarshallingContext.class);
    reader.readFrom(CswRecordCollection.class, null, null, null, httpHeaders, is);
    // Verify the context arguments were set correctly
    verify(mockProvider, times(1)).unmarshal(any(HierarchicalStreamReader.class), captor.capture());
    UnmarshallingContext context = captor.getValue();
    // Assert the properties needed for CswRecordConverter
    assertThat(context.get(CswConstants.CSW_MAPPING), notNullValue());
    Object cswMapping = context.get(CswConstants.CSW_MAPPING);
    assertThat(cswMapping, instanceOf(Map.class));
    assertThat(context.get(Core.RESOURCE_URI), instanceOf(String.class));
    assertThat(context.get(Core.RESOURCE_URI), is(CswConstants.CSW_SOURCE));
    assertThat(context.get(Core.THUMBNAIL), instanceOf(String.class));
    assertThat(context.get(Core.THUMBNAIL), is(CswConstants.CSW_REFERENCES));
    assertThat(context.get(CswConstants.AXIS_ORDER_PROPERTY), instanceOf(CswAxisOrder.class));
    assertThat(context.get(CswConstants.AXIS_ORDER_PROPERTY), is(CswAxisOrder.LAT_LON));
    // Assert the output Schema is set.
    assertThat(context.get(CswConstants.OUTPUT_SCHEMA_PARAMETER), instanceOf(String.class));
    assertThat(context.get(CswConstants.OUTPUT_SCHEMA_PARAMETER), is(CswConstants.CSW_OUTPUT_SCHEMA));
    assertThat(context.get(CswConstants.TRANSFORMER_LOOKUP_KEY), instanceOf(String.class));
    assertThat(context.get(CswConstants.TRANSFORMER_LOOKUP_KEY), is(TransformerManager.SCHEMA));
    assertThat(context.get(CswConstants.TRANSFORMER_LOOKUP_VALUE), instanceOf(String.class));
    assertThat(context.get(CswConstants.TRANSFORMER_LOOKUP_VALUE), is(CswConstants.CSW_OUTPUT_SCHEMA));
}
Also used : CswSourceConfiguration(org.codice.ddf.spatial.ogc.csw.catalog.common.CswSourceConfiguration) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) CswAxisOrder(org.codice.ddf.spatial.ogc.csw.catalog.common.CswAxisOrder) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) UnmarshallingContext(com.thoughtworks.xstream.converters.UnmarshallingContext) DefaultCswRecordMap(org.codice.ddf.spatial.ogc.csw.catalog.common.converter.DefaultCswRecordMap) HashMap(java.util.HashMap) Map(java.util.Map) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Test(org.junit.Test)

Example 15 with UnmarshallingContext

use of com.thoughtworks.xstream.converters.UnmarshallingContext in project ddf by codice.

the class GetRecordsMessageBodyReaderTest method testConfigurationArguments.

@Test
public void testConfigurationArguments() throws Exception {
    CswSourceConfiguration config = new CswSourceConfiguration(encryptionService, permissions);
    config.setMetacardCswMappings(DefaultCswRecordMap.getCswToMetacardAttributeNames());
    config.setOutputSchema(CswConstants.CSW_OUTPUT_SCHEMA);
    config.setCswAxisOrder(CswAxisOrder.LAT_LON);
    config.putMetacardCswMapping(Core.THUMBNAIL, CswConstants.CSW_REFERENCES);
    config.putMetacardCswMapping(Core.RESOURCE_URI, CswConstants.CSW_SOURCE);
    GetRecordsMessageBodyReader reader = new GetRecordsMessageBodyReader(mockProvider, config);
    ArgumentCaptor<UnmarshallingContext> captor = ArgumentCaptor.forClass(UnmarshallingContext.class);
    try (InputStream is = GetRecordsMessageBodyReaderTest.class.getResourceAsStream("/getRecordsResponse.xml")) {
        MultivaluedMap<String, String> httpHeaders = new MultivaluedHashMap<>();
        reader.readFrom(CswRecordCollection.class, null, null, null, httpHeaders, is);
    }
    // Verify the context arguments were set correctly
    verify(mockProvider, times(1)).unmarshal(any(HierarchicalStreamReader.class), captor.capture());
    UnmarshallingContext context = captor.getValue();
    // Assert the properties needed for CswRecordConverter
    assertThat(context.get(CswConstants.CSW_MAPPING), notNullValue());
    Object cswMapping = context.get(CswConstants.CSW_MAPPING);
    assertThat(cswMapping, instanceOf(Map.class));
    assertThat(context.get(Core.RESOURCE_URI), instanceOf(String.class));
    assertThat(context.get(Core.RESOURCE_URI), is(CswConstants.CSW_SOURCE));
    assertThat(context.get(Core.THUMBNAIL), instanceOf(String.class));
    assertThat(context.get(Core.THUMBNAIL), is(CswConstants.CSW_REFERENCES));
    assertThat(context.get(CswConstants.AXIS_ORDER_PROPERTY), instanceOf(CswAxisOrder.class));
    assertThat(context.get(CswConstants.AXIS_ORDER_PROPERTY), is(CswAxisOrder.LAT_LON));
    // Assert the output Schema is set.
    assertThat(context.get(CswConstants.OUTPUT_SCHEMA_PARAMETER), instanceOf(String.class));
    assertThat(context.get(CswConstants.OUTPUT_SCHEMA_PARAMETER), is(CswConstants.CSW_OUTPUT_SCHEMA));
    assertThat(context.get(CswConstants.TRANSFORMER_LOOKUP_KEY), instanceOf(String.class));
    assertThat(context.get(CswConstants.TRANSFORMER_LOOKUP_KEY), is(TransformerManager.SCHEMA));
    assertThat(context.get(CswConstants.TRANSFORMER_LOOKUP_VALUE), instanceOf(String.class));
    assertThat(context.get(CswConstants.TRANSFORMER_LOOKUP_VALUE), is(CswConstants.CSW_OUTPUT_SCHEMA));
}
Also used : CswSourceConfiguration(org.codice.ddf.spatial.ogc.csw.catalog.common.CswSourceConfiguration) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) CswAxisOrder(org.codice.ddf.spatial.ogc.csw.catalog.common.CswAxisOrder) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) UnmarshallingContext(com.thoughtworks.xstream.converters.UnmarshallingContext) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) Map(java.util.Map) DefaultCswRecordMap(org.codice.ddf.spatial.ogc.csw.catalog.common.converter.DefaultCswRecordMap) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Test(org.junit.Test)

Aggregations

UnmarshallingContext (com.thoughtworks.xstream.converters.UnmarshallingContext)21 Test (org.junit.Test)20 HierarchicalStreamReader (com.thoughtworks.xstream.io.HierarchicalStreamReader)19 TreeUnmarshaller (com.thoughtworks.xstream.core.TreeUnmarshaller)16 StringReader (java.io.StringReader)12 InputStream (java.io.InputStream)10 WstxDriver (com.thoughtworks.xstream.io.xml.WstxDriver)8 XppReader (com.thoughtworks.xstream.io.xml.XppReader)8 HashMap (java.util.HashMap)8 InputTransformer (ddf.catalog.transform.InputTransformer)6 Map (java.util.Map)6 Matchers.anyString (org.mockito.Matchers.anyString)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 XStream (com.thoughtworks.xstream.XStream)3 DomReader (com.thoughtworks.xstream.io.xml.DomReader)2 StaxDriver (com.thoughtworks.xstream.io.xml.StaxDriver)2 Metacard (ddf.catalog.data.Metacard)2 InputStreamReader (java.io.InputStreamReader)2 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)2