### Question:
SecurityDataService { public Optional<EncryptedData> encryptConsentData(String encryptedId, byte[] aspspConsentData) { return getDecryptedIdSetByEncryptedId(encryptedId) .flatMap(dta -> getEncryptedData(dta, aspspConsentData)); } @Autowired  SecurityDataService(Environment environment, CryptoProviderHolder cryptoProviderHolder); Optional<String> encryptId(String originalId); Optional<String> decryptId(String encryptedId); Optional<EncryptedData> encryptConsentData(String encryptedId, byte[] aspspConsentData); Optional<DecryptedData> decryptConsentData(String encryptedId, byte[] aspspConsentData); boolean isConsentIdEncrypted(String consentId);  }### Answer:
@Test void encryptConsentData_Success() { String encryptedId = getEncryptedConsentId(CRYPTO_PROVIDER_ID); byte[] dataWithKey = ArrayUtils.addAll(CONSENT_DATA, CONSENT_KEY.getBytes()); when(cryptoProviderHolder.getProviderById(CRYPTO_PROVIDER_ID)).thenReturn(Optional.of(CRYPTO_PROVIDER)); when(cryptoProviderHolder.getProviderById(CRYPTO_PROVIDER_DATA)).thenReturn(Optional.of(CRYPTO_PROVIDER)); EncryptedData expected = new EncryptedData(dataWithKey); Optional<EncryptedData> actual = securityDataService.encryptConsentData(encryptedId, CONSENT_DATA); assertTrue(actual.isPresent()); assertEquals(expected, actual.get()); }
@Test void encryptConsentData_Failure_NonExistingAlgorithm() { String encryptedId = getEncryptedConsentId(NON_EXISTING_CRYPT_PROVIDER_ID); when(cryptoProviderHolder.getProviderById(NON_EXISTING_CRYPT_PROVIDER_ID)).thenReturn(Optional.empty()); Optional<EncryptedData> actual = securityDataService.encryptConsentData(encryptedId, CONSENT_DATA); assertFalse(actual.isPresent()); }
@Test void encryptConsentData_Failure_DecryptionError() { String encryptedId = getEncryptedConsentId(FAILING_CRYPTO_PROVIDER_ID); when(cryptoProviderHolder.getProviderById(FAILING_CRYPTO_PROVIDER_ID)).thenReturn(Optional.of(FAILING_CRYPTO_PROVIDER)); Optional<EncryptedData> actual = securityDataService.encryptConsentData(encryptedId, CONSENT_DATA); assertFalse(actual.isPresent()); }