001/* 002 * Copyright 2023 the original author or authors. 003 * <p> 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * <p> 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * <p> 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package de.cuioss.test.valueobjects.objects.impl; 017 018import static de.cuioss.test.valueobjects.objects.impl.AbstractInlineInstantiator.PROPERTIES_MUST_NOT_BE_NULL; 019import static org.junit.jupiter.api.Assertions.assertNotNull; 020 021import java.util.List; 022 023import de.cuioss.test.valueobjects.objects.BuilderInstantiator; 024import de.cuioss.test.valueobjects.objects.ParameterizedInstantiator; 025import de.cuioss.test.valueobjects.objects.RuntimeProperties; 026import de.cuioss.test.valueobjects.property.PropertyMetadata; 027import de.cuioss.test.valueobjects.property.PropertySupport; 028import lombok.Getter; 029import lombok.NonNull; 030import lombok.RequiredArgsConstructor; 031 032/** 033 * @author Oliver Wolff 034 * @param <T> identifying the type of object to be instantiated 035 */ 036@RequiredArgsConstructor 037public class BuilderParameterizedInstantiator<T> implements ParameterizedInstantiator<T> { 038 039 @NonNull 040 private final BuilderInstantiator<T> instantiator; 041 042 @NonNull 043 @Getter 044 private final RuntimeProperties runtimeProperties; 045 046 @Override 047 public T newInstance(final List<PropertySupport> properties, final boolean generatePropertyValues) { 048 assertNotNull(properties, PROPERTIES_MUST_NOT_BE_NULL); 049 050 final var builder = instantiator.newBuilderInstance(); 051 052 if (generatePropertyValues) { 053 properties.forEach(PropertySupport::generateTestValue); 054 } 055 for (final PropertySupport propertySupport : properties) { 056 propertySupport.apply(builder); 057 } 058 return instantiator.build(builder); 059 } 060 061 @Override 062 public T newInstance(final List<PropertyMetadata> properties) { 063 assertNotNull(properties, PROPERTIES_MUST_NOT_BE_NULL); 064 return newInstance(properties.stream().map(PropertySupport::new).toList(), true); 065 } 066 067 @Override 068 public T newInstanceMinimal() { 069 return newInstance(runtimeProperties.getRequiredProperties()); 070 } 071 072 @Override 073 public T newInstanceFull() { 074 return newInstance(runtimeProperties.getAllProperties()); 075 } 076 077 @Override 078 public String toString() { 079 final var builder = new StringBuilder(getClass().getName()); 080 builder.append("\nInstantiator: ").append(instantiator).append(runtimeProperties.toString()); 081 return builder.toString(); 082 } 083 084}