@Test void returnsConstantEnvName() { String environmentName = "sampleEnvironment"; ImmutableEnvironment strategy = new ImmutableEnvironment(environmentName); assertThat(strategy.getName()).isEqualTo(environmentName); } }
@Test void getConfigurationThrowsWhenOneOfSourcesThrows() { when(underlyingSources[3].getConfiguration(ArgumentMatchers.any())).thenThrow(new IllegalStateException()); assertThatThrownBy(() -> mergeConfigurationSource.getConfiguration(new ImmutableEnvironment("test"))).isExactlyInstanceOf(IllegalStateException.class); }
@Test void getConfigurationReadsFromGivenPath() { Environment environment = new ImmutableEnvironment(fileRepo.dirPath.resolve("otherApplicationConfigs").toString()); assertThat(source.getConfiguration(environment)).containsOnly(MapEntry.entry("some.setting", "otherAppSetting")); }
@Test void getConfigurationThrowsAfterFailedReload() throws Exception { server.shutdown(); try { source.getConfiguration(new ImmutableEnvironment("us-west-2")); } catch (Exception e) { // NOP } assertThatThrownBy(() -> source.getConfiguration(new ImmutableEnvironment(""))) .isExactlyInstanceOf(SourceCommunicationException.class); }
@Test void getConfigurationDisallowsLeadingSlashInClasspathLocation() { Environment environment = new ImmutableEnvironment("/otherApplicationConfigs"); assertThatThrownBy(() -> source.getConfiguration(environment)).isExactlyInstanceOf(MissingEnvironmentException.class); }
@Test void getConfigurationThrowsWhenOneOfSourcesThrowsOnMissingEnvironment() { when(underlyingSources[1].getConfiguration(ArgumentMatchers.any())).thenThrow(new MissingEnvironmentException("")); assertThatThrownBy(() -> mergeConfigurationSource.getConfiguration(new ImmutableEnvironment("test"))).isExactlyInstanceOf(MissingEnvironmentException.class); }
@Test void getConfigurationThrowsOnMissingEnvironment() { assertThatThrownBy(() -> source.getConfiguration(new ImmutableEnvironment("awlerijawoetinawwerlkjn"))).isExactlyInstanceOf(MissingEnvironmentException.class); }
@Test void getConfigurationThrowsOnMissingEnvironment() { assertThatThrownBy(() -> source.getConfiguration(new ImmutableEnvironment("awlerijawoetinawwerlkjn"))).isExactlyInstanceOf(MissingEnvironmentException.class); }
@BeforeEach void setUp() throws Exception { fileRepo = new TempConfigurationFileRepo("org.cfg4j-test-repo"); fileRepo.changeProperty(Paths.get("application.properties"), "some.setting", "masterValue"); fileRepo.changeProperty(Paths.get("otherConfig.properties"), "otherConfig.setting", "masterValue"); fileRepo.changeProperty(Paths.get("malformed.properties"), "otherConfig.setting", "\\uzzzzz"); fileRepo.changeProperty(Paths.get("otherApplicationConfigs/application.properties"), "some.setting", "otherAppSetting"); environment = new ImmutableEnvironment(fileRepo.dirPath.toString()); source = new FilesConfigurationSource(); source.init(); }
@Test void getConfigurationIgnoresLeadingSlashInGivenEnvironment() { Environment environment = new ImmutableEnvironment("/us-west-1"); assertThat(source.getConfiguration(environment)).contains(MapEntry.entry("featureA.toggle", "disabled")); }
@Test void getConfigurationReadsFromGivenPath() { Environment environment = new ImmutableEnvironment("otherApplicationConfigs"); assertThat(source.getConfiguration(environment)).containsOnly(MapEntry.entry("some.setting", "otherAppSetting")); }
@Test void getConfigurationThrowsOnMissingBranch() throws Exception { try (GitConfigurationSource gitConfigurationSource = getSourceForRemoteRepoWithDefaults()) { assertThatThrownBy(() -> gitConfigurationSource.getConfiguration(new ImmutableEnvironment("nonExistentBranch"))) .isExactlyInstanceOf(MissingEnvironmentException.class); } }
@Test void getConfigurationReturnsAllKeysFromGivenEnvironment() { Environment environment = new ImmutableEnvironment("us-west-1"); assertThat(source.getConfiguration(environment)).contains(MapEntry.entry("featureA.toggle", "disabled")); }
@Test void getConfigurationReadsConfigFromGivenBranch() throws Exception { try (GitConfigurationSource gitConfigurationSource = getSourceForRemoteRepoWithDefaults()) { Environment environment = new ImmutableEnvironment(TEST_ENV_BRANCH); assertThat(gitConfigurationSource.getConfiguration(environment)).contains(MapEntry.entry("some.setting", "testValue")); } }
@Test void getConfigurationMergesConfigurationsWithCollidingKeys() { Environment environment = new ImmutableEnvironment("test"); sourcesWithProps(environment, "prop", "value1", "prop", "value2"); assertThat(mergeConfigurationSource.getConfiguration(environment)).containsOnly(MapEntry.entry("prop", "value2")); }
@Test void getConfigurationReadsFromGivenPath() throws Exception { try (GitConfigurationSource gitConfigurationSource = getSourceForRemoteRepoWithDefaults()) { Environment environment = new ImmutableEnvironment("/otherApplicationConfigs/"); assertThat(gitConfigurationSource.getConfiguration(environment)).contains(MapEntry.entry("some.setting", "otherAppSetting")); } }
@Test void getConfigurationThrowsBeforeInitCalled() throws Exception { try (GitConfigurationSource gitConfigurationSource = getSourceBuilderForRemoteRepoWithDefaults().build()) { assertThatThrownBy(() -> gitConfigurationSource.getConfiguration(new ImmutableEnvironment(""))) .isExactlyInstanceOf(IllegalStateException.class); } }
@Test void getConfigurationMergesConfigurations() { Environment environment = new ImmutableEnvironment("test"); sourcesWithProps(environment, "prop1", "value1", "prop2", "value2"); assertThat(mergeConfigurationSource.getConfiguration(environment)).containsOnly(MapEntry.entry("prop1", "value1"), MapEntry.entry("prop2", "value2")); }
@Test void getConfigurationThrowsBeforeInitCalled() { source = new ConsulConfigurationSourceBuilder() .withHost(server.getHostName()) .withPort(server.getPort()) .build(); assertThatThrownBy(() -> source.getConfiguration(new ImmutableEnvironment(""))) .isExactlyInstanceOf(IllegalStateException.class); }
@Test void readsConfigsFromConsulConfigurationSource() { ConfigurationSource source = new ConsulConfigurationSourceBuilder() .withHost(server.getHostName()) .withPort(server.getPort()) .build(); ConfigurationProvider provider = new ConfigurationProviderBuilder() .withConfigurationSource(source) .withEnvironment(new ImmutableEnvironment("us-west-1")) .build(); assertThat(provider.getProperty("featureA.toggle", String.class)).isEqualTo("disabled"); }