use of org.apache.synapse.MessageContext 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);
}
use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.
the class PropertyMediatorTest method testTypeAwarePropertyHandling.
public void testTypeAwarePropertyHandling() throws Exception {
PropertyMediator propMediatorOne = new PropertyMediator();
propMediatorOne.setName("nameOne");
propMediatorOne.setValue("valueOne", XMLConfigConstants.DATA_TYPES.STRING.name());
PropertyMediator propMediatorTwo = new PropertyMediator();
propMediatorTwo.setName("nameTwo");
propMediatorTwo.setValue("25000", XMLConfigConstants.DATA_TYPES.INTEGER.name());
propMediatorTwo.setScope(XMLConfigConstants.SCOPE_AXIS2);
PropertyMediator propMediatorThree = new PropertyMediator();
propMediatorThree.setName("nameThree");
propMediatorThree.setValue("123.456", XMLConfigConstants.DATA_TYPES.DOUBLE.name());
propMediatorThree.setScope(XMLConfigConstants.SCOPE_TRANSPORT);
PropertyMediator propMediatorFour = new PropertyMediator();
propMediatorFour.setName("nameFour");
propMediatorFour.setValue("true", XMLConfigConstants.DATA_TYPES.BOOLEAN.name());
PropertyMediator propMediatorFive = new PropertyMediator();
propMediatorFive.setName("nameFive");
propMediatorFive.setValue("123456", XMLConfigConstants.DATA_TYPES.LONG.name());
propMediatorFive.setScope(XMLConfigConstants.SCOPE_AXIS2);
PropertyMediator propMediatorSix = new PropertyMediator();
propMediatorSix.setName("nameSix");
propMediatorSix.setValue("12345", XMLConfigConstants.DATA_TYPES.SHORT.name());
propMediatorSix.setScope(XMLConfigConstants.SCOPE_TRANSPORT);
PropertyMediator propMediatorSeven = new PropertyMediator();
propMediatorSeven.setName("nameSeven");
propMediatorSeven.setValue("123.456", XMLConfigConstants.DATA_TYPES.FLOAT.name());
MessageContext synCtx = TestUtils.createLightweightSynapseMessageContext("<empty/>");
propMediatorOne.mediate(synCtx);
propMediatorTwo.mediate(synCtx);
propMediatorThree.mediate(synCtx);
propMediatorFour.mediate(synCtx);
propMediatorFive.mediate(synCtx);
propMediatorSix.mediate(synCtx);
propMediatorSeven.mediate(synCtx);
org.apache.axis2.context.MessageContext axisCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
Map transportHeaders = (Map) axisCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
Object valueOne = synCtx.getProperty("nameOne");
Object valueTwo = axisCtx.getProperty("nameTwo");
Object valueThree = transportHeaders.get("nameThree");
Object valueFour = synCtx.getProperty("nameFour");
Object valueFive = axisCtx.getProperty("nameFive");
Object valueSix = transportHeaders.get("nameSix");
Object valueSeven = synCtx.getProperty("nameSeven");
assertEquals("valueOne", valueOne);
assertEquals(new Integer(25000), valueTwo);
assertEquals(new Double(123.456), valueThree);
assertEquals(Boolean.TRUE, valueFour);
assertEquals(new Long(123456), valueFive);
assertEquals(new Short("12345"), valueSix);
assertEquals(new Float(123.456), valueSeven);
}
use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.
the class PropertyMediatorTest method testMediatorPropertiesExpression.
public void testMediatorPropertiesExpression() throws Exception {
// set a local property to the synapse context
PropertyMediator propMediator = new PropertyMediator();
propMediator.setName("name");
propMediator.setValue("value");
MessageContext synCtx = TestUtils.getTestContext("<empty/>");
propMediator.mediate(synCtx);
// read property through a mediator property
MediatorProperty medProp = new MediatorProperty();
medProp.setExpression(new SynapseXPath("synapse:get-property('name')"));
assertTrue("value".equals(medProp.getEvaluatedExpression(synCtx)));
}
use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.
the class ValidateMediatorTest method makeValidInvocation.
private void makeValidInvocation(ValidateMediator validate) throws Exception {
// set the schema url, source xpath and any name spaces
validate.setSchemaKeys(createKeyListFromStaticKey("xsd-key-1"));
validate.setSource(createXPath("//m0:CheckPriceRequest"));
MessageContext synCtx = new TestMessageContextBuilder().setRequireAxis2MessageContext(true).addFileEntry("xsd-key-1", "./../../repository/conf/sample/resources/validate/validate.xsd").setBodyFromString(VALID_ENVELOPE).build();
// test validate mediator, with static enveope
test(validate, synCtx, false);
}
use of org.apache.synapse.MessageContext in project wso2-synapse by wso2.
the class ValidateMediatorTest method testValidateMediatorValidCaseTwoSchemas.
public void testValidateMediatorValidCaseTwoSchemas() throws Exception {
// create a validate mediator
ValidateMediator validate = new ValidateMediator();
// set the schema url, source xpath and any name spaces
validate.setSchemaKeys(createKeyListFromMoreKeys("xsd-key-1", "xsd-key-2"));
validate.setSource(createXPath("//m1:Outer"));
MessageContext synCtx = new TestMessageContextBuilder().setRequireAxis2MessageContext(true).addFileEntry("xsd-key-1", "./../../repository/conf/sample/resources/validate/validate.xsd").addFileEntry("xsd-key-2", "./../../repository/conf/sample/resources/validate/validate2.xsd").setBodyFromString(VALID_ENVELOPE_TWO_SCHEMAS).build();
// test validate mediator, with static envelope
test(validate, synCtx, false);
}
Aggregations