use of com.thoughtworks.xstream.io.xml.WstxDriver in project ddf by codice.
the class TestGetRecordsResponseConverter method testUnmarshalGetRecordsResponseConversionWithEmptyBoundingBox.
@Test
public void testUnmarshalGetRecordsResponseConversionWithEmptyBoundingBox() {
XStream xstream = new XStream(new WstxDriver());
xstream.setClassLoader(this.getClass().getClassLoader());
GetRecordsResponseConverter grrc = new GetRecordsResponseConverter(mockProvider);
xstream.registerConverter(grrc);
xstream.alias("GetRecordsResponse", CswRecordCollection.class);
String xml = "<csw:GetRecordsResponse xmlns:csw=\"http://www.opengis.net/cat/csw\">\r\n" + " <csw:SearchStatus status=\"subset\" timestamp=\"2013-05-01T02:13:36+0200\"/>\r\n" + " <csw:SearchResults elementSet=\"full\" nextRecord=\"11\" numberOfRecordsMatched=\"479\" numberOfRecordsReturned=\"10\" recordSchema=\"csw:Record\">\r\n" + " <csw:Record xmlns:csw=\"http://www.opengis.net/cat/csw\">\r\n" + " <dc:identifier xmlns:dc=\"http://purl.org/dc/elements/1.1/\">{8C1F6297-EC96-4302-A01E-14988C9149FD}</dc:identifier>\r\n" + " <dc:title xmlns:dc=\"http://purl.org/dc/elements/1.1/\">title 1</dc:title>\r\n" + " <dct:modified xmlns:dct=\"http://purl.org/dc/terms/\">2008-12-15</dct:modified>\r\n" + " <dc:subject xmlns:dc=\"http://purl.org/dc/elements/1.1/\">subject 1</dc:subject>\r\n" + " <dc:subject xmlns:dc=\"http://purl.org/dc/elements/1.1/\">second subject</dc:subject>\r\n" + " <dct:abstract xmlns:dct=\"http://purl.org/dc/terms/\">abstract 1</dct:abstract>\r\n" + " <dc:rights xmlns:dc=\"http://purl.org/dc/elements/1.1/\">copyright 1</dc:rights>\r\n" + " <dc:rights xmlns:dc=\"http://purl.org/dc/elements/1.1/\">copyright 2</dc:rights>\r\n" + " <dc:language xmlns:dc=\"http://purl.org/dc/elements/1.1/\">english</dc:language> \r\n" + " <ows:BoundingBox xmlns:ows=\"http://www.opengis.net/ows\" crs=\"EPSG:RD_New (28992)\">\r\n" + " <ows:LowerCorner></ows:LowerCorner>\r\n" + " <ows:UpperCorner></ows:UpperCorner>\r\n" + " </ows:BoundingBox> \r\n" + " <dc:type xmlns:dc=\"http://purl.org/dc/elements/1.1/\">dataset</dc:type>\r\n" + " <dc:format xmlns:dc=\"http://purl.org/dc/elements/1.1/\">Shapefile</dc:format> \r\n" + " </csw:Record>\r\n" + " </csw:SearchResults>\r\n" + "</csw:GetRecordsResponse>";
InputStream inStream = IOUtils.toInputStream(xml);
CswRecordCollection cswRecords = (CswRecordCollection) xstream.fromXML(inStream);
IOUtils.closeQuietly(inStream);
assertThat(cswRecords.getNumberOfRecordsReturned(), is(10L));
assertThat(cswRecords.getNumberOfRecordsMatched(), is(479L));
List<Metacard> metacards = cswRecords.getCswRecords();
assertThat(metacards, not(nullValue()));
assertThat(metacards.size(), is(1));
}
use of com.thoughtworks.xstream.io.xml.WstxDriver 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)));
}
use of com.thoughtworks.xstream.io.xml.WstxDriver in project ddf by codice.
the class TestCswTransformProvider method testMarshalOtherSchema.
@Test
public void testMarshalOtherSchema() throws Exception {
when(mockMetacardManager.getTransformerByProperty(TransformerManager.SCHEMA, OTHER_SCHEMA)).thenReturn(mockMetacardTransformer);
when(mockMetacardTransformer.transform(any(Metacard.class), any(Map.class))).thenReturn(new BinaryContentImpl(IOUtils.toInputStream(getRecord()), new MimeType(MediaType.APPLICATION_XML)));
StringWriter stringWriter = new StringWriter();
HierarchicalStreamWriter writer = new WstxDriver().createWriter(stringWriter);
CswTransformProvider provider = new CswTransformProvider(mockMetacardManager, null);
MarshallingContext context = new TreeMarshaller(writer, 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.marshal(getMetacard(), writer, context);
// Verify the context arguments were set correctly
verify(mockMetacardManager, times(1)).getTransformerByProperty(captor.capture(), captor.capture());
String outputSchema = captor.getValue();
assertThat(outputSchema, is(OTHER_SCHEMA));
}
use of com.thoughtworks.xstream.io.xml.WstxDriver 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.io.xml.WstxDriver in project ddf by codice.
the class TestCswTransformProvider method testMarshalNoTransformers.
@Test(expected = ConversionException.class)
public void testMarshalNoTransformers() throws Exception {
when(mockMetacardManager.getTransformerBySchema(anyString())).thenReturn(null);
StringWriter stringWriter = new StringWriter();
HierarchicalStreamWriter writer = new WstxDriver().createWriter(stringWriter);
CswTransformProvider provider = new CswTransformProvider(mockMetacardManager, null);
MarshallingContext context = new TreeMarshaller(writer, null, null);
provider.marshal(getMetacard(), writer, context);
}
Aggregations