Search in sources :

Example 6 with RobotCapability

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

the class RobotCapabilityServiceTest method dummyReadByRobot.

private AnswerList<RobotCapability> dummyReadByRobot() {
    AnswerList<RobotCapability> answer = new AnswerList<>(new MessageEvent(MessageEventEnum.DATA_OPERATION_OK));
    answer.setDataList(EXISTING_CAPABILITIES);
    return answer;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) MessageEvent(org.cerberus.engine.entity.MessageEvent) RobotCapability(org.cerberus.crud.entity.RobotCapability)

Example 7 with RobotCapability

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

the class CreateRobot method processRequest.

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 * @throws org.cerberus.exception.CerberusException
 * @throws org.json.JSONException
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, CerberusException, JSONException {
    JSONObject jsonResponse = new JSONObject();
    Answer ans = new Answer();
    Gson gson = new Gson();
    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");
    String charset = request.getCharacterEncoding();
    /**
     * Parsing and securing all required parameters.
     */
    // Parameter that are already controled by GUI (no need to decode) --> We SECURE them
    // Parameter that needs to be secured --> We SECURE+DECODE them
    String robot = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("robot"), null, charset);
    String port = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("port"), null, charset);
    String platform = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("platform"), null, charset);
    String browser = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("browser"), null, charset);
    String version = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("version"), "", charset);
    String active = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("active"), "Y", charset);
    String description = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("description"), "", charset);
    String userAgent = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("useragent"), "", charset);
    String screenSize = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("screensize"), "", charset);
    String robotDecli = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("robotDecli"), "", charset);
    String hostUser = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("hostUsername"), null, charset);
    String hostPassword = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("hostPassword"), null, charset);
    List<RobotCapability> capabilities = (List<RobotCapability>) (request.getParameter("capabilities") == null ? Collections.emptyList() : gson.fromJson(request.getParameter("capabilities"), new TypeToken<List<RobotCapability>>() {
    }.getType()));
    // Parameter that we cannot secure as we need the html --> We DECODE them
    String host = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("host"), null, charset);
    // Securing capabilities by setting them the associated robot name
    // Check also if there is no duplicated capability
    Map<String, Object> capabilityMap = new HashMap<String, Object>();
    for (RobotCapability capability : capabilities) {
        capabilityMap.put(capability.getCapability(), null);
        capability.setRobot(robot);
    }
    Integer robotid = 0;
    boolean robotid_error = false;
    try {
        if (request.getParameter("robotid") != null && !request.getParameter("robotid").equals("")) {
            robotid = Integer.valueOf(policy.sanitize(request.getParameter("robotid")));
        }
    } catch (Exception ex) {
        robotid_error = true;
    }
    /**
     * Checking all constrains before calling the services.
     */
    if (StringUtil.isNullOrEmpty(robot)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Robot").replace("%OPERATION%", "Create").replace("%REASON%", "Robot name is missing."));
        ans.setResultMessage(msg);
    } else if (StringUtil.isNullOrEmpty(host)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Robot").replace("%OPERATION%", "Create").replace("%REASON%", "Robot host is missing."));
        ans.setResultMessage(msg);
    } else if (StringUtil.isNullOrEmpty(platform)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Robot").replace("%OPERATION%", "Create").replace("%REASON%", "Robot platform is missing."));
        ans.setResultMessage(msg);
    } else if (robotid_error) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Robot").replace("%OPERATION%", "Create").replace("%REASON%", "Could not manage to convert robotid to an integer value or robotid is missing."));
        ans.setResultMessage(msg);
    } else if (capabilityMap.size() != capabilities.size()) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Robot").replace("%OPERATION%", "Create").replace("%REASON%", "There is at least one duplicated capability. Please edit or remove it to continue."));
        ans.setResultMessage(msg);
    } else {
        /**
         * All data seems cleans so we can call the services.
         */
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        IRobotService robotService = appContext.getBean(IRobotService.class);
        IFactoryRobot robotFactory = appContext.getBean(IFactoryRobot.class);
        Robot robotData = robotFactory.create(robotid, robot, host, port, platform, browser, version, active, description, userAgent, screenSize, hostUser, hostPassword, capabilities, robotDecli);
        ans = robotService.create(robotData);
        if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
            /**
             * Object created. Adding Log entry.
             */
            ILogEventService logEventService = appContext.getBean(LogEventService.class);
            logEventService.createForPrivateCalls("/CreateRobot", "CREATE", "Create Robot : ['" + robot + "']", 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 : PolicyFactory(org.owasp.html.PolicyFactory) HashMap(java.util.HashMap) MessageEvent(org.cerberus.engine.entity.MessageEvent) Gson(com.google.gson.Gson) ServletException(javax.servlet.ServletException) JSONException(org.json.JSONException) IOException(java.io.IOException) CerberusException(org.cerberus.exception.CerberusException) IRobotService(org.cerberus.crud.service.IRobotService) Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) IFactoryRobot(org.cerberus.crud.factory.IFactoryRobot) JSONObject(org.json.JSONObject) ILogEventService(org.cerberus.crud.service.ILogEventService) List(java.util.List) JSONObject(org.json.JSONObject) RobotCapability(org.cerberus.crud.entity.RobotCapability) IFactoryRobot(org.cerberus.crud.factory.IFactoryRobot) Robot(org.cerberus.crud.entity.Robot)

Example 8 with RobotCapability

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

the class RobotCapabilityDAO method readByRobot.

@Override
public AnswerList<RobotCapability> readByRobot(String robot) {
    AnswerList<RobotCapability> ans = new AnswerList<>();
    MessageEvent msg = null;
    LOG.debug(Query.READ_BY_ROBOT);
    try (Connection connection = databaseSpring.connect();
        PreparedStatement preStat = connection.prepareStatement(Query.READ_BY_ROBOT)) {
        // Prepare and execute query
        preStat.setString(1, robot);
        try (ResultSet resultSet = preStat.executeQuery()) {
            // Parse query
            List<RobotCapability> result = new ArrayList<>();
            while (resultSet.next()) {
                result.add(loadFromResultSet(resultSet));
            }
            ans.setDataList(result);
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK).resolveDescription("ITEM", OBJECT_NAME).resolveDescription("OPERATION", "SELECT");
        }
    } catch (Exception e) {
        LOG.warn("Unable to execute query : " + e.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED).resolveDescription("DESCRIPTION", e.toString());
    } finally {
        // We always set the result message
        ans.setResultMessage(msg);
    }
    return ans;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) IFactoryRobotCapability(org.cerberus.crud.factory.IFactoryRobotCapability) RobotCapability(org.cerberus.crud.entity.RobotCapability) SQLException(java.sql.SQLException)

Example 9 with RobotCapability

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

the class RobotCapabilityServiceTest method beforeClass.

@BeforeClass
public static void beforeClass() {
    RobotCapability rc1 = new RobotCapability();
    rc1.setRobot(ROBOT_NAME);
    rc1.setCapability("capability(1)");
    rc1.setValue("value(1)");
    RobotCapability rc2 = new RobotCapability();
    rc2.setRobot(ROBOT_NAME);
    rc2.setCapability("capability(2)");
    rc2.setValue("value(2)");
    RobotCapability rc3 = new RobotCapability();
    rc3.setRobot(ROBOT_NAME);
    rc3.setCapability("capability(3)");
    rc3.setValue("value(3)");
    RobotCapability rc4 = new RobotCapability();
    rc4.setRobot(ROBOT_NAME);
    rc4.setCapability("capability(4)");
    rc4.setValue("value(4)");
    EXISTING_CAPABILITIES = Arrays.asList(rc1, rc2, rc3, rc4);
}
Also used : RobotCapability(org.cerberus.crud.entity.RobotCapability) BeforeClass(org.junit.BeforeClass)

Example 10 with RobotCapability

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

the class RobotCapabilityServiceTest method testCompareListAndUpdateInsertDeleteElements.

@Test
public void testCompareListAndUpdateInsertDeleteElements() {
    when(robotCapabilityDAO.readByRobot(anyString())).thenReturn(dummyReadByRobot());
    RobotCapability rc1 = new RobotCapability();
    rc1.setRobot(ROBOT_NAME);
    rc1.setCapability("capability(1)");
    rc1.setValue("value(1)");
    RobotCapability rc2 = new RobotCapability();
    rc2.setRobot(ROBOT_NAME);
    rc2.setCapability("capability(2)");
    rc2.setValue("value(2 *changed*)");
    RobotCapability rc3 = new RobotCapability();
    rc3.setRobot(ROBOT_NAME);
    rc3.setCapability("capability(3)");
    rc3.setValue("value(3)");
    RobotCapability rc4 = new RobotCapability();
    rc4.setRobot(ROBOT_NAME);
    rc4.setCapability("capability(4)");
    rc4.setValue("value(4)");
    RobotCapability rc5 = new RobotCapability();
    rc5.setRobot(ROBOT_NAME);
    rc5.setCapability("capability(5)");
    rc5.setValue("value(5)");
    RobotCapability rc6 = new RobotCapability();
    rc5.setRobot(ROBOT_NAME);
    rc5.setCapability("capability(6)");
    rc5.setValue("value(6)");
    robotCapabilityService.compareListAndUpdateInsertDeleteElements(ROBOT_NAME, Arrays.asList(rc1, rc2, rc3, rc5, rc6));
    verify(robotCapabilityDAO).create(rc5);
    verify(robotCapabilityDAO).create(rc6);
    verify(robotCapabilityDAO).update(rc2);
    verify(robotCapabilityDAO).delete(rc4);
}
Also used : RobotCapability(org.cerberus.crud.entity.RobotCapability) Test(org.junit.Test)

Aggregations

RobotCapability (org.cerberus.crud.entity.RobotCapability)11 MessageEvent (org.cerberus.engine.entity.MessageEvent)5 Answer (org.cerberus.util.answer.Answer)4 IOException (java.io.IOException)3 ServletException (javax.servlet.ServletException)3 Robot (org.cerberus.crud.entity.Robot)3 ILogEventService (org.cerberus.crud.service.ILogEventService)3 IRobotService (org.cerberus.crud.service.IRobotService)3 CerberusException (org.cerberus.exception.CerberusException)3 ApplicationContext (org.springframework.context.ApplicationContext)3 Gson (com.google.gson.Gson)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 IFactoryRobotCapability (org.cerberus.crud.factory.IFactoryRobotCapability)2 AnswerList (org.cerberus.util.answer.AnswerList)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 PrintWriter (java.io.PrintWriter)1 Connection (java.sql.Connection)1