Skip to content

Commit 876e04d

Browse files
committed
Added tests for CreditCardRule
1 parent 68409c8 commit 876e04d

File tree

4 files changed

+51
-11
lines changed

4 files changed

+51
-11
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
language: java
1+
language: java
2+
after_success:
3+
- bash <(curl -s https://codecov.io/bash)

src/main/java/dev/ditsche/validator/rule/ruleset/CreditCardRule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
public class CreditCardRule implements Rule {
1414

15-
private final String PATTERN = "^(\\\\d{4}[- ]?){3}\\\\d{4}$";
15+
private final String PATTERN = "^(\\d{4}[- ]?){3}\\d{4}$";
1616

1717
@Override
1818
public RuleResult passes(Object value) {
@@ -25,6 +25,6 @@ public RuleResult passes(Object value) {
2525

2626
@Override
2727
public String message(String field) {
28-
return String.format("The field \"%s\" needs to be a valid credit card", field);
28+
return String.format("The field \"%s\" needs to be a valid credit card number", field);
2929
}
3030
}

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import dev.ditsche.validator.dto.NestedEntity;
44
import dev.ditsche.validator.dto.TestEntity;
5-
import dev.ditsche.validator.error.ValidationException;
65
import org.junit.jupiter.api.BeforeEach;
76
import org.junit.jupiter.api.Test;
87

98
import static dev.ditsche.validator.rule.builder.Rules.*;
9+
import static org.assertj.core.api.Assertions.assertThat;
1010

1111
public class ValidatorTest {
1212

@@ -19,8 +19,9 @@ public void setUp() {
1919

2020
@Test
2121
public void shouldValidateExample() {
22-
TestEntity testEntity = new TestEntity("Mr", "[email protected]", "Tobias", 4, new NestedEntity("Max"));
23-
22+
final String email = "[email protected] ";
23+
TestEntity testEntity = new TestEntity("Mr", email, "Tobias", 4, new NestedEntity("Max"));
24+
assertThat(email).isEqualTo(testEntity.getEmail());
2425
validator = Validator.fromRules(
2526
string("title").required().trim().max(3),
2627
string("email").required().trim().email(),
@@ -30,12 +31,9 @@ public void shouldValidateExample() {
3031
string("name").required().trim().min(1)
3132
)
3233
);
34+
testEntity = validator.validate(testEntity);
35+
assertThat(email).isNotEqualTo(testEntity.getEmail());
3336

34-
try {
35-
testEntity = validator.validate(testEntity);
36-
} catch (ValidationException ex) {
37-
throw ex;
38-
}
3937
}
4038

4139
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 CreditCardRuleTest {
11+
12+
private final CreditCardRule creditCardRule = new CreditCardRule();
13+
14+
@Test
15+
public void shouldFailIfNoStringIsProvided() {
16+
Stream.of(null, 1, new LinkedList<>(), 2.00f).forEach(value -> {
17+
assertThat(creditCardRule.passes(value).isPassed()).isFalse();
18+
});
19+
}
20+
21+
@Test
22+
public void shouldFailIfStringIsInvalid() {
23+
Stream.of("", "2222-2222-2222-222a", "4242-2424-4242-04 3").forEach(value -> {
24+
assertThat(creditCardRule.passes(value).isPassed()).isFalse();
25+
});
26+
}
27+
28+
@Test
29+
public void shouldPassWithValidCreditCardNumber() {
30+
Stream.of("2222-2222-2222-2222", "4242-2424-4242-4242").forEach(value -> {
31+
assertThat(creditCardRule.passes(value).isPassed()).isTrue();
32+
});
33+
}
34+
35+
@Test
36+
public void shouldReturnValidErrorMessage() {
37+
assertThat(creditCardRule.message("test")).isEqualTo("The field \"test\" needs to be a valid credit card number");
38+
}
39+
40+
}

0 commit comments

Comments
 (0)