use of org.cerberus.crud.service.IInvariantService in project cerberus-source by cerberustesting.
the class DeleteInvariant 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);
String charset = request.getCharacterEncoding();
response.setContentType("application/json");
String id = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("idName"), "", charset);
String value = request.getParameter("value");
boolean userHasPermissions = request.isUserInRole("Administrator");
/**
* Checking all constrains before calling the services.
*/
if (StringUtil.isNullOrEmpty(id)) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "Delete").replace("%REASON%", "Invariant name is missing!"));
ans.setResultMessage(msg);
} else if (!userHasPermissions) {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "Delete").replace("%REASON%", "You don't have the right to do that"));
ans.setResultMessage(msg);
} else {
/**
* All data seems cleans so we can call the services.
*/
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IInvariantService invariantService = appContext.getBean(IInvariantService.class);
Invariant invariantData = invariantService.convert(invariantService.readByKey(id, value));
if (invariantService.hasPermissionsDelete(invariantData, request)) {
ans = invariantService.delete(invariantData);
if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
/**
* Object updated. Adding Log entry.
*/
ILogEventService logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/DeleteInvariant2", "DELETE", "Delete Invariant : ['" + id + "']", request);
}
} else {
msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "Delete").replace("%REASON%", "You don't have the right to do that."));
ans.setResultMessage(msg);
}
}
/**
* Formating and returning the json result.
*/
jsonResponse.put("messageType", ans.getResultMessage().getMessage().getCodeString());
jsonResponse.put("message", ans.getResultMessage().getDescription());
response.getWriter().print(jsonResponse.toString());
response.getWriter().flush();
}
use of org.cerberus.crud.service.IInvariantService in project cerberus-source by cerberustesting.
the class FindInvariantByID 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 {
PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
String idName = policy.sanitize(request.getParameter("idName"));
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
response.setContentType("application/json");
response.setCharacterEncoding("utf8");
// Calling Servlet Transversal Util.
ServletUtil.servletStart(request);
IInvariantService invariantService = appContext.getBean(InvariantService.class);
JSONArray array = new JSONArray();
// TODO: handle if the response does not turn ok
AnswerList answer = invariantService.readByIdname(idName);
for (Invariant myInvariant : (List<Invariant>) answer.getDataList()) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("value", myInvariant.getValue());
jsonObject.put("description", myInvariant.getDescription());
array.put(jsonObject);
}
response.getWriter().print(array.toString());
}
use of org.cerberus.crud.service.IInvariantService in project cerberus-source by cerberustesting.
the class GetInvariantList method doPost.
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("idName");
String idName = ParameterParserUtil.parseStringParam(id, "");
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IInvariantService invariantService = appContext.getBean(InvariantService.class);
JSONObject jsonResponse = new JSONObject();
String action = request.getParameter("action");
try {
if (request.getParameter("action") != null) {
// retrieve all the information in one client call
if ("getNInvariant".equals(action)) {
// gets a list of invariants
JSONObject listOfInvariants = new JSONObject(idName);
for (int i = 0; i < listOfInvariants.length(); i++) {
String invariantName = (String) listOfInvariants.get(String.valueOf(i));
JSONArray array = new JSONArray();
// TODO: handle if the response does not turn ok
AnswerList answer = invariantService.readByIdname(invariantName);
for (Invariant myInvariant : (List<Invariant>) answer.getDataList()) {
array.put(myInvariant.getValue());
}
jsonResponse.put(invariantName, array);
}
}
} else {
// gets one item
// TODO: handle if the response does not turn ok
AnswerList answer = invariantService.readByIdname(idName);
for (Invariant myInvariant : (List<Invariant>) answer.getDataList()) {
jsonResponse.put(myInvariant.getValue(), myInvariant.getValue());
}
}
response.setContentType("application/json");
response.getWriter().print(jsonResponse.toString());
} catch (JSONException e) {
LOG.warn(e);
response.setContentType("text/html");
response.getWriter().print(e.getMessage());
}
}
use of org.cerberus.crud.service.IInvariantService in project cerberus-source by cerberustesting.
the class UpdateTestCaseWithDependencies 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
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, CerberusException {
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
String initialTest = request.getParameter("informationInitialTest");
String initialTestCase = request.getParameter("informationInitialTestCase");
String test = request.getParameter("informationTest");
String testCase = request.getParameter("informationTestCase");
TestCase tc = getTestCaseFromParameter(request, appContext, test, testCase);
boolean duplicate = false;
ITestService tService = appContext.getBean(ITestService.class);
ITestCaseService tcService = appContext.getBean(ITestCaseService.class);
ITestCaseCountryService tccService = appContext.getBean(ITestCaseCountryService.class);
ITestCaseCountryPropertiesService tccpService = appContext.getBean(ITestCaseCountryPropertiesService.class);
ITestCaseStepService tcsService = appContext.getBean(ITestCaseStepService.class);
ITestCaseStepActionService tcsaService = appContext.getBean(ITestCaseStepActionService.class);
ITestCaseStepActionControlService tcsacService = appContext.getBean(ITestCaseStepActionControlService.class);
IInvariantService invariantService = appContext.getBean(IInvariantService.class);
IUserService userService = appContext.getBean(IUserService.class);
IUserGroupService userGroupService = appContext.getBean(IUserGroupService.class);
/**
* Get User and Groups of this user
*/
User user = userService.findUserByKey(request.getUserPrincipal().getName());
// List<UserGroup> userGroupList = groupService.findGroupByUser(user);
List<UserGroup> userGroupList = userGroupService.convert(userGroupService.readByUser(user.getLogin()));
List<String> groupList = new ArrayList();
for (UserGroup group : userGroupList) {
groupList.add(group.getGroup());
}
/**
* Verify the Test is the same than initialTest If it is the same > Do
* nothing If it is not the same > Verify if test already exists If not
* exist > create it If exist > do nothing
*/
if (!tc.getTest().equals(initialTest)) {
if (tService.findTestByKey(tc.getTest()) == null) {
if (groupList.contains("TestAdmin")) {
Test newTest = tService.findTestByKey(initialTest);
newTest.setTest(tc.getTest());
tService.convert(tService.create(newTest));
} else {
response.sendError(403, MessageGeneralEnum.GUI_TEST_CREATION_NOT_HAVE_RIGHT.getDescription());
return;
}
}
}
if (!tc.getTest().equals(initialTest) || !tc.getTestCase().equals(initialTestCase)) {
duplicate = true;
}
/**
* If the testcase is a duplication, set the creator as the one which
* duplicate the testcase and the status in the initial one.
*/
if (duplicate) {
tc.setUsrCreated(user.getLogin());
// TODO: handle if the response does not turn ok
AnswerList answer = invariantService.readByIdname("TCSTATUS");
tc.setStatus(((List<Invariant>) answer.getDataList()).get(0).getValue());
}
/**
* If not duplicate and test in Working status and user with no admin
* right, raise an error
*/
if (!duplicate && "WORKING".equals(tc.getStatus()) && !groupList.contains("TestAdmin")) {
response.sendError(403, MessageGeneralEnum.GUI_TESTCASE_NON_ADMIN_SAVE_WORKING_TESTCASE.getDescription());
return;
}
/**
* Verify testcase is the same than initialTestCase If it is the same >
* update If it is not the same, > verify if testcase already exist If
* it already exist > Send Error If it do not already exists > Create it
*/
if (!duplicate) {
tcService.updateTestCase(tc);
} else if (tcService.findTestCaseByKey(tc.getTest(), tc.getTestCase()) != null) {
response.sendError(403, MessageGeneralEnum.GUI_TESTCASE_DUPLICATION_ALREADY_EXISTS.getDescription());
return;
} else {
tcService.createTestCase(tc);
}
/**
* For the list of testcase country verify it exists. If it does not
* exists > create it If it exist, verify if it's the
*/
List<TestCaseCountry> tccFromPage = getTestCaseCountryFromParameter(request, appContext, test, testCase);
List<TestCaseCountry> tccFromDtb = tccService.findTestCaseCountryByTestTestCase(initialTest, initialTestCase);
/**
* Iterate on (TestCaseCountry From Page - TestCaseCountry From
* Database) If TestCaseCountry in Database has same key : Update and
* remove from the list. If TestCaseCountry in database does ot exist :
* Insert it.
*/
List<TestCaseCountry> tccToUpdateOrInsert = new ArrayList(tccFromPage);
tccToUpdateOrInsert.removeAll(tccFromDtb);
List<TestCaseCountry> tccToUpdateOrInsertToIterate = new ArrayList(tccToUpdateOrInsert);
for (TestCaseCountry tccDifference : tccToUpdateOrInsertToIterate) {
for (TestCaseCountry tccInDatabase : tccFromDtb) {
if (tccDifference.hasSameKey(tccInDatabase)) {
tccToUpdateOrInsert.remove(tccDifference);
}
}
}
tccService.insertListTestCaseCountry(tccToUpdateOrInsert);
/**
* Iterate on (TestCaseCountry From Database - TestCaseCountry From
* Page). If TestCaseCountry in Page has same key : remove from the
* list. Then delete the list of TestCaseCountry
*/
if (!duplicate) {
List<TestCaseCountry> tccToDelete = new ArrayList(tccFromDtb);
tccToDelete.removeAll(tccFromPage);
List<TestCaseCountry> tccToDeleteToIterate = new ArrayList(tccToDelete);
for (TestCaseCountry tccDifference : tccToDeleteToIterate) {
for (TestCaseCountry tccInPage : tccFromPage) {
if (tccDifference.hasSameKey(tccInPage)) {
tccToDelete.remove(tccDifference);
}
}
}
tccService.deleteListTestCaseCountry(tccToDelete);
}
/**
* For the list of testcase country verify it exists. If it does not
* exists > create it If it exist, verify if it's the
*/
List<TestCaseCountryProperties> tccpFromPage = getTestCaseCountryPropertiesFromParameter(request, appContext, test, testCase);
List<TestCaseCountryProperties> tccpFromDtb = tccpService.findListOfPropertyPerTestTestCase(initialTest, initialTestCase);
/**
* Iterate on (TestCaseCountryProperties From Page -
* TestCaseCountryProperties From Database) If TestCaseCountryProperties
* in Database has same key : Update and remove from the list. If
* TestCaseCountryProperties in database does ot exist : Insert it.
*/
List<TestCaseCountryProperties> tccpToUpdateOrInsert = new ArrayList(tccpFromPage);
tccpToUpdateOrInsert.removeAll(tccpFromDtb);
List<TestCaseCountryProperties> tccpToUpdateOrInsertToIterate = new ArrayList(tccpToUpdateOrInsert);
for (TestCaseCountryProperties tccpDifference : tccpToUpdateOrInsertToIterate) {
for (TestCaseCountryProperties tccpInDatabase : tccpFromDtb) {
if (tccpDifference.hasSameKey(tccpInDatabase)) {
tccpService.updateTestCaseCountryProperties(tccpDifference);
tccpToUpdateOrInsert.remove(tccpDifference);
}
}
}
tccpService.insertListTestCaseCountryProperties(tccpToUpdateOrInsert);
/**
* Iterate on (TestCaseCountryProperties From Database -
* TestCaseCountryProperties From Page). If TestCaseCountryProperties in
* Page has same key : remove from the list. Then delete the list of
* TestCaseCountryProperties
*/
if (!duplicate) {
List<TestCaseCountryProperties> tccpToDelete = new ArrayList(tccpFromDtb);
tccpToDelete.removeAll(tccpFromPage);
List<TestCaseCountryProperties> tccpToDeleteToIterate = new ArrayList(tccpToDelete);
for (TestCaseCountryProperties tccpDifference : tccpToDeleteToIterate) {
for (TestCaseCountryProperties tccpInPage : tccpFromPage) {
if (tccpDifference.hasSameKey(tccpInPage)) {
tccpToDelete.remove(tccpDifference);
}
}
}
tccpService.deleteListTestCaseCountryProperties(tccpToDelete);
}
/*
* Get steps, actions and controls from page by:
* - generating a new step, action or control number,
* - setting the correct related step and action for action or control
*/
List<TestCaseStep> tcsFromPage = getTestCaseStepFromParameter(request, appContext, test, testCase, duplicate);
List<TestCaseStepAction> tcsaFromPage = new ArrayList();
List<TestCaseStepActionControl> tcsacFromPage = new ArrayList();
int nextStepNumber = getMaxStepNumber(tcsFromPage);
for (TestCaseStep tcs : tcsFromPage) {
if (tcs.getStep() == -1) {
tcs.setStep(++nextStepNumber);
}
if (tcs.getTestCaseStepAction() != null) {
int nextSequenceNumber = getMaxSequenceNumber(tcs.getTestCaseStepAction());
for (TestCaseStepAction tcsa : tcs.getTestCaseStepAction()) {
if (tcsa.getSequence() == -1) {
tcsa.setSequence(++nextSequenceNumber);
}
tcsa.setStep(tcs.getStep());
if (tcsa.getTestCaseStepActionControl() != null) {
int nextControlNumber = getMaxControlNumber(tcsa.getTestCaseStepActionControl());
for (TestCaseStepActionControl tscac : tcsa.getTestCaseStepActionControl()) {
if (tscac.getControlSequence() == -1) {
tscac.setControlSequence(++nextControlNumber);
}
tscac.setStep(tcs.getStep());
tscac.setSequence(tcsa.getSequence());
}
tcsacFromPage.addAll(tcsa.getTestCaseStepActionControl());
}
}
tcsaFromPage.addAll(tcs.getTestCaseStepAction());
}
}
/*
* Create, update or delete step, action and control according to the needs
*/
List<TestCaseStep> tcsFromDtb = new ArrayList(tcsService.getListOfSteps(initialTest, initialTestCase));
tcsService.compareListAndUpdateInsertDeleteElements(tcsFromPage, tcsFromDtb, duplicate);
List<TestCaseStepAction> tcsaFromDtb = new ArrayList(tcsaService.findTestCaseStepActionbyTestTestCase(initialTest, initialTestCase));
tcsaService.compareListAndUpdateInsertDeleteElements(tcsaFromPage, tcsaFromDtb, duplicate);
List<TestCaseStepActionControl> tcsacFromDtb = new ArrayList(tcsacService.findControlByTestTestCase(initialTest, initialTestCase));
tcsacService.compareListAndUpdateInsertDeleteElements(tcsacFromPage, tcsacFromDtb, duplicate);
/**
* Adding Log entry.
*/
ILogEventService logEventService = appContext.getBean(LogEventService.class);
logEventService.createForPrivateCalls("/UpdateTestCase", "UPDATE", "Update testcase : ['" + tc.getTest() + "'|'" + tc.getTestCase() + "']", request);
String encodedTest = URLEncoder.encode(tc.getTest(), "UTF-8");
String encodedTestCase = URLEncoder.encode(tc.getTestCase(), "UTF-8");
response.sendRedirect(response.encodeRedirectURL("TestCase.jsp?Load=Load&Test=" + encodedTest + "&TestCase=" + encodedTestCase));
}
use of org.cerberus.crud.service.IInvariantService in project cerberus-source by cerberustesting.
the class ReadTestCaseExecution method getCountryList.
private JSONObject getCountryList(HttpServletRequest request, ApplicationContext appContext) {
JSONObject countryList = new JSONObject();
try {
IInvariantService invariantService = appContext.getBean(InvariantService.class);
// TODO: handle if the response does not turn ok
AnswerList answer = invariantService.readByIdname("COUNTRY");
for (Invariant country : (List<Invariant>) answer.getDataList()) {
countryList.put(country.getValue(), ParameterParserUtil.parseStringParam(request.getParameter(country.getValue()), "off"));
}
} catch (JSONException ex) {
LOG.warn(ex);
}
return countryList;
}
Aggregations