Search in sources :

Example 6 with HierarchicalStreamReader

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

the class TestCswTransformProvider method testUnmarshalOtherSchema.

@Test
public void testUnmarshalOtherSchema() throws Exception {
    InputTransformer mockInputTransformer = mock(InputTransformer.class);
    when(mockInputManager.getTransformerByProperty(TransformerManager.SCHEMA, OTHER_SCHEMA)).thenReturn(mockInputTransformer);
    when(mockInputTransformer.transform(any(InputStream.class))).thenReturn(getMetacard());
    // XppReader is not namespace aware so it will read the XML and ignore the namespaces
    // WstxReader is namespace aware. It may fail for XML fragments.
    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.TRANSFORMER_LOOKUP_KEY, TransformerManager.SCHEMA);
    context.put(CswConstants.TRANSFORMER_LOOKUP_VALUE, OTHER_SCHEMA);
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    provider.unmarshal(reader, context);
    // Verify the context arguments were set correctly
    verify(mockInputManager, times(1)).getTransformerByProperty(captor.capture(), captor.capture());
    String outputSchema = captor.getValue();
    assertThat(outputSchema, is(OTHER_SCHEMA));
}
Also used : InputStream(java.io.InputStream) XppReader(com.thoughtworks.xstream.io.xml.XppReader) 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 7 with HierarchicalStreamReader

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

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

the class TestCswUnmarshallHelper method testConvertRecordPropertyToMetacardAttribute.

@Test
public void testConvertRecordPropertyToMetacardAttribute() {
    valueMap.put(AttributeType.AttributeFormat.BINARY, "TEST_BINARY");
    valueMap.put(AttributeType.AttributeFormat.GEOMETRY, TEST_BOUNDING_BOX);
    matcherMap.put(AttributeType.AttributeFormat.BINARY, notNullValue());
    matcherMap.put(AttributeType.AttributeFormat.GEOMETRY, is("POLYGON ((44.792 -6.171, 44.792 -2.228, 51.126 -2.228, 51.126 -6.171, 44.792 -6.171))"));
    AttributeType.AttributeFormat[] attributeFormats = AttributeType.AttributeFormat.values();
    for (AttributeType.AttributeFormat attributeFormat : attributeFormats) {
        HierarchicalStreamReader reader = getReader(attributeFormat);
        Serializable ser = CswUnmarshallHelper.convertRecordPropertyToMetacardAttribute(attributeFormat, reader, CswAxisOrder.LAT_LON);
        Matcher m = matcherMap.get(attributeFormat);
        assertThat(ser, m);
    }
}
Also used : Serializable(java.io.Serializable) Matcher(org.hamcrest.Matcher) AttributeType(ddf.catalog.data.AttributeType) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) Test(org.junit.Test)

Example 9 with HierarchicalStreamReader

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

the class TestXStreamAttributeCopier method testCopyXml.

@Test
public void testCopyXml() throws IOException {
    // Move to the <Delete> element.
    reader.moveDown();
    StringWriter sw = new StringWriter();
    Map<String, String> attributeMap = new HashMap<>();
    attributeMap.put("foo", "bar");
    HierarchicalStreamReader deleteReader = XStreamAttributeCopier.copyXml(reader, sw, attributeMap);
    // Verify that the reader is at the end of <Delete>.
    assertThat(reader.hasMoreChildren(), is(false));
    // Move back up to <Transaction>.
    reader.moveUp();
    // Move down to <Insert>.
    reader.moveDown();
    // Verify that the reader is now at <Insert>.
    assertThat(reader.getNodeName(), is("csw:Insert"));
    assertThat(deleteReader.getNodeName(), is("csw:Delete"));
    assertThat(deleteReader.getAttributeCount(), is(3));
    // Verify that the attribute was copied to the element.
    assertThat(deleteReader.getAttribute("foo"), is("bar"));
    // Verify that the XML was copied correctly.
    assertThat(deleteReader.getAttribute(CswConstants.TYPE_NAME_PARAMETER), is("csw:Record"));
    assertThat(deleteReader.getAttribute(CswConstants.HANDLE_PARAMETER), is("something"));
    deleteReader.moveDown();
    assertThat(deleteReader.getNodeName(), is("csw:Constraint"));
    assertThat(deleteReader.getAttribute(CswConstants.VERSION), is("2.0.0"));
    deleteReader.moveDown();
    assertThat(deleteReader.getNodeName(), is("ogc:Filter"));
    deleteReader.moveDown();
    assertThat(deleteReader.getNodeName(), is("ogc:PropertyIsEqualTo"));
    deleteReader.moveDown();
    assertThat(deleteReader.getNodeName(), is("ogc:PropertyName"));
    assertThat(deleteReader.getValue(), is("title"));
    deleteReader.moveUp();
    deleteReader.moveDown();
    assertThat(deleteReader.getNodeName(), is("ogc:Literal"));
    assertThat(deleteReader.getValue(), is("Test Title"));
    deleteReader.moveUp();
    // Verify that PropertyIsEqualTo has no more children.
    assertThat(deleteReader.hasMoreChildren(), is(false));
    deleteReader.moveUp();
    // Verify that Filter has no more children.
    assertThat(deleteReader.hasMoreChildren(), is(false));
    deleteReader.moveUp();
    // Verify that Constraint has no more children.
    assertThat(deleteReader.hasMoreChildren(), is(false));
    deleteReader.moveUp();
    // Verify that Delete has no more children.
    assertThat(deleteReader.hasMoreChildren(), is(false));
}
Also used : StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) Test(org.junit.Test)

Example 10 with HierarchicalStreamReader

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

the class TestXstreamPathConverter method testAttributeValid.

@Test
public void testAttributeValid() throws XMLStreamException {
    XMLStreamReader streamReader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(GML_XML));
    HierarchicalStreamReader reader = new StaxReader(new QNameMap(), streamReader);
    XstreamPathValueTracker pathValueTracker = (XstreamPathValueTracker) xstream.unmarshal(reader, null, argumentHolder);
    assertThat(pathValueTracker.getFirstValue(POLYGON_GML_ID_PATH), is("p1"));
}
Also used : StaxReader(com.thoughtworks.xstream.io.xml.StaxReader) XMLStreamReader(javax.xml.stream.XMLStreamReader) StringReader(java.io.StringReader) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) QNameMap(com.thoughtworks.xstream.io.xml.QNameMap) 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