use of org.apache.synapse.unittest.testcase.data.classes.AssertNotNull in project wso2-synapse by wso2.
the class SynapseTestcaseDataReader method readAndStoreTestCaseData.
/**
* Read test-case data from the descriptor data.
* Append test-case data into the test data holder object
*
* @return testCaseDataHolder object with test case data
*/
TestCaseData readAndStoreTestCaseData() {
TestCaseData testCaseDataHolder = new TestCaseData();
// Set test case count as zero
int testCasesCount = 0;
// Read test cases from descriptor data
QName qualifiedTestCases = new QName("", TEST_CASES, "");
OMElement testCasesNode = importXMLFile.getFirstChildWithName(qualifiedTestCases);
// Iterate through test-cases in descriptor data
Iterator<?> testCaseIterator = Collections.emptyIterator();
if (testCasesNode != null) {
testCaseIterator = testCasesNode.getChildElements();
}
while (testCaseIterator.hasNext()) {
TestCase testCase = new TestCase();
OMElement testCaseNode = (OMElement) (testCaseIterator.next());
String testCaseName = testCaseNode.getAttributeValue(new QName(NAME_ATTRIBUTE));
testCase.setTestCaseName(testCaseName);
// Read input child from test-case node
QName qualifiedInput = new QName("", TEST_CASE_INPUT, "");
OMElement testCaseInputNode = testCaseNode.getFirstChildWithName(qualifiedInput);
// Read input node data of payload and properties if not null
if (testCaseInputNode != null) {
readTestCaseInputData(testCaseInputNode, testCase);
}
// Read assertions of test-case node
QName qualifiedAssertions = new QName("", TEST_CASE_ASSERTIONS, "");
OMElement testCaseAssertionNode = testCaseNode.getFirstChildWithName(qualifiedAssertions);
ArrayList<AssertEqual> assertEquals = new ArrayList<>();
ArrayList<AssertNotNull> assertNotNulls = new ArrayList<>();
readTestCaseAssertions(testCaseAssertionNode, assertEquals, assertNotNulls);
// set assertion values in testCase object
testCase.setAssertEquals(assertEquals);
testCase.setAssertNotNull(assertNotNulls);
// set testCase object in testCase data holder
testCaseDataHolder.setTestCases(testCase);
testCasesCount++;
}
// Set test case count in test data holder
testCaseDataHolder.setTestCaseCount(testCasesCount);
log.info("Test case data from descriptor data read successfully");
return testCaseDataHolder;
}
use of org.apache.synapse.unittest.testcase.data.classes.AssertNotNull in project wso2-synapse by wso2.
the class SynapseTestcaseDataReader method readTestCaseAssertions.
/**
* Read test case assertion data from the descriptor data.
* Read assertEquals and assertNotNull data if exists
*
* @param testCaseAssertionNode node of test case assertions
* @param assertEquals array of assertEquals
* @param assertNotNulls array of assertNotNulls
*/
private void readTestCaseAssertions(OMElement testCaseAssertionNode, ArrayList<AssertEqual> assertEquals, ArrayList<AssertNotNull> assertNotNulls) {
// Read assertions - AssertEquals of test-case node
Iterator<?> assertEqualsIterator = testCaseAssertionNode.getChildrenWithName(new QName(TEST_CASE_ASSERTION_EQUALS));
while (assertEqualsIterator.hasNext()) {
AssertEqual assertion = new AssertEqual();
OMElement assertEqualNode = (OMElement) (assertEqualsIterator.next());
QName qualifiedAssertActual = new QName("", ASSERTION_ACTUAL, "");
OMElement assertActualNode = assertEqualNode.getFirstChildWithName(qualifiedAssertActual);
String actual = assertActualNode.getText();
assertion.setActual(actual);
QName qualifiedAssertMessage = new QName("", ASSERTION_MESSAGE, "");
OMElement assertMessageNode = assertEqualNode.getFirstChildWithName(qualifiedAssertMessage);
String message = assertMessageNode.getText();
assertion.setMessage(message);
QName qualifiedExpectedMessage = new QName("", ASSERTION_EXPECTED, "");
OMElement assertExpectedNode = assertEqualNode.getFirstChildWithName(qualifiedExpectedMessage);
String expected = assertExpectedNode.getText();
assertion.setExpected(expected);
assertEquals.add(assertion);
}
// Read assertions - AssertNotNull of test-case node
Iterator<?> assertNotNullIterator = testCaseAssertionNode.getChildrenWithName(new QName(TEST_CASE_ASSERTION_NOTNULL));
while (assertNotNullIterator.hasNext()) {
AssertNotNull assertion = new AssertNotNull();
OMElement assertEqualNode = (OMElement) (assertNotNullIterator.next());
QName qualifiedAssertActual = new QName("", ASSERTION_ACTUAL, "");
OMElement assertActualNode = assertEqualNode.getFirstChildWithName(qualifiedAssertActual);
String actual = assertActualNode.getText();
assertion.setActual(actual);
QName qualifiedAssertMessage = new QName("", ASSERTION_MESSAGE, "");
OMElement assertMessageNode = assertEqualNode.getFirstChildWithName(qualifiedAssertMessage);
String message = assertMessageNode.getText();
assertion.setMessage(message);
assertNotNulls.add(assertion);
}
}
use of org.apache.synapse.unittest.testcase.data.classes.AssertNotNull in project wso2-synapse by wso2.
the class Assertor method startAssertNotNullsForServices.
/**
* Method of assertNotNull for Service test cases.
*
* @param assertNotNull array of assertNotNull
* @param response service's http response
* @param testCaseSummary testSummary object for this test case
*/
private static void startAssertNotNullsForServices(List<AssertNotNull> assertNotNull, Map.Entry<String, HttpResponse> response, TestCaseSummary testCaseSummary) {
log.info("Assert Not Null - assert property for services started");
for (AssertNotNull assertItem : assertNotNull) {
TestCaseAssertionSummary testAssertion = new TestCaseAssertionSummary();
Assertor.AssertionPrerequisite.setAssertNotNullPrerequisite(assertItem);
boolean isAssertNull = true;
HttpResponse serviceResponse = response.getValue();
String mediatedResult = Constants.STRING_NULL;
switch(Assertor.AssertionPrerequisite.getExpressionPrefix()) {
case INPUT_PROPERTY_BODY:
isAssertNull = serviceResponse.getEntity() == null;
break;
case RESPONSE_PROPERTY_STATUS_CODE:
isAssertNull = serviceResponse.getStatusLine() == null;
break;
case RESPONSE_PROPERTY_HTTP_VERSION:
if (serviceResponse.getStatusLine() != null) {
isAssertNull = serviceResponse.getStatusLine().getProtocolVersion() == null;
}
break;
case INPUT_PROPERTY_TRANSPORT:
Header[] responseHeaders = serviceResponse.getAllHeaders();
if (responseHeaders != null) {
for (Header header : responseHeaders) {
if (header.getName().equals(Assertor.AssertionPrerequisite.getExpressionProperty())) {
isAssertNull = header.getValue() == null;
break;
}
}
}
break;
default:
mediatedResult = "Received assert expression: " + Assertor.AssertionPrerequisite.getAssertExpression() + " is not a valid operation type for services";
log.error(mediatedResult);
}
log.info("Service Assert Actual - " + Assertor.AssertionPrerequisite.getAssertExpression());
log.info("Service mediated result for assertNotNull is - " + !isAssertNull);
if (!isAssertNull) {
log.info("Service assertNotNull for " + Assertor.AssertionPrerequisite.getAssertExpression() + " expression passed successfully");
} else {
testAssertion.setAssertionType(Constants.TEST_CASE_ASSERTION_NOTNULL);
testAssertion.setAssertionExpression(Assertor.AssertionPrerequisite.getAssertExpression());
testAssertion.setAssertionActualValue(mediatedResult);
testAssertion.setAssertionDescription("Tested service url - " + response.getKey() + "\nRecieved status code - " + (serviceResponse.getStatusLine() != null ? serviceResponse.getStatusLine().getStatusCode() : "null"));
testAssertion.setAssertionErrorMessage(Assertor.AssertionPrerequisite.getMessage());
testCaseSummary.addTestCaseAssertion(testAssertion);
log.error("Service assertNotNull for " + Assertor.AssertionPrerequisite.getAssertExpression() + " expression failed with a message - " + Assertor.AssertionPrerequisite.getMessage());
}
}
}
use of org.apache.synapse.unittest.testcase.data.classes.AssertNotNull in project wso2-synapse by wso2.
the class Assertor method startAssertNotNullsForSequence.
/**
* Method of assertionNotNull for Sequence test cases.
*
* @param assertNotNull array of assertNotNull
* @param messageContext message context
* @param testCaseSummary testSummary object for this test case
*/
private static void startAssertNotNullsForSequence(List<AssertNotNull> assertNotNull, MessageContext messageContext, TestCaseSummary testCaseSummary) {
log.info("Assert Not Null - assert property for sequences started");
for (AssertNotNull assertItem : assertNotNull) {
TestCaseAssertionSummary testAssertion = new TestCaseAssertionSummary();
Assertor.AssertionPrerequisite.setAssertNotNullPrerequisite(assertItem);
boolean isAssertNull = true;
String mediatedResult = Constants.STRING_NULL;
Axis2MessageContext axis2MessageContext = (Axis2MessageContext) messageContext;
org.apache.axis2.context.MessageContext axis2MessageCtx = axis2MessageContext.getAxis2MessageContext();
switch(Assertor.AssertionPrerequisite.getExpressionPrefix()) {
case INPUT_PROPERTY_BODY:
isAssertNull = messageContext.getEnvelope().getBody().getFirstElement() == null;
break;
case INPUT_PROPERTY_CONTEXT:
isAssertNull = messageContext.getProperty(Assertor.AssertionPrerequisite.getExpressionProperty()) == null;
break;
case INPUT_PROPERTY_AXIS2:
isAssertNull = axis2MessageCtx.getProperty(Assertor.AssertionPrerequisite.getExpressionProperty()) == null;
break;
case INPUT_PROPERTY_TRANSPORT:
Object headers = axis2MessageCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
@SuppressWarnings("unchecked") Map<String, Object> headersMap = (Map) headers;
isAssertNull = headersMap.get(Assertor.AssertionPrerequisite.getExpressionProperty()) == null;
break;
default:
mediatedResult = "Received assert expression: " + Assertor.AssertionPrerequisite.getAssertExpression() + " is not a valid operation type for sequences";
log.error(mediatedResult);
}
log.info("Sequence Assertion Expression - " + Assertor.AssertionPrerequisite.getAssertExpression());
log.info("Sequence mediated result for assertNotNull is " + !isAssertNull);
if (!isAssertNull) {
log.info("Sequence assertNotNull for " + Assertor.AssertionPrerequisite.getAssertExpression() + " expression passed successfully");
} else {
testAssertion.setAssertionType(Constants.TEST_CASE_ASSERTION_NOTNULL);
testAssertion.setAssertionExpression(Assertor.AssertionPrerequisite.getAssertExpression());
testAssertion.setAssertionActualValue(mediatedResult);
testAssertion.setAssertionErrorMessage(Assertor.AssertionPrerequisite.getMessage());
testCaseSummary.addTestCaseAssertion(testAssertion);
log.error("Sequence assertNotNull for " + Assertor.AssertionPrerequisite.getAssertExpression() + " expression failed with a message - " + Assertor.AssertionPrerequisite.getMessage());
}
}
}
Aggregations