use of com.thoughtworks.xstream.converters.UnmarshallingContext 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));
}
use of com.thoughtworks.xstream.converters.UnmarshallingContext 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"));
}
use of com.thoughtworks.xstream.converters.UnmarshallingContext 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);
}
use of com.thoughtworks.xstream.converters.UnmarshallingContext 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));
}
use of com.thoughtworks.xstream.converters.UnmarshallingContext 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);
}
Aggregations