Search in sources :

Example 1 with MigrateKeyRequest

use of com.google.recaptchaenterprise.v1.MigrateKeyRequest in project java-recaptchaenterprise by googleapis.

the class RecaptchaEnterpriseServiceClientTest method migrateKeyExceptionTest.

@Test
public void migrateKeyExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockRecaptchaEnterpriseService.addException(exception);
    try {
        MigrateKeyRequest request = MigrateKeyRequest.newBuilder().setName(KeyName.of("[PROJECT]", "[KEY]").toString()).build();
        client.migrateKey(request);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) MigrateKeyRequest(com.google.recaptchaenterprise.v1.MigrateKeyRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) Test(org.junit.Test)

Example 2 with MigrateKeyRequest

use of com.google.recaptchaenterprise.v1.MigrateKeyRequest in project java-recaptchaenterprise by googleapis.

the class MigrateKey method migrateKey.

/**
 * Migrate a key from reCAPTCHA (non-Enterprise) to reCAPTCHA Enterprise. If you created the key
 * using Admin console: https://www.google.com/recaptcha/admin/site, then use this API to migrate
 * to reCAPTCHA Enterprise. For more info, see:
 * https://cloud.google.com/recaptcha-enterprise/docs/migrate-recaptcha
 *
 * @param projectId: Google Cloud Project Id.
 * @param recaptchaSiteKey: Specify the site key to migrate.
 */
public static void migrateKey(String projectId, String recaptchaSiteKey) throws IOException {
    // clean up any remaining background resources.
    try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
        // Specify the key name to migrate.
        MigrateKeyRequest migrateKeyRequest = MigrateKeyRequest.newBuilder().setName(KeyName.of(projectId, recaptchaSiteKey).toString()).build();
        Key response = client.migrateKey(migrateKeyRequest);
        // key is present.
        for (Key key : recaptcha.ListSiteKeys.listSiteKeys(projectId).iterateAll()) {
            if (key.equals(response)) {
                System.out.printf("Key migrated successfully: %s", recaptchaSiteKey);
            }
        }
    }
}
Also used : MigrateKeyRequest(com.google.recaptchaenterprise.v1.MigrateKeyRequest) Key(com.google.recaptchaenterprise.v1.Key) RecaptchaEnterpriseServiceClient(com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient)

Example 3 with MigrateKeyRequest

use of com.google.recaptchaenterprise.v1.MigrateKeyRequest in project java-recaptchaenterprise by googleapis.

the class RecaptchaEnterpriseServiceClientTest method migrateKeyTest.

@Test
public void migrateKeyTest() throws Exception {
    Key expectedResponse = Key.newBuilder().setName(KeyName.of("[PROJECT]", "[KEY]").toString()).setDisplayName("displayName1714148973").putAllLabels(new HashMap<String, String>()).setCreateTime(Timestamp.newBuilder().build()).setTestingOptions(TestingOptions.newBuilder().build()).build();
    mockRecaptchaEnterpriseService.addResponse(expectedResponse);
    MigrateKeyRequest request = MigrateKeyRequest.newBuilder().setName(KeyName.of("[PROJECT]", "[KEY]").toString()).build();
    Key actualResponse = client.migrateKey(request);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockRecaptchaEnterpriseService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    MigrateKeyRequest actualRequest = ((MigrateKeyRequest) actualRequests.get(0));
    Assert.assertEquals(request.getName(), actualRequest.getName());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) MigrateKeyRequest(com.google.recaptchaenterprise.v1.MigrateKeyRequest) HashMap(java.util.HashMap) Key(com.google.recaptchaenterprise.v1.Key) Test(org.junit.Test)

Aggregations

MigrateKeyRequest (com.google.recaptchaenterprise.v1.MigrateKeyRequest)3 Key (com.google.recaptchaenterprise.v1.Key)2 Test (org.junit.Test)2 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)1 RecaptchaEnterpriseServiceClient (com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient)1 AbstractMessage (com.google.protobuf.AbstractMessage)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 HashMap (java.util.HashMap)1