Search in sources :

Example 1 with ITestCaseStepActionControlService

use of org.cerberus.crud.service.ITestCaseStepActionControlService in project cerberus-source by cerberustesting.

the class ReadTestCaseStep method getStepByKey.

private JSONObject getStepByKey(String test, String testcase, int step, ApplicationContext appContext, HttpServletResponse response) throws JSONException {
    JSONObject jsonResponse = new JSONObject();
    ITestCaseStepService stepService = appContext.getBean(TestCaseStepService.class);
    ITestCaseStepActionService stepActionService = appContext.getBean(TestCaseStepActionService.class);
    ITestCaseStepActionControlService stepActionControlService = appContext.getBean(TestCaseStepActionControlService.class);
    TestCaseStep testCaseStep = stepService.findTestCaseStep(test, testcase, step);
    Gson gson = new Gson();
    JSONObject result = new JSONObject(gson.toJson(testCaseStep));
    jsonResponse.put("step", result);
    // jsonResponse.put("step", testCaseStep);
    List<TestCaseStepAction> tcsActionList = stepActionService.getListOfAction(test, testcase, step);
    if (tcsActionList != null) {
        JSONArray list = new JSONArray();
        for (TestCaseStepAction t : tcsActionList) {
            JSONObject obj = new JSONObject(gson.toJson(t));
            obj.put("controlList", new JSONArray());
            obj.put("objType", "action");
            list.put(obj);
        }
        jsonResponse.put("tcsActionList", list);
    }
    List<TestCaseStepActionControl> tcsActionControlList = stepActionControlService.findControlByTestTestCaseStep(test, testcase, step);
    if (tcsActionControlList != null) {
        JSONArray list2 = new JSONArray();
        for (TestCaseStepActionControl t : tcsActionControlList) {
            JSONObject obj = new JSONObject(gson.toJson(t));
            list2.put(obj);
        }
        jsonResponse.put("tcsActionControlList", list2);
    }
    return jsonResponse;
}
Also used : ITestCaseStepActionService(org.cerberus.crud.service.ITestCaseStepActionService) TestCaseStepAction(org.cerberus.crud.entity.TestCaseStepAction) JSONObject(org.json.JSONObject) ITestCaseStepService(org.cerberus.crud.service.ITestCaseStepService) JSONArray(org.json.JSONArray) Gson(com.google.gson.Gson) ITestCaseStepActionControlService(org.cerberus.crud.service.ITestCaseStepActionControlService) TestCaseStep(org.cerberus.crud.entity.TestCaseStep) TestCaseStepActionControl(org.cerberus.crud.entity.TestCaseStepActionControl)

Example 2 with ITestCaseStepActionControlService

use of org.cerberus.crud.service.ITestCaseStepActionControlService 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));
}
Also used : ITestCaseStepActionService(org.cerberus.crud.service.ITestCaseStepActionService) IFactoryTestCaseStepAction(org.cerberus.crud.factory.IFactoryTestCaseStepAction) TestCaseStepAction(org.cerberus.crud.entity.TestCaseStepAction) User(org.cerberus.crud.entity.User) TestCaseCountryProperties(org.cerberus.crud.entity.TestCaseCountryProperties) IFactoryTestCaseCountryProperties(org.cerberus.crud.factory.IFactoryTestCaseCountryProperties) ArrayList(java.util.ArrayList) IFactoryTestCaseStep(org.cerberus.crud.factory.IFactoryTestCaseStep) TestCaseStep(org.cerberus.crud.entity.TestCaseStep) IUserGroupService(org.cerberus.crud.service.IUserGroupService) ITestCaseCountryService(org.cerberus.crud.service.ITestCaseCountryService) UserGroup(org.cerberus.crud.entity.UserGroup) ApplicationContext(org.springframework.context.ApplicationContext) ITestService(org.cerberus.crud.service.ITestService) Test(org.cerberus.crud.entity.Test) ITestCaseCountryPropertiesService(org.cerberus.crud.service.ITestCaseCountryPropertiesService) ITestCaseService(org.cerberus.crud.service.ITestCaseService) TestCaseCountry(org.cerberus.crud.entity.TestCaseCountry) IFactoryTestCaseCountry(org.cerberus.crud.factory.IFactoryTestCaseCountry) ILogEventService(org.cerberus.crud.service.ILogEventService) AnswerList(org.cerberus.util.answer.AnswerList) ArrayList(java.util.ArrayList) List(java.util.List) TestCaseStepActionControl(org.cerberus.crud.entity.TestCaseStepActionControl) IFactoryTestCaseStepActionControl(org.cerberus.crud.factory.IFactoryTestCaseStepActionControl) AnswerList(org.cerberus.util.answer.AnswerList) ITestCaseStepService(org.cerberus.crud.service.ITestCaseStepService) IInvariantService(org.cerberus.crud.service.IInvariantService) ITestCaseStepActionControlService(org.cerberus.crud.service.ITestCaseStepActionControlService) TestCase(org.cerberus.crud.entity.TestCase) IFactoryTestCase(org.cerberus.crud.factory.IFactoryTestCase) IUserService(org.cerberus.crud.service.IUserService)

Example 3 with ITestCaseStepActionControlService

use of org.cerberus.crud.service.ITestCaseStepActionControlService in project cerberus-source by cerberustesting.

the class ImportTestCaseStep method processRequest.

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, CerberusException {
    response.setContentType("text/html;charset=UTF-8");
    appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    ITestCaseStepService testCaseStepService = appContext.getBean(ITestCaseStepService.class);
    ITestCaseStepActionService testCaseStepActionService = appContext.getBean(ITestCaseStepActionService.class);
    ITestCaseStepActionControlService testCaseStepActionControlService = appContext.getBean(ITestCaseStepActionControlService.class);
    ITestCaseCountryService testCaseCountry = appContext.getBean(ITestCaseCountryService.class);
    ITestCaseCountryPropertiesService testCaseCountryProperties = appContext.getBean(ITestCaseCountryPropertiesService.class);
    this.database = appContext.getBean(DatabaseSpring.class);
    /**
     * Get Parameters Test : Target Test TestCase : Target TestCase Step :
     * Target Step fromTest : from Test fromTestCase : from TestCase
     * fromStep : from Step
     */
    String test = request.getParameter("Test");
    String testCase = request.getParameter("TestCase");
    Integer step = Integer.valueOf(request.getParameter("Step"));
    String fromTest = request.getParameter("FromTest");
    String fromTestCase = request.getParameter("FromTestCase");
    Integer fromStep = Integer.valueOf(request.getParameter("FromStep"));
    String importProperty = "N";
    if (request.getParameter("ImportProperty") != null) {
        LOG.debug(request.getParameter("ImportProperty"));
        importProperty = request.getParameter("ImportProperty");
    }
    /**
     * Get TestCaseStep, List of TestCaseStepAction and List of
     * TestCaseStepActionControl from Test, Testcase, Step
     */
    TestCaseStep fromTcs = testCaseStepService.findTestCaseStep(fromTest, fromTestCase, fromStep);
    List<TestCaseStepAction> fromTcsa = testCaseStepActionService.getListOfAction(fromTest, fromTestCase, fromStep);
    List<TestCaseStepActionControl> fromTcsac = testCaseStepActionControlService.findControlByTestTestCaseStep(fromTest, fromTestCase, fromStep);
    /**
     * Get List of Country of the origin testcase and the destination
     * Testcase
     */
    List<String> tccListString = null;
    List<String> tccFromListString = null;
    List<TestCaseCountryProperties> tccpList = null;
    if (importProperty.equalsIgnoreCase("Y")) {
        tccListString = testCaseCountry.findListOfCountryByTestTestCase(test, testCase);
        tccFromListString = testCaseCountry.findListOfCountryByTestTestCase(test, testCase);
    }
    /**
     * Modify the object with the target test, testcase, step, country
     */
    LOG.debug("Rewrite TestCaseStep");
    fromTcs.setTest(test);
    fromTcs.setTestCase(testCase);
    fromTcs.setStep(step);
    LOG.debug("Rewrite TestCaseStepAction");
    List<TestCaseStepAction> tcsaToImport = new ArrayList();
    // retrieve list of property name used in the step
    List<String> propertyNamesOfStep = new ArrayList<String>();
    for (TestCaseStepAction tcsa : fromTcsa) {
        tcsa.setTest(test);
        tcsa.setTestCase(testCase);
        tcsa.setStep(step);
        tcsaToImport.add(tcsa);
        if (!propertyNamesOfStep.contains(tcsa.getValue2())) {
            propertyNamesOfStep.add(tcsa.getValue2());
        }
    }
    LOG.debug("Rewrite TestCaseStepActionControl");
    List<TestCaseStepActionControl> tcsacToImport = new ArrayList();
    for (TestCaseStepActionControl tcsac : fromTcsac) {
        tcsac.setTest(test);
        tcsac.setTestCase(testCase);
        tcsac.setStep(step);
        tcsacToImport.add(tcsac);
    }
    /**
     * For the country defined in the destination testcase, insert the
     * properties of the origine testcase
     */
    List<TestCaseCountryProperties> tccpToImport = new ArrayList();
    if (importProperty.equalsIgnoreCase("Y")) {
        LOG.debug("Rewrite TestCaseCountryProperties");
        if (tccListString != null) {
            tccListString.retainAll(tccFromListString);
            if (!tccListString.isEmpty()) {
                for (String country : tccListString) {
                    tccpList = testCaseCountryProperties.findListOfPropertyPerTestTestCaseCountry(fromTest, fromTestCase, country);
                    for (TestCaseCountryProperties tccp : tccpList) {
                        if (propertyNamesOfStep.contains(tccp.getProperty())) {
                            tccp.setTest(test);
                            tccp.setTestCase(testCase);
                            tccpToImport.add(tccp);
                        }
                    }
                }
            }
        }
    }
    /**
     * Import Step, List of testcasestepaction, List of
     * testcasestepactioncontrol
     */
    LOG.debug("Import Step");
    testCaseStepService.create(fromTcs);
    testCaseStepActionService.insertListTestCaseStepAction(tcsaToImport);
    testCaseStepActionControlService.insertListTestCaseStepActionControl(tcsacToImport);
    if (importProperty.equalsIgnoreCase("Y")) {
        // testCaseCountry.insertListTestCaseCountry(tccToImport);
        testCaseCountryProperties.insertListTestCaseCountryProperties(tccpToImport);
    }
    response.sendRedirect("TestCase.jsp?Load=Load&Test=" + test + "&TestCase=" + testCase);
}
Also used : ITestCaseStepActionService(org.cerberus.crud.service.ITestCaseStepActionService) TestCaseStepAction(org.cerberus.crud.entity.TestCaseStepAction) TestCaseCountryProperties(org.cerberus.crud.entity.TestCaseCountryProperties) ITestCaseStepService(org.cerberus.crud.service.ITestCaseStepService) ArrayList(java.util.ArrayList) ITestCaseStepActionControlService(org.cerberus.crud.service.ITestCaseStepActionControlService) TestCaseStep(org.cerberus.crud.entity.TestCaseStep) ITestCaseCountryService(org.cerberus.crud.service.ITestCaseCountryService) ITestCaseCountryPropertiesService(org.cerberus.crud.service.ITestCaseCountryPropertiesService) DatabaseSpring(org.cerberus.database.DatabaseSpring) TestCaseStepActionControl(org.cerberus.crud.entity.TestCaseStepActionControl)

Example 4 with ITestCaseStepActionControlService

use of org.cerberus.crud.service.ITestCaseStepActionControlService in project cerberus-source by cerberustesting.

the class DuplicateTestCase method processRequest.

// </editor-fold>
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, JSONException, CerberusException {
    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"), null);
    /**
     * Checking all constrains before calling the services.
     */
    if (StringUtil.isNullOrEmpty(test) || StringUtil.isNullOrEmpty(testCase) || StringUtil.isNullOrEmpty(originalTest) || originalTestCase != null) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Test Case").replace("%OPERATION%", "Duplicate").replace("%REASON%", "mandatory fields are missing."));
        ans.setResultMessage(msg);
    } else {
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        ITestCaseService testCaseService = appContext.getBean(ITestCaseService.class);
        ITestCaseCountryService testCaseCountryService = appContext.getBean(ITestCaseCountryService.class);
        ITestCaseCountryPropertiesService testCaseCountryPropertiesService = appContext.getBean(ITestCaseCountryPropertiesService.class);
        ITestCaseStepService testCaseStepService = appContext.getBean(ITestCaseStepService.class);
        ITestCaseStepActionService testCaseStepActionService = appContext.getBean(ITestCaseStepActionService.class);
        ITestCaseStepActionControlService testCaseStepActionControlService = appContext.getBean(ITestCaseStepActionControlService.class);
        ITestCaseLabelService testCaseLabelService = appContext.getBean(ITestCaseLabelService.class);
        AnswerItem originalTestAI = testCaseService.readByKey(originalTest, originalTestCase);
        AnswerItem targetTestAI = testCaseService.readByKey(test, testCase);
        TestCase originalTC = (TestCase) originalTestAI.getItem();
        TestCase targetTC = (TestCase) targetTestAI.getItem();
        if (!(originalTestAI.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && originalTestAI.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%", "TestCase").replace("%OPERATION%", "Duplicate").replace("%REASON%", "TestCase does not exist."));
            ans.setResultMessage(msg);
        } else /**
         * The service was able to perform the query and confirm the object
         * exist, then we can update it.
         */
        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%", "TestCase").replace("%OPERATION%", "Duplicate").replace("%REASON%", "Not enought privilege to duplicate the testcase. You must belong to Test Privilege."));
            ans.setResultMessage(msg);
        } else if (targetTC != null) {
            // If target Test Case already exists.
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
            msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCase").replace("%OPERATION%", "Duplicate").replace("%REASON%", "The test case you try to create already exists. Please define a test/testcase that is not already existing."));
            ans.setResultMessage(msg);
        } else {
            getInfo(request, originalTC);
            // Update object with new testcase id and insert it in db
            originalTC.setTest(test);
            originalTC.setTestCase(testCase);
            ans = testCaseService.create(originalTC);
            List<TestCaseCountry> countryList = new ArrayList();
            countryList = testCaseCountryService.findTestCaseCountryByTestTestCase(originalTest, originalTestCase);
            boolean success = true;
            if (!countryList.isEmpty()) {
                ans = testCaseCountryService.duplicateList(countryList, test, testCase);
            }
            // List<TestCaseCountry> countryList = getCountryList(test, testCase, request);
            // boolean success = false;
            // if (countryList.isEmpty()) {
            // success = true;
            // } else {
            // success = testCaseCountryService.insertListTestCaseCountry(countryList);
            // }
            List<TestCaseCountryProperties> tccpList = new ArrayList();
            if (!countryList.isEmpty() && ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && success) {
                tccpList = testCaseCountryPropertiesService.findListOfPropertyPerTestTestCase(originalTest, originalTestCase);
                if (!tccpList.isEmpty()) {
                    ans = testCaseCountryPropertiesService.duplicateList(tccpList, test, testCase);
                }
            }
            List<TestCaseStep> tcsList = new ArrayList();
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && success) {
                tcsList = testCaseStepService.getListOfSteps(originalTest, originalTestCase);
                if (!tcsList.isEmpty()) {
                    ans = testCaseStepService.duplicateList(tcsList, test, testCase);
                }
            }
            List<TestCaseStepAction> tcsaList = new ArrayList();
            if (!tcsList.isEmpty() && ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && success) {
                tcsaList = testCaseStepActionService.findTestCaseStepActionbyTestTestCase(originalTest, originalTestCase);
                if (!tcsaList.isEmpty()) {
                    ans = testCaseStepActionService.duplicateList(tcsaList, test, testCase);
                }
            }
            if (!tcsList.isEmpty() && !tcsaList.isEmpty() && ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && success) {
                List<TestCaseStepActionControl> tcsacList = testCaseStepActionControlService.findControlByTestTestCase(originalTest, originalTestCase);
                if (!tcsacList.isEmpty()) {
                    ans = testCaseStepActionControlService.duplicateList(tcsacList, test, testCase);
                }
            }
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && success) {
                List<TestCaseLabel> tclList = testCaseLabelService.readByTestTestCase(originalTest, originalTestCase).getDataList();
                if (!tclList.isEmpty()) {
                    ans = testCaseLabelService.duplicateList(tclList, test, testCase);
                }
            }
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && success) {
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCase").replace("%OPERATION%", "Duplicate"));
                ans.setResultMessage(msg);
                /**
                 * Update was successful. Adding Log entry.
                 */
                ILogEventService logEventService = appContext.getBean(LogEventService.class);
                logEventService.createForPrivateCalls("/DuplicateTestCase", "CREATE", "Create testcase : ['" + test + "'|'" + testCase + "']", 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();
}
Also used : ITestCaseStepActionService(org.cerberus.crud.service.ITestCaseStepActionService) TestCaseStepAction(org.cerberus.crud.entity.TestCaseStepAction) TestCaseCountryProperties(org.cerberus.crud.entity.TestCaseCountryProperties) PolicyFactory(org.owasp.html.PolicyFactory) MessageEvent(org.cerberus.engine.entity.MessageEvent) ArrayList(java.util.ArrayList) TestCaseStep(org.cerberus.crud.entity.TestCaseStep) ITestCaseCountryService(org.cerberus.crud.service.ITestCaseCountryService) ApplicationContext(org.springframework.context.ApplicationContext) ITestCaseCountryPropertiesService(org.cerberus.crud.service.ITestCaseCountryPropertiesService) ITestCaseService(org.cerberus.crud.service.ITestCaseService) TestCaseCountry(org.cerberus.crud.entity.TestCaseCountry) IFactoryTestCaseCountry(org.cerberus.crud.factory.IFactoryTestCaseCountry) ILogEventService(org.cerberus.crud.service.ILogEventService) TestCaseStepActionControl(org.cerberus.crud.entity.TestCaseStepActionControl) ITestCaseLabelService(org.cerberus.crud.service.ITestCaseLabelService) ITestCaseStepService(org.cerberus.crud.service.ITestCaseStepService) TestCaseLabel(org.cerberus.crud.entity.TestCaseLabel) ITestCaseStepActionControlService(org.cerberus.crud.service.ITestCaseStepActionControlService) AnswerItem(org.cerberus.util.answer.AnswerItem) Answer(org.cerberus.util.answer.Answer) JSONObject(org.json.JSONObject) TestCase(org.cerberus.crud.entity.TestCase)

Example 5 with ITestCaseStepActionControlService

use of org.cerberus.crud.service.ITestCaseStepActionControlService in project cerberus-source by cerberustesting.

the class UpdateTestCaseWithDependencies1 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, JSONException {
    JSONObject jsonResponse = new JSONObject();
    Answer ans = new Answer();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
    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.
     */
    StringBuilder sb = new StringBuilder();
    BufferedReader br = request.getReader();
    String str;
    while ((str = br.readLine()) != null) {
        sb.append(str);
    }
    JSONObject jObj = new JSONObject(sb.toString());
    String initialTest = jObj.getString("informationInitialTest");
    String initialTestCase = jObj.getString("informationInitialTestCase");
    String test = jObj.getString("informationTest");
    String testCase = jObj.getString("informationTestCase");
    JSONArray properties = jObj.getJSONArray("propArr");
    JSONArray stepArray = jObj.getJSONArray("stepArray");
    boolean duplicate = false;
    /**
     * 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%", "Update").replace("%REASON%", "mendatory fields are missing."));
        ans.setResultMessage(msg);
    } else {
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        ITestCaseService testCaseService = appContext.getBean(ITestCaseService.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);
        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%", "TestCase").replace("%OPERATION%", "Update").replace("%REASON%", "TestCase does not exist."));
            ans.setResultMessage(msg);
        } else /**
         * The service was able to perform the query and confirm the object
         * exist, then we can update it.
         */
        if (!testCaseService.hasPermissionsUpdate(tc, request)) {
            // 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%", "TestCase").replace("%OPERATION%", "Update").replace("%REASON%", "Not enought privilege to update the testcase. You mut belong to Test Privilege or even TestAdmin in case the test is in WORKING status."));
            ans.setResultMessage(msg);
        } else {
            // Test Case exist and we can update it so Global update start here //
            /**
             * TestcaseCountryProperties Update.
             */
            List<TestCaseCountryProperties> tccpFromPage = getTestCaseCountryPropertiesFromParameter(request, appContext, test, testCase, properties);
            tccpService.compareListAndUpdateInsertDeleteElements(initialTest, initialTestCase, tccpFromPage);
            /*
                 * 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, stepArray);
            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);
            tc.setTestCaseVersion(tc.getTestCaseVersion() + 1);
            testCaseService.updateTestCase(tc);
            /**
             * Adding Log entry.
             */
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                /**
                 * Update was successful. Adding Log entry.
                 */
                ILogEventService logEventService = appContext.getBean(LogEventService.class);
                logEventService.createForPrivateCalls("/UpdateTestCaseWithDependencies1", "UPDATE", "Update testcase : ['" + tc.getTest() + "'|'" + tc.getTestCase() + "'] version : " + tc.getTestCaseVersion(), 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();
}
Also used : ITestCaseStepActionService(org.cerberus.crud.service.ITestCaseStepActionService) IFactoryTestCaseStepAction(org.cerberus.crud.factory.IFactoryTestCaseStepAction) TestCaseStepAction(org.cerberus.crud.entity.TestCaseStepAction) PolicyFactory(org.owasp.html.PolicyFactory) MessageEvent(org.cerberus.engine.entity.MessageEvent) ITestCaseStepService(org.cerberus.crud.service.ITestCaseStepService) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) ILogEventService(org.cerberus.crud.service.ILogEventService) LogEventService(org.cerberus.crud.service.impl.LogEventService) ITestCaseStepActionControlService(org.cerberus.crud.service.ITestCaseStepActionControlService) IFactoryTestCaseStep(org.cerberus.crud.factory.IFactoryTestCaseStep) TestCaseStep(org.cerberus.crud.entity.TestCaseStep) AnswerItem(org.cerberus.util.answer.AnswerItem) Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) TestCase(org.cerberus.crud.entity.TestCase) ITestCaseCountryPropertiesService(org.cerberus.crud.service.ITestCaseCountryPropertiesService) BufferedReader(java.io.BufferedReader) ITestCaseService(org.cerberus.crud.service.ITestCaseService) ILogEventService(org.cerberus.crud.service.ILogEventService) ArrayList(java.util.ArrayList) List(java.util.List) TestCaseStepActionControl(org.cerberus.crud.entity.TestCaseStepActionControl) IFactoryTestCaseStepActionControl(org.cerberus.crud.factory.IFactoryTestCaseStepActionControl)

Aggregations

TestCaseStep (org.cerberus.crud.entity.TestCaseStep)6 TestCaseStepAction (org.cerberus.crud.entity.TestCaseStepAction)6 TestCaseStepActionControl (org.cerberus.crud.entity.TestCaseStepActionControl)6 ITestCaseStepActionControlService (org.cerberus.crud.service.ITestCaseStepActionControlService)6 ITestCaseStepActionService (org.cerberus.crud.service.ITestCaseStepActionService)6 ITestCaseStepService (org.cerberus.crud.service.ITestCaseStepService)6 ArrayList (java.util.ArrayList)5 ITestCaseCountryPropertiesService (org.cerberus.crud.service.ITestCaseCountryPropertiesService)4 TestCase (org.cerberus.crud.entity.TestCase)3 TestCaseCountryProperties (org.cerberus.crud.entity.TestCaseCountryProperties)3 IFactoryTestCaseStep (org.cerberus.crud.factory.IFactoryTestCaseStep)3 IFactoryTestCaseStepAction (org.cerberus.crud.factory.IFactoryTestCaseStepAction)3 IFactoryTestCaseStepActionControl (org.cerberus.crud.factory.IFactoryTestCaseStepActionControl)3 ILogEventService (org.cerberus.crud.service.ILogEventService)3 ITestCaseCountryService (org.cerberus.crud.service.ITestCaseCountryService)3 ITestCaseService (org.cerberus.crud.service.ITestCaseService)3 JSONObject (org.json.JSONObject)3 ApplicationContext (org.springframework.context.ApplicationContext)3 List (java.util.List)2 TestCaseCountry (org.cerberus.crud.entity.TestCaseCountry)2