JsonFileKeyStorage implements KeyStorage { @Override public KeyEntry load(String keyName) { if (!exists(keyName)) { throw new KeyEntryNotFoundException(); } File file = new File(keysPath, keyName.toLowerCase()); try (FileInputStream is = new FileInputStream(file)) { ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int n = 0; while (-1 != (n = is.read(buffer))) { os.write(buffer, 0, n); } byte[] bytes = os.toByteArray(); JsonKeyEntry entry = getGson().fromJson(new String(bytes, Charset.forName("UTF-8")), JsonKeyEntry.class); entry.setName(keyName); return entry; } catch (Exception e) { throw new KeyStorageException(e); } } JsonFileKeyStorage();  JsonFileKeyStorage(String keysPath); @Override void delete(String keyName); @Override boolean exists(String keyName); @Override KeyEntry load(String keyName); @Override Set<String> names(); @Override void store(KeyEntry keyEntry); @Override KeyEntry createEntry(String name, byte[] value); @Override void update(KeyEntry keyEntry);  }
@Test public void load() { storage.store(entry); KeyEntry loadedEntry = storage.load(alias); assertTrue(loadedEntry instanceof JsonKeyEntry); assertEquals(entry.getName(), loadedEntry.getName()); assertArrayEquals(entry.getValue(), loadedEntry.getValue()); assertEquals(entry.getMeta(), loadedEntry.getMeta()); }
@Test public void load_nonExisting() { assertThrows(KeyEntryNotFoundException.class, () -> { storage.load(alias); }); }
@Test public void load_nullName() { assertThrows(KeyEntryNotFoundException.class, () -> { storage.load(alias); }); }