<commit_before><commit_msg>Add the tests for the ValidatingBuilders checking the underlying validation.<commit_after>package io.spine.validate;

import com.google.protobuf.Timestamp;
import io.spine.Identifier;
import io.spine.net.EmailAddress;
import io.spine.people.PersonName;
import io.spine.test.validate.msg.builder.Contract;
import io.spine.test.validate.msg.builder.CustomerVBuilder;
import io.spine.test.validate.msg.builder.Product;
import io.spine.time.Time;
import org.junit.Before;
import org.junit.Test;

import static com.google.protobuf.util.Durations.fromSeconds;
import static com.google.protobuf.util.Timestamps.add;
import static io.spine.time.Time.getCurrentTime;

/**
 * A test suite covering the {@link ValidatingBuilder} behavior.
 *
 * <p>Since most {@code ValidatingBuilders} are generated, the concrete test suits for each of them
 * are not required.
 *
 * <p>Any {@code ValidatingBuilder} implementation should pass these tests. When implementing your
 * own {@code ValidatingBuilder}, be sure to check if it fits the constraints stated below.
 *
 * @author <NAME>
 */
public class ValidatingBuilderShould {

    private CustomerVBuilder builder;

    @Before
    public void setUp() {
        builder = fill();
    }

    @Test(expected = ValidationException.class)
    public void check_required_validated_repeated_fields() {
        builder.addName("invalid_name");
    }

    @Test(expected = ValidationException.class)
    public void ensure_required_validated_repeated_fields() {
        builder.clearName();
        builder.build();
    }

    @Test(expected = ValidationException.class)
    public void check_required_validated_map_field_keys() {
        builder.putPositionInCompany("", PersonName.newBuilder()
                                                   .setGivenName("Abraham")
                                                   .setFamilyName("Lincoln")
                                                   .build());
    }

    @Test(expected = ValidationException.class)
    public void check_required_validated_map_field_values() {
        builder.putPositionInCompany("President of the US", PersonName.getDefaultInstance());
    }

    @Test(expected = ValidationException.class)
    public void ensure_required_validated_map_fields() {
        builder.clearPositionInCompany();
        builder.build();
    }

    @Test(expected = ValidationException.class)
    public void check_validated_repeated_fields() {
        builder.addEmail(EmailAddress.getDefaultInstance());
    }

    @Test
    public void dispense_with_validated_repeated_fields() {
        builder.clearEmail();
        builder.build();
    }

    @Test(expected = ValidationException.class)
    public void check_validated_map_field_keys() {
        final Timestamp timeInPast = add(getCurrentTime(), fromSeconds(-10L));
        builder.putContract("", Contract.newBuilder()
                                        .setSince(timeInPast)
                                        .setText("My contract text")
                                        .build());
    }

    @Test(expected = ValidationException.class)
    public void check_validated_map_field_values() {
        builder.putContract(Identifier.newUuid(), Contract.getDefaultInstance());
    }

    @Test
    public void dispense_with_validated_map_fields() {
        builder.clearContract();
        builder.build();
    }

    @Test
    public void accept_any_required_repeated_fields() {
        builder.addProduct(Product.UNRECOGNIZED);
    }

    @Test(expected = ValidationException.class)
    public void ensure_required_repeated_fields() {
        builder.clearProduct();
        builder.build();
    }

    @Test
    public void accept_any_required_map_field_key() {
        builder.putAppearedOnMarket("", Time.getCurrentTime());
    }

    @Test
    public void accept_any_required_map_field_value() {
        builder.putAppearedOnMarket("Montenegro", Timestamp.getDefaultInstance());
    }

    @Test(expected = ValidationException.class)
    public void ensure_required_map_fields() {
        builder.clearAppearedOnMarket();
        builder.build();
    }

    @Test
    public void accept_any_unchecked_repeated_fields() {
        builder.addFaxNumber("Who knows how do they actually look like?");
    }

    @Test
    public void dispense_with_unchecked_repeated_fields() {
        builder.clearFaxNumber();
        builder.build();
    }

    @Test
    public void accept_any_unchecked_map_field_key() {
        final Timestamp timeInPast = add(getCurrentTime(), fromSeconds(-1000L));
        builder.putOldContract("", Contract.newBuilder()
                                           .setSince(timeInPast)
                                           .setText("My very old contract text")
                                           .build());
    }

    @Test
    public void accept_any_unchecked_map_field_value() {
        builder.putOldContract("Forgotten", Contract.getDefaultInstance());
    }

    @Test
    public void dispense_with_unchecked_map_fields() {
        builder.clearOldContract();
        builder.build();
    }

    /**
     * Creates a valid {@link CustomerVBuilder} instance.
     */
    private static CustomerVBuilder fill() {
        final CustomerVBuilder builder = CustomerVBuilder.newBuilder();
        builder.build(); // Ensure no ValidationException is thrown.
        return builder;
    }
}
{'fraction_non_alphanumeric': 0.09525567657374931, 'fraction_numerical': 0.0011076241462063873, 'mean_word_length': 2.6682464454976302, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2892126', 'n_tokens_mistral': 1435, 'n_tokens_neox': 1393, 'n_words': 287}