<commit_before>/*
 * Copyright 2017 BlackSource, LLC.
 *
 * 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 tech.blacksource.blacknectar.service;

import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import java.sql.Connection;
import java.sql.SQLException;
import javax.inject.Singleton;
import javax.sql.DataSource;
import org.postgresql.ds.PGSimpleDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.aroma.client.Aroma;
import tech.aroma.client.Urgency;
import tech.sirwellington.alchemy.arguments.AlchemyAssertion;
import tech.sirwellington.alchemy.arguments.FailedAssertionException;

import static tech.sirwellington.alchemy.arguments.Arguments.checkThat;

/**
 *
 * @author SirWellington
 */
public final class ModuleTestingDatabase extends AbstractModule
{

    private final static Logger LOG = LoggerFactory.getLogger(ModuleTestingDatabase.class);

    @Override
    protected void configure()
    {
    }

    @Provides
    @Singleton
    DataSource provideSQLConnection(Aroma aroma) throws SQLException
    {
        int port = 5432;
        String host = "database.blacksource.tech";
        String user = Files.readFile("./secrets/postgres-user.txt").trim();
        String password = Files.readFile("./secrets/postgres-password.txt").trim();
        //Explicitly setting the schema seems to conflict with Postgis functions, so 
        //ignoring for now.
        String schema = "test";
        String applicationName = "BlackNectar";

        String url = String.format("jdbc:postgresql://%s:%d/postgres?user=%s&password=%s&ApplicationName=%s&currentSchema=%s",
                                   host,
                                   port,
                                   user,
                                   password,
                                   applicationName,
                                   schema);


        PGSimpleDataSource dataSource = new PGSimpleDataSource();
        dataSource.setUrl(url);
        
        try(Connection connection = dataSource.getConnection();)
        {
            checkThat(connection)
                .throwing(SQLException.class)
                .is(connected());
        }
        catch (SQLException ex)
        {
            String message = "Failed to create connection to PostgreSQL. Defaulting to SQLite.";
            LOG.error(message, ex);
            aroma.begin().titled("SQL Connection Failed")
                .text(message, ex)
                .withUrgency(Urgency.HIGH)
                .send();

            throw ex;
        }

        return dataSource;
    }

    private AlchemyAssertion<Connection> connected()
    {
        return connection -><commit_msg>#48 - Updates Testing Database<commit_after>/*
 * Copyright 2017 BlackSource, LLC.
 *
 * 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 tech.blacksource.blacknectar.service;

import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import java.sql.Connection;
import java.sql.SQLException;
import javax.inject.Singleton;
import javax.sql.DataSource;
import org.postgresql.ds.PGSimpleDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.aroma.client.Aroma;
import tech.aroma.client.Urgency;
import tech.sirwellington.alchemy.arguments.AlchemyAssertion;
import tech.sirwellington.alchemy.arguments.FailedAssertionException;

import static tech.sirwellington.alchemy.arguments.Arguments.checkThat;

/**
 *
 * @author SirWellington
 */
public final class ModuleTestingDatabase extends AbstractModule
{

    private final static Logger LOG = LoggerFactory.getLogger(ModuleTestingDatabase.class);

    @Override
    protected void configure()
    {
    }

    @Provides
    @Singleton
    DataSource provideSQLConnection(Aroma aroma) throws SQLException
    {
        int port = 5432;
        String database = "testing";
        String host = "database.blacksource.tech";
        String user = Files.readFile("./secrets/postgres-user.txt").trim();
        String password = Files.readFile("./secrets/postgres-password.txt").trim();
        
        String applicationName = "BlackNectar";

        String url = String.format("jdbc:postgresql://%s:%d/%s?user=%s&password=%s&ApplicationName=%s",
                                   host,
                                   port,
                                   database,
                                   user,
                                   password,
                                   applicationName);


        PGSimpleDataSource dataSource = new PGSimpleDataSource();
        dataSource.setUrl(url);
        
        try(Connection connection = dataSource.getConnection();)
        {
            checkThat(connection)
                .throwing(SQLException.class)
                .is(connected());
        }
        catch (SQLException ex)
        {
            String message = "Failed to create connection to PostgreSQL. Defaulting to SQLite.";
            LOG.error(message, ex);
            aroma.begin().titled("SQL Connection Failed")
                .text(message, ex)
                .withUrgency(Urgency.HIGH)
                .send();

            throw ex;
        }

        return dataSource;
    }

    private AlchemyAssertion<Connection> connected()
    {
        return connection ->
{'fraction_non_alphanumeric': 0.08984867591424968, 'fraction_numerical': 0.004728877679697352, 'mean_word_length': 2.3447548761201897, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6892095', 'n_tokens_mistral': 1689, 'n_tokens_neox': 1597, 'n_words': 435}