Skip to content

Commit

Permalink
Add more tests and fix nullability (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
dstepanov authored Mar 21, 2023
1 parent 1ee41e2 commit eea7bd0
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ class DefaultConstraintDescriptor<T extends Annotation> implements ConstraintDes
private final AnnotationValue<T> annotationValue;
private final AnnotationMetadata annotationMetadata;

DefaultConstraintDescriptor(Class<T> constraintType,
AnnotationValue<T> annotationValue,
AnnotationMetadata annotationMetadata) {
DefaultConstraintDescriptor(@NonNull Class<T> constraintType,
@NonNull AnnotationValue<T> annotationValue,
@NonNull AnnotationMetadata annotationMetadata) {
this(constraintType,
annotationValue.stringValue("message").orElse(null),
(String) annotationValue.getDefaultValues().get("message"),
annotationValue.getDefaultValues() == null ? null : (String) annotationValue.getDefaultValues().get("message"),
Set.of(annotationValue.classValues("groups")),
(Set) Set.of(annotationValue.classValues("payload")),
(List) List.of(annotationValue.classValues(ValidationAnnotationUtil.CONSTRAINT_VALIDATED_BY)),
Expand All @@ -74,15 +74,15 @@ class DefaultConstraintDescriptor<T extends Annotation> implements ConstraintDes
annotationMetadata);
}

DefaultConstraintDescriptor(Class<T> type,
String message,
String defaultMessage,
Set<Class<?>> groups,
Set<Class<? extends Payload>> payload,
List<Class<? extends ConstraintValidator<T, ?>>> validatedBy,
ConstraintTarget validationAppliesTo,
AnnotationValue<T> annotationValue,
AnnotationMetadata annotationMetadata) {
DefaultConstraintDescriptor(@NonNull Class<T> type,
@Nullable String message,
@Nullable String defaultMessage,
@NonNull Set<Class<?>> groups,
@NonNull Set<Class<? extends Payload>> payload,
@NonNull List<Class<? extends ConstraintValidator<T, ?>>> validatedBy,
@NonNull ConstraintTarget validationAppliesTo,
@NonNull AnnotationValue<T> annotationValue,
@NonNull AnnotationMetadata annotationMetadata) {
this.type = type;
this.message = message;
this.defaultMessage = defaultMessage;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.micronaut.validation.validator;

import io.micronaut.core.annotation.Introspected;
import jakarta.validation.constraints.Max;

@Introspected
public class MyBeanWithPrimitives {

@Max(20)
private int number;

public int getNumber() {
return number;
}

public void setNumber(int number) {
this.number = number;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import jakarta.validation.constraints.NotBlank
import jakarta.validation.constraints.NotNull
import jakarta.validation.constraints.Size
import jakarta.validation.metadata.BeanDescriptor
import org.apache.groovy.util.Maps
import spock.lang.AutoCleanup
import spock.lang.Shared
import spock.lang.Specification
Expand Down Expand Up @@ -860,15 +859,79 @@ class ValidatorSpec extends Specification {
then:
noExceptionThrown()
}
void "test shouldn't iterate"() {
when:
def service = applicationContext.getBean(MyService)
service.myMethod1()
service.myMethod2()
service.myMethod3()
service.myMethod4(new BeanMap<String, Integer>())
service.myMethod5(new BeanList<BeanMap<String, Integer>>())
then:
noExceptionThrown()
}
void "test validate primitives parameter"() {
when:
def service = applicationContext.getBean(MyService2)
service.myMethod1(10)
then:
Exception e = thrown()
e.message.contains('''myMethod1.a: must be less than or equal to 5''')
}
void "test validate primitives return"() {
when:
def service = applicationContext.getBean(MyService2)
service.myMethod1(1)
then:
Exception e = thrown()
e.message.contains('''myMethod1.<return value>: must be greater than or equal to 10''')
}
void "test validate bean with primitives"() {
when:
def service = applicationContext.getBean(MyService2)
def bean = new MyBeanWithPrimitives()
bean.number = 100
service.myMethod2(bean)
then:
Exception e = thrown()
e.message.contains('''myMethod2.bean.number: must be less than or equal to 20''')
}
}
class Bean extends AbstractMap<String, Integer> {
@Override
Set<Entry<String, Integer>> entrySet() {
return Maps.of("A", 1, "B", 2, "C", 3).entrySet()
throw new IllegalStateException("Should be iterated")
}
}
class BeanMap<K, V> extends AbstractMap<K, V> {
@Override
Set<Entry<String, Integer>> entrySet() {
throw new IllegalStateException("Should be iterated")
}
}
class BeanList<V> extends AbstractList<V> {
@Override
V get(int index) {
throw new IllegalStateException("Should be iterated")
}
@Override
int size() {
throw new IllegalStateException("Should be iterated")
}
}
@Validated
Expand All @@ -879,6 +942,37 @@ class MyService {
return [new Bean(), new Bean()] as List
}
List<Bean> myMethod1() {
return [new Bean(), new Bean()] as List
}
List<BeanMap<String, Integer>> myMethod2() {
return [new BeanMap<String, Integer>(), new BeanMap<String, Integer>()] as List
}
BeanMap<String, Integer> myMethod3() {
return new BeanMap<String, Integer>()
}
void myMethod4(BeanMap<String, Integer> val) {
}
void myMethod5(List<BeanMap<String, Integer>> val) {
}
}
@Validated
@Singleton
class MyService2 {
@Min(10) int myMethod1(@Max(5) int a) {
return a
}
void myMethod2(@Valid MyBeanWithPrimitives bean) {
}
}
@Singleton
Expand Down

0 comments on commit eea7bd0

Please sign in to comment.