use of com.axway.ats.agent.core.templateactions.exceptions.XmlUtilitiesException in project ats-framework by Axway.
the class HeaderMatcher method performMatch.
@Override
public boolean performMatch(Object expectedObject, Object actualObject) {
String actualText = (String) actualObject;
markProcessed();
boolean actualResult = false;
if (actualText != null) {
if (headerName.equalsIgnoreCase(SET_COOKIE_HEADER_NAME)) {
Matcher matcher = COOKIE_VALUE_PATTERN.matcher(actualText);
while (matcher.find()) {
String cookieName = matcher.group(1);
if (cookieName != null) {
// see the COOKIE_VALUE_PATTERN JavaDoc
String cookieValue = matcher.group(2);
if (cookieValue == null) {
cookieValue = matcher.group(3);
}
if (!COOKIE_ATTRIBUTE_NAMES_TO_SKIP.contains(cookieName.toLowerCase())) {
log.info("New cookie '" + cookieName + "' with value '" + cookieValue + "'");
ThreadContext.setAttribute(ThreadContext.COOKIE_VAR_PREFFIX + cookieName, cookieValue);
actualResult = true;
}
} else {
log.warn("The '" + SET_COOKIE_HEADER_NAME + "' header has unexpected format '" + actualText + "'");
}
}
} else {
int actualHeaderNumericValue;
switch(matchMode) {
case EQUALS:
try {
String expectedResult = headerValueToMatch;
if (headerValueToMatch != null && headerValueToMatch.contains("${")) {
expectedResult = XmlUtilities.applyUserParameters(headerValueToMatch);
}
actualResult = actualText.equals(expectedResult);
} catch (XmlUtilitiesException e) {
log.error("Can not apply user parameters", e);
}
break;
case CONTAINS:
actualResult = actualText.contains(headerValueToMatch);
break;
case RANGE:
try {
actualHeaderNumericValue = Integer.parseInt(actualText);
actualResult = minValue <= actualHeaderNumericValue && actualHeaderNumericValue <= maxValue;
} catch (NumberFormatException nfe) {
log.warn("The value '" + actualText + "' of header '" + headerName + "' is not a numeric value");
}
break;
case RANGE_OFFSET:
int baseValue = Integer.parseInt(headerValueToMatch);
if (baseValue >= 0) {
try {
actualHeaderNumericValue = Integer.parseInt(actualText);
actualResult = (baseValue - offsetValue <= actualHeaderNumericValue) && (actualHeaderNumericValue <= baseValue + offsetValue);
} catch (NumberFormatException nfe) {
log.warn("The value '" + actualText + "' of header '" + headerName + "' is not a numeric value");
}
} else {
log.warn("Can not execute a range offset verification as the base value must be greater or equal 0, but it is '" + baseValue + "'. " + toString());
}
break;
case LIST:
for (String listValue : listValues) {
if (listValue.equals(actualText)) {
actualResult = true;
break;
}
}
break;
case REGEX:
actualResult = Pattern.compile(headerValueToMatch).matcher(actualText).find();
break;
case RANDOM:
actualResult = true;
break;
case EXTRACT:
try {
actualResult = extractUserParameter("response header", headerValueToMatch, actualText);
} catch (XmlUtilitiesException e) {
log.error("Can not extract user parameter", e);
}
break;
}
}
if (log.isDebugEnabled()) {
if (actualResult) {
log.debug("Matched the value '" + actualText + "' for " + toString());
} else {
log.debug("Did not match the value '" + actualText + "' for " + toString());
}
}
} else {
log.error("Header '" + headerName + "' not found. Needed for " + toString());
}
return actualResult;
}
use of com.axway.ats.agent.core.templateactions.exceptions.XmlUtilitiesException 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.exceptions.XmlUtilitiesException in project ats-framework by Axway.
the class XmlUtilities method stringToXmlDocumentObj.
/**
* @param xmlString xml string
* @return xml Document object
* @throws XmlUtilitiesException
*/
public static Document stringToXmlDocumentObj(String xmlString) throws XmlUtilitiesException {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
return db.parse(new ByteArrayInputStream(xmlString.getBytes()));
} catch (Exception e) {
throw new XmlUtilitiesException("Error transforming String to XML document", e);
}
}
use of com.axway.ats.agent.core.templateactions.exceptions.XmlUtilitiesException in project ats-framework by Axway.
the class XmlUtilities method applyUserParameters.
/**
* Applies all requested by the user changes on the provided string.
* All user parameters are expected in the form ${user_param_name}
*
* @param stringToModify the string to modify
* @return the modified string
* @throws XmlUtilitiesException
*/
public static String applyUserParameters(String stringToModify) throws XmlUtilitiesException {
StringBuilder sb = new StringBuilder(stringToModify);
int paramStartIndex = -1;
for (String key : ThreadContext.getAttributeNames()) {
String paramName = "${" + key + "}";
paramStartIndex = sb.indexOf(paramName);
if (paramStartIndex > -1) {
Object paramValue = ThreadContext.getAttribute(key);
if (paramValue instanceof Queue<?>) {
Queue<?> valuesQueue = (Queue<?>) paramValue;
while ((paramStartIndex = sb.indexOf(paramName, paramStartIndex)) > -1) {
// replace the first occurrence of the parameter and remove its value from the Queue
Object value = valuesQueue.poll();
if (value == null) {
throw new XmlUtilitiesException("The number of parameters " + paramName + " is more than the number of provided values for them.");
}
sb.replace(paramStartIndex, paramStartIndex + paramName.length(), value.toString());
}
} else {
while ((paramStartIndex = sb.indexOf(paramName, paramStartIndex)) > -1) {
sb.replace(paramStartIndex, paramStartIndex + paramName.length(), paramValue.toString());
}
}
}
}
while ((paramStartIndex = sb.indexOf("${", paramStartIndex)) > -1) {
if (sb.indexOf("}", paramStartIndex) > -1 && sb.charAt(paramStartIndex + 2) != '=') {
// TODO - change to warn after detailed review. Currently it seems this method is applied before new parameters extraction
log.info("Currently there is no value to replace parameter " + sb.substring(paramStartIndex, sb.indexOf("}", paramStartIndex) + 1) + ". Increase HttpClient's logging severity to 'TRACE' in order to see the current request data.");
}
paramStartIndex++;
}
return sb.toString();
}
use of com.axway.ats.agent.core.templateactions.exceptions.XmlUtilitiesException in project ats-framework by Axway.
the class XmlUtilities method verifyResponseFile.
private void verifyResponseFile(ActionResponseObject expectedHttpResponseNode, Node actualHttpResponseNode) throws XmlUtilitiesException {
String expectedResponseFile = expectedHttpResponseNode.getResourceFile();
Node actualResponseFileNode = getFirstChildNode(actualHttpResponseNode, TOKEN_HTTP_RESOURCE_FILE);
if (expectedResponseFile == null && actualResponseFileNode == null) {
// no file is expected and no file was received
} else if (expectedResponseFile != null && actualResponseFileNode != null) {
if (matchFilesBySize || matchFilesByContent) /* pre-check before MD5 sum */
{
String expectedFileSize = expectedHttpResponseNode.getResourceFileSize();
String actualFileSize = getNodeAttribute(actualResponseFileNode, "size");
if ((actualFileSize == null && expectedFileSize != null) || (actualFileSize != null && expectedFileSize == null) || (actualFileSize != null && !actualFileSize.equals(expectedFileSize))) {
throw new XmlUtilitiesException("The expected response file '" + expectedResponseFile + "' has the length of " + expectedFileSize + " while the actual has the length of " + actualFileSize);
}
}
if (matchFilesByContent) {
// compare both files using MD5 sums
String actualResponseFile = expectedResponseFile.substring(0, expectedResponseFile.lastIndexOf(AtsSystemProperties.SYSTEM_FILE_SEPARATOR)) + AtsSystemProperties.SYSTEM_FILE_SEPARATOR + "actual" + AtsSystemProperties.SYSTEM_FILE_SEPARATOR + Thread.currentThread().getName() + AtsSystemProperties.SYSTEM_FILE_SEPARATOR + actualResponseFileNode.getTextContent();
String expectedFileMD5Sum;
String actualFileMD5Sum;
LocalFileSystemOperations localFileOperations = new LocalFileSystemOperations();
try {
expectedFileMD5Sum = localFileOperations.computeMd5Sum(expectedResponseFile, Md5SumMode.BINARY);
} catch (Exception e) {
throw new XmlUtilitiesException("Error calculating MD5 sum for " + expectedResponseFile, e);
}
try {
actualFileMD5Sum = localFileOperations.computeMd5Sum(actualResponseFile, Md5SumMode.BINARY);
} catch (Exception e) {
throw new XmlUtilitiesException("Error calculating MD5 sum for " + actualResponseFile, e);
}
if (!expectedFileMD5Sum.equalsIgnoreCase(actualFileMD5Sum)) {
throw new XmlUtilitiesException("The expected response file '" + expectedResponseFile + "' is not the same as the actual '" + actualResponseFile + "'");
}
}
} else {
throw new XmlUtilitiesException("Expected to " + (expectedResponseFile != null ? "receive the " + expectedResponseFile + " file" : "not receive a response file") + ", but " + (actualResponseFileNode != null ? "received the " + actualResponseFileNode.getTextContent() + " file" : "did not receive a response file"));
}
}
Aggregations