Search in sources :

Example 1 with WstxDriver

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

the class TestGetRecordsResponseConverter method testUnmarshalGetRecordsResponseConversionWithEmptyBoundingBox.

@Test
public void testUnmarshalGetRecordsResponseConversionWithEmptyBoundingBox() {
    XStream xstream = new XStream(new WstxDriver());
    xstream.setClassLoader(this.getClass().getClassLoader());
    GetRecordsResponseConverter grrc = new GetRecordsResponseConverter(mockProvider);
    xstream.registerConverter(grrc);
    xstream.alias("GetRecordsResponse", CswRecordCollection.class);
    String xml = "<csw:GetRecordsResponse xmlns:csw=\"http://www.opengis.net/cat/csw\">\r\n" + "  <csw:SearchStatus status=\"subset\" timestamp=\"2013-05-01T02:13:36+0200\"/>\r\n" + "  <csw:SearchResults elementSet=\"full\" nextRecord=\"11\" numberOfRecordsMatched=\"479\" numberOfRecordsReturned=\"10\" recordSchema=\"csw:Record\">\r\n" + "    <csw:Record xmlns:csw=\"http://www.opengis.net/cat/csw\">\r\n" + "      <dc:identifier xmlns:dc=\"http://purl.org/dc/elements/1.1/\">{8C1F6297-EC96-4302-A01E-14988C9149FD}</dc:identifier>\r\n" + "      <dc:title xmlns:dc=\"http://purl.org/dc/elements/1.1/\">title 1</dc:title>\r\n" + "      <dct:modified xmlns:dct=\"http://purl.org/dc/terms/\">2008-12-15</dct:modified>\r\n" + "      <dc:subject xmlns:dc=\"http://purl.org/dc/elements/1.1/\">subject 1</dc:subject>\r\n" + "      <dc:subject xmlns:dc=\"http://purl.org/dc/elements/1.1/\">second subject</dc:subject>\r\n" + "      <dct:abstract xmlns:dct=\"http://purl.org/dc/terms/\">abstract 1</dct:abstract>\r\n" + "      <dc:rights xmlns:dc=\"http://purl.org/dc/elements/1.1/\">copyright 1</dc:rights>\r\n" + "      <dc:rights xmlns:dc=\"http://purl.org/dc/elements/1.1/\">copyright 2</dc:rights>\r\n" + "      <dc:language xmlns:dc=\"http://purl.org/dc/elements/1.1/\">english</dc:language>      \r\n" + "      <ows:BoundingBox xmlns:ows=\"http://www.opengis.net/ows\" crs=\"EPSG:RD_New (28992)\">\r\n" + "        <ows:LowerCorner></ows:LowerCorner>\r\n" + "        <ows:UpperCorner></ows:UpperCorner>\r\n" + "      </ows:BoundingBox>   \r\n" + "      <dc:type xmlns:dc=\"http://purl.org/dc/elements/1.1/\">dataset</dc:type>\r\n" + "      <dc:format xmlns:dc=\"http://purl.org/dc/elements/1.1/\">Shapefile</dc:format> \r\n" + "    </csw:Record>\r\n" + "  </csw:SearchResults>\r\n" + "</csw:GetRecordsResponse>";
    InputStream inStream = IOUtils.toInputStream(xml);
    CswRecordCollection cswRecords = (CswRecordCollection) xstream.fromXML(inStream);
    IOUtils.closeQuietly(inStream);
    assertThat(cswRecords.getNumberOfRecordsReturned(), is(10L));
    assertThat(cswRecords.getNumberOfRecordsMatched(), is(479L));
    List<Metacard> metacards = cswRecords.getCswRecords();
    assertThat(metacards, not(nullValue()));
    assertThat(metacards.size(), is(1));
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) Metacard(ddf.catalog.data.Metacard) XStream(com.thoughtworks.xstream.XStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CswRecordCollection(org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 2 with WstxDriver

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

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

the class TestCswTransformProvider method testMarshalOtherSchema.

@Test
public void testMarshalOtherSchema() throws Exception {
    when(mockMetacardManager.getTransformerByProperty(TransformerManager.SCHEMA, OTHER_SCHEMA)).thenReturn(mockMetacardTransformer);
    when(mockMetacardTransformer.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);
    context.put(CswConstants.TRANSFORMER_LOOKUP_KEY, TransformerManager.SCHEMA);
    context.put(CswConstants.TRANSFORMER_LOOKUP_VALUE, OTHER_SCHEMA);
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    provider.marshal(getMetacard(), writer, context);
    // Verify the context arguments were set correctly
    verify(mockMetacardManager, times(1)).getTransformerByProperty(captor.capture(), captor.capture());
    String outputSchema = captor.getValue();
    assertThat(outputSchema, is(OTHER_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 4 with WstxDriver

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

the class TestCswTransformProvider method testUnmarshalNoTransformers.

@Test(expected = ConversionException.class)
public void testUnmarshalNoTransformers() throws Exception {
    when(mockInputManager.getTransformerBySchema(anyString())).thenReturn(null);
    HierarchicalStreamReader reader = new WstxDriver().createReader(new StringReader(getRecord()));
    CswTransformProvider provider = new CswTransformProvider(null, mockInputManager);
    UnmarshallingContext context = new TreeUnmarshaller(null, null, null, null);
    provider.unmarshal(reader, context);
}
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 5 with WstxDriver

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

the class TestCswTransformProvider method testMarshalNoTransformers.

@Test(expected = ConversionException.class)
public void testMarshalNoTransformers() throws Exception {
    when(mockMetacardManager.getTransformerBySchema(anyString())).thenReturn(null);
    StringWriter stringWriter = new StringWriter();
    HierarchicalStreamWriter writer = new WstxDriver().createWriter(stringWriter);
    CswTransformProvider provider = new CswTransformProvider(mockMetacardManager, null);
    MarshallingContext context = new TreeMarshaller(writer, null, null);
    provider.marshal(getMetacard(), writer, context);
}
Also used : WstxDriver(com.thoughtworks.xstream.io.xml.WstxDriver) TreeMarshaller(com.thoughtworks.xstream.core.TreeMarshaller) StringWriter(java.io.StringWriter) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) 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