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

import com.google.common.testing.SerializableTester;
import com.google.protobuf.StringValue;
import io.spine.server.BoundedContext;
import org.junit.Before;
import org.junit.Test;

/**
 * @author Alexander Yevsyukov
 */
public class DefaultEntityFactoryShould {

    private EntityFactory<Long, TestEntity> entityFactory;

    @Before
    public void setUp() {
        final BoundedContext bc = BoundedContext.newBuilder()
                                                .build();
        RecordBasedRepository<Long, TestEntity, StringValue> repository = new TestRepository();
        bc.register(repository);

        entityFactory = repository.entityFactory();
    }

    @Test
    public void serialize() {
        SerializableTester.reserializeAndAssert(entityFactory);
    }

    /**
     * A test entity class which is not versionable.
     */
    private static class TestEntity extends AbstractEntity<Long, StringValue> {
        private TestEntity(Long id) {
            super(id);
        }
    }

    /**
     * A test repository.
     */
    private static class TestRepository
            extends DefaultRecordBasedRepository<Long, TestEntity, StringValue> {
    }
}

@@ -1076,34 +1076,121 @@
 testing.
-SerializableTester
+EqualsTester;%0Aimport com.google.common.testing.SerializableTester;%0Aimport com.google.protobuf.DoubleValue
 ;%0Aimport
@@ -1227,47 +1227,8 @@
 ue;%0A
-import io.spine.server.BoundedContext;%0A
 impo
@@ -1396,16 +1396,17 @@
 stEntity
+1
 %3E entity
@@ -1416,165 +1416,197 @@
 tory
-;%0A%0A    @Before%0A    public void setUp() %7B%0A        final BoundedContext bc = BoundedContext.newBuilder()%0A                                                .build
+1;%0A    private EntityFactory%3CLong, TestEntity2%3E entityFactory2;%0A%0A    @Before%0A    public void setUp() %7B%0A        RecordBasedRepository%3CLong, TestEntity1, StringValue%3E r1 = new TestRepository1
 ();%0A
@@ -1651,32 +1651,33 @@
 stEntity
+2
 , 
-String
+Double
 Value%3E r
 epositor
@@ -1668,25 +1668,17 @@
 Value%3E r
-epository
+2
  = new T
@@ -1690,28 +1690,30 @@
 pository
+2
 ();%0A
+%0A
         
 bc.regis
@@ -1708,34 +1708,45 @@
     
-bc.register(reposi
+entityFactory1 = r1.entityFac
 tory
+(
 );%0A
-%0A
     
@@ -1766,21 +1766,14 @@
 tory
+2
  = r
-epository
+2
 .ent
@@ -1894,16 +1894,17 @@
 yFactory
+1
 );%0A    %7D
@@ -1905,33 +1905,240 @@
 %0A    %7D%0A%0A    
-/**%0A     
+@Test%0A    public void have_custom_equals() %7B%0A        new EqualsTester()%0A                .addEqualityGroup(entityFactory1)%0A                .addEqualityGroup(entityFactory2)%0A                .testEquals();%0A    %7D%0A%0A    /*
 * A test ent
@@ -2172,21 +2172,16 @@
 ionable.
-%0A    
  */%0A    
@@ -2211,16 +2211,17 @@
 stEntity
+1
  extends
@@ -2283,16 +2283,17 @@
 stEntity
+1
 (Long id
@@ -2347,87 +2347,477 @@
  /**
-%0A     * A test repository.%0A     */%0A    private static class TestRepository%0A    
+ A test repository */%0A    private static class TestRepository1%0A            extends DefaultRecordBasedRepository%3CLong, TestEntity1, StringValue%3E %7B%0A    %7D%0A%0A    /** Another entity with the same ID and different state. */%0A    private static class TestEntity2 extends AbstractEntity%3CLong, DoubleValue%3E %7B%0A        protected TestEntity2(Long id) %7B%0A            super(id);%0A        %7D%0A    %7D%0A%0A    /** A repository for %7B@link TestEntity2%7D. */%0A    private static class TestRepository2%0A
     
@@ -2873,24 +2873,25 @@
 stEntity
+2
 , 
-String
+Double
 Value%3E %7B

Test `DefaultEntityFactory.equals()`