/*
 * Copyright 2016, 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.storage.jdbc;

import com.google.protobuf.StringValue;
import com.zaxxer.hikari.HikariConfig;
import org.junit.Before;
import org.junit.Test;
import org.spine3.server.Entity;
import org.spine3.server.aggregate.Aggregate;
import org.spine3.server.storage.AggregateStorage;
import org.spine3.server.storage.CommandStorage;
import org.spine3.server.storage.EntityStorage;
import org.spine3.server.storage.EventStorage;
import org.spine3.server.storage.jdbc.util.DataSourceWrapper;
import org.spine3.server.storage.jdbc.util.HikariDataSourceWrapper;

import static org.junit.Assert.assertNotNull;
import static org.spine3.base.Identifiers.newUuid;

/**
 * @author Alexander Litus
 */
@SuppressWarnings({"InstanceMethodNamingConvention", "MagicNumber"})
public class JdbcStorageFactoryShould {

    /**
     * The URL prefix of an in-memory HyperSQL DB.
     */
    private static final String HSQL_IN_MEMORY_DB_URL_PREFIX = "jdbc:hsqldb:mem:";

    private JdbcStorageFactory factory;

    @Before
    public void setUpTest() {
        final String dbUrl = newInMemoryDbUrl("factoryTests");
        final DataSourceConfig config = DataSourceConfig.newBuilder()
                .setJdbcUrl(dbUrl)
                .setUsername("SA")
                .setPassword("pwd")
                .setMaxPoolSize(12)
                .build();
        factory = JdbcStorageFactory.newInstance(config);
    }

    @Test
    public void create_entity_storage() {
        final EntityStorage<String> storage = factory.createEntityStorage(TestEntity.class);
        assertNotNull(storage);
    }

    @Test
    public void create_aggregate_storage() {
        final AggregateStorage<String> storage = factory.createAggregateStorage(TestAggregate.class);
        assertNotNull(storage);
    }

    @Test
    public void create_event_storage() {
        final EventStorage storage = factory.createEventStorage();
        assertNotNull(storage);
    }

    @Test
    public void create_command_storage() {
        final CommandStorage storage = factory.createCommandStorage();
        assertNotNull(storage);
    }

    public static DataSourceWrapper newInMemoryDataSource(String dbName) {
        final HikariConfig config = new HikariConfig();
        final String dbUrl = newInMemoryDbUrl(dbName);
        config.setJdbcUrl(dbUrl);
        // not setting username and password is OK for in-memory database
        final DataSourceWrapper dataSource = HikariDataSourceWrapper.newInstance(config);
        return dataSource;
    }

    private static String newInMemoryDbUrl(String dbNamePrefix) {
        return HSQL_IN_MEMORY_DB_URL_PREFIX + dbNamePrefix + newUuid();
    }

    private static class TestEntity extends Entity<String, StringValue> {

        private TestEntity(String id) {
            super(id);
        }
    }

    private static class TestAggregate extends Aggregate<String, StringValue> {

        private TestAggregate(String id) {
            super(id);
        }
    }
}

@@ -1286,32 +1286,29 @@
 ver.
-storage.AggregateStorage
+projection.Projection
 ;%0Aim
@@ -1338,30 +1338,17 @@
 storage.
-CommandStorage
+*
 ;%0Aimport
@@ -1378,68 +1378,35 @@
 age.
-EntityStorage;%0Aimport org.spine3.server.storage.EventStorage
+jdbc.util.DataSourceWrapper
 ;%0Aim
@@ -1438,32 +1438,38 @@
 orage.jdbc.util.
+Hikari
 DataSourceWrappe
@@ -1493,56 +1493,28 @@
 ne3.
-server.storage.jdbc.util.HikariDataSourceWrapper
+test.project.Project
 ;%0A%0Ai
@@ -3051,24 +3051,224 @@
 ge);%0A    %7D%0A%0A
+    @Test%0A    public void create_projection_storage() %7B%0A        final ProjectionStorage%3CString%3E storage = factory.createProjectionStorage(TestProjection.class);%0A        assertNotNull(storage);%0A    %7D%0A%0A
     public s
@@ -4135,11 +4135,176 @@
  %7D%0A    %7D
+%0A%0A    private static class TestProjection extends Projection%3CString, Project%3E %7B%0A%0A        protected TestProjection(String id) %7B%0A            super(id);%0A        %7D%0A    %7D
 %0A%7D%0A

Add a test.