An Introduction to Vavr's Validation API - Alejandro Gervasio | alejandro.gervasio@gmail.com (#2526)

* Initial Commit

* Refactored Application class

* Refactored Application class

* Refactored Application class

* Refactored User class

* Added ValidationTest class

* Reorganized imports in ValidatorTest class

* Reformatted source code

* Reformatted source code
This commit is contained in:
Alejandro Gervasio 2017-09-01 18:33:58 -03:00 committed by Zeger Hendrikse
parent 86c1d6d22d
commit 7e26990eb9
5 changed files with 155 additions and 0 deletions

View File

@ -0,0 +1,17 @@
package com.baeldung.vavrvalidation.application;
import com.baeldung.vavrvalidation.model.User;
import com.baeldung.vavrvalidation.validator.UserValidator;
import io.vavr.collection.Seq;
import io.vavr.control.Validation;
public class Application {
public static void main(String[] args) {
UserValidator userValidator = new UserValidator();
Validation<Seq<String>, User> validation = userValidator.validateUser("John", "john@domain.com");
// process validation results here
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.vavrvalidation.model;
public class User {
private String name;
private String email;
public User(String name, String email) {
this.name = name;
this.email = email;
}
@Override
public String toString() {
return "User [name=" + name + ", email=" + email + "]";
}
// standard setters and getters
}

View File

@ -0,0 +1,29 @@
package com.baeldung.vavrvalidation.validator;
import com.baeldung.vavrvalidation.model.User;
import io.vavr.collection.CharSeq;
import io.vavr.collection.Seq;
import io.vavr.control.Validation;
public class UserValidator {
private static final String NAME_PATTERN = "^[a-zA-Z0-9]+$";
private static final String NAME_ERROR = "Name contains invalid characters";
private static final String EMAIL_PATTERN =
"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
private static final String EMAIL_ERROR = "Email must be a well-formed email address";
public Validation<Seq<String>, User> validateUser(String name, String email) {
return Validation
.combine(validateField(name, NAME_PATTERN, NAME_ERROR)
,validateField(email, EMAIL_PATTERN, EMAIL_ERROR))
.ap(User::new);
}
private Validation<String, String> validateField(String field, String pattern, String error) {
return CharSeq.of(field).replaceAll(pattern, "").transform(seq -> seq.isEmpty()
? Validation.valid(field)
: Validation.invalid(error));
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.vavrvalidation.validator;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.instanceOf;
import org.junit.Test;
import com.baeldung.vavrvalidation.validator.UserValidator;
import io.vavr.control.Validation.Invalid;
import io.vavr.control.Validation.Valid;
public class UserValidatorTest {
@Test
public void givenValidUserParams_whenValidated_thenInstanceOfValid() {
UserValidator userValidator = new UserValidator();
assertThat(userValidator.validateUser("John", "john@domain.com"), instanceOf(Valid.class));
}
@Test
public void givenInvalidUserParams_whenValidated_thenInstanceOfInvalid() {
UserValidator userValidator = new UserValidator();
assertThat(userValidator.validateUser("John", "no-email"), instanceOf(Invalid.class));
}
}

View File

@ -0,0 +1,68 @@
package com.baeldung.vavrvalidation.validator;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.baeldung.vavrvalidation.model.User;
import io.vavr.collection.Seq;
import io.vavr.control.Either.Right;
import io.vavr.control.Validation.Invalid;
import io.vavr.control.Validation.Valid;
public class ValidationTest {
private static UserValidator userValidator;
@BeforeClass
public static void setUpUserValidatorInstance() {
userValidator = new UserValidator();
}
@AfterClass
public static void teardownUserValidatorInstance() {
userValidator = null;
}
@Test
public void givenInvalidUserParams_whenValidated_thenInvalidInstance() {
assertThat(userValidator.validateUser("John", "no-email"), instanceOf(Invalid.class));
}
@Test
public void givenValidUserParams_whenValidated_thenValidInstance() {
assertThat(userValidator.validateUser("John", "john@domain.com"), instanceOf(Valid.class));
}
@Test
public void givenInvalidUserParams_whenValidated_thenTrueWithIsInvalidMethod() {
assertTrue(userValidator.validateUser("John", "no-email").isInvalid());
}
@Test
public void givenValidUserParams_whenValidated_thenTrueWithIsValidMethod() {
assertTrue(userValidator.validateUser("John", "john@domain.com").isValid());
}
@Test
public void givenInValidUserParams_withGetErrorMethod_thenGetErrorMessages() {
assertEquals("Name contains invalid characters, Email must be a well-formed email address", userValidator.validateUser(" ", "no-email")
.getError().intersperse(", ").fold("", String::concat));
}
@Test
public void givenValidUserParams_withGetMethod_thenGetUserInstance() {
assertThat(userValidator.validateUser("John", "john@domain.com").get(), instanceOf(User.class));
}
@Test
public void givenValidUserParams_withtoEitherMethod_thenRightInstance() {
assertThat(userValidator.validateUser("John", "john@domain.com").toEither(), instanceOf(Right.class));
}
@Test
public void givenValidUserParams_withFoldMethodWithListlengthUserHashCode_thenEqualstoParamsLength() {
assertEquals(2, (int) userValidator.validateUser(" ", " ").fold(Seq::length, User::hashCode));
}
}