@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 usesEmptyStringAsEnvName() { assertThat(new DefaultEnvironment().getName()).isEmpty(); } }
@Test void getConfigurationThrowsOnMissingConfigFile() throws Exception { remoteRepo.deleteFile(Paths.get("application.properties")); try (GitConfigurationSource gitConfigurationSource = getSourceForRemoteRepoWithDefaults()) { assertThatThrownBy(() -> gitConfigurationSource.getConfiguration(new DefaultEnvironment())) .isExactlyInstanceOf(IllegalStateException.class); } }
private ConfigurationProvider buildProvider() { Environment environment = new DefaultEnvironment(); return new ConfigurationProviderBuilder() .withConfigurationSource(buildSource(environment)) .withEnvironment(environment) .withReloadStrategy(new PeriodicalReloadStrategy(10000)) .build(); }