/*
 * 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.entity;

import com.google.protobuf.Message;
import com.google.protobuf.StringValue;
import org.junit.Test;
import org.spine3.base.Error;
import org.spine3.server.aggregate.AggregatePart;
import org.spine3.test.entity.number.NaturalNumber;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import static java.lang.System.lineSeparator;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.spine3.server.entity.AbstractEntity.getConstructor;
import static org.spine3.server.entity.EntityStateValidationError.INVALID_ENTITY_STATE;

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

    @SuppressWarnings("unchecked")
    // Supply a "wrong" value on purpose to cause the validation failure.
    @Test(expected = IllegalStateException.class)
    public void throw_exception_when_aggregate_does_not_have_appropriate_constructor() {
        getConstructor(AggregatePart.class, String.class);
    }

    @Test
    public void define_final_updateState_method() throws NoSuchMethodException {
        final Method updateState = AbstractEntity.class.getDeclaredMethod(
                "updateState", Message.class);
        final int modifiers = updateState.getModifiers();
        assertTrue(Modifier.isFinal(modifiers));
    }

    @Test
    public void prevent_validate_overriding() throws NoSuchMethodException {
        final Method validate = AbstractEntity.class.getDeclaredMethod(
                "validate", Message.class);
        final int modifiers = validate.getModifiers();
        assertTrue(Modifier.isPrivate(modifiers) || Modifier.isFinal(modifiers));
    }

    @Test
    public void throw_InvalidEntityStateException_if_state_is_invalid() {
        final NaturalNumberEntity entity = new NaturalNumberEntity(0L);
        final NaturalNumber invalidNaturalNumber = newNaturalNumber(-1);
        try {
            // This should pass.
            entity.updateState(newNaturalNumber(1));

            // This should fail.
            entity.updateState(invalidNaturalNumber);

            fail("Exception expected.");
        } catch (InvalidEntityStateException e) {
            final Error error = e.getError();

            assertEquals(invalidNaturalNumber, e.getEntityState());
            assertEquals(EntityStateValidationError.getDescriptor()
                                                   .getFullName(), error.getType());
            assertEquals(INVALID_ENTITY_STATE.getNumber(), error.getCode());
            assertEquals("Entity state does match the validation constraints. Violation list:"
                         + lineSeparator() + "Number must be greater than or equal to 0.",
                         error.getMessage());
        }
    }

    @SuppressWarnings("ConstantConditions") // The goal of the test.
    @Test(expected = NullPointerException.class)
    public void not_accept_null_to_checkEntityState() {
        final AnEntity entity = new AnEntity(0L);
        entity.checkEntityState(null);
    }

    @Test
    public void allow_valid_state() {
        final AnEntity entity = new AnEntity(0L);
        assertTrue(entity.checkEntityState(StringValue.getDefaultInstance())
                         .isEmpty());
    }

    private static class AnEntity extends AbstractEntity<Long, StringValue> {
        protected AnEntity(Long id) {
            super(id);
        }
    }

    private static class NaturalNumberEntity extends AbstractEntity<Long, NaturalNumber> {
        private NaturalNumberEntity(Long id) {
            super(id);
        }
    }

    private static NaturalNumber newNaturalNumber(int value) {
        return NaturalNumber.newBuilder()
                            .setValue(value)
                            .build();
    }
}

@@ -1148,38 +1148,8 @@
 st;%0A
-import org.spine3.base.Error;%0A
 impo
@@ -1194,16 +1194,16 @@
 tePart;%0A
+
 import o
@@ -1320,99 +1320,8 @@
 r;%0A%0A
-import static java.lang.System.lineSeparator;%0Aimport static org.junit.Assert.assertEquals;%0A
 impo
@@ -1495,69 +1495,30 @@
 ne3.
-server.entity.EntityStateValidationError.INVALID_ENTITY_STATE
+test.Verify.assertSize
 ;%0A%0A/
@@ -3100,27 +3100,21 @@
     
-final Error error =
+assertSize(1,
  e.g
@@ -3122,18 +3122,16 @@
 tError()
-;%0A
 %0A       
@@ -3139,360 +3139,77 @@
     
-assertEquals(invalidNaturalNumber, e.getEntityState());%0A            assertEquals(EntityStateValidationError.getDescriptor()%0A                                                   .getFullName(), error.getType());%0A            assertEquals(INVALID_ENTITY_STATE.getNumber(), error.getCode());%0A            assertEquals(%22Entity state does match the validation c
+               .getValidationError()%0A                           .getC
 onst
@@ -3217,11 +3217,8 @@
 aint
-s. 
 Viol
@@ -3226,148 +3226,12 @@
 tion
- list:%22%0A                         + lineSeparator() + %22Number must be greater than or equal to 0.%22,%0A                         error.getMessage
+List
 ());

Modify test