mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-08-05 20:22:43 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -1,63 +1,66 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.monad;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* The Monad pattern defines a monad structure, that enables chaining operations in pipelines and
|
||||
* processing data step by step. Formally, monad consists of a type constructor M and two
|
||||
* operations:
|
||||
* <br>bind - that takes monadic object and a function from plain object to the
|
||||
* monadic value and returns monadic value.
|
||||
* <br>return - that takes plain type object and returns this object wrapped in a monadic value.
|
||||
*
|
||||
* <p>In the given example, the Monad pattern is represented as a {@link Validator} that takes an
|
||||
* instance of a plain object with {@link Validator#of(Object)} and validates it {@link
|
||||
* Validator#validate(Function, Predicate, String)} against given predicates.
|
||||
*
|
||||
* <p>As a validation result {@link Validator#get()} either returns valid object
|
||||
* or throws {@link IllegalStateException} with list of exceptions collected during validation.
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
var user = new User("user", 24, Sex.FEMALE, "foobar.com");
|
||||
LOGGER.info(Validator.of(user).validate(User::name, Objects::nonNull, "name is null")
|
||||
.validate(User::name, name -> !name.isEmpty(), "name is empty")
|
||||
.validate(User::email, email -> !email.contains("@"), "email doesn't contains '@'")
|
||||
.validate(User::age, age -> age > 20 && age < 30, "age isn't between...").get()
|
||||
.toString());
|
||||
}
|
||||
}
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.monad;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* The Monad pattern defines a monad structure, that enables chaining operations in pipelines and
|
||||
* processing data step by step. Formally, monad consists of a type constructor M and two
|
||||
* operations: <br>
|
||||
* bind - that takes monadic object and a function from plain object to the monadic value and
|
||||
* returns monadic value. <br>
|
||||
* return - that takes plain type object and returns this object wrapped in a monadic value.
|
||||
*
|
||||
* <p>In the given example, the Monad pattern is represented as a {@link Validator} that takes an
|
||||
* instance of a plain object with {@link Validator#of(Object)} and validates it {@link
|
||||
* Validator#validate(Function, Predicate, String)} against given predicates.
|
||||
*
|
||||
* <p>As a validation result {@link Validator#get()} either returns valid object or throws {@link
|
||||
* IllegalStateException} with list of exceptions collected during validation.
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
var user = new User("user", 24, Sex.FEMALE, "foobar.com");
|
||||
LOGGER.info(
|
||||
Validator.of(user)
|
||||
.validate(User::name, Objects::nonNull, "name is null")
|
||||
.validate(User::name, name -> !name.isEmpty(), "name is empty")
|
||||
.validate(User::email, email -> !email.contains("@"), "email doesn't contains '@'")
|
||||
.validate(User::age, age -> age > 20 && age < 30, "age isn't between...")
|
||||
.get()
|
||||
.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +1,31 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.monad;
|
||||
|
||||
/**
|
||||
* Enumeration of Types of Sex.
|
||||
*/
|
||||
public enum Sex {
|
||||
MALE, FEMALE
|
||||
}
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.monad;
|
||||
|
||||
/** Enumeration of Types of Sex. */
|
||||
public enum Sex {
|
||||
MALE,
|
||||
FEMALE
|
||||
}
|
||||
|
||||
@@ -27,11 +27,9 @@ package com.iluwatar.monad;
|
||||
/**
|
||||
* Record class.
|
||||
*
|
||||
* @param name - name
|
||||
* @param age - age
|
||||
* @param sex - sex
|
||||
* @param name - name
|
||||
* @param age - age
|
||||
* @param sex - sex
|
||||
* @param email - email address
|
||||
*/
|
||||
public record User(String name, int age, Sex sex, String email) {
|
||||
}
|
||||
|
||||
public record User(String name, int age, Sex sex, String email) {}
|
||||
|
||||
@@ -1,121 +1,116 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.monad;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* Class representing Monad design pattern. Monad is a way of chaining operations on the given
|
||||
* object together step by step. In Validator each step results in either success or failure
|
||||
* indicator, giving a way of receiving each of them easily and finally getting validated object or
|
||||
* list of exceptions.
|
||||
*
|
||||
* @param <T> Placeholder for an object.
|
||||
*/
|
||||
public class Validator<T> {
|
||||
/**
|
||||
* Object that is validated.
|
||||
*/
|
||||
private final T obj;
|
||||
|
||||
/**
|
||||
* List of exception thrown during validation.
|
||||
*/
|
||||
private final List<Throwable> exceptions = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Creates a monadic value of given object.
|
||||
*
|
||||
* @param obj object to be validated
|
||||
*/
|
||||
private Validator(T obj) {
|
||||
this.obj = obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates validator against given object.
|
||||
*
|
||||
* @param t object to be validated
|
||||
* @param <T> object's type
|
||||
* @return new instance of a validator
|
||||
*/
|
||||
public static <T> Validator<T> of(T t) {
|
||||
return new Validator<>(Objects.requireNonNull(t));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the validation is successful.
|
||||
*
|
||||
* @param validation one argument boolean-valued function that represents one step of validation.
|
||||
* Adds exception to main validation exception list when single step validation
|
||||
* ends with failure.
|
||||
* @param message error message when object is invalid
|
||||
* @return this
|
||||
*/
|
||||
public Validator<T> validate(Predicate<? super T> validation, String message) {
|
||||
if (!validation.test(obj)) {
|
||||
exceptions.add(new IllegalStateException(message));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extension for the {@link Validator#validate(Predicate, String)} method, dedicated for objects,
|
||||
* that need to be projected before requested validation.
|
||||
*
|
||||
* @param projection function that gets an objects, and returns projection representing element to
|
||||
* be validated.
|
||||
* @param validation see {@link Validator#validate(Predicate, String)}
|
||||
* @param message see {@link Validator#validate(Predicate, String)}
|
||||
* @param <U> see {@link Validator#validate(Predicate, String)}
|
||||
* @return this
|
||||
*/
|
||||
public <U> Validator<T> validate(
|
||||
Function<? super T, ? extends U> projection,
|
||||
Predicate<? super U> validation,
|
||||
String message
|
||||
) {
|
||||
return validate(projection.andThen(validation::test)::apply, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Receives validated object or throws exception when invalid.
|
||||
*
|
||||
* @return object that was validated
|
||||
* @throws IllegalStateException when any validation step results with failure
|
||||
*/
|
||||
public T get() throws IllegalStateException {
|
||||
if (exceptions.isEmpty()) {
|
||||
return obj;
|
||||
}
|
||||
var e = new IllegalStateException();
|
||||
exceptions.forEach(e::addSuppressed);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
package com.iluwatar.monad;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* Class representing Monad design pattern. Monad is a way of chaining operations on the given
|
||||
* object together step by step. In Validator each step results in either success or failure
|
||||
* indicator, giving a way of receiving each of them easily and finally getting validated object or
|
||||
* list of exceptions.
|
||||
*
|
||||
* @param <T> Placeholder for an object.
|
||||
*/
|
||||
public class Validator<T> {
|
||||
/** Object that is validated. */
|
||||
private final T obj;
|
||||
|
||||
/** List of exception thrown during validation. */
|
||||
private final List<Throwable> exceptions = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Creates a monadic value of given object.
|
||||
*
|
||||
* @param obj object to be validated
|
||||
*/
|
||||
private Validator(T obj) {
|
||||
this.obj = obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates validator against given object.
|
||||
*
|
||||
* @param t object to be validated
|
||||
* @param <T> object's type
|
||||
* @return new instance of a validator
|
||||
*/
|
||||
public static <T> Validator<T> of(T t) {
|
||||
return new Validator<>(Objects.requireNonNull(t));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the validation is successful.
|
||||
*
|
||||
* @param validation one argument boolean-valued function that represents one step of validation.
|
||||
* Adds exception to main validation exception list when single step validation ends with
|
||||
* failure.
|
||||
* @param message error message when object is invalid
|
||||
* @return this
|
||||
*/
|
||||
public Validator<T> validate(Predicate<? super T> validation, String message) {
|
||||
if (!validation.test(obj)) {
|
||||
exceptions.add(new IllegalStateException(message));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extension for the {@link Validator#validate(Predicate, String)} method, dedicated for objects,
|
||||
* that need to be projected before requested validation.
|
||||
*
|
||||
* @param projection function that gets an objects, and returns projection representing element to
|
||||
* be validated.
|
||||
* @param validation see {@link Validator#validate(Predicate, String)}
|
||||
* @param message see {@link Validator#validate(Predicate, String)}
|
||||
* @param <U> see {@link Validator#validate(Predicate, String)}
|
||||
* @return this
|
||||
*/
|
||||
public <U> Validator<T> validate(
|
||||
Function<? super T, ? extends U> projection,
|
||||
Predicate<? super U> validation,
|
||||
String message) {
|
||||
return validate(projection.andThen(validation::test)::apply, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Receives validated object or throws exception when invalid.
|
||||
*
|
||||
* @return object that was validated
|
||||
* @throws IllegalStateException when any validation step results with failure
|
||||
*/
|
||||
public T get() throws IllegalStateException {
|
||||
if (exceptions.isEmpty()) {
|
||||
return obj;
|
||||
}
|
||||
var e = new IllegalStateException();
|
||||
exceptions.forEach(e::addSuppressed);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,15 +28,11 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Application Test
|
||||
*/
|
||||
|
||||
/** Application Test */
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,9 +30,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import java.util.Objects;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Test for Monad Pattern
|
||||
*/
|
||||
/** Test for Monad Pattern */
|
||||
class MonadTest {
|
||||
|
||||
@Test
|
||||
@@ -40,10 +38,8 @@ class MonadTest {
|
||||
var tom = new User(null, 21, Sex.MALE, "tom@foo.bar");
|
||||
assertThrows(
|
||||
IllegalStateException.class,
|
||||
() -> Validator.of(tom)
|
||||
.validate(User::name, Objects::nonNull, "name cannot be null")
|
||||
.get()
|
||||
);
|
||||
() ->
|
||||
Validator.of(tom).validate(User::name, Objects::nonNull, "name cannot be null").get());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -51,22 +47,23 @@ class MonadTest {
|
||||
var john = new User("John", 17, Sex.MALE, "john@qwe.bar");
|
||||
assertThrows(
|
||||
IllegalStateException.class,
|
||||
() -> Validator.of(john)
|
||||
.validate(User::name, Objects::nonNull, "name cannot be null")
|
||||
.validate(User::age, age -> age > 21, "user is underage")
|
||||
.get()
|
||||
);
|
||||
() ->
|
||||
Validator.of(john)
|
||||
.validate(User::name, Objects::nonNull, "name cannot be null")
|
||||
.validate(User::age, age -> age > 21, "user is underage")
|
||||
.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForValid() {
|
||||
var sarah = new User("Sarah", 42, Sex.FEMALE, "sarah@det.org");
|
||||
var validated = Validator.of(sarah)
|
||||
.validate(User::name, Objects::nonNull, "name cannot be null")
|
||||
.validate(User::age, age -> age > 21, "user is underage")
|
||||
.validate(User::sex, sex -> sex == Sex.FEMALE, "user is not female")
|
||||
.validate(User::email, email -> email.contains("@"), "email does not contain @ sign")
|
||||
.get();
|
||||
var validated =
|
||||
Validator.of(sarah)
|
||||
.validate(User::name, Objects::nonNull, "name cannot be null")
|
||||
.validate(User::age, age -> age > 21, "user is underage")
|
||||
.validate(User::sex, sex -> sex == Sex.FEMALE, "user is not female")
|
||||
.validate(User::email, email -> email.contains("@"), "email does not contain @ sign")
|
||||
.get();
|
||||
assertSame(validated, sarah);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user