package org.richfaces.testng;

import org.ajax4jsf.template.Template;
import org.richfaces.SeleniumTestBase;
import org.testng.annotations.Test;

public class AjaxValidatorTest extends SeleniumTestBase {

    private static final String NAME = "name";

    private static final String AGE = "age";

    private static final String EMAIL = "email";

    private static final String ERR_MSG_POSTFIX = "_err_msg";

    @Test
    public void testAjaxValidatorComponentWithStandartValidators(Template template) {
        renderPage(template);

        writeStatus("Check ajax validator component with standart validators. "
                + "Validation has to be occured by javascript event 'onblur'");
        String parentId = getParentId() + "_form:";

        String tfNameId = parentId + NAME;
        String tfAgeId = parentId + AGE;

        String tfNameErrMsg = tfNameId + ERR_MSG_POSTFIX;
        String tfAgeErrMsg = tfAgeId + ERR_MSG_POSTFIX;

        assertNotPresent(tfNameErrMsg);
        assertNotPresent(tfAgeErrMsg);

        writeStatus("Name must be between 3 and 12 at length. Type shorter one");
        type(tfNameId, "Mi");
        selenium.fireEvent(tfNameId, "blur");
        waitForAjaxCompletion();
        assertPresent(tfNameErrMsg);

        writeStatus("Age must be greater than or equal to 18. Type less than that");
        type(tfAgeId, "3");
        selenium.fireEvent(tfAgeId, "blur");
        waitForAjaxCompletion();
        assertPresent(tfAgeErrMsg);

        writeStatus("Correct the inputs and leave. Error messages have to disappear");

        type(tfNameId, "Mick");
        selenium.fireEvent(tfNameId, "blur");
        waitForAjaxCompletion();
        assertNotPresent(tfNameErrMsg);

        type(tfAgeId, "33");
        selenium.fireEvent(tfAgeId, "blur");
        waitForAjaxCompletion();
        assertNotPresent(tfAgeErrMsg);

    }

    @Test
    public void testAjaxValidatorComponentWithHibernateValidator(Template template) {
        renderPage(template);

        writeStatus("Check ajax validator component with hibernate validator. "
                + "Validation has to be occured by javascript event 'onblur'");
        String parentId = getParentId() + "_form1:";

        String tfNameId = parentId + NAME;
        String tfAgeId = parentId + AGE;
        String tfEmailId = parentId + EMAIL;

        String tfNameErrMsg = tfNameId + ERR_MSG_POSTFIX;
        String tfAgeErrMsg = tfAgeId + ERR_MSG_POSTFIX;
        String tfEmailErrMsg = tfEmailId + ERR_MSG_POSTFIX;

        assertNotPresent(tfNameErrMsg);
        assertNotPresent(tfAgeErrMsg);
        assertNotPresent(tfEmailErrMsg);

        writeStatus("Name must be between 3 and 12 at length. Type shorter one");
        type(tfNameId, "Mi");
        selenium.fireEvent(tfNameId, "blur");
        waitForAjaxCompletion();
        assertPresent(tfNameErrMsg);

        writeStatus("Age must be between 18 and 100. Type less than that");
        type(tfAgeId, "3");
        selenium.fireEvent(tfAgeId, "blur");
        waitForAjaxCompletion();
        assertPresent(tfAgeErrMsg);

        writeStatus("Email must be an email. Type not a well-formed email");
        type(tfEmailId, "notemail");
        selenium.fireEvent(tfEmailId, "blur");
        waitForAjaxCompletion();
        assertPresent(tfEmailErrMsg);

        writeStatus("Correct the inputs and leave. Error messages have to disappear");

        type(tfNameId, "Mick");
        selenium.fireEvent(tfNameId, "blur");
        waitForAjaxCompletion();
        assertNotPresent(tfNameErrMsg);

        type(tfAgeId, "33");
        selenium.fireEvent(tfAgeId, "blur");
        waitForAjaxCompletion();
        assertNotPresent(tfAgeErrMsg);

        type(tfEmailId, "email@ya.com");
        selenium.fireEvent(tfEmailId, "blur");
        waitForAjaxCompletion();
        assertNotPresent(tfEmailErrMsg);

    }

    private void assertPresent(String id) {
        AssertTextNotEquals(id, "", "Message [" + id + "] must not be empty on the page");
    }

    private void assertNotPresent(String id) {
        AssertTextEquals(id, "", "Message [" + id + "] must be empty on the page");
    }

    @Override
    public String getTestUrl() {
        return "pages/ajaxValidator/ajaxValidatorTest.xhtml";
    }
}
