EncryptedConfigMapperUtils { public static <T> T getConfig(File configFile, Class<T> clazz, ObjectMapper mapper) throws JsonParseException, JsonMappingException, IOException { JsonNode configNode = mapper.readValue(configFile, JsonNode.class); JsonNode substitutedNode = JsonNodeVisitors.dispatch(configNode, JSON_NODE_STRING_REPLACER); return mapper.treeToValue(substitutedNode, clazz); } private  EncryptedConfigMapperUtils(); static T getConfig(File configFile, Class<T> clazz, ObjectMapper mapper); static T getConfig(String configFileContent, Class<T> clazz, ObjectMapper mapper);  }
@Test public final void testCanDecryptValueInConfig() throws IOException { TestConfig config = EncryptedConfigMapperUtils.getConfig(CONFIG_FILE, TestConfig.class, MAPPER); assertEquals("value", config.getUnencrypted()); assertEquals("value", config.getEncrypted()); assertEquals("don't use quotes", config.getEncryptedWithSingleQuote()); assertEquals("double quote is \"", config.getEncryptedWithDoubleQuote()); assertEquals("[oh dear", config.getEncryptedMalformedYaml()); assertThat(config.getArrayWithSomeEncryptedValues(), contains("value", "value", "other value", "[oh dear")); assertThat(config.getPojoWithEncryptedValues(), both(hasProperty("username", equalTo("some-user"))) .and(hasProperty("password", equalTo("value")))); }
@Test public final void testCanDecryptValueInConfigFileContent() throws IOException { String configFileContent = new String(Files.readAllBytes(CONFIG_FILE.toPath()), StandardCharsets.UTF_8); TestConfig config = EncryptedConfigMapperUtils.getConfig(configFileContent, TestConfig.class, MAPPER); assertEquals("value", config.getUnencrypted()); assertEquals("value", config.getEncrypted()); assertEquals("don't use quotes", config.getEncryptedWithSingleQuote()); assertEquals("double quote is \"", config.getEncryptedWithDoubleQuote()); assertEquals("[oh dear", config.getEncryptedMalformedYaml()); assertThat(config.getArrayWithSomeEncryptedValues(), contains("value", "value", "other value", "[oh dear")); assertThat(config.getPojoWithEncryptedValues(), both(hasProperty("username", equalTo("some-user"))) .and(hasProperty("password", equalTo("value")))); }