### Question:
AwsPublicKeyConnector implements PublicKeyConnector { @Override public void register(PublicKeyRegisterRequest request) { LOGGER.debug("Importing public key {} to {} region on AWS", request.getPublicKeyId(), request.getRegion()); AwsCredentialView awsCredential = new AwsCredentialView(request.getCredential()); try { AmazonEC2Client client = awsClient.createAccess(awsCredential, request.getRegion()); if (!exists(client, request.getPublicKeyId())) { ImportKeyPairRequest importKeyPairRequest = new ImportKeyPairRequest(request.getPublicKeyId(), request.getPublicKey()); client.importKeyPair(importKeyPairRequest); } } catch (Exception e) { String errorMessage = String.format("Failed to import public key [%s:'%s', region: '%s'], detailed message: %s", getType(awsCredential), getAwsId(awsCredential), request.getRegion(), e.getMessage()); LOGGER.error(errorMessage, e); throw new CloudConnectorException(e.getMessage(), e); } } AwsPublicKeyConnector(AwsClient awsClient); @Override void register(PublicKeyRegisterRequest request); @Override void unregister(PublicKeyUnregisterRequest request); @Override boolean exists(PublicKeyDescribeRequest request); @Override Platform platform(); @Override Variant variant();  }### Answer:
@Test void registerExisting() { PublicKeyRegisterRequest request = generateRegisterRequest(); when(ec2client.describeKeyPairs(any())).thenReturn(new DescribeKeyPairsResult()); underTest.register(request); verifyNoMoreInteractions(ec2client); }
@Test void registerNew() { PublicKeyRegisterRequest request = generateRegisterRequest(); when(ec2client.describeKeyPairs(any())).thenThrow(new AmazonServiceException("no such key")); underTest.register(request); ArgumentCaptor<ImportKeyPairRequest> captor = ArgumentCaptor.forClass(ImportKeyPairRequest.class); verify(ec2client).importKeyPair(captor.capture()); ImportKeyPairRequest result = captor.getValue(); assertThat(result.getKeyName()).isEqualTo(PUBLIC_KEY_ID); assertThat(result.getPublicKeyMaterial()).isEqualTo(PUBLIC_KEY); verifyNoMoreInteractions(ec2client); }
@Test void registerProviderException() { PublicKeyRegisterRequest request = generateRegisterRequest(); when(ec2client.describeKeyPairs(any())).thenThrow(new AmazonServiceException("no such key")); when(ec2client.importKeyPair(any())).thenThrow(new RuntimeException(RUNTIME_EXCEPTION)); assertThatThrownBy(() -> underTest.register(request)).isInstanceOf(CloudConnectorException.class).hasMessage(RUNTIME_EXCEPTION); verifyNoMoreInteractions(ec2client); }