Java Code Examples for javax.validation.ConstraintValidatorContext#ConstraintViolationBuilder

The following examples show how to use javax.validation.ConstraintValidatorContext#ConstraintViolationBuilder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpELClassValidator.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean isValid(Object value, Function<String, ConstraintValidatorContext.ConstraintViolationBuilder> constraintViolationBuilderFunction) {
    if (!enabled) {
        return true;
    }
    if (conditionExpression != null) {
        return (boolean) conditionExpression.getValue(spelContext, value);
    }

    Map<String, String> violations = (Map<String, String>) exprExpression.getValue(spelContext, value);
    if (CollectionsExt.isNullOrEmpty(violations)) {
        return true;
    }

    violations.forEach((field, message) -> constraintViolationBuilderFunction.apply(message)
            .addPropertyNode(field)
            .addConstraintViolation()
            .disableDefaultConstraintViolation());

    return false;
}
 
Example 2
Source File: SchedulingConstraintSetValidator.java    From titus-control-plane with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean isValid(Container container, Function<String, ConstraintValidatorContext.ConstraintViolationBuilder> constraintViolationBuilderFunction) {
    if (container == null) {
        return true;
    }
    Set<String> common = new HashSet<>(container.getSoftConstraints().keySet());
    common.retainAll(container.getHardConstraints().keySet());

    if (common.isEmpty()) {
        return true;
    }

    constraintViolationBuilderFunction.apply(
            "Soft and hard constraints not unique. Shared constraints: " + common
    ).addConstraintViolation().disableDefaultConstraintViolation();
    return false;
}
 
Example 3
Source File: CollectionValidator.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
private boolean isValid(Collection<?> value, Function<String, ConstraintValidatorContext.ConstraintViolationBuilder> constraintViolationBuilderFunction) {
    if (value.isEmpty()) {
        return true;
    }

    if (!constraintAnnotation.allowNullValues()) {
        if (value.stream().anyMatch(Objects::isNull)) {
            attachMessage(constraintViolationBuilderFunction, "null values not allowed");
            return false;
        }
    }

    return true;
}
 
Example 4
Source File: ContextMockUtil.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public static ConstraintValidatorContext createContextMock() {
    ConstraintValidatorContext contextMock = mock(ConstraintValidatorContext.class);
    ConstraintValidatorContext.ConstraintViolationBuilder constraintViolationBuilderMock = mock(ConstraintValidatorContext.ConstraintViolationBuilder.class);
    ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderCustomizableContext nodeBuilderContextMock
            = mock(ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderCustomizableContext.class);

    when(contextMock.buildConstraintViolationWithTemplate(anyString()))
            .thenReturn(constraintViolationBuilderMock);
    when(constraintViolationBuilderMock.addPropertyNode(anyString()))
            .thenReturn(nodeBuilderContextMock);
    when(nodeBuilderContextMock.addConstraintViolation()).thenReturn(contextMock);
    return contextMock;
}
 
Example 5
Source File: ContextMockUtil.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public static ConstraintValidatorContext createContextMock() {
    ConstraintValidatorContext contextMock = mock(ConstraintValidatorContext.class);
    ConstraintValidatorContext.ConstraintViolationBuilder constraintViolationBuilderMock = mock(ConstraintValidatorContext.ConstraintViolationBuilder.class);
    ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderCustomizableContext nodeBuilderContextMock
            = mock(ConstraintValidatorContext.ConstraintViolationBuilder.NodeBuilderCustomizableContext.class);

    when(contextMock.buildConstraintViolationWithTemplate(anyString()))
            .thenReturn(constraintViolationBuilderMock);
    when(constraintViolationBuilderMock.addPropertyNode(anyString()))
            .thenReturn(nodeBuilderContextMock);
    when(nodeBuilderContextMock.addConstraintViolation()).thenReturn(contextMock);
    return contextMock;
}
 
Example 6
Source File: NeverNullValidator.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isValid(Object value, Function<String, ConstraintValidatorContext.ConstraintViolationBuilder> constraintViolationBuilderFunction) {
    Set<String> nullFields = validate(value);
    if (nullFields.isEmpty()) {
        return true;
    }
    constraintViolationBuilderFunction.apply(buildMessage(nullFields)).addConstraintViolation().disableDefaultConstraintViolation();
    return false;
}
 
Example 7
Source File: SpELFieldValidator.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isValid(Object value, Function<String, ConstraintValidatorContext.ConstraintViolationBuilder> constraintViolationBuilderFunction) {
    if (!enabled) {
        return true;
    }
    return (boolean) expression.getValue(spelContext, new Root(value));
}
 
Example 8
Source File: CollectionValidator.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
private boolean isValid(Map<?, ?> value, Function<String, ConstraintValidatorContext.ConstraintViolationBuilder> constraintViolationBuilderFunction) {
    if (value.isEmpty()) {
        return true;
    }

    if (!constraintAnnotation.allowEmptyKeys()) {
        if (value.keySet().stream().anyMatch(key -> key == null || (key instanceof String && ((String) key).isEmpty()))) {
            attachMessage(constraintViolationBuilderFunction, "empty key names not allowed");
            return false;
        }
    }

    if (!constraintAnnotation.allowNullKeys()) {
        if (value.keySet().stream().anyMatch(Objects::isNull)) {
            attachMessage(constraintViolationBuilderFunction, "null key names not allowed");
            return false;
        }
    }

    if (!constraintAnnotation.allowNullValues()) {
        Set<String> badEntryKeys = value.entrySet().stream()
                .filter(e -> e.getValue() == null)
                .map(e -> e.getKey() instanceof String ? (String) e.getKey() : "<not_string>")
                .collect(Collectors.toSet());
        if (!badEntryKeys.isEmpty()) {
            attachMessage(constraintViolationBuilderFunction, "null values found for keys: " + new TreeSet<>(badEntryKeys));
            return false;
        }
    }

    return true;
}
 
Example 9
Source File: UrlValidatorTest.java    From tessera with Apache License 2.0 5 votes vote down vote up
@Test
public void invalid() {
    final ConstraintValidatorContext context = mock(ConstraintValidatorContext.class);
    final ConstraintValidatorContext.ConstraintViolationBuilder builder = mock(ConstraintValidatorContext.ConstraintViolationBuilder.class);
    when(context.buildConstraintViolationWithTemplate(anyString())).thenReturn(builder);

    final boolean result = urlValidator.isValid("invalidurl", context);

    assertThat(result).isFalse();
    verify(context).disableDefaultConstraintViolation();
    verify(context).buildConstraintViolationWithTemplate("Invalid URL: no protocol: invalidurl");
    verifyNoMoreInteractions(context);
}
 
Example 10
Source File: CollectionValidator.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isValid(Object value, Function<String, ConstraintValidatorContext.ConstraintViolationBuilder> constraintViolationBuilderFunction) {

    if (value == null) {
        return true;
    }
    if (value instanceof Collection) {
        return isValid((Collection<?>) value, constraintViolationBuilderFunction);
    }
    if (value instanceof Map) {
        return isValid((Map<?, ?>) value, constraintViolationBuilderFunction);
    }
    return false;
}
 
Example 11
Source File: SchedulingConstraintValidator.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isValid(Map<String, String> value, Function<String, ConstraintValidatorContext.ConstraintViolationBuilder> constraintViolationBuilderFunction) {
    HashSet<String> unknown = value.keySet().stream().map(String::toLowerCase).collect(Collectors.toCollection(HashSet::new));
    unknown.removeAll(JobConstraints.CONSTRAINT_NAMES);
    if (unknown.isEmpty()) {
        return true;
    }
    constraintViolationBuilderFunction.apply("Unrecognized constraints " + unknown)
            .addConstraintViolation().disableDefaultConstraintViolation();
    return false;
}
 
Example 12
Source File: NoDuplicateKeyVaultConfigsValidatorTest.java    From tessera with Apache License 2.0 5 votes vote down vote up
@Test
public void multiConfigsSameTypesInvalid() {
    ConstraintValidatorContext.ConstraintViolationBuilder builder =
            mock(ConstraintValidatorContext.ConstraintViolationBuilder.class);

    String defaultMessageTemplate = "DEFAULT_MESSAGE_TEMPLATE";
    when(constraintValidatorContext.getDefaultConstraintMessageTemplate()).thenReturn(defaultMessageTemplate);

    when(constraintValidatorContext.buildConstraintViolationWithTemplate(anyString())).thenReturn(builder);

    DefaultKeyVaultConfig azure = mock(DefaultKeyVaultConfig.class);
    when(azure.getKeyVaultType()).thenReturn(KeyVaultType.AZURE);

    DefaultKeyVaultConfig hashicorp = mock(DefaultKeyVaultConfig.class);
    when(hashicorp.getKeyVaultType()).thenReturn(KeyVaultType.HASHICORP);

    DefaultKeyVaultConfig aws = mock(DefaultKeyVaultConfig.class);
    when(aws.getKeyVaultType()).thenReturn(KeyVaultType.AWS);

    DefaultKeyVaultConfig azure2 = mock(DefaultKeyVaultConfig.class);
    when(azure2.getKeyVaultType()).thenReturn(KeyVaultType.AZURE);

    DefaultKeyVaultConfig hashicorp2 = mock(DefaultKeyVaultConfig.class);
    when(hashicorp2.getKeyVaultType()).thenReturn(KeyVaultType.HASHICORP);

    DefaultKeyVaultConfig aws2 = mock(DefaultKeyVaultConfig.class);
    when(aws2.getKeyVaultType()).thenReturn(KeyVaultType.AWS);

    List<KeyVaultConfig> configs = Arrays.asList(azure, hashicorp, aws, azure2, hashicorp2, aws2);

    KeyConfiguration keyConfiguration = mock(KeyConfiguration.class);
    when(keyConfiguration.getKeyVaultConfigs()).thenReturn(configs);

    boolean result = validator.isValid(keyConfiguration, constraintValidatorContext);
    assertThat(result).isFalse();

    verify(constraintValidatorContext).buildConstraintViolationWithTemplate("AZURE " + defaultMessageTemplate);
    verify(constraintValidatorContext).buildConstraintViolationWithTemplate("HASHICORP " + defaultMessageTemplate);
    verify(constraintValidatorContext).buildConstraintViolationWithTemplate("AWS " + defaultMessageTemplate);
}
 
Example 13
Source File: KeyVaultConfigValidatorTest.java    From tessera with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    context = mock(ConstraintValidatorContext.class);

    ConstraintValidatorContext.ConstraintViolationBuilder builder =
            mock(ConstraintValidatorContext.ConstraintViolationBuilder.class);
    when(context.buildConstraintViolationWithTemplate(any(String.class))).thenReturn(builder);

    keyVaultConfigValidator = new KeyVaultConfigValidator();

    ValidKeyVaultConfig validKeyVaultConfig = mock(ValidKeyVaultConfig.class);
    keyVaultConfigValidator.initialize(validKeyVaultConfig);
}
 
Example 14
Source File: CollectionValidator.java    From titus-control-plane with Apache License 2.0 4 votes vote down vote up
private void attachMessage(Function<String, ConstraintValidatorContext.ConstraintViolationBuilder> constraintViolationBuilderFunction, String message) {
    constraintViolationBuilderFunction.apply(message).addConstraintViolation().disableDefaultConstraintViolation();
}
 
Example 15
Source File: MatchingKeyVaultConfigsForKeyDataValidatorTest.java    From tessera with Apache License 2.0 3 votes vote down vote up
@Test
public void keyDataDoesnotMatchVaultConfig() {

    ConstraintValidatorContext.ConstraintViolationBuilder constraintViolationBuilder =
            mock(ConstraintValidatorContext.ConstraintViolationBuilder.class);

    when(constraintValidatorContext.buildConstraintViolationWithTemplate(anyString()))
            .thenReturn(constraintViolationBuilder);

    KeyConfiguration keyConfiguration = new KeyConfiguration();

    KeyData keyData = new KeyData();
    keyData.setAzureVaultPublicKeyId("AzureVaultPublicKeyId");
    keyData.setAzureVaultPrivateKeyId("AzureVaultPrivateKeyId");

    keyConfiguration.setKeyData(Arrays.asList(keyData));

    DefaultKeyVaultConfig keyVaultConfig = new DefaultKeyVaultConfig();
    keyVaultConfig.setKeyVaultType(KeyVaultType.HASHICORP);

    keyConfiguration.addKeyVaultConfig(keyVaultConfig);

    assertThat(validator.isValid(keyConfiguration, constraintValidatorContext)).isFalse();

    verify(constraintValidatorContext).disableDefaultConstraintViolation();
    verify(constraintValidatorContext).buildConstraintViolationWithTemplate(anyString());
}
 
Example 16
Source File: UnsupportedKeyPairValidatorTest.java    From tessera with Apache License 2.0 3 votes vote down vote up
@Before
public void setUp() {

    this.context = mock(ConstraintValidatorContext.class);
    ConstraintValidatorContext.ConstraintViolationBuilder builder = mock(ConstraintValidatorContext.ConstraintViolationBuilder.class);

    when(context.buildConstraintViolationWithTemplate(any(String.class))).thenReturn(builder);

    this.keyPair = new UnsupportedKeyPair();
}
 
Example 17
Source File: AbstractConstraintValidator.java    From titus-control-plane with Apache License 2.0 2 votes vote down vote up
/**
 * Implementing classes will need to apply the builder function with the violation message string
 * to retrieve the underlying instance of {@link javax.validation.ConstraintValidatorContext} in order
 * to continue add any violations.
 *
 * @param type                               type of the object under validation
 * @param constraintViolationBuilderFunction function to apply with a violation message string
 * @return validation status
 */
abstract protected boolean isValid(T type, Function<String, ConstraintValidatorContext.ConstraintViolationBuilder> constraintViolationBuilderFunction);