/**
 * Copyright © 2012-2025 by Data Virtuality GmbH
 */
package org.teiid.jdbc;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

/**
 *
 * @author Roman Pedchenko
 * @date 2025.08.07
 */
public class TestFipsForDriver {
    @AfterEach
    void tearDown() {
        FipsManager.removeFipsProvider();
    }

    @Test
    public void testDriverWithFipsValidation() throws Exception {
        Class.forName("com.datavirtuality.dv.jdbc.Driver");
        String jdbcUrl = "jdbc:datavirtuality:vdb@127.0.0.2:31999;" +
                         "ssl=true;" +
                         "fips=enabled;" +
                         "fips.provider=BCFIPS";

        Properties connectionProps = new Properties();
        connectionProps.setProperty("user", "testuser");
        connectionProps.setProperty("password", "testpass");

        assertThatThrownBy(() -> {
            Connection conn = DriverManager.getConnection(jdbcUrl, connectionProps);
            conn.close();
        }).isInstanceOf(SQLException.class);

        assertThat(FipsManager.isFipsModeEnabled()).isTrue();
        assertThat(FipsManager.getCurrentFipsProvider()).isEqualTo("BCFIPS");
    }

    @Test
    public void testDriverRejectsNonFipsConnection() throws Exception {
        FipsManager.removeFipsProvider();
        assertThat(FipsManager.isFipsModeEnabled()).isFalse();

        Class.forName("com.datavirtuality.dv.jdbc.Driver");

        String nonFipsUrl = "jdbc:datavirtuality:vdb@127.0.0.2:31999;ssl=true";
        Properties props = new Properties();
        props.setProperty("user", "testuser");
        props.setProperty("password", "testpass");

        assertThatThrownBy(() -> {
            Connection conn = DriverManager.getConnection(nonFipsUrl, props);
            conn.close();
        }).isInstanceOf(SQLException.class);
        assertThat(FipsManager.isFipsModeEnabled()).isFalse();
        assertThat(FipsManager.getCurrentFipsProvider()).isNull();
    }
}
