Skip to content

Commit

Permalink
Test failure
Browse files Browse the repository at this point in the history
  • Loading branch information
KLICKICE committed Jul 9, 2024
1 parent 223a4a7 commit b6dbf9c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package core.basesyntax;

//write your code here
public class PasswordValidationException extends Exception {
public PasswordValidationException(String massage) {
super(massage);
}
}
16 changes: 14 additions & 2 deletions src/main/java/core/basesyntax/PasswordValidator.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
package core.basesyntax;

public class PasswordValidator {
public void validate(String password, String repeatPassword) {
//write your code here
private static final int PASSWORD_MAX_LENGTH = 10;

public boolean validate(String password, String repeatPassword) throws
PasswordValidationException {
if (password == null || repeatPassword == null) {
throw new PasswordValidationException("Password or repeatPassword cannot be null");
}

if (password.equals(repeatPassword) && password.length() >= PASSWORD_MAX_LENGTH) {
System.out.println("Successful");
return true;
}

throw new PasswordValidationException("Wrong passwords");
}
}
8 changes: 4 additions & 4 deletions src/main/java/core/basesyntax/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public void setRepeatPassword(String repeatPassword) {
@Override
public String toString() {
return "User{"
+ "email='" + email + '\''
+ ", password='" + password + '\''
+ ", repeatPassword='" + repeatPassword + '\''
+ '}';
+ "email='" + email + '\''
+ ", password='" + password + '\''
+ ", repeatPassword='" + repeatPassword + '\''
+ '}';
}
}
10 changes: 9 additions & 1 deletion src/main/java/core/basesyntax/UserService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package core.basesyntax;

public class UserService {
private PasswordValidator validator = new PasswordValidator();

public void registerUser(User user) {
//write your code here
try {
if (validator.validate(user.getPassword(), user.getRepeatPassword())) {
saveUser(user);
}
} catch (PasswordValidationException e) {
System.out.println(e.getMessage()); // Print the exception message
}
}

public void saveUser(User user) {
Expand Down
54 changes: 27 additions & 27 deletions src/test/java/core/basesyntax/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void registerUser_correctInputData() {
String actualMessage = outContent.toString().trim();
String expectedResult = "User " + user.toString() + " was saved to database!!!";
Assert.assertEquals("User " + user.toString() + " should be saved. " +
"Let's call method saveUser()\n", expectedResult, actualMessage);
"Let's call method saveUser()\n", expectedResult, actualMessage);
}

@Test
Expand All @@ -50,32 +50,32 @@ public void registerUser_incorrectInputData() {
String actualMessage = outContent.toString().trim();
String expectedResult = "Your passwords are incorrect. Try again.";
Assert.assertEquals("You should print message: \"" + expectedResult
+ "\" in catch block, after failed validation\n", expectedResult, actualMessage);
+ "\" in catch block, after failed validation\n", expectedResult, actualMessage);
}

@Test
public void registerUser_throwsException() {
Class<?>[] exceptionTypes = getRegisterMethod().getExceptionTypes();
Assert.assertEquals("Don't add exception to the signature of method registerUser(). " +
"Let's use try-catch construction\n", 0, exceptionTypes.length);
"Let's use try-catch construction\n", 0, exceptionTypes.length);
}

private Method getRegisterMethod() {
return Arrays.stream(UserService.class.getDeclaredMethods())
.filter(m -> m.getName().equals("registerUser"))
.findFirst()
.orElseThrow(() -> new RuntimeException("Method registerUser() should be present " +
"in the UserService class"));
.filter(m -> m.getName().equals("registerUser"))
.findFirst()
.orElseThrow(() -> new RuntimeException("Method registerUser() should be present " +
"in the UserService class"));
}

@Test
public void passwordValidate_exceptionClassHasConstructor() {
boolean isInputParamPresent =
Arrays.stream(PasswordValidationException.class.getConstructors())
.flatMap(c -> Arrays.stream(c.getParameterTypes()))
.anyMatch(t -> t.equals(String.class));
Arrays.stream(PasswordValidationException.class.getConstructors())
.flatMap(c -> Arrays.stream(c.getParameterTypes()))
.anyMatch(t -> t.equals(String.class));
Assert.assertEquals("Don't hardcode the message in the exception class, "
+ "you should have constructor with message\n", true, isInputParamPresent);
+ "you should have constructor with message\n", true, isInputParamPresent);
}

@Test
Expand All @@ -96,29 +96,29 @@ public void passwordValidate_exceptionExpected() throws Exception {
passwordValidator.validate(user.getPassword(), user.getRepeatPassword());
} catch (Exception e) {
Assert.assertEquals("Your exception should have a message \"Wrong passwords\"\n",
"Wrong passwords", e.getMessage());
"Wrong passwords", e.getMessage());
}
}

@Test
public void passwordValidate_throwsExceptionExpected() {
Class<?>[] exceptionTypes = getValidateMethod().getExceptionTypes();
Assert.assertTrue("Add an exception to the signature of method validate()\n",
exceptionTypes.length != 0);
exceptionTypes.length != 0);

Assert.assertEquals("You should throw only one exception in signature of the " +
"method validate()\n", 1, exceptionTypes.length);
"method validate()\n", 1, exceptionTypes.length);

Assert.assertEquals("You should add your exception to signature of method validate()\n",
"PasswordValidationException", exceptionTypes[0].getSimpleName());
"PasswordValidationException", exceptionTypes[0].getSimpleName());
}

private Method getValidateMethod() {
return Arrays.stream(PasswordValidator.class.getDeclaredMethods())
.filter(m -> m.getName().equals("validate"))
.findFirst()
.orElseThrow(() -> new RuntimeException("Method validate() should be present " +
"in the PasswordValidator class"));
.filter(m -> m.getName().equals("validate"))
.findFirst()
.orElseThrow(() -> new RuntimeException("Method validate() should be present " +
"in the PasswordValidator class"));
}

@Test
Expand All @@ -128,7 +128,7 @@ public void passwordValidate_correctInputData() {
passwordValidator.validate(user.getPassword(), user.getRepeatPassword());
} catch (Exception e) {
Assert.assertEquals("Checking of passwords doesn't work correctly! " +
"We got exception while testing a valid input\n", true, false);
"We got exception while testing a valid input\n", true, false);
}
}

Expand All @@ -138,8 +138,8 @@ public void passwordValidate_incorrectInputData() {
try {
passwordValidator.validate(user.getPassword(), user.getRepeatPassword());
Assert.assertEquals("Validation should throw PasswordValidationException for parameters: "
+ "password - " + user.getPassword()
+ " and repeatPassword - " + user.getRepeatPassword() + "\n", true, false);
+ "password - " + user.getPassword()
+ " and repeatPassword - " + user.getRepeatPassword() + "\n", true, false);
} catch (Exception ignored) {
}
}
Expand All @@ -150,8 +150,8 @@ public void passwordValidate_shortInputData() {
try {
passwordValidator.validate(user.getPassword(), user.getRepeatPassword());
Assert.assertEquals("Validation should throw PasswordValidationException for parameters: "
+ "password - " + user.getPassword()
+ " and repeatPassword - " + user.getRepeatPassword() + "\n", true, false);
+ "password - " + user.getPassword()
+ " and repeatPassword - " + user.getRepeatPassword() + "\n", true, false);
} catch (Exception ignored) {
}
}
Expand All @@ -162,7 +162,7 @@ public void passwordValidate_emptyInputData() {
try {
passwordValidator.validate(user.getPassword(), user.getRepeatPassword());
Assert.assertEquals("Validation should throw PasswordValidationException for empty input data\n",
true, false);
true, false);
} catch (Exception ignored) {
}
}
Expand All @@ -173,10 +173,10 @@ public void passwordValidate_nullInputData() {
try {
passwordValidator.validate(user.getPassword(), user.getRepeatPassword());
Assert.assertEquals("Validation should throw PasswordValidationException for null input data\n",
true, false);
true, false);
} catch (NullPointerException e) {
Assert.assertEquals("Validation shouldn't throw NullPointerException for parameters: password - "
+ user.getPassword() + " and repeatPassword - " + user.getRepeatPassword() + "\n", true, false);
+ user.getPassword() + " and repeatPassword - " + user.getRepeatPassword() + "\n", true, false);
} catch (Exception ignored) {
}
}
Expand Down

0 comments on commit b6dbf9c

Please sign in to comment.