Search in sources :

Example 41 with MessageContext

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);
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) OMElement(org.apache.axiom.om.OMElement) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 42 with MessageContext

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);
}
Also used : MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) Map(java.util.Map) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 43 with MessageContext

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)));
}
Also used : SynapseXPath(org.apache.synapse.util.xpath.SynapseXPath) MediatorProperty(org.apache.synapse.mediators.MediatorProperty) MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 44 with MessageContext

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);
}
Also used : TestMessageContextBuilder(org.apache.synapse.TestMessageContextBuilder) MessageContext(org.apache.synapse.MessageContext)

Example 45 with MessageContext

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);
}
Also used : TestMessageContextBuilder(org.apache.synapse.TestMessageContextBuilder) MessageContext(org.apache.synapse.MessageContext)

Aggregations

MessageContext (org.apache.synapse.MessageContext)220 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)86 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)54 SynapseException (org.apache.synapse.SynapseException)29 TestMessageContextBuilder (org.apache.synapse.TestMessageContextBuilder)26 ArrayList (java.util.ArrayList)24 Axis2SynapseEnvironment (org.apache.synapse.core.axis2.Axis2SynapseEnvironment)24 SynapseXPath (org.apache.synapse.util.xpath.SynapseXPath)24 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)18 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)18 OMElement (org.apache.axiom.om.OMElement)17 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)17 HashMap (java.util.HashMap)16 Mediator (org.apache.synapse.Mediator)16 TestMessageContext (org.apache.synapse.TestMessageContext)16 Map (java.util.Map)15 Properties (java.util.Properties)15 Test (org.junit.Test)15 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)14 QName (javax.xml.namespace.QName)13