use of org.cerberus.crud.entity.TestCaseLabel in project cerberus-source by cerberustesting.
the class CreateTestCase method processRequest.
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, CerberusException, JSONException {
JSONObject jsonResponse = new JSONObject();
Answer ans = new Answer();
MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
ans.setResultMessage(msg);
PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
response.setContentType("application/json");
// Calling Servlet Transversal Util.
ServletUtil.servletStart(request);
/**
* Parsing and securing all required parameters.
*/
String test = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("test"), "");
String testcase = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("testCase"), "");
String originalTest = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("originalTest"), "");
String originalTestCase = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("originalTestCase"), "");
// Prepare the final answer.
MessageEvent msg1 = new MessageEvent(MessageEventEnum.GENERIC_OK);
Answer finalAnswer = new Answer(msg1);
/**
* Checking all constrains before calling the services.
*/
if (StringUtil.isNullOrEmpty(test) && StringUtil.isNullOrEmpty(testcase)) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Test Case").replace("%OPERATION%", "Create").replace("%REASON%", "mandatory fields (test or testcase) are missing."));
finalAnswer.setResultMessage(msg);
} else {
/**
* All data seems cleans so we can call the services.
*/
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
testCaseService = appContext.getBean(ITestCaseService.class);
testCaseLabelService = appContext.getBean(ITestCaseLabelService.class);
testCaseLabelFactory = appContext.getBean(IFactoryTestCaseLabel.class);
testCaseCountryService = appContext.getBean(ITestCaseCountryService.class);
testCaseCountryFactory = appContext.getBean(IFactoryTestCaseCountry.class);
testCaseCountryPropertiesService = appContext.getBean(ITestCaseCountryPropertiesService.class);
testCaseStepService = appContext.getBean(ITestCaseStepService.class);
testCaseStepActionService = appContext.getBean(ITestCaseStepActionService.class);
testCaseStepActionControlService = appContext.getBean(ITestCaseStepActionControlService.class);
TestCase testCaseData = getTestCaseFromRequest(request);
ans = testCaseService.create(testCaseData);
finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
/**
* Object created. Adding Log entry.
*/
ILogEventService logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/CreateTestCase", "CREATE", "Create TestCase : ['" + testcase + "']", request);
// Update labels
if (request.getParameter("labelList") != null) {
JSONArray objLabelArray = new JSONArray(request.getParameter("labelList"));
List<TestCaseLabel> labelList = new ArrayList();
labelList = getLabelListFromRequest(request, appContext, test, testcase, objLabelArray);
// Update the Database with the new list.
ans = testCaseLabelService.compareListAndUpdateInsertDeleteElements(test, testcase, labelList);
finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
}
// Update Countries
if (request.getParameter("countryList") != null) {
JSONArray objCountryArray = new JSONArray(request.getParameter("countryList"));
List<TestCaseCountry> tccList = new ArrayList();
tccList = getCountryListFromRequest(request, appContext, test, testcase, objCountryArray);
// Update the Database with the new list.
ans = testCaseCountryService.compareListAndUpdateInsertDeleteElements(test, testcase, tccList);
finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
// Duplicate other objects.
List<TestCaseCountryProperties> tccpList = new ArrayList();
List<TestCaseCountryProperties> newTccpList = new ArrayList();
if (!tccList.isEmpty() && ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
tccpList = testCaseCountryPropertiesService.findListOfPropertyPerTestTestCase(originalTest, originalTestCase);
// Build a new list with the countries that exist for the testcase.
for (TestCaseCountryProperties curTccp : tccpList) {
if (testCaseCountryService.exist(test, testcase, curTccp.getCountry())) {
newTccpList.add(curTccp);
}
}
if (!newTccpList.isEmpty()) {
ans = testCaseCountryPropertiesService.duplicateList(newTccpList, test, testcase);
finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
}
}
}
List<TestCaseStep> tcsList = new ArrayList();
if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
tcsList = testCaseStepService.getListOfSteps(originalTest, originalTestCase);
if (!tcsList.isEmpty()) {
ans = testCaseStepService.duplicateList(tcsList, test, testcase);
finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
}
}
List<TestCaseStepAction> tcsaList = new ArrayList();
if (!tcsList.isEmpty() && ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
tcsaList = testCaseStepActionService.findTestCaseStepActionbyTestTestCase(originalTest, originalTestCase);
if (!tcsaList.isEmpty()) {
ans = testCaseStepActionService.duplicateList(tcsaList, test, testcase);
finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
}
}
if (!tcsList.isEmpty() && !tcsaList.isEmpty() && ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
List<TestCaseStepActionControl> tcsacList = testCaseStepActionControlService.findControlByTestTestCase(originalTest, originalTestCase);
if (!tcsacList.isEmpty()) {
ans = testCaseStepActionControlService.duplicateList(tcsacList, test, testcase);
finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
}
}
}
}
/**
* Formating and returning the json result.
*/
jsonResponse.put("messageType", finalAnswer.getResultMessage().getMessage().getCodeString());
jsonResponse.put("message", finalAnswer.getResultMessage().getDescription());
response.getWriter().print(jsonResponse);
response.getWriter().flush();
}
use of org.cerberus.crud.entity.TestCaseLabel in project cerberus-source by cerberustesting.
the class CreateTestCase method getLabelListFromRequest.
private List<TestCaseLabel> getLabelListFromRequest(HttpServletRequest request, ApplicationContext appContext, String test, String testCase, JSONArray json) throws CerberusException, JSONException, UnsupportedEncodingException {
List<TestCaseLabel> labelList = new ArrayList();
for (int i = 0; i < json.length(); i++) {
JSONObject objectJson = json.getJSONObject(i);
// Parameter that are already controled by GUI (no need to decode) --> We SECURE them
boolean delete = objectJson.getBoolean("toDelete");
Integer labelId = objectJson.getInt("labelId");
Timestamp creationDate = new Timestamp(new Date().getTime());
if (!delete) {
labelList.add(testCaseLabelFactory.create(0, test, testCase, labelId, request.getRemoteUser(), creationDate, request.getRemoteUser(), creationDate, null));
}
}
return labelList;
}
use of org.cerberus.crud.entity.TestCaseLabel in project cerberus-source by cerberustesting.
the class CreateTestCaseLabel method processRequest.
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, CerberusException, JSONException {
JSONObject jsonResponse = new JSONObject();
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
ILogEventService logEventService = appContext.getBean(LogEventService.class);
Answer ans = new Answer();
MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
ans.setResultMessage(msg);
PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
String charset = request.getCharacterEncoding();
response.setContentType("application/json");
// Calling Servlet Transversal Util.
ServletUtil.servletStart(request);
/**
* Parsing and securing all required parameters.
*/
// Parameter that are already controled by GUI (no need to decode) --> We SECURE them
// Parameter that needs to be secured --> We SECURE+DECODE them
// Parameter that we cannot secure as we need the html --> We DECODE them
Integer myIdInt = 0;
String[] myLabelIdList = request.getParameterValues("labelid");
String[] myTestList = request.getParameterValues("test");
String[] myTestCaseList = request.getParameterValues("testcase");
if ((myTestList.length == 0) || (myTestCaseList.length == 0) || (myLabelIdList.length == 0)) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Create").replace("%REASON%", "Missing Parameter (either test, testcase or labelid)."));
ans.setResultMessage(msg);
} else if (myTestList.length != myTestCaseList.length) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Create").replace("%REASON%", "Number of Test does not match number of testcase."));
ans.setResultMessage(msg);
}
StringBuilder output_message = new StringBuilder();
int massErrorCounter = 0;
for (int i = 0; i < myLabelIdList.length; i++) {
String myLabelId = myLabelIdList[i];
myIdInt = 0;
boolean label_error = true;
try {
if (myLabelId != null && !myLabelId.equals("")) {
myIdInt = Integer.valueOf(policy.sanitize(myLabelId));
label_error = false;
}
} catch (Exception ex) {
label_error = true;
}
/**
* Checking all constrains before calling the services.
*/
if (label_error) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Update").replace("%REASON%", "Could not manage to convert labelid to an integer value or labelid is missing."));
ans.setResultMessage(msg);
massErrorCounter++;
output_message.append("<br>id : ").append(myLabelId).append(" - ").append(msg.getDescription());
} else {
/**
* All data seems cleans so we can call the services.
*/
ILabelService labelService = appContext.getBean(ILabelService.class);
IFactoryTestCaseLabel factoryTestCaseLabel = appContext.getBean(IFactoryTestCaseLabel.class);
ITestCaseLabelService testCaseLabelService = appContext.getBean(ITestCaseLabelService.class);
ITestCaseService testCaseService = appContext.getBean(ITestCaseService.class);
IApplicationService applicationService = appContext.getBean(IApplicationService.class);
AnswerItem resp = labelService.readByKey(myIdInt);
if (!(resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.getItem() != null)) {
/**
* Object could not be found. We stop here and report the
* error.
*/
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Create").replace("%REASON%", "Label does not exist."));
ans.setResultMessage(msg);
massErrorCounter++;
output_message.append("<br>labelid : ").append(myLabelId).append(" - ").append(msg.getDescription());
} else {
Label myLab = (Label) resp.getItem();
for (int j = 0; j < myTestList.length; j++) {
String myTest = myTestList[j];
String myTestCase = myTestCaseList[j];
resp = testCaseService.readByKey(myTest, myTestCase);
if (!(resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.getItem() != null)) {
/**
* Object could not be found. We stop here and
* report the error.
*/
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Create").replace("%REASON%", "Test Case does not exist."));
ans.setResultMessage(msg);
massErrorCounter++;
output_message.append("<br>testcase : ").append(myLabelId).append(" - ").append(msg.getDescription());
} else {
TestCase myTestCaseObj = (TestCase) resp.getItem();
resp = applicationService.readByKey(myTestCaseObj.getApplication());
if ((resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.getItem() != null)) {
Application myApplication = (Application) resp.getItem();
if ((StringUtil.isNullOrEmpty(myLab.getSystem())) || (myApplication.getSystem().equals(myLab.getSystem()))) {
TestCaseLabel tcLabel = factoryTestCaseLabel.create(0, myTest, myTestCase, myIdInt, request.getRemoteUser(), null, "", null, null);
ans = testCaseLabelService.create(tcLabel);
if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
/**
* Update was successful. Adding Log
* entry.
*/
logEventService.createForPrivateCalls("/CreateTestCaseLabel", "CREATE", "Created TestCaseLabel : ['" + myIdInt + "'|'" + myTest + "'|'" + myTestCase + "']", request);
} else {
massErrorCounter++;
output_message.append("<br>Label : ").append(myLabelId).append(" Test : '").append(myTest).append("' TestCase : '").append(myTestCase).append("' - ").append(ans.getResultMessage().getDescription());
}
} else {
massErrorCounter++;
output_message.append("<br>Label : ").append(myLabelId).append(" Test : '").append(myTest).append("' TestCase : '").append(myTestCase).append("' - ").append("Label does not belong to the same system as TestCase system.");
}
}
}
}
}
}
}
if (myTestList.length > 1) {
if (massErrorCounter == myTestList.length) {
// All updates are in ERROR.
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Mass Update").replace("%REASON%", massErrorCounter + " label links(s) out of " + (myTestList.length * myLabelIdList.length) + " failed to be created due to an issue.<br>") + output_message.toString());
ans.setResultMessage(msg);
} else if (massErrorCounter > 0) {
// At least 1 update in error
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_WARNING);
msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Mass Update").replace("%REASON%", massErrorCounter + " label links(s) out of " + (myTestList.length * myLabelIdList.length) + " failed to be created due to an issue.<br>") + output_message.toString());
ans.setResultMessage(msg);
} else {
// No error detected.
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Mass Update") + "\n\nAll " + (myTestList.length * myLabelIdList.length) + " label links(s) created successfuly.");
ans.setResultMessage(msg);
}
logEventService.createForPrivateCalls("/CreateTestCaseLabel", "MASSUPDATE", msg.getDescription(), request);
}
/**
* Formating and returning the json result.
*/
jsonResponse.put("messageType", ans.getResultMessage().getMessage().getCodeString());
jsonResponse.put("message", ans.getResultMessage().getDescription());
response.getWriter().print(jsonResponse);
response.getWriter().flush();
}
use of org.cerberus.crud.entity.TestCaseLabel in project cerberus-source by cerberustesting.
the class ReadTestCase method findTestCaseByTestTestCase.
private AnswerItem findTestCaseByTestTestCase(String test, String testCase, ApplicationContext appContext, HttpServletRequest request) throws JSONException {
AnswerItem item = new AnswerItem();
JSONObject object = new JSONObject();
testCaseService = appContext.getBean(ITestCaseService.class);
testCaseCountryService = appContext.getBean(ITestCaseCountryService.class);
testCaseLabelService = appContext.getBean(ITestCaseLabelService.class);
// finds the project
AnswerItem answerTestCase = testCaseService.readByKey(test, testCase);
if (answerTestCase.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && answerTestCase.getItem() != null) {
// if the service returns an OK message then we can get the item and convert it to JSONformat
TestCase tc = (TestCase) answerTestCase.getItem();
JSONObject response = convertToJSONObject(tc);
// Country List feed.
JSONArray countryArray = new JSONArray();
AnswerList answerTestCaseCountryList = testCaseCountryService.readByTestTestCase(null, test, testCase);
for (TestCaseCountry country : (List<TestCaseCountry>) answerTestCaseCountryList.getDataList()) {
countryArray.put(convertToJSONObject(country));
}
response.put("countryList", countryArray);
// Label List feed.
JSONArray labelArray = new JSONArray();
AnswerList answerTestCaseLabelList = testCaseLabelService.readByTestTestCase(test, testCase);
for (TestCaseLabel label : (List<TestCaseLabel>) answerTestCaseLabelList.getDataList()) {
labelArray.put(convertToJSONObject(label));
}
response.put("labelList", labelArray);
object.put("contentTable", response);
object.put("hasPermissionsUpdate", testCaseService.hasPermissionsUpdate(tc, request));
}
item.setItem(object);
item.setResultMessage(answerTestCase.getResultMessage());
return item;
}
use of org.cerberus.crud.entity.TestCaseLabel in project cerberus-source by cerberustesting.
the class ReadTestCase method findTestCaseByTest.
// </editor-fold>
private AnswerItem findTestCaseByTest(String system, String test, ApplicationContext appContext, HttpServletRequest request) throws JSONException {
AnswerItem answer = new AnswerItem();
JSONObject object = new JSONObject();
testCaseService = appContext.getBean(ITestCaseService.class);
testCaseCountryService = appContext.getBean(ITestCaseCountryService.class);
testCaseLabelService = appContext.getBean(ITestCaseLabelService.class);
int startPosition = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayStart"), "0"));
int length = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayLength"), "0"));
String searchParameter = ParameterParserUtil.parseStringParam(request.getParameter("sSearch"), "");
String sColumns = ParameterParserUtil.parseStringParam(request.getParameter("sColumns"), "tec.test,tec.testcase,tec.application,project,ticket,description,behaviororvalueexpected,readonly,bugtrackernewurl,deploytype,mavengroupid");
String[] columnToSort = sColumns.split(",");
List<String> individualLike = new ArrayList(Arrays.asList(ParameterParserUtil.parseStringParam(request.getParameter("sLike"), "").split(",")));
// Get Sorting information
int numberOfColumnToSort = Integer.parseInt(ParameterParserUtil.parseStringParam(request.getParameter("iSortingCols"), "1"));
int columnToSortParameter = 0;
String sort = "asc";
StringBuilder sortInformation = new StringBuilder();
for (int c = 0; c < numberOfColumnToSort; c++) {
columnToSortParameter = Integer.parseInt(ParameterParserUtil.parseStringParam(request.getParameter("iSortCol_" + c), "0"));
sort = ParameterParserUtil.parseStringParam(request.getParameter("sSortDir_" + c), "asc");
String columnName = columnToSort[columnToSortParameter];
sortInformation.append(columnName).append(" ").append(sort);
if (c != numberOfColumnToSort - 1) {
sortInformation.append(" , ");
}
}
Map<String, List<String>> individualSearch = new HashMap<String, List<String>>();
for (int a = 0; a < columnToSort.length; a++) {
if (null != request.getParameter("sSearch_" + a) && !request.getParameter("sSearch_" + a).isEmpty()) {
List<String> search = new ArrayList(Arrays.asList(request.getParameter("sSearch_" + a).split(",")));
if (individualLike.contains(columnToSort[a])) {
individualSearch.put(columnToSort[a] + ":like", search);
} else {
individualSearch.put(columnToSort[a], search);
}
}
}
AnswerList testCaseList = testCaseService.readByTestByCriteria(system, test, startPosition, length, sortInformation.toString(), searchParameter, individualSearch);
AnswerList testCaseCountryList = testCaseCountryService.readByTestTestCase(system, test, null);
/**
* Find the list of labels
*/
AnswerList testCaseLabelList = testCaseLabelService.readByTestTestCase(test, null);
LinkedHashMap<String, JSONObject> testCaseWithCountry = new LinkedHashMap();
for (TestCaseCountry country : (List<TestCaseCountry>) testCaseCountryList.getDataList()) {
String key = country.getTest() + "_" + country.getTestCase();
if (testCaseWithCountry.containsKey(key)) {
testCaseWithCountry.get(key).put(country.getCountry(), country.getCountry());
} else {
testCaseWithCountry.put(key, new JSONObject().put(country.getCountry(), country.getCountry()));
}
}
/**
* Iterate on the label retrieved and generate HashMap based on the key
* Test_TestCase
*/
LinkedHashMap<String, JSONArray> testCaseWithLabel = new LinkedHashMap();
for (TestCaseLabel label : (List<TestCaseLabel>) testCaseLabelList.getDataList()) {
String key = label.getTest() + "_" + label.getTestcase();
if (testCaseWithLabel.containsKey(key)) {
JSONObject jo = new JSONObject().put("name", label.getLabel().getLabel()).put("color", label.getLabel().getColor()).put("description", label.getLabel().getDescription());
testCaseWithLabel.get(key).put(jo);
} else {
JSONObject jo = new JSONObject().put("name", label.getLabel().getLabel()).put("color", label.getLabel().getColor()).put("description", label.getLabel().getDescription());
testCaseWithLabel.put(key, new JSONArray().put(jo));
}
}
JSONArray jsonArray = new JSONArray();
if (testCaseList.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
// the service was able to perform the query, then we should get all values
for (TestCase testCase : (List<TestCase>) testCaseList.getDataList()) {
String key = testCase.getTest() + "_" + testCase.getTestCase();
JSONObject value = convertToJSONObject(testCase);
value.put("hasPermissionsDelete", testCaseService.hasPermissionsDelete(testCase, request));
value.put("hasPermissionsUpdate", testCaseService.hasPermissionsUpdate(testCase, request));
value.put("hasPermissionsCreate", testCaseService.hasPermissionsCreate(testCase, request));
value.put("countryList", testCaseWithCountry.get(key));
value.put("labels", testCaseWithLabel.get(key));
jsonArray.put(value);
}
}
// object.put("hasPermissions", testCaseService.hasPermissions(request));
object.put("hasPermissionsCreate", testCaseService.hasPermissionsCreate(null, request));
object.put("contentTable", jsonArray);
object.put("iTotalRecords", testCaseList.getTotalRows());
object.put("iTotalDisplayRecords", testCaseList.getTotalRows());
answer.setItem(object);
answer.setResultMessage(testCaseList.getResultMessage());
return answer;
}
Aggregations