DefaultDecryptor implements Decryptor { @Override public byte[] decrypt(byte[] value, byte[] keyRef) { Optional<byte[]> maybeKey = keyProvider.getKey(keyRef); return maybeKey.map(key -> { try { return cryptoAlgorithm.decrypt(value, key); } catch (Exception e) { log.error("error while decrypting data", e); return null; } } ).orElse(null); } DefaultDecryptor(KeyProvider keyProvider, CryptoAlgorithm cryptoAlgorithm); @Override byte[] decrypt(byte[] value, byte[] keyRef);  }
@Test public void testDecrypt() throws Exception { byte[] keyRef = "keyref".getBytes(StandardCharsets.UTF_8); byte[] key = "key".getBytes(StandardCharsets.UTF_8); byte[] clearData = "clear data".getBytes(StandardCharsets.UTF_8); byte[] encodedData = "encoded data".getBytes(StandardCharsets.UTF_8); given(keyProvider.getKey(keyRef)).willReturn(Optional.of(key)); given(encryptionAlgorithm.decrypt(encodedData, key)).willReturn(clearData); byte[] res = defaultDecryptor.decrypt(encodedData, keyRef); assertThat(res).isEqualTo(clearData); }