All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkiverse.embedded.postgresql.EmbeddedPostgreSQLConfigUtils Maven / Gradle / Ivy

package io.quarkiverse.embedded.postgresql;

import static java.lang.String.format;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import io.quarkus.datasource.runtime.DataSourcesBuildTimeConfig;

public class EmbeddedPostgreSQLConfigUtils {

    private static final String QUARKUS_DATASOURCE_REACTIVE_URL = "quarkus.datasource.reactive.url";
    private static final String QUARKUS_DATASOURCE_JDBC_URL = "quarkus.datasource.jdbc.url";
    private static final String QUARKUS_NAMED_DATASOURCE_REACTIVE_URL = "quarkus.datasource.\"%s\".reactive.url";
    private static final String QUARKUS_NAMED_DATASOURCE_JDBC_URL = "quarkus.datasource.\"%s\".jdbc.url";
    private static final String QUARKUS_DATASOURCE_USERNAME = "quarkus.datasource.username";
    private static final String QUARKUS_DATASOURCE_PASSWORD = "quarkus.datasource.password";
    private static final String QUARKUS_NAMED_DATASOURCE_USERNAME = "quarkus.datasource.\"%s\".username";
    private static final String QUARKUS_NAMED_DATASOURCE_PASSWORD = "quarkus.datasource.\"%s\".password";
    public static final String DEFAULT_DATABASE = "postgres";
    public static final String DEFAULT_REACTIVE_URL = "postgresql://localhost:%d/%s?stringtype=unspecified";
    public static final String DEFAULT_JDBC_URL = "jdbc:postgresql://localhost:%d/%s?stringtype=unspecified";
    public static final String DEFAULT_USERNAME = "postgres";
    public static final String DEFAULT_PASSWORD = "postgres";

    private static final int START_PORT = 5432;

    public static Map getConfig(int port, Map dbNames) {
        Map allConfigs = new HashMap<>();

        dbNames.forEach((key, value) -> {
            allConfigs.put(format(QUARKUS_NAMED_DATASOURCE_REACTIVE_URL, key),
                    format(DEFAULT_REACTIVE_URL, port, value));
            allConfigs.put(format(QUARKUS_NAMED_DATASOURCE_JDBC_URL, key),
                    format(DEFAULT_JDBC_URL, port, value));
            allConfigs.put(format(QUARKUS_NAMED_DATASOURCE_USERNAME, key), DEFAULT_USERNAME);
            allConfigs.put(format(QUARKUS_NAMED_DATASOURCE_PASSWORD, key), DEFAULT_PASSWORD);
        });

        allConfigs.put(QUARKUS_DATASOURCE_REACTIVE_URL, format(DEFAULT_REACTIVE_URL, port, DEFAULT_DATABASE));
        allConfigs.put(QUARKUS_DATASOURCE_JDBC_URL, format(DEFAULT_JDBC_URL, port, DEFAULT_DATABASE));
        allConfigs.put(QUARKUS_DATASOURCE_USERNAME, DEFAULT_USERNAME);
        allConfigs.put(QUARKUS_DATASOURCE_PASSWORD, DEFAULT_PASSWORD);

        return allConfigs;
    }

    public static Integer getDefaultPort() {
        return (int) Math.round(Math.random() * 10) + START_PORT;
    }

    public static Map getDBNames(DataSourcesBuildTimeConfig dataSourcesBuildTimeConfig) {
        return dataSourcesBuildTimeConfig.dataSources().entrySet().stream()
                .filter(e -> !e.getKey().equals(""))
                .filter(ds -> ds.getValue().dbKind().filter(kind -> kind.equals("postgresql")).isPresent())
                .map(Entry::getKey)
                .collect(Collectors.toMap(e -> e, PostgreSQLSyntaxUtils::sanitizeDbName));
    }

    private EmbeddedPostgreSQLConfigUtils() {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy