Search in sources :

Example 1 with GetKeyRequest

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

the class RecaptchaEnterpriseServiceClientTest method getKeyTest.

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

Example 2 with GetKeyRequest

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

the class RecaptchaEnterpriseServiceV1Beta1ClientTest method getKeyExceptionTest.

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

Example 3 with GetKeyRequest

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

the class UpdateSiteKey method updateSiteKey.

/**
 * Update the properties of the given site key present under the project id.
 *
 * @param projectID: GCloud Project ID.
 * @param recaptchaSiteKeyID: Specify the site key.
 * @param domainName: Specify the domain name for which the settings should be updated.
 */
public static void updateSiteKey(String projectID, String recaptchaSiteKeyID, String domainName) throws IOException, InterruptedException, ExecutionException, TimeoutException {
    // clean up any remaining background resources.
    try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
        // Set the name and the new settings for the key.
        UpdateKeyRequest updateKeyRequest = UpdateKeyRequest.newBuilder().setKey(Key.newBuilder().setDisplayName("any descriptive name for the key").setName(KeyName.of(projectID, recaptchaSiteKeyID).toString()).setWebSettings(WebKeySettings.newBuilder().setAllowAmpTraffic(true).addAllowedDomains(domainName).build()).build()).build();
        client.updateKeyCallable().futureCall(updateKeyRequest).get();
        // Check if the key has been updated.
        GetKeyRequest getKeyRequest = GetKeyRequest.newBuilder().setName(KeyName.of(projectID, recaptchaSiteKeyID).toString()).build();
        Key response = client.getKey(getKeyRequest);
        // Get the changed property.
        boolean allowedAmpTraffic = response.getWebSettings().getAllowAmpTraffic();
        if (!allowedAmpTraffic) {
            System.out.println("Error! reCAPTCHA Site key property hasn't been updated. Please try again !");
            return;
        }
        System.out.println("reCAPTCHA Site key successfully updated !");
    }
}
Also used : UpdateKeyRequest(com.google.recaptchaenterprise.v1.UpdateKeyRequest) GetKeyRequest(com.google.recaptchaenterprise.v1.GetKeyRequest) Key(com.google.recaptchaenterprise.v1.Key) RecaptchaEnterpriseServiceClient(com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient)

Example 4 with GetKeyRequest

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

the class RecaptchaEnterpriseServiceClientTest method getKeyExceptionTest.

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

Example 5 with GetKeyRequest

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

the class RecaptchaEnterpriseServiceV1Beta1ClientTest method getKeyTest.

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

Aggregations

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