CreateNewKeyPairUnlessUserSpecifiedOtherwise { @VisibleForTesting public void execute(URI org, String group, String identity, TerremarkVCloudTemplateOptions options) { String sshKeyFingerprint = options.getSshKeyFingerprint(); boolean shouldAutomaticallyCreateKeyPair = options.shouldAutomaticallyCreateKeyPair(); if (sshKeyFingerprint == null && shouldAutomaticallyCreateKeyPair) { synchronized (credentialStore) { if (credentialStore.containsKey("group#" + group)) { LoginCredentials creds = LoginCredentials.fromCredentials(credentialStore.get("group#" + group)); checkState(creds.getOptionalPrivateKey().isPresent(), "incorrect state: should have private key for: %s", creds); options.sshKeyFingerprint(SshKeys.fingerprintPrivateKey(creds.getPrivateKey())); } else { KeyPair keyPair = createUniqueKeyPair.apply(new OrgAndName(org, group)); credentialStore.put("group#" + group, LoginCredentials.builder().user(identity).privateKey( keyPair.getPrivateKey()).build()); options.sshKeyFingerprint(keyPair.getFingerPrint()); } } } } @Inject  CreateNewKeyPairUnlessUserSpecifiedOtherwise(Map<String, Credentials> credentialStore,
            CreateUniqueKeyPair createUniqueKeyPair); @VisibleForTesting void execute(URI org, String group, String identity, TerremarkVCloudTemplateOptions options);  }
@Test(enabled = false) public void testCreateNewKeyPairUnlessUserSpecifiedOtherwise_createsNewKeyPairAndReturnsItsNameByDefault() { URI org = URI.create("org1"); String identity = "identity"; String group = "group"; String systemGeneratedFingerprint = "systemGeneratedKeyPairfinger"; TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions(); CreateNewKeyPairUnlessUserSpecifiedOtherwise strategy = setupStrategy(); LoginCredentials keyPairCredentials = LoginCredentials.builder().privateKey(KeyPairHandlerTest.keyPair.getPrivateKey()).build(); KeyPair keyPair = createMock(KeyPair.class); expect(strategy.credentialStore.containsKey("group#group")).andReturn(false); expect(strategy.createUniqueKeyPair.apply(new OrgAndName(org, "group"))).andReturn(keyPair); expect(keyPair.getFingerPrint()).andReturn(KeyPairHandlerTest.keyPair.getFingerPrint()).atLeastOnce(); expect(strategy.credentialStore.put("group#group", keyPairCredentials)).andReturn(null); replay(keyPair); replayStrategy(strategy); strategy.execute(org, group, identity, options); assertEquals(options.getSshKeyFingerprint(), systemGeneratedFingerprint); verify(keyPair); verifyStrategy(strategy); }