/*
 * Copyright 2017, TeamDev Ltd. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.spine3.server.validate;

import com.google.protobuf.Descriptors.FieldDescriptor;
import com.google.protobuf.Timestamp;
import org.junit.Before;
import org.junit.Test;
import org.spine3.base.ConversionException;
import org.spine3.test.aggregate.ProjectId;
import org.spine3.test.aggregate.TaskId;
import org.spine3.test.types.Task;
import org.spine3.test.validate.msg.PatternStringFieldValue;
import org.spine3.type.ParametrizedTypeImpl;
import org.spine3.validate.ConstraintViolationThrowable;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import static com.google.common.collect.Lists.newArrayList;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.spine3.base.Identifiers.newUuid;

/**
 * @author Illia Shepilov
 */
public class AbstractValidatingBuilderShould {

    private AbstractValidatingBuilder<Task> validatingBuilder;

    @Before
    public void setUp() {
        validatingBuilder = new ConcreteValidatingBuilder();
    }

    @Test
    public void return_converted_value() throws ConversionException {
        final ParameterizedType type = ParametrizedTypeImpl.from(List.class,
                                                                 new Type[]{Integer.class});
        final List<Integer> convertedValue =
                validatingBuilder.getConvertedValue(type, "1");
        final List<Integer> expectedList = newArrayList(1);
        assertThat(convertedValue, is(expectedList));
    }

    @Test(expected = ConversionException.class)
    public void throw_exception_when_appropriate_stringifier_is_not_found() throws
                                                                            ConversionException {
        final String stringToConvert = "{value:1}";
        validatingBuilder.getConvertedValue(TaskId.class, stringToConvert);
    }

    @Test(expected = ConversionException.class)
    public void throw_exception_when_string_cannot_be_converted() throws ConversionException {
        final String stringToConvert = "";
        final Type type = ParametrizedTypeImpl.from(List.class, new Type[]{Timestamp.class});
        validatingBuilder.getConvertedValue(type, stringToConvert);
    }

    @Test
    public void validate_value() throws ConstraintViolationThrowable {
        final FieldDescriptor descriptor = ProjectId.getDescriptor()
                                                    .getFields()
                                                    .get(0);
        validatingBuilder.validate(descriptor, newUuid(), "id");
    }

    @Test(expected = ConstraintViolationThrowable.class)
    public void throw_exception_when_field_contains_constraint_violations()
            throws ConstraintViolationThrowable {
        final FieldDescriptor descriptor = PatternStringFieldValue.getDescriptor()
                                                                  .getFields()
                                                                  .get(0);
        validatingBuilder.validate(descriptor,
                                   "incorrectEmail",
                                   "email");
    }

    private static class ConcreteValidatingBuilder extends AbstractValidatingBuilder<Task> {
        @Override
        public Task build() {
            return Task.getDefaultInstance();
        }
    }
}

@@ -1233,16 +1233,148 @@
 eption;%0A
+import org.spine3.base.MissingStringifierException;%0Aimport org.spine3.base.Stringifier;%0Aimport org.spine3.base.StringifierRegistry;%0A
 import o
@@ -1405,24 +1405,24 @@
 .ProjectId;%0A
-
 import org.s
@@ -1955,16 +1955,76 @@
 newUuid;
+%0Aimport static org.spine3.base.Stringifiers.listStringifier;
 %0A%0A/**%0A *
@@ -2511,32 +2511,218 @@
 nteger.class%7D);%0A
+        final Stringifier%3CList%3CInteger%3E%3E stringifier = listStringifier(Integer.class);%0A        StringifierRegistry.getInstance()%0A                           .register(stringifier, type);%0A
         final Li
@@ -2952,34 +2952,42 @@
 (expected = 
-Conversion
+MissingStringifier
 Exception.cl
@@ -3493,16 +3493,16 @@
 t = %22%22;%0A
-
         
@@ -3579,32 +3579,222 @@
 estamp.class%7D);%0A
+        final Stringifier%3CList%3CTimestamp%3E%3E stringifier = listStringifier(Timestamp.class);%0A        StringifierRegistry.getInstance()%0A                           .register(stringifier, type);%0A
         validati

Fix tests for the `AbstractValidatingBuilder` class.