Search in sources :

Example 66 with XStream

use of com.thoughtworks.xstream.XStream in project ddf by codice.

the class TestCswRecordConverter method testUnmarshalCswRecordWithProductAndThumbnail.

@Test
public void testUnmarshalCswRecordWithProductAndThumbnail() throws URISyntaxException, IOException, JAXBException, ParserConfigurationException, SAXException {
    XStream xstream = new XStream(new WstxDriver());
    xstream.registerConverter(converter);
    InputStream is = TestCswRecordConverter.class.getResourceAsStream("/Csw_Record.xml");
    // get the URL to the thumbnail image and stick it in the xml string
    // this makes the test filesystem independent
    URL thumbnail = TestCswRecordConverter.class.getResource("/ddf_globe.png");
    String xml = null;
    if (thumbnail != null) {
        StringWriter writer = new StringWriter();
        IOUtils.copy(is, writer);
        xml = writer.toString();
        xml = xml.replace(THUMBNAIL_URL, thumbnail.toString());
    }
    xstream.alias("csw:Record", MetacardImpl.class);
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(IOUtils.toInputStream(xml));
    HierarchicalStreamReader reader = new DomReader(doc);
    DataHolder holder = xstream.newDataHolder();
    Metacard mc = (Metacard) xstream.unmarshal(reader, null, holder);
    assertThat(mc, notNullValue());
    String productUrl = "http://example.com/product.pdf";
    assertThat(mc.getAttribute(Core.RESOURCE_URI).getValue(), is(productUrl));
    assertThat(mc.getThumbnail(), is(getThumbnailByteArray(thumbnail)));
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DomReader(com.thoughtworks.xstream.io.xml.DomReader) XStream(com.thoughtworks.xstream.XStream) InputStream(java.io.InputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) Document(org.w3c.dom.Document) URL(java.net.URL) Metacard(ddf.catalog.data.Metacard) StringWriter(java.io.StringWriter) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DataHolder(com.thoughtworks.xstream.converters.DataHolder) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) Test(org.junit.Test)

Example 67 with XStream

use of com.thoughtworks.xstream.XStream in project ddf by codice.

the class TestCswRecordConverter method testUnmarshalNoNamespaceDeclaration.

@Test
public void testUnmarshalNoNamespaceDeclaration() throws IOException, SAXException {
    XStream xstream = new XStream(new XppDriver());
    xstream.registerConverter(converter);
    xstream.alias("Record", MetacardImpl.class);
    xstream.alias("csw:Record", MetacardImpl.class);
    InputStream is = IOUtils.toInputStream(getRecordNoNamespaceDeclaration());
    Metacard mc = (Metacard) xstream.fromXML(is);
    Metacard expectedMetacard = getTestMetacard();
    assertThat(mc, notNullValue());
    assertThat(mc.getContentTypeName(), is(expectedMetacard.getContentTypeName()));
    assertThat(mc.getCreatedDate(), is(expectedMetacard.getCreatedDate()));
    assertThat(mc.getEffectiveDate(), is(expectedMetacard.getEffectiveDate()));
    assertThat(mc.getId(), is(expectedMetacard.getId()));
    assertThat(mc.getModifiedDate(), is(expectedMetacard.getModifiedDate()));
    assertThat(mc.getTitle(), is(expectedMetacard.getTitle()));
    assertThat(mc.getResourceURI(), is(expectedMetacard.getResourceURI()));
}
Also used : Metacard(ddf.catalog.data.Metacard) XppDriver(com.thoughtworks.xstream.io.xml.XppDriver) XStream(com.thoughtworks.xstream.XStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 68 with XStream

use of com.thoughtworks.xstream.XStream in project ddf by codice.

the class TestCswRecordConverter method testUnmarshalSingleCswRecordToMetacardContentTypeMapsToFormat.

@Test
public void testUnmarshalSingleCswRecordToMetacardContentTypeMapsToFormat() throws ParserConfigurationException, IOException, SAXException {
    XStream xstream = new XStream(new WstxDriver());
    xstream.registerConverter(converter);
    xstream.alias("csw:Record", MetacardImpl.class);
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(TestCswRecordConverter.class.getResource("/Csw_Record.xml").getPath());
    HierarchicalStreamReader reader = new DomReader(doc);
    DataHolder holder = xstream.newDataHolder();
    Metacard mc = (Metacard) xstream.unmarshal(reader, null, holder);
    assertThat(mc, notNullValue());
    assertThat(mc.getContentTypeName(), is("IMAGE-PRODUCT"));
    assertThat(mc.getAttribute(Media.FORMAT).getValue(), is("PDF"));
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) Metacard(ddf.catalog.data.Metacard) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DomReader(com.thoughtworks.xstream.io.xml.DomReader) DocumentBuilder(javax.xml.parsers.DocumentBuilder) XStream(com.thoughtworks.xstream.XStream) DataHolder(com.thoughtworks.xstream.converters.DataHolder) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 69 with XStream

use of com.thoughtworks.xstream.XStream in project ddf by codice.

the class TestCswRecordConverter method testUnmarshalCswRecordMultipleResourceUri.

@Test
public void testUnmarshalCswRecordMultipleResourceUri() {
    XStream xstream = new XStream(new WstxDriver());
    xstream.registerConverter(converter);
    xstream.alias("Record", MetacardImpl.class);
    InputStream is = TestCswRecordConverter.class.getResourceAsStream("/Csw_Record_MultiValueFields.xml");
    Metacard mc = (Metacard) xstream.fromXML(is);
    assertThat(mc.getResourceURI().toString(), is("http://example.com/product_supplement.pdf"));
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) Metacard(ddf.catalog.data.Metacard) XStream(com.thoughtworks.xstream.XStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 70 with XStream

use of com.thoughtworks.xstream.XStream in project ddf by codice.

the class TestGenericFeatureConverter method testUnmarshalMultiQueryFeatureCollectionXmlToObjectWithMetacardMapper.

/*
     * This test will check is the MetacardMapper maps the feature value of 'states.STATE_NAME' to the metacard property 'title'.
     */
@Test
public void testUnmarshalMultiQueryFeatureCollectionXmlToObjectWithMetacardMapper() {
    //Create Metacard Mapper
    String featureProp = "ext.states.STATE_NAME";
    String metacardAttr = "title";
    MetacardMapper metacardMapper = mock(MetacardMapper.class);
    when(metacardMapper.getMetacardAttribute(featureProp)).thenReturn(metacardAttr);
    XStream xstream = new XStream(new WstxDriver());
    FeatureCollectionConverterWfs20 fcConverter = new FeatureCollectionConverterWfs20();
    Map<String, FeatureConverter> fcMap = new HashMap<String, FeatureConverter>();
    GenericFeatureConverterWfs20 converter = new GenericFeatureConverterWfs20(metacardMapper);
    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.getTitle(), "Missouri");
    // 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) MetacardMapper(org.codice.ddf.spatial.ogc.wfs.catalog.mapper.MetacardMapper) Test(org.junit.Test)

Aggregations

XStream (com.thoughtworks.xstream.XStream)199 Test (org.junit.Test)55 IOException (java.io.IOException)33 InputStream (java.io.InputStream)29 WstxDriver (com.thoughtworks.xstream.io.xml.WstxDriver)22 Metacard (ddf.catalog.data.Metacard)21 DomDriver (com.thoughtworks.xstream.io.xml.DomDriver)17 HashMap (java.util.HashMap)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 StaxDriver (com.thoughtworks.xstream.io.xml.StaxDriver)12 XStreamUtils.createTrustingXStream (org.kie.soup.commons.xstream.XStreamUtils.createTrustingXStream)12 Matchers.anyString (org.mockito.Matchers.anyString)11 HierarchicalStreamWriter (com.thoughtworks.xstream.io.HierarchicalStreamWriter)10 CswRecordCollection (org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)10 FileNotFoundException (java.io.FileNotFoundException)9 Writer (java.io.Writer)8 ArrayList (java.util.ArrayList)8 GmlGeometryConverter (org.codice.ddf.spatial.ogc.wfs.catalog.converter.impl.GmlGeometryConverter)8 XStreamException (com.thoughtworks.xstream.XStreamException)7 MarshallingContext (com.thoughtworks.xstream.converters.MarshallingContext)7