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.ObjectInstantiator; 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 de.cuioss.tools.logging.CuiLogger; 029import lombok.Getter; 030import lombok.NonNull; 031import lombok.RequiredArgsConstructor; 032 033/** 034 * @param <T> identifying the type of object to be instantiated 035 * 036 * @author Oliver Wolff 037 */ 038@RequiredArgsConstructor 039public class BeanInstantiator<T> implements ParameterizedInstantiator<T> { 040 041 private static final CuiLogger log = new CuiLogger(BeanInstantiator.class); 042 043 @NonNull 044 private final ObjectInstantiator<T> instantiator; 045 046 @NonNull 047 @Getter 048 private final RuntimeProperties runtimeProperties; 049 050 @Override 051 public T newInstance(final List<PropertySupport> properties, final boolean generatePropertyValues) { 052 assertNotNull(properties, PROPERTIES_MUST_NOT_BE_NULL); 053 final var instance = instantiator.newInstance(); 054 if (generatePropertyValues) { 055 properties.forEach(PropertySupport::generateTestValue); 056 } 057 for (final PropertySupport propertySupport : properties) { 058 if (!propertySupport.getPropertyMetadata().getPropertyReadWrite().isWriteable()) { 059 log.warn( 060 "Trying to apply a property '{}' which is not writable. Please check usage and configuration of @VerifyBeanProperty, maybe add the property to 'exclude' list.", 061 propertySupport.getName()); 062 } 063 propertySupport.apply(instance); 064 } 065 return instance; 066 } 067 068 @Override 069 public T newInstance(final List<PropertyMetadata> properties) { 070 assertNotNull(properties, PROPERTIES_MUST_NOT_BE_NULL); 071 return newInstance(properties.stream().map(PropertySupport::new).toList(), true); 072 } 073 074 @Override 075 public T newInstanceMinimal() { 076 return newInstance(runtimeProperties.getRequiredProperties()); 077 } 078 079 @Override 080 public T newInstanceFull() { 081 return newInstance(runtimeProperties.getAdditionalProperties()); 082 } 083 084 @Override 085 public String toString() { 086 final var builder = new StringBuilder(getClass().getName()); 087 builder.append("\nProperty Configuration: ").append(runtimeProperties.toString()); 088 return builder.toString(); 089 } 090}