SubstitutingConfigurationFactory extends YamlConfigurationFactory<T> { @Override protected T build(JsonNode node, String path) throws IOException, ConfigurationException { try { JsonNode substitutedNode = JsonNodeVisitors.dispatch(node, substitutor); return super.build(substitutedNode, path); } catch (StringSubstitutionException e) { String error = String.format( "The value '%s' for field '%s' could not be replaced", e.getValue(), e.getField()); throw new ConfigurationDecryptionException(path, ImmutableList.of(error), e); } } SubstitutingConfigurationFactory(Class<T> klass,
            Validator validator,
            ObjectMapper objectMapper,
            String propertyPrefix,
            JsonNodeVisitor<JsonNode> substitutor);   }
@Test public final void decryptionSucceeds() throws IOException, ConfigurationException { TestConfig config = factory.build(new File("src/test/resources/testConfig.yml")); assertThat(config.getEncrypted()).isEqualTo("value"); assertThat(config.getUnencrypted()).isEqualTo("value"); assertThat(config.getEncryptedWithSingleQuote()).isEqualTo("don't use quotes"); assertThat(config.getEncryptedWithDoubleQuote()).isEqualTo("double quote is \""); assertThat(config.getEncryptedMalformedYaml()).isEqualTo("[oh dear"); assertThat(config.getArrayWithSomeEncryptedValues()) .containsExactly("value", "value", "other value", "[oh dear"); assertThat(config.getPojoWithEncryptedValues()).isEqualTo(Person.of("some-user", "value")); }
@Test public final void decryptionFailsWithNiceMessage() throws IOException, ConfigurationException { try { factory.build(new File("src/test/resources/testConfigWithError.yml")); failBecauseExceptionWasNotThrown(ConfigurationDecryptionException.class); } catch (ConfigurationDecryptionException e) { assertThat(e.getMessage()).contains("src/test/resources/testConfigWithError.yml has an error"); assertThat(e.getMessage()).contains( "The value 'enc:ERROR' for field 'arrayWithSomeEncryptedValues[3]' could not be replaced"); } }