Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ready #1361

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Ready #1361

Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 message) {
super(message);
}
}
12 changes: 10 additions & 2 deletions src/main/java/core/basesyntax/PasswordValidator.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
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) {
throw new PasswordValidationException("Wrong passwords");
}
if (password.equals(repeatPassword) && password.length() >= 10) {

} else {
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 + '\''
+ '}';
}
}
8 changes: 7 additions & 1 deletion src/main/java/core/basesyntax/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
55 changes: 28 additions & 27 deletions src/test/java/core/basesyntax/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -96,29 +97,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 +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);
}
}

Expand All @@ -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) {
}
}
Expand All @@ -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) {
}
}
Expand All @@ -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) {
}
}
Expand All @@ -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) {
}
}
Expand Down
Loading