All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.jinahya.assertj.validation.AbstractConstraintDescriptorAssert Maven / Gradle / Ivy

The newest version!
package com.github.jinahya.assertj.validation;

/*-
 * #%L
 * assertj-bean-validation
 * %%
 * Copyright (C) 2021 - 2022 Jinahya, Inc.
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.AssertFactory;
import org.assertj.core.api.CollectionAssert;
import org.assertj.core.api.ObjectAssertFactory;

import javax.validation.ConstraintTarget;
import javax.validation.ConstraintViolation;
import javax.validation.metadata.ConstraintDescriptor;
import java.lang.annotation.Annotation;
import java.util.function.Function;

/**
 * An abstract assertion class for verifying {@link ConstraintViolation} values.
 *
 * @param    self type parameter
 * @param  actual type parameter
 * @param       constraint's annotation type
 */
public abstract class AbstractConstraintDescriptorAssert<
        SELF extends AbstractConstraintDescriptorAssert, ACTUAL extends ConstraintDescriptor, T extends Annotation>
        extends AbstractAssert
        implements ConstraintDescriptorAssert {

    /**
     * Creates a new instance with specified arguments.
     *
     * @param actual   an actual value to verify.
     * @param selfType self type.
     */
    protected AbstractConstraintDescriptorAssert(final ACTUAL actual, final Class selfType) {
        super(actual, selfType);
    }

    // ------------------------------------------------------------------------------------------------------ annotation

    @Override
    public > A extractingAnnotation(
            final Function extractor,
            final AssertFactory factory) {
        return isNotNull()
                .extracting(extractor, factory);
    }

    // -------------------------------------------------------------------------------------------- composingConstraints
    @Override
    public CollectionAssert> extractingComposingConstraints() {
        return isNotNull()
                .extracting(ConstraintDescriptor::getComposingConstraints, CollectionAssert::new);
    }

// -------------------------------------------------------------------------------------- constraintValidatorClasses

    // ---------------------------------------------------------------------------------------------------------- groups

    @Override
    public CollectionAssert> extractingGroups() {
        return isNotNull()
                .extracting(ConstraintDescriptor::getGroups, CollectionAssert::new);
    }

    // ------------------------------------------------------------------------------------------------- messageTemplate

    // --------------------------------------------------------------------------------------------------------- payload

    // --------------------------------------------------------------------------------------------- validationAppliesTo
    @Override
    public SELF hostsConstraintTarget(final ConstraintTarget constraintTarget) {
        isNotNull()
                .extracting(ConstraintDescriptor::getValidationAppliesTo, new ObjectAssertFactory<>())
                .isSameAs(constraintTarget);
        return myself;
    }

    // --------------------------------------------------------------------------------------------------- valueWrapping

    // ----------------------------------------------------------------------------------------- reportAsSingleViolation

    // ---------------------------------------------------------------------------------------------------------- unwrap

    @Override
    public > A extractingAsUnwrapped(
            final Class type,
            final AssertFactory assertFactory) {
        return isNotNull()
                .extracting(a -> a.unwrap(type), assertFactory)
                ;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy