public synchronized String encrypt(String keyRingId, String keyId, String message) {

    CryptoKeyName keyName = CryptoKeyName.of(projectId, region, keyRingId, keyId);
    LOG.info("Encrypting given message using key {}.", keyName.toString());

    try (KeyManagementServiceClient client = clientFactory.getKMSClient()) {

      EncryptResponse response = client.encrypt(keyName, ByteString.copyFromUtf8(message));

      LOG.info("Successfully encrypted message.");
      return new String(
          Base64.getEncoder().encode(response.getCiphertext().toByteArray()),
          StandardCharsets.UTF_8);
    }
  }
@Test
  public void testEncryptShouldThrowErrorWhenClientFailsToConnect() {
    when(kmsClientFactory.getKMSClient()).thenThrow(KMSResourceManagerException.class);

    assertThrows(
        KMSResourceManagerException.class,
        () -> testManager.encrypt(KEYRING_ID, KEY_ID, "test message"));
  }