Search in sources :

Example 1 with CreateKeyRequest

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

the class RecaptchaEnterpriseServiceClientTest method createKeyExceptionTest.

@Test
public void createKeyExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockRecaptchaEnterpriseService.addException(exception);
    try {
        CreateKeyRequest request = CreateKeyRequest.newBuilder().setParent(ProjectName.of("[PROJECT]").toString()).setKey(Key.newBuilder().build()).build();
        client.createKey(request);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) CreateKeyRequest(com.google.recaptchaenterprise.v1.CreateKeyRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) Test(org.junit.Test)

Example 2 with CreateKeyRequest

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

the class RecaptchaEnterpriseServiceV1Beta1ClientTest method createKeyTest.

@Test
public void createKeyTest() throws Exception {
    Key expectedResponse = Key.newBuilder().setName(KeyName.of("[PROJECT]", "[KEY]").toString()).setDisplayName("displayName1714148973").build();
    mockRecaptchaEnterpriseServiceV1Beta1.addResponse(expectedResponse);
    CreateKeyRequest request = CreateKeyRequest.newBuilder().setParent(ProjectName.of("[PROJECT]").toString()).setKey(Key.newBuilder().build()).build();
    Key actualResponse = client.createKey(request);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockRecaptchaEnterpriseServiceV1Beta1.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    CreateKeyRequest actualRequest = ((CreateKeyRequest) actualRequests.get(0));
    Assert.assertEquals(request.getParent(), actualRequest.getParent());
    Assert.assertEquals(request.getKey(), actualRequest.getKey());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) CreateKeyRequest(com.google.recaptchaenterprise.v1beta1.CreateKeyRequest) Key(com.google.recaptchaenterprise.v1beta1.Key) Test(org.junit.Test)

Example 3 with CreateKeyRequest

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

the class RecaptchaEnterpriseServiceClientTest method createKeyTest.

@Test
public void createKeyTest() 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);
    CreateKeyRequest request = CreateKeyRequest.newBuilder().setParent(ProjectName.of("[PROJECT]").toString()).setKey(Key.newBuilder().build()).build();
    Key actualResponse = client.createKey(request);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockRecaptchaEnterpriseService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    CreateKeyRequest actualRequest = ((CreateKeyRequest) actualRequests.get(0));
    Assert.assertEquals(request.getParent(), actualRequest.getParent());
    Assert.assertEquals(request.getKey(), actualRequest.getKey());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) HashMap(java.util.HashMap) CreateKeyRequest(com.google.recaptchaenterprise.v1.CreateKeyRequest) Key(com.google.recaptchaenterprise.v1.Key) Test(org.junit.Test)

Example 4 with CreateKeyRequest

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

the class RecaptchaEnterpriseServiceV1Beta1ClientTest method createKeyExceptionTest.

@Test
public void createKeyExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockRecaptchaEnterpriseServiceV1Beta1.addException(exception);
    try {
        CreateKeyRequest request = CreateKeyRequest.newBuilder().setParent(ProjectName.of("[PROJECT]").toString()).setKey(Key.newBuilder().build()).build();
        client.createKey(request);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) CreateKeyRequest(com.google.recaptchaenterprise.v1beta1.CreateKeyRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) Test(org.junit.Test)

Example 5 with CreateKeyRequest

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

the class CreateSiteKey method createSiteKey.

/**
 * Create reCAPTCHA Site key which binds a domain name to a unique key.
 *
 * @param projectID : GCloud Project ID.
 * @param domainName : Specify the domain name in which the reCAPTCHA should be activated.
 */
public static String createSiteKey(String projectID, String domainName) throws IOException {
    // clean up any remaining background resources.
    try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
        // Set the type of reCAPTCHA to be displayed.
        // For different types, see: https://cloud.google.com/recaptcha-enterprise/docs/keys
        Key scoreKey = Key.newBuilder().setDisplayName("any_descriptive_name_for_the_key").setWebSettings(WebKeySettings.newBuilder().addAllowedDomains(domainName).setAllowAmpTraffic(false).setIntegrationType(IntegrationType.SCORE).build()).build();
        CreateKeyRequest createKeyRequest = CreateKeyRequest.newBuilder().setParent(ProjectName.of(projectID).toString()).setKey(scoreKey).build();
        // Get the name of the created reCAPTCHA site key.
        Key response = client.createKey(createKeyRequest);
        String keyName = response.getName();
        String recaptchaSiteKey = keyName.substring(keyName.lastIndexOf("/") + 1);
        System.out.println("reCAPTCHA Site key created successfully. Site Key: " + recaptchaSiteKey);
        return recaptchaSiteKey;
    }
}
Also used : CreateKeyRequest(com.google.recaptchaenterprise.v1.CreateKeyRequest) Key(com.google.recaptchaenterprise.v1.Key) RecaptchaEnterpriseServiceClient(com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient)

Aggregations

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