Search in sources :

Example 61 with SynapseXPath

use of org.apache.synapse.util.xpath.SynapseXPath in project wso2-synapse by wso2.

the class ValidateMediatorTest method testMultipleKeys.

/**
 * Test with multiple keys including static and dynamic keys
 *
 * @param num number from 0 to 1
 * @throws Exception Exception in case of an error in tests
 */
private void testMultipleKeys(int num) throws Exception {
    String xsdKeyValue = null;
    String path;
    SynapseXPath xpath;
    // create a validate mediator
    ValidateMediator validate = new ValidateMediator();
    // default source, xsdFile, and state of key (dynamic or static)
    String source = "";
    String xsdFile = "";
    boolean isDynamicKey = true;
    // based on source, different xsdFiles can be used
    if (num == 0) {
        source = VALID_ENVELOPE;
        xsdKeyValue = "xsd-key";
        isDynamicKey = false;
        xsdFile = "validate";
    } else if (num == 1) {
        source = DYNAMIC_KEY_ENVELOPE;
        // xsdFile = "dynamic_key1.xsd";
        xsdKeyValue = "DynamicXsdKey";
        isDynamicKey = true;
        xsdFile = "validate3";
    }
    if (isDynamicKey) {
        // set the schema url using dynamic key (Xpath)
        path = "//m0:CheckPriceRequest/m0:" + xsdKeyValue;
        xpath = new SynapseXPath(path);
        xpath.addNamespace("m0", "http://services.samples/xsd");
        validate.setSchemaKeys(createKeyListFromDynamicKey(xpath));
    } else {
        // set the schema url using static key
        validate.setSchemaKeys(createKeyListFromStaticKey(xsdKeyValue));
    }
    MessageContext synCtx = new TestMessageContextBuilder().setRequireAxis2MessageContext(true).addFileEntry(xsdKeyValue, "./../../repository/conf/sample/resources/validate/" + xsdFile + ".xsd").setBodyFromString(source).build();
    test(validate, synCtx, false);
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) TestMessageContextBuilder(org.apache.synapse.TestMessageContextBuilder) MessageContext(org.apache.synapse.MessageContext)

Example 62 with SynapseXPath

use of org.apache.synapse.util.xpath.SynapseXPath in project wso2-synapse by wso2.

the class ValidateMediatorTest method createXPath.

private SynapseXPath createXPath(String expression) throws JaxenException {
    SynapseXPath xpath = new SynapseXPath(expression);
    xpath.addNamespace("m0", "http://services.samples/xsd");
    xpath.addNamespace("m1", "http://services.samples/xsd2");
    return xpath;
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath)

Example 63 with SynapseXPath

use of org.apache.synapse.util.xpath.SynapseXPath in project wso2-synapse by wso2.

the class RecipientListEndpointTest method testSendToDynamicMembers.

/**
 * Test on Sending messages to a dynamic EP based on an expression
 *
 * @throws Exception on test failure
 */
@Test
public void testSendToDynamicMembers() throws Exception {
    // perform init
    Axis2SynapseEnvironment synapseEnvironment = getMockedSynapseEnvironment();
    RecipientListEndpoint recipientListEndpoint = new RecipientListEndpoint(2);
    recipientListEndpoint.init(synapseEnvironment);
    Mockito.when(synapseEnvironment.createMessageContext()).thenReturn(createMessageContext());
    // add dynamic EPs
    Value dynamicEPs = new Value(new SynapseXPath("//endpoints"));
    recipientListEndpoint.setDynamicEnpointSet(dynamicEPs);
    // test send message
    String samplePayload = "<test><endpoints>http://localhost:9000/services/SimpleStockQuoteService," + "http://localhost:9001/services/SimpleStockQuoteService" + "</endpoints><body>wso2</body></test>";
    Axis2MessageContext messageContext = getMessageContext(samplePayload);
    // message will be sent to EP using this env (which is mocked and do nothing)
    messageContext.setEnvironment(synapseEnvironment);
    // messageContext.setTo(new EndpointReference("http://localhost:9000/services/SimpleStockQuoteService"));
    recipientListEndpoint.sendMessage(messageContext);
}
Also used : Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) Value(org.apache.synapse.mediators.Value) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Test(org.junit.Test)

Example 64 with SynapseXPath

use of org.apache.synapse.util.xpath.SynapseXPath in project wso2-synapse by wso2.

the class AnnotatedCommandMediatorTest method testNamspaces.

public void testNamspaces() throws Exception {
    AnnotatedCommandMediator m = new AnnotatedCommandMediator();
    m.setCommand(AnnotatedCommand.class);
    SynapseXPath ax = m.afterFields.get(AnnotatedCommand.class.getDeclaredField("ReadAndUpdateField"));
    assertEquals(1, ax.getNamespaces().size());
    assertEquals("http://myns", ax.getNamespaces().values().iterator().next());
    assertEquals("myns", ax.getNamespaces().keySet().iterator().next());
    ax = m.afterFields.get(AnnotatedCommand.class.getDeclaredField("nsTest1"));
    assertEquals(2, ax.getNamespaces().size());
    assertTrue(ax.getNamespaces().keySet().contains("myns"));
    assertTrue(ax.getNamespaces().keySet().contains("ns"));
    assertTrue(ax.getNamespaces().values().contains("http://myns"));
    assertTrue(ax.getNamespaces().values().contains("http://ns"));
    ax = m.afterFields.get(AnnotatedCommand.class.getDeclaredField("nsTest2"));
    assertEquals(2, ax.getNamespaces().size());
    assertTrue(ax.getNamespaces().keySet().contains("myns"));
    assertTrue(ax.getNamespaces().keySet().contains("xns"));
    assertTrue(ax.getNamespaces().values().contains("http://myns"));
    assertTrue(ax.getNamespaces().values().contains("http://xns"));
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath)

Example 65 with SynapseXPath

use of org.apache.synapse.util.xpath.SynapseXPath in project wso2-synapse by wso2.

the class FilterMediatorTest method testFilterConditionWithThenElseKey.

public void testFilterConditionWithThenElseKey() throws Exception {
    setFilterConditionPassed(false);
    // create a new filter mediator
    FilterMediator filter = new FilterMediator();
    // set source xpath condition to //symbol
    SynapseXPath source = new SynapseXPath("//wsx:symbol");
    source.addNamespace("wsx", "http://www.webserviceX.NET/");
    filter.setSource(source);
    // set regex to MSFT
    Pattern regex = Pattern.compile("MSFT");
    filter.setRegex(regex);
    MessageContext msgCtx = TestUtils.getTestContext(REQ);
    SequenceMediator seq = new SequenceMediator();
    seq.setName("refSeq");
    seq.addChild(testMediator);
    msgCtx.getConfiguration().addSequence("refSeq", testMediator);
    filter.setElseKey("refSeq");
    // test validate mediator, with static enveope
    filter.mediate(msgCtx);
    assertTrue(filterConditionPassed);
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) Pattern(java.util.regex.Pattern) MessageContext(org.apache.synapse.MessageContext) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator)

Aggregations

SynapseXPath (org.apache.synapse.util.xpath.SynapseXPath)68 MessageContext (org.apache.synapse.MessageContext)24 JaxenException (org.jaxen.JaxenException)20 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)15 OMElement (org.apache.axiom.om.OMElement)14 OMAttribute (org.apache.axiom.om.OMAttribute)11 Value (org.apache.synapse.mediators.Value)9 TestMessageContext (org.apache.synapse.TestMessageContext)8 Pattern (java.util.regex.Pattern)6 Iterator (java.util.Iterator)5 QName (javax.xml.namespace.QName)5 EndpointReference (org.apache.axis2.addressing.EndpointReference)4 SynapseException (org.apache.synapse.SynapseException)4 TestMessageContextBuilder (org.apache.synapse.TestMessageContextBuilder)4 SynapseJsonPath (org.apache.synapse.util.xpath.SynapseJsonPath)4 Method (java.lang.reflect.Method)3 Map (java.util.Map)3 MediatorProperty (org.apache.synapse.mediators.MediatorProperty)3 Field (java.lang.reflect.Field)2 List (java.util.List)2