Search in sources :

Example 16 with WstxDriver

use of com.thoughtworks.xstream.io.xml.WstxDriver in project ddf by codice.

the class TestCswTransformProvider method testUnmarshalCswRecord.

@Test
public void testUnmarshalCswRecord() 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);
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    provider.unmarshal(reader, context);
    // Verify the context arguments were set correctly
    verify(mockInputManager, times(1)).getTransformerBySchema(captor.capture());
    String outputSchema = captor.getValue();
    assertThat(outputSchema, is(CswConstants.CSW_OUTPUT_SCHEMA));
}
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) Matchers.anyString(org.mockito.Matchers.anyString) TreeUnmarshaller(com.thoughtworks.xstream.core.TreeUnmarshaller) Test(org.junit.Test)

Example 17 with WstxDriver

use of com.thoughtworks.xstream.io.xml.WstxDriver in project ddf by codice.

the class TestCswTransformProvider method testMarshalCswRecord.

@Test
public void testMarshalCswRecord() throws Exception {
    when(mockMetacardManager.getTransformerBySchema(CswConstants.CSW_OUTPUT_SCHEMA)).thenReturn(mockCswRecordConverter);
    when(mockCswRecordConverter.transform(any(Metacard.class), any(Map.class))).thenReturn(new BinaryContentImpl(IOUtils.toInputStream(getRecord()), new MimeType(MediaType.APPLICATION_XML)));
    StringWriter stringWriter = new StringWriter();
    HierarchicalStreamWriter writer = new WstxDriver().createWriter(stringWriter);
    CswTransformProvider provider = new CswTransformProvider(mockMetacardManager, null);
    MarshallingContext context = new TreeMarshaller(writer, null, null);
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    provider.marshal(getMetacard(), writer, context);
    // Verify the context arguments were set correctly
    verify(mockMetacardManager, times(1)).getTransformerBySchema(captor.capture());
    String outputSchema = captor.getValue();
    assertThat(outputSchema, is(CswConstants.CSW_OUTPUT_SCHEMA));
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) TreeMarshaller(com.thoughtworks.xstream.core.TreeMarshaller) Metacard(ddf.catalog.data.Metacard) StringWriter(java.io.StringWriter) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) BinaryContentImpl(ddf.catalog.data.impl.BinaryContentImpl) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) Matchers.anyString(org.mockito.Matchers.anyString) HashMap(java.util.HashMap) Map(java.util.Map) MimeType(javax.activation.MimeType) Test(org.junit.Test)

Example 18 with WstxDriver

use of com.thoughtworks.xstream.io.xml.WstxDriver 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 19 with WstxDriver

use of com.thoughtworks.xstream.io.xml.WstxDriver 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 20 with WstxDriver

use of com.thoughtworks.xstream.io.xml.WstxDriver in project ddf by codice.

the class TestGenericFeatureConverter method testUnmarshalMultiQueryFeatureCollectionXmlToObject.

@Test
public void testUnmarshalMultiQueryFeatureCollectionXmlToObject() {
    XStream xstream = new XStream(new WstxDriver());
    FeatureCollectionConverterWfs20 fcConverter = new FeatureCollectionConverterWfs20();
    Map<String, FeatureConverter> fcMap = new HashMap<String, FeatureConverter>();
    GenericFeatureConverterWfs20 converter = new GenericFeatureConverterWfs20();
    fcMap.put("states", converter);
    fcMap.put("streams", converter);
    fcConverter.setFeatureConverterMap(fcMap);
    xstream.registerConverter(fcConverter);
    converter.setMetacardType(buildStatesMetacardType());
    converter.setCoordinateOrder(GeospatialUtil.LAT_LON_ORDER);
    xstream.registerConverter(converter);
    xstream.alias("FeatureCollection", Wfs20FeatureCollection.class);
    InputStream is = TestGenericFeatureConverter.class.getResourceAsStream("/geoserver_sample.xml");
    Wfs20FeatureCollection wfc = (Wfs20FeatureCollection) xstream.fromXML(is);
    assertEquals(7, wfc.getMembers().size());
    Metacard mc = wfc.getMembers().get(0);
    assertEquals(mc.getId(), "states.10");
    // Verifies that lat/lon was swapped to lon/lat order for the WKT conversion
    // to set the metacard's location        
    assertTrue(mc.getLocation().startsWith("MULTIPOLYGON (((-89.104965 36.953869, -89.129585 36.86644, -89.166496 36.843422000000004,"));
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) Metacard(ddf.catalog.data.Metacard) HashMap(java.util.HashMap) XStream(com.thoughtworks.xstream.XStream) InputStream(java.io.InputStream) Wfs20FeatureCollection(org.codice.ddf.spatial.ogc.wfs.v2_0_0.catalog.common.Wfs20FeatureCollection) FeatureConverter(org.codice.ddf.spatial.ogc.wfs.catalog.converter.FeatureConverter) Test(org.junit.Test)

Aggregations

WstxDriver (com.thoughtworks.xstream.io.xml.WstxDriver)29 Test (org.junit.Test)28 XStream (com.thoughtworks.xstream.XStream)22 Metacard (ddf.catalog.data.Metacard)19 InputStream (java.io.InputStream)19 HashMap (java.util.HashMap)12 FeatureConverter (org.codice.ddf.spatial.ogc.wfs.catalog.converter.FeatureConverter)8 GmlGeometryConverter (org.codice.ddf.spatial.ogc.wfs.catalog.converter.impl.GmlGeometryConverter)8 HierarchicalStreamReader (com.thoughtworks.xstream.io.HierarchicalStreamReader)6 Wfs20FeatureCollection (org.codice.ddf.spatial.ogc.wfs.v2_0_0.catalog.common.Wfs20FeatureCollection)6 Matchers.anyString (org.mockito.Matchers.anyString)6 GenericFeatureConverter (org.codice.ddf.spatial.ogc.wfs.catalog.converter.impl.GenericFeatureConverter)5 UnmarshallingContext (com.thoughtworks.xstream.converters.UnmarshallingContext)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 StringWriter (java.io.StringWriter)4 Ignore (org.junit.Ignore)4 MarshallingContext (com.thoughtworks.xstream.converters.MarshallingContext)3 TreeMarshaller (com.thoughtworks.xstream.core.TreeMarshaller)3 TreeUnmarshaller (com.thoughtworks.xstream.core.TreeUnmarshaller)3 HierarchicalStreamWriter (com.thoughtworks.xstream.io.HierarchicalStreamWriter)3