|
| 1 | +package com.baeldung.vavrvalidation.validator; |
| 2 | + |
| 3 | +import static org.hamcrest.CoreMatchers.*; |
| 4 | +import static org.junit.Assert.*; |
| 5 | +import org.junit.AfterClass; |
| 6 | +import org.junit.BeforeClass; |
| 7 | +import org.junit.Test; |
| 8 | +import com.baeldung.vavrvalidation.model.User; |
| 9 | +import io.vavr.collection.Seq; |
| 10 | +import io.vavr.control.Either.Right; |
| 11 | +import io.vavr.control.Validation.Invalid; |
| 12 | +import io.vavr.control.Validation.Valid; |
| 13 | + |
| 14 | +public class ValidationTest { |
| 15 | + |
| 16 | + private static UserValidator userValidator; |
| 17 | + |
| 18 | + @BeforeClass |
| 19 | + public static void setUpUserValidatorInstance() { |
| 20 | + userValidator = new UserValidator(); |
| 21 | + } |
| 22 | + |
| 23 | + @AfterClass |
| 24 | + public static void teardownUserValidatorInstance() { |
| 25 | + userValidator = null; |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void givenInvalidUserParams_whenValidated_thenInvalidInstance() { |
| 30 | + assertThat(userValidator.validateUser("John", "no-email"), instanceOf(Invalid.class)); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void givenValidUserParams_whenValidated_thenValidInstance() { |
| 35 | + assertThat(userValidator.validateUser("John", "john@domain.com"), instanceOf(Valid.class)); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void givenInvalidUserParams_whenValidated_thenTrueWithIsInvalidMethod() { |
| 40 | + assertTrue(userValidator.validateUser("John", "no-email").isInvalid()); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void givenValidUserParams_whenValidated_thenTrueWithIsValidMethod() { |
| 45 | + assertTrue(userValidator.validateUser("John", "john@domain.com").isValid()); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void givenInValidUserParams_withGetErrorMethod_thenGetErrorMessages() { |
| 50 | + assertEquals("Name contains invalid characters, Email must be a well-formed email address", userValidator.validateUser(" ", "no-email") |
| 51 | + .getError().intersperse(", ").fold("", String::concat)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void givenValidUserParams_withGetMethod_thenGetUserInstance() { |
| 56 | + assertThat(userValidator.validateUser("John", "john@domain.com").get(), instanceOf(User.class)); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void givenValidUserParams_withtoEitherMethod_thenRightInstance() { |
| 61 | + assertThat(userValidator.validateUser("John", "john@domain.com").toEither(), instanceOf(Right.class)); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void givenValidUserParams_withFoldMethodWithListlengthUserHashCode_thenEqualstoParamsLength() { |
| 66 | + assertEquals(2, (int) userValidator.validateUser(" ", " ").fold(Seq::length, User::hashCode)); |
| 67 | + } |
| 68 | +} |
0 commit comments