DefaultEncryptor implements Encryptor { @Override public byte[] encrypt(byte[] value, byte[] keyRef) { Optional<byte[]> maybeKey = keyProvider.getKey(keyRef); return maybeKey.map(key -> { try { return cryptoAlgorithm.encrypt(value, key); } catch (Exception e) { log.error("error while encrypting data", e); return null; } }).orElse(null); } DefaultEncryptor(KeyProvider keyProvider, CryptoAlgorithm cryptoAlgorithm); @Override byte[] encrypt(byte[] value, byte[] keyRef);  }
@Test public void testEncrypt() 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.encrypt(clearData, key)).willReturn(encodedData); byte[] res = defaultEncryptor.encrypt(clearData, keyRef); assertThat(res).isEqualTo(encodedData); }