Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Reduce duplicate code and code complexity

  • Loading branch information...
commit 0c35e381af5877fbd1611369a95901dadf6a3973 1 parent 5fa95c9
@christophd authored
View
173 modules/citrus-core/src/main/java/com/consol/citrus/config/xml/AbstractMessageActionParser.java
@@ -29,6 +29,8 @@
import com.consol.citrus.validation.builder.PayloadTemplateMessageBuilder;
import com.consol.citrus.validation.interceptor.XpathMessageConstructionInterceptor;
import com.consol.citrus.validation.script.GroovyScriptMessageBuilder;
+import com.consol.citrus.variable.MessageHeaderVariableExtractor;
+import com.consol.citrus.variable.VariableExtractor;
/**
* Parser providing basic message element configurations used in send and receive actions.
@@ -43,75 +45,108 @@
* @param messageElement
*/
public AbstractMessageContentBuilder<?> constructMessageBuilder(Element messageElement) {
- PayloadTemplateMessageBuilder payloadTemplateMessageBuilder = null;
- GroovyScriptMessageBuilder scriptMessageBuilder = null;
+ AbstractMessageContentBuilder<?> messageBuilder = null;
if (messageElement != null) {
- // parse payload with xs-any element
- Element payloadElement = DomUtils.getChildElementByTagName(messageElement, "payload");
- if (payloadElement != null) {
- payloadTemplateMessageBuilder = new PayloadTemplateMessageBuilder();
- payloadTemplateMessageBuilder.setPayloadData(PayloadElementParser.parseMessagePayload(payloadElement));
- }
-
- Element xmlDataElement = DomUtils.getChildElementByTagName(messageElement, "data");
- if (xmlDataElement != null) {
- payloadTemplateMessageBuilder = new PayloadTemplateMessageBuilder();
- payloadTemplateMessageBuilder.setPayloadData(DomUtils.getTextValue(xmlDataElement));
- }
-
- Element xmlResourceElement = DomUtils.getChildElementByTagName(messageElement, "resource");
- if (xmlResourceElement != null) {
- payloadTemplateMessageBuilder = new PayloadTemplateMessageBuilder();
- payloadTemplateMessageBuilder.setPayloadResource(FileUtils.getResourceFromFilePath(xmlResourceElement.getAttribute("file")));
+ messageBuilder = parsePayloadTemplateBuilder(messageElement);
+
+ if (messageBuilder == null) {
+ messageBuilder = parseScriptBuilder(messageElement);
}
+ }
+
+ return messageBuilder != null ? messageBuilder : new PayloadTemplateMessageBuilder();
+ }
+
+ /**
+ * @param messageElement
+ * @return
+ */
+ private GroovyScriptMessageBuilder parseScriptBuilder(Element messageElement) {
+ GroovyScriptMessageBuilder scriptMessageBuilder = null;
+
+ Element builderElement = DomUtils.getChildElementByTagName(messageElement, "builder");
+ if (builderElement != null) {
+ String builderType = builderElement.getAttribute("type");
- if (payloadElement != null || xmlDataElement != null || xmlResourceElement != null) {
- Map<String, String> setMessageValues = new HashMap<String, String>();
- List<?> messageValueElements = DomUtils.getChildElementsByTagName(messageElement, "element");
- for (Iterator<?> iter = messageValueElements.iterator(); iter.hasNext();) {
- Element messageValue = (Element) iter.next();
- setMessageValues.put(messageValue.getAttribute("path"), messageValue.getAttribute("value"));
- }
-
- if (!setMessageValues.isEmpty()) {
- XpathMessageConstructionInterceptor interceptor = new XpathMessageConstructionInterceptor(setMessageValues);
- payloadTemplateMessageBuilder.addMessageConstructingInterceptor(interceptor);
- }
+ if (!StringUtils.hasText(builderType)) {
+ throw new BeanCreationException("Missing message builder type - please define valid type " +
+ "attribute for message builder");
+ } else if (builderType.equals("groovy")) {
+ scriptMessageBuilder = new GroovyScriptMessageBuilder();
+ } else {
+ throw new BeanCreationException("Unsupported message builder type: '" + builderType + "'");
}
- Element builderElement = DomUtils.getChildElementByTagName(messageElement, "builder");
- if (builderElement != null) {
- String builderType = builderElement.getAttribute("type");
-
- if (!StringUtils.hasText(builderType)) {
- throw new BeanCreationException("Missing message builder type - please define valid type " +
- "attribute for message builder");
- } else if (builderType.equals("groovy")) {
- scriptMessageBuilder = new GroovyScriptMessageBuilder();
- } else {
- throw new BeanCreationException("Unsupported message builder type: '" + builderType + "'");
- }
-
- String scriptResource = builderElement.getAttribute("file");
-
- if (StringUtils.hasText(scriptResource)) {
- scriptMessageBuilder.setScriptResource(FileUtils.getResourceFromFilePath(scriptResource));
- } else {
- scriptMessageBuilder.setScriptData(DomUtils.getTextValue(builderElement));
- }
+ String scriptResource = builderElement.getAttribute("file");
+
+ if (StringUtils.hasText(scriptResource)) {
+ scriptMessageBuilder.setScriptResource(FileUtils.getResourceFromFilePath(scriptResource));
+ } else {
+ scriptMessageBuilder.setScriptData(DomUtils.getTextValue(builderElement));
}
}
- if (payloadTemplateMessageBuilder != null) {
- return payloadTemplateMessageBuilder;
- } else if (scriptMessageBuilder != null) {
- return scriptMessageBuilder;
- } else {
- return new PayloadTemplateMessageBuilder();
+ return scriptMessageBuilder;
+ }
+
+ /**
+ * Parses message payload template information given in message element.
+ * @param messageElement
+ * @param payloadTemplateMessageBuilder
+ */
+ private PayloadTemplateMessageBuilder parsePayloadTemplateBuilder(Element messageElement) {
+ PayloadTemplateMessageBuilder messageBuilder = null;
+
+ messageBuilder = parsePayloadElement(messageElement);
+
+ Element xmlDataElement = DomUtils.getChildElementByTagName(messageElement, "data");
+ if (xmlDataElement != null) {
+ messageBuilder = new PayloadTemplateMessageBuilder();
+ messageBuilder.setPayloadData(DomUtils.getTextValue(xmlDataElement));
+ }
+
+ Element xmlResourceElement = DomUtils.getChildElementByTagName(messageElement, "resource");
+ if (xmlResourceElement != null) {
+ messageBuilder = new PayloadTemplateMessageBuilder();
+ messageBuilder.setPayloadResource(FileUtils.getResourceFromFilePath(xmlResourceElement.getAttribute("file")));
}
+
+ if (messageBuilder != null) {
+ Map<String, String> overwriteMessageValues = new HashMap<String, String>();
+ List<?> messageValueElements = DomUtils.getChildElementsByTagName(messageElement, "element");
+ for (Iterator<?> iter = messageValueElements.iterator(); iter.hasNext();) {
+ Element messageValue = (Element) iter.next();
+ overwriteMessageValues.put(messageValue.getAttribute("path"), messageValue.getAttribute("value"));
+ }
+
+ if (!overwriteMessageValues.isEmpty()) {
+ XpathMessageConstructionInterceptor interceptor = new XpathMessageConstructionInterceptor(overwriteMessageValues);
+ messageBuilder.addMessageConstructingInterceptor(interceptor);
+ }
+ }
+
+ return messageBuilder;
}
-
+
+ /**
+ * Parses the xs:any payload elements nested in message element.
+ * @param messageElement
+ * @param payloadTemplateMessageBuilder
+ */
+ private PayloadTemplateMessageBuilder parsePayloadElement(Element messageElement) {
+ PayloadTemplateMessageBuilder messageBuilder = null;
+
+ // parse payload with xs-any element
+ Element payloadElement = DomUtils.getChildElementByTagName(messageElement, "payload");
+ if (payloadElement != null) {
+ messageBuilder = new PayloadTemplateMessageBuilder();
+ messageBuilder.setPayloadData(PayloadElementParser.parseMessagePayload(payloadElement));
+ }
+
+ return messageBuilder;
+ }
+
/**
* Parse message header elements in action and add headers to
* message content builder.
@@ -132,4 +167,26 @@ protected void parseHeaderElements(Element actionElement, AbstractMessageContent
messageBuilder.setMessageHeaders(messageHeaders);
}
}
+
+ /**
+ * Parses header extract information.
+ * @param element the root action element.
+ * @param variableExtractors the variable extractors to add new extractors to.
+ */
+ protected void parseExtractHeaderElements(Element element, List<VariableExtractor> variableExtractors) {
+ Element extractElement = DomUtils.getChildElementByTagName(element, "extract");
+ Map<String, String> extractHeaderValues = new HashMap<String, String>();
+ if (extractElement != null) {
+ List<?> headerValueElements = DomUtils.getChildElementsByTagName(extractElement, "header");
+ for (Iterator<?> iter = headerValueElements.iterator(); iter.hasNext();) {
+ Element headerValue = (Element) iter.next();
+ extractHeaderValues.put(headerValue.getAttribute("name"), headerValue.getAttribute("variable"));
+ }
+
+ MessageHeaderVariableExtractor headerVariableExtractor = new MessageHeaderVariableExtractor();
+ headerVariableExtractor.setHeaderMappings(extractHeaderValues);
+
+ variableExtractors.add(headerVariableExtractor);
+ }
+ }
}
View
16 modules/citrus-core/src/main/java/com/consol/citrus/config/xml/ReceiveMessageActionParser.java
@@ -112,21 +112,12 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {
*/
private List<VariableExtractor> getVariableExtractors(Element element) {
List<VariableExtractor> variableExtractors = new ArrayList<VariableExtractor>();
+
+ parseExtractHeaderElements(element, variableExtractors);
Element extractElement = DomUtils.getChildElementByTagName(element, "extract");
Map<String, String> extractMessageValues = new HashMap<String, String>();
- Map<String, String> extractHeaderValues = new HashMap<String, String>();
if (extractElement != null) {
- List<?> headerValueElements = DomUtils.getChildElementsByTagName(extractElement, "header");
- for (Iterator<?> iter = headerValueElements.iterator(); iter.hasNext();) {
- Element headerValue = (Element) iter.next();
- extractHeaderValues.put(headerValue.getAttribute("name"), headerValue.getAttribute("variable"));
- }
- MessageHeaderVariableExtractor headerVariableExtractor = new MessageHeaderVariableExtractor();
- headerVariableExtractor.setHeaderMappings(extractHeaderValues);
-
- variableExtractors.add(headerVariableExtractor);
-
List<?> messageValueElements = DomUtils.getChildElementsByTagName(extractElement, "message");
for (Iterator<?> iter = messageValueElements.iterator(); iter.hasNext();) {
Element messageValue = (Element) iter.next();
@@ -259,10 +250,11 @@ private ScriptValidationContext getScriptValidationContext(Element element) {
* @param context the message validation context.
*/
private void parseValidationElements(Element messageElement, XmlMessageValidationContext context) {
- //check for validate elements, these elements can either have script, xpath or namespace validation information
+ //check for validate elements, these elements can either have script, xpath or namespace validation information
//script validation is handled separately for now we only handle xpath and namepsace validation
Map<String, String> validateNamespaces = new HashMap<String, String>();
Map<String, String> validateXpathExpressions = new HashMap<String, String>();
+
List<?> validateElements = DomUtils.getChildElementsByTagName(messageElement, "validate");
if (validateElements.size() > 0) {
for (Iterator<?> iter = validateElements.iterator(); iter.hasNext();) {
View
20 modules/citrus-core/src/main/java/com/consol/citrus/config/xml/SendMessageActionParser.java
@@ -16,7 +16,8 @@
package com.consol.citrus.config.xml;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.List;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.config.BeanDefinition;
@@ -28,7 +29,6 @@
import com.consol.citrus.util.FileUtils;
import com.consol.citrus.validation.builder.AbstractMessageContentBuilder;
-import com.consol.citrus.variable.MessageHeaderVariableExtractor;
import com.consol.citrus.variable.VariableExtractor;
/**
@@ -67,21 +67,7 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {
}
List<VariableExtractor> variableExtractors = new ArrayList<VariableExtractor>();
-
- Element extractElement = DomUtils.getChildElementByTagName(element, "extract");
- Map<String, String> extractHeaderValues = new HashMap<String, String>();
- if (extractElement != null) {
- List<?> headerValueElements = DomUtils.getChildElementsByTagName(extractElement, "header");
- for (Iterator<?> iter = headerValueElements.iterator(); iter.hasNext();) {
- Element headerValue = (Element) iter.next();
- extractHeaderValues.put(headerValue.getAttribute("name"), headerValue.getAttribute("variable"));
- }
-
- MessageHeaderVariableExtractor headerVariableExtractor = new MessageHeaderVariableExtractor();
- headerVariableExtractor.setHeaderMappings(extractHeaderValues);
-
- variableExtractors.add(headerVariableExtractor);
- }
+ parseExtractHeaderElements(element, variableExtractors);
if (!variableExtractors.isEmpty()) {
builder.addPropertyValue("variableExtractors", variableExtractors);
Please sign in to comment.
Something went wrong with that request. Please try again.