|
| 1 | +package com.github.microtweak.validator.conditional.internal; |
| 2 | + |
| 3 | +import com.github.microtweak.validator.conditional.core.constraint.AssertTrueWhen; |
| 4 | +import com.github.microtweak.validator.conditional.core.constraint.DecimalMaxWhen; |
| 5 | +import com.github.microtweak.validator.conditional.core.constraint.SizeWhen; |
| 6 | +import com.github.microtweak.validator.conditional.core.internal.CvConstraintAnnotationDescriptor; |
| 7 | +import com.github.microtweak.validator.conditional.core.internal.CvConstraintDescriptorImpl; |
| 8 | +import com.github.microtweak.validator.conditional.core.internal.CvMessageInterpolatorContext; |
| 9 | +import com.github.microtweak.validator.conditional.core.spi.PlatformProvider; |
| 10 | +import com.github.microtweak.validator.conditional.internal.literal.ConstraintLiteral; |
| 11 | +import com.github.microtweak.validator.conditional.junit5.ProviderTest; |
| 12 | +import org.apache.commons.lang3.function.TriFunction; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +import javax.validation.MessageInterpolator; |
| 16 | +import javax.validation.metadata.ConstraintDescriptor; |
| 17 | +import java.lang.annotation.Annotation; |
| 18 | +import java.util.Locale; |
| 19 | +import java.util.function.BiFunction; |
| 20 | + |
| 21 | +import static org.junit.jupiter.api.Assertions.assertAll; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 23 | + |
| 24 | +@ProviderTest |
| 25 | +public class CvMessageInterpolatorContextTests { |
| 26 | + |
| 27 | + private static final PlatformProvider platform = PlatformProvider.getInstance(); |
| 28 | + |
| 29 | + private static final TriFunction<Annotation, Object, Locale, String> interpolateMessageCustomLocaleFn = (conditionalConstraint, validatedValue, customLocale) -> { |
| 30 | + final CvConstraintAnnotationDescriptor annotationDescriptor = new CvConstraintAnnotationDescriptor(conditionalConstraint); |
| 31 | + |
| 32 | + final ConstraintDescriptor<?> constraintDescriptor = new CvConstraintDescriptorImpl<>(annotationDescriptor, null); |
| 33 | + |
| 34 | + final MessageInterpolator.Context interpolatorContext = new CvMessageInterpolatorContext(constraintDescriptor, validatedValue); |
| 35 | + |
| 36 | + return platform.getMessageInterpolator().interpolate(constraintDescriptor.getMessageTemplate(), interpolatorContext, customLocale); |
| 37 | + }; |
| 38 | + |
| 39 | + private static final BiFunction<Annotation, Object, String> interpolateMessageFn = (conditionalConstraint, validatedValue) -> |
| 40 | + interpolateMessageCustomLocaleFn.apply(conditionalConstraint, validatedValue, Locale.ENGLISH); |
| 41 | + |
| 42 | + @Test |
| 43 | + public void interpolateSimpleMessageInline() { |
| 44 | + final AssertTrueWhen assertTrueWhen = new ConstraintLiteral<>(AssertTrueWhen.class) |
| 45 | + .message("must be true") |
| 46 | + .build(); |
| 47 | + |
| 48 | + final String interpolated = interpolateMessageFn.apply(assertTrueWhen, null); |
| 49 | + |
| 50 | + assertEquals("must be true", interpolated); |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + @Test |
| 55 | + public void interpolateSimpleMessageResourceBundle() { |
| 56 | + final AssertTrueWhen assertTrueWhen = new ConstraintLiteral<>(AssertTrueWhen.class).build(); |
| 57 | + |
| 58 | + final String interpolated = interpolateMessageFn.apply(assertTrueWhen, null); |
| 59 | + |
| 60 | + assertEquals("must be true", interpolated); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void interpolateWithCustomLocaleSimpleMessageResourceBundle() { |
| 65 | + final AssertTrueWhen assertTrueWhen = new ConstraintLiteral<>(AssertTrueWhen.class).build(); |
| 66 | + |
| 67 | + final String interpolated = interpolateMessageCustomLocaleFn.apply(assertTrueWhen, false, Locale.ITALIAN); |
| 68 | + |
| 69 | + assertEquals("deve essere true", interpolated); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void interpolateMessageWithAttributesInline() { |
| 74 | + final SizeWhen sizeWhen = new ConstraintLiteral<>(SizeWhen.class) |
| 75 | + .message("size must be between {min} and {max}") |
| 76 | + .attribute("min", 5) |
| 77 | + .attribute("max", 10) |
| 78 | + .build(); |
| 79 | + |
| 80 | + final String interpolated = interpolateMessageFn.apply(sizeWhen, ""); |
| 81 | + |
| 82 | + assertEquals("size must be between 5 and 10", interpolated); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void interpolateMessageWithAttributesResourceBundle() { |
| 87 | + final SizeWhen sizeWhen = new ConstraintLiteral<>(SizeWhen.class) |
| 88 | + .attribute("min", 5) |
| 89 | + .attribute("max", 10) |
| 90 | + .build(); |
| 91 | + |
| 92 | + final String interpolated = interpolateMessageFn.apply(sizeWhen, ""); |
| 93 | + |
| 94 | + assertEquals("size must be between 5 and 10", interpolated); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void interpolateMessageWithExpressionLanguageInline() { |
| 99 | + final String defaultMessage = "must be less than ${inclusive == true ? 'or equal to ' : ''}{value}"; |
| 100 | + final String expectedValue = "10.0"; |
| 101 | + final String validatedValue = "1.0"; |
| 102 | + |
| 103 | + assertAll( |
| 104 | + () -> { |
| 105 | + final DecimalMaxWhen decimalMaxWhen = new ConstraintLiteral<>(DecimalMaxWhen.class) |
| 106 | + .message(defaultMessage) |
| 107 | + .attribute("value", "10.0") |
| 108 | + .attribute("inclusive", false) |
| 109 | + .build(); |
| 110 | + |
| 111 | + final String interpolated = interpolateMessageFn.apply(decimalMaxWhen, validatedValue); |
| 112 | + assertEquals("must be less than " + expectedValue, interpolated); |
| 113 | + }, |
| 114 | + () -> { |
| 115 | + final DecimalMaxWhen decimalMaxWhen = new ConstraintLiteral<>(DecimalMaxWhen.class) |
| 116 | + .message(defaultMessage) |
| 117 | + .attribute("value", "10.0") |
| 118 | + .attribute("inclusive", true) |
| 119 | + .build(); |
| 120 | + |
| 121 | + final String interpolated = interpolateMessageFn.apply(decimalMaxWhen, validatedValue); |
| 122 | + assertEquals("must be less than or equal to " + expectedValue, interpolated); |
| 123 | + } |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void interpolateMessageWithExpressionLanguageResourceBundle() { |
| 129 | + final String expectedValue = "10.0"; |
| 130 | + final String validatedValue = "1.0"; |
| 131 | + |
| 132 | + assertAll( |
| 133 | + () -> { |
| 134 | + final DecimalMaxWhen decimalMaxWhen = new ConstraintLiteral<>(DecimalMaxWhen.class) |
| 135 | + .attribute("value", "10.0") |
| 136 | + .attribute("inclusive", false) |
| 137 | + .build(); |
| 138 | + |
| 139 | + final String interpolated = interpolateMessageFn.apply(decimalMaxWhen, validatedValue); |
| 140 | + assertEquals("must be less than " + expectedValue, interpolated); |
| 141 | + }, |
| 142 | + () -> { |
| 143 | + final DecimalMaxWhen decimalMaxWhen = new ConstraintLiteral<>(DecimalMaxWhen.class) |
| 144 | + .attribute("value", "10.0") |
| 145 | + .attribute("inclusive", true) |
| 146 | + .build(); |
| 147 | + |
| 148 | + final String interpolated = interpolateMessageFn.apply(decimalMaxWhen, validatedValue); |
| 149 | + assertEquals("must be less than or equal to " + expectedValue, interpolated); |
| 150 | + } |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | +} |
0 commit comments