Search in sources :

Example 1 with HierarchicalStreamReader

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

the class TestBoundingBoxReader method testJTSConverterEPSG32636.

@Test
public void testJTSConverterEPSG32636() throws Exception {
    // Setup
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse("src/test/resources/BoundingBoxEPSG32636.xml");
    HierarchicalStreamReader hReader = new DomReader(doc);
    BoundingBoxReader boundingBoxReader = new BoundingBoxReader(hReader, CswAxisOrder.LAT_LON);
    // Perform Test
    String wktInLonLat = boundingBoxReader.getWkt();
    LOGGER.debug("WKT: {}", wktInLonLat);
    // Verify
    assertThat(wktInLonLat, is(POLYGON_UTM_LON_LAT));
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DomReader(com.thoughtworks.xstream.io.xml.DomReader) DocumentBuilder(javax.xml.parsers.DocumentBuilder) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 2 with HierarchicalStreamReader

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

the class TestCswUnmarshallHelper method getReader.

private HierarchicalStreamReader getReader(AttributeType.AttributeFormat attributeFormat) {
    HierarchicalStreamReader reader = mock(HierarchicalStreamReader.class);
    if (attributeFormat.equals(AttributeType.AttributeFormat.GEOMETRY)) {
        Stack<String> boundingBoxNodes = new Stack<>();
        boundingBoxNodes.push(valueMap.get(attributeFormat));
        boundingBoxNodes.push("-2.228 51.126");
        boundingBoxNodes.push("UpperCorner");
        boundingBoxNodes.push("-6.171 44.792");
        boundingBoxNodes.push("LowerCorner");
        boundingBoxNodes.push("BoundingBox");
        boundingBoxNodes.push("BoundingBox");
        boundingBoxNodes.push("BoundingBox");
        Answer<String> answer = invocationOnMock -> boundingBoxNodes.pop();
        when(reader.getNodeName()).thenAnswer(answer);
        when(reader.getValue()).thenAnswer(answer);
    } else {
        when(reader.getValue()).thenReturn(valueMap.get(attributeFormat));
    }
    return reader;
}
Also used : ISODateTimeFormat(org.joda.time.format.ISODateTimeFormat) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) AttributeType(ddf.catalog.data.AttributeType) Stack(java.util.Stack) Serializable(java.io.Serializable) LocalDate(org.joda.time.LocalDate) Answer(org.mockito.stubbing.Answer) CswAxisOrder(org.codice.ddf.spatial.ogc.csw.catalog.common.CswAxisOrder) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Map(java.util.Map) Matcher(org.hamcrest.Matcher) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DateFormat(java.text.DateFormat) Mockito.mock(org.mockito.Mockito.mock) Before(org.junit.Before) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) Stack(java.util.Stack)

Example 3 with HierarchicalStreamReader

use of com.thoughtworks.xstream.io.HierarchicalStreamReader 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 4 with HierarchicalStreamReader

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

the class TestCswRecordConverter method testSetMetacardContentTypeToCswRecordType.

/**
     * Test to verify that a metacard's content type is set to the CSW Record's type field.
     */
@Test
public void testSetMetacardContentTypeToCswRecordType() throws ParserConfigurationException, SAXException, IOException {
    // Setup
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(TestCswRecordConverter.class.getResource("/Csw_Record.xml").getPath());
    HierarchicalStreamReader reader = new DomReader(doc);
    UnmarshallingContext context = new TreeUnmarshaller(null, null, null, null);
    // Perform test
    Metacard metacard = (Metacard) converter.unmarshal(reader, context);
    // Verify
    LOGGER.debug("metacard id: {}", metacard.getId());
    LOGGER.debug("metacard content type: {}", metacard.getContentTypeName());
    assertThat(metacard.getContentTypeName(), is("IMAGE-PRODUCT"));
}
Also used : Metacard(ddf.catalog.data.Metacard) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DomReader(com.thoughtworks.xstream.io.xml.DomReader) DocumentBuilder(javax.xml.parsers.DocumentBuilder) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) UnmarshallingContext(com.thoughtworks.xstream.converters.UnmarshallingContext) Document(org.w3c.dom.Document) TreeUnmarshaller(com.thoughtworks.xstream.core.TreeUnmarshaller) Test(org.junit.Test)

Example 5 with HierarchicalStreamReader

use of com.thoughtworks.xstream.io.HierarchicalStreamReader 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)

Aggregations

HierarchicalStreamReader (com.thoughtworks.xstream.io.HierarchicalStreamReader)34 Test (org.junit.Test)28 DomReader (com.thoughtworks.xstream.io.xml.DomReader)13 DocumentBuilder (javax.xml.parsers.DocumentBuilder)13 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)13 Document (org.w3c.dom.Document)13 StringReader (java.io.StringReader)11 UnmarshallingContext (com.thoughtworks.xstream.converters.UnmarshallingContext)8 InputStream (java.io.InputStream)8 TreeUnmarshaller (com.thoughtworks.xstream.core.TreeUnmarshaller)7 WstxDriver (com.thoughtworks.xstream.io.xml.WstxDriver)6 XppReader (com.thoughtworks.xstream.io.xml.XppReader)6 HashMap (java.util.HashMap)6 XStream (com.thoughtworks.xstream.XStream)5 Metacard (ddf.catalog.data.Metacard)5 Serializable (java.io.Serializable)5 DataHolder (com.thoughtworks.xstream.converters.DataHolder)4 QNameMap (com.thoughtworks.xstream.io.xml.QNameMap)4 StaxReader (com.thoughtworks.xstream.io.xml.StaxReader)4 IOException (java.io.IOException)4