use of com.axway.ats.agent.core.templateactions.model.matchers.XPathBodyMatcher in project ats-framework by Axway.
the class AbstractActionObject method resolveXpathEntries.
// TODO: use Deque (non-synchronized) impl. instead of Stack
protected void resolveXpathEntries(Node node, Stack<String> nodeXpath, Map<String, Integer> allNodesIndexMap) throws DOMException, InvalidMatcherException {
String name = node.getNodeName();
// calculate the XPath for this node
String nodeXpathString = name;
if (nodeXpath.size() > 0) {
nodeXpathString = nodeXpath.peek().toString() + "/" + nodeXpathString;
}
// calculate the index for this node
Integer nodeIndex = allNodesIndexMap.get(nodeXpathString);
if (nodeIndex == null) {
nodeIndex = 1;
} else {
nodeIndex = nodeIndex + 1;
}
allNodesIndexMap.put(nodeXpathString, nodeIndex);
// calculate the XPath for this node including its index
String nodeXpathWithIndexString = nodeXpathString + "[" + nodeIndex + "]";
nodeXpath.push(nodeXpathWithIndexString);
NodeList nodeList = node.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node currentNode = nodeList.item(i);
if (currentNode != null && currentNode.getNodeType() == Node.TEXT_NODE) {
String nodeValue = currentNode.getNodeValue();
if (nodeValue != null) {
nodeValue = nodeValue.trim();
if (nodeValue.contains("${")) {
// this node contains a user parameter we are interested in
String xpath = "//" + nodeXpath.peek();
TemplateBodyNodeMatchMode matchMode;
if (nodeValue.contains("${=")) {
// retriever
matchMode = TemplateBodyNodeMatchMode.EXTRACT;
} else {
// matcher
if (nodeValue.startsWith("${CONTAINS=")) {
nodeValue = nodeValue.substring("${CONTAINS=".length(), nodeValue.length() - 1);
matchMode = TemplateBodyNodeMatchMode.CONTAINS;
} else if (nodeValue.startsWith("${EQUALS=")) {
nodeValue = nodeValue.substring("${EQUALS=".length(), nodeValue.length() - 1);
matchMode = TemplateBodyNodeMatchMode.EQUALS;
} else if (nodeValue.startsWith("${RANGE=")) {
nodeValue = nodeValue.substring("${RANGE=".length(), nodeValue.length() - 1);
matchMode = TemplateBodyNodeMatchMode.RANGE;
} else if (nodeValue.startsWith("${LIST=")) {
nodeValue = nodeValue.substring("${LIST=".length(), nodeValue.length() - 1);
matchMode = TemplateBodyNodeMatchMode.LIST;
} else if (nodeValue.startsWith("${REGEX=")) {
nodeValue = nodeValue.substring("${REGEX=".length(), nodeValue.length() - 1);
matchMode = TemplateBodyNodeMatchMode.REGEX;
} else {
// ignore user parameters e.g. ${targetHost}
continue;
}
}
XPathBodyMatcher xPathMatcher = new XPathBodyMatcher(xpath, nodeValue, matchMode);
log.debug("Extraceted XPath matcher: " + xPathMatcher.toString());
xpathBodyMatchers.add(xPathMatcher);
}
}
} else {
// a regular node, search its children
resolveXpathEntries(currentNode, nodeXpath, allNodesIndexMap);
}
}
nodeXpath.pop();
}
use of com.axway.ats.agent.core.templateactions.model.matchers.XPathBodyMatcher in project ats-framework by Axway.
the class XmlUtilities method verifyResponse.
/**
* Verify the expected and actual responses match
*
* @param expectedHttpResponseObject
* @param currentActionRequestNumber current request/response step in this action/xml (starts from 1)
* @throws Exception
*/
public void verifyResponse(String actionsXml, String actionName, int currentActionRequestNumber, ActionResponseObject expectedHttpResponseObject, HttpClient httpClient, TemplateActionsResponseVerificationConfigurator responseVerificationConfigurator) throws Exception {
ActionParser actualHttpResponse = readActionResponse(httpClient, actionsXml, currentActionRequestNumber, false);
if (HttpClient.log.isTraceEnabled()) {
String causeMsg = "Print response for debugging purposes";
logActualResponse(causeMsg, actionName, currentActionRequestNumber, actualHttpResponse, false);
}
// stepMatchers now are after reading response so not to influence StopWatches
// list with all body matchers
List<ResponseMatcher> stepMatchers = new ArrayList<ResponseMatcher>();
// add all matchers from the XML file
List<XPathBodyMatcher> xpathBodyMatchers = applyUserParameters(expectedHttpResponseObject.getXpathBodyMatchers());
stepMatchers.addAll(xpathBodyMatchers);
// add all matchers from the test case
TemplateActionResponseVerificator responseVerificator = responseVerificationConfigurator.getActionVerificator(actionName);
if (responseVerificator != null) {
// there is a verificator for this action
stepMatchers.addAll(responseVerificator.getStepBodyMatchers(currentActionRequestNumber));
}
try {
// Compare HTTP response code
String expectedResponseResult = expectedHttpResponseObject.getResponseResult();
String actualResponseResult = getFirstChildNode(actualHttpResponse.getActionNodeWithoutBody(), TOKEN_HTTP_RESPONSE_RESULT).getTextContent();
if (!expectedResponseResult.equalsIgnoreCase(actualResponseResult)) {
String causeMsg = "Expected response result '" + expectedResponseResult + "' is different than the actual '" + actualResponseResult + "'.";
logActualResponse(causeMsg, actionName, currentActionRequestNumber, actualHttpResponse, true);
throw new XmlUtilitiesException(causeMsg);
}
// Compare response headers. It extracts any user parameters if present in the headers.
verifyResponseHeaders(actionName, currentActionRequestNumber, expectedHttpResponseObject.getHttpHeaderMatchers(), actualHttpResponse, responseVerificationConfigurator);
// Compare response files
verifyResponseFile(expectedHttpResponseObject, actualHttpResponse.getActionNodeWithoutBody());
if (!stepMatchers.isEmpty()) {
// TODO verify the response body here
}
} finally {
actualHttpResponse.cleanupMembers();
}
log.info(actionName + "[" + currentActionRequestNumber + "] -> " + "Verified HTTP response");
}
use of com.axway.ats.agent.core.templateactions.model.matchers.XPathBodyMatcher in project ats-framework by Axway.
the class TemplateLoadClient method checkBodyNode.
/**
* Verify the value of a specified response body node
*
* @param actionName the name of the action
* @param stepNumber the action step number
* @param nodeXPath XPath specifying the node
* @param valueToMatch the expected value
* @param matchMode the match mode
*
* @throws InvalidMatcherException
*/
@PublicAtsApi
public void checkBodyNode(String actionName, int stepNumber, String nodeXPath, String valueToMatch, TemplateBodyNodeMatchMode matchMode) throws InvalidMatcherException {
List<ResponseMatcher> stepMatchers = getStepBodyMatchers(actionName, stepNumber);
stepMatchers.add(new XPathBodyMatcher(nodeXPath, valueToMatch, matchMode));
}
use of com.axway.ats.agent.core.templateactions.model.matchers.XPathBodyMatcher in project ats-framework by Axway.
the class XmlUtilities method applyUserParameters.
/**
* Applies the user parameters, but does not modify the original (as they come from the XML file)
* matchers
*
* @param staticXpathBodyMatchers
* @return
* @throws InvalidMatcherException
*/
private List<XPathBodyMatcher> applyUserParameters(List<XPathBodyMatcher> staticXpathBodyMatchers) throws InvalidMatcherException {
List<XPathBodyMatcher> actualXpathBodyMatchers = new ArrayList<XPathBodyMatcher>();
for (XPathBodyMatcher matcher : staticXpathBodyMatchers) {
String matcherValue = matcher.getMatcherValue();
String attributeName = (String) ThreadContext.getAttribute(matcherValue);
if (attributeName != null) {
matcherValue = ThreadContext.getAttribute(attributeName).toString();
}
actualXpathBodyMatchers.add(new XPathBodyMatcher(matcher.getXpath(), matcherValue, matcher.getMatchMode()));
}
return actualXpathBodyMatchers;
}
Aggregations