EncryptionKeyService { public Optional<EncryptionKey> getKey(String keyLabel) { for (EncryptionKey key : encryptionKeys) { if (key.getLabel().equals(keyLabel)) { return Optional.of(key); } } return Optional.empty(); } EncryptionKeyService(
            final @Value("${encryption.active_key_label}") String activeKeyLabel,
            final @Value("#{@config['encryption']['encryption_keys']}") List<EncryptionKey> encryptionKeys); EncryptionKey getActiveKey(); Optional<EncryptionKey> getKey(String keyLabel);  }
@Test public void shouldBeAbleToFetchInactiveKey() { Optional<EncryptionKeyService.EncryptionKey> key = encryptionKeyService.getKey("active-key2"); assertThat(key.isPresent(), is(true)); assertThat(key.get().getLabel(), is("active-key2")); assertThat(key.get().getPassphrase(), is("some-passphrase2")); }
@Test public void shouldThrowAMeaningfulErrorWhenUnableToFindAKey() { Optional<EncryptionKeyService.EncryptionKey> missingKey = encryptionKeyService.getKey("key-that-does-not-exist"); assertThat(missingKey.isPresent(), is(false)); }