Skip to content

Commit c5c165d

Browse files
committed
Added more tests
1 parent d91876b commit c5c165d

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

src/test/java/dev/ditsche/validator/ValidatorTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,66 @@ public void shouldValidateExample() {
3333
);
3434
testEntity = validator.validate(testEntity);
3535
assertThat(email).isNotEqualTo(testEntity.getEmail());
36+
assertThat(testEntity.getEmail()).isEqualTo(email.trim());
37+
}
38+
39+
@Test
40+
public void shouldSkipOptionalPropertyIfNotSet() {
41+
TestEntity testEntity = new TestEntity("Mr", "[email protected]", null, 3, null);
42+
validator = Validator.fromRules(
43+
string("title").required().trim().max(3),
44+
string("email").required().trim().email(),
45+
string("firstName").optional().trim().alphanum().max(80),
46+
number("count").max(5)
47+
);
48+
testEntity = validator.validate(testEntity);
49+
assertThat(testEntity.getFirstName()).isNull();
50+
}
51+
52+
@Test
53+
public void shouldValidateOptionalPropertyIfSet() {
54+
TestEntity testEntity = new TestEntity("Mr", "[email protected]", "Max ", 3, null);
55+
validator = Validator.fromRules(
56+
string("title").required().trim().max(3),
57+
string("email").required().trim().email(),
58+
string("firstName").optional().trim().alphanum().max(80),
59+
number("count").max(5)
60+
);
61+
testEntity = validator.validate(testEntity);
62+
assertThat(testEntity.getFirstName().length()).isEqualTo(3);
63+
}
3664

65+
@Test
66+
public void shouldSkipOptionalNestedPropertyIfNotSet() {
67+
TestEntity testEntity = new TestEntity("Mr", "[email protected]", null, 3, null);
68+
validator = Validator.fromRules(
69+
string("title").required().trim().max(3),
70+
string("email").required().trim().email(),
71+
string("firstName").optional().trim().alphanum().max(80),
72+
number("count").max(5),
73+
object("nestedEntity").optional().fields(
74+
string("name").required().trim().min(1)
75+
)
76+
);
77+
testEntity = validator.validate(testEntity);
78+
assertThat(testEntity.getNestedEntity()).isNull();
79+
}
80+
81+
@Test
82+
public void shouldValidateOptionalNestedPropertyIfSet() {
83+
final String name = " Test ";
84+
TestEntity testEntity = new TestEntity("Mr", "[email protected]", null, 3, new NestedEntity(name));
85+
validator = Validator.fromRules(
86+
string("title").required().trim().max(3),
87+
string("email").required().trim().email(),
88+
string("firstName").optional().trim().alphanum().max(80),
89+
number("count").max(5),
90+
object("nestedEntity").optional().fields(
91+
string("name").required().trim().min(1)
92+
)
93+
);
94+
testEntity = validator.validate(testEntity);
95+
assertThat(testEntity.getNestedEntity().getName()).isNotEqualTo(name);
3796
}
3897

3998
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package dev.ditsche.validator.rule.ruleset;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.LinkedList;
6+
import java.util.stream.Stream;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
public class BooleanRuleTest {
11+
12+
private final boolean value = true;
13+
private final BooleanRule booleanRule = new BooleanRule(value);
14+
15+
@Test
16+
public void shouldFailIfNoBooleanIsProvided() {
17+
Stream.of(null, 1, new LinkedList<>(), 2.00f, "false").forEach(value -> {
18+
assertThat(booleanRule.passes(value).isPassed()).isFalse();
19+
});
20+
}
21+
22+
@Test
23+
public void shouldFailWithOppositeValue() {
24+
assertThat(booleanRule.passes(!value).isPassed()).isFalse();
25+
}
26+
27+
@Test
28+
public void shouldPassWithValidInput() {
29+
assertThat(booleanRule.passes(value).isPassed()).isTrue();
30+
}
31+
32+
@Test
33+
public void shouldReturnValidErrorMessage() {
34+
assertThat(booleanRule.message("test")).isEqualTo("The field \"test\" needs to be truthy");
35+
final BooleanRule br = new BooleanRule(false);
36+
assertThat(br.message("test")).isEqualTo("The field \"test\" needs to be falsy");
37+
}
38+
39+
}

0 commit comments

Comments
 (0)