Search in sources :

Example 21 with TestMessageContextBuilder

use of org.apache.synapse.TestMessageContextBuilder in project wso2-synapse by wso2.

the class XSLTMediatorTest method testTransformXSLTCustomSourceNonMainElement.

public void testTransformXSLTCustomSourceNonMainElement() throws Exception {
    // create a new switch mediator
    XSLTMediator transformMediator = new XSLTMediator();
    // set xpath condition to select source
    SynapseXPath xpath = new SynapseXPath("//m0:CheckPriceRequest");
    xpath.addNamespace("m0", "http://services.samples/xsd");
    transformMediator.setSource(xpath);
    // set XSLT transformation URL
    setXsltTransformationURL(transformMediator, "xslt-key");
    // invoke transformation, with static enveope
    MessageContext synCtx = new TestMessageContextBuilder().addFileEntry("xslt-key", "../../repository/conf/sample/resources/transform/transform_unittest.xslt").setBodyFromString(ENCLOSING_SOURCE).addTextAroundBody().build();
    transformMediator.mediate(synCtx);
    // validate result
    OMContainer body = synCtx.getEnvelope().getBody();
    if (body.getFirstOMChild().getNextOMSibling() instanceof OMElement) {
        OMElement someOtherElem = (OMElement) body.getFirstOMChild().getNextOMSibling();
        assertTrue("someOtherElement".equals(someOtherElem.getLocalName()));
        assertTrue("http://someother".equals(someOtherElem.getNamespace().getNamespaceURI()));
        assertQuoteElement(someOtherElem.getFirstOMChild());
    } else {
        fail("Unexpected element found in SOAP body");
    }
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) TestMessageContextBuilder(org.apache.synapse.TestMessageContextBuilder) MessageContext(org.apache.synapse.MessageContext)

Example 22 with TestMessageContextBuilder

use of org.apache.synapse.TestMessageContextBuilder in project wso2-synapse by wso2.

the class XSLTMediatorTest method test.

protected static void test(Class sbf, Class rbf) throws Exception {
    XSLTMediator transformMediator = new XSLTMediator();
    Value xsltKey = new Value("xslt-key");
    transformMediator.setXsltKey(xsltKey);
    MessageContext mc = new TestMessageContextBuilder().addEntry("xslt-key", XSLTMediator.class.getResource("identity.xslt")).build();
    OMFactory factory = OMAbstractFactory.getOMFactory();
    OMElement orgRoot = factory.createOMElement(new QName("root"));
    OMElement orgElement = factory.createOMElement(new QName("urn:mynamespace", "element1"));
    orgElement.setText("test");
    OMAttribute orgAttribute = orgElement.addAttribute("att", "testValue", null);
    orgRoot.addChild(orgElement);
    mc.getEnvelope().getBody().addChild(orgRoot);
    transformMediator.addAttribute(XSLTMediator.SOURCE_BUILDER_FACTORY, sbf.getName());
    transformMediator.addAttribute(XSLTMediator.RESULT_BUILDER_FACTORY, rbf.getName());
    transformMediator.mediate(mc);
    OMElement root = mc.getEnvelope().getBody().getFirstElement();
    assertEquals(orgRoot.getQName(), root.getQName());
    OMElement element = (OMElement) root.getFirstOMChild();
    assertEquals(orgElement.getQName(), element.getQName());
    assertEquals(orgElement.getText(), element.getText());
    assertEquals(orgAttribute, orgElement.getAttribute(orgAttribute.getQName()));
    assertNull(element.getNextOMSibling());
}
Also used : TestMessageContextBuilder(org.apache.synapse.TestMessageContextBuilder) QName(javax.xml.namespace.QName) Value(org.apache.synapse.mediators.Value) MessageContext(org.apache.synapse.MessageContext)

Example 23 with TestMessageContextBuilder

use of org.apache.synapse.TestMessageContextBuilder in project wso2-synapse by wso2.

the class ValidateMediatorTest method testValidateMediatorJSONSchemaWithInvalidSourceValidCase.

/**
 * This method tests the mediator when an invalid source json-path is given.
 * Test gets passed if mediator invokes on-fail sequence.
 * @throws Exception
 */
public void testValidateMediatorJSONSchemaWithInvalidSourceValidCase() throws Exception {
    // create a validate mediator
    ValidateMediator validate = new ValidateMediator();
    // set the schema url, source json-path and any name spaces
    validate.setSchemaKeys(createKeyListFromStaticKey("JSON-key"));
    // set invalid json-path
    validate.setSource(createJSONPath("$.msg.get"));
    MessageContext synCtx = new TestMessageContextBuilder().setRequireAxis2MessageContext(true).addFileEntry("JSON-key", "./../../repository/conf/sample/resources/validate/StockQuoteSchema.json").setJsonBodyFromString(VALID_JSON_MESSAGE1).build();
    // test validate mediator, with static envelope
    test(validate, synCtx, true);
}
Also used : TestMessageContextBuilder(org.apache.synapse.TestMessageContextBuilder) MessageContext(org.apache.synapse.MessageContext)

Example 24 with TestMessageContextBuilder

use of org.apache.synapse.TestMessageContextBuilder 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 25 with TestMessageContextBuilder

use of org.apache.synapse.TestMessageContextBuilder in project wso2-synapse by wso2.

the class ValidateMediatorTest method testValidateMediatorJSONSchemaInValidCase.

public void testValidateMediatorJSONSchemaInValidCase() throws Exception {
    // create a validate mediator
    ValidateMediator validate = new ValidateMediator();
    // set the schema url, source xpath and any name spaces
    validate.setSchemaKeys(createKeyListFromStaticKey("JSON-key"));
    validate.setSource(createJSONPath("$.msg"));
    MessageContext synCtx = new TestMessageContextBuilder().setRequireAxis2MessageContext(true).addFileEntry("JSON-key", "./../../repository/conf/sample/resources/validate/StockQuoteSchema.json").setJsonBodyFromString(INVALID_JSON_MESSAGE1).build();
    // test validate mediator, with static enveope
    test(validate, synCtx, true);
}
Also used : TestMessageContextBuilder(org.apache.synapse.TestMessageContextBuilder) MessageContext(org.apache.synapse.MessageContext)

Aggregations

TestMessageContextBuilder (org.apache.synapse.TestMessageContextBuilder)29 MessageContext (org.apache.synapse.MessageContext)26 SynapseXPath (org.apache.synapse.util.xpath.SynapseXPath)4 Map (java.util.Map)3 Entry (org.apache.synapse.config.Entry)3 QName (javax.xml.namespace.QName)2 Value (org.apache.synapse.mediators.Value)2 SynapseException (org.apache.synapse.SynapseException)1 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)1