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.io.Serializable; 022import java.util.List; 023 024import de.cuioss.test.valueobjects.objects.ConfigurationCallBackHandler; 025import de.cuioss.test.valueobjects.objects.ParameterizedInstantiator; 026import de.cuioss.test.valueobjects.objects.RuntimeProperties; 027import de.cuioss.test.valueobjects.objects.TestObjectProvider; 028import de.cuioss.test.valueobjects.property.PropertyMetadata; 029import de.cuioss.test.valueobjects.property.PropertySupport; 030import lombok.Getter; 031import lombok.NonNull; 032import lombok.RequiredArgsConstructor; 033 034/** 035 * Variant of {@link ParameterizedInstantiator} that deals with test, where the 036 * actual beans to be tested are Injected by cdi, see {@link TestObjectProvider} 037 * as well 038 * 039 * @param <T> identifying the type to be tested is usually but not necessarily 040 * at least {@link Serializable}. 041 * @author Oliver Wolff 042 */ 043@RequiredArgsConstructor 044public class InjectedBeanInstantiator<T> implements ParameterizedInstantiator<T> { 045 046 private final TestObjectProvider<T> objectProvider; 047 048 private final ConfigurationCallBackHandler<T> callbackHandler; 049 050 @Getter 051 @NonNull 052 private final RuntimeProperties runtimeProperties; 053 054 @Override 055 public T newInstance(final List<PropertySupport> properties, final boolean generatePropertyValues) { 056 assertNotNull(properties, PROPERTIES_MUST_NOT_BE_NULL); 057 final var instance = objectProvider.getUnderTest(); 058 assertNotNull(instance, "Unable to obtain instance from " + objectProvider.toString()); 059 if (generatePropertyValues) { 060 properties.forEach(PropertySupport::generateTestValue); 061 } 062 for (final PropertySupport propertySupport : properties) { 063 propertySupport.apply(instance); 064 } 065 callbackHandler.configure(instance); 066 return instance; 067 } 068 069 @Override 070 public T newInstanceFull() { 071 return newInstance(runtimeProperties.getAdditionalProperties()); 072 } 073 074 @Override 075 public T newInstance(final List<PropertyMetadata> properties) { 076 assertNotNull(properties, PROPERTIES_MUST_NOT_BE_NULL); 077 return newInstance(properties.stream().map(PropertySupport::new).toList(), true); 078 } 079 080 @Override 081 public T newInstanceMinimal() { 082 return newInstance(runtimeProperties.getRequiredProperties()); 083 } 084 085 @Override 086 public String toString() { 087 final var builder = new StringBuilder(getClass().getName()); 088 builder.append("\nProperty Configuration: ").append(runtimeProperties.toString()); 089 return builder.toString(); 090 } 091 092}