PropertiesLoader { public Map<String, String> loadFromList(List<String> lines) { Map<String, String> props = new HashMap<String, String>(); for (String line : lines) { String[] keyval = parseLine(line); if (keyval != null) { props.put(keyval[0], keyval[1]); } } return props; } PropertiesLoader(CryptoService cryptoService);  PropertiesLoader(); Map<String, String> loadFromList(List<String> lines); Map<String, String> loadFromFile(String path); static String unwrapEncryptedValue(String encValue); String parseValue(String value); static String[] splitKeyVal(String line);  }
@Test public void shouldCallDecryptWhenLoadingEncryptedValue() { PropertiesLoader loader = new PropertiesLoader(mockedCryptoService); loader.loadFromList(prepareEncryptedSettings()); verify(mockedCryptoService, times(1)).decrypt(ENCRYPTED_PASSWORD); }
@Test public void testNonEncryptedPropertyLoad() throws Exception { Map<String, String> props = fakeLoader.loadFromList(prepareEncryptedSettings()); assertEquals("mydemodb", props.get("database")); }
@Test public void testEncryptedPropertyLoad() throws Exception { Map<String, String> props = fakeLoader.loadFromList(prepareEncryptedSettings()); assertEquals(DB_PASSWORD, props.get("password")); }
@Test public void shouldSkipCommentedLines() throws Exception { List<String> lines = new java.util.ArrayList<String>(); lines.add("#=A comment here"); lines.add("username=myname"); Map<String, String> props = fakeLoader.loadFromList(lines); assertThat(props, not(hasKey(startsWith("#")))); }
@Test public void testEncryptedPropertyLoadRealCryptoSvc() throws Exception { java.io.File ksRoot = tempKeyStoreFolder.getRoot(); CryptoTestsConfig.createTestKeyStore(ksRoot); CryptoService crypto = CryptoTestsConfig.getCryptoService(ksRoot); PropertiesLoader loader = new PropertiesLoader(crypto); Map<String, String> props = loader.loadFromList( PropertiesTestsSetUp.prepareEncryptedSettings(crypto.encrypt(DB_PASSWORD))); assertEquals(DB_PASSWORD, props.get("password")); }