fr.vergne.pester.junit.TestCasesGenerator Maven / Gradle / Ivy
Show all versions of pester-core Show documentation
package fr.vergne.pester.junit;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.params.provider.Arguments;
import fr.vergne.pester.definition.ConstructorDefinition;
import fr.vergne.pester.definition.PojoDefinition;
import fr.vergne.pester.definition.PropertyDefinition;
import fr.vergne.pester.value.Nullable;
public class TestCasesGenerator {
public static Stream streamTestCases(PojoDefinition> pojoDefinition, TestParameter[] testParameters) {
return Stream.of(pojoDefinition)
.flatMap(toDefinitionItems())
.map(toOptionalArguments(testParameters))
.filter(hasAllArguments())
.distinct()
.map(toTestArguments());
}
private static Function, Stream>> toDefinitionItems() {
return pojoDefinition -> {
// Retrieve the various data sources
Collection> parentClasses = new LinkedList<>(Arrays.asList(pojoDefinition.getParentClass().orElse(null)));
Collection> interfaceClasses = new LinkedList<>(pojoDefinition.getInterfaces());
Collection> constructors = new LinkedList<>(pojoDefinition.getConstructors());
Collection> properties = new LinkedList<>(pojoDefinition.getProperties());
// Ensure each of them has at least one element
Stream.of(parentClasses, interfaceClasses, constructors, properties)
.filter(collection -> collection.isEmpty())
.forEach(collection -> collection.add(null));
// Generate the combinations
// The extra null prevents from having zero data because of an empty source
return parentClasses.stream()
.flatMap(parentClass -> interfaceClasses.stream()
.flatMap(interfaceClass -> constructors.stream()
.flatMap(constructorDefinition -> properties.stream()
.map(propertyDefinition -> new DefinitionItem<>(
Optional.ofNullable(pojoDefinition),
Optional.ofNullable(constructorDefinition),
Optional.ofNullable(propertyDefinition),
Optional.ofNullable(parentClass),
Optional.ofNullable(interfaceClass))))));
};
}
private static Function>, Arguments> toTestArguments() {
return arguments -> Arguments.of(arguments.stream()
.map(Optional::get)
.map(nullableToValue())
.toArray());
}
private static Predicate>> hasAllArguments() {
return arguments -> arguments.stream().allMatch(Optional::isPresent);
}
private static Function, List>> toOptionalArguments(TestParameter[] testParameters) {
return definitionItem -> Stream.of(testParameters)
.map(parameter -> parameter.extractFrom(definitionItem))
.map(nonOptionalToOptional())
.collect(Collectors.toList());
}
private static Function