Search in sources :

Example 16 with HierarchicalStreamReader

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

use of com.thoughtworks.xstream.io.HierarchicalStreamReader 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 18 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 19 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 20 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)

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