/** * Get configuration set for a given {@code environment} from the cache. For cache to be seeded * you have to call the {@link #reload(Environment)} method before calling this method. Otherwise * the method will throw {@link MissingEnvironmentException}. * * @param environment environment to use * @return configuration set for {@code environment} * @throws MissingEnvironmentException when there's no config for the given environment in the cache */ @Override public Properties getConfiguration(Environment environment) { if (cachedConfigurationPerEnvironment.containsKey(environment.getName())) { return cachedConfigurationPerEnvironment.get(environment.getName()); } else { throw new MissingEnvironmentException(environment.getName()); } }
@Override public String getBranchNameFor(Environment environment) { String[] tokens = environment.getName().split("/"); String branchName = tokens[0].trim(); if (branchName.isEmpty()) { branchName = "master"; } return branchName; } }
/** * 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 getConfigurationThrowsWhenOneOfSourcesThrowsOnMissingEnvironment() { when(underlyingSources[1].getConfiguration(ArgumentMatchers.any())).thenThrow(new MissingEnvironmentException("")); assertThatThrownBy(() -> mergeConfigurationSource.getConfiguration(new ImmutableEnvironment("test"))).isExactlyInstanceOf(MissingEnvironmentException.class); }
@Test void getConfigurationDisallowsLeadingSlashInClasspathLocation() { Environment environment = new ImmutableEnvironment("/otherApplicationConfigs"); assertThatThrownBy(() -> source.getConfiguration(environment)).isExactlyInstanceOf(MissingEnvironmentException.class); }
@Test void returnsConstantEnvName() { String environmentName = "sampleEnvironment"; ImmutableEnvironment strategy = new ImmutableEnvironment(environmentName); assertThat(strategy.getName()).isEqualTo(environmentName); } }
@Test void reloadPropagatesMissingEnvExceptions() { when(delegateSource.getConfiguration(any(Environment.class))).thenThrow(new MissingEnvironmentException("")); assertThatThrownBy(() -> cachedConfigurationSource.reload(new DefaultEnvironment())).isExactlyInstanceOf(MissingEnvironmentException.class); }
@Test void usesEmptyStringAsEnvName() { assertThat(new DefaultEnvironment().getName()).isEmpty(); } }
@Test void getConfigurationThrowsOnMissingEnvironment() { assertThatThrownBy(() -> source.getConfiguration(new ImmutableEnvironment("awlerijawoetinawwerlkjn"))).isExactlyInstanceOf(MissingEnvironmentException.class); }
/** * Get configuration set for a given {@code environment} from this source in a form of {@link Properties}. * The configuration set is a result of the first {@link ConfigurationSource#getConfiguration(Environment)} * call to underlying sources that succeeds. Sources are called in a provided order. * * @param environment environment to use * @return configuration set for {@code environment} from the first source that works * @throws MissingEnvironmentException when requested environment couldn't be found in any of the underlying source * @throws IllegalStateException when unable to fetch configuration from any of the underlying sources */ @Override public Properties getConfiguration(Environment environment) { boolean allMissEnvironment = true; for (ConfigurationSource source : sources) { try { return source.getConfiguration(environment); } catch (MissingEnvironmentException e) { // NOP } catch (IllegalStateException e) { allMissEnvironment = false; } } if (allMissEnvironment) { throw new MissingEnvironmentException(environment.getName()); } throw new IllegalStateException(); }
@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"); }
/** * Format the provided {@link Environment} to ensure it ends with an underscore * * @param environment The provided {@link Environment} context * @return The formatted {@link String} of the {@link Environment} context */ private static String formatEnvironmentContext(Environment environment) { String environmentName = environment.getName(); if (environmentName == null || environmentName.isEmpty()) { return ""; } else { return environmentName.endsWith("_") ? environmentName : environmentName + "_"; } } }
@Test void getConfigurationThrowsOnMissingEnvironment() { assertThatThrownBy(() -> source.getConfiguration(new ImmutableEnvironment("awlerijawoetinawwerlkjn"))).isExactlyInstanceOf(MissingEnvironmentException.class); }
@Test void returnsEmptyConfiguration() { assertThat(source.getConfiguration(new DefaultEnvironment())).isEmpty(); }
/** * Reload configuration set for a given {@code environment} from this source in a form of {@link Properties}. * After reload completes the configuration can be accesses via {@link #getConfiguration(Environment)} method. * * @param environment environment to reload * @throws MissingEnvironmentException when requested environment couldn't be found * @throws IllegalStateException when unable to fetch configuration */ public void reload(Environment environment) { Properties configuration = underlyingSource.getConfiguration(environment); cachedConfigurationPerEnvironment.put(environment.getName(), configuration); } }
@Test void returnsOSName() { assertThat(source.getConfiguration(new DefaultEnvironment())).containsKey("os.name"); }
@Override public Path getPathFor(Environment environment) { String[] tokens = environment.getName().split("/"); return FileSystems.getDefault().getPath("", Arrays.copyOfRange(tokens, 1, tokens.length)); } }
@Test void doesNotReturnUndefinedKeys() { assertThat(source.getConfiguration(new DefaultEnvironment())).doesNotContainKey("missing.property"); }
private ConfigurationProvider buildProvider() { Environment environment = new DefaultEnvironment(); return new ConfigurationProviderBuilder() .withConfigurationSource(buildSource(environment)) .withEnvironment(environment) .withReloadStrategy(new PeriodicalReloadStrategy(10000)) .build(); }