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

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

package com.github.jinahya.assertj.validation;

/*-
 * #%L
 * assertj-bean-validation
 * %%
 * Copyright (C) 2021 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 static com.github.jinahya.assertj.validation.BeanValidationUtils.validate;
import static com.github.jinahya.assertj.validation.BeanValidationUtils.validateProperty;
import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;

/**
 * An assertion class for validating a bean object and its properties.
 *
 * @author Jin Kwon <onacit_at_gmail.com>
 */
public class BeanValidationAssert extends AbstractBeanValidationAssert {

    /**
     * Creates a new instance with specified bean object reference.
     *
     * @param actual the actual bean object; must be not {@code null}.
     * @see #actual
     */
    public BeanValidationAssert(final Object actual) {
        super(actual, BeanValidationAssert.class);
    }

    /**
     * Verifies that the {@link #actual} is valid.
     *
     * @return {@link #myself self}.
     * @see #using(Object)
     * @see #targeting(Class[])
     * @see javax...#validate(T,
     * Class...)
     * @see jakarta...#validate(T,
     * Class...)
     */
    public BeanValidationAssert isValid() {
        isNotNull();
        assertThat(validate(validator(), actual, groups())).isEmpty();
        return this;
    }

    /**
     * Verifies that the {@link #actual} has a valid property.
     *
     * @param propertyName the name of the property to verify.
     * @return {@link #myself self}
     * @see #using(Object)
     * @see #targeting(Class[])
     * @see javax...#validateProperty(T,
     * String, Class...)
     * @see jakarta...#validateProperty(T,
     * String, Class...)
     */
    public BeanValidationAssert hasValidProperty(final String propertyName) {
        requireNonNull(propertyName, "propertyName is null");
        isNotNull();
        assertThat(validateProperty(validator(), actual, propertyName, groups())).isEmpty();
        return this;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy