PrivateKeyStorage { public void store(VirgilPrivateKey privateKey, String name, Map<String, String> meta) throws CryptoException { byte[] exportedKeyData = this.keyExporter.exportPrivateKey(privateKey); KeyEntry keyEntry = new PrivateKeyEntry(name, exportedKeyData); if (meta != null && !meta.isEmpty()) { keyEntry.getMeta().putAll(meta); } this.keyStorage.store(keyEntry); } PrivateKeyStorage(VirgilPrivateKeyExporter keyExporter, KeyStorage keyStorage); void delete(String keyName); boolean exists(String keyName); Tuple<VirgilPrivateKey, Map<String, String>> load(String keyName); Set<String> names(); void store(VirgilPrivateKey privateKey, String name, Map<String, String> meta);  }
@Test public void store() throws CryptoException { byte[] privateKeyData = this.crypto.exportPrivateKey((VirgilPrivateKey) privateKey); when(this.keyExporter.exportPrivateKey(privateKey)).thenReturn(privateKeyData); Map<String, String> meta = new HashMap<>(); meta.put("key1", "value1"); meta.put("key2", "value2"); storage.store(this.privateKey, this.keyName, meta); verify(this.keyExporter, times(1)).exportPrivateKey(privateKey); ArgumentCaptor<KeyEntry> keyEntryCaptor = ArgumentCaptor.forClass(KeyEntry.class); verify(this.keyStorage, times(1)).store(keyEntryCaptor.capture()); KeyEntry keyEntry = keyEntryCaptor.getValue(); assertNotNull(keyEntry); assertEquals(this.keyName, keyEntry.getName()); assertArrayEquals(crypto.exportPrivateKey((VirgilPrivateKey) this.privateKey), keyEntry.getValue()); assertNotNull(keyEntry.getMeta()); assertEquals(meta, keyEntry.getMeta()); }
@SuppressWarnings("unchecked") @Test public void store_noMeta() throws CryptoException { byte[] privateKeyData = this.crypto.exportPrivateKey((VirgilPrivateKey) privateKey); when(this.keyExporter.exportPrivateKey(privateKey)).thenReturn(privateKeyData); storage.store(this.privateKey, this.keyName, Collections.EMPTY_MAP); verify(this.keyExporter, times(1)).exportPrivateKey(privateKey); ArgumentCaptor<KeyEntry> keyEntryCaptor = ArgumentCaptor.forClass(KeyEntry.class); verify(this.keyStorage, times(1)).store(keyEntryCaptor.capture()); KeyEntry keyEntry = keyEntryCaptor.getValue(); assertNotNull(keyEntry); assertEquals(this.keyName, keyEntry.getName()); assertArrayEquals(crypto.exportPrivateKey((VirgilPrivateKey) this.privateKey), keyEntry.getValue()); assertNotNull(keyEntry.getMeta()); assertTrue(keyEntry.getMeta().isEmpty()); }