Search in sources :

Example 51 with TestCase

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

the class ExecutionStartService method startExecution.

@Override
public TestCaseExecution startExecution(TestCaseExecution tCExecution) throws CerberusException {
    /**
     * Start timestamp.
     */
    long executionStart = new Date().getTime();
    LOG.debug("Initializing Start Timestamp : " + executionStart);
    tCExecution.setStart(executionStart);
    /**
     * Checking the parameters.
     */
    LOG.debug("Checking the parameters.");
    Invariant myInvariant;
    try {
        myInvariant = invariantService.convert(invariantService.readByKey("OUTPUTFORMAT", tCExecution.getOutputFormat()));
    } catch (CerberusException ex) {
        MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_OUTPUTFORMAT_INVALID);
        mes.setDescription(mes.getDescription().replace("%PARAM%", tCExecution.getOutputFormat()));
        LOG.debug(mes.getDescription());
        throw new CerberusException(mes);
    }
    try {
        myInvariant = invariantService.convert(invariantService.readByKey("VERBOSE", String.valueOf(tCExecution.getVerbose())));
    } catch (CerberusException ex) {
        MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_VERBOSE_INVALID);
        mes.setDescription(mes.getDescription().replace("%PARAM%", String.valueOf(tCExecution.getVerbose())));
        LOG.debug(mes.getDescription());
        throw new CerberusException(mes);
    }
    try {
        myInvariant = invariantService.convert(invariantService.readByKey("SCREENSHOT", String.valueOf(tCExecution.getScreenshot())));
    } catch (CerberusException ex) {
        MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_SCREENSHOT_INVALID);
        mes.setDescription(mes.getDescription().replace("%PARAM%", String.valueOf(tCExecution.getScreenshot())));
        LOG.debug(mes.getDescription());
        throw new CerberusException(mes);
    }
    LOG.debug("Parameters checked.");
    /**
     * Load TestCase information and set TCase to the TestCaseExecution
     * object.
     */
    tCExecution.setResultMessage(new MessageGeneral(MessageGeneralEnum.EXECUTION_PE_LOADINGDATA));
    LOG.debug("Loading Test Case Information. " + tCExecution.getTest() + "-" + tCExecution.getTestCase());
    // Integrate this.loadTestCaseService.loadTestCase(tCExecution); inside with Dependency.
    try {
        // TestCase tCase = testCaseService.findTestCaseByKey(tCExecution.getTest(), tCExecution.getTestCase());
        TestCase tCase = testCaseService.convert(testCaseService.readByKey(tCExecution.getTest(), tCExecution.getTestCase()));
        if (tCase != null) {
            tCExecution.setTestCaseObj(tCase);
            tCExecution.setDescription(tCase.getDescription());
            tCExecution.setConditionOper(tCase.getConditionOper());
            tCExecution.setConditionVal1(tCase.getConditionVal1());
            tCExecution.setConditionVal1Init(tCase.getConditionVal1());
            tCExecution.setConditionVal2(tCase.getConditionVal2());
            tCExecution.setConditionVal2Init(tCase.getConditionVal2());
            tCExecution.setTestCaseVersion(tCase.getTestCaseVersion());
        } else {
            throw new CerberusException(new MessageGeneral(MessageGeneralEnum.NO_DATA_FOUND));
        }
        /**
         * Copy the status of the testcase to the status column of the
         * Execution. This is done to know how stable was the testcase at
         * the time of the execution.
         */
        tCExecution.setStatus(tCase.getStatus());
    } catch (CerberusException ex) {
        MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_TESTCASE_NOT_FOUND);
        mes.setDescription(mes.getDescription().replace("%TEST%", tCExecution.getTest()));
        mes.setDescription(mes.getDescription().replace("%TESTCASE%", tCExecution.getTestCase()));
        LOG.debug(mes.getDescription());
        throw new CerberusException(mes);
    }
    LOG.debug("Test Case Information Loaded - " + tCExecution.getTest() + "-" + tCExecution.getTestCase());
    /**
     * Load Test information and Set TestObject to the TestCaseExecution
     * object.
     */
    LOG.debug("Loading Test Information");
    try {
        tCExecution.setTestObj(this.testService.convert(this.testService.readByKey(tCExecution.getTest())));
    } catch (CerberusException ex) {
        MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_TEST_NOT_FOUND);
        mes.setDescription(mes.getDescription().replace("%TEST%", tCExecution.getTest()));
        LOG.debug(mes.getDescription());
        throw new CerberusException(mes);
    }
    LOG.debug("Test Information Loaded - " + tCExecution.getTest());
    /**
     * Load Application information and Set Application to the
     * TestCaseExecution object.
     */
    LOG.debug("Loading Application Information");
    try {
        tCExecution.setApplication(tCExecution.getTestCaseObj().getApplication());
        tCExecution.setApplicationObj(applicationService.convert(this.applicationService.readByKey(tCExecution.getTestCaseObj().getApplication())));
    } catch (CerberusException ex) {
        MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_APPLICATION_NOT_FOUND);
        mes.setDescription(mes.getDescription().replace("%APPLI%", tCExecution.getTestCaseObj().getApplication()));
        LOG.debug(mes.getDescription());
        throw new CerberusException(mes);
    }
    LOG.debug("Application Information Loaded - " + tCExecution.getApplicationObj().getApplication() + " - " + tCExecution.getApplicationObj().getDescription());
    /**
     * Init System from Application.
     */
    tCExecution.setSystem(tCExecution.getApplicationObj().getSystem());
    /**
     * Load Country information and Set it to the TestCaseExecution object.
     */
    LOG.debug("Loading Country Information");
    try {
        tCExecution.setCountryObj(invariantService.convert(invariantService.readByKey("COUNTRY", tCExecution.getCountry())));
    } catch (CerberusException ex) {
        MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_COUNTRY_NOT_FOUND);
        mes.setDescription(mes.getDescription().replace("%COUNTRY%", tCExecution.getCountry()));
        LOG.debug(mes.getDescription());
        throw new CerberusException(mes);
    }
    LOG.debug("Country Information Loaded - " + tCExecution.getCountryObj().getValue() + " - " + tCExecution.getCountryObj().getDescription());
    /**
     * Checking if execution is manual or automaticaly configured. If
     * Manual, CountryEnvironmentParameters object is manually created with
     * the servlet parameters. If automatic, parameters are build from the
     * CountryEnvironmentParameters. table in the database. Environmentdata
     * will always be filled with the environment. Environment will be
     * forced to MANUAL if execution is manual.
     */
    LOG.debug("Checking if connectivity parameters are manual or automatic from the database. '" + tCExecution.isManualURL() + "'");
    if (tCExecution.isManualURL()) {
        LOG.debug("Execution will be done with manual application connectivity setting.");
        if (StringUtil.isNullOrEmpty(tCExecution.getMyHost())) {
            MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_MANUALURL_INVALID);
            LOG.debug(mes.getDescription());
            throw new CerberusException(mes);
        } else {
            CountryEnvironmentParameters cea;
            cea = this.factorycountryEnvironmentParameters.create(tCExecution.getApplicationObj().getSystem(), tCExecution.getCountry(), tCExecution.getEnvironment(), tCExecution.getApplicationObj().getApplication(), tCExecution.getMyHost(), "", tCExecution.getMyContextRoot(), tCExecution.getMyLoginRelativeURL(), "", "", "", "", CountryEnvironmentParameters.DEFAULT_POOLSIZE);
            cea.setIp(tCExecution.getMyHost());
            cea.setUrl(tCExecution.getMyContextRoot());
            tCExecution.setUrl(StringUtil.getURLFromString(cea.getIp(), cea.getUrl(), "", ""));
            cea.setUrlLogin(tCExecution.getMyLoginRelativeURL());
            tCExecution.setCountryEnvironmentParameters(cea);
            LOG.debug(" -> Execution will be done with manual application connectivity setting. IP/URL/LOGIN : " + cea.getIp() + "-" + cea.getUrl() + "-" + cea.getUrlLogin());
        }
        /**
         * If execution is manual, we force the env at 'MANUAL-ENVDATA'
         * string. We keep envData information in order to trace the env
         * data that has been used.
         */
        tCExecution.setEnvironment("MANUAL-" + tCExecution.getEnvironmentData());
    } else {
        /**
         * Automatic application configuration execution.
         */
        LOG.debug("Execution will be done with automatic application connectivity setting.");
        /**
         * Load Country/Environment/Application information and set them to
         * the TestCaseExecution object
         */
        LOG.debug("Loading Country/Environment/Application Information. " + tCExecution.getCountry() + "-" + tCExecution.getEnvironment() + "-" + tCExecution.getApplicationObj().getApplication());
        CountryEnvironmentParameters cea;
        try {
            cea = this.countryEnvironmentParametersService.convert(this.countryEnvironmentParametersService.readByKey(tCExecution.getApplicationObj().getSystem(), tCExecution.getCountry(), tCExecution.getEnvironment(), tCExecution.getApplicationObj().getApplication()));
            if (cea != null) {
                tCExecution.setCountryEnvironmentParameters(cea);
                // tCExecution.setUrl(cea.getIp()+ cea.getUrl());
                tCExecution.setUrl(StringUtil.getURLFromString(cea.getIp(), cea.getUrl(), "", ""));
            } else {
                MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_COUNTRYENVAPP_NOT_FOUND);
                mes.setDescription(mes.getDescription().replace("%COUNTRY%", tCExecution.getCountry()));
                mes.setDescription(mes.getDescription().replace("%ENV%", tCExecution.getEnvironment()));
                mes.setDescription(mes.getDescription().replace("%APPLI%", tCExecution.getTestCaseObj().getApplication()));
                LOG.error(mes.getDescription());
                throw new CerberusException(mes);
            }
        /**
         * Forcing the IP URL and Login config from DevIP, DevURL and
         * DevLogin parameter only if DevURL is defined.
         */
        } catch (CerberusException ex) {
            MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_COUNTRYENVAPP_NOT_FOUND);
            mes.setDescription(mes.getDescription().replace("%COUNTRY%", tCExecution.getCountry()));
            mes.setDescription(mes.getDescription().replace("%ENV%", tCExecution.getEnvironment()));
            mes.setDescription(mes.getDescription().replace("%APPLI%", tCExecution.getTestCaseObj().getApplication()));
            LOG.error(mes.getDescription());
            throw new CerberusException(mes);
        }
        LOG.debug("Country/Environment/Application Information Loaded. " + tCExecution.getCountry() + " - " + tCExecution.getEnvironment() + " - " + tCExecution.getApplicationObj().getApplication());
        LOG.debug(" -> Execution will be done with automatic application connectivity setting. IP/URL/LOGIN : " + cea.getIp() + "-" + cea.getUrl() + "-" + cea.getUrlLogin());
        tCExecution.setEnvironmentData(tCExecution.getEnvironment());
    }
    /**
     * Load Environment object from invariant table.
     */
    LOG.debug("Loading Environment Information. " + tCExecution.getEnvironmentData());
    try {
        tCExecution.setEnvironmentDataObj(invariantService.convert(invariantService.readByKey("ENVIRONMENT", tCExecution.getEnvironmentData())));
    } catch (CerberusException ex) {
        if (tCExecution.isManualURL()) {
            MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_ENVIRONMENT_DOESNOTEXIST_MAN);
            mes.setDescription(mes.getDescription().replace("%ENV%", tCExecution.getEnvironmentData()));
            LOG.debug(mes.getDescription());
            throw new CerberusException(mes);
        } else {
            MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_ENVIRONMENT_DOESNOTEXIST);
            mes.setDescription(mes.getDescription().replace("%ENV%", tCExecution.getEnvironmentData()));
            LOG.debug(mes.getDescription());
            throw new CerberusException(mes);
        }
    }
    LOG.debug("Environment Information Loaded");
    /**
     * Load Country/Environment information and set them to the
     * TestCaseExecution object. Environment considered here is the data
     * environment.
     */
    LOG.debug("Loading Country/Environment Information. " + tCExecution.getCountry() + " - " + tCExecution.getEnvironmentData());
    CountryEnvParam countEnvParam;
    try {
        countEnvParam = this.countryEnvParamService.convert(this.countryEnvParamService.readByKey(tCExecution.getApplicationObj().getSystem(), tCExecution.getCountry(), tCExecution.getEnvironmentData()));
        tCExecution.setCountryEnvParam(countEnvParam);
        /**
         * Copy the Build/Revision of the environment to the Execution. This
         * is done to keep track of all execution done on a specific version
         * of system
         */
        tCExecution.setBuild(countEnvParam.getBuild());
        tCExecution.setRevision(countEnvParam.getRevision());
    } catch (CerberusException ex) {
        MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_COUNTRYENV_NOT_FOUND);
        mes.setDescription(mes.getDescription().replace("%SYSTEM%", tCExecution.getApplicationObj().getSystem()));
        mes.setDescription(mes.getDescription().replace("%COUNTRY%", tCExecution.getCountry()));
        mes.setDescription(mes.getDescription().replace("%ENV%", tCExecution.getEnvironmentData()));
        LOG.debug(mes.getDescription());
        throw new CerberusException(mes);
    }
    LOG.debug("Country/Environment Information Loaded. " + tCExecution.getCountry() + " - " + tCExecution.getEnvironmentData());
    /**
     * If Timeout is defined at the execution level, set action wait default
     * to this value, else Get the cerberus_action_wait_default parameter.
     * This parameter will be used by tha wait action if no timeout/event is
     * defined.
     */
    try {
        if (!tCExecution.getTimeout().isEmpty()) {
            tCExecution.setCerberus_action_wait_default(Integer.valueOf(tCExecution.getTimeout()));
        } else {
            tCExecution.setCerberus_action_wait_default(parameterService.getParameterIntegerByKey("cerberus_action_wait_default", tCExecution.getApplicationObj().getSystem(), 90000));
        }
    } catch (NumberFormatException ex) {
        LOG.warn("Parameter cerberus_action_wait_default must be an integer, default value set to 90000 milliseconds. " + ex.toString());
        tCExecution.setCerberus_action_wait_default(90000);
    }
    /**
     * Check if test can be executed TODO : Replace Message with try/catch
     * cerberus exception
     */
    tCExecution.setResultMessage(new MessageGeneral(MessageGeneralEnum.EXECUTION_PE_VALIDATIONSTARTING));
    LOG.debug("Performing the Checks before starting the execution");
    MessageGeneral canExecuteTestCase = this.executionCheckService.checkTestCaseExecution(tCExecution);
    tCExecution.setResultMessage(canExecuteTestCase);
    /**
     * We stop if the result is not OK
     */
    if (!(tCExecution.getResultMessage().equals(new MessageGeneral(MessageGeneralEnum.EXECUTION_PE_CHECKINGPARAMETERS)))) {
        return tCExecution;
    }
    LOG.debug("Checks performed -- > OK to continue.");
    /**
     * For GUI application, check if Browser is supported.
     */
    if (!tCExecution.getManualExecution().equals("Y") && tCExecution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_GUI)) {
        try {
            myInvariant = invariantService.convert(invariantService.readByKey("BROWSER", tCExecution.getBrowser()));
        } catch (CerberusException ex) {
            MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_BROWSER_NOT_SUPPORTED);
            mes.setDescription(mes.getDescription().replace("%BROWSER%", tCExecution.getBrowser()));
            LOG.debug(mes.getDescription());
            throw new CerberusException(mes);
        }
    }
    /**
     * Start server if execution is not manual
     */
    if (!tCExecution.getManualExecution().equals("Y")) {
        tCExecution.setResultMessage(new MessageGeneral(MessageGeneralEnum.EXECUTION_PE_STARTINGROBOTSERVER));
        if (tCExecution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_GUI) || tCExecution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_APK) || tCExecution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_IPA) || tCExecution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_FAT)) {
            if (tCExecution.getIp().equalsIgnoreCase("")) {
                MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_SELENIUM_EMPTYORBADIP);
                mes.setDescription(mes.getDescription().replace("%IP%", tCExecution.getIp()));
                LOG.debug(mes.getDescription());
                throw new CerberusException(mes);
            }
            /**
             * Start Selenium server
             */
            LOG.debug("Starting Server.");
            tCExecution.setResultMessage(new MessageGeneral(MessageGeneralEnum.EXECUTION_PE_CREATINGRUNID));
            try {
                this.serverService.startServer(tCExecution);
            } catch (CerberusException ex) {
                LOG.debug(ex.getMessageError().getDescription());
                throw new CerberusException(ex.getMessageError());
            }
            LOG.debug("Server Started.");
        }
    }
    /**
     * Register RunID inside database.
     */
    tCExecution.setResultMessage(new MessageGeneral(MessageGeneralEnum.EXECUTION_PE_CREATINGRUNID));
    LOG.debug("Registering Execution ID on database");
    long runID = 0;
    try {
        runID = this.testCaseExecutionService.registerRunID(tCExecution);
        if (runID != 0) {
            tCExecution.setId(runID);
            executionUUIDObject.setExecutionUUID(tCExecution.getExecutionUUID(), tCExecution);
            // Update Queue Execution here if QueueID =! 0.
            if (tCExecution.getQueueID() != 0) {
                inQueueService.updateToExecuting(tCExecution.getQueueID(), "", runID);
            }
        } else {
            MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_COULDNOTCREATE_RUNID);
            tCExecution.setResultMessage(mes);
            LOG.fatal("Could not create RunID, or cannot retreive the generated Key");
            throw new CerberusException(mes);
        }
    } catch (CerberusException ex) {
        tCExecution.setResultMessage(new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_COULDNOTCREATE_RUNID));
        LOG.warn(ex.getMessageError().getDescription());
        throw new CerberusException(ex.getMessageError());
    }
    LOG.debug("Execution ID registered on database : " + tCExecution.getId());
    /**
     * Stop the browser if executionID is equal to zero (to prevent database
     * instabilities)
     */
    if (!tCExecution.getManualExecution().equals("Y")) {
        try {
            if (tCExecution.getId() == 0) {
                LOG.debug("Starting to Stop the Selenium Server.");
                this.serverService.stopServer(tCExecution.getSession());
                LOG.debug("Selenium Server stopped.");
            }
        } catch (Exception ex) {
            LOG.warn(ex.toString());
        }
    }
    /**
     * Feature Flipping. Should be removed when websocket push is fully
     * working
     */
    tCExecution.setCerberus_featureflipping_activatewebsocketpush(parameterService.getParameterBooleanByKey("cerberus_featureflipping_activatewebsocketpush", "", false));
    tCExecution.setCerberus_featureflipping_websocketpushperiod(parameterService.getParameterLongByKey("cerberus_featureflipping_websocketpushperiod", "", 5000));
    return tCExecution;
}
Also used : Invariant(org.cerberus.crud.entity.Invariant) CerberusException(org.cerberus.exception.CerberusException) MessageGeneral(org.cerberus.engine.entity.MessageGeneral) TestCase(org.cerberus.crud.entity.TestCase) IFactoryCountryEnvironmentParameters(org.cerberus.crud.factory.IFactoryCountryEnvironmentParameters) CountryEnvironmentParameters(org.cerberus.crud.entity.CountryEnvironmentParameters) CountryEnvParam(org.cerberus.crud.entity.CountryEnvParam) Date(java.util.Date) CerberusException(org.cerberus.exception.CerberusException)

Example 52 with TestCase

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

the class TestCaseDAO method findTestCaseByTestSystem.

@Override
public List<TestCase> findTestCaseByTestSystem(String test, String system) {
    List<TestCase> list = null;
    StringBuilder sb = new StringBuilder();
    sb.append("SELECT * FROM testcase tec join application app on tec.application = app.application ");
    sb.append(" WHERE tec.test = ? and app.system = ? ");
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + sb.toString());
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(sb.toString());
        try {
            preStat.setString(1, test);
            preStat.setString(2, system);
            ResultSet resultSet = preStat.executeQuery();
            try {
                list = new ArrayList<TestCase>();
                while (resultSet.next()) {
                    list.add(this.loadFromResultSet(resultSet));
                }
            } catch (SQLException exception) {
                LOG.error("Unable to execute query : " + exception.toString());
            } finally {
                resultSet.close();
            }
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
        } finally {
            preStat.close();
        }
    } catch (SQLException exception) {
        LOG.error("Unable to execute query : " + exception.toString());
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            LOG.warn(e.toString());
        }
    }
    return list;
}
Also used : TestCase(org.cerberus.crud.entity.TestCase) IFactoryTestCase(org.cerberus.crud.factory.IFactoryTestCase) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 53 with TestCase

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

the class TestCaseDAO method findTestCaseByTest.

@Override
public List<TestCase> findTestCaseByTest(String test) {
    List<TestCase> list = null;
    final String query = "SELECT * FROM testcase tec WHERE test = ?";
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query);
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            preStat.setString(1, test);
            ResultSet resultSet = preStat.executeQuery();
            try {
                list = new ArrayList<TestCase>();
                while (resultSet.next()) {
                    list.add(this.loadFromResultSet(resultSet));
                }
            } catch (SQLException exception) {
                LOG.error("Unable to execute query : " + exception.toString());
            } finally {
                resultSet.close();
            }
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
        } finally {
            preStat.close();
        }
    } catch (SQLException exception) {
        LOG.error("Unable to execute query : " + exception.toString());
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException exception) {
            LOG.warn(exception.toString());
        }
    }
    return list;
}
Also used : TestCase(org.cerberus.crud.entity.TestCase) IFactoryTestCase(org.cerberus.crud.factory.IFactoryTestCase) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 54 with TestCase

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

the class TestCaseDAO method readByVarious.

@Override
public AnswerList<List<TestCase>> readByVarious(String[] test, String[] idProject, String[] app, String[] creator, String[] implementer, String[] system, String[] campaign, String[] labelid, String[] priority, String[] group, String[] status, int length) {
    AnswerList answer = new AnswerList();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    List<TestCase> testCaseList = new ArrayList<TestCase>();
    StringBuilder query = new StringBuilder();
    query.append("SELECT * FROM testcase tec ");
    query.append("LEFT JOIN application app ON tec.application = app.application ");
    if ((labelid != null) || (campaign != null)) {
        query.append("LEFT JOIN testcaselabel tel ON tec.test = tel.test AND tec.testcase = tel.testcase ");
        query.append("LEFT JOIN campaignlabel cpl ON cpl.labelId = tel.labelId ");
    }
    query.append("WHERE 1=1 AND tec.tcactive = 'Y' ");
    query.append(createInClauseFromList(test, "tec.test", "AND ", " "));
    query.append(createInClauseFromList(idProject, "tec.project", "AND ", " "));
    query.append(createInClauseFromList(app, "tec.application", "AND ", " "));
    query.append(createInClauseFromList(creator, "tec.usrCreated", "AND ", " "));
    query.append(createInClauseFromList(implementer, "tec.implementer", "AND ", " "));
    query.append(createInClauseFromList(priority, "tec.priority", "AND ", " "));
    query.append(createInClauseFromList(group, "tec.group", "AND ", " "));
    query.append(createInClauseFromList(status, "tec.status", "AND ", " "));
    query.append(createInClauseFromList(system, "app.system", "AND ", " "));
    query.append(createInClauseFromList(labelid, "tel.labelid", "AND ", " "));
    if (campaign != null) {
        query.append(createInClauseFromList(campaign, "cpl.campaign", " AND (", ") "));
    }
    query.append("GROUP BY tec.test, tec.testcase ");
    if (length != -1) {
        query.append("LIMIT ?");
    }
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query.toString());
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        if (length != -1) {
            preStat.setInt(1, length);
        }
        try {
            ResultSet resultSet = preStat.executeQuery();
            try {
                // gets the data
                while (resultSet.next()) {
                    testCaseList.add(this.loadFromResultSet(resultSet));
                }
                if (testCaseList.size() >= MAX_ROW_SELECTED) {
                    // Result of SQl was limited by MAX_ROW_SELECTED constrain. That means that we may miss some lines in the resultList.
                    LOG.error("Partial Result in the query.");
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_WARNING_PARTIAL_RESULT);
                    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Maximum row reached : " + MAX_ROW_SELECTED));
                    answer = new AnswerList(testCaseList, testCaseList.size());
                } else if (testCaseList.size() <= 0) {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                    answer = new AnswerList(testCaseList, testCaseList.size());
                } else {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
                    answer = new AnswerList(testCaseList, testCaseList.size());
                }
            } catch (SQLException exception) {
                LOG.error("Unable to execute query : " + exception.toString());
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
                msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
            } finally {
                if (resultSet != null) {
                    resultSet.close();
                }
            }
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
        } finally {
            if (preStat != null) {
                preStat.close();
            }
        }
    } catch (SQLException exception) {
        LOG.error("Unable to execute query : " + exception.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
        msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
    } finally {
        try {
            if (!this.databaseSpring.isOnTransaction()) {
                if (connection != null) {
                    connection.close();
                }
            }
        } catch (SQLException exception) {
            LOG.warn("Unable to close connection : " + exception.toString());
        }
    }
    answer.setResultMessage(msg);
    answer.setDataList(testCaseList);
    return answer;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) TestCase(org.cerberus.crud.entity.TestCase) IFactoryTestCase(org.cerberus.crud.factory.IFactoryTestCase) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 55 with TestCase

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

the class TestCaseDAO method readTestCaseByStepsInLibrary.

@Override
public AnswerList readTestCaseByStepsInLibrary(String test) {
    AnswerList response = new AnswerList();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
    List<TestCase> list = new ArrayList<TestCase>();
    StringBuilder query = new StringBuilder();
    query.append("SELECT * FROM testcase tec ");
    query.append("LEFT OUTER JOIN application app ON app.application=tec.application ");
    query.append("INNER JOIN testcasestep  tcs ON tec.test = tcs.test and tec.testcase = tcs.testcase ");
    query.append("WHERE tec.test= ? and (tcs.inlibrary = 'Y' or tcs.inlibrary = 'y') ");
    query.append("GROUP BY tec.testcase order by tec.testcase ");
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query.toString());
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        try {
            preStat.setString(1, test);
            ResultSet resultSet = preStat.executeQuery();
            try {
                list = new ArrayList<TestCase>();
                while (resultSet.next()) {
                    list.add(loadFromResultSet(resultSet));
                }
                if (list.size() >= MAX_ROW_SELECTED) {
                    // Result of SQl was limited by MAX_ROW_SELECTED constrain. That means that we may miss some lines in the resultList.
                    LOG.error("Partial Result in the query.");
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_WARNING_PARTIAL_RESULT);
                    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Maximum row reached : " + MAX_ROW_SELECTED));
                    response = new AnswerList(list, list.size());
                } else if (list.size() <= 0) {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                    response = new AnswerList(list, list.size());
                } else {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
                    response = new AnswerList(list, list.size());
                }
            } catch (SQLException exception) {
                LOG.error("Unable to execute query : " + exception.toString());
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
                msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
            } finally {
                if (resultSet != null) {
                    resultSet.close();
                }
            }
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
        } finally {
            if (preStat != null) {
                preStat.close();
            }
        }
    } catch (SQLException exception) {
        LOG.error("Unable to execute query : " + exception.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
        msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            LOG.warn(e.toString());
        }
    }
    response.setDataList(list);
    response.setResultMessage(msg);
    return response;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) TestCase(org.cerberus.crud.entity.TestCase) IFactoryTestCase(org.cerberus.crud.factory.IFactoryTestCase) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

TestCase (org.cerberus.crud.entity.TestCase)68 ITestCaseService (org.cerberus.crud.service.ITestCaseService)30 ArrayList (java.util.ArrayList)29 IFactoryTestCase (org.cerberus.crud.factory.IFactoryTestCase)29 JSONObject (org.json.JSONObject)26 AnswerItem (org.cerberus.util.answer.AnswerItem)24 ApplicationContext (org.springframework.context.ApplicationContext)24 MessageEvent (org.cerberus.engine.entity.MessageEvent)23 SQLException (java.sql.SQLException)17 TestCaseCountry (org.cerberus.crud.entity.TestCaseCountry)17 JSONArray (org.json.JSONArray)17 PolicyFactory (org.owasp.html.PolicyFactory)17 Connection (java.sql.Connection)16 PreparedStatement (java.sql.PreparedStatement)16 ResultSet (java.sql.ResultSet)16 CerberusException (org.cerberus.exception.CerberusException)16 TestCaseStep (org.cerberus.crud.entity.TestCaseStep)15 ILogEventService (org.cerberus.crud.service.ILogEventService)15 AnswerList (org.cerberus.util.answer.AnswerList)15 List (java.util.List)13