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

com.github.jinahya.assertj.validation.ConstraintDescriptorAssert 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.Assert;
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.metadata.ConstraintDescriptor;
import java.lang.annotation.Annotation;
import java.util.function.Function;

/**
 * An assertion interface for verifying {@link ConstraintDescriptor} values.
 *
 * @param    self type parameter
 * @param  actual type parameter
 * @param       type of constraint annotation
 */
public interface ConstraintDescriptorAssert<
        SELF extends ConstraintDescriptorAssert,
        ACTUAL extends ConstraintDescriptor,
        T extends Annotation>
        extends Assert {

    // ------------------------------------------------------------------------------------------------------ annotation
    > A extractingAnnotation(
            final Function extractor,
            final AssertFactory factory);

    default Assert extractingAnnotation() {
        return extractingAnnotation(ConstraintDescriptor::getAnnotation, new ObjectAssertFactory<>());
    }

    // ------------------------------------------------------------------------------------------------------ attributes
//    , String, Object>> A extractingAttributes(
//            final Function> attributesExtractor,
//            final AssertFactory assertFactory);
//
//    default , String, Object>> A extractingAttributes(
//            final AssertFactory assertFactory) {
//        return extractingAttributes(ConstraintDescriptor::getAttributes, assertFactory);
//    }

    // -------------------------------------------------------------------------------------------- composingConstraints
    CollectionAssert> extractingComposingConstraints();

    @SuppressWarnings({
            "unchecked"
    })
    default SELF doesNotHaveComposingConstraints() {
        extractingComposingConstraints().isEmpty();
        return (SELF) this;
    }

    // -------------------------------------------------------------------------------------- constraintValidatorClasses
//    ListAssert> extractingConstraintValidatorClasses();

    // ---------------------------------------------------------------------------------------------------------- groups
    CollectionAssert> extractingGroups();

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

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

    // --------------------------------------------------------------------------------------------- validationAppliesTo
    SELF hostsConstraintTarget(ConstraintTarget constraintTarget);

    default SELF doesNotHostAnyConstraintTarget() {
        return hostsConstraintTarget(null);
    }

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

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

    // ---------------------------------------------------------------------------------------------------------- unwrap
    > A extractingAsUnwrapped(
            final Class type,
            final AssertFactory assertFactory
    );

    /**
     * Verifies that the {@code actual} constraint descriptor value is equal to specified value when
     * {@link ConstraintDescriptor#unwrap(Class) unwrapped} as specified type.
     *
     * @param type                   the to be unwrapped as.
     * @param expectedUnwrappedValue expected unwrapped value.
     * @param                     the type of the unwrapped object.
     * @return this assertion object.
     * @see ConstraintDescriptor#unwrap(Class)
     */
    @SuppressWarnings({
            "unchecked"
    })
    default  SELF isEqualToWhenUnwrappedAs(final Class type, final Object expectedUnwrappedValue) {
        extractingAsUnwrapped(type, new ObjectAssertFactory<>())
                .isEqualTo(expectedUnwrappedValue);
        return (SELF) this;
    }
}