Search in sources :

Example 1 with MessageInterpolatorContext

use of org.hibernate.validator.internal.engine.MessageInterpolatorContext in project graphql-java-extended-validation by graphql-java.

the class ResourceBundleMessageInterpolator method buildHibernateContext.

@SuppressWarnings("ConstantConditions")
private MessageInterpolatorContext buildHibernateContext(Map<String, Object> messageParams, ValidationEnvironment validationEnvironment) {
    Object validatedValue = validationEnvironment.getValidatedValue();
    ConstraintAnnotationDescriptor<BridgeAnnotation> annotationDescriptor = new ConstraintAnnotationDescriptor.Builder<>(BridgeAnnotation.class).build();
    ConstraintDescriptorImpl<BridgeAnnotation> constraintDescriptor = new ConstraintDescriptorImpl<>(ConstraintHelper.forAllBuiltinConstraints(), null, annotationDescriptor, ConstraintLocationKind.FIELD, ConstraintType.GENERIC);
    Map<String, Object> expressionVariables = StandardELVariables.standardELVars(validationEnvironment);
    Class<?> rootBeanType = null;
    Path propertyPath = null;
    return new MessageInterpolatorContext(constraintDescriptor, validatedValue, rootBeanType, propertyPath, messageParams, expressionVariables, ExpressionLanguageFeatureLevel.BEAN_PROPERTIES, true);
}
Also used : Path(jakarta.validation.Path) ResultPath(graphql.execution.ResultPath) MessageInterpolatorContext(org.hibernate.validator.internal.engine.MessageInterpolatorContext) ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) ConstraintDescriptorImpl(org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl)

Example 2 with MessageInterpolatorContext

use of org.hibernate.validator.internal.engine.MessageInterpolatorContext in project hibernate-validator by hibernate.

the class MessageInterpolatorContextTest method testContextWithRightDescriptorAndValueAndRootBeanClassIsPassedToMessageInterpolator.

@Test
@TestForIssue(jiraKey = "HV-333")
public void testContextWithRightDescriptorAndValueAndRootBeanClassIsPassedToMessageInterpolator() {
    // use a easy mock message interpolator for verifying that the right MessageInterpolatorContext
    // will be passed
    MessageInterpolator mock = createMock(MessageInterpolator.class);
    Configuration<?> config = ValidatorUtil.getConfiguration().messageInterpolator(mock);
    Validator validator = config.buildValidatorFactory().getValidator();
    BeanDescriptor beanDescriptor = validator.getConstraintsForClass(TestBean.class);
    PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty("test");
    Set<ConstraintDescriptor<?>> constraintDescriptors = propertyDescriptor.getConstraintDescriptors();
    assertTrue(constraintDescriptors.size() == 1);
    // prepare the mock interpolator to expect the right interpolate call
    String validatedValue = "value";
    expect(mock.interpolate(MESSAGE, new MessageInterpolatorContext(constraintDescriptors.iterator().next(), validatedValue, TestBean.class, null, Collections.<String, Object>emptyMap(), Collections.<String, Object>emptyMap(), ExpressionLanguageFeatureLevel.BEAN_METHODS, false))).andReturn("invalid");
    replay(mock);
    Set<ConstraintViolation<TestBean>> violations = validator.validate(new TestBean(validatedValue));
    assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("invalid"));
    // verify that the right context was passed
    verify(mock);
}
Also used : PropertyDescriptor(jakarta.validation.metadata.PropertyDescriptor) MessageInterpolatorContext(org.hibernate.validator.internal.engine.MessageInterpolatorContext) HibernateMessageInterpolatorContext(org.hibernate.validator.messageinterpolation.HibernateMessageInterpolatorContext) BeanDescriptor(jakarta.validation.metadata.BeanDescriptor) ConstraintDescriptor(jakarta.validation.metadata.ConstraintDescriptor) ConstraintViolation(jakarta.validation.ConstraintViolation) Validator(jakarta.validation.Validator) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) MessageInterpolator(jakarta.validation.MessageInterpolator) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 3 with MessageInterpolatorContext

use of org.hibernate.validator.internal.engine.MessageInterpolatorContext in project hibernate-validator by hibernate.

the class MessageInterpolatorContextTest method testGetPropertyPath.

@Test
@TestForIssue(jiraKey = "HV-1657")
public void testGetPropertyPath() {
    Path pathMock = createMock(Path.class);
    MessageInterpolator.Context context = new MessageInterpolatorContext(null, null, null, pathMock, Collections.<String, Object>emptyMap(), Collections.<String, Object>emptyMap(), ExpressionLanguageFeatureLevel.BEAN_METHODS, false);
    assertSame(context.unwrap(HibernateMessageInterpolatorContext.class).getPropertyPath(), pathMock);
}
Also used : Path(jakarta.validation.Path) Context(jakarta.validation.MessageInterpolator.Context) MessageInterpolatorContext(org.hibernate.validator.internal.engine.MessageInterpolatorContext) HibernateMessageInterpolatorContext(org.hibernate.validator.messageinterpolation.HibernateMessageInterpolatorContext) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) MessageInterpolator(jakarta.validation.MessageInterpolator) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 4 with MessageInterpolatorContext

use of org.hibernate.validator.internal.engine.MessageInterpolatorContext in project hibernate-validator by hibernate.

the class ResourceBundleMessageInterpolatorTest method testUserResourceBundle.

@Test
public void testUserResourceBundle() {
    interpolator = new ResourceBundleMessageInterpolator();
    MessageInterpolatorContext messageInterpolatorContext = createMessageInterpolatorContext(notNullDescriptor);
    String expected = "no puede ser null";
    String actual = interpolator.interpolate(notNullDescriptor.getAnnotation().message(), messageInterpolatorContext, new Locale("es", "ES"));
    assertEquals(actual, expected, "Wrong substitution");
}
Also used : MessageInterpolatorContext(org.hibernate.validator.internal.engine.MessageInterpolatorContext) Locale(java.util.Locale) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 5 with MessageInterpolatorContext

use of org.hibernate.validator.internal.engine.MessageInterpolatorContext in project hibernate-validator by hibernate.

the class ResourceBundleMessageInterpolatorTest method testMessageLiterals.

@Test
public void testMessageLiterals() {
    interpolator = new ResourceBundleMessageInterpolator(new TestResourceBundleLocator());
    MessageInterpolatorContext messageInterpolatorContext = createMessageInterpolatorContext(notNullDescriptor);
    String expected = "{";
    String actual = interpolator.interpolate("\\{", messageInterpolatorContext);
    assertEquals(actual, expected, "Wrong substitution");
    expected = "}";
    actual = interpolator.interpolate("\\}", messageInterpolatorContext);
    assertEquals(actual, expected, "Wrong substitution");
    expected = "\\";
    actual = interpolator.interpolate("\\", messageInterpolatorContext);
    assertEquals(actual, expected, "Wrong substitution");
}
Also used : MessageInterpolatorContext(org.hibernate.validator.internal.engine.MessageInterpolatorContext) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

MessageInterpolatorContext (org.hibernate.validator.internal.engine.MessageInterpolatorContext)16 ResourceBundleMessageInterpolator (org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator)13 BeforeTest (org.testng.annotations.BeforeTest)13 Test (org.testng.annotations.Test)13 MessageInterpolator (jakarta.validation.MessageInterpolator)5 HibernateMessageInterpolatorContext (org.hibernate.validator.messageinterpolation.HibernateMessageInterpolatorContext)5 Context (jakarta.validation.MessageInterpolator.Context)4 Path (jakarta.validation.Path)2 Locale (java.util.Locale)2 ConstraintDescriptorImpl (org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl)2 ConstraintAnnotationDescriptor (org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor)2 TestForIssue (org.hibernate.validator.testutil.TestForIssue)2 ResultPath (graphql.execution.ResultPath)1 ConstraintViolation (jakarta.validation.ConstraintViolation)1 Validator (jakarta.validation.Validator)1 NotNull (jakarta.validation.constraints.NotNull)1 BeanDescriptor (jakarta.validation.metadata.BeanDescriptor)1 ConstraintDescriptor (jakarta.validation.metadata.ConstraintDescriptor)1 PropertyDescriptor (jakarta.validation.metadata.PropertyDescriptor)1 HashMap (java.util.HashMap)1