/*
 * Copyright 2019 Steinar Bang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations
 * under the License.
 */
package no.priv.bang.ukelonn.db.liquibase;

import static org.junit.Assert.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

import javax.sql.DataSource;

import org.junit.BeforeClass;
import org.junit.Test;
import org.ops4j.pax.jdbc.derby.impl.DerbyDataSourceFactory;
import org.osgi.service.jdbc.DataSourceFactory;


public class UkelonnLiquibaseTest {

    private static DataSource dataSource;

    @BeforeClass
    static public void beforeAllTests() throws Exception {
        DataSourceFactory derbyDataSourceFactory = new DerbyDataSourceFactory();
        Properties properties = new Properties();
        properties.setProperty(DataSourceFactory.JDBC_URL, "jdbc:derby:memory:ukelonn;create=true");
        dataSource = derbyDataSourceFactory.createDataSource(properties);
    }

    @Test
    public void testCreateSchema() throws Exception {
        try(Connection connection = createConnection()) {
            UkelonnLiquibase handleregLiquibase = new UkelonnLiquibase();
            handleregLiquibase.createInitialSchema(connection);
            handleregLiquibase.updateSchema(connection);
        }

        try(Connection connection = createConnection()) {
            try(PreparedStatement statement = connection.prepareStatement("select * from users")) {
                ResultSet results = statement.executeQuery();
                int count = 0;
                while(results.next()) {
                    ++count;
                }

                assertEquals(0, count);
            }
        }
    }

    @Test
    public void testForceReleaseLocks() throws Exception {
        try(Connection connection = createConnection()) {
            UkelonnLiquibase handleregLiquibase = new UkelonnLiquibase();
            handleregLiquibase.forceReleaseLocks(connection);
            // Nothing to test for but if we get here, no exceptions have been thrown
        }
    }

    static private Connection createConnection() throws Exception {
        return dataSource.getConnection();
    }


}

@@ -2462,85 +2462,498 @@
     
+%7D%0A%0A
     
-// Nothing to test for but if we get here, no exceptions have been thrown
+    try(Connection connection = createConnection()) %7B%0A            try(PreparedStatement statement = connection.prepareStatement(%22select * from databasechangeloglock%22)) %7B%0A                try(ResultSet results = statement.executeQuery()) %7B%0A                    boolean locked = true;%0A                    while(results.next()) %7B%0A                        locked = results.getBoolean(%22locked%22);%0A                    %7D%0A%0A                    assertFalse(locked);%0A                %7D%0A            %7D
 %0A   

Add an assert to the force liquibase lock test.