/** * Construct {@link ConfigurationProvider}s builder. * <p> * Default setup (override using with*() methods) * <ul> * <li>ConfigurationSource: {@link EmptyConfigurationSource}</li> * <li>ReloadStrategy: {@link ImmediateReloadStrategy}</li> * <li>Environment: {@link DefaultEnvironment}</li> * <li>Metrics: disabled</li> * </ul> */ public ConfigurationProviderBuilder() { configurationSource = new EmptyConfigurationSource(); reloadStrategy = new ImmediateReloadStrategy(); environment = new DefaultEnvironment(); prefix = ""; }
@Test void usesEmptyStringAsEnvName() { assertThat(new DefaultEnvironment().getName()).isEmpty(); } }
@Test void getConfigurationReturnsReloadResult() { Properties properties = new Properties(); when(delegateSource.getConfiguration(any(Environment.class))).thenReturn(properties); cachedConfigurationSource.reload(new DefaultEnvironment()); assertThat(cachedConfigurationSource.getConfiguration(new DefaultEnvironment())).isEqualTo(properties); }
@Test void reloadPropagatesIllegalStateExceptions() { when(delegateSource.getConfiguration(any(Environment.class))).thenThrow(new IllegalStateException("")); assertThatThrownBy(() -> cachedConfigurationSource.reload(new DefaultEnvironment())).isExactlyInstanceOf(IllegalStateException.class); } }
@Test void getConfigurationCallsDelegate() { Properties properties = new Properties(); when(delegate.getConfiguration(any(Environment.class))).thenReturn(properties); assertThat(source.getConfiguration(new DefaultEnvironment())).isEqualTo(properties); }
@Test void getConfigurationPropagatesIllegalStateExceptions() { when(delegate.getConfiguration(any(Environment.class))).thenThrow(new IllegalStateException("")); assertThatThrownBy(() -> source.getConfiguration(new DefaultEnvironment())).isExactlyInstanceOf(IllegalStateException.class); }
@Test void returnsManuallyDefinedKeysAfterReload() { assertThat(source.getConfiguration(new DefaultEnvironment())).doesNotContainKey("reloaded.property"); System.setProperty("reloaded.property", "defined"); assertThat(source.getConfiguration(new DefaultEnvironment())).containsKey("reloaded.property"); }
@Test @Disabled("FIXME: implementation should be caching results") void getConfigurationDoesNotChangeValueBetweenReloads() { Properties properties = new Properties(); properties.put("testConfig", "testValue"); when(delegateSource.getConfiguration(any(Environment.class))).thenReturn(properties); cachedConfigurationSource.reload(new DefaultEnvironment()); properties.put("testConfig", "testValueChanged"); assertThat(cachedConfigurationSource.getConfiguration(new DefaultEnvironment())).contains(entry("testConfig", "testValue")); }
@Test void returnsManuallyDefinedKeys() { System.setProperty("manually.added.property", "defined"); assertThat(source.getConfiguration(new DefaultEnvironment())).containsKey("manually.added.property"); }
@Test void getConfigurationReadsFromHomeForDefaultEnvironment() { System.setProperty("user.home", fileRepo.dirPath.resolve("otherApplicationConfigs").toString()); assertThat(source.getConfiguration(new DefaultEnvironment())).containsOnly(MapEntry.entry("some.setting", "otherAppSetting")); }
@Test void getConfigurationReadsFromGivenFiles() throws Exception { ConfigFilesProvider configFilesProvider = () -> Arrays.asList(Paths.get("application.properties"), Paths.get("otherConfig.properties")); Environment environment = new DefaultEnvironment(); try (GitConfigurationSource gitConfigurationSource = getSourceForRemoteRepoWithFilesProvider(configFilesProvider)) { assertThat(gitConfigurationSource.getConfiguration(environment)).containsKeys("some.setting", "otherConfig.setting"); } }
@Test void getConfigurationReadsFromGivenFiles() { configFilesProvider = () -> Arrays.asList( Paths.get("application.properties"), Paths.get("otherConfig.properties") ); source = new ClasspathConfigurationSource(configFilesProvider); assertThat(source.getConfiguration(new DefaultEnvironment())).containsOnlyKeys("some.setting", "otherConfig.setting"); }
@Test void getConfigurationThrowsOnMissingConfigFile() { configFilesProvider = () -> Collections.singletonList( Paths.get("nonexistent.properties") ); source = new ClasspathConfigurationSource(configFilesProvider); assertThatThrownBy(() -> source.getConfiguration(new DefaultEnvironment())).isExactlyInstanceOf(IllegalStateException.class); }
@Test void reloadPropagatesMissingEnvExceptions() { when(delegateSource.getConfiguration(any(Environment.class))).thenThrow(new MissingEnvironmentException("")); assertThatThrownBy(() -> cachedConfigurationSource.reload(new DefaultEnvironment())).isExactlyInstanceOf(MissingEnvironmentException.class); }
@Test void getConfigurationThrowsOnMalformedConfigFile() { ConfigFilesProvider configFilesProvider = () -> Collections.singletonList(Paths.get("malformed.properties")); assertThatThrownBy(() -> getSourceForRemoteRepoWithFilesProvider(configFilesProvider).getConfiguration(new DefaultEnvironment())) .isExactlyInstanceOf(IllegalStateException.class); }
@Test void getConfigurationThrowsOnMalformedConfigFile() { configFilesProvider = () -> Collections.singletonList( Paths.get("malformed.properties") ); source = new ClasspathConfigurationSource(configFilesProvider); assertThatThrownBy(() -> source.getConfiguration(new DefaultEnvironment())).isExactlyInstanceOf(IllegalStateException.class); }
@Test void initializesStrategyOnBuild() { ReloadStrategy reloadStrategy = mock(ReloadStrategy.class); builder .withReloadStrategy(reloadStrategy) .withEnvironment(new DefaultEnvironment()) .build(); verify(reloadStrategy, times(1)).register(any(Reloadable.class)); } }
@Test void getConfigurationPropagatesMissingEnvironmentExceptions() { when(delegate.getConfiguration(any(Environment.class))).thenThrow(new MissingEnvironmentException("")); assertThatThrownBy(() -> source.getConfiguration(new DefaultEnvironment())).isExactlyInstanceOf(MissingEnvironmentException.class); }
@Test void returnsPath() { assertThat(source.getConfiguration(new DefaultEnvironment())).containsKey("PATH"); }
@Test void returnsEmptyConfiguration() { assertThat(source.getConfiguration(new DefaultEnvironment())).isEmpty(); }