Search in sources :

Example 6 with XpathQuery

use of com.adaptris.core.services.metadata.xpath.XpathQuery in project interlok by adaptris.

the class XStreamMarshallerTest method adapterInstanceFieldChecks.

// redmineID 2457 - ensures that marshalling/unmarshalling the given files results in no loss of data
public static void adapterInstanceFieldChecks(Adapter fromXML) {
    assertNotNull(fromXML);
    assertEquals("SimpleAdapterTest", fromXML.getUniqueId());
    assertTrue(fromXML.getEventHandler() instanceof DefaultEventHandler);
    assertTrue(((DefaultEventHandler) fromXML.getEventHandler()).getConnection() instanceof NullConnection);
    assertTrue(((DefaultEventHandler) fromXML.getEventHandler()).getProducer() instanceof NullMessageProducer);
    // ShutdownWaitSeconds is now null.
    assertNull(((DefaultEventHandler) fromXML.getEventHandler()).getShutdownWaitSeconds());
    assertTrue(fromXML.getMessageErrorHandler() instanceof NullProcessingExceptionHandler);
    Channel channel = fromXML.getChannelList().get(0);
    assertTrue(channel.getConsumeConnection() instanceof NullConnection);
    assertTrue(channel.getProduceConnection() instanceof NullConnection);
    // Check workflow
    WorkflowList workflowList = channel.getWorkflowList();
    assertNotNull(workflowList);
    assertEquals(1, workflowList.size());
    // test workflow
    StandardWorkflow standardWorkflow = (StandardWorkflow) workflowList.get(0);
    assertNotNull(standardWorkflow);
    assertEquals("workflow1", standardWorkflow.getUniqueId());
    // test workflow consumer
    NullMessageConsumer consumer = (NullMessageConsumer) standardWorkflow.getConsumer();
    assertNotNull(consumer);
    // test services
    ServiceCollection serviceCollection = standardWorkflow.getServiceCollection();
    assertNotNull(serviceCollection);
    assertEquals("serviceListID1", serviceCollection.getUniqueId());
    assertEquals(3, serviceCollection.size());
    assertTrue(serviceCollection instanceof ServiceList);
    // test service 1
    Service service1 = serviceCollection.get(0);
    assertEquals("serviceID1", service1.getUniqueId());
    assertTrue(service1 instanceof AddMetadataService);
    Set<MetadataElement> metadataElements = ((AddMetadataService) service1).getMetadataElements();
    assertEquals(1, metadataElements.size());
    for (Iterator<MetadataElement> iterator = metadataElements.iterator(); iterator.hasNext(); ) {
        MetadataElement metadataElement = iterator.next();
        assertEquals("key1", metadataElement.getKey());
        assertEquals("val1", metadataElement.getValue());
        break;
    }
    // test service 2
    Service service2 = serviceCollection.get(1);
    assertEquals("serviceID2", service2.getUniqueId());
    assertTrue(service2 instanceof XpathMetadataService);
    List<XpathQuery> xpathQueries = ((XpathMetadataService) service2).getXpathQueries();
    assertEquals(1, xpathQueries.size());
    assertEquals(ConfiguredXpathQuery.class, xpathQueries.get(0).getClass());
    assertEquals("/a/b/c", ((ConfiguredXpathQuery) xpathQueries.get(0)).getXpathQuery());
    // Test service3
    Service service3 = serviceCollection.get(2);
    assertEquals("serviceID3", service3.getUniqueId());
    assertTrue(service3 instanceof LogMessageService);
    assertTrue(StringUtils.isBlank(((LogMessageService) service3).getLogPrefix()));
}
Also used : LogMessageService(com.adaptris.core.services.LogMessageService) XpathMetadataService(com.adaptris.core.services.metadata.XpathMetadataService) LogMessageService(com.adaptris.core.services.LogMessageService) AddMetadataService(com.adaptris.core.services.metadata.AddMetadataService) AddMetadataService(com.adaptris.core.services.metadata.AddMetadataService) XpathQuery(com.adaptris.core.services.metadata.xpath.XpathQuery) ConfiguredXpathQuery(com.adaptris.core.services.metadata.xpath.ConfiguredXpathQuery) XpathMetadataService(com.adaptris.core.services.metadata.XpathMetadataService)

Example 7 with XpathQuery

use of com.adaptris.core.services.metadata.xpath.XpathQuery in project interlok by adaptris.

the class XpathMetadataService method doService.

@Override
public void doService(AdaptrisMessage msg) throws ServiceException {
    Set<MetadataElement> metadataElements = new HashSet<MetadataElement>();
    NamespaceContext namespaceCtx = SimpleNamespaceContext.create(getNamespaceContext(), msg);
    try {
        DocumentBuilderFactoryBuilder builder = documentFactoryBuilder();
        if (namespaceCtx != null) {
            builder.setNamespaceAware(true);
        }
        XPath xpathToUse = XPath.newXPathInstance(builder, namespaceCtx);
        Document doc = XmlHelper.createDocument(msg, builder);
        for (XpathQuery query : queriesToExecute) {
            metadataElements.add(query.resolveXpath(doc, xpathToUse, query.createXpathQuery(msg)));
        }
        logMetadata("Xpath Metadata resolved {}", metadataElements);
        msg.setMetadata(metadataElements);
    } catch (Exception e) {
        throw new ServiceException(e);
    }
}
Also used : XPath(com.adaptris.util.text.xml.XPath) XpathQuery(com.adaptris.core.services.metadata.xpath.XpathQuery) ServiceException(com.adaptris.core.ServiceException) NamespaceContext(javax.xml.namespace.NamespaceContext) SimpleNamespaceContext(com.adaptris.util.text.xml.SimpleNamespaceContext) DocumentBuilderFactoryBuilder(com.adaptris.core.util.DocumentBuilderFactoryBuilder) MetadataElement(com.adaptris.core.MetadataElement) Document(org.w3c.dom.Document) ServiceException(com.adaptris.core.ServiceException) CoreException(com.adaptris.core.CoreException) HashSet(java.util.HashSet)

Example 8 with XpathQuery

use of com.adaptris.core.services.metadata.xpath.XpathQuery in project interlok by adaptris.

the class XpathMetadataServiceTest method testDoService_UsingXpathQuery_WithNamespaceContext.

@Test
public void testDoService_UsingXpathQuery_WithNamespaceContext() throws CoreException {
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(XML_WITH_NAMESPACE);
    XpathMetadataService service = new XpathMetadataService();
    service.setXpathQueries(new ArrayList<XpathQuery>(Arrays.asList(new ConfiguredXpathQuery("failureCount", "count(/svrl:schematron-output/svrl:failed-assert)"))));
    service.setNamespaceContext(createContextEntries());
    execute(service, msg);
    assertTrue(msg.containsKey("failureCount"));
    assertEquals("2", msg.getMetadataValue("failureCount"));
}
Also used : XpathQuery(com.adaptris.core.services.metadata.xpath.XpathQuery) MultiItemConfiguredXpathQuery(com.adaptris.core.services.metadata.xpath.MultiItemConfiguredXpathQuery) MetadataXpathQuery(com.adaptris.core.services.metadata.xpath.MetadataXpathQuery) MultiItemMetadataXpathQuery(com.adaptris.core.services.metadata.xpath.MultiItemMetadataXpathQuery) ConfiguredXpathQuery(com.adaptris.core.services.metadata.xpath.ConfiguredXpathQuery) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) MultiItemConfiguredXpathQuery(com.adaptris.core.services.metadata.xpath.MultiItemConfiguredXpathQuery) ConfiguredXpathQuery(com.adaptris.core.services.metadata.xpath.ConfiguredXpathQuery) Test(org.junit.Test)

Example 9 with XpathQuery

use of com.adaptris.core.services.metadata.xpath.XpathQuery in project interlok by adaptris.

the class XpathMetadataServiceTest method testDoService_NotXML.

@Test
public void testDoService_NotXML() throws Exception {
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage("ABCDEFG");
    XpathMetadataService service = new XpathMetadataService().withMetadataLogger(new TruncateMetadata(20));
    service.setXpathQueries(new ArrayList<XpathQuery>(Arrays.asList(new ConfiguredXpathQuery("source", "//source-id"), new ConfiguredXpathQuery("destination", "//destination-id"))));
    try {
        execute(service, msg);
        fail();
    } catch (ServiceException expected) {
    }
}
Also used : TruncateMetadata(com.adaptris.core.util.TruncateMetadata) XpathQuery(com.adaptris.core.services.metadata.xpath.XpathQuery) MultiItemConfiguredXpathQuery(com.adaptris.core.services.metadata.xpath.MultiItemConfiguredXpathQuery) MetadataXpathQuery(com.adaptris.core.services.metadata.xpath.MetadataXpathQuery) MultiItemMetadataXpathQuery(com.adaptris.core.services.metadata.xpath.MultiItemMetadataXpathQuery) ConfiguredXpathQuery(com.adaptris.core.services.metadata.xpath.ConfiguredXpathQuery) ServiceException(com.adaptris.core.ServiceException) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) MultiItemConfiguredXpathQuery(com.adaptris.core.services.metadata.xpath.MultiItemConfiguredXpathQuery) ConfiguredXpathQuery(com.adaptris.core.services.metadata.xpath.ConfiguredXpathQuery) Test(org.junit.Test)

Example 10 with XpathQuery

use of com.adaptris.core.services.metadata.xpath.XpathQuery in project interlok by adaptris.

the class XpathMetadataServiceTest method testDoService_UsingXpathQuery_WithNamespaceContext_NotNamespaceAware.

@Test
public void testDoService_UsingXpathQuery_WithNamespaceContext_NotNamespaceAware() throws CoreException {
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(XML_WITH_NAMESPACE);
    XpathMetadataService service = new XpathMetadataService();
    service.setXpathQueries(new ArrayList<XpathQuery>(Arrays.asList(new ConfiguredXpathQuery("failureCount", "count(/schematron-output/failed-assert)"))));
    execute(service, msg);
    // count(/schematron-output/failed-assert) will return 0; becaue Saxon failed
    // like the mostly amusing thing that it is.
    assertEquals("0", msg.getMetadataValue("failureCount"));
    service.setXmlDocumentFactoryConfig(new DocumentBuilderFactoryBuilder().withNamespaceAware(false));
    execute(service, msg);
    assertTrue(msg.containsKey("failureCount"));
    assertEquals("2", msg.getMetadataValue("failureCount"));
}
Also used : XpathQuery(com.adaptris.core.services.metadata.xpath.XpathQuery) MultiItemConfiguredXpathQuery(com.adaptris.core.services.metadata.xpath.MultiItemConfiguredXpathQuery) MetadataXpathQuery(com.adaptris.core.services.metadata.xpath.MetadataXpathQuery) MultiItemMetadataXpathQuery(com.adaptris.core.services.metadata.xpath.MultiItemMetadataXpathQuery) ConfiguredXpathQuery(com.adaptris.core.services.metadata.xpath.ConfiguredXpathQuery) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) MultiItemConfiguredXpathQuery(com.adaptris.core.services.metadata.xpath.MultiItemConfiguredXpathQuery) ConfiguredXpathQuery(com.adaptris.core.services.metadata.xpath.ConfiguredXpathQuery) DocumentBuilderFactoryBuilder(com.adaptris.core.util.DocumentBuilderFactoryBuilder) Test(org.junit.Test)

Aggregations

XpathQuery (com.adaptris.core.services.metadata.xpath.XpathQuery)12 ConfiguredXpathQuery (com.adaptris.core.services.metadata.xpath.ConfiguredXpathQuery)10 MetadataXpathQuery (com.adaptris.core.services.metadata.xpath.MetadataXpathQuery)8 MultiItemConfiguredXpathQuery (com.adaptris.core.services.metadata.xpath.MultiItemConfiguredXpathQuery)8 MultiItemMetadataXpathQuery (com.adaptris.core.services.metadata.xpath.MultiItemMetadataXpathQuery)8 Test (org.junit.Test)8 AdaptrisMessage (com.adaptris.core.AdaptrisMessage)6 ServiceException (com.adaptris.core.ServiceException)4 DocumentBuilderFactoryBuilder (com.adaptris.core.util.DocumentBuilderFactoryBuilder)3 ArrayList (java.util.ArrayList)2 CoreException (com.adaptris.core.CoreException)1 MetadataElement (com.adaptris.core.MetadataElement)1 LogMessageService (com.adaptris.core.services.LogMessageService)1 AddMetadataService (com.adaptris.core.services.metadata.AddMetadataService)1 XpathMetadataService (com.adaptris.core.services.metadata.XpathMetadataService)1 XpathObjectQuery (com.adaptris.core.services.metadata.xpath.XpathObjectQuery)1 TruncateMetadata (com.adaptris.core.util.TruncateMetadata)1 KeyValuePair (com.adaptris.util.KeyValuePair)1 SimpleNamespaceContext (com.adaptris.util.text.xml.SimpleNamespaceContext)1 XPath (com.adaptris.util.text.xml.XPath)1