EncryptionProviderFactory { public EncryptionProvider getEncryptionService(final EncryptionKeyProvider provider) throws Exception { final EncryptionProvider encryptionService; if (map.containsKey(provider.getProviderName())) { return map.get(provider.getProviderName()); } else { switch (provider.getProviderType()) { case HSM: encryptionService = new LunaEncryptionService(new LunaConnection(provider.getConfiguration()), encryptionKeysConfiguration.isKeyCreationEnabled(), timedRetry); break; case KMS_PLUGIN: encryptionService = new KMSEncryptionProvider(provider.getConfiguration()); break; default: encryptionService = new PasswordEncryptionService(passwordKeyProxyFactory); break; } map.put(provider.getProviderName(), encryptionService); return encryptionService; } } @Autowired  EncryptionProviderFactory(final EncryptionKeysConfiguration keysConfiguration, final TimedRetry timedRetry,
                                   final PasswordKeyProxyFactory passwordKeyProxyFactory); EncryptionProvider getEncryptionService(final EncryptionKeyProvider provider);  }
@Test public void getEncryptionService_whenEncryptionServiceIsAlreadyInitialized() throws Exception { final EncryptionProviderFactory subject = new EncryptionProviderFactory( mock(EncryptionKeysConfiguration.class), mock(TimedRetry.class), mock(PasswordKeyProxyFactory.class) ); when(provider.getProviderType()).thenReturn(ProviderType.INTERNAL); final InternalEncryptionService internal = (InternalEncryptionService) subject.getEncryptionService(provider); final InternalEncryptionService internalAgain = (InternalEncryptionService) subject.getEncryptionService(provider); assertThat(internal, sameInstance(internalAgain)); assertThat(internal, instanceOf(PasswordEncryptionService.class)); }