DefaultDecryptableDataDetector implements DecryptableDataDetector { @Override public void check() { final Collection<UUID> uuids = keySet.getUuids(); final Long countTotalCredentials = credentialVersionDataService.count(); final Long countCredentialsEncryptedWithKeyWeHave = credentialVersionDataService.countEncryptedWithKeyUuidIn(uuids); if (countTotalCredentials > 0 && countCredentialsEncryptedWithKeyWeHave == 0) { throw new RuntimeException( "The encryption keys provided cannot decrypt any of the " + countTotalCredentials + " value(s) in the database. " + "Please make sure you've provided the necessary encryption keys."); } } DefaultDecryptableDataDetector(
    final EncryptionKeySet keySet,
    final CredentialVersionDataService credentialVersionDataService
  ); @Override void check();  }
@Test public void whenNoCredentialsCouldBeDecrypted_whenThereAreNoCredentials_doesNotError() { when(credentialVersionDataService.count()).thenReturn(0L); when(credentialVersionDataService.countEncryptedWithKeyUuidIn(any())).thenReturn(0L); decryptableDataDetector = new DefaultDecryptableDataDetector( keySet, credentialVersionDataService ); decryptableDataDetector.check(); }
@Test public void whenNoCredentialsCouldBeDecrypted_whenThereAreCredentials_andNoneCanBeDecrypted() { when(credentialVersionDataService.count()).thenReturn(4L); when(credentialVersionDataService.countEncryptedWithKeyUuidIn(any())).thenReturn(0L); decryptableDataDetector = new DefaultDecryptableDataDetector( keySet, credentialVersionDataService ); try { decryptableDataDetector.check(); } catch (final RuntimeException rte) { assertThat(rte.getMessage(), containsString("The encryption keys provided cannot decrypt any of the 4 value(s) in the database." + " Please make sure you've provided the necessary encryption keys.")); } }
@Test public void whenCredentialsCanBeDecrypted_doesNotError() { when(credentialVersionDataService.count()).thenReturn(4L); when(credentialVersionDataService.countEncryptedWithKeyUuidIn(any())).thenReturn(1L); decryptableDataDetector = new DefaultDecryptableDataDetector( keySet, credentialVersionDataService ); decryptableDataDetector.check(); }