Search in sources :

Example 26 with Validator

use of javax.validation.Validator in project keywhiz by square.

the class MigrationsRule method apply.

@Override
public Statement apply(final Statement base, Description description) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            File yamlFile = new File(Resources.getResource("keywhiz-test.yaml").getFile());
            Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
            ObjectMapper objectMapper = KeywhizService.customizeObjectMapper(Jackson.newObjectMapper());
            KeywhizConfig config = new ConfigurationFactory<>(KeywhizConfig.class, validator, objectMapper, "dw").build(yamlFile);
            DataSource dataSource = config.getDataSourceFactory().build(new MetricRegistry(), "db-migrations");
            Flyway flyway = new Flyway();
            flyway.setDataSource(dataSource);
            flyway.setLocations(config.getMigrationsDir());
            flyway.clean();
            flyway.migrate();
            DSLContext dslContext = DSLContexts.databaseAgnostic(dataSource);
            DbSeedCommand.doImport(dslContext);
            base.evaluate();
        }
    };
}
Also used : Flyway(org.flywaydb.core.Flyway) Statement(org.junit.runners.model.Statement) MetricRegistry(com.codahale.metrics.MetricRegistry) DSLContext(org.jooq.DSLContext) File(java.io.File) Validator(javax.validation.Validator) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DataSource(javax.sql.DataSource)

Example 27 with Validator

use of javax.validation.Validator in project keywhiz by square.

the class LdapLookupConfigTest method parsesLDAPLookupCorrectly.

@Test
public void parsesLDAPLookupCorrectly() throws Exception {
    File yamlFile = new File(Resources.getResource("fixtures/keywhiz-ldap-lookup-test.yaml").getFile());
    Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
    ObjectMapper objectMapper = bootstrap.getObjectMapper().copy();
    LdapLookupConfig lookupConfig = new ConfigurationFactory<>(LdapLookupConfig.class, validator, objectMapper, "dw").build(yamlFile);
    assertThat(lookupConfig.getRequiredRoles()).containsOnly("keywhizAdmins");
    assertThat(lookupConfig.getRoleBaseDN()).isEqualTo("ou=ApplicationAccess,dc=test,dc=com");
    assertThat(lookupConfig.getUserBaseDN()).isEqualTo("ou=people,dc=test,dc=com");
    assertThat(lookupConfig.getUserAttribute()).isEqualTo("uid");
}
Also used : LdapLookupConfig(keywhiz.auth.ldap.LdapLookupConfig) File(java.io.File) Validator(javax.validation.Validator) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 28 with Validator

use of javax.validation.Validator in project wildfly by wildfly.

the class LazyValidatorFactoryTestCase method testHibernateValidatorIsUsedAsProviderByDefault.

@Test
public void testHibernateValidatorIsUsedAsProviderByDefault() {
    HibernateValidatorFactory hibernateValidatorFactory = validatorFactory.unwrap(HibernateValidatorFactory.class);
    assertNotNull("LazyValidatorFactory should delegate to the HV factory by default", hibernateValidatorFactory);
    Validator validator = validatorFactory.getValidator();
    assertNotNull("LazyValidatorFactory should provide a validator", validator);
}
Also used : HibernateValidatorFactory(org.hibernate.validator.HibernateValidatorFactory) Validator(javax.validation.Validator) Test(org.junit.Test)

Example 29 with Validator

use of javax.validation.Validator in project wildfly by wildfly.

the class LazyValidatorFactoryTestCase method testSpecificProviderCanBeConfiguredInValidationXml.

@Test
@WithContextClassLoader(TestClassLoader.class)
public void testSpecificProviderCanBeConfiguredInValidationXml() {
    Validator validator = validatorFactory.getValidator();
    assertNotNull("LazyValidatorFactory should provide a validator", validator);
    assertTrue("Validator should be of type created by XML-configured provider", validator instanceof MyValidatorImpl);
}
Also used : MyValidatorImpl(org.jboss.as.ee.beanvalidation.testprovider.MyValidatorImpl) Validator(javax.validation.Validator) Test(org.junit.Test) WithContextClassLoader(org.jboss.as.ee.beanvalidation.testutil.WithContextClassLoader)

Example 30 with Validator

use of javax.validation.Validator in project wildfly by wildfly.

the class BootStrapValidationTestCase method testCustomConstraintValidatorFactory.

@Test
public void testCustomConstraintValidatorFactory() {
    HibernateValidatorConfiguration configuration = Validation.byProvider(HibernateValidator.class).configure();
    assertNotNull(configuration);
    ValidatorFactory factory = configuration.buildValidatorFactory();
    Validator validator = factory.getValidator();
    Employee emp = new Employee();
    // create employee
    emp.setEmpId("M1234");
    emp.setFirstName("MADHUMITA");
    emp.setLastName("SADHUKHAN");
    emp.setEmail("madhu@redhat.com");
    Set<ConstraintViolation<Employee>> constraintViolations = validator.validate(emp);
    assertEquals("Wrong number of constraints", constraintViolations.size(), 1);
    assertEquals("Created by default factory", constraintViolations.iterator().next().getMessage());
    // get a new factory using a custom configuration
    configuration.constraintValidatorFactory(new CustomConstraintValidatorFactory(configuration.getDefaultConstraintValidatorFactory()));
    factory = configuration.buildValidatorFactory();
    validator = factory.getValidator();
    constraintViolations = validator.validate(emp);
    assertEquals("Wrong number of constraints", constraintViolations.size(), 1);
    assertEquals("Created by custom factory", constraintViolations.iterator().next().getMessage());
}
Also used : HibernateValidator(org.hibernate.validator.HibernateValidator) ValidatorFactory(javax.validation.ValidatorFactory) ConstraintValidatorFactory(javax.validation.ConstraintValidatorFactory) HibernateValidatorFactory(org.hibernate.validator.HibernateValidatorFactory) ConstraintViolation(javax.validation.ConstraintViolation) HibernateValidatorConfiguration(org.hibernate.validator.HibernateValidatorConfiguration) ConstraintValidator(javax.validation.ConstraintValidator) Validator(javax.validation.Validator) HibernateValidator(org.hibernate.validator.HibernateValidator) Test(org.junit.Test)

Aggregations

Validator (javax.validation.Validator)42 Test (org.junit.Test)25 ConstraintViolation (javax.validation.ConstraintViolation)19 ValidatorFactory (javax.validation.ValidatorFactory)13 InitialContext (javax.naming.InitialContext)8 ArrayList (java.util.ArrayList)5 HibernateValidator (org.hibernate.validator.HibernateValidator)5 File (java.io.File)4 NamingException (javax.naming.NamingException)4 ConstraintViolationException (javax.validation.ConstraintViolationException)4 Title (net.thucydides.core.annotations.Title)4 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)4 PodamFactory (uk.co.jemos.podam.api.PodamFactory)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 HashSet (java.util.HashSet)3 Context (javax.naming.Context)3 OpenEJBException (org.apache.openejb.OpenEJBException)3 HibernateValidatorFactory (org.hibernate.validator.HibernateValidatorFactory)3 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2