Skip to content

Commit f6106ae

Browse files
User class adjustments
1 parent 99ad1a8 commit f6106ae

File tree

3 files changed

+169
-34
lines changed

3 files changed

+169
-34
lines changed

account-service/src/main/java/com/piggymetrics/account/domain/User.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ public String getUsername() {
1919
}
2020

2121
public void setUsername(String username) {
22-
if (username.length() < 3) {
23-
throw new IllegalArgumentException();
24-
}
2522
this.username = username;
2623
}
2724

@@ -30,9 +27,6 @@ public String getPassword() {
3027
}
3128

3229
public void setPassword(String password) {
33-
if (password.length() < 6) {
34-
throw new IllegalArgumentException();
35-
}
3630
this.password = password;
3731
}
38-
}
32+
}

account-service/src/main/java/com/piggymetrics/account/domain/UserValidator.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
public class UserValidator {
66

7-
public static void validate(User user) {
7+
public static boolean validate(User user) {
88
if (user == null) {
99
throw new IllegalArgumentException("User cannot be null");
1010
}
@@ -19,6 +19,9 @@ public static void validate(User user) {
1919
if (!isUsernameValid && !isPasswordValid) {
2020
throw new IllegalArgumentException("Credentials Invalid");
2121
}
22+
23+
return true;
24+
2225
}
2326

2427
public static boolean validateUsername(String username) throws IllegalArgumentException {
@@ -34,7 +37,7 @@ public static boolean validateUsername(String username) throws IllegalArgumentEx
3437

3538
public static boolean validatePassword(String password) throws IllegalArgumentException {
3639
/*public static boolean validatePassword(
37-
//@InTestsUseStrings({"qwerty", "123"}) String password)
40+
@InTestsUseStrings({"qwerty", "123"}) String password)
3841
throws IllegalArgumentException {*/
3942
if (password == null || password.length() < 6 || password.length() > 40) {
4043
return false;

account-service/src/test/java/com/piggymetrics/account/domain/UserValidatorDiffblueTest.java

Lines changed: 163 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,55 @@
44
import static org.junit.jupiter.api.Assertions.assertThrows;
55
import static org.junit.jupiter.api.Assertions.assertTrue;
66

7-
import org.junit.jupiter.api.Disabled;
87
import org.junit.jupiter.api.DisplayName;
98
import org.junit.jupiter.api.Tag;
109
import org.junit.jupiter.api.Test;
1110

1211
class UserValidatorDiffblueTest {
13-
1412
/**
1513
* Test {@link UserValidator#validate(User)}.
1614
* <ul>
17-
* <li>Given {@code abc}.</li>
18-
* <li>When {@link User} (default constructor) Password is {@code abc}.</li>
15+
* <li>Given {@code null}.</li>
16+
* <li>When {@link User} (default constructor) Username is {@code null}.</li>
17+
* <li>Then throw {@link IllegalArgumentException}.</li>
1918
* </ul>
2019
* <p>
2120
* Method under test: {@link UserValidator#validate(User)}
2221
*/
2322
@Test
24-
@DisplayName("Test validate(User); given 'abc'; when User (default constructor) Password is 'abc'")
25-
@Disabled("TODO: Complete this test")
23+
@DisplayName("Test validate(User); given 'null'; when User (default constructor) Username is 'null'; then throw IllegalArgumentException")
2624
@Tag("MaintainedByDiffblue")
27-
void testValidate_givenAbc_whenUserPasswordIsAbc() {
28-
// TODO: Diffblue Cover was only able to create a partial test for this method:
29-
// Reason: No inputs found that don't throw a trivial exception.
30-
// Diffblue Cover tried to run the arrange/act section, but the method under
31-
// test threw
32-
// java.lang.IllegalArgumentException
33-
// at com.piggymetrics.account.domain.User.setPassword(User.java:34)
34-
// See https://diff.blue/R013 to resolve this issue.
25+
void testValidate_givenNull_whenUserUsernameIsNull_thenThrowIllegalArgumentException() {
26+
// Arrange
27+
User user = new User();
28+
user.setUsername(null);
29+
user.setPassword(null);
30+
31+
// Act and Assert
32+
assertThrows(IllegalArgumentException.class, () -> UserValidator.validate(user));
33+
}
3534

35+
/**
36+
* Test {@link UserValidator#validate(User)}.
37+
* <ul>
38+
* <li>Given {@code qw}.</li>
39+
* <li>When {@link User} (default constructor) Username is {@code qw}.</li>
40+
* <li>Then throw {@link IllegalArgumentException}.</li>
41+
* </ul>
42+
* <p>
43+
* Method under test: {@link UserValidator#validate(User)}
44+
*/
45+
@Test
46+
@DisplayName("Test validate(User); given 'qw'; when User (default constructor) Username is 'qw'; then throw IllegalArgumentException")
47+
@Tag("MaintainedByDiffblue")
48+
void testValidate_givenQw_whenUserUsernameIsQw_thenThrowIllegalArgumentException() {
3649
// Arrange
3750
User user = new User();
3851
user.setPassword("abc");
39-
user.setUsername("Smith");
52+
user.setUsername("qw");
4053

41-
// Act
42-
UserValidator.validate(user);
54+
// Act and Assert
55+
assertThrows(IllegalArgumentException.class, () -> UserValidator.validate(user));
4356
}
4457

4558
/**
@@ -48,24 +61,99 @@ void testValidate_givenAbc_whenUserPasswordIsAbc() {
4861
* <li>Given {@code qwertyuio}.</li>
4962
* <li>When {@link User} (default constructor) Password is
5063
* {@code qwertyuio}.</li>
64+
* <li>Then return {@code true}.</li>
5165
* </ul>
5266
* <p>
5367
* Method under test: {@link UserValidator#validate(User)}
5468
*/
5569
@Test
56-
@DisplayName("Test validate(User); given 'qwertyuio'; when User (default constructor) Password is 'qwertyuio'")
70+
@DisplayName("Test validate(User); given 'qwertyuio'; when User (default constructor) Password is 'qwertyuio'; then return 'true'")
5771
@Tag("MaintainedByDiffblue")
58-
void testValidate_givenQwertyuio_whenUserPasswordIsQwertyuio() {
59-
// TODO: Diffblue Cover was only able to create a partial test for this method:
60-
// Diffblue AI was unable to find a test
61-
72+
void testValidate_givenQwertyuio_whenUserPasswordIsQwertyuio_thenReturnTrue() {
6273
// Arrange
6374
User user = new User();
6475
user.setPassword("qwertyuio");
6576
user.setUsername("Smith");
6677

67-
// Act
68-
UserValidator.validate(user);
78+
// Act and Assert
79+
assertTrue(UserValidator.validate(user));
80+
}
81+
82+
/**
83+
* Test {@link UserValidator#validate(User)}.
84+
* <ul>
85+
* <li>Given {@code SmithCredentials Invalid}.</li>
86+
* </ul>
87+
* <p>
88+
* Method under test: {@link UserValidator#validate(User)}
89+
*/
90+
@Test
91+
@DisplayName("Test validate(User); given 'SmithCredentials Invalid'")
92+
@Tag("MaintainedByDiffblue")
93+
void testValidate_givenSmithCredentialsInvalid() {
94+
// Arrange
95+
User user = new User();
96+
user.setPassword("abc");
97+
user.setUsername("SmithCredentials Invalid");
98+
99+
// Act and Assert
100+
assertThrows(IllegalArgumentException.class, () -> UserValidator.validate(user));
101+
}
102+
103+
/**
104+
* Test {@link UserValidator#validate(User)}.
105+
* <ul>
106+
* <li>Given {@code Smith}.</li>
107+
* <li>When {@link User} (default constructor) Username is {@code Smith}.</li>
108+
* </ul>
109+
* <p>
110+
* Method under test: {@link UserValidator#validate(User)}
111+
*/
112+
@Test
113+
@DisplayName("Test validate(User); given 'Smith'; when User (default constructor) Username is 'Smith'")
114+
@Tag("MaintainedByDiffblue")
115+
void testValidate_givenSmith_whenUserUsernameIsSmith() {
116+
// Arrange
117+
User user = new User();
118+
user.setPassword("abc");
119+
user.setUsername("Smith");
120+
121+
// Act and Assert
122+
assertThrows(IllegalArgumentException.class, () -> UserValidator.validate(user));
123+
}
124+
125+
/**
126+
* Test {@link UserValidator#validate(User)}.
127+
* <ul>
128+
* <li>When {@code null}.</li>
129+
* <li>Then throw {@link IllegalArgumentException}.</li>
130+
* </ul>
131+
* <p>
132+
* Method under test: {@link UserValidator#validate(User)}
133+
*/
134+
@Test
135+
@DisplayName("Test validate(User); when 'null'; then throw IllegalArgumentException")
136+
@Tag("MaintainedByDiffblue")
137+
void testValidate_whenNull_thenThrowIllegalArgumentException() {
138+
// Arrange, Act and Assert
139+
assertThrows(IllegalArgumentException.class, () -> UserValidator.validate(null));
140+
}
141+
142+
/**
143+
* Test {@link UserValidator#validateUsername(String)}.
144+
* <ul>
145+
* <li>When {@code null}.</li>
146+
* <li>Then return {@code false}.</li>
147+
* </ul>
148+
* <p>
149+
* Method under test: {@link UserValidator#validateUsername(String)}
150+
*/
151+
@Test
152+
@DisplayName("Test validateUsername(String); when 'null'; then return 'false'")
153+
@Tag("MaintainedByDiffblue")
154+
void testValidateUsername_whenNull_thenReturnFalse() throws IllegalArgumentException {
155+
// Arrange, Act and Assert
156+
assertFalse(UserValidator.validateUsername(null));
69157
}
70158

71159
/**
@@ -85,6 +173,40 @@ void testValidateUsername_whenQw_thenReturnFalse() throws IllegalArgumentExcepti
85173
assertFalse(UserValidator.validateUsername("qw"));
86174
}
87175

176+
/**
177+
* Test {@link UserValidator#validateUsername(String)}.
178+
* <ul>
179+
* <li>When {@code SmithUsernameUsername}.</li>
180+
* <li>Then return {@code false}.</li>
181+
* </ul>
182+
* <p>
183+
* Method under test: {@link UserValidator#validateUsername(String)}
184+
*/
185+
@Test
186+
@DisplayName("Test validateUsername(String); when 'SmithUsernameUsername'; then return 'false'")
187+
@Tag("MaintainedByDiffblue")
188+
void testValidateUsername_whenSmithUsernameUsername_thenReturnFalse() throws IllegalArgumentException {
189+
// Arrange, Act and Assert
190+
assertFalse(UserValidator.validateUsername("SmithUsernameUsername"));
191+
}
192+
193+
/**
194+
* Test {@link UserValidator#validateUsername(String)}.
195+
* <ul>
196+
* <li>When {@code Smith}.</li>
197+
* <li>Then return {@code true}.</li>
198+
* </ul>
199+
* <p>
200+
* Method under test: {@link UserValidator#validateUsername(String)}
201+
*/
202+
@Test
203+
@DisplayName("Test validateUsername(String); when 'Smith'; then return 'true'")
204+
@Tag("MaintainedByDiffblue")
205+
void testValidateUsername_whenSmith_thenReturnTrue() throws IllegalArgumentException {
206+
// Arrange, Act and Assert
207+
assertTrue(UserValidator.validateUsername("Smith"));
208+
}
209+
88210
/**
89211
* Test {@link UserValidator#validatePassword(String)}.
90212
* <ul>
@@ -102,6 +224,23 @@ void testValidatePassword_whenAbc_thenReturnFalse() throws IllegalArgumentExcept
102224
assertFalse(UserValidator.validatePassword("abc"));
103225
}
104226

227+
/**
228+
* Test {@link UserValidator#validatePassword(String)}.
229+
* <ul>
230+
* <li>When {@code null}.</li>
231+
* <li>Then return {@code false}.</li>
232+
* </ul>
233+
* <p>
234+
* Method under test: {@link UserValidator#validatePassword(String)}
235+
*/
236+
@Test
237+
@DisplayName("Test validatePassword(String); when 'null'; then return 'false'")
238+
@Tag("MaintainedByDiffblue")
239+
void testValidatePassword_whenNull_thenReturnFalse() throws IllegalArgumentException {
240+
// Arrange, Act and Assert
241+
assertFalse(UserValidator.validatePassword(null));
242+
}
243+
105244
/**
106245
* Test {@link UserValidator#validatePassword(String)}.
107246
* <ul>
@@ -118,5 +257,4 @@ void testValidatePassword_whenQwertyuio_thenReturnTrue() throws IllegalArgumentE
118257
// Arrange, Act and Assert
119258
assertTrue(UserValidator.validatePassword("qwertyuio"));
120259
}
121-
122260
}

0 commit comments

Comments
 (0)