Search in sources :

Example 6 with XPath

use of com.adaptris.util.text.xml.XPath in project interlok by adaptris.

the class XmlPayloadTranslatorTest method testXpathStatementParam.

@Test
public void testXpathStatementParam() throws Exception {
    createDatabase();
    List<AdapterTypeVersion> dbItems = generate(10);
    AdapterTypeVersion entry = dbItems.get(0);
    populateDatabase(dbItems, false);
    JdbcDataQueryService s = createXmlService();
    s.setResultSetTranslator(new XmlPayloadTranslator());
    AdaptrisMessage msg = createMessage(entry);
    execute(s, msg);
    assertTrue(ADAPTER_ID_KEY + " exists", msg.containsKey(ADAPTER_ID_KEY));
    assertNotSame(XML_PAYLOAD_PREFIX + entry.getUniqueId() + XML_PAYLOAD_SUFFIX, msg.getContent());
    assertFalse(msg.containsKey(JdbcDataQueryService.class.getCanonicalName()));
    System.out.println(msg.getContent());
    XPath xp = new XPath();
    Document xmlDoc = XmlHelper.createDocument(msg, DocumentBuilderFactoryBuilder.newInstance());
    assertNull(xp.selectSingleNode(xmlDoc, "/Results/OriginalMessage"));
    assertNotNull(xp.selectSingleNode(xmlDoc, "/Results/Row"));
    assertEquals(entry.getVersion(), xp.selectSingleTextItem(xmlDoc, "/Results/Row/ADAPTER_VERSION"));
    assertEquals(String.valueOf(entry.getCounter()), xp.selectSingleTextItem(xmlDoc, "/Results/Row/COUNTER"));
    assertEquals(entry.getTranslatorType(), xp.selectSingleTextItem(xmlDoc, "/Results/Row/MESSAGE_TRANSLATOR_TYPE"));
    // Convert date into a Timestamp for toString purposes, as it's different from Date.toString()
    assertEquals(new Timestamp(entry.getDate().getTime()).toString(), xp.selectSingleTextItem(xmlDoc, "/Results/Row/INSERTED_ON"));
}
Also used : XPath(com.adaptris.util.text.xml.XPath) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) Document(org.w3c.dom.Document) Timestamp(java.sql.Timestamp) Test(org.junit.Test)

Example 7 with XPath

use of com.adaptris.util.text.xml.XPath in project interlok by adaptris.

the class XmlPayloadTranslatorTest method testService_XpathParam_WithTranslators.

@Test
public void testService_XpathParam_WithTranslators() throws Exception {
    createDatabase();
    List<AdapterTypeVersion> dbItems = generate(10);
    AdapterTypeVersion entry = dbItems.get(0);
    populateDatabase(dbItems, false);
    JdbcDataQueryService s = createXmlService();
    s.setResultSetTranslator(new XmlPayloadTranslator().withColumnTranslators(new StringColumnTranslator(), new StringColumnTranslator(), new TimestampColumnTranslator(), new IntegerColumnTranslator()).withColumnNameStyle(ColumnStyle.NoStyle));
    AdaptrisMessage msg = createMessage(entry);
    execute(s, msg);
    XPath xp = new XPath();
    Document xmlDoc = XmlHelper.createDocument(msg, DocumentBuilderFactoryBuilder.newInstance());
    assertNull(xp.selectSingleNode(xmlDoc, "/Results/OriginalMessage"));
    assertNotNull(xp.selectSingleNode(xmlDoc, "/Results/Row"));
    assertEquals(entry.getVersion(), xp.selectSingleTextItem(xmlDoc, "/Results/Row/ADAPTER_VERSION"));
    assertEquals(String.valueOf(entry.getCounter()), xp.selectSingleTextItem(xmlDoc, "/Results/Row/COUNTER"));
    assertEquals(entry.getTranslatorType(), xp.selectSingleTextItem(xmlDoc, "/Results/Row/MESSAGE_TRANSLATOR_TYPE"));
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    assertEquals(df.format(entry.getDate()), xp.selectSingleTextItem(xmlDoc, "/Results/Row/INSERTED_ON"));
}
Also used : IntegerColumnTranslator(com.adaptris.core.services.jdbc.types.IntegerColumnTranslator) XPath(com.adaptris.util.text.xml.XPath) StringColumnTranslator(com.adaptris.core.services.jdbc.types.StringColumnTranslator) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) TimestampColumnTranslator(com.adaptris.core.services.jdbc.types.TimestampColumnTranslator) Document(org.w3c.dom.Document) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 8 with XPath

use of com.adaptris.util.text.xml.XPath in project interlok by adaptris.

the class MetadataXpathQueryTest method testResolveXpath_function.

@Test
public void testResolveXpath_function() throws Exception {
    MetadataXpathQuery query = init(create());
    Document doc = XmlHelper.createDocument(XML);
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(XML);
    msg.addMetadata("xpathMetadataKey", "count(/message)");
    MetadataElement result = query.resolveXpath(doc, new XPath(), query.createXpathQuery(msg));
    assertEquals("1", result.getValue());
}
Also used : XPath(com.adaptris.util.text.xml.XPath) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) MetadataElement(com.adaptris.core.MetadataElement) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 9 with XPath

use of com.adaptris.util.text.xml.XPath in project interlok by adaptris.

the class MetadataXpathQueryTest method testResolveNodesAsString.

@Test
public void testResolveNodesAsString() throws Exception {
    MetadataXpathQuery query = init(create());
    query.setAsXmlString(true);
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(XML);
    msg.addMetadata("xpathMetadataKey", "//source-id");
    MetadataElement result = query.resolveXpath(XmlHelper.createDocument(msg.getContent()), new XPath(), query.createXpathQuery(msg));
    assertEquals("<source-id>partnera</source-id>", result.getValue().strip());
}
Also used : XPath(com.adaptris.util.text.xml.XPath) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) MetadataElement(com.adaptris.core.MetadataElement) Test(org.junit.Test)

Example 10 with XPath

use of com.adaptris.util.text.xml.XPath in project interlok by adaptris.

the class MetadataXpathQueryTest method testResolveXpath_NamespaceWithNamespaceContext.

// Invalid test
// Namedspaced document with non-namespace xpath never matches with SAXON
// public void testResolveXpath_NamespaceNoNamespaceContext() throws Exception {
// MetadataXpathQuery query = init(create());
// Document doc = XmlHelper.createDocument(XML_WITH_NAMESPACE);
// AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(XML_WITH_NAMESPACE);
// msg.addMetadata("xpathMetadataKey", "count(/schematron-output/failed-assert)");
// MetadataElement result = query.resolveXpath(doc, null, query.createXpathQuery(msg));
// assertEquals("2", result.getValue());
// }
@Test
public void testResolveXpath_NamespaceWithNamespaceContext() throws Exception {
    MetadataXpathQuery query = init(create());
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(XML_WITH_NAMESPACE);
    msg.addMetadata("xpathMetadataKey", "count(/svrl:schematron-output/svrl:failed-assert)");
    StaticNamespaceContext ctx = new StaticNamespaceContext();
    Document doc = XmlHelper.createDocument(XML_WITH_NAMESPACE, ctx);
    MetadataElement result = query.resolveXpath(doc, new XPath(ctx), query.createXpathQuery(msg));
    assertEquals("2", result.getValue());
}
Also used : XPath(com.adaptris.util.text.xml.XPath) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) MetadataElement(com.adaptris.core.MetadataElement) Document(org.w3c.dom.Document) Test(org.junit.Test)

Aggregations

XPath (com.adaptris.util.text.xml.XPath)75 Document (org.w3c.dom.Document)64 Test (org.junit.Test)63 AdaptrisMessage (com.adaptris.core.AdaptrisMessage)45 MetadataElement (com.adaptris.core.MetadataElement)23 DocumentBuilderFactoryBuilder (com.adaptris.core.util.DocumentBuilderFactoryBuilder)21 CoreException (com.adaptris.core.CoreException)12 Node (org.w3c.dom.Node)11 SimpleNamespaceContext (com.adaptris.util.text.xml.SimpleNamespaceContext)9 NamespaceContext (javax.xml.namespace.NamespaceContext)9 ServiceException (com.adaptris.core.ServiceException)7 InsertNode (com.adaptris.util.text.xml.InsertNode)6 NodeList (org.w3c.dom.NodeList)6 NullService (com.adaptris.core.NullService)5 PooledSplitJoinService (com.adaptris.core.services.splitter.PooledSplitJoinService)5 XpathMessageSplitter (com.adaptris.core.services.splitter.XpathMessageSplitter)5 TimeInterval (com.adaptris.util.TimeInterval)5 XmlHelper.createDocument (com.adaptris.core.util.XmlHelper.createDocument)3 DefaultMessageFactory (com.adaptris.core.DefaultMessageFactory)2 MultiPayloadAdaptrisMessage (com.adaptris.core.MultiPayloadAdaptrisMessage)2