Search in sources :

Example 1 with SaxEventHandlerDelegate

use of org.codice.ddf.transformer.xml.streaming.lib.SaxEventHandlerDelegate in project ddf by codice.

the class TestXmlInputTransformer method testNormalTransform.

/*
        Tests a base XmlInputTransformer, CONTENT_TYPE is null because it is not in the base xmlToMetacard mapping
     */
@Test
public void testNormalTransform() throws FileNotFoundException, CatalogTransformerException {
    inputStream = new FileInputStream("src/test/resources/metacard2.xml");
    saxEventHandlerFactory = new XmlSaxEventHandlerFactoryImpl();
    saxEventHandler = saxEventHandlerFactory.getNewSaxEventHandler();
    assertThat(saxEventHandler.getSupportedAttributeDescriptors().size(), is(greaterThan(0)));
    GMLHandler gh = new GMLHandler(new GeometryFactory(), (ErrorHandler) null);
    gmlHandler = new GmlHandler(gh, gml3ToWkt);
    assertThat(gmlHandler.getSupportedAttributeDescriptors().size(), is(greaterThan(0)));
    saxEventHandlerDelegate = new SaxEventHandlerDelegate(Arrays.asList(saxEventHandler, gmlHandler));
    Metacard metacard = saxEventHandlerDelegate.read(inputStream).getMetacard(null);
    assertThat(metacard.getAttribute(Metacard.TITLE).getValues().size(), is(1));
    assertThat(metacard.getAttribute(Metacard.TITLE).getValues().get(0), is("Title!"));
    assertThat(metacard.getAttribute(Metacard.DESCRIPTION).getValues().size(), is(1));
    assertThat(metacard.getAttribute(Metacard.DESCRIPTION).getValues().get(0), is("Description!"));
    assertThat(metacard.getAttribute(Metacard.POINT_OF_CONTACT).getValues().size(), is(1));
    assertThat(metacard.getAttribute(Metacard.POINT_OF_CONTACT).getValues().get(0), is("POC!"));
    assertThat(metacard.getAttribute(Metacard.RESOURCE_URI).getValues().size(), is(1));
    assertThat(metacard.getAttribute(Metacard.RESOURCE_URI).getValues().get(0), is("foobar"));
    assertThat(metacard.getAttribute(Metacard.CONTENT_TYPE), is(nullValue()));
    assertThat(metacard.getAttribute(Metacard.GEOGRAPHY).getValues().size(), is(1));
    assertThat(metacard.getAttribute(Metacard.GEOGRAPHY).getValues().get(0), is("POINT (100 200)"));
}
Also used : SaxEventHandlerDelegate(org.codice.ddf.transformer.xml.streaming.lib.SaxEventHandlerDelegate) Metacard(ddf.catalog.data.Metacard) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) GMLHandler(com.vividsolutions.jts.io.gml2.GMLHandler) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 2 with SaxEventHandlerDelegate

use of org.codice.ddf.transformer.xml.streaming.lib.SaxEventHandlerDelegate in project ddf by codice.

the class TestXmlInputTransformer method testBadGHTransform.

@Test(expected = NullPointerException.class)
public void testBadGHTransform() throws FileNotFoundException, CatalogTransformerException, SAXException {
    inputStream = new FileInputStream("src/test/resources/metacard2.xml");
    saxEventHandler = new XmlSaxEventHandlerFactoryImpl().getNewSaxEventHandler();
    GMLHandler gh = mock(GMLHandler.class);
    doThrow(new SAXException()).when(gh).characters(any(char[].class), anyInt(), anyInt());
    doThrow(new SAXException()).when(gh).startElement(anyString(), anyString(), anyString(), any(Attributes.class));
    doThrow(new SAXException()).when(gh).endElement(anyString(), anyString(), anyString());
    gmlHandler = new GmlHandler(gh, gml3ToWkt);
    saxEventHandlerDelegate = new SaxEventHandlerDelegate(Arrays.asList(saxEventHandler, gmlHandler));
    saxEventHandlerDelegate.read(inputStream);
}
Also used : SaxEventHandlerDelegate(org.codice.ddf.transformer.xml.streaming.lib.SaxEventHandlerDelegate) GMLHandler(com.vividsolutions.jts.io.gml2.GMLHandler) Attributes(org.xml.sax.Attributes) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException) Test(org.junit.Test)

Example 3 with SaxEventHandlerDelegate

use of org.codice.ddf.transformer.xml.streaming.lib.SaxEventHandlerDelegate in project ddf by codice.

the class XmlInputTransformerTest method testConfiguredTransform.

/*
     Configures a custom xmlToMetacard mapping, CONTENT_TYPE is not null because it is in the custom xmlToMetacard mapping
  */
@Test
public void testConfiguredTransform() throws FileNotFoundException, CatalogTransformerException {
    Map xmlToMetacard = new HashMap<>();
    xmlToMetacard.put("title", Metacard.TITLE);
    xmlToMetacard.put("point-of-contact", Metacard.POINT_OF_CONTACT);
    xmlToMetacard.put("description", Metacard.DESCRIPTION);
    xmlToMetacard.put("source", Metacard.RESOURCE_URI);
    xmlToMetacard.put("type", Metacard.CONTENT_TYPE);
    XmlSaxEventHandlerFactoryImpl factory = new XmlSaxEventHandlerFactoryImpl();
    factory.setXmlToMetacardMapping(xmlToMetacard);
    saxEventHandler = factory.getNewSaxEventHandler();
    assertThat(factory.getXmlToMetacardMapping().equals(xmlToMetacard), is(true));
    saxEventHandlerDelegate = new SaxEventHandlerDelegate(Collections.singletonList(saxEventHandler));
    inputStream = new FileInputStream("src/test/resources/metacard2.xml");
    saxEventHandlerDelegate.read(inputStream);
    Metacard metacard = saxEventHandlerDelegate.getMetacard(null);
    assertThat(metacard.getAttribute(Metacard.TITLE).getValues().size(), is(1));
    assertThat(metacard.getAttribute(Metacard.TITLE).getValues().get(0), is("Title!"));
    assertThat(metacard.getAttribute(Metacard.DESCRIPTION).getValues().size(), is(1));
    assertThat(metacard.getAttribute(Metacard.DESCRIPTION).getValues().get(0), is("Description!"));
    assertThat(metacard.getAttribute(Metacard.POINT_OF_CONTACT).getValues().size(), is(1));
    assertThat(metacard.getAttribute(Metacard.POINT_OF_CONTACT).getValues().get(0), is("POC!"));
    assertThat(metacard.getAttribute(Metacard.RESOURCE_URI).getValues().size(), is(1));
    assertThat(metacard.getAttribute(Metacard.RESOURCE_URI).getValues().get(0), is("foobar"));
    assertThat(metacard.getAttribute(Metacard.CONTENT_TYPE).getValues().size(), is(1));
    assertThat(metacard.getAttribute(Metacard.CONTENT_TYPE).getValues().get(0), is("ddf.metacard"));
}
Also used : SaxEventHandlerDelegate(org.codice.ddf.transformer.xml.streaming.lib.SaxEventHandlerDelegate) Metacard(ddf.catalog.data.Metacard) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 4 with SaxEventHandlerDelegate

use of org.codice.ddf.transformer.xml.streaming.lib.SaxEventHandlerDelegate in project ddf by codice.

the class XmlInputTransformerTest method testBadGHTransform.

@Test(expected = NullPointerException.class)
public void testBadGHTransform() throws FileNotFoundException, CatalogTransformerException, SAXException {
    inputStream = new FileInputStream("src/test/resources/metacard2.xml");
    saxEventHandler = new XmlSaxEventHandlerFactoryImpl().getNewSaxEventHandler();
    GMLHandler gh = mock(GMLHandler.class);
    doThrow(new SAXException()).when(gh).characters(any(char[].class), anyInt(), anyInt());
    doThrow(new SAXException()).when(gh).startElement(anyString(), anyString(), anyString(), any(Attributes.class));
    doThrow(new SAXException()).when(gh).endElement(anyString(), anyString(), anyString());
    gmlHandler = new GmlHandler(gh, gml3ToWkt);
    saxEventHandlerDelegate = new SaxEventHandlerDelegate(Arrays.asList(saxEventHandler, gmlHandler));
    saxEventHandlerDelegate.read(inputStream);
}
Also used : SaxEventHandlerDelegate(org.codice.ddf.transformer.xml.streaming.lib.SaxEventHandlerDelegate) GMLHandler(org.locationtech.jts.io.gml2.GMLHandler) Attributes(org.xml.sax.Attributes) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException) Test(org.junit.Test)

Example 5 with SaxEventHandlerDelegate

use of org.codice.ddf.transformer.xml.streaming.lib.SaxEventHandlerDelegate in project ddf by codice.

the class TestXmlInputTransformer method testConfiguredTransform.

/*
        Configures a custom xmlToMetacard mapping, CONTENT_TYPE is not null because it is in the custom xmlToMetacard mapping
     */
@Test
public void testConfiguredTransform() throws FileNotFoundException, CatalogTransformerException {
    Map xmlToMetacard = new HashMap<>();
    xmlToMetacard.put("title", Metacard.TITLE);
    xmlToMetacard.put("point-of-contact", Metacard.POINT_OF_CONTACT);
    xmlToMetacard.put("description", Metacard.DESCRIPTION);
    xmlToMetacard.put("source", Metacard.RESOURCE_URI);
    xmlToMetacard.put("type", Metacard.CONTENT_TYPE);
    XmlSaxEventHandlerFactoryImpl factory = new XmlSaxEventHandlerFactoryImpl();
    factory.setXmlToMetacardMapping(xmlToMetacard);
    saxEventHandler = factory.getNewSaxEventHandler();
    assertThat(factory.getXmlToMetacardMapping().equals(xmlToMetacard), is(true));
    saxEventHandlerDelegate = new SaxEventHandlerDelegate(Collections.singletonList(saxEventHandler));
    inputStream = new FileInputStream("src/test/resources/metacard2.xml");
    saxEventHandlerDelegate.read(inputStream);
    Metacard metacard = saxEventHandlerDelegate.getMetacard(null);
    assertThat(metacard.getAttribute(Metacard.TITLE).getValues().size(), is(1));
    assertThat(metacard.getAttribute(Metacard.TITLE).getValues().get(0), is("Title!"));
    assertThat(metacard.getAttribute(Metacard.DESCRIPTION).getValues().size(), is(1));
    assertThat(metacard.getAttribute(Metacard.DESCRIPTION).getValues().get(0), is("Description!"));
    assertThat(metacard.getAttribute(Metacard.POINT_OF_CONTACT).getValues().size(), is(1));
    assertThat(metacard.getAttribute(Metacard.POINT_OF_CONTACT).getValues().get(0), is("POC!"));
    assertThat(metacard.getAttribute(Metacard.RESOURCE_URI).getValues().size(), is(1));
    assertThat(metacard.getAttribute(Metacard.RESOURCE_URI).getValues().get(0), is("foobar"));
    assertThat(metacard.getAttribute(Metacard.CONTENT_TYPE).getValues().size(), is(1));
    assertThat(metacard.getAttribute(Metacard.CONTENT_TYPE).getValues().get(0), is("ddf.metacard"));
}
Also used : SaxEventHandlerDelegate(org.codice.ddf.transformer.xml.streaming.lib.SaxEventHandlerDelegate) Metacard(ddf.catalog.data.Metacard) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

FileInputStream (java.io.FileInputStream)6 SaxEventHandlerDelegate (org.codice.ddf.transformer.xml.streaming.lib.SaxEventHandlerDelegate)6 Test (org.junit.Test)6 Metacard (ddf.catalog.data.Metacard)4 GMLHandler (com.vividsolutions.jts.io.gml2.GMLHandler)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 GMLHandler (org.locationtech.jts.io.gml2.GMLHandler)2 Attributes (org.xml.sax.Attributes)2 SAXException (org.xml.sax.SAXException)2 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)1 GeometryFactory (org.locationtech.jts.geom.GeometryFactory)1