/*
 * 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.storage.jdbc.query;

import io.spine.server.storage.jdbc.DataSourceWrapper;
import io.spine.server.storage.jdbc.DatabaseException;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static io.spine.server.storage.jdbc.GivenDataSource.whichIsStoredInMemory;

/**
 * @author Dmytro Dashenkov
 */
public class QueryExecutorShould {

    @Test(expected = DatabaseException.class)
    public void handle_sql_exception() {
        final DataSourceWrapper dataSourceWrapper = whichIsStoredInMemory("foo");
        final QueryExecutor query = new QueryExecutor(dataSourceWrapper, log());
        query.execute("invalid query");
    }

    private static Logger log() {
        return LogSingleton.INSTANCE.value;
    }

    private enum LogSingleton {
        INSTANCE;
        @SuppressWarnings("NonSerializableFieldInSerializableClass")
        private final Logger value = LoggerFactory.getLogger(QueryExecutorShould.class);
    }
}

@@ -1051,24 +1051,79 @@
 dbc.query;%0A%0A
+import io.spine.server.storage.jdbc.ConnectionWrapper;%0A
 import io.sp
@@ -1312,80 +1312,382 @@
 ort 
-static io.spine.server.storage.jdbc.GivenDataSource.whichIsStoredInMemor
+java.sql.PreparedStatement;%0Aimport java.sql.SQLException;%0A%0Aimport static io.spine.Identifier.newUuid;%0Aimport static org.mockito.ArgumentMatchers.anyBoolean;%0Aimport static org.mockito.ArgumentMatchers.anyString;%0Aimport static org.mockito.Mockito.doReturn;%0Aimport static org.mockito.Mockito.doThrow;%0Aimport static org.mockito.Mockito.mock;%0Aimport static org.mockito.Mockito.sp
 y;%0A%0A
@@ -1840,18 +1840,57 @@
 xception
-()
+_on_query_execution() throws SQLException
  %7B%0A     
@@ -1930,45 +1930,498 @@
 urce
-Wrapper = whichIsStoredInMemory(%22foo%22
+ = mock(DataSourceWrapper.class);%0A        final ConnectionWrapper connection = mock(ConnectionWrapper.class);%0A        final PreparedStatement statement = mock(PreparedStatement.class);%0A%0A        doReturn(connection).when(dataSource)%0A                            .getConnection(anyBoolean());%0A        doReturn(statement).when(connection)%0A                           .prepareStatement(anyString());%0A        doThrow(SQLException.class).when(statement)%0A                                   .execute(
 );%0A 
@@ -2455,16 +2455,20 @@
 query = 
+spy(
 new Quer
@@ -2491,23 +2491,16 @@
 urce
-Wrapper
 , log())
 ;%0A  
@@ -2491,24 +2491,25 @@
 urce, log())
+)
 ;%0A        qu
@@ -2524,23 +2524,17 @@
 ute(
-%22invalid query%22
+newUuid()
 );%0A 

Fix the test for `QueryExecutor`