/
DurationValidatorTest.java
107 lines (90 loc) · 4.48 KB
/
DurationValidatorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package io.dropwizard.validation;
import com.google.common.collect.ImmutableList;
import io.dropwizard.util.Duration;
import org.junit.Test;
import javax.validation.Valid;
import javax.validation.Validator;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import static org.assertj.core.api.Assertions.assertThat;
public class DurationValidatorTest {
@SuppressWarnings("unused")
public static class Example {
@MaxDuration(value = 30, unit = TimeUnit.SECONDS)
private Duration tooBig = Duration.minutes(10);
@MaxDuration(value = 30, unit = TimeUnit.SECONDS, exclusive = true)
private Duration tooBigExclusive = Duration.seconds(30);
@MinDuration(value = 30, unit = TimeUnit.SECONDS)
private Duration tooSmall = Duration.milliseconds(100);
@MinDuration(value = 30, unit = TimeUnit.SECONDS, exclusive = true)
private Duration tooSmallExclusive = Duration.seconds(30);
@DurationRange(min = 10, max = 30, unit = TimeUnit.MINUTES)
private Duration outOfRange = Duration.minutes(60);
@Valid
private List<@MaxDuration(value = 30, unit = TimeUnit.SECONDS) Duration> maxDurs =
ImmutableList.of(Duration.minutes(10));
@Valid
private List<@MinDuration(value = 30, unit = TimeUnit.SECONDS) Duration> minDurs =
ImmutableList.of(Duration.milliseconds(100));
@Valid
private List<@DurationRange(min = 10, max = 30, unit = TimeUnit.MINUTES) Duration> rangeDurs =
ImmutableList.of(Duration.minutes(60));
public void setTooBig(Duration tooBig) {
this.tooBig = tooBig;
}
public void setTooBigExclusive(Duration tooBigExclusive) {
this.tooBigExclusive = tooBigExclusive;
}
public void setTooSmall(Duration tooSmall) {
this.tooSmall = tooSmall;
}
public void setTooSmallExclusive(Duration tooSmallExclusive) {
this.tooSmallExclusive = tooSmallExclusive;
}
public void setOutOfRange(Duration outOfRange) {
this.outOfRange = outOfRange;
}
public void setMaxDurs(List<Duration> maxDurs) {
this.maxDurs = maxDurs;
}
public void setMinDurs(List<Duration> minDurs) {
this.minDurs = minDurs;
}
public void setRangeDurs(List<Duration> rangeDurs) {
this.rangeDurs = rangeDurs;
}
}
private final Validator validator = BaseValidator.newValidator();
@Test
public void returnsASetOfErrorsForAnObject() throws Exception {
if ("en".equals(Locale.getDefault().getLanguage())) {
final ImmutableList<String> errors =
ConstraintViolations.format(validator.validate(new Example()));
assertThat(errors)
.containsOnly(
"outOfRange must be between 10 MINUTES and 30 MINUTES",
"tooBig must be less than or equal (or less than if in 'exclusive' mode) to 30 SECONDS",
"tooBigExclusive must be less than or equal (or less than if in 'exclusive' mode) to 30 SECONDS",
"tooSmall must be greater than or equal (or greater if in 'exclusive' mode) to 30 SECONDS",
"tooSmallExclusive must be greater than or equal (or greater if in 'exclusive' mode) to 30 SECONDS",
"maxDurs[0].<collection element> must be less than or equal (or less than if in 'exclusive' mode) to 30 SECONDS",
"minDurs[0].<collection element> must be greater than or equal (or greater if in 'exclusive' mode) to 30 SECONDS",
"rangeDurs[0].<collection element> must be between 10 MINUTES and 30 MINUTES");
}
}
@Test
public void returnsAnEmptySetForAValidObject() throws Exception {
final Example example = new Example();
example.setTooBig(Duration.seconds(10));
example.setTooBigExclusive(Duration.seconds(29));
example.setTooSmall(Duration.seconds(100));
example.setTooSmallExclusive(Duration.seconds(31));
example.setOutOfRange(Duration.minutes(15));
example.setMaxDurs(ImmutableList.of(Duration.seconds(10)));
example.setMinDurs(ImmutableList.of(Duration.seconds(100)));
example.setRangeDurs(ImmutableList.of(Duration.minutes(15)));
assertThat(validator.validate(example))
.isEmpty();
}
}