Search in sources :

Example 26 with TestCaseStep

use of org.cerberus.crud.entity.TestCaseStep in project cerberus-source by cerberustesting.

the class GetStepInLibrary 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 {
    PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
    // String system = policy.sanitize(request.getParameter("system"));
    String system = ParameterParserUtil.parseStringParamAndSanitize(request.getParameter("system"), null);
    String test = policy.sanitize(request.getParameter("test"));
    String testCase = policy.sanitize(request.getParameter("testCase"));
    String withTestCase = policy.sanitize(request.getParameter("withTestCase"));
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    ITestCaseStepService testCaseStepService = appContext.getBean(ITestCaseStepService.class);
    ITestCaseService testCaseService = appContext.getBean(ITestCaseService.class);
    JSONArray array = new JSONArray();
    JSONObject jsonObject = new JSONObject();
    try {
        List<TestCaseStep> tcsList;
        if (test.equals("") && testCase.equals("")) {
            tcsList = testCaseStepService.getStepLibraryBySystem(system);
        } else if (testCase.equals("")) {
            tcsList = testCaseStepService.getStepLibraryBySystemTest(system, test);
        } else {
            tcsList = testCaseStepService.getStepLibraryBySystemTestTestCase(system, test, testCase);
        }
        for (TestCaseStep list : tcsList) {
            JSONObject tcs = new JSONObject();
            tcs.put("test", list.getTest());
            tcs.put("testCase", list.getTestCase());
            tcs.put("step", list.getStep());
            tcs.put("sort", list.getSort());
            tcs.put("description", list.getDescription());
            if (list.getTestCaseObj() != null) {
                tcs.put("tcdesc", list.getTestCaseObj().getDescription());
                tcs.put("tcapp", list.getTestCaseObj().getApplication());
            }
            array.put(tcs);
        }
        jsonObject.put("testCaseStepList", array);
        response.setContentType("application/json");
        response.getWriter().print(jsonObject.toString());
    } catch (JSONException exception) {
        LOG.warn(exception.toString());
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) PolicyFactory(org.owasp.html.PolicyFactory) JSONObject(org.json.JSONObject) ITestCaseStepService(org.cerberus.crud.service.ITestCaseStepService) ITestCaseService(org.cerberus.crud.service.ITestCaseService) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) TestCaseStep(org.cerberus.crud.entity.TestCaseStep)

Example 27 with TestCaseStep

use of org.cerberus.crud.entity.TestCaseStep in project cerberus-source by cerberustesting.

the class DeleteTest method externallyUsedTestCaseSteps.

/**
 * Get {@link TestCaseStep} which are using an other {@link TestCaseStep} from the given {@link Test} but which are NOT included into this {@link Test}
 *
 * @param test the {@link Test} from which getting externally used {@link TestCaseStep}s
 * @return a {@link Collection} of {@link TestCaseStep} which are using an other {@link TestCaseStep} from the given {@link Test} but which are NOT included into this {@link Test}
 * @throws CerberusException if an unexpected error occurred
 */
private Collection<TestCaseStep> externallyUsedTestCaseSteps(final Test test) throws CerberusException {
    // Get the associated ApplicationContext to this servlet
    final ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    // Get all TestCaseSteps which are using an other TestCaseSteps from given Test
    final ITestCaseStepService testCaseStepService = applicationContext.getBean(ITestCaseStepService.class);
    final List<TestCaseStep> stepsInUse = testCaseStepService.getTestCaseStepsUsingTestInParameter(test.getTest());
    // Filter the retrieved list to only retain those which are not included from the given Test
    return Collections2.filter(stepsInUse, new Predicate<TestCaseStep>() {

        @Override
        public boolean apply(@Nullable final TestCaseStep input) {
            return !input.getTest().equals(test.getTest());
        }

        @Override
        public boolean test(TestCaseStep t) {
            // To change body of generated methods, choose Tools | Templates.
            return Predicate.super.test(t);
        }
    });
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) ITestCaseStepService(org.cerberus.crud.service.ITestCaseStepService) TestCaseStep(org.cerberus.crud.entity.TestCaseStep)

Example 28 with TestCaseStep

use of org.cerberus.crud.entity.TestCaseStep in project cerberus-source by cerberustesting.

the class DeleteTestCase 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, 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"), null);
    /**
     * Checking all constrains before calling the services.
     */
    if (StringUtil.isNullOrEmpty(test) || testCase == null) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCase").replace("%OPERATION%", "Delete").replace("%REASON%", "mendatory fields is missing!"));
        ans.setResultMessage(msg);
    } else {
        /**
         * All data seems cleans so we can call the services.
         */
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        ITestCaseService testCaseService = appContext.getBean(ITestCaseService.class);
        ITestCaseStepService testCaseStepService = appContext.getBean(ITestCaseStepService.class);
        AnswerItem resp = testCaseService.readByKey(test, testCase);
        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%", "Delete").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 delete it.
             */
            TestCase testCaseData = (TestCase) resp.getItem();
            List<TestCaseStep> tcsList = testCaseStepService.getTestCaseStepUsingTestCaseInParamter(testCaseData.getTest(), testCaseData.getTestCase());
            if (tcsList != null && !tcsList.isEmpty()) {
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
                msg.setDescription(msg.getDescription().replace("%ITEM%", "TestCase").replace("%OPERATION%", "Delete").replace("%REASON%", "You're trying to delete a testcase which have some step used in other tests. Please remove the link before deleting this testcase."));
                ans.setResultMessage(msg);
            } else {
                ans = testCaseService.delete(testCaseData);
            }
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                /**
                 * Delete was successful. Adding Log entry.
                 */
                ILogEventService logEventService = appContext.getBean(LogEventService.class);
                logEventService.createForPrivateCalls("/DeleteTestCase", "DELETE", "Delete TestCase : ['" + 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.toString());
    response.getWriter().flush();
}
Also used : PolicyFactory(org.owasp.html.PolicyFactory) MessageEvent(org.cerberus.engine.entity.MessageEvent) ITestCaseStepService(org.cerberus.crud.service.ITestCaseStepService) 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) ITestCaseService(org.cerberus.crud.service.ITestCaseService) ILogEventService(org.cerberus.crud.service.ILogEventService)

Example 29 with TestCaseStep

use of org.cerberus.crud.entity.TestCaseStep in project cerberus-source by cerberustesting.

the class UpdateTestCaseWithDependencies1 method getTestCaseStepFromParameter.

private List<TestCaseStep> getTestCaseStepFromParameter(HttpServletRequest request, ApplicationContext appContext, String test, String testCase, boolean duplicate, JSONArray stepArray) throws JSONException {
    List<TestCaseStep> testCaseStep = new ArrayList();
    ITestCaseStepService tcsService = appContext.getBean(ITestCaseStepService.class);
    IFactoryTestCaseStep testCaseStepFactory = appContext.getBean(IFactoryTestCaseStep.class);
    for (int i = 0; i < stepArray.length(); i++) {
        JSONObject step = stepArray.getJSONObject(i);
        boolean delete = step.getBoolean("toDelete");
        int stepNumber = step.isNull("step") ? -1 : step.getInt("step");
        int sort = step.isNull("sort") ? -1 : step.getInt("sort");
        String loop = step.getString("loop");
        String conditionOper = step.getString("conditionOper");
        String conditionVal1 = step.getString("conditionVal1");
        String conditionVal2 = step.getString("conditionVal2");
        String description = step.getString("description");
        String useStep = step.getString("useStep");
        String useStepTest = step.getString("useStepTest");
        String useStepTestCase = step.getString("useStepTestCase");
        int useStepStep = step.getInt("useStepStep");
        String inLibrary = step.getString("inLibrary");
        JSONArray stepActions = step.getJSONArray("actionArr");
        if (!delete) {
            TestCaseStep tcStep = testCaseStepFactory.create(test, testCase, stepNumber, sort, loop, conditionOper, conditionVal1, conditionVal2, description, useStep, useStepTest, useStepTestCase, useStepStep, inLibrary);
            if (useStep.equals("N")) {
                tcStep.setTestCaseStepAction(getTestCaseStepActionFromParameter(request, appContext, test, testCase, stepActions));
            } else {
                TestCaseStep tcs = null;
                if (useStepStep != -1 && !useStepTest.equals("") && !useStepTestCase.equals("")) {
                    tcs = tcsService.findTestCaseStep(useStepTest, useStepTestCase, useStepStep);
                    if (tcs != null) {
                        tcStep.setUseStepTest(tcs.getTest());
                        tcStep.setUseStepTestCase(tcs.getTestCase());
                        tcStep.setUseStepStep(tcs.getStep());
                    }
                }
            }
            testCaseStep.add(tcStep);
        }
    }
    return testCaseStep;
}
Also used : JSONObject(org.json.JSONObject) ITestCaseStepService(org.cerberus.crud.service.ITestCaseStepService) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) IFactoryTestCaseStep(org.cerberus.crud.factory.IFactoryTestCaseStep) IFactoryTestCaseStep(org.cerberus.crud.factory.IFactoryTestCaseStep) TestCaseStep(org.cerberus.crud.entity.TestCaseStep)

Example 30 with TestCaseStep

use of org.cerberus.crud.entity.TestCaseStep 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)41 ArrayList (java.util.ArrayList)20 IFactoryTestCaseStep (org.cerberus.crud.factory.IFactoryTestCaseStep)19 ITestCaseStepService (org.cerberus.crud.service.ITestCaseStepService)16 TestCase (org.cerberus.crud.entity.TestCase)15 TestCaseStepAction (org.cerberus.crud.entity.TestCaseStepAction)13 JSONObject (org.json.JSONObject)13 ApplicationContext (org.springframework.context.ApplicationContext)12 SQLException (java.sql.SQLException)11 TestCaseCountryProperties (org.cerberus.crud.entity.TestCaseCountryProperties)11 TestCaseStepActionControl (org.cerberus.crud.entity.TestCaseStepActionControl)11 Connection (java.sql.Connection)10 PreparedStatement (java.sql.PreparedStatement)10 ResultSet (java.sql.ResultSet)10 ITestCaseService (org.cerberus.crud.service.ITestCaseService)10 JSONArray (org.json.JSONArray)10 PolicyFactory (org.owasp.html.PolicyFactory)10 ITestCaseStepActionService (org.cerberus.crud.service.ITestCaseStepActionService)9 MessageEvent (org.cerberus.engine.entity.MessageEvent)9 TestCaseCountry (org.cerberus.crud.entity.TestCaseCountry)8