Search in sources :

Example 6 with ConstraintViolation

use of jakarta.validation.ConstraintViolation in project spring-cloud-config by spring-cloud.

the class SshPropertyValidatorTest method supportedParametersSuccesful.

@Test
public void supportedParametersSuccesful() throws Exception {
    MultipleJGitEnvironmentProperties validSettings = new MultipleJGitEnvironmentProperties();
    validSettings.setUri(SSH_URI);
    validSettings.setIgnoreLocalSshSettings(true);
    validSettings.setPrivateKey(VALID_PRIVATE_KEY);
    validSettings.setHostKey(VALID_HOST_KEY);
    validSettings.setHostKeyAlgorithm("ssh-rsa");
    Set<ConstraintViolation<MultipleJGitEnvironmentProperties>> constraintViolations = validator.validate(validSettings);
    assertThat(constraintViolations).hasSize(0);
}
Also used : ConstraintViolation(jakarta.validation.ConstraintViolation) MultipleJGitEnvironmentProperties(org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentProperties) Test(org.junit.Test)

Example 7 with ConstraintViolation

use of jakarta.validation.ConstraintViolation in project spring-cloud-config by spring-cloud.

the class SshPropertyValidatorTest method unsupportedAlgoFails.

@Test
public void unsupportedAlgoFails() throws Exception {
    MultipleJGitEnvironmentProperties unsupportedAlgo = new MultipleJGitEnvironmentProperties();
    unsupportedAlgo.setUri(SSH_URI);
    unsupportedAlgo.setIgnoreLocalSshSettings(true);
    unsupportedAlgo.setPrivateKey(VALID_PRIVATE_KEY);
    unsupportedAlgo.setHostKey("some_host_key");
    unsupportedAlgo.setHostKeyAlgorithm("unsupported");
    Set<ConstraintViolation<MultipleJGitEnvironmentProperties>> constraintViolations = validator.validate(unsupportedAlgo);
    assertThat(constraintViolations).hasSize(1);
}
Also used : ConstraintViolation(jakarta.validation.ConstraintViolation) MultipleJGitEnvironmentProperties(org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentProperties) Test(org.junit.Test)

Example 8 with ConstraintViolation

use of jakarta.validation.ConstraintViolation in project hibernate-orm by hibernate.

the class HibernateTraversableResolverTest method testEmbedded.

@Test
public void testEmbedded() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Screen screen = new Screen();
    PowerSupply ps = new PowerSupply();
    screen.setPowerSupply(ps);
    Button button = new Button();
    button.setName(null);
    button.setSize(3);
    screen.setStopButton(button);
    try {
        s.persist(screen);
        s.flush();
        fail("@NotNull on embedded property is not evaluated");
    } catch (ConstraintViolationException e) {
        assertEquals(1, e.getConstraintViolations().size());
        ConstraintViolation<?> cv = e.getConstraintViolations().iterator().next();
        assertEquals(Screen.class, cv.getRootBeanClass());
        // toString works since hibernate validator's Path implementation works accordingly. Should do a Path comparison though
        assertEquals("stopButton.name", cv.getPropertyPath().toString());
    }
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolation(jakarta.validation.ConstraintViolation) ConstraintViolationException(jakarta.validation.ConstraintViolationException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 9 with ConstraintViolation

use of jakarta.validation.ConstraintViolation in project hibernate-orm by hibernate.

the class HibernateTraversableResolverTest method testEmbeddedCollection.

@Test
public void testEmbeddedCollection() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Screen screen = new Screen();
    PowerSupply ps = new PowerSupply();
    screen.setPowerSupply(ps);
    DisplayConnector conn = new DisplayConnector();
    conn.setNumber(0);
    screen.getConnectors().add(conn);
    try {
        s.persist(screen);
        s.flush();
        fail("Collection of embedded objects should be validated");
    } catch (ConstraintViolationException e) {
        assertEquals(1, e.getConstraintViolations().size());
        final ConstraintViolation constraintViolation = e.getConstraintViolations().iterator().next();
        assertEquals(Screen.class, constraintViolation.getRootBeanClass());
        // toString works since hibernate validator's Path implementation works accordingly. Should do a Path comparison though
        assertEquals("connectors[].number", constraintViolation.getPropertyPath().toString());
    }
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolation(jakarta.validation.ConstraintViolation) ConstraintViolationException(jakarta.validation.ConstraintViolationException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 10 with ConstraintViolation

use of jakarta.validation.ConstraintViolation in project hibernate-orm by hibernate.

the class HibernateTraversableResolverTest method testToOneAssocNotValidated.

@Test
public void testToOneAssocNotValidated() {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Screen screen = new Screen();
    PowerSupply ps = new PowerSupply();
    ps.setPosition("1");
    ps.setPower(new BigDecimal(350));
    screen.setPowerSupply(ps);
    try {
        s.persist(screen);
        s.flush();
        fail("Associated objects should not be validated");
    } catch (ConstraintViolationException e) {
        assertEquals(1, e.getConstraintViolations().size());
        final ConstraintViolation constraintViolation = e.getConstraintViolations().iterator().next();
        assertEquals(PowerSupply.class, constraintViolation.getRootBeanClass());
    }
    tx.rollback();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolation(jakarta.validation.ConstraintViolation) ConstraintViolationException(jakarta.validation.ConstraintViolationException) BigDecimal(java.math.BigDecimal) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

ConstraintViolation (jakarta.validation.ConstraintViolation)397 Test (org.testng.annotations.Test)309 Validator (jakarta.validation.Validator)258 TestForIssue (org.hibernate.validator.testutil.TestForIssue)134 HibernateValidator (org.hibernate.validator.HibernateValidator)123 ValidatorUtil.getValidator (org.hibernate.validator.testutils.ValidatorUtil.getValidator)70 ExecutableValidator (jakarta.validation.executable.ExecutableValidator)60 ConstraintValidator (jakarta.validation.ConstraintValidator)57 ConstraintMapping (org.hibernate.validator.cfg.ConstraintMapping)54 Method (java.lang.reflect.Method)46 ValidatorFactory (jakarta.validation.ValidatorFactory)41 Test (org.junit.Test)39 ConstraintViolationException (jakarta.validation.ConstraintViolationException)35 Path (jakarta.validation.Path)35 NotNull (jakarta.validation.constraints.NotNull)32 HibernateValidatorConfiguration (org.hibernate.validator.HibernateValidatorConfiguration)27 NotNullDef (org.hibernate.validator.cfg.defs.NotNullDef)19 SizeDef (org.hibernate.validator.cfg.defs.SizeDef)18 Size (jakarta.validation.constraints.Size)17 PropertyNode (org.hibernate.validator.path.PropertyNode)14