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;
017
018import java.io.Serializable;
019import java.util.List;
020import java.util.SortedSet;
021
022import org.junit.jupiter.api.BeforeEach;
023import org.junit.jupiter.api.extension.ExtendWith;
024
025import de.cuioss.test.generator.TypedGenerator;
026import de.cuioss.test.generator.junit.GeneratorControllerExtension;
027import de.cuioss.test.valueobjects.api.property.PropertyConfig;
028import de.cuioss.test.valueobjects.api.property.PropertyConfigs;
029import de.cuioss.test.valueobjects.junit5.extension.GeneratorRegistryController;
030import de.cuioss.test.valueobjects.property.PropertyMetadata;
031import de.cuioss.test.valueobjects.util.GeneratorRegistry;
032import de.cuioss.test.valueobjects.util.ReflectionHelper;
033import de.cuioss.tools.reflect.MoreReflection;
034import lombok.Getter;
035import lombok.Setter;
036
037/**
038 * Base class for dynamically testing properties. It provides the handling of
039 * {@link PropertyMetadata} and {@link TypedGenerator}. In addition it computes
040 * the runtime class of the Generic type T and exposes it accordingly, see
041 * {@link #getTargetBeanClass()}
042 * <h2>Configuration</h2> The tests can be configured using certain annotations,
043 * depending on what you want to achieve:
044 * <ul>
045 * <li>Properties: The configuration of properties to be tested can be tweaked
046 * in multiple ways, see {@link de.cuioss.test.valueobjects.api.property} for
047 * details</li>
048 * <li>{@link TypedGenerator}: see {@link GeneratorRegistry} for
049 * documentation</li>
050 * </ul>
051 * Usage examples can be found at the package-documentation:
052 * {@link de.cuioss.test.valueobjects.junit5}
053 *
054 * @param <T> identifying the type to be tested is usually but not necessarily
055 *            at least {@link Serializable}.
056 * @author Oliver Wolff
057 */
058@SuppressWarnings("squid:S2187") // owolff: this is a base class for concrete tests
059@ExtendWith({ GeneratorControllerExtension.class, GeneratorRegistryController.class })
060public class PropertyAwareTest<T> implements GeneratorRegistry {
061
062    @Getter
063    @Setter
064    private Class<T> targetBeanClass;
065
066    @Getter
067    private List<PropertyMetadata> propertyMetadata;
068
069    /**
070     * Initializes all contracts, properties and generator
071     */
072    @BeforeEach
073    public void initializePropertiesAndGenerators() {
074        targetBeanClass = MoreReflection.extractFirstGenericTypeArgument(getClass());
075
076        propertyMetadata = resolvePropertyMetadata();
077    }
078
079    /**
080     * Resolves the {@link PropertyMetadata} by using reflections and the
081     * annotations {@link PropertyConfig} and / {@link PropertyConfigs} if provided
082     *
083     * @return a {@link SortedSet} of {@link PropertyMetadata} defining the base
084     *         line for the configured attributes
085     */
086    protected List<PropertyMetadata> resolvePropertyMetadata() {
087        return ReflectionHelper.handlePropertyMetadata(getClass(), getTargetBeanClass());
088    }
089}