Search in sources :

Example 1 with Validator

use of org.jaffa.rules.fieldvalidators.Validator in project jaffa-framework by jaffa-projects.

the class BatchValidatorTest method testValidationException.

/**
 * Test that a validation exception is returned to the caller of the validation method
 *
 * @throws org.jaffa.exceptions.FrameworkException
 *
 * @throws org.jaffa.exceptions.ApplicationException
 */
@Test(expected = ApplicationException.class)
public void testValidationException() throws FrameworkException, ApplicationException {
    // validator that will throw exception on validation
    Validator mockValidator = mock(Validator.class);
    doThrow(new MandatoryFieldException()).when(mockValidator).validate(anyObject());
    // create bulk validator with a sub-validators that will throw an exception
    BatchValidator validator = new BatchValidator();
    validator.getValidatorSet().add(mockValidator);
    // call validate
    validator.validate(new Object());
}
Also used : MandatoryFieldException(org.jaffa.datatypes.exceptions.MandatoryFieldException) Matchers.anyObject(org.mockito.Matchers.anyObject) Validator(org.jaffa.rules.fieldvalidators.Validator) Test(org.junit.Test)

Example 2 with Validator

use of org.jaffa.rules.fieldvalidators.Validator in project jaffa-framework by jaffa-projects.

the class BatchValidatorTest method testValidation.

/**
 * Test that the bulk validator fires validate on all sub-validators
 *
 * @throws FrameworkException
 * @throws ApplicationException
 */
@Test
public void testValidation() throws FrameworkException, ApplicationException {
    // validators that will pass validation
    Validator mockValidator1 = mock(Validator.class);
    Validator mockValidator2 = mock(Validator.class);
    // create bulk validator with 2 sub-validators
    BatchValidator validator = new BatchValidator();
    validator.getValidatorSet().add(mockValidator1);
    validator.getValidatorSet().add(mockValidator2);
    // call validate
    Object object = new Object();
    validator.validate(object);
    // verify both sub-validators are called
    verify(mockValidator1).validate(object);
    verify(mockValidator2).validate(object);
}
Also used : Matchers.anyObject(org.mockito.Matchers.anyObject) Validator(org.jaffa.rules.fieldvalidators.Validator) Test(org.junit.Test)

Example 3 with Validator

use of org.jaffa.rules.fieldvalidators.Validator in project jaffa-framework by jaffa-projects.

the class PersistentConfigValidatorTest method testPersistentConfig.

/**
 * Test Persistent has a validator pushed into it during construction
 */
@Test
public void testPersistentConfig() throws NoSuchFieldException, IllegalAccessException {
    // creating a new instance of a persistent object should have its validator pushed into it
    // automatically by persistent config
    Persistent persistent = new TestModelPersistent();
    Field f = persistent.getClass().getSuperclass().getDeclaredField("validator");
    f.setAccessible(true);
    Validator validator = (Validator) f.get(persistent);
    assertNotNull(validator);
}
Also used : Field(java.lang.reflect.Field) Persistent(org.jaffa.persistence.Persistent) Validator(org.jaffa.rules.fieldvalidators.Validator) Test(org.junit.Test)

Example 4 with Validator

use of org.jaffa.rules.fieldvalidators.Validator in project jaffa-framework by jaffa-projects.

the class RuleValidatorFactoryTest method testNullValidator.

/**
 * Test to verify that a null validator is returned if there is no validator available
 * for the class name, realm, and variation provided.
 *
 * @throws Exception
 */
@Test
public void testNullValidator() throws Exception {
    // setup
    Object plainObj = new Object();
    // execute
    Validator validator = target.getValidator(plainObj);
    // asserts
    assertNull(validator);
}
Also used : Validator(org.jaffa.rules.fieldvalidators.Validator) Test(org.junit.Test)

Example 5 with Validator

use of org.jaffa.rules.fieldvalidators.Validator in project jaffa-framework by jaffa-projects.

the class RuleValidatorFactoryTest method testReturnValidator.

/**
 * Test that rule manager returns a validator
 *
 * @throws Exception
 */
@Test
public void testReturnValidator() throws Exception {
    target.addValidatorTypes("mandatory");
    Validator validator = target.getValidator(fakeModel);
    assertNotNull(validator);
    assertTrue(validator instanceof BatchValidator);
    BatchValidator domainModelValidator = (BatchValidator) validator;
    assertTrue(domainModelValidator.getValidatorSet().size() == 1);
    MandatoryValidator mandatoryValidator = (MandatoryValidator) domainModelValidator.getValidatorSet().iterator().next();
    assertNotNull(mandatoryValidator);
    assertEquals(fakeModel.getClass().getName(), mandatoryValidator.getActualClassName(fakeModel.getClass()));
    assertTrue(mandatoryValidator.getRuleMap().size() == 2);
}
Also used : Validator(org.jaffa.rules.fieldvalidators.Validator) Test(org.junit.Test)

Aggregations

Validator (org.jaffa.rules.fieldvalidators.Validator)8 Test (org.junit.Test)8 Field (java.lang.reflect.Field)2 Matchers.anyObject (org.mockito.Matchers.anyObject)2 TestPersistentBean (org.jaffa.beans.factory.config.TestPersistentBean)1 MandatoryFieldException (org.jaffa.datatypes.exceptions.MandatoryFieldException)1 FakeModel (org.jaffa.persistence.FakeModel)1 Persistent (org.jaffa.persistence.Persistent)1