use of org.apache.synapse.util.xpath.SynapseXPath in project wso2-synapse by wso2.
the class XSLTMediatorTest method testMultipleKeys.
/**
* Test with multiple keys including static and dynamic keys
*
* @param num number from 0 to 2
* @throws Exception Exception in case of an error in tests
*/
private void testMultipleKeys(int num) throws Exception {
String xsltKeyValue = null;
String path;
SynapseXPath xpath;
Value xsltKey;
XSLTMediator transformMediator = new XSLTMediator();
// default source, xsltFile, and state of key (dynamic or static)
String source = "";
String xsltFile = "";
boolean isDynamicKey = true;
// based on source, different XSLTFiles can be used
if (num == 0) {
source = SOURCE_STATIC_KEY;
xsltFile = "static_key.xslt";
xsltKeyValue = "StaticXsltKey";
isDynamicKey = false;
} else if (num == 1) {
source = SOURCE_DYNAMIC_KEY1;
xsltFile = "dynamic_key_1.xslt";
xsltKeyValue = "DynamicXsltKey1";
isDynamicKey = true;
} else if (num == 2) {
source = SOURCE_DYNAMIC_KEY2;
xsltFile = "dynamic_key_2.xslt";
xsltKeyValue = "DynamicXsltKey2";
isDynamicKey = true;
}
if (isDynamicKey) {
path = "//m0:CheckPriceRequest/m0:" + xsltKeyValue;
xpath = new SynapseXPath(path);
xpath.addNamespace("m0", "http://services.samples/xsd");
// Create key from dynamic key (xpath)
xsltKey = new Value(xpath);
// set XSLT transformation URL (Xpath)
transformMediator.setXsltKey(xsltKey);
} else {
// static key
path = xsltKeyValue;
// set XSLT transformation URL (static)
setXsltTransformationURL(transformMediator, path);
}
// Mediate twice for synCtx
MessageContext synCtx = new TestMessageContextBuilder().addEntry(xsltKeyValue, getClass().getResource(xsltFile)).setBodyFromString(source).addTextAroundBody().build();
transformMediator.mediate(synCtx);
synCtx = new TestMessageContextBuilder().addEntry(xsltKeyValue, getClass().getResource(xsltFile)).setBodyFromString(source).addTextAroundBody().build();
transformMediator.mediate(synCtx);
}
use of org.apache.synapse.util.xpath.SynapseXPath in project wso2-synapse by wso2.
the class XSLTMediatorTest method testTransformXSLTCustomSource.
public void testTransformXSLTCustomSource() throws Exception {
// create a new XSLT 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");
MessageContext synCtx = new TestMessageContextBuilder().addFileEntry("xslt-key", "../../repository/conf/sample/resources/transform/transform_unittest.xslt").setBodyFromString(SOURCE).addTextAroundBody().build();
transformMediator.mediate(synCtx);
// validate result
assertQuoteElement(synCtx.getEnvelope().getBody().getFirstOMChild().getNextOMSibling());
}
use of org.apache.synapse.util.xpath.SynapseXPath in project wso2-synapse by wso2.
the class URLRewriteMediatorTest method testUnconditionalRewriteScenario3.
public void testUnconditionalRewriteScenario3() throws Exception {
URLRewriteMediator mediator = new URLRewriteMediator();
mediator.setOutputProperty("outURL");
RewriteAction action1 = new RewriteAction();
action1.setValue(targetURL);
RewriteRule rule1 = new RewriteRule();
rule1.addRewriteAction(action1);
mediator.addRule(rule1);
RewriteAction action2 = new RewriteAction();
action2.setValue("/services/SimpleStockQuoteService");
action2.setFragmentIndex(URIFragments.PATH);
RewriteAction action3 = new RewriteAction();
action3.setXpath(new SynapseXPath("get-property('port')"));
action3.setFragmentIndex(URIFragments.PORT);
RewriteRule rule2 = new RewriteRule();
rule2.addRewriteAction(action2);
rule2.addRewriteAction(action3);
mediator.addRule(rule2);
MessageContext msgCtx = TestUtils.createLightweightSynapseMessageContext("<empty/>");
msgCtx.setTo(new EndpointReference("http://localhost:8280"));
msgCtx.setProperty("port", 9000);
mediator.mediate(msgCtx);
assertEquals(targetURL, msgCtx.getProperty("outURL"));
}
use of org.apache.synapse.util.xpath.SynapseXPath in project wso2-synapse by wso2.
the class SynapseCommodityServiceTest method testN2N.
public void testN2N() throws Exception {
// Creating the Simple Commodity Client
System.getProperties().remove(org.apache.axis2.Constants.AXIS2_CONF);
ServiceClient businessClient = new ServiceClient(null, null);
Options options = new Options();
options.setTo(new EndpointReference("http://127.0.0.1:10100/CommodityQuote"));
businessClient.setOptions(options);
OMElement response = null;
response = businessClient.sendReceive(commodityPayload());
assertNotNull(response);
SynapseXPath xPath = new SynapseXPath("//return");
xPath.addNamespace("ns", "http://services.samples/xsd");
OMElement returnEle = (OMElement) xPath.selectSingleNode(response);
assertNotNull(returnEle);
assertEquals(returnEle.getText().trim(), "100");
}
use of org.apache.synapse.util.xpath.SynapseXPath in project wso2-synapse by wso2.
the class PropertyMediatorTest method testXMLPropertyHandling.
public void testXMLPropertyHandling() throws Exception {
PropertyMediator propMediatorOne = new PropertyMediator();
propMediatorOne.setName("nameOne");
String xml = "<Project><name>Synapse</name></Project>";
OMElement valueOne = TestUtils.createOMElement(xml);
propMediatorOne.setValueElement(valueOne);
// Test setting XML properties
MessageContext synCtx = TestUtils.getTestContext("<getQuote><symbol>IBM</symbol></getQuote>");
propMediatorOne.mediate(synCtx);
Object prop = synCtx.getProperty("nameOne");
// Objects are not equal, so need to compare the content
assertEquals(valueOne.toString(), prop.toString());
// Test XML property retreival
String exprValue = new SynapseXPath("synapse:get-property('nameOne')").stringValueOf(synCtx);
assertEquals(xml, exprValue);
// Test property removal
propMediatorOne.setAction(PropertyMediator.ACTION_REMOVE);
propMediatorOne.mediate(synCtx);
assertNull(synCtx.getProperty("nameOne"));
// Setting XML properties using expressions
synCtx.setProperty("nameOne", xml);
PropertyMediator propertyMediatorTwo = new PropertyMediator();
propertyMediatorTwo.setName("nameTwo");
propertyMediatorTwo.setExpression(new SynapseXPath("synapse:get-property('nameOne')"), XMLConfigConstants.DATA_TYPES.OM.name());
propertyMediatorTwo.mediate(synCtx);
Object exprProp = synCtx.getProperty("nameTwo");
assertTrue(exprProp != null && exprProp instanceof OMElement);
}
Aggregations