use of org.cerberus.util.answer.AnswerItem in project cerberus-source by cerberustesting.
the class UserDAO method readByKey.
@Override
public AnswerItem readByKey(String login) {
AnswerItem ans = new AnswerItem();
User result;
final String query = "SELECT * FROM `user` usr WHERE usr.`login` = ?";
MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
Connection connection = this.databaseSpring.connect();
try {
PreparedStatement preStat = connection.prepareStatement(query);
try {
preStat.setString(1, login);
ResultSet resultSet = preStat.executeQuery();
try {
if (resultSet.first()) {
result = loadFromResultSet(resultSet);
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
ans.setItem(result);
} else {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
}
} catch (SQLException exception) {
LOG.error("Unable to execute query : " + exception.toString());
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
} finally {
resultSet.close();
}
} catch (SQLException exception) {
LOG.error("Unable to execute query : " + exception.toString());
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
} finally {
preStat.close();
}
} catch (SQLException exception) {
LOG.error("Unable to execute query : " + exception.toString());
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException exception) {
LOG.warn("Unable to close connection : " + exception.toString());
}
}
// sets the message
ans.setResultMessage(msg);
return ans;
}
use of org.cerberus.util.answer.AnswerItem in project cerberus-source by cerberustesting.
the class ImportFileService method importAndValidateXMLFromInputStream.
@Override
public AnswerItem importAndValidateXMLFromInputStream(InputStream filecontent, InputStream schemaContent, XMLHandlerEnumType handlerType) {
AnswerItem answer = new AnswerItem();
MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Test Data Library").replace("%OPERATION%", "Import"));
if (schemaContent != null) {
try {
// InputStream data = new BufferedInputStream(filecontent);
Charset charset = StandardCharsets.UTF_8;
String textContent = IOUtils.toString(filecontent, charset);
Source source = new StreamSource(IOUtils.toInputStream(textContent, charset));
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Source sourceschema = new StreamSource(schemaContent);
Schema schema = factory.newSchema(sourceschema);
Validator validator = schema.newValidator();
// is valid
validator.validate(source);
// document is valid, then proceed to load the data
answer.setItem(parseXMLFile(IOUtils.toInputStream(textContent, charset), handlerType));
} catch (SAXException ex) {
LOG.warn("Unable to parse XML: " + ex.toString());
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_IMPORT_ERROR_FORMAT);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Test Data Library").replace("%FORMAT%", "XML"));
} catch (ParserConfigurationException ex) {
LOG.warn("Unable to parse XML: " + ex.toString());
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to parse the XML document. Please try again later."));
} catch (IOException ex) {
LOG.warn("Unable to parse XML: " + ex.toString());
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Unable to verify if the XML document is valid. Please try again later."));
}
}
answer.setResultMessage(msg);
return answer;
}
use of org.cerberus.util.answer.AnswerItem in project cerberus-source by cerberustesting.
the class CampaignParameterService method parseParametersByCampaign.
@Override
public AnswerItem<Map<String, List<String>>> parseParametersByCampaign(final String campaignName) {
final AnswerList<CampaignParameter> campaignParameters = readByCampaign(campaignName);
if (!campaignParameters.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
return new AnswerItem<>(campaignParameters.getResultMessage());
}
final Map<String, List<String>> sortedCampaignParameters = new HashMap<>();
for (final CampaignParameter campaignParameter : campaignParameters.getDataList()) {
if (!sortedCampaignParameters.containsKey(campaignParameter.getParameter())) {
sortedCampaignParameters.put(campaignParameter.getParameter(), new ArrayList<String>());
}
sortedCampaignParameters.get(campaignParameter.getParameter()).add(campaignParameter.getValue());
}
return new AnswerItem<>(sortedCampaignParameters, new MessageEvent(MessageEventEnum.DATA_OPERATION_OK));
}
use of org.cerberus.util.answer.AnswerItem in project cerberus-source by cerberustesting.
the class TagService method createAuto.
@Override
public Answer createAuto(String tagS, String campaign, String user) {
AnswerItem answerTag;
answerTag = readByKey(tagS);
Tag tag = (Tag) answerTag.getItem();
if (tag == null) {
Answer ans = tagDAO.create(factoryTag.create(0, tagS, "", campaign, null, user, null, user, null));
if (!StringUtil.isNullOrEmpty(campaign)) {
emailService.generateAndSendNotifyStartTagExecution(tagS, campaign);
}
return ans;
// If campaign is not empty, we could notify the Start of campaign execution.
} else {
if ((StringUtil.isNullOrEmpty(tag.getCampaign())) && !StringUtil.isNullOrEmpty(campaign)) {
tag.setCampaign(campaign);
return tagDAO.update(tag.getTag(), tag);
}
return null;
}
}
use of org.cerberus.util.answer.AnswerItem in project cerberus-source by cerberustesting.
the class ActionService method doAction.
@Override
public TestCaseStepActionExecution doAction(TestCaseStepActionExecution testCaseStepActionExecution) {
MessageEvent res;
TestCaseExecution tCExecution = testCaseStepActionExecution.getTestCaseStepExecution().gettCExecution();
AnswerItem<String> answerDecode = new AnswerItem();
/**
* Decode the object field before doing the action.
*/
try {
// When starting a new action, we reset the property list that was already calculated.
tCExecution.setRecursiveAlreadyCalculatedPropertiesList(new ArrayList());
answerDecode = variableService.decodeStringCompletly(testCaseStepActionExecution.getValue1(), tCExecution, testCaseStepActionExecution, false);
testCaseStepActionExecution.setValue1((String) answerDecode.getItem());
if (!(answerDecode.isCodeStringEquals("OK"))) {
// If anything wrong with the decode --> we stop here with decode message in the action result.
testCaseStepActionExecution.setActionResultMessage(answerDecode.getResultMessage().resolveDescription("FIELD", "Action Value1"));
testCaseStepActionExecution.setExecutionResultMessage(new MessageGeneral(answerDecode.getResultMessage().getMessage()));
testCaseStepActionExecution.setStopExecution(answerDecode.getResultMessage().isStopTest());
testCaseStepActionExecution.setEnd(new Date().getTime());
LOG.debug("Action interupted due to decode 'Action Value1' Error.");
return testCaseStepActionExecution;
}
} catch (CerberusEventException cex) {
testCaseStepActionExecution.setActionResultMessage(cex.getMessageError());
testCaseStepActionExecution.setExecutionResultMessage(new MessageGeneral(cex.getMessageError().getMessage()));
testCaseStepActionExecution.setEnd(new Date().getTime());
return testCaseStepActionExecution;
}
try {
// When starting a new action, we reset the property list that was already calculated.
tCExecution.setRecursiveAlreadyCalculatedPropertiesList(new ArrayList());
answerDecode = variableService.decodeStringCompletly(testCaseStepActionExecution.getValue2(), tCExecution, testCaseStepActionExecution, false);
testCaseStepActionExecution.setValue2((String) answerDecode.getItem());
if (!(answerDecode.isCodeStringEquals("OK"))) {
// If anything wrong with the decode --> we stop here with decode message in the action result.
testCaseStepActionExecution.setActionResultMessage(answerDecode.getResultMessage().resolveDescription("FIELD", "Action Value2"));
testCaseStepActionExecution.setExecutionResultMessage(new MessageGeneral(answerDecode.getResultMessage().getMessage()));
testCaseStepActionExecution.setStopExecution(answerDecode.getResultMessage().isStopTest());
testCaseStepActionExecution.setEnd(new Date().getTime());
LOG.debug("Action interupted due to decode 'Action Value2' Error.");
return testCaseStepActionExecution;
}
} catch (CerberusEventException cex) {
testCaseStepActionExecution.setActionResultMessage(cex.getMessageError());
testCaseStepActionExecution.setExecutionResultMessage(new MessageGeneral(cex.getMessageError().getMessage()));
testCaseStepActionExecution.setEnd(new Date().getTime());
return testCaseStepActionExecution;
}
/**
* Timestamp starts after the decode. TODO protect when property is
* null.
*/
testCaseStepActionExecution.setStart(new Date().getTime());
String value1 = testCaseStepActionExecution.getValue1();
String value2 = testCaseStepActionExecution.getValue2();
String propertyName = testCaseStepActionExecution.getPropertyName();
LOG.debug("Doing Action : " + testCaseStepActionExecution.getAction() + " with value1 : " + value1 + " and value2 : " + value2);
// When starting a new action, we reset the property list that was already calculated.
tCExecution.setRecursiveAlreadyCalculatedPropertiesList(new ArrayList());
try {
switch(testCaseStepActionExecution.getAction()) {
case TestCaseStepAction.ACTION_CLICK:
res = this.doActionClick(tCExecution, value1, value2);
break;
case TestCaseStepAction.ACTION_MOUSELEFTBUTTONPRESS:
res = this.doActionMouseLeftButtonPress(tCExecution, value1, value2);
break;
case TestCaseStepAction.ACTION_MOUSELEFTBUTTONRELEASE:
res = this.doActionMouseLeftButtonRelease(tCExecution, value1, value2);
break;
case TestCaseStepAction.ACTION_DOUBLECLICK:
res = this.doActionDoubleClick(tCExecution, value1, value2);
break;
case TestCaseStepAction.ACTION_RIGHTCLICK:
res = this.doActionRightClick(tCExecution, value1, value2);
break;
case TestCaseStepAction.ACTION_MOUSEOVER:
res = this.doActionMouseOver(tCExecution, value1, value2);
break;
case TestCaseStepAction.ACTION_FOCUSTOIFRAME:
res = this.doActionFocusToIframe(tCExecution, value1, value2);
break;
case TestCaseStepAction.ACTION_FOCUSDEFAULTIFRAME:
res = this.doActionFocusDefaultIframe(tCExecution);
break;
case TestCaseStepAction.ACTION_SWITCHTOWINDOW:
res = this.doActionSwitchToWindow(tCExecution, value1, value2);
break;
case TestCaseStepAction.ACTION_MANAGEDIALOG:
res = this.doActionManageDialog(tCExecution, value1, value2);
break;
case TestCaseStepAction.ACTION_OPENURLWITHBASE:
res = this.doActionOpenURL(tCExecution, value1, value2, true);
break;
case TestCaseStepAction.ACTION_OPENURLLOGIN:
testCaseStepActionExecution.setValue1(testCaseStepActionExecution.getTestCaseStepExecution().gettCExecution().getCountryEnvironmentParameters().getUrlLogin());
res = this.doActionUrlLogin(tCExecution);
break;
case TestCaseStepAction.ACTION_OPENURL:
res = this.doActionOpenURL(tCExecution, value1, value2, false);
break;
case TestCaseStepAction.ACTION_EXECUTEJS:
res = this.doActionExecuteJS(tCExecution, value1, value2);
break;
case TestCaseStepAction.ACTION_OPENAPP:
res = this.doActionOpenApp(tCExecution, value1);
break;
case TestCaseStepAction.ACTION_CLOSEAPP:
res = this.doActionCloseApp(tCExecution, value1);
break;
case TestCaseStepAction.ACTION_SELECT:
res = this.doActionSelect(tCExecution, value1, value2);
break;
case TestCaseStepAction.ACTION_KEYPRESS:
res = this.doActionKeyPress(tCExecution, value1, value2);
break;
case TestCaseStepAction.ACTION_TYPE:
res = this.doActionType(tCExecution, value1, value2, propertyName);
break;
case TestCaseStepAction.ACTION_HIDEKEYBOARD:
res = this.doActionHideKeyboard(tCExecution);
break;
case TestCaseStepAction.ACTION_SWIPE:
res = this.doActionSwipe(tCExecution, value1, value2);
break;
case TestCaseStepAction.ACTION_WAIT:
res = this.doActionWait(tCExecution, value1, value2);
break;
case TestCaseStepAction.ACTION_WAITVANISH:
res = this.doActionWaitVanish(tCExecution, value1);
break;
case TestCaseStepAction.ACTION_CALLSERVICE:
res = this.doActionCallService(testCaseStepActionExecution, value1);
break;
case TestCaseStepAction.ACTION_EXECUTESQLUPDATE:
res = this.doActionExecuteSQLUpdate(tCExecution, value1, value2);
break;
case TestCaseStepAction.ACTION_EXECUTESQLSTOREPROCEDURE:
res = this.doActionExecuteSQLStoredProcedure(tCExecution, value1, value2);
break;
case TestCaseStepAction.ACTION_CALCULATEPROPERTY:
res = this.doActionCalculateProperty(testCaseStepActionExecution, value1, value2);
break;
case TestCaseStepAction.ACTION_DONOTHING:
res = new MessageEvent(MessageEventEnum.ACTION_SUCCESS);
break;
// DEPRECATED ACTIONS FROM HERE.
case TestCaseStepAction.ACTION_MOUSEOVERANDWAIT:
res = this.doActionMouseOverAndWait(tCExecution, value1, value2);
res.setDescription(MESSAGE_DEPRECATED + " " + res.getDescription());
logEventService.createForPrivateCalls("ENGINE", "mouseOverAndWait", MESSAGE_DEPRECATED + " Deprecated Action triggered by TestCase : ['" + testCaseStepActionExecution.getTest() + "|" + testCaseStepActionExecution.getTestCase() + "']");
LOG.warn(MESSAGE_DEPRECATED + " Deprecated Action mouseOverAndWait triggered by TestCase : ['" + testCaseStepActionExecution.getTest() + "'|'" + testCaseStepActionExecution.getTestCase() + "']");
break;
case TestCaseStepAction.ACTION_REMOVEDIFFERENCE:
res = this.doActionRemoveDifference(testCaseStepActionExecution, value1, value2);
res.setDescription(MESSAGE_DEPRECATED + " " + res.getDescription());
logEventService.createForPrivateCalls("ENGINE", "removeDifference", MESSAGE_DEPRECATED + " Deprecated Action triggered by TestCase : ['" + testCaseStepActionExecution.getTest() + "|" + testCaseStepActionExecution.getTestCase() + "']");
LOG.warn(MESSAGE_DEPRECATED + " Deprecated Action removeDifference triggered by TestCase : ['" + testCaseStepActionExecution.getTest() + "'|'" + testCaseStepActionExecution.getTestCase() + "']");
break;
case TestCaseStepAction.ACTION_GETPAGESOURCE:
res = this.doActionGetPageSource(testCaseStepActionExecution);
res.setDescription(MESSAGE_DEPRECATED + " " + res.getDescription());
logEventService.createForPrivateCalls("ENGINE", "getPageSource", MESSAGE_DEPRECATED + " Deprecated Action triggered by TestCase : ['" + testCaseStepActionExecution.getTest() + "|" + testCaseStepActionExecution.getTestCase() + "']");
LOG.warn(MESSAGE_DEPRECATED + " Deprecated Action getPageSource triggered by TestCase : ['" + testCaseStepActionExecution.getTest() + "'|'" + testCaseStepActionExecution.getTestCase() + "']");
break;
case TestCaseStepAction.ACTION_TAKESCREENSHOT:
res = this.doActionTakeScreenshot(testCaseStepActionExecution);
res.setDescription(MESSAGE_DEPRECATED + " " + res.getDescription());
logEventService.createForPrivateCalls("ENGINE", "takeScreenshot", MESSAGE_DEPRECATED + " Deprecated Action triggered by TestCase : ['" + testCaseStepActionExecution.getTest() + "|" + testCaseStepActionExecution.getTestCase() + "']");
LOG.warn(MESSAGE_DEPRECATED + " Deprecated Action takeScreenshot triggered by TestCase : ['" + testCaseStepActionExecution.getTest() + "'|'" + testCaseStepActionExecution.getTestCase() + "']");
break;
case TestCaseStepAction.ACTION_CLICKANDWAIT:
res = this.doActionClickWait(tCExecution, value1, value2);
res.setDescription(MESSAGE_DEPRECATED + " " + res.getDescription());
logEventService.createForPrivateCalls("ENGINE", "clickAndWait", MESSAGE_DEPRECATED + " Deprecated Action triggered by TestCase : ['" + testCaseStepActionExecution.getTest() + "|" + testCaseStepActionExecution.getTestCase() + "']");
LOG.warn(MESSAGE_DEPRECATED + " Deprecated Action clickAndWait triggered by TestCase : ['" + testCaseStepActionExecution.getTest() + "'|'" + testCaseStepActionExecution.getTestCase() + "']");
break;
case TestCaseStepAction.ACTION_ENTER:
res = this.doActionKeyPress(tCExecution, value1, "RETURN");
res.setDescription(MESSAGE_DEPRECATED + " " + res.getDescription());
logEventService.createForPrivateCalls("ENGINE", "enter", MESSAGE_DEPRECATED + " Deprecated Action triggered by TestCase : ['" + testCaseStepActionExecution.getTest() + "|" + testCaseStepActionExecution.getTestCase() + "']");
LOG.warn(MESSAGE_DEPRECATED + " Deprecated Action enter triggered by TestCase : ['" + testCaseStepActionExecution.getTest() + "'|'" + testCaseStepActionExecution.getTestCase() + "']");
break;
case TestCaseStepAction.ACTION_SELECTANDWAIT:
res = this.doActionSelect(tCExecution, value1, value2);
this.doActionWait(tCExecution, StringUtil.NULL, StringUtil.NULL);
res.setDescription(MESSAGE_DEPRECATED + " " + res.getDescription());
logEventService.createForPrivateCalls("ENGINE", "selectAndWait", MESSAGE_DEPRECATED + " Deprecated Action triggered by TestCase : ['" + testCaseStepActionExecution.getTest() + "|" + testCaseStepActionExecution.getTestCase() + "']");
LOG.warn(MESSAGE_DEPRECATED + " Deprecated Action selectAndWait triggered by TestCase : ['" + testCaseStepActionExecution.getTest() + "'|'" + testCaseStepActionExecution.getTestCase() + "']");
break;
default:
res = new MessageEvent(MessageEventEnum.ACTION_FAILED_UNKNOWNACTION);
res.setDescription(res.getDescription().replace("%ACTION%", testCaseStepActionExecution.getAction()));
}
} catch (final Exception unexpected) {
LOG.error("Unexpected exception: " + unexpected.getMessage(), unexpected);
res = new MessageEvent(MessageEventEnum.ACTION_FAILED_GENERIC).resolveDescription("DETAIL", unexpected.getMessage());
}
LOG.debug("Result of the action : " + res.getCodeString() + " " + res.getDescription());
/**
* In case 1/ the action is flaged as being Forced with a specific
* return code = PE and 2/ the return of the action is stoping the test
* --> whatever the return of the action is, we force the return to move
* forward the test with no screenshot, pagesource.
*/
if (testCaseStepActionExecution.getForceExeStatus().equals("PE") && res.isStopTest()) {
res.setDescription(res.getDescription() + " -- Execution forced to continue.");
res.setDoScreenshot(false);
res.setGetPageSource(false);
res.setStopTest(false);
res.setMessage(MessageGeneralEnum.EXECUTION_PE_TESTEXECUTING);
}
testCaseStepActionExecution.setActionResultMessage(res);
/**
* Determine here the impact of the Action on the full test return code
* from the ResultMessage of the Action.
*/
testCaseStepActionExecution.setExecutionResultMessage(new MessageGeneral(res.getMessage()));
/**
* Determine here if we stop the test from the ResultMessage of the
* Action.
*/
testCaseStepActionExecution.setStopExecution(res.isStopTest());
testCaseStepActionExecution.setEnd(new Date().getTime());
return testCaseStepActionExecution;
}
Aggregations