package com.atlassian.stash.internal.db;

import org.junit.Test;

import static com.atlassian.stash.internal.db.DbType.*;
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.junit.Assert.assertEquals;

/**
 * Tests the operation of the {@link DbType} enumeration.
 */
public class DbTypeTest {
    //==================================================================================================================
    // Tests of the forKey method
    //==================================================================================================================

    /**
     * Tests the positive path (key found) through the {@code forKey} method.
     */
    @Test
    public void forOracleKey() {
        assertEquals(of(ORACLE), forKey("oracle"));
    }

    /**
     * Tests the negative path (key not found) through the {@code forKey} method.
     */
    @Test
    public void forBollocksKey() {
        assertEquals(empty(), forKey("bollocks"));
    }

    //==================================================================================================================
    // Tests of the forDriver method
    //==================================================================================================================

    @Test
    public void forOracleDriver() {
        assertEquals(of(ORACLE), forDriver("oracle.jdbc.driver.OracleDriver"));
    }

    //jTDS was removed in 2.1, and should no longer return a valid DbType
    @Test
    public void forMsSqlJtdsDriver() {
        assertEquals(empty(), forDriver("net.sourceforge.jtds.jdbc.Driver"));
    }

    @Test
    public void testMsSqlMicrosoftDriver() {
        assertEquals(of(MSSQL), forDriver("com.microsoft.sqlserver.jdbc.SQLServerDriver"));
    }

    /**
     * Negative path (DB type not found) through the forDriver method
     */
    @Test
    public void forBollocksDriver() {
        assertEquals(empty(), forDriver("com.example.jdbc.NoSuchDriver"));
    }

    //==================================================================================================================
    // Tests concerning JDBC URL generation
    //==================================================================================================================

    @Test
    public void msSqlMicrosoftUrl() {
        assertEquals("jdbc:sqlserver://example.com:6098;databaseName=stash;",
                     MSSQL.generateUrl("example.com", "stash", 6098));
    }

    @Test
    public void mySqlUrl() {
        assertEquals("jdbc:mysql://example.com:6098/stash?characterEncoding=utf8&useUnicode=true&sessionVariables=storage_engine%3DInnoDB",
                     MYSQL.generateUrl("example.com", "stash", 6098));
    }

    @Test
    public void generateOracleUrl() {
        assertEquals("jdbc:oracle:thin:@//example.com:6098/stash",
                     ORACLE.generateUrl("example.com", "stash", 6098));
    }

    @Test
    public void generatePostgresUrl() {
        assertEquals("jdbc:postgresql://example.com:6098/stash",
                     POSTGRES.generateUrl("example.com", "stash", 6098));
    }
}
