package com.rgi.geopackage.extensions.network;

import com.rgi.geopackage.GeoPackage;
import com.rgi.geopackage.extensions.implementation.BadImplementationException;
import com.rgi.geopackage.verification.ConformanceException;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Node;

import java.io.File;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

import static org.junit.Assert.*;

/*
 * Created by steven.lander on 11/2/15.
 */
public class NodeExitGetterTest
{
    @Before
    public void setUp() throws IOException
    {
        try
        {
            Class.forName("org.sqlite.JDBC");
        }
        catch(final ClassNotFoundException exception)
        {
            // Could not register driver
            throw new IOException(exception);
        }
    }

    public static GeoPackage getGeoPackage(final File gpkgFile) throws IOException
    {
        try
        {
            return new GeoPackage(gpkgFile, GeoPackage.OpenMode.Open);
        }
        catch(final ClassNotFoundException | ConformanceException | SQLException exception)
        {
            System.out.println("Could not open test GeoPackage.");
            throw new IOException(exception);
        }
    }

    public static Connection getConnection(final File gpkgFile) throws IOException
    {
        try
        {
            return DriverManager.getConnection("jdbc:sqlite:" + gpkgFile.getAbsolutePath());
        }
        catch(final SQLException exception)
        {
            System.out.println("Could not get DB instance.");
            throw new IOException(exception);
        }
    }

    public static GeoPackageNetworkExtension getNetworkExtension(final GeoPackage gpkg) throws IOException
    {
        try
        {
            return gpkg.extensions().getExtensionImplementation(GeoPackageNetworkExtension.class);
        }
        catch(final BadImplementationException exception)
        {
            System.out.println("Could not get network extension.");
            throw new IOException(exception);
        }
    }

    public static Network getNetwork(final GeoPackageNetworkExtension networkExtension) throws IOException
    {
        try
        {
            return networkExtension.getNetwork("alaska2");
        }
        catch(final SQLException exception)
        {
            System.out.println("Could not get network.");
            throw new IOException(exception);
        }
    }

    @Test
    public void testOpen()
    {
        try(final GeoPackage gpkg = new GeoPackage(this.gpkgFile, GeoPackage.OpenMode.Open);
            final Connection connection = NodeExitGetterTest.getConnection(this.gpkgFile))
        {
            final GeoPackageNetworkExtension networkExtension = NodeExitGetterTest.getNetworkExtension(gpkg);
            final Network network = NodeExitGetterTest.getNetwork(networkExtension);

            final NodeExitGetter nge = new NodeExitGetter(connection,
                                                          network,
                                                          networkExtension.getAttributeDescriptions(network, AttributedType.Node),
                                                          null);
        }
        catch(final IOException | ClassNotFoundException | ConformanceException | SQLException ignored)
        {
            fail("Failed to open the node exit getter.");
        }
    }

    @Test
    public void testOpenGetExitsOne()
    {
        try(final GeoPackage gpkg = new GeoPackage(this.gpkgFile, GeoPackage.OpenMode.Open);
            final Connection connection = NodeExitGetterTest.getConnection(this.gpkgFile))
        {
            final Network network = NodeExitGetterTest.getNetwork(NodeExitGetterTest.getNetworkExtension(gpkg));
            final NodeExitGetter nge = new NodeExitGetter(connection, network, null, null);

            // There should be 3 exits from node 1 in the network
            assertEquals("Exits from node 1 should be 3.", 3, nge.getExits(1).size());
        }
        catch(final IOException | ClassNotFoundException | ConformanceException | SQLException ignored)
        {
            fail("Get exits returned an invalid number of exits.");
        }
    }

    @Test
    public void testOpenGetExitsTwo()
    {
        try(final GeoPackage gpkg = new GeoPackage(this.gpkgFile, GeoPackage.OpenMode.Open);
            final Connection connection = NodeExitGetterTest.getConnection(this.gpkgFile))
        {
            final Network network = NodeExitGetterTest.getNetwork(NodeExitGetterTest.getNetworkExtension(gpkg));
            final NodeExitGetter nge = new NodeExitGetter(connection, network, null, null);

            // -1 is an invalid node identifier
            final List<AttributedEdge> results = nge.getExits(-1);
            assertNull("Exits from node 1 should be 3.", results);
        }
        catch(final IOException | ClassNotFoundException | ConformanceException | SQLException ignored)
        {
            fail("An invalid exit identifier produced non-null results.");
        }
    }

    /*@Test
    public void testProcessResult()
    {
        final Supplier<String> sqlSupplier = () ->
                "SELECT id, to_node from alaska2 where id = 8 or id = 40;";

        try(final GeoPackage gpkg = new GeoPackage(this.gpkgFile, GeoPackage.OpenMode.Open);
            final Connection connection = NodeExitGetterTest.getConnection(this.gpkgFile);
            final PreparedStatement preparedStatement = connection.prepareStatement(sqlSupplier.get());
            final ResultSet resultSet = preparedStatement.executeQuery())
        {
            final Network network = NodeExitGetterTest.getNetwork(NodeExitGetterTest.getNetworkExtension(gpkg));
            final GeoPackageNetworkExtension networkExtension = NodeExitGetterTest.getNetworkExtension(gpkg);
            final NodeExitGetter nge = new NodeExitGetter(connection, network, networkExtension.getAttributeDescriptions(network, AttributedType.Node), null);

            final List<AttributedEdge> edges = nge.execute();
        }
        catch(final IOException | ClassNotFoundException | ConformanceException | SQLException ignored)
        {
            fail("An invalid exit identifier produced non-null results.");
        }
    }*/

    private final File gpkgFile = new File(ClassLoader.getSystemResource("testNetwork.gpkg").getFile());
    private GeoPackageNetworkExtension networkExtension;
    private Connection connection;
    private Network network;
}

@@ -271,33 +271,8 @@
 est;
-%0Aimport org.w3c.dom.Node;
 %0A%0Aim
@@ -334,17 +334,26 @@
 ava.sql.
-*
+Connection
 ;%0Aimport
@@ -362,51 +362,87 @@
 ava.
-util.ArrayList;%0Aimport java.util.Collection
+sql.DriverManager;%0Aimport java.sql.PreparedStatement;%0Aimport java.sql.ResultSet
 ;%0Aim
@@ -455,24 +455,24 @@
 ava.
-util.Collec
+sql.SQLExcep
 tion
-s
 ;%0Aim
@@ -5228,1225 +5228,8 @@
  %7D%0A%0A
-    /*@Test%0A    public void testProcessResult()%0A    %7B%0A        final Supplier%3CString%3E sqlSupplier = () -%3E%0A                %22SELECT id, to_node from alaska2 where id = 8 or id = 40;%22;%0A%0A        try(final GeoPackage gpkg = new GeoPackage(this.gpkgFile, GeoPackage.OpenMode.Open);%0A            final Connection connection = NodeExitGetterTest.getConnection(this.gpkgFile);%0A            final PreparedStatement preparedStatement = connection.prepareStatement(sqlSupplier.get());%0A            final ResultSet resultSet = preparedStatement.executeQuery())%0A        %7B%0A            final Network network = NodeExitGetterTest.getNetwork(NodeExitGetterTest.getNetworkExtension(gpkg));%0A            final GeoPackageNetworkExtension networkExtension = NodeExitGetterTest.getNetworkExtension(gpkg);%0A            final NodeExitGetter nge = new NodeExitGetter(connection, network, networkExtension.getAttributeDescriptions(network, AttributedType.Node), null);%0A%0A            final List%3CAttributedEdge%3E edges = nge.execute();%0A        %7D%0A        catch(final IOException %7C ClassNotFoundException %7C ConformanceException %7C SQLException ignored)%0A        %7B%0A            fail(%22An invalid exit identifier produced non-null results.%22);%0A        %7D%0A    %7D*/%0A%0A
     

remove unnecessary tests