Search in sources :

Example 1 with DatabaseAdapterConfig

use of org.projectnessie.versioned.persist.adapter.DatabaseAdapterConfig in project nessie by projectnessie.

the class DatabaseAdapters method createDatabaseConnectionProvider.

public static DatabaseConnectionProvider<DatabaseConnectionConfig> createDatabaseConnectionProvider(String databaseAdapterName, Map<String, String> configuration) {
    DatabaseAdapterFactory<DatabaseAdapterConfig, DatabaseAdapterConfig, DatabaseConnectionProvider<?>> factory = DatabaseAdapterFactory.loadFactoryByName(databaseAdapterName);
    String providerSpec = databaseAdapterName.indexOf(':') == -1 ? null : databaseAdapterName.substring(databaseAdapterName.indexOf(':') + 1).toLowerCase(Locale.ROOT);
    DatabaseAdapterFactory.Builder<DatabaseAdapterConfig, DatabaseAdapterConfig, DatabaseConnectionProvider<?>> builder = factory.newBuilder();
    TestConnectionProviderSource<DatabaseConnectionConfig> providerSource = TestConnectionProviderSource.findCompatibleProviderSource(builder.getConfig(), factory, providerSpec);
    providerSource.configureConnectionProviderConfigFromDefaults(config -> SystemPropertiesConfigurer.configureFromPropertiesGeneric(config, DatabaseConnectionConfig.class, prop -> configuration.getOrDefault(prop, System.getProperty(prop))));
    try {
        // createConnectionProvider method is protected, not defined on DatabaseConnectionProvider,
        // but on AbstractTestConnectionProviderSource
        Method createConnectionProviderMethod = providerSource.getClass().getMethod("createConnectionProvider");
        @SuppressWarnings("unchecked") DatabaseConnectionProvider<DatabaseConnectionConfig> connectionProvider = (DatabaseConnectionProvider<DatabaseConnectionConfig>) createConnectionProviderMethod.invoke(providerSource);
        connectionProvider.configure(providerSource.getConnectionProviderConfig());
        connectionProvider.initialize();
        return connectionProvider;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : DatabaseConnectionProvider(org.projectnessie.versioned.persist.adapter.DatabaseConnectionProvider) TestConnectionProviderSource(org.projectnessie.versioned.persist.tests.extension.TestConnectionProviderSource) GenericContentVariantSupplier(org.projectnessie.versioned.persist.store.GenericContentVariantSupplier) InvocationTargetException(java.lang.reflect.InvocationTargetException) Builder(org.projectnessie.versioned.persist.adapter.DatabaseAdapterFactory.Builder) TableCommitMetaStoreWorker(org.projectnessie.server.store.TableCommitMetaStoreWorker) DatabaseConnectionConfig(org.projectnessie.versioned.persist.adapter.DatabaseConnectionConfig) Locale(java.util.Locale) Map(java.util.Map) DatabaseAdapterConfig(org.projectnessie.versioned.persist.adapter.DatabaseAdapterConfig) DatabaseAdapterFactory(org.projectnessie.versioned.persist.adapter.DatabaseAdapterFactory) Method(java.lang.reflect.Method) ContentVariantSupplier(org.projectnessie.versioned.persist.adapter.ContentVariantSupplier) DatabaseAdapter(org.projectnessie.versioned.persist.adapter.DatabaseAdapter) SystemPropertiesConfigurer(org.projectnessie.versioned.persist.tests.SystemPropertiesConfigurer) DatabaseConnectionConfig(org.projectnessie.versioned.persist.adapter.DatabaseConnectionConfig) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) DatabaseAdapterFactory(org.projectnessie.versioned.persist.adapter.DatabaseAdapterFactory) DatabaseConnectionProvider(org.projectnessie.versioned.persist.adapter.DatabaseConnectionProvider) DatabaseAdapterConfig(org.projectnessie.versioned.persist.adapter.DatabaseAdapterConfig)

Example 2 with DatabaseAdapterConfig

use of org.projectnessie.versioned.persist.adapter.DatabaseAdapterConfig in project nessie by projectnessie.

the class TestTryLoopState method mockedConfig.

DatabaseAdapterConfig mockedConfig(int commitRetries, long commitTimeout, long lowerDefault, long upperDefault, long maxSleep) {
    DatabaseAdapterConfig mock = mock(DatabaseAdapterConfig.class);
    when(mock.getCommitRetries()).thenReturn(commitRetries);
    when(mock.getCommitTimeout()).thenReturn(commitTimeout);
    when(mock.getRetryInitialSleepMillisLower()).thenReturn(lowerDefault);
    when(mock.getRetryInitialSleepMillisUpper()).thenReturn(upperDefault);
    when(mock.getRetryMaxSleepMillis()).thenReturn(maxSleep);
    return mock;
}
Also used : DatabaseAdapterConfig(org.projectnessie.versioned.persist.adapter.DatabaseAdapterConfig)

Example 3 with DatabaseAdapterConfig

use of org.projectnessie.versioned.persist.adapter.DatabaseAdapterConfig in project nessie by projectnessie.

the class DatabaseAdapterExtension method extractCustomConfiguration.

private static Function<AdjustableDatabaseAdapterConfig, DatabaseAdapterConfig> extractCustomConfiguration(NessieDbAdapter adapterAnnotation, ExtensionContext context) {
    Function<AdjustableDatabaseAdapterConfig, DatabaseAdapterConfig> applyCustomConfig = c -> c;
    if (!adapterAnnotation.configMethod().isEmpty()) {
        Method configMethod = findMethod(context.getRequiredTestClass(), adapterAnnotation.configMethod(), AdjustableDatabaseAdapterConfig.class).orElseThrow(() -> new IllegalArgumentException(String.format("%s.configMethod='%s' does not exist in %s", NessieDbAdapter.class.getSimpleName(), adapterAnnotation.configMethod(), context.getRequiredTestClass().getName())));
        makeAccessible(configMethod);
        if (!Modifier.isStatic(configMethod.getModifiers()) || Modifier.isPrivate(configMethod.getModifiers()) || !DatabaseAdapterConfig.class.isAssignableFrom(configMethod.getReturnType())) {
            throw new IllegalArgumentException(String.format("%s.configMethod='%s' must have the signature 'static %s %s(%s)' in %s", NessieDbAdapter.class.getSimpleName(), adapterAnnotation.configMethod(), DatabaseAdapterConfig.class.getSimpleName(), adapterAnnotation.configMethod(), AdjustableDatabaseAdapterConfig.class.getSimpleName(), context.getRequiredTestClass().getName()));
        }
        applyCustomConfig = c -> {
            try {
                return (DatabaseAdapterConfig) configMethod.invoke(null, c);
            } catch (InvocationTargetException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        };
    }
    return applyCustomConfig;
}
Also used : ReflectionUtils.makeAccessible(org.junit.platform.commons.util.ReflectionUtils.makeAccessible) TracingDatabaseAdapter(org.projectnessie.versioned.persist.adapter.spi.TracingDatabaseAdapter) CloseableResource(org.junit.jupiter.api.extension.ExtensionContext.Store.CloseableResource) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Function(java.util.function.Function) CONFIG_NAME_PREFIX(org.projectnessie.versioned.persist.tests.SystemPropertiesConfigurer.CONFIG_NAME_PREFIX) GenericContentVariantSupplier(org.projectnessie.versioned.persist.store.GenericContentVariantSupplier) ArrayList(java.util.ArrayList) AfterEachCallback(org.junit.jupiter.api.extension.AfterEachCallback) VersionStore(org.projectnessie.versioned.VersionStore) Parameter(java.lang.reflect.Parameter) DatabaseAdapterFactory(org.projectnessie.versioned.persist.adapter.DatabaseAdapterFactory) AnnotationUtils.findRepeatableAnnotations(org.junit.platform.commons.util.AnnotationUtils.findRepeatableAnnotations) Method(java.lang.reflect.Method) ReflectionUtils.isPrivate(org.junit.platform.commons.util.ReflectionUtils.isPrivate) Namespace(org.junit.jupiter.api.extension.ExtensionContext.Namespace) DatabaseAdapter(org.projectnessie.versioned.persist.adapter.DatabaseAdapter) SystemPropertiesConfigurer(org.projectnessie.versioned.persist.tests.SystemPropertiesConfigurer) ReflectionUtils.findMethod(org.junit.platform.commons.util.ReflectionUtils.findMethod) PersistVersionStore(org.projectnessie.versioned.persist.store.PersistVersionStore) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) TracingVersionStore(org.projectnessie.versioned.TracingVersionStore) DatabaseConnectionProvider(org.projectnessie.versioned.persist.adapter.DatabaseConnectionProvider) AnnotationUtils.findAnnotatedFields(org.junit.platform.commons.util.AnnotationUtils.findAnnotatedFields) Field(java.lang.reflect.Field) ExtensionConfigurationException(org.junit.jupiter.api.extension.ExtensionConfigurationException) ReflectionUtils(org.junit.platform.commons.util.ReflectionUtils) InvocationTargetException(java.lang.reflect.InvocationTargetException) Consumer(java.util.function.Consumer) ParameterContext(org.junit.jupiter.api.extension.ParameterContext) List(java.util.List) StoreWorker(org.projectnessie.versioned.StoreWorker) BeforeAllCallback(org.junit.jupiter.api.extension.BeforeAllCallback) AdjustableDatabaseAdapterConfig(org.projectnessie.versioned.persist.adapter.AdjustableDatabaseAdapterConfig) Modifier(java.lang.reflect.Modifier) ExceptionUtils(org.junit.platform.commons.util.ExceptionUtils) Annotation(java.lang.annotation.Annotation) BeforeEachCallback(org.junit.jupiter.api.extension.BeforeEachCallback) DatabaseAdapterConfig(org.projectnessie.versioned.persist.adapter.DatabaseAdapterConfig) Optional(java.util.Optional) AnnotationUtils(org.junit.platform.commons.util.AnnotationUtils) ParameterResolver(org.junit.jupiter.api.extension.ParameterResolver) AnnotatedElement(java.lang.reflect.AnnotatedElement) AdjustableDatabaseAdapterConfig(org.projectnessie.versioned.persist.adapter.AdjustableDatabaseAdapterConfig) DatabaseAdapterConfig(org.projectnessie.versioned.persist.adapter.DatabaseAdapterConfig) AdjustableDatabaseAdapterConfig(org.projectnessie.versioned.persist.adapter.AdjustableDatabaseAdapterConfig) Method(java.lang.reflect.Method) ReflectionUtils.findMethod(org.junit.platform.commons.util.ReflectionUtils.findMethod) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

DatabaseAdapterConfig (org.projectnessie.versioned.persist.adapter.DatabaseAdapterConfig)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 DatabaseAdapter (org.projectnessie.versioned.persist.adapter.DatabaseAdapter)2 DatabaseAdapterFactory (org.projectnessie.versioned.persist.adapter.DatabaseAdapterFactory)2 DatabaseConnectionProvider (org.projectnessie.versioned.persist.adapter.DatabaseConnectionProvider)2 GenericContentVariantSupplier (org.projectnessie.versioned.persist.store.GenericContentVariantSupplier)2 Annotation (java.lang.annotation.Annotation)1 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 Field (java.lang.reflect.Field)1 Modifier (java.lang.reflect.Modifier)1 Parameter (java.lang.reflect.Parameter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 AfterEachCallback (org.junit.jupiter.api.extension.AfterEachCallback)1