From 15394bd9ec06bf7066522159ad6f7f0e7e44b5e6 Mon Sep 17 00:00:00 2001 From: DedunuKarunarathne <46235093+DedunuKarunarathne@users.noreply.github.com> Date: Thu, 9 Jan 2025 09:41:26 +0530 Subject: [PATCH 1/2] Initialize custom Xpath extensions in unit test synapse environment Initialize custom Xpath extensions in unit test synapse environment. Fixes: https://github.com/wso2/product-micro-integrator/issues/3842 --- .../synapse/Axis2SynapseController.java | 32 +------------------ .../unittest/ConfigurationDeployer.java | 2 ++ .../util/xpath/ext/XpathExtensionUtil.java | 29 +++++++++++++++++ 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/Axis2SynapseController.java b/modules/core/src/main/java/org/apache/synapse/Axis2SynapseController.java index 7938412717..74e3dbcbdc 100644 --- a/modules/core/src/main/java/org/apache/synapse/Axis2SynapseController.java +++ b/modules/core/src/main/java/org/apache/synapse/Axis2SynapseController.java @@ -50,8 +50,6 @@ import org.apache.synapse.eventing.SynapseEventSource; import org.apache.synapse.task.*; import org.wso2.securevault.secret.handler.SharedSecretCallbackHandlerCache; -import org.apache.synapse.util.xpath.ext.SynapseXpathFunctionContextProvider; -import org.apache.synapse.util.xpath.ext.SynapseXpathVariableResolver; import org.apache.synapse.util.xpath.ext.XpathExtensionUtil; import java.io.File; @@ -396,7 +394,7 @@ public SynapseEnvironment createSynapseEnvironment() { } //we initialize xpath extensions here since synapse environment is available - initXpathExtensions(synapseEnvironment); + XpathExtensionUtil.initXpathExtensions(synapseEnvironment); try { deployMediationLibraryArtifacts(); @@ -417,34 +415,6 @@ public SynapseEnvironment createSynapseEnvironment() { return synapseEnvironment; } - /** - * This method initializes Xpath Extensions available through synapse.properties file. - * Xpath Extensions can be defined in Variable Context Extensions + Function Context Extensions - * synapse.xpath.var.extensions --> Variable Extensions - * synapse.xpath.func.extensions --> Function Extensions - * - * @param synapseEnvironment SynapseEnvironment - */ - private void initXpathExtensions(SynapseEnvironment synapseEnvironment) { - Axis2SynapseEnvironment axis2SynapseEnvironment = (Axis2SynapseEnvironment) synapseEnvironment; - - /*Initialize Function Context extensions for xpath - */ - List functionExtensions = - XpathExtensionUtil.getRegisteredFunctionExtensions(); - for (SynapseXpathFunctionContextProvider functionExtension : functionExtensions) { - axis2SynapseEnvironment.setXpathFunctionExtensions(functionExtension); - } - - /*Initialize Variable Context extensions for xpath - */ - List variableExtensions = - XpathExtensionUtil.getRegisteredVariableExtensions(); - for (SynapseXpathVariableResolver variableExtension : variableExtensions) { - axis2SynapseEnvironment.setXpathVariableExtensions(variableExtension); - } - } - /** * The mediation library deployer will handling the process of deploying the * libararyArtifacts, this is required since the library specific artifacts diff --git a/modules/core/src/main/java/org/apache/synapse/unittest/ConfigurationDeployer.java b/modules/core/src/main/java/org/apache/synapse/unittest/ConfigurationDeployer.java index e2ed318a18..6b38edb455 100755 --- a/modules/core/src/main/java/org/apache/synapse/unittest/ConfigurationDeployer.java +++ b/modules/core/src/main/java/org/apache/synapse/unittest/ConfigurationDeployer.java @@ -32,6 +32,7 @@ import org.apache.synapse.deployers.ProxyServiceDeployer; import org.apache.synapse.deployers.SequenceDeployer; import org.apache.synapse.deployers.TemplateDeployer; +import org.apache.synapse.util.xpath.ext.XpathExtensionUtil; import java.util.AbstractMap; import java.util.Map; @@ -209,6 +210,7 @@ private ConfigurationContext createConfigurationContext(SynapseConfiguration syn axisConfiguration.addParameter(new Parameter(SynapseConstants.SYNAPSE_ENV, synapseEnvironment)); axisConfiguration.addParameter(new Parameter(SynapseConstants.SYNAPSE_CONFIG, synapseConfiguration)); configurationContext.setAxisConfiguration(axisConfiguration); + XpathExtensionUtil.initXpathExtensions(synapseEnvironment); return configurationContext; } diff --git a/modules/core/src/main/java/org/apache/synapse/util/xpath/ext/XpathExtensionUtil.java b/modules/core/src/main/java/org/apache/synapse/util/xpath/ext/XpathExtensionUtil.java index d8325b499b..b344cdbf53 100644 --- a/modules/core/src/main/java/org/apache/synapse/util/xpath/ext/XpathExtensionUtil.java +++ b/modules/core/src/main/java/org/apache/synapse/util/xpath/ext/XpathExtensionUtil.java @@ -24,6 +24,7 @@ import org.apache.synapse.SynapseException; import org.apache.synapse.config.SynapsePropertiesLoader; import org.apache.synapse.core.SynapseEnvironment; +import org.apache.synapse.core.axis2.Axis2SynapseEnvironment; import org.jaxen.Function; import java.util.ArrayList; @@ -50,6 +51,34 @@ public class XpathExtensionUtil { private static final Log log = LogFactory.getLog(XpathExtensionUtil.class); + /** + * This method initializes Xpath Extensions available through synapse.properties file. + * Xpath Extensions can be defined in Variable Context Extensions + Function Context Extensions + * synapse.xpath.var.extensions --> Variable Extensions + * synapse.xpath.func.extensions --> Function Extensions + * + * @param synapseEnvironment SynapseEnvironment + */ + public static void initXpathExtensions(SynapseEnvironment synapseEnvironment) { + Axis2SynapseEnvironment axis2SynapseEnvironment = (Axis2SynapseEnvironment) synapseEnvironment; + + /*Initialize Function Context extensions for xpath + */ + List functionExtensions = + XpathExtensionUtil.getRegisteredFunctionExtensions(); + for (SynapseXpathFunctionContextProvider functionExtension : functionExtensions) { + axis2SynapseEnvironment.setXpathFunctionExtensions(functionExtension); + } + + /*Initialize Variable Context extensions for xpath + */ + List variableExtensions = + XpathExtensionUtil.getRegisteredVariableExtensions(); + for (SynapseXpathVariableResolver variableExtension : variableExtensions) { + axis2SynapseEnvironment.setXpathVariableExtensions(variableExtension); + } + } + /** * Get all registered variable context extensions. Synapse will look for synapse.properties * property synapse.xpath.var.extensions From ca246277f6954c9d7beb53dd40dfa80b548fdbe8 Mon Sep 17 00:00:00 2001 From: DedunuKarunarathne <46235093+DedunuKarunarathne@users.noreply.github.com> Date: Thu, 9 Jan 2025 10:56:07 +0530 Subject: [PATCH 2/2] Code format --- .../apache/synapse/util/xpath/ext/XpathExtensionUtil.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/modules/core/src/main/java/org/apache/synapse/util/xpath/ext/XpathExtensionUtil.java b/modules/core/src/main/java/org/apache/synapse/util/xpath/ext/XpathExtensionUtil.java index b344cdbf53..adb4203cca 100644 --- a/modules/core/src/main/java/org/apache/synapse/util/xpath/ext/XpathExtensionUtil.java +++ b/modules/core/src/main/java/org/apache/synapse/util/xpath/ext/XpathExtensionUtil.java @@ -64,16 +64,14 @@ public static void initXpathExtensions(SynapseEnvironment synapseEnvironment) { /*Initialize Function Context extensions for xpath */ - List functionExtensions = - XpathExtensionUtil.getRegisteredFunctionExtensions(); + List functionExtensions = getRegisteredFunctionExtensions(); for (SynapseXpathFunctionContextProvider functionExtension : functionExtensions) { axis2SynapseEnvironment.setXpathFunctionExtensions(functionExtension); } /*Initialize Variable Context extensions for xpath */ - List variableExtensions = - XpathExtensionUtil.getRegisteredVariableExtensions(); + List variableExtensions = getRegisteredVariableExtensions(); for (SynapseXpathVariableResolver variableExtension : variableExtensions) { axis2SynapseEnvironment.setXpathVariableExtensions(variableExtension); }