Search in sources :

Example 1 with UpdateKeyRequest

use of com.google.recaptchaenterprise.v1beta1.UpdateKeyRequest in project java-recaptchaenterprise by googleapis.

the class RecaptchaEnterpriseServiceClientTest method updateKeyTest.

@Test
public void updateKeyTest() 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);
    UpdateKeyRequest request = UpdateKeyRequest.newBuilder().setKey(Key.newBuilder().build()).setUpdateMask(FieldMask.newBuilder().build()).build();
    Key actualResponse = client.updateKey(request);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockRecaptchaEnterpriseService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    UpdateKeyRequest actualRequest = ((UpdateKeyRequest) actualRequests.get(0));
    Assert.assertEquals(request.getKey(), actualRequest.getKey());
    Assert.assertEquals(request.getUpdateMask(), actualRequest.getUpdateMask());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : UpdateKeyRequest(com.google.recaptchaenterprise.v1.UpdateKeyRequest) AbstractMessage(com.google.protobuf.AbstractMessage) HashMap(java.util.HashMap) Key(com.google.recaptchaenterprise.v1.Key) Test(org.junit.Test)

Example 2 with UpdateKeyRequest

use of com.google.recaptchaenterprise.v1beta1.UpdateKeyRequest in project java-recaptchaenterprise by googleapis.

the class RecaptchaEnterpriseServiceV1Beta1ClientTest method updateKeyExceptionTest.

@Test
public void updateKeyExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockRecaptchaEnterpriseServiceV1Beta1.addException(exception);
    try {
        UpdateKeyRequest request = UpdateKeyRequest.newBuilder().setKey(Key.newBuilder().build()).setUpdateMask(FieldMask.newBuilder().build()).build();
        client.updateKey(request);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : UpdateKeyRequest(com.google.recaptchaenterprise.v1beta1.UpdateKeyRequest) InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException) Test(org.junit.Test)

Example 3 with UpdateKeyRequest

use of com.google.recaptchaenterprise.v1beta1.UpdateKeyRequest in project java-recaptchaenterprise by googleapis.

the class RecaptchaEnterpriseServiceV1Beta1ClientTest method updateKeyTest.

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

Example 4 with UpdateKeyRequest

use of com.google.recaptchaenterprise.v1beta1.UpdateKeyRequest 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 5 with UpdateKeyRequest

use of com.google.recaptchaenterprise.v1beta1.UpdateKeyRequest in project java-recaptchaenterprise by googleapis.

the class RecaptchaEnterpriseServiceClientTest method updateKeyExceptionTest.

@Test
public void updateKeyExceptionTest() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockRecaptchaEnterpriseService.addException(exception);
    try {
        UpdateKeyRequest request = UpdateKeyRequest.newBuilder().setKey(Key.newBuilder().build()).setUpdateMask(FieldMask.newBuilder().build()).build();
        client.updateKey(request);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : UpdateKeyRequest(com.google.recaptchaenterprise.v1.UpdateKeyRequest) InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException) Test(org.junit.Test)

Aggregations

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