use of org.cerberus.crud.factory.IFactoryTestCaseCountry in project cerberus-source by cerberustesting.
the class CreateTestCaseCountry 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
* @throws org.cerberus.exception.CerberusException
* @throws org.json.JSONException
*/
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);
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
String test = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("test"), "", charset);
String testcase = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("testCase"), null, charset);
String country = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("country"), "", charset);
String usrCreated = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("usrCreated"), "", charset);
// Parameter that we cannot secure as we need the html --> We DECODE them
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
/**
* Checking all constrains before calling the services.
*/
if ((StringUtil.isNullOrEmpty(testcase)) || (StringUtil.isNullOrEmpty(test)) || (StringUtil.isNullOrEmpty(country))) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCaseCountry").replace("%OPERATION%", "Create").replace("%REASON%", "test or testcase or country is missing!"));
ans.setResultMessage(msg);
} else {
// Checking the autorities here.
ITestCaseService testCaseService = appContext.getBean(ITestCaseService.class);
AnswerItem resp = testCaseService.readByKey(test, testcase);
TestCase tc = (TestCase) resp.getItem();
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%", "TestCaseCountry").replace("%OPERATION%", "Create").replace("%REASON%", "TestCase does not exist."));
ans.setResultMessage(msg);
} else if (!request.isUserInRole("Test")) {
// We cannot update the testcase if the user is not at least in Test role.
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCaseCountry").replace("%OPERATION%", "Create").replace("%REASON%", "Not enought privilege to create the testCaseCountry. You must belong to Test Privilege."));
ans.setResultMessage(msg);
} else if ((tc.getStatus().equalsIgnoreCase("WORKING")) && !(request.isUserInRole("TestAdmin"))) {
// If Test Case is WORKING we need TestAdmin priviliges.
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCaseCountry").replace("%OPERATION%", "Create").replace("%REASON%", "Not enought privilege to create the testCaseCountry. The test case is in WORKING status and needs TestAdmin privilege to be updated"));
ans.setResultMessage(msg);
} else {
/**
* All data seems cleans so we can call the services.
*/
ITestCaseCountryService testCaseCountryService = appContext.getBean(ITestCaseCountryService.class);
IFactoryTestCaseCountry factoryTestCaseCountry = appContext.getBean(IFactoryTestCaseCountry.class);
TestCaseCountry testCaseCountryData = factoryTestCaseCountry.create(test, testcase, country);
ans = testCaseCountryService.create(testCaseCountryData);
if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
/**
* Object created. Adding Log entry.
*/
ILogEventService logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/CreateTestCaseCountry", "CREATE", "Create TestCaseCountry : ['" + test + "'|'" + testcase + "'|'" + country + "']", 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.factory.IFactoryTestCaseCountry in project cerberus-source by cerberustesting.
the class UpdateTestCaseWithDependencies method getTestCaseCountryFromParameter.
private List<TestCaseCountry> getTestCaseCountryFromParameter(HttpServletRequest request, ApplicationContext appContext, String test, String testCase) {
IFactoryTestCaseCountry testCaseCountryFactory = appContext.getBean(IFactoryTestCaseCountry.class);
List<TestCaseCountry> countries = new ArrayList<TestCaseCountry>();
if (request.getParameterValues("editTestCaseCountry") != null) {
for (String country : request.getParameterValues("editTestCaseCountry")) {
countries.add(testCaseCountryFactory.create(test, testCase, country));
}
}
return countries;
}
use of org.cerberus.crud.factory.IFactoryTestCaseCountry in project cerberus-source by cerberustesting.
the class DuplicateTestCase method getCountryList.
private List<TestCaseCountry> getCountryList(String targetTest, String targetTestCase, HttpServletRequest request) throws CerberusException, JSONException, UnsupportedEncodingException {
List<TestCaseCountry> countryList = new ArrayList();
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IInvariantService invariantService = appContext.getBean(InvariantService.class);
IFactoryTestCaseCountry testCaseCountryFactory = appContext.getBean(IFactoryTestCaseCountry.class);
// TODO: handle if the response does not turn ok
AnswerList answer = invariantService.readByIdname("COUNTRY");
for (Invariant country : (List<Invariant>) answer.getDataList()) {
String countrySelected = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter(country.getValue()), "");
if ("".equals(countrySelected)) {
countryList.add(testCaseCountryFactory.create(targetTest, targetTestCase, country.getValue()));
}
}
return countryList;
}
Aggregations