Search in sources :

Example 1 with TreeUnmarshaller

use of com.thoughtworks.xstream.core.TreeUnmarshaller in project ddf by codice.

the class TestXStreamAttributeCopier method testCopyXmlNamespaceDeclarationsIntoContext.

@Test
public void testCopyXmlNamespaceDeclarationsIntoContext() {
    UnmarshallingContext context = new TreeUnmarshaller(null, null, null, null);
    XStreamAttributeCopier.copyXmlNamespaceDeclarationsIntoContext(reader, context);
    @SuppressWarnings("unchecked") Map<String, String> attributeMap = (Map<String, String>) context.get(CswConstants.NAMESPACE_DECLARATIONS);
    assertThat(attributeMap.size(), is(2));
    assertThat(attributeMap.get("xmlns:csw"), is(CswConstants.CSW_OUTPUT_SCHEMA));
    assertThat(attributeMap.get("xmlns:ogc"), is(CswConstants.OGC_SCHEMA));
}
Also used : UnmarshallingContext(com.thoughtworks.xstream.converters.UnmarshallingContext) HashMap(java.util.HashMap) Map(java.util.Map) TreeUnmarshaller(com.thoughtworks.xstream.core.TreeUnmarshaller) Test(org.junit.Test)

Example 2 with TreeUnmarshaller

use of com.thoughtworks.xstream.core.TreeUnmarshaller 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 3 with TreeUnmarshaller

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

use of com.thoughtworks.xstream.core.TreeUnmarshaller 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 5 with TreeUnmarshaller

use of com.thoughtworks.xstream.core.TreeUnmarshaller in project ddf by codice.

the class CswTransformProviderTest method testUnmarshalMissingNamespaces.

@Test
public void testUnmarshalMissingNamespaces() throws Exception {
    InputTransformer mockInputTransformer = mock(InputTransformer.class);
    when(mockInputManager.getTransformerBySchema(anyString())).thenReturn(mockInputTransformer);
    Map<String, String> namespaces = new HashMap<>();
    namespaces.put("xmlns:csw", "http://www.opengis.net/cat/csw/2.0.2");
    namespaces.put("xmlns:dc", "http://purl.org/dc/elements/1.1/");
    namespaces.put("xmlns:dct", "http://purl.org/dc/terms/");
    HierarchicalStreamReader reader = new XppReader(new StringReader(getRecordMissingNamespaces()), XmlPullParserFactory.newInstance().newPullParser());
    CswTransformProvider provider = new CswTransformProvider(null, mockInputManager);
    UnmarshallingContext context = new TreeUnmarshaller(null, null, null, null);
    context.put(CswConstants.NAMESPACE_DECLARATIONS, namespaces);
    context.put(CswConstants.OUTPUT_SCHEMA_PARAMETER, OTHER_SCHEMA);
    ArgumentCaptor<InputStream> captor = ArgumentCaptor.forClass(InputStream.class);
    provider.unmarshal(reader, context);
    // Verify the context arguments were set correctly
    verify(mockInputTransformer, times(1)).transform(captor.capture());
    InputStream inStream = captor.getValue();
    String result = IOUtils.toString(inStream);
    XMLUnit.setIgnoreWhitespace(true);
    XMLAssert.assertXMLEqual(getRecord(), result);
}
Also used : HashMap(java.util.HashMap) XppReader(com.thoughtworks.xstream.io.xml.XppReader) InputStream(java.io.InputStream) StringReader(java.io.StringReader) HierarchicalStreamReader(com.thoughtworks.xstream.io.HierarchicalStreamReader) UnmarshallingContext(com.thoughtworks.xstream.converters.UnmarshallingContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) InputTransformer(ddf.catalog.transform.InputTransformer) TreeUnmarshaller(com.thoughtworks.xstream.core.TreeUnmarshaller) Test(org.junit.Test)

Aggregations

UnmarshallingContext (com.thoughtworks.xstream.converters.UnmarshallingContext)16 TreeUnmarshaller (com.thoughtworks.xstream.core.TreeUnmarshaller)16 Test (org.junit.Test)16 HierarchicalStreamReader (com.thoughtworks.xstream.io.HierarchicalStreamReader)14 StringReader (java.io.StringReader)12 WstxDriver (com.thoughtworks.xstream.io.xml.WstxDriver)6 XppReader (com.thoughtworks.xstream.io.xml.XppReader)6 InputTransformer (ddf.catalog.transform.InputTransformer)6 InputStream (java.io.InputStream)6 HashMap (java.util.HashMap)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 Matchers.anyString (org.mockito.Matchers.anyString)4 DomReader (com.thoughtworks.xstream.io.xml.DomReader)2 StaxDriver (com.thoughtworks.xstream.io.xml.StaxDriver)2 Metacard (ddf.catalog.data.Metacard)2 Map (java.util.Map)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 Document (org.w3c.dom.Document)2