Mercurial > stress-tester
changeset 926:bb5eeffce305
FieldValueExtractorTransformer
author | Devel 2 |
---|---|
date | Fri, 27 Apr 2018 13:34:15 +0200 |
parents | 7dfc850d0715 |
children | 4d8642593e5c |
files | stress-tester/src/main/java/com/passus/st/client/http/extractor/XmlValueExtractor.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilter.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformer.java stress-tester/src/main/java/com/passus/st/config/FieldValueExtractorTransformer.java stress-tester/src/main/java/com/passus/st/config/FieldValueExtractorTransformerNodeDefCreator.java |
diffstat | 5 files changed, 138 insertions(+), 94 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/extractor/XmlValueExtractor.java Fri Apr 27 13:00:56 2018 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/http/extractor/XmlValueExtractor.java Fri Apr 27 13:34:15 2018 +0200 @@ -1,27 +1,23 @@ package com.passus.st.client.http.extractor; import com.passus.commons.Assert; -import java.io.StringReader; -import java.io.StringWriter; -import java.util.Objects; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.transform.OutputKeys; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; -import javax.xml.xpath.XPath; -import javax.xml.xpath.XPathConstants; -import javax.xml.xpath.XPathExpression; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.xpath.XPathFactory; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.xml.sax.InputSource; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import javax.xml.xpath.*; +import java.io.StringReader; +import java.io.StringWriter; +import java.util.Objects; + /** * * @author Mirosław Hawrot
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilter.java Fri Apr 27 13:00:56 2018 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilter.java Fri Apr 27 13:34:15 2018 +0200 @@ -8,9 +8,11 @@ import com.passus.config.schema.KeyNameVaryListNodeDefinition; import com.passus.config.schema.NodeDefinition; import com.passus.config.schema.NodeDefinitionCreator; -import com.passus.lookup.filter.LookupValueExtractorDefinitionCreator; -import com.passus.net.http.*; +import com.passus.net.http.HttpRequest; +import com.passus.net.http.HttpResponse; import com.passus.st.client.http.HttpFlowContext; +import com.passus.st.client.http.filter.HttpMessageModificationOperations.Operation; +import com.passus.st.config.FieldValueExtractorTransformerNodeDefCreator; import com.passus.st.config.HeaderOperationNodeDefinition; import com.passus.st.plugin.PluginConstants; import com.passus.st.validation.HeaderNameValidator; @@ -22,7 +24,6 @@ import java.util.List; import static com.passus.config.schema.ConfigurationSchemaBuilder.*; -import com.passus.st.client.http.filter.HttpMessageModificationOperations.Operation; /** * @author Mirosław Hawrot @@ -128,26 +129,19 @@ @Override public NodeDefinition create() { - NodeDefinition lookupNodeDef = LookupValueExtractorDefinitionCreator.createNodeDef(); - + NodeDefinition fieldValExtractorNodeDef = FieldValueExtractorTransformerNodeDefCreator.createNodeDef(); NodeDefinition paramNodeDef = new HeaderOperationNodeDefinition( mixedDef( valueDef(), listDef(valueDef()), - mapDef( - tupleDef("$expr", valueDef()).setRequired(false), - tupleDef("$lookup", lookupNodeDef).setRequired(false) - ) + fieldValExtractorNodeDef ) ).addParam(tupleDef("*escape", valueDefBool()).setRequired(false)); HeaderOperationNodeDefinition headerNodeDef = new HeaderOperationNodeDefinition( mixedDef( valueDef(), - mapDef( - tupleDef("$expr", valueDef()).setRequired(false), - tupleDef("$lookup", lookupNodeDef).setRequired(false) - ) + fieldValExtractorNodeDef ) ).addParam(tupleDef("*escape", valueDefBool()).setRequired(false));
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformer.java Fri Apr 27 13:00:56 2018 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpMessageModificationFilterTransformer.java Fri Apr 27 13:34:15 2018 +0200 @@ -9,6 +9,7 @@ import com.passus.st.client.http.extractor.ContentExtractorUtils; import com.passus.st.client.http.extractor.ContentReplacer; import com.passus.st.client.http.filter.HttpMessageModificationOperations.*; +import com.passus.st.config.FieldValueExtractorTransformer; import com.passus.st.filter.Transformers; import java.util.ArrayList; @@ -22,8 +23,11 @@ */ public class HttpMessageModificationFilterTransformer implements NodeTransformer { - private static Operation createNameOperation(CTupleNode tuple, - Errors errors, Class<? extends Operation> clazz) { + private FieldValueExtractorTransformer fieldValueExtractorTransformer = new FieldValueExtractorTransformer(); + + private Operation createNameOperation(CTupleNode tuple, + Class<? extends Operation> clazz, + Errors errors, ConfigurationContext context) { if (validateType(tuple.getNode(), NodeType.VALUE, errors)) { CValueNode valNode = (CValueNode) tuple.getNode(); try { @@ -38,53 +42,10 @@ return null; } - private static ValueExtractor createValueExtractor(CNode node, Errors errors, - ExpressionNodeTransformer expressionNodeTransformer, - LookupValueExtractorTransformer lookupValueTransformer) { - NodeType type = node.getType(); - ValueExtractor valueExtractor = null; - if (type == NodeType.VALUE) { - try { - valueExtractor = expressionNodeTransformer.transform(node); - } catch (Exception e) { - errors.reject("Invalid expression.", node); - } - } else if (type == NodeType.MAP) { - CMapNode mapNode = (CMapNode) node; - if (mapNode.size() != 1) { - errors.reject("Invalid node.", node); - return null; - } - CTupleNode tupleNode = mapNode.getFirstChild(); - String opName = tupleNode.getName(); - if (opName.equals("$expr")) { - try { - valueExtractor = expressionNodeTransformer.transform(node); - } catch (Exception e) { - errors.reject("Invalid expression.", node); - } - } else if (opName.equals("$lookup")) { - CNode valNode = tupleNode.getNode(); - try { - valueExtractor = (ValueExtractor) lookupValueTransformer.transform(valNode); - } catch (NodeConversionException e) { - errors.reject(valNode, "Invalid lookup definition. " + e.getMessage()); - } catch (Exception e) { - errors.reject(valNode, "Invalid lookup definition."); - } - } - } else { - errors.reject("Invalid node.", node); - } - - return valueExtractor; - } - - private static AbstractNameValueOperation createNameValueOperation(CTupleNode nodeTuple, - Errors errors, Class<? extends AbstractNameValueOperation> clazz, - ExpressionNodeTransformer expressionNodeTransformer, - LookupValueExtractorTransformer lookupValueTransformer) { + private AbstractNameValueOperation createNameValueOperation(CTupleNode nodeTuple, + Class<? extends AbstractNameValueOperation> clazz, + Errors errors, ConfigurationContext context) { AbstractNameValueOperation op = null; if (validateType(nodeTuple.getNode(), NodeType.MAP, errors)) { @@ -100,7 +61,7 @@ break; default: CNode valNode = tuple.getNode(); - valueExtractor = createValueExtractor(valNode, errors, expressionNodeTransformer, lookupValueTransformer); + valueExtractor = fieldValueExtractorTransformer.transform(valNode, errors, context); if (valueExtractor == null) { return null; } @@ -128,10 +89,9 @@ return op; } - private static AddParamOperation createAddParamOperation(CTupleNode nodeTuple, - Errors errors, Class<? extends AddParamOperation> clazz, - ExpressionNodeTransformer expressionNodeTransformer, - LookupValueExtractorTransformer lookupValueTransformer) { + private AddParamOperation createAddParamOperation(CTupleNode nodeTuple, + Class<? extends AddParamOperation> clazz, + Errors errors, ConfigurationContext context) { AddParamOperation op = null; if (validateType(nodeTuple.getNode(), NodeType.MAP, errors)) { @@ -148,7 +108,7 @@ default: paramName = tuple.getName(); CNode valNode = tuple.getNode(); - valueExtractor = createValueExtractor(valNode, errors, expressionNodeTransformer, lookupValueTransformer); + valueExtractor = fieldValueExtractorTransformer.transform(valNode, errors, context); if (valueExtractor == null) { return null; } @@ -195,40 +155,40 @@ Operation op = null; switch (opName.toLowerCase()) { case "removeheader": - op = createNameOperation(tuple, errors, RemoveHeaderOperation.class); + op = createNameOperation(tuple, RemoveHeaderOperation.class, errors, context); break; case "addheader": - op = createNameValueOperation(tuple, errors, AddHeaderOperation.class, expressionNodeTransformer, lookupValueTransformer); + op = createNameValueOperation(tuple, AddHeaderOperation.class, errors, context); break; case "setheader": - op = createNameValueOperation(tuple, errors, SetHeaderOperation.class, expressionNodeTransformer, lookupValueTransformer); + op = createNameValueOperation(tuple, SetHeaderOperation.class, errors, context); break; case "removecookie": - op = createNameOperation(tuple, errors, RemoveCookieOperation.class); + op = createNameOperation(tuple, RemoveCookieOperation.class, errors, context); break; case "addcookie": - op = createNameValueOperation(tuple, errors, AddCookieOperation.class, expressionNodeTransformer, lookupValueTransformer); + op = createNameValueOperation(tuple, AddCookieOperation.class, errors, context); break; case "setcookie": - op = createNameValueOperation(tuple, errors, SetCookieOperation.class, expressionNodeTransformer, lookupValueTransformer); + op = createNameValueOperation(tuple, SetCookieOperation.class, errors, context); break; case "removepostparam": - op = createNameOperation(tuple, errors, PostDataRemoveParamOperation.class); + op = createNameOperation(tuple, PostDataRemoveParamOperation.class, errors, context); break; case "addpostparam": - op = createAddParamOperation(tuple, errors, PostDataAddParamOperation.class, expressionNodeTransformer, lookupValueTransformer); + op = createAddParamOperation(tuple, PostDataAddParamOperation.class, errors, context); break; case "setpostparam": - op = createAddParamOperation(tuple, errors, PostDataSetParamOperation.class, expressionNodeTransformer, lookupValueTransformer); + op = createAddParamOperation(tuple, PostDataSetParamOperation.class, errors, context); break; case "removequeryparam": - op = createNameOperation(tuple, errors, RemoveQueryParameterOperation.class); + op = createNameOperation(tuple, RemoveQueryParameterOperation.class, errors, context); break; case "addqueryparam": - op = createNameValueOperation(tuple, errors, AddQueryParameterOperation.class, expressionNodeTransformer, lookupValueTransformer); + op = createNameValueOperation(tuple, AddQueryParameterOperation.class, errors, context); break; case "setqueryparam": - op = createNameValueOperation(tuple, errors, SetQueryParameterOperation.class, expressionNodeTransformer, lookupValueTransformer); + op = createNameValueOperation(tuple, SetQueryParameterOperation.class, errors, context); break; case "setcontent": if (validateType(tuple.getNode(), NodeType.MAP, errors)) {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/config/FieldValueExtractorTransformer.java Fri Apr 27 13:34:15 2018 +0200 @@ -0,0 +1,71 @@ +package com.passus.st.config; + +import com.passus.config.*; +import com.passus.config.validation.Errors; +import com.passus.filter.ValueExtractor; +import com.passus.filter.config.ExpressionNodeTransformer; +import com.passus.filter.config.ValueExtractorTransformer; +import com.passus.lookup.filter.LookupValueExtractorTransformer; +import com.passus.st.filter.Transformers; + +public class FieldValueExtractorTransformer implements ValueExtractorTransformer<ValueExtractor> { + + public static final String[] OPERATORS = {"$expr", "$lookup"}; + + @Override + public String[] getOperators() { + return OPERATORS; + } + + @Override + public ValueExtractor transform(CNode node, Errors errors, ConfigurationContext context) { + final ExpressionNodeTransformer expressionNodeTransformer = Transformers.expressionNodeTransformer(context); + final LookupValueExtractorTransformer lookupValueTransformer = Transformers.lookupValueTransformer(context); + + NodeType type = node.getType(); + ValueExtractor valueExtractor = null; + if (type == NodeType.VALUE) { + try { + valueExtractor = expressionNodeTransformer.transform(node); + } catch (Exception e) { + errors.reject(node, "Invalid expression."); + } + } else if (type == NodeType.MAP) { + CMapNode mapNode = (CMapNode) node; + if (mapNode.size() != 1) { + errors.reject(node, "Invalid node."); + return null; + } + + CTupleNode tupleNode = mapNode.getFirstChild(); + String opName = tupleNode.getName(); + if (opName.equals("$expr")) { + try { + valueExtractor = expressionNodeTransformer.transform(node); + } catch (Exception e) { + errors.reject(node, "Invalid expression."); + } + } else if (opName.equals("$lookup")) { + CNode valNode = tupleNode.getNode(); + try { + valueExtractor = (ValueExtractor) lookupValueTransformer.transform(valNode); + } catch (NodeConversionException e) { + errors.reject(valNode, "Invalid lookup definition. " + e.getMessage()); + } catch (Exception e) { + errors.reject(valNode, "Invalid lookup definition."); + } + } else { + errors.reject(node, "Not supported operator '" + opName + "'."); + } + } else { + errors.reject("Invalid node.", node); + } + + return valueExtractor; + } + + @Override + public CNode reverseTransform(ValueExtractor transformer, Errors errors, ConfigurationContext context) { + return null; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/config/FieldValueExtractorTransformerNodeDefCreator.java Fri Apr 27 13:34:15 2018 +0200 @@ -0,0 +1,23 @@ +package com.passus.st.config; + +import com.passus.config.schema.NodeDefinition; +import com.passus.config.schema.NodeDefinitionCreator; +import com.passus.lookup.filter.LookupValueExtractorDefinitionCreator; + +import static com.passus.config.schema.ConfigurationSchemaBuilder.*; + +public class FieldValueExtractorTransformerNodeDefCreator implements NodeDefinitionCreator { + + public static NodeDefinition createNodeDef() { + NodeDefinition lookupNodeDef = LookupValueExtractorDefinitionCreator.createNodeDef(); + return mapDef( + tupleDef("$expr", valueDef()).setRequired(false), + tupleDef("$lookup", lookupNodeDef).setRequired(false) + ); + } + + @Override + public NodeDefinition create() { + return createNodeDef(); + } +}