throw new MissingEnvironmentException("Directory doesn't exist: " + environment.getName());
/** * 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()); } }
throw new MissingEnvironmentException("Directory doesn't exist: " + rootPath);
/** * 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(); }
@Override public Properties getConfiguration(Environment environment) { if (!initialized) { throw new IllegalStateException("Configuration source has to be successfully initialized before you request configuration."); } reload(); try { checkoutToBranch(branchResolver.getBranchNameFor(environment)); } catch (GitAPIException e) { throw new MissingEnvironmentException(environment.getName(), e); } Properties properties = new Properties(); List<Path> paths = new ArrayList<>(); for (Path path : configFilesProvider.getConfigFiles()) { paths.add(clonedRepoPath.resolve(pathResolver.getPathFor(environment)).resolve(path)); } for (Path path : paths) { try (InputStream input = new FileInputStream(path.toFile())) { PropertiesProvider provider = propertiesProviderSelector.getProvider(path.getFileName().toString()); properties.putAll(provider.getProperties(input)); } catch (IOException e) { throw new IllegalStateException("Unable to load configuration from " + path.toString() + " file", e); } } return properties; }
@Test void initPropagatesMissingEnvExceptions() { doThrow(new MissingEnvironmentException("")).when(delegateSource).init(); assertThatThrownBy(() -> cachedConfigurationSource.init()).isExactlyInstanceOf(MissingEnvironmentException.class); }
@Test void reloadPropagatesMissingEnvExceptions() { when(delegateSource.getConfiguration(any(Environment.class))).thenThrow(new MissingEnvironmentException("")); assertThatThrownBy(() -> cachedConfigurationSource.reload(new DefaultEnvironment())).isExactlyInstanceOf(MissingEnvironmentException.class); }
@Test void getConfigurationPropagatesMissingEnvironmentExceptions() { when(delegate.getConfiguration(any(Environment.class))).thenThrow(new MissingEnvironmentException("")); assertThatThrownBy(() -> source.getConfiguration(new DefaultEnvironment())).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 getConfigurationThrowsWhenAllSourcesThrowOnMissingEnvironment() { makeAllSourcesThrow(new MissingEnvironmentException("")); assertThatThrownBy(() -> fallbackConfigurationSource.getConfiguration(mock(Environment.class))).isExactlyInstanceOf(MissingEnvironmentException.class); }
@Test void allConfigurationAsPropertiesThrowsWhenMissingEnvironment() { when(configurationSource.getConfiguration(anyEnvironment())).thenThrow(new MissingEnvironmentException("")); assertThatThrownBy(() -> simpleConfigurationProvider.allConfigurationAsProperties()).isExactlyInstanceOf(IllegalStateException.class); }