diff --git a/src/main/java/core/basesyntax/PasswordValidationException.java b/src/main/java/core/basesyntax/PasswordValidationException.java index c9e7ed3e7..70c1d49fd 100644 --- a/src/main/java/core/basesyntax/PasswordValidationException.java +++ b/src/main/java/core/basesyntax/PasswordValidationException.java @@ -1,3 +1,7 @@ package core.basesyntax; -//write your code here +public class PasswordValidationException extends Exception { + public PasswordValidationException(String message) { + super(message); + } +} diff --git a/src/main/java/core/basesyntax/PasswordValidator.java b/src/main/java/core/basesyntax/PasswordValidator.java index d4655c9f5..0b1b0abaf 100644 --- a/src/main/java/core/basesyntax/PasswordValidator.java +++ b/src/main/java/core/basesyntax/PasswordValidator.java @@ -1,7 +1,13 @@ package core.basesyntax; public class PasswordValidator { - public void validate(String password, String repeatPassword) { - //write your code here + public void validate(String password, String repeatPassword) + throws PasswordValidationException { + if (password == null + || repeatPassword == null + || !password.equals(repeatPassword) + || password.length() < 10) { + throw new PasswordValidationException("Wrong passwords"); + } } } diff --git a/src/main/java/core/basesyntax/User.java b/src/main/java/core/basesyntax/User.java index e702946a8..fd3b5205e 100644 --- a/src/main/java/core/basesyntax/User.java +++ b/src/main/java/core/basesyntax/User.java @@ -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 + '\'' + + '}'; } } diff --git a/src/main/java/core/basesyntax/UserService.java b/src/main/java/core/basesyntax/UserService.java index 52f08d130..b8b2d3eb1 100644 --- a/src/main/java/core/basesyntax/UserService.java +++ b/src/main/java/core/basesyntax/UserService.java @@ -2,7 +2,13 @@ public class UserService { public void registerUser(User user) { - //write your code here + PasswordValidator validator = new PasswordValidator(); + try { + validator.validate(user.getPassword(), user.getRepeatPassword()); + saveUser(user); + } catch (PasswordValidationException e) { + System.out.println("Your passwords are incorrect. Try again."); + } } public void saveUser(User user) { diff --git a/src/test/java/core/basesyntax/UserServiceTest.java b/src/test/java/core/basesyntax/UserServiceTest.java index 757d0f4ec..2a708af0f 100644 --- a/src/test/java/core/basesyntax/UserServiceTest.java +++ b/src/test/java/core/basesyntax/UserServiceTest.java @@ -5,6 +5,7 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; + import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.lang.reflect.Method; @@ -40,7 +41,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 @@ -50,32 +51,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 @@ -96,7 +97,7 @@ 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()); } } @@ -104,21 +105,21 @@ public void passwordValidate_exceptionExpected() throws Exception { 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 @@ -128,7 +129,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); } } @@ -138,8 +139,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) { } } @@ -150,8 +151,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) { } } @@ -162,7 +163,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) { } } @@ -173,10 +174,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) { } }