testRegular()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
dl 0
loc 11
rs 9.85
1
package io.github.ro4.spelvalidation;
2
3
import org.junit.Assert;
4
import org.junit.Before;
5
import org.junit.Test;
6
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
7
import org.springframework.core.annotation.AnnotationUtils;
8
import org.springframework.util.ReflectionUtils;
9
10
import javax.validation.ConstraintValidatorContext;
11
import java.lang.annotation.Annotation;
12
import java.util.Objects;
13
14
public class SpELAssertTest {
15
    private SpELValidator spELValidator;
16
17
    private ConstraintValidatorContext validatorContext;
18
19
    @Before
20
    public void setUp() {
21
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
22
        spELValidator = context.getBean("spELValidator", SpELValidator.class);
23
        validatorContext = new DummyConstraintValidatorContext();
24
    }
25
26
    @Test
27
    public void testRegular() {
28
        TestDto dto = new TestDto();
29
        dto.setName("John");
30
        dto.setMaxAge(10);
31
        dto.setMinAge(11);
32
        SpELAssert spELAssert = AnnotationUtils.findAnnotation(TestDto.class, SpELAssert.class);
33
        spELValidator.initialize(spELAssert);
34
        Assert.assertFalse(spELValidator.isValid(dto, validatorContext));
35
        dto.setMinAge(9);
36
        Assert.assertTrue(spELValidator.isValid(dto, validatorContext));
37
    }
38
39
    @Test
40
    public void testCallBean() {
41
        Annotation[] annotations = ReflectionUtils.findField(TestDto.class, "name").getAnnotations();
42
        for (Annotation annotation : annotations) {
43
            if (!(annotation instanceof SpELAssert)) {
44
                continue;
45
            }
46
            spELValidator.initialize((SpELAssert) annotation);
47
            Assert.assertFalse(spELValidator.isValid("hello1", validatorContext));
48
            Assert.assertTrue(spELValidator.isValid("hello", validatorContext));
49
        }
50
    }
51
52
53
    @Test
54
    public void testOnlyValue() {
55
        SpELAssert spELAssert = ReflectionUtils.findField(TestDto.class, "maxAge").getAnnotation(SpELAssert.class);
56
        spELValidator.initialize(spELAssert);
57
        Assert.assertFalse(spELValidator.isValid(101, validatorContext));
58
        Assert.assertTrue(spELValidator.isValid(100, validatorContext));
59
    }
60
61
    @Test
62
    public void testOther() {
63
        Annotation[] annotations = ReflectionUtils.findField(TestDto.class, "minAge").getDeclaredAnnotations();
64
        for (Annotation annotation : annotations) {
65
            if (!(annotation instanceof SpELAssert.List)) {
66
                continue;
67
            }
68
            for (SpELAssert spELAssert : ((SpELAssert.List) annotation).value()) {
69
                spELValidator.initialize(spELAssert);
70
                if (Objects.equals(spELAssert.value(), "true")) {
71
                    Assert.assertTrue(spELValidator.isValid("anything", validatorContext));
72
                } else {
73
                    Assert.assertFalse(spELValidator.isValid(111, validatorContext));
74
                    Assert.assertTrue(spELValidator.isValid(109, validatorContext));
75
                }
76
            }
77
        }
78
    }
79
80
}
81