-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserValidatorTest.java
83 lines (66 loc) · 3.03 KB
/
UserValidatorTest.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
package dev.hertlein.pocs.archunit.user.core.service;
import dev.hertlein.pocs.archunit.user.core.model.User;
import dev.hertlein.pocs.archunit.user.core.model.UserId;
import dev.hertlein.pocs.archunit.user.core.model.UserName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.NullSource;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatNoException;
class UserValidatorTest {
private static final UserName A_VALID_USER_NAME = new UserName("a-first-name", "a-last-name");
private static final User A_VALID_USER = new User(new UserId("an-id"), A_VALID_USER_NAME);
private final UserValidator userValidator = new UserValidator();
@Nested
class ValidateUserNameShould {
@Test
void notThrowExceptionIfIsValid() {
assertThatNoException().isThrownBy(() ->
userValidator.validateUserName(A_VALID_USER_NAME)
);
}
@ParameterizedTest
@NullAndEmptySource
void throwExceptionIfFirstNameIsInvalid(String firstName) {
UserName invalidUserName = new UserName(firstName, A_VALID_USER_NAME.lastName());
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> userValidator.validateUserName(invalidUserName))
.withMessageContaining("FirstName");
}
@ParameterizedTest
@NullAndEmptySource
void throwExceptionIfLastNameIsInvalid(String lastName) {
UserName invalidUserName = new UserName(A_VALID_USER_NAME.firstName(), lastName);
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> userValidator.validateUserName(invalidUserName))
.withMessageContaining("LastName");
}
}
@Nested
class ValidateUserShould {
@Test
void notThrowExceptionIfIsValid() {
assertThatNoException().isThrownBy(() ->
userValidator.validateUser(A_VALID_USER)
);
}
@ParameterizedTest
@NullAndEmptySource
void throwExceptionIfUserIdInUserIdIsInvalid(String userId) {
User invalidUser = new User(new UserId(userId), A_VALID_USER_NAME);
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> userValidator.validateUser(invalidUser))
.withMessageContaining("UserId");
}
@ParameterizedTest
@NullSource
void throwExceptionIfUserIdIsInvalid(UserId userId) {
User invalidUser = new User(userId, A_VALID_USER_NAME);
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> userValidator.validateUser(invalidUser))
.withMessageContaining("UserId");
}
}
}