Search in sources :

Example 11 with TreeUnmarshaller

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

the class TestCswTransformProvider method testUnmarshalCopyPreservesNamespaces.

@Test
public void testUnmarshalCopyPreservesNamespaces() throws Exception {
    InputTransformer mockInputTransformer = mock(InputTransformer.class);
    when(mockInputManager.getTransformerBySchema(anyString())).thenReturn(mockInputTransformer);
    StaxDriver driver = new StaxDriver();
    driver.setRepairingNamespace(true);
    driver.getQnameMap().setDefaultNamespace(CswConstants.CSW_OUTPUT_SCHEMA);
    driver.getQnameMap().setDefaultPrefix(CswConstants.CSW_NAMESPACE_PREFIX);
    // Have to use XppReader in order to preserve the namespaces.
    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.OUTPUT_SCHEMA_PARAMETER, "http://example.com/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 : StaxDriver(com.thoughtworks.xstream.io.xml.StaxDriver) 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) Matchers.anyString(org.mockito.Matchers.anyString) InputTransformer(ddf.catalog.transform.InputTransformer) TreeUnmarshaller(com.thoughtworks.xstream.core.TreeUnmarshaller) Test(org.junit.Test)

Example 12 with TreeUnmarshaller

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

the class TestCswTransformProvider method testUnmarshalCswRecordCoordinateOrder.

@Test
public void testUnmarshalCswRecordCoordinateOrder() throws Exception {
    when(mockInputManager.getTransformerBySchema(CswConstants.CSW_OUTPUT_SCHEMA)).thenReturn(mockCswRecordConverter);
    HierarchicalStreamReader reader = new WstxDriver().createReader(new StringReader(getRecord()));
    CswTransformProvider provider = new CswTransformProvider(null, mockInputManager);
    UnmarshallingContext context = new TreeUnmarshaller(null, null, null, null);
    context.put(CswConstants.AXIS_ORDER_PROPERTY, CswAxisOrder.LAT_LON);
    ArgumentCaptor<HierarchicalStreamReader> readerArgumentCaptor = ArgumentCaptor.forClass(HierarchicalStreamReader.class);
    ArgumentCaptor<UnmarshallingContext> unmarshallingContextArgumentCaptor = ArgumentCaptor.forClass(UnmarshallingContext.class);
    provider.unmarshal(reader, context);
    // Verify that CswRecordConverter unmarshal was called
    verify(mockCswRecordConverter, times(1)).unmarshal(readerArgumentCaptor.capture(), unmarshallingContextArgumentCaptor.capture());
    HierarchicalStreamReader hierarchicalStreamReader = readerArgumentCaptor.getValue();
    UnmarshallingContext unmarshallingContext = unmarshallingContextArgumentCaptor.getValue();
    // Verify that reader and context are passed to the CswRecordConverter correctly
    assertThat(hierarchicalStreamReader, is(reader));
    assertThat(unmarshallingContext, is(context));
    assertThat(context.get(CswConstants.AXIS_ORDER_PROPERTY), is(CswAxisOrder.LAT_LON));
}
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 13 with TreeUnmarshaller

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

the class XStreamAttributeCopierTest 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 14 with TreeUnmarshaller

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

the class CswTransformProviderTest method testUnmarshalCswRecordCoordinateOrder.

@Test
public void testUnmarshalCswRecordCoordinateOrder() throws Exception {
    when(mockInputManager.getTransformerBySchema(CswConstants.CSW_OUTPUT_SCHEMA)).thenReturn(mockCswRecordConverter);
    HierarchicalStreamReader reader = new WstxDriver().createReader(new StringReader(getRecord()));
    CswTransformProvider provider = new CswTransformProvider(null, mockInputManager);
    UnmarshallingContext context = new TreeUnmarshaller(null, null, null, null);
    context.put(CswConstants.AXIS_ORDER_PROPERTY, CswAxisOrder.LAT_LON);
    ArgumentCaptor<HierarchicalStreamReader> readerArgumentCaptor = ArgumentCaptor.forClass(HierarchicalStreamReader.class);
    ArgumentCaptor<UnmarshallingContext> unmarshallingContextArgumentCaptor = ArgumentCaptor.forClass(UnmarshallingContext.class);
    provider.unmarshal(reader, context);
    // Verify that CswRecordConverter unmarshal was called
    verify(mockCswRecordConverter, times(1)).unmarshal(readerArgumentCaptor.capture(), unmarshallingContextArgumentCaptor.capture());
    HierarchicalStreamReader hierarchicalStreamReader = readerArgumentCaptor.getValue();
    UnmarshallingContext unmarshallingContext = unmarshallingContextArgumentCaptor.getValue();
    // Verify that reader and context are passed to the CswRecordConverter correctly
    assertThat(hierarchicalStreamReader, is(reader));
    assertThat(unmarshallingContext, is(context));
    assertThat(context.get(CswConstants.AXIS_ORDER_PROPERTY), is(CswAxisOrder.LAT_LON));
}
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 15 with TreeUnmarshaller

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

the class CswTransformProviderTest method testUnmarshalCswRecord.

@Test
public void testUnmarshalCswRecord() throws Exception {
    when(mockInputManager.getTransformerBySchema(CswConstants.CSW_OUTPUT_SCHEMA)).thenReturn(mockCswRecordConverter);
    HierarchicalStreamReader reader = new WstxDriver().createReader(new StringReader(getRecord()));
    CswTransformProvider provider = new CswTransformProvider(null, mockInputManager);
    UnmarshallingContext context = new TreeUnmarshaller(null, null, null, null);
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    provider.unmarshal(reader, context);
    // Verify the context arguments were set correctly
    verify(mockInputManager, times(1)).getTransformerBySchema(captor.capture());
    String outputSchema = captor.getValue();
    assertThat(outputSchema, is(CswConstants.CSW_OUTPUT_SCHEMA));
}
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) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) 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