From c7b3a779e03a1d53c4eff7a739600ed4747fcf95 Mon Sep 17 00:00:00 2001 From: Garrett Jones Date: Wed, 31 Aug 2016 10:19:40 -0700 Subject: [PATCH] Pub/Sub SPI: Adding IAM methods and auto-generated unit tests --- google-cloud-pubsub/pom.xml | 2 +- .../cloud/pubsub/spi/v1/PublisherApi.java | 239 +++++++++++ .../pubsub/spi/v1/PublisherSettings.java | 96 ++++- .../cloud/pubsub/spi/v1/SubscriberApi.java | 239 +++++++++++ .../pubsub/spi/v1/SubscriberSettings.java | 96 ++++- .../cloud/pubsub/spi/v1/package-info.java | 34 +- .../cloud/pubsub/spi/v1/MockIAMPolicy.java | 50 +++ .../pubsub/spi/v1/MockIAMPolicyImpl.java | 79 ++++ .../cloud/pubsub/spi/v1/MockPublisher.java | 50 +++ .../pubsub/spi/v1/MockPublisherImpl.java | 109 ++++++ .../cloud/pubsub/spi/v1/MockSubscriber.java | 50 +++ .../pubsub/spi/v1/MockSubscriberImpl.java | 130 ++++++ .../cloud/pubsub/spi/v1/PublisherTest.java | 313 +++++++++++++++ .../cloud/pubsub/spi/v1/SubscriberTest.java | 370 ++++++++++++++++++ 14 files changed, 1835 insertions(+), 22 deletions(-) create mode 100644 google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/MockIAMPolicy.java create mode 100644 google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/MockIAMPolicyImpl.java create mode 100644 google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/MockPublisher.java create mode 100644 google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/MockPublisherImpl.java create mode 100644 google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/MockSubscriber.java create mode 100644 google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/MockSubscriberImpl.java create mode 100644 google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/PublisherTest.java create mode 100644 google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/SubscriberTest.java diff --git a/google-cloud-pubsub/pom.xml b/google-cloud-pubsub/pom.xml index 18b22f50d5ad..861af28c66a0 100644 --- a/google-cloud-pubsub/pom.xml +++ b/google-cloud-pubsub/pom.xml @@ -30,7 +30,7 @@ com.google.api.grpc grpc-google-pubsub-v1 - 0.0.7 + 0.0.9 io.grpc diff --git a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/PublisherApi.java b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/PublisherApi.java index 199280657158..4b992d2db4b5 100644 --- a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/PublisherApi.java +++ b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/PublisherApi.java @@ -16,6 +16,11 @@ import com.google.api.gax.core.PageAccessor; import com.google.api.gax.grpc.ApiCallable; import com.google.api.gax.protobuf.PathTemplate; +import com.google.iam.v1.GetIamPolicyRequest; +import com.google.iam.v1.Policy; +import com.google.iam.v1.SetIamPolicyRequest; +import com.google.iam.v1.TestIamPermissionsRequest; +import com.google.iam.v1.TestIamPermissionsResponse; import com.google.protobuf.ByteString; import com.google.protobuf.Empty; import com.google.pubsub.v1.DeleteTopicRequest; @@ -105,6 +110,10 @@ public class PublisherApi implements AutoCloseable { private final ApiCallable> listTopicSubscriptionsPagedCallable; private final ApiCallable deleteTopicCallable; + private final ApiCallable setIamPolicyCallable; + private final ApiCallable getIamPolicyCallable; + private final ApiCallable + testIamPermissionsCallable; public final PublisherSettings getSettings() { return settings; @@ -204,6 +213,12 @@ protected PublisherApi(PublisherSettings settings) throws IOException { settings.listTopicSubscriptionsSettings(), this.channel, this.executor); this.deleteTopicCallable = ApiCallable.create(settings.deleteTopicSettings(), this.channel, this.executor); + this.setIamPolicyCallable = + ApiCallable.create(settings.setIamPolicySettings(), this.channel, this.executor); + this.getIamPolicyCallable = + ApiCallable.create(settings.getIamPolicySettings(), this.channel, this.executor); + this.testIamPermissionsCallable = + ApiCallable.create(settings.testIamPermissionsSettings(), this.channel, this.executor); if (settings.getChannelProvider().shouldAutoClose()) { closeables.add( @@ -726,6 +741,230 @@ public final ApiCallable deleteTopicCallable() { return deleteTopicCallable; } + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Sets the access control policy on the specified resource. Replaces any + * existing policy. + * + * Sample code: + *

+   * try (PublisherApi publisherApi = PublisherApi.create()) {
+   *   String formattedResource = PublisherApi.formatTopicName("[PROJECT]", "[TOPIC]");
+   *   Policy policy = Policy.newBuilder().build();
+   *   Policy response = publisherApi.setIamPolicy(formattedResource, policy);
+   * }
+   * 
+ * + * @param resource REQUIRED: The resource for which policy is being specified. + * Resource is usually specified as a path, such as, + * projects/{project}/zones/{zone}/disks/{disk}. + * @param policy REQUIRED: The complete policy to be applied to the 'resource'. The size of + * the policy is limited to a few 10s of KB. An empty policy is in general a + * valid policy but certain services (like Projects) might reject them. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final Policy setIamPolicy(String resource, Policy policy) { + TOPIC_PATH_TEMPLATE.validate(resource, "setIamPolicy"); + SetIamPolicyRequest request = + SetIamPolicyRequest.newBuilder().setResource(resource).setPolicy(policy).build(); + return setIamPolicy(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Sets the access control policy on the specified resource. Replaces any + * existing policy. + * + * Sample code: + *

+   * try (PublisherApi publisherApi = PublisherApi.create()) {
+   *   String formattedResource = PublisherApi.formatTopicName("[PROJECT]", "[TOPIC]");
+   *   Policy policy = Policy.newBuilder().build();
+   *   SetIamPolicyRequest request = SetIamPolicyRequest.newBuilder()
+   *     .setResource(formattedResource)
+   *     .setPolicy(policy)
+   *     .build();
+   *   Policy response = publisherApi.setIamPolicy(request);
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final Policy setIamPolicy(SetIamPolicyRequest request) { + return setIamPolicyCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Sets the access control policy on the specified resource. Replaces any + * existing policy. + * + * Sample code: + *

+   * try (PublisherApi publisherApi = PublisherApi.create()) {
+   *   String formattedResource = PublisherApi.formatTopicName("[PROJECT]", "[TOPIC]");
+   *   Policy policy = Policy.newBuilder().build();
+   *   SetIamPolicyRequest request = SetIamPolicyRequest.newBuilder()
+   *     .setResource(formattedResource)
+   *     .setPolicy(policy)
+   *     .build();
+   *   ListenableFuture<Policy> future = publisherApi.setIamPolicyCallable().futureCall(request);
+   *   // Do something
+   *   Policy response = future.get();
+   * }
+   * 
+ */ + public final ApiCallable setIamPolicyCallable() { + return setIamPolicyCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Gets the access control policy for a resource. Is empty if the + * policy or the resource does not exist. + * + * Sample code: + *

+   * try (PublisherApi publisherApi = PublisherApi.create()) {
+   *   String formattedResource = PublisherApi.formatTopicName("[PROJECT]", "[TOPIC]");
+   *   Policy response = publisherApi.getIamPolicy(formattedResource);
+   * }
+   * 
+ * + * @param resource REQUIRED: The resource for which policy is being requested. Resource + * is usually specified as a path, such as, projects/{project}. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final Policy getIamPolicy(String resource) { + TOPIC_PATH_TEMPLATE.validate(resource, "getIamPolicy"); + GetIamPolicyRequest request = GetIamPolicyRequest.newBuilder().setResource(resource).build(); + return getIamPolicy(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Gets the access control policy for a resource. Is empty if the + * policy or the resource does not exist. + * + * Sample code: + *

+   * try (PublisherApi publisherApi = PublisherApi.create()) {
+   *   String formattedResource = PublisherApi.formatTopicName("[PROJECT]", "[TOPIC]");
+   *   GetIamPolicyRequest request = GetIamPolicyRequest.newBuilder()
+   *     .setResource(formattedResource)
+   *     .build();
+   *   Policy response = publisherApi.getIamPolicy(request);
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + private final Policy getIamPolicy(GetIamPolicyRequest request) { + return getIamPolicyCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Gets the access control policy for a resource. Is empty if the + * policy or the resource does not exist. + * + * Sample code: + *

+   * try (PublisherApi publisherApi = PublisherApi.create()) {
+   *   String formattedResource = PublisherApi.formatTopicName("[PROJECT]", "[TOPIC]");
+   *   GetIamPolicyRequest request = GetIamPolicyRequest.newBuilder()
+   *     .setResource(formattedResource)
+   *     .build();
+   *   ListenableFuture<Policy> future = publisherApi.getIamPolicyCallable().futureCall(request);
+   *   // Do something
+   *   Policy response = future.get();
+   * }
+   * 
+ */ + public final ApiCallable getIamPolicyCallable() { + return getIamPolicyCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Returns permissions that a caller has on the specified resource. + * + * Sample code: + *

+   * try (PublisherApi publisherApi = PublisherApi.create()) {
+   *   String formattedResource = PublisherApi.formatTopicName("[PROJECT]", "[TOPIC]");
+   *   List<String> permissions = new ArrayList<>();
+   *   TestIamPermissionsResponse response = publisherApi.testIamPermissions(formattedResource, permissions);
+   * }
+   * 
+ * + * @param resource REQUIRED: The resource for which policy detail is being requested. + * Resource is usually specified as a path, such as, projects/{project}. + * @param permissions The set of permissions to check for the 'resource'. Permissions with + * wildcards (such as '*' or 'storage.*') are not allowed. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final TestIamPermissionsResponse testIamPermissions( + String resource, List permissions) { + TOPIC_PATH_TEMPLATE.validate(resource, "testIamPermissions"); + TestIamPermissionsRequest request = + TestIamPermissionsRequest.newBuilder() + .setResource(resource) + .addAllPermissions(permissions) + .build(); + return testIamPermissions(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Returns permissions that a caller has on the specified resource. + * + * Sample code: + *

+   * try (PublisherApi publisherApi = PublisherApi.create()) {
+   *   String formattedResource = PublisherApi.formatTopicName("[PROJECT]", "[TOPIC]");
+   *   List<String> permissions = new ArrayList<>();
+   *   TestIamPermissionsRequest request = TestIamPermissionsRequest.newBuilder()
+   *     .setResource(formattedResource)
+   *     .addAllPermissions(permissions)
+   *     .build();
+   *   TestIamPermissionsResponse response = publisherApi.testIamPermissions(request);
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final TestIamPermissionsResponse testIamPermissions(TestIamPermissionsRequest request) { + return testIamPermissionsCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Returns permissions that a caller has on the specified resource. + * + * Sample code: + *

+   * try (PublisherApi publisherApi = PublisherApi.create()) {
+   *   String formattedResource = PublisherApi.formatTopicName("[PROJECT]", "[TOPIC]");
+   *   List<String> permissions = new ArrayList<>();
+   *   TestIamPermissionsRequest request = TestIamPermissionsRequest.newBuilder()
+   *     .setResource(formattedResource)
+   *     .addAllPermissions(permissions)
+   *     .build();
+   *   ListenableFuture<TestIamPermissionsResponse> future = publisherApi.testIamPermissionsCallable().futureCall(request);
+   *   // Do something
+   *   TestIamPermissionsResponse response = future.get();
+   * }
+   * 
+ */ + public final ApiCallable + testIamPermissionsCallable() { + return testIamPermissionsCallable; + } + /** * Initiates an orderly shutdown in which preexisting calls continue but new calls are immediately * cancelled. diff --git a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/PublisherSettings.java b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/PublisherSettings.java index f91bc59e9658..11db4dde916f 100644 --- a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/PublisherSettings.java +++ b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/PublisherSettings.java @@ -30,6 +30,12 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Lists; import com.google.common.collect.Sets; +import com.google.iam.v1.GetIamPolicyRequest; +import com.google.iam.v1.IAMPolicyGrpc; +import com.google.iam.v1.Policy; +import com.google.iam.v1.SetIamPolicyRequest; +import com.google.iam.v1.TestIamPermissionsRequest; +import com.google.iam.v1.TestIamPermissionsResponse; import com.google.protobuf.Empty; import com.google.pubsub.v1.DeleteTopicRequest; import com.google.pubsub.v1.GetTopicRequest; @@ -118,6 +124,10 @@ public class PublisherSettings extends ServiceApiSettings { ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> listTopicSubscriptionsSettings; private final SimpleCallSettings deleteTopicSettings; + private final SimpleCallSettings setIamPolicySettings; + private final SimpleCallSettings getIamPolicySettings; + private final SimpleCallSettings + testIamPermissionsSettings; /** * Returns the object with the settings used for calls to createTopic. @@ -164,6 +174,28 @@ public SimpleCallSettings deleteTopicSettings() { return deleteTopicSettings; } + /** + * Returns the object with the settings used for calls to setIamPolicy. + */ + public SimpleCallSettings setIamPolicySettings() { + return setIamPolicySettings; + } + + /** + * Returns the object with the settings used for calls to getIamPolicy. + */ + public SimpleCallSettings getIamPolicySettings() { + return getIamPolicySettings; + } + + /** + * Returns the object with the settings used for calls to testIamPermissions. + */ + public SimpleCallSettings + testIamPermissionsSettings() { + return testIamPermissionsSettings; + } + /** * Returns the default service address. */ @@ -221,6 +253,9 @@ private PublisherSettings(Builder settingsBuilder) throws IOException { listTopicsSettings = settingsBuilder.listTopicsSettings().build(); listTopicSubscriptionsSettings = settingsBuilder.listTopicSubscriptionsSettings().build(); deleteTopicSettings = settingsBuilder.deleteTopicSettings().build(); + setIamPolicySettings = settingsBuilder.setIamPolicySettings().build(); + getIamPolicySettings = settingsBuilder.getIamPolicySettings().build(); + testIamPermissionsSettings = settingsBuilder.testIamPermissionsSettings().build(); } private static PageStreamingDescriptor @@ -353,6 +388,10 @@ public static class Builder extends ServiceApiSettings.Builder { ListTopicSubscriptionsRequest, ListTopicSubscriptionsResponse, String> listTopicSubscriptionsSettings; private SimpleCallSettings.Builder deleteTopicSettings; + private SimpleCallSettings.Builder setIamPolicySettings; + private SimpleCallSettings.Builder getIamPolicySettings; + private SimpleCallSettings.Builder + testIamPermissionsSettings; private static final ImmutableMap> RETRYABLE_CODE_DEFINITIONS; @@ -422,6 +461,13 @@ private Builder() { deleteTopicSettings = SimpleCallSettings.newBuilder(PublisherGrpc.METHOD_DELETE_TOPIC); + setIamPolicySettings = SimpleCallSettings.newBuilder(IAMPolicyGrpc.METHOD_SET_IAM_POLICY); + + getIamPolicySettings = SimpleCallSettings.newBuilder(IAMPolicyGrpc.METHOD_GET_IAM_POLICY); + + testIamPermissionsSettings = + SimpleCallSettings.newBuilder(IAMPolicyGrpc.METHOD_TEST_IAM_PERMISSIONS); + methodSettingsBuilders = ImmutableList.of( createTopicSettings, @@ -429,7 +475,10 @@ private Builder() { getTopicSettings, listTopicsSettings, listTopicSubscriptionsSettings, - deleteTopicSettings); + deleteTopicSettings, + setIamPolicySettings, + getIamPolicySettings, + testIamPermissionsSettings); } private static Builder createDefault() { @@ -474,6 +523,21 @@ private static Builder createDefault() { .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + builder + .setIamPolicySettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .getIamPolicySettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .testIamPermissionsSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + return builder; } @@ -486,6 +550,9 @@ private Builder(PublisherSettings settings) { listTopicsSettings = settings.listTopicsSettings.toBuilder(); listTopicSubscriptionsSettings = settings.listTopicSubscriptionsSettings.toBuilder(); deleteTopicSettings = settings.deleteTopicSettings.toBuilder(); + setIamPolicySettings = settings.setIamPolicySettings.toBuilder(); + getIamPolicySettings = settings.getIamPolicySettings.toBuilder(); + testIamPermissionsSettings = settings.testIamPermissionsSettings.toBuilder(); methodSettingsBuilders = ImmutableList.of( @@ -494,7 +561,10 @@ private Builder(PublisherSettings settings) { getTopicSettings, listTopicsSettings, listTopicSubscriptionsSettings, - deleteTopicSettings); + deleteTopicSettings, + setIamPolicySettings, + getIamPolicySettings, + testIamPermissionsSettings); } @Override @@ -599,6 +669,28 @@ public SimpleCallSettings.Builder deleteTopicSettings return deleteTopicSettings; } + /** + * Returns the builder for the settings used for calls to setIamPolicy. + */ + public SimpleCallSettings.Builder setIamPolicySettings() { + return setIamPolicySettings; + } + + /** + * Returns the builder for the settings used for calls to getIamPolicy. + */ + public SimpleCallSettings.Builder getIamPolicySettings() { + return getIamPolicySettings; + } + + /** + * Returns the builder for the settings used for calls to testIamPermissions. + */ + public SimpleCallSettings.Builder + testIamPermissionsSettings() { + return testIamPermissionsSettings; + } + @Override public PublisherSettings build() throws IOException { return new PublisherSettings(this); diff --git a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/SubscriberApi.java b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/SubscriberApi.java index 3952b74a3b73..8e2c7258b954 100644 --- a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/SubscriberApi.java +++ b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/SubscriberApi.java @@ -16,6 +16,11 @@ import com.google.api.gax.core.PageAccessor; import com.google.api.gax.grpc.ApiCallable; import com.google.api.gax.protobuf.PathTemplate; +import com.google.iam.v1.GetIamPolicyRequest; +import com.google.iam.v1.Policy; +import com.google.iam.v1.SetIamPolicyRequest; +import com.google.iam.v1.TestIamPermissionsRequest; +import com.google.iam.v1.TestIamPermissionsResponse; import com.google.protobuf.Empty; import com.google.pubsub.v1.AcknowledgeRequest; import com.google.pubsub.v1.DeleteSubscriptionRequest; @@ -109,6 +114,10 @@ public class SubscriberApi implements AutoCloseable { private final ApiCallable acknowledgeCallable; private final ApiCallable pullCallable; private final ApiCallable modifyPushConfigCallable; + private final ApiCallable setIamPolicyCallable; + private final ApiCallable getIamPolicyCallable; + private final ApiCallable + testIamPermissionsCallable; public final SubscriberSettings getSettings() { return settings; @@ -235,6 +244,12 @@ protected SubscriberApi(SubscriberSettings settings) throws IOException { this.pullCallable = ApiCallable.create(settings.pullSettings(), this.channel, this.executor); this.modifyPushConfigCallable = ApiCallable.create(settings.modifyPushConfigSettings(), this.channel, this.executor); + this.setIamPolicyCallable = + ApiCallable.create(settings.setIamPolicySettings(), this.channel, this.executor); + this.getIamPolicyCallable = + ApiCallable.create(settings.getIamPolicySettings(), this.channel, this.executor); + this.testIamPermissionsCallable = + ApiCallable.create(settings.testIamPermissionsSettings(), this.channel, this.executor); if (settings.getChannelProvider().shouldAutoClose()) { closeables.add( @@ -994,6 +1009,230 @@ public final ApiCallable modifyPushConfigCallabl return modifyPushConfigCallable; } + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Sets the access control policy on the specified resource. Replaces any + * existing policy. + * + * Sample code: + *

+   * try (SubscriberApi subscriberApi = SubscriberApi.create()) {
+   *   String formattedResource = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
+   *   Policy policy = Policy.newBuilder().build();
+   *   Policy response = subscriberApi.setIamPolicy(formattedResource, policy);
+   * }
+   * 
+ * + * @param resource REQUIRED: The resource for which policy is being specified. + * Resource is usually specified as a path, such as, + * projects/{project}/zones/{zone}/disks/{disk}. + * @param policy REQUIRED: The complete policy to be applied to the 'resource'. The size of + * the policy is limited to a few 10s of KB. An empty policy is in general a + * valid policy but certain services (like Projects) might reject them. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final Policy setIamPolicy(String resource, Policy policy) { + SUBSCRIPTION_PATH_TEMPLATE.validate(resource, "setIamPolicy"); + SetIamPolicyRequest request = + SetIamPolicyRequest.newBuilder().setResource(resource).setPolicy(policy).build(); + return setIamPolicy(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Sets the access control policy on the specified resource. Replaces any + * existing policy. + * + * Sample code: + *

+   * try (SubscriberApi subscriberApi = SubscriberApi.create()) {
+   *   String formattedResource = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
+   *   Policy policy = Policy.newBuilder().build();
+   *   SetIamPolicyRequest request = SetIamPolicyRequest.newBuilder()
+   *     .setResource(formattedResource)
+   *     .setPolicy(policy)
+   *     .build();
+   *   Policy response = subscriberApi.setIamPolicy(request);
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final Policy setIamPolicy(SetIamPolicyRequest request) { + return setIamPolicyCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Sets the access control policy on the specified resource. Replaces any + * existing policy. + * + * Sample code: + *

+   * try (SubscriberApi subscriberApi = SubscriberApi.create()) {
+   *   String formattedResource = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
+   *   Policy policy = Policy.newBuilder().build();
+   *   SetIamPolicyRequest request = SetIamPolicyRequest.newBuilder()
+   *     .setResource(formattedResource)
+   *     .setPolicy(policy)
+   *     .build();
+   *   ListenableFuture<Policy> future = subscriberApi.setIamPolicyCallable().futureCall(request);
+   *   // Do something
+   *   Policy response = future.get();
+   * }
+   * 
+ */ + public final ApiCallable setIamPolicyCallable() { + return setIamPolicyCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Gets the access control policy for a resource. Is empty if the + * policy or the resource does not exist. + * + * Sample code: + *

+   * try (SubscriberApi subscriberApi = SubscriberApi.create()) {
+   *   String formattedResource = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
+   *   Policy response = subscriberApi.getIamPolicy(formattedResource);
+   * }
+   * 
+ * + * @param resource REQUIRED: The resource for which policy is being requested. Resource + * is usually specified as a path, such as, projects/{project}. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final Policy getIamPolicy(String resource) { + SUBSCRIPTION_PATH_TEMPLATE.validate(resource, "getIamPolicy"); + GetIamPolicyRequest request = GetIamPolicyRequest.newBuilder().setResource(resource).build(); + return getIamPolicy(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Gets the access control policy for a resource. Is empty if the + * policy or the resource does not exist. + * + * Sample code: + *

+   * try (SubscriberApi subscriberApi = SubscriberApi.create()) {
+   *   String formattedResource = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
+   *   GetIamPolicyRequest request = GetIamPolicyRequest.newBuilder()
+   *     .setResource(formattedResource)
+   *     .build();
+   *   Policy response = subscriberApi.getIamPolicy(request);
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + private final Policy getIamPolicy(GetIamPolicyRequest request) { + return getIamPolicyCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Gets the access control policy for a resource. Is empty if the + * policy or the resource does not exist. + * + * Sample code: + *

+   * try (SubscriberApi subscriberApi = SubscriberApi.create()) {
+   *   String formattedResource = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
+   *   GetIamPolicyRequest request = GetIamPolicyRequest.newBuilder()
+   *     .setResource(formattedResource)
+   *     .build();
+   *   ListenableFuture<Policy> future = subscriberApi.getIamPolicyCallable().futureCall(request);
+   *   // Do something
+   *   Policy response = future.get();
+   * }
+   * 
+ */ + public final ApiCallable getIamPolicyCallable() { + return getIamPolicyCallable; + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Returns permissions that a caller has on the specified resource. + * + * Sample code: + *

+   * try (SubscriberApi subscriberApi = SubscriberApi.create()) {
+   *   String formattedResource = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
+   *   List<String> permissions = new ArrayList<>();
+   *   TestIamPermissionsResponse response = subscriberApi.testIamPermissions(formattedResource, permissions);
+   * }
+   * 
+ * + * @param resource REQUIRED: The resource for which policy detail is being requested. + * Resource is usually specified as a path, such as, projects/{project}. + * @param permissions The set of permissions to check for the 'resource'. Permissions with + * wildcards (such as '*' or 'storage.*') are not allowed. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final TestIamPermissionsResponse testIamPermissions( + String resource, List permissions) { + SUBSCRIPTION_PATH_TEMPLATE.validate(resource, "testIamPermissions"); + TestIamPermissionsRequest request = + TestIamPermissionsRequest.newBuilder() + .setResource(resource) + .addAllPermissions(permissions) + .build(); + return testIamPermissions(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Returns permissions that a caller has on the specified resource. + * + * Sample code: + *

+   * try (SubscriberApi subscriberApi = SubscriberApi.create()) {
+   *   String formattedResource = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
+   *   List<String> permissions = new ArrayList<>();
+   *   TestIamPermissionsRequest request = TestIamPermissionsRequest.newBuilder()
+   *     .setResource(formattedResource)
+   *     .addAllPermissions(permissions)
+   *     .build();
+   *   TestIamPermissionsResponse response = subscriberApi.testIamPermissions(request);
+   * }
+   * 
+ * + * @param request The request object containing all of the parameters for the API call. + * @throws com.google.api.gax.grpc.ApiException if the remote call fails + */ + public final TestIamPermissionsResponse testIamPermissions(TestIamPermissionsRequest request) { + return testIamPermissionsCallable().call(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Returns permissions that a caller has on the specified resource. + * + * Sample code: + *

+   * try (SubscriberApi subscriberApi = SubscriberApi.create()) {
+   *   String formattedResource = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]");
+   *   List<String> permissions = new ArrayList<>();
+   *   TestIamPermissionsRequest request = TestIamPermissionsRequest.newBuilder()
+   *     .setResource(formattedResource)
+   *     .addAllPermissions(permissions)
+   *     .build();
+   *   ListenableFuture<TestIamPermissionsResponse> future = subscriberApi.testIamPermissionsCallable().futureCall(request);
+   *   // Do something
+   *   TestIamPermissionsResponse response = future.get();
+   * }
+   * 
+ */ + public final ApiCallable + testIamPermissionsCallable() { + return testIamPermissionsCallable; + } + /** * Initiates an orderly shutdown in which preexisting calls continue but new calls are immediately * cancelled. diff --git a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/SubscriberSettings.java b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/SubscriberSettings.java index 1804008a6474..803e4e9da3e2 100644 --- a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/SubscriberSettings.java +++ b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/SubscriberSettings.java @@ -26,6 +26,12 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Lists; import com.google.common.collect.Sets; +import com.google.iam.v1.GetIamPolicyRequest; +import com.google.iam.v1.IAMPolicyGrpc; +import com.google.iam.v1.Policy; +import com.google.iam.v1.SetIamPolicyRequest; +import com.google.iam.v1.TestIamPermissionsRequest; +import com.google.iam.v1.TestIamPermissionsResponse; import com.google.protobuf.Empty; import com.google.pubsub.v1.AcknowledgeRequest; import com.google.pubsub.v1.DeleteSubscriptionRequest; @@ -113,6 +119,10 @@ public class SubscriberSettings extends ServiceApiSettings { private final SimpleCallSettings acknowledgeSettings; private final SimpleCallSettings pullSettings; private final SimpleCallSettings modifyPushConfigSettings; + private final SimpleCallSettings setIamPolicySettings; + private final SimpleCallSettings getIamPolicySettings; + private final SimpleCallSettings + testIamPermissionsSettings; /** * Returns the object with the settings used for calls to createSubscription. @@ -172,6 +182,28 @@ public SimpleCallSettings modifyPushConfigSettin return modifyPushConfigSettings; } + /** + * Returns the object with the settings used for calls to setIamPolicy. + */ + public SimpleCallSettings setIamPolicySettings() { + return setIamPolicySettings; + } + + /** + * Returns the object with the settings used for calls to getIamPolicy. + */ + public SimpleCallSettings getIamPolicySettings() { + return getIamPolicySettings; + } + + /** + * Returns the object with the settings used for calls to testIamPermissions. + */ + public SimpleCallSettings + testIamPermissionsSettings() { + return testIamPermissionsSettings; + } + /** * Returns the default service address. */ @@ -231,6 +263,9 @@ private SubscriberSettings(Builder settingsBuilder) throws IOException { acknowledgeSettings = settingsBuilder.acknowledgeSettings().build(); pullSettings = settingsBuilder.pullSettings().build(); modifyPushConfigSettings = settingsBuilder.modifyPushConfigSettings().build(); + setIamPolicySettings = settingsBuilder.setIamPolicySettings().build(); + getIamPolicySettings = settingsBuilder.getIamPolicySettings().build(); + testIamPermissionsSettings = settingsBuilder.testIamPermissionsSettings().build(); } private static PageStreamingDescriptor< @@ -279,6 +314,10 @@ public static class Builder extends ServiceApiSettings.Builder { private SimpleCallSettings.Builder acknowledgeSettings; private SimpleCallSettings.Builder pullSettings; private SimpleCallSettings.Builder modifyPushConfigSettings; + private SimpleCallSettings.Builder setIamPolicySettings; + private SimpleCallSettings.Builder getIamPolicySettings; + private SimpleCallSettings.Builder + testIamPermissionsSettings; private static final ImmutableMap> RETRYABLE_CODE_DEFINITIONS; @@ -347,6 +386,13 @@ private Builder() { modifyPushConfigSettings = SimpleCallSettings.newBuilder(SubscriberGrpc.METHOD_MODIFY_PUSH_CONFIG); + setIamPolicySettings = SimpleCallSettings.newBuilder(IAMPolicyGrpc.METHOD_SET_IAM_POLICY); + + getIamPolicySettings = SimpleCallSettings.newBuilder(IAMPolicyGrpc.METHOD_GET_IAM_POLICY); + + testIamPermissionsSettings = + SimpleCallSettings.newBuilder(IAMPolicyGrpc.METHOD_TEST_IAM_PERMISSIONS); + methodSettingsBuilders = ImmutableList.of( createSubscriptionSettings, @@ -356,7 +402,10 @@ private Builder() { modifyAckDeadlineSettings, acknowledgeSettings, pullSettings, - modifyPushConfigSettings); + modifyPushConfigSettings, + setIamPolicySettings, + getIamPolicySettings, + testIamPermissionsSettings); } private static Builder createDefault() { @@ -402,6 +451,21 @@ private static Builder createDefault() { .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + builder + .setIamPolicySettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .getIamPolicySettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + + builder + .testIamPermissionsSettings() + .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("non_idempotent")) + .setRetrySettingsBuilder(RETRY_PARAM_DEFINITIONS.get("default")); + return builder; } @@ -416,6 +480,9 @@ private Builder(SubscriberSettings settings) { acknowledgeSettings = settings.acknowledgeSettings.toBuilder(); pullSettings = settings.pullSettings.toBuilder(); modifyPushConfigSettings = settings.modifyPushConfigSettings.toBuilder(); + setIamPolicySettings = settings.setIamPolicySettings.toBuilder(); + getIamPolicySettings = settings.getIamPolicySettings.toBuilder(); + testIamPermissionsSettings = settings.testIamPermissionsSettings.toBuilder(); methodSettingsBuilders = ImmutableList.of( @@ -426,7 +493,10 @@ private Builder(SubscriberSettings settings) { modifyAckDeadlineSettings, acknowledgeSettings, pullSettings, - modifyPushConfigSettings); + modifyPushConfigSettings, + setIamPolicySettings, + getIamPolicySettings, + testIamPermissionsSettings); } @Override @@ -546,6 +616,28 @@ public SimpleCallSettings.Builder modifyPushConf return modifyPushConfigSettings; } + /** + * Returns the builder for the settings used for calls to setIamPolicy. + */ + public SimpleCallSettings.Builder setIamPolicySettings() { + return setIamPolicySettings; + } + + /** + * Returns the builder for the settings used for calls to getIamPolicy. + */ + public SimpleCallSettings.Builder getIamPolicySettings() { + return getIamPolicySettings; + } + + /** + * Returns the builder for the settings used for calls to testIamPermissions. + */ + public SimpleCallSettings.Builder + testIamPermissionsSettings() { + return testIamPermissionsSettings; + } + @Override public SubscriberSettings build() throws IOException { return new SubscriberSettings(this); diff --git a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/package-info.java b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/package-info.java index 66324daa0162..1bac8a3590f9 100644 --- a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/package-info.java +++ b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/spi/v1/package-info.java @@ -17,6 +17,23 @@ * * The interfaces provided are listed below, along with a usage sample * + * ============ + * PublisherApi + * ============ + * + * Service Description: The service that an application uses to manipulate topics, and to send + * messages to a topic. + * + * Sample for PublisherApi: + *
+ * 
+ * try (PublisherApi publisherApi = PublisherApi.create()) {
+ *   String formattedName = PublisherApi.formatTopicName("[PROJECT]", "[TOPIC]");
+ *   Topic response = publisherApi.createTopic(formattedName);
+ * }
+ * 
+ * 
+ * * ============= * SubscriberApi * ============= @@ -37,22 +54,5 @@ * * * - * ============ - * PublisherApi - * ============ - * - * Service Description: The service that an application uses to manipulate topics, and to send - * messages to a topic. - * - * Sample for PublisherApi: - *
- * 
- * try (PublisherApi publisherApi = PublisherApi.create()) {
- *   String formattedName = PublisherApi.formatTopicName("[PROJECT]", "[TOPIC]");
- *   Topic response = publisherApi.createTopic(formattedName);
- * }
- * 
- * 
- * */ package com.google.cloud.pubsub.spi.v1; diff --git a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/MockIAMPolicy.java b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/MockIAMPolicy.java new file mode 100644 index 000000000000..be1117e47043 --- /dev/null +++ b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/MockIAMPolicy.java @@ -0,0 +1,50 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.cloud.pubsub.spi.v1; + +import com.google.api.gax.testing.MockGrpcService; +import com.google.iam.v1.IAMPolicyGrpc; +import com.google.protobuf.GeneratedMessage; +import io.grpc.ServerServiceDefinition; +import java.util.List; + +@javax.annotation.Generated("by GAPIC") +public class MockIAMPolicy implements MockGrpcService { + private MockIAMPolicyImpl serviceImpl; + + public MockIAMPolicy() { + serviceImpl = new MockIAMPolicyImpl(); + } + + @Override + public List getRequests() { + return serviceImpl.getRequests(); + } + + @Override + public void setResponses(List responses) { + serviceImpl.setResponses(responses); + } + + @Override + public ServerServiceDefinition getServiceDefinition() { + return IAMPolicyGrpc.bindService(serviceImpl); + } + + @Override + public void reset() { + serviceImpl.reset(); + } +} diff --git a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/MockIAMPolicyImpl.java b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/MockIAMPolicyImpl.java new file mode 100644 index 000000000000..224bdfcc5f7a --- /dev/null +++ b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/MockIAMPolicyImpl.java @@ -0,0 +1,79 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.cloud.pubsub.spi.v1; + +import com.google.common.collect.Lists; +import com.google.iam.v1.GetIamPolicyRequest; +import com.google.iam.v1.IAMPolicyGrpc.IAMPolicy; +import com.google.iam.v1.Policy; +import com.google.iam.v1.SetIamPolicyRequest; +import com.google.iam.v1.TestIamPermissionsRequest; +import com.google.iam.v1.TestIamPermissionsResponse; +import com.google.protobuf.GeneratedMessage; +import io.grpc.stub.StreamObserver; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +@javax.annotation.Generated("by GAPIC") +public class MockIAMPolicyImpl implements IAMPolicy { + private ArrayList requests; + private Queue responses; + + public MockIAMPolicyImpl() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } + + public List getRequests() { + return requests; + } + + public void setResponses(List responses) { + this.responses = Lists.newLinkedList(responses); + } + + public void reset() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } + + @Override + public void setIamPolicy(SetIamPolicyRequest request, StreamObserver responseObserver) { + Policy response = (Policy) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void getIamPolicy(GetIamPolicyRequest request, StreamObserver responseObserver) { + Policy response = (Policy) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void testIamPermissions( + TestIamPermissionsRequest request, + StreamObserver responseObserver) { + TestIamPermissionsResponse response = (TestIamPermissionsResponse) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } +} diff --git a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/MockPublisher.java b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/MockPublisher.java new file mode 100644 index 000000000000..b33087c4e803 --- /dev/null +++ b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/MockPublisher.java @@ -0,0 +1,50 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.cloud.pubsub.spi.v1; + +import com.google.api.gax.testing.MockGrpcService; +import com.google.protobuf.GeneratedMessage; +import com.google.pubsub.v1.PublisherGrpc; +import io.grpc.ServerServiceDefinition; +import java.util.List; + +@javax.annotation.Generated("by GAPIC") +public class MockPublisher implements MockGrpcService { + private MockPublisherImpl serviceImpl; + + public MockPublisher() { + serviceImpl = new MockPublisherImpl(); + } + + @Override + public List getRequests() { + return serviceImpl.getRequests(); + } + + @Override + public void setResponses(List responses) { + serviceImpl.setResponses(responses); + } + + @Override + public ServerServiceDefinition getServiceDefinition() { + return PublisherGrpc.bindService(serviceImpl); + } + + @Override + public void reset() { + serviceImpl.reset(); + } +} diff --git a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/MockPublisherImpl.java b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/MockPublisherImpl.java new file mode 100644 index 000000000000..da815f23426f --- /dev/null +++ b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/MockPublisherImpl.java @@ -0,0 +1,109 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.cloud.pubsub.spi.v1; + +import com.google.common.collect.Lists; +import com.google.protobuf.Empty; +import com.google.protobuf.GeneratedMessage; +import com.google.pubsub.v1.DeleteTopicRequest; +import com.google.pubsub.v1.GetTopicRequest; +import com.google.pubsub.v1.ListTopicSubscriptionsRequest; +import com.google.pubsub.v1.ListTopicSubscriptionsResponse; +import com.google.pubsub.v1.ListTopicsRequest; +import com.google.pubsub.v1.ListTopicsResponse; +import com.google.pubsub.v1.PublishRequest; +import com.google.pubsub.v1.PublishResponse; +import com.google.pubsub.v1.PublisherGrpc.Publisher; +import com.google.pubsub.v1.Topic; +import io.grpc.stub.StreamObserver; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +@javax.annotation.Generated("by GAPIC") +public class MockPublisherImpl implements Publisher { + private ArrayList requests; + private Queue responses; + + public MockPublisherImpl() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } + + public List getRequests() { + return requests; + } + + public void setResponses(List responses) { + this.responses = Lists.newLinkedList(responses); + } + + public void reset() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } + + @Override + public void createTopic(Topic request, StreamObserver responseObserver) { + Topic response = (Topic) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void publish(PublishRequest request, StreamObserver responseObserver) { + PublishResponse response = (PublishResponse) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void getTopic(GetTopicRequest request, StreamObserver responseObserver) { + Topic response = (Topic) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void listTopics( + ListTopicsRequest request, StreamObserver responseObserver) { + ListTopicsResponse response = (ListTopicsResponse) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void listTopicSubscriptions( + ListTopicSubscriptionsRequest request, + StreamObserver responseObserver) { + ListTopicSubscriptionsResponse response = (ListTopicSubscriptionsResponse) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void deleteTopic(DeleteTopicRequest request, StreamObserver responseObserver) { + Empty response = (Empty) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } +} diff --git a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/MockSubscriber.java b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/MockSubscriber.java new file mode 100644 index 000000000000..906834f3ac95 --- /dev/null +++ b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/MockSubscriber.java @@ -0,0 +1,50 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.cloud.pubsub.spi.v1; + +import com.google.api.gax.testing.MockGrpcService; +import com.google.protobuf.GeneratedMessage; +import com.google.pubsub.v1.SubscriberGrpc; +import io.grpc.ServerServiceDefinition; +import java.util.List; + +@javax.annotation.Generated("by GAPIC") +public class MockSubscriber implements MockGrpcService { + private MockSubscriberImpl serviceImpl; + + public MockSubscriber() { + serviceImpl = new MockSubscriberImpl(); + } + + @Override + public List getRequests() { + return serviceImpl.getRequests(); + } + + @Override + public void setResponses(List responses) { + serviceImpl.setResponses(responses); + } + + @Override + public ServerServiceDefinition getServiceDefinition() { + return SubscriberGrpc.bindService(serviceImpl); + } + + @Override + public void reset() { + serviceImpl.reset(); + } +} diff --git a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/MockSubscriberImpl.java b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/MockSubscriberImpl.java new file mode 100644 index 000000000000..7674ece2c2f4 --- /dev/null +++ b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/MockSubscriberImpl.java @@ -0,0 +1,130 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.cloud.pubsub.spi.v1; + +import com.google.common.collect.Lists; +import com.google.protobuf.Empty; +import com.google.protobuf.GeneratedMessage; +import com.google.pubsub.v1.AcknowledgeRequest; +import com.google.pubsub.v1.DeleteSubscriptionRequest; +import com.google.pubsub.v1.GetSubscriptionRequest; +import com.google.pubsub.v1.ListSubscriptionsRequest; +import com.google.pubsub.v1.ListSubscriptionsResponse; +import com.google.pubsub.v1.ModifyAckDeadlineRequest; +import com.google.pubsub.v1.ModifyPushConfigRequest; +import com.google.pubsub.v1.PullRequest; +import com.google.pubsub.v1.PullResponse; +import com.google.pubsub.v1.SubscriberGrpc.Subscriber; +import com.google.pubsub.v1.Subscription; +import io.grpc.stub.StreamObserver; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +@javax.annotation.Generated("by GAPIC") +public class MockSubscriberImpl implements Subscriber { + private ArrayList requests; + private Queue responses; + + public MockSubscriberImpl() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } + + public List getRequests() { + return requests; + } + + public void setResponses(List responses) { + this.responses = Lists.newLinkedList(responses); + } + + public void reset() { + requests = new ArrayList<>(); + responses = new LinkedList<>(); + } + + @Override + public void createSubscription( + Subscription request, StreamObserver responseObserver) { + Subscription response = (Subscription) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void getSubscription( + GetSubscriptionRequest request, StreamObserver responseObserver) { + Subscription response = (Subscription) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void listSubscriptions( + ListSubscriptionsRequest request, + StreamObserver responseObserver) { + ListSubscriptionsResponse response = (ListSubscriptionsResponse) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void deleteSubscription( + DeleteSubscriptionRequest request, StreamObserver responseObserver) { + Empty response = (Empty) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void modifyAckDeadline( + ModifyAckDeadlineRequest request, StreamObserver responseObserver) { + Empty response = (Empty) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void acknowledge(AcknowledgeRequest request, StreamObserver responseObserver) { + Empty response = (Empty) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void pull(PullRequest request, StreamObserver responseObserver) { + PullResponse response = (PullResponse) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + + @Override + public void modifyPushConfig( + ModifyPushConfigRequest request, StreamObserver responseObserver) { + Empty response = (Empty) responses.remove(); + requests.add(request); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } +} diff --git a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/PublisherTest.java b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/PublisherTest.java new file mode 100644 index 000000000000..18104e858939 --- /dev/null +++ b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/PublisherTest.java @@ -0,0 +1,313 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.cloud.pubsub.spi.v1; + +import com.google.api.gax.core.PageAccessor; +import com.google.api.gax.testing.MockGrpcService; +import com.google.api.gax.testing.MockServiceHelper; +import com.google.common.collect.Lists; +import com.google.iam.v1.GetIamPolicyRequest; +import com.google.iam.v1.Policy; +import com.google.iam.v1.SetIamPolicyRequest; +import com.google.iam.v1.TestIamPermissionsRequest; +import com.google.iam.v1.TestIamPermissionsResponse; +import com.google.protobuf.ByteString; +import com.google.protobuf.Empty; +import com.google.protobuf.GeneratedMessage; +import com.google.pubsub.v1.DeleteTopicRequest; +import com.google.pubsub.v1.GetTopicRequest; +import com.google.pubsub.v1.ListTopicSubscriptionsRequest; +import com.google.pubsub.v1.ListTopicSubscriptionsResponse; +import com.google.pubsub.v1.ListTopicsRequest; +import com.google.pubsub.v1.ListTopicsResponse; +import com.google.pubsub.v1.PublishRequest; +import com.google.pubsub.v1.PublishResponse; +import com.google.pubsub.v1.PubsubMessage; +import com.google.pubsub.v1.Topic; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +@javax.annotation.Generated("by GAPIC") +public class PublisherTest { + private static MockPublisher mockPublisher; + private static MockIAMPolicy mockIAMPolicy; + private static MockSubscriber mockSubscriber; + private static MockServiceHelper serviceHelper; + private PublisherApi api; + + @BeforeClass + public static void startStaticServer() { + mockPublisher = new MockPublisher(); + mockIAMPolicy = new MockIAMPolicy(); + mockSubscriber = new MockSubscriber(); + serviceHelper = + new MockServiceHelper( + "in-process-1", + Arrays.asList(mockPublisher, mockIAMPolicy, mockSubscriber)); + serviceHelper.start(); + } + + @AfterClass + public static void stopServer() { + serviceHelper.stop(); + } + + @Before + public void setUp() throws IOException { + serviceHelper.reset(); + PublisherSettings settings = + PublisherSettings.defaultBuilder() + .provideChannelWith(serviceHelper.createChannel(), true) + .build(); + api = PublisherApi.create(settings); + } + + @After + public void tearDown() throws Exception { + api.close(); + } + + @Test + @SuppressWarnings("all") + public void createTopicTest() { + String formattedName2 = PublisherApi.formatTopicName("[PROJECT]", "[TOPIC]"); + Topic expectedResponse = Topic.newBuilder().setName(formattedName2).build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockPublisher.setResponses(expectedResponses); + + String formattedName = PublisherApi.formatTopicName("[PROJECT]", "[TOPIC]"); + + Topic actualResponse = api.createTopic(formattedName); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockPublisher.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + Topic actualRequest = (Topic) actualRequests.get(0); + + Assert.assertEquals(formattedName, actualRequest.getName()); + } + + @Test + @SuppressWarnings("all") + public void publishTest() { + String messageIdsElement = "messageIdsElement-744837059"; + List messageIds = Arrays.asList(messageIdsElement); + PublishResponse expectedResponse = + PublishResponse.newBuilder().addAllMessageIds(messageIds).build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockPublisher.setResponses(expectedResponses); + + String formattedTopic = PublisherApi.formatTopicName("[PROJECT]", "[TOPIC]"); + ByteString data = ByteString.copyFromUtf8("-86"); + PubsubMessage messagesElement = PubsubMessage.newBuilder().setData(data).build(); + List messages = Arrays.asList(messagesElement); + + PublishResponse actualResponse = api.publish(formattedTopic, messages); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockPublisher.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + PublishRequest actualRequest = (PublishRequest) actualRequests.get(0); + + Assert.assertEquals(formattedTopic, actualRequest.getTopic()); + Assert.assertEquals(messages, actualRequest.getMessagesList()); + } + + @Test + @SuppressWarnings("all") + public void getTopicTest() { + String name = "name3373707"; + Topic expectedResponse = Topic.newBuilder().setName(name).build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockPublisher.setResponses(expectedResponses); + + String formattedTopic = PublisherApi.formatTopicName("[PROJECT]", "[TOPIC]"); + + Topic actualResponse = api.getTopic(formattedTopic); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockPublisher.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + GetTopicRequest actualRequest = (GetTopicRequest) actualRequests.get(0); + + Assert.assertEquals(formattedTopic, actualRequest.getTopic()); + } + + @Test + @SuppressWarnings("all") + public void listTopicsTest() { + Topic topicsElement = Topic.newBuilder().build(); + List topics = Arrays.asList(topicsElement); + String nextPageToken = "nextPageToken-1530815211"; + ListTopicsResponse expectedResponse = + ListTopicsResponse.newBuilder() + .addAllTopics(topics) + .setNextPageToken(nextPageToken) + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockPublisher.setResponses(expectedResponses); + + String formattedProject = PublisherApi.formatProjectName("[PROJECT]"); + + PageAccessor pageAccessor = api.listTopics(formattedProject); + + // PageAccessor will not make actual request until it is being used. + // Add all the pages here in order to make grpc requests. + List resources = Lists.newArrayList(pageAccessor.getPageValues()); + Assert.assertEquals(1, resources.size()); + Assert.assertEquals(expectedResponse.getTopicsList().get(0), resources.get(0)); + + List actualRequests = mockPublisher.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + ListTopicsRequest actualRequest = (ListTopicsRequest) actualRequests.get(0); + + Assert.assertEquals(formattedProject, actualRequest.getProject()); + } + + @Test + @SuppressWarnings("all") + public void listTopicSubscriptionsTest() { + String subscriptionsElement = "subscriptionsElement1698708147"; + List subscriptions = Arrays.asList(subscriptionsElement); + String nextPageToken = "nextPageToken-1530815211"; + ListTopicSubscriptionsResponse expectedResponse = + ListTopicSubscriptionsResponse.newBuilder() + .addAllSubscriptions(subscriptions) + .setNextPageToken(nextPageToken) + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockPublisher.setResponses(expectedResponses); + + String formattedTopic = PublisherApi.formatTopicName("[PROJECT]", "[TOPIC]"); + + PageAccessor pageAccessor = api.listTopicSubscriptions(formattedTopic); + + // PageAccessor will not make actual request until it is being used. + // Add all the pages here in order to make grpc requests. + List resources = Lists.newArrayList(pageAccessor.getPageValues()); + Assert.assertEquals(1, resources.size()); + Assert.assertEquals(expectedResponse.getSubscriptionsList().get(0), resources.get(0)); + + List actualRequests = mockPublisher.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + ListTopicSubscriptionsRequest actualRequest = + (ListTopicSubscriptionsRequest) actualRequests.get(0); + + Assert.assertEquals(formattedTopic, actualRequest.getTopic()); + } + + @Test + @SuppressWarnings("all") + public void deleteTopicTest() { + Empty expectedResponse = Empty.newBuilder().build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockPublisher.setResponses(expectedResponses); + + String formattedTopic = PublisherApi.formatTopicName("[PROJECT]", "[TOPIC]"); + + api.deleteTopic(formattedTopic); + + List actualRequests = mockPublisher.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + DeleteTopicRequest actualRequest = (DeleteTopicRequest) actualRequests.get(0); + + Assert.assertEquals(formattedTopic, actualRequest.getTopic()); + } + + @Test + @SuppressWarnings("all") + public void setIamPolicyTest() { + int version = 351608024; + ByteString etag = ByteString.copyFromUtf8("21"); + Policy expectedResponse = Policy.newBuilder().setVersion(version).setEtag(etag).build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockIAMPolicy.setResponses(expectedResponses); + + String formattedResource = PublisherApi.formatTopicName("[PROJECT]", "[TOPIC]"); + Policy policy = Policy.newBuilder().build(); + + Policy actualResponse = api.setIamPolicy(formattedResource, policy); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockIAMPolicy.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + SetIamPolicyRequest actualRequest = (SetIamPolicyRequest) actualRequests.get(0); + + Assert.assertEquals(formattedResource, actualRequest.getResource()); + Assert.assertEquals(policy, actualRequest.getPolicy()); + } + + @Test + @SuppressWarnings("all") + public void getIamPolicyTest() { + int version = 351608024; + ByteString etag = ByteString.copyFromUtf8("21"); + Policy expectedResponse = Policy.newBuilder().setVersion(version).setEtag(etag).build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockIAMPolicy.setResponses(expectedResponses); + + String formattedResource = PublisherApi.formatTopicName("[PROJECT]", "[TOPIC]"); + + Policy actualResponse = api.getIamPolicy(formattedResource); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockIAMPolicy.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + GetIamPolicyRequest actualRequest = (GetIamPolicyRequest) actualRequests.get(0); + + Assert.assertEquals(formattedResource, actualRequest.getResource()); + } + + @Test + @SuppressWarnings("all") + public void testIamPermissionsTest() { + List permissions2 = new ArrayList<>(); + TestIamPermissionsResponse expectedResponse = + TestIamPermissionsResponse.newBuilder().addAllPermissions(permissions2).build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockIAMPolicy.setResponses(expectedResponses); + + String formattedResource = PublisherApi.formatTopicName("[PROJECT]", "[TOPIC]"); + List permissions = new ArrayList<>(); + + TestIamPermissionsResponse actualResponse = + api.testIamPermissions(formattedResource, permissions); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockIAMPolicy.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + TestIamPermissionsRequest actualRequest = (TestIamPermissionsRequest) actualRequests.get(0); + + Assert.assertEquals(formattedResource, actualRequest.getResource()); + Assert.assertEquals(permissions, actualRequest.getPermissionsList()); + } +} diff --git a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/SubscriberTest.java b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/SubscriberTest.java new file mode 100644 index 000000000000..210580ee0d2e --- /dev/null +++ b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/spi/v1/SubscriberTest.java @@ -0,0 +1,370 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.google.cloud.pubsub.spi.v1; + +import com.google.api.gax.core.PageAccessor; +import com.google.api.gax.testing.MockGrpcService; +import com.google.api.gax.testing.MockServiceHelper; +import com.google.common.collect.Lists; +import com.google.iam.v1.GetIamPolicyRequest; +import com.google.iam.v1.Policy; +import com.google.iam.v1.SetIamPolicyRequest; +import com.google.iam.v1.TestIamPermissionsRequest; +import com.google.iam.v1.TestIamPermissionsResponse; +import com.google.protobuf.ByteString; +import com.google.protobuf.Empty; +import com.google.protobuf.GeneratedMessage; +import com.google.pubsub.v1.AcknowledgeRequest; +import com.google.pubsub.v1.DeleteSubscriptionRequest; +import com.google.pubsub.v1.GetSubscriptionRequest; +import com.google.pubsub.v1.ListSubscriptionsRequest; +import com.google.pubsub.v1.ListSubscriptionsResponse; +import com.google.pubsub.v1.ModifyAckDeadlineRequest; +import com.google.pubsub.v1.ModifyPushConfigRequest; +import com.google.pubsub.v1.PullRequest; +import com.google.pubsub.v1.PullResponse; +import com.google.pubsub.v1.PushConfig; +import com.google.pubsub.v1.Subscription; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +@javax.annotation.Generated("by GAPIC") +public class SubscriberTest { + private static MockPublisher mockPublisher; + private static MockIAMPolicy mockIAMPolicy; + private static MockSubscriber mockSubscriber; + private static MockServiceHelper serviceHelper; + private SubscriberApi api; + + @BeforeClass + public static void startStaticServer() { + mockPublisher = new MockPublisher(); + mockIAMPolicy = new MockIAMPolicy(); + mockSubscriber = new MockSubscriber(); + serviceHelper = + new MockServiceHelper( + "in-process-1", + Arrays.asList(mockPublisher, mockIAMPolicy, mockSubscriber)); + serviceHelper.start(); + } + + @AfterClass + public static void stopServer() { + serviceHelper.stop(); + } + + @Before + public void setUp() throws IOException { + serviceHelper.reset(); + SubscriberSettings settings = + SubscriberSettings.defaultBuilder() + .provideChannelWith(serviceHelper.createChannel(), true) + .build(); + api = SubscriberApi.create(settings); + } + + @After + public void tearDown() throws Exception { + api.close(); + } + + @Test + @SuppressWarnings("all") + public void createSubscriptionTest() { + String formattedName2 = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]"); + String formattedTopic2 = SubscriberApi.formatTopicName("[PROJECT]", "[TOPIC]"); + int ackDeadlineSeconds2 = -921632575; + Subscription expectedResponse = + Subscription.newBuilder() + .setName(formattedName2) + .setTopic(formattedTopic2) + .setAckDeadlineSeconds(ackDeadlineSeconds2) + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockSubscriber.setResponses(expectedResponses); + + String formattedName = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]"); + String formattedTopic = SubscriberApi.formatTopicName("[PROJECT]", "[TOPIC]"); + PushConfig pushConfig = PushConfig.newBuilder().build(); + int ackDeadlineSeconds = 2135351438; + + Subscription actualResponse = + api.createSubscription(formattedName, formattedTopic, pushConfig, ackDeadlineSeconds); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockSubscriber.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + Subscription actualRequest = (Subscription) actualRequests.get(0); + + Assert.assertEquals(formattedName, actualRequest.getName()); + Assert.assertEquals(formattedTopic, actualRequest.getTopic()); + Assert.assertEquals(pushConfig, actualRequest.getPushConfig()); + Assert.assertEquals(ackDeadlineSeconds, actualRequest.getAckDeadlineSeconds()); + } + + @Test + @SuppressWarnings("all") + public void getSubscriptionTest() { + String name = "name3373707"; + String topic = "topic110546223"; + int ackDeadlineSeconds = 2135351438; + Subscription expectedResponse = + Subscription.newBuilder() + .setName(name) + .setTopic(topic) + .setAckDeadlineSeconds(ackDeadlineSeconds) + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockSubscriber.setResponses(expectedResponses); + + String formattedSubscription = + SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]"); + + Subscription actualResponse = api.getSubscription(formattedSubscription); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockSubscriber.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + GetSubscriptionRequest actualRequest = (GetSubscriptionRequest) actualRequests.get(0); + + Assert.assertEquals(formattedSubscription, actualRequest.getSubscription()); + } + + @Test + @SuppressWarnings("all") + public void listSubscriptionsTest() { + Subscription subscriptionsElement = Subscription.newBuilder().build(); + List subscriptions = Arrays.asList(subscriptionsElement); + String nextPageToken = "nextPageToken-1530815211"; + ListSubscriptionsResponse expectedResponse = + ListSubscriptionsResponse.newBuilder() + .addAllSubscriptions(subscriptions) + .setNextPageToken(nextPageToken) + .build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockSubscriber.setResponses(expectedResponses); + + String formattedProject = SubscriberApi.formatProjectName("[PROJECT]"); + + PageAccessor pageAccessor = api.listSubscriptions(formattedProject); + + // PageAccessor will not make actual request until it is being used. + // Add all the pages here in order to make grpc requests. + List resources = Lists.newArrayList(pageAccessor.getPageValues()); + Assert.assertEquals(1, resources.size()); + Assert.assertEquals(expectedResponse.getSubscriptionsList().get(0), resources.get(0)); + + List actualRequests = mockSubscriber.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + ListSubscriptionsRequest actualRequest = (ListSubscriptionsRequest) actualRequests.get(0); + + Assert.assertEquals(formattedProject, actualRequest.getProject()); + } + + @Test + @SuppressWarnings("all") + public void deleteSubscriptionTest() { + Empty expectedResponse = Empty.newBuilder().build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockSubscriber.setResponses(expectedResponses); + + String formattedSubscription = + SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]"); + + api.deleteSubscription(formattedSubscription); + + List actualRequests = mockSubscriber.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + DeleteSubscriptionRequest actualRequest = (DeleteSubscriptionRequest) actualRequests.get(0); + + Assert.assertEquals(formattedSubscription, actualRequest.getSubscription()); + } + + @Test + @SuppressWarnings("all") + public void modifyAckDeadlineTest() { + Empty expectedResponse = Empty.newBuilder().build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockSubscriber.setResponses(expectedResponses); + + String formattedSubscription = + SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]"); + List ackIds = new ArrayList<>(); + int ackDeadlineSeconds = 2135351438; + + api.modifyAckDeadline(formattedSubscription, ackIds, ackDeadlineSeconds); + + List actualRequests = mockSubscriber.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + ModifyAckDeadlineRequest actualRequest = (ModifyAckDeadlineRequest) actualRequests.get(0); + + Assert.assertEquals(formattedSubscription, actualRequest.getSubscription()); + Assert.assertEquals(ackIds, actualRequest.getAckIdsList()); + Assert.assertEquals(ackDeadlineSeconds, actualRequest.getAckDeadlineSeconds()); + } + + @Test + @SuppressWarnings("all") + public void acknowledgeTest() { + Empty expectedResponse = Empty.newBuilder().build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockSubscriber.setResponses(expectedResponses); + + String formattedSubscription = + SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]"); + List ackIds = new ArrayList<>(); + + api.acknowledge(formattedSubscription, ackIds); + + List actualRequests = mockSubscriber.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + AcknowledgeRequest actualRequest = (AcknowledgeRequest) actualRequests.get(0); + + Assert.assertEquals(formattedSubscription, actualRequest.getSubscription()); + Assert.assertEquals(ackIds, actualRequest.getAckIdsList()); + } + + @Test + @SuppressWarnings("all") + public void pullTest() { + PullResponse expectedResponse = PullResponse.newBuilder().build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockSubscriber.setResponses(expectedResponses); + + String formattedSubscription = + SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]"); + boolean returnImmediately = false; + int maxMessages = 496131527; + + PullResponse actualResponse = api.pull(formattedSubscription, returnImmediately, maxMessages); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockSubscriber.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + PullRequest actualRequest = (PullRequest) actualRequests.get(0); + + Assert.assertEquals(formattedSubscription, actualRequest.getSubscription()); + Assert.assertEquals(returnImmediately, actualRequest.getReturnImmediately()); + Assert.assertEquals(maxMessages, actualRequest.getMaxMessages()); + } + + @Test + @SuppressWarnings("all") + public void modifyPushConfigTest() { + Empty expectedResponse = Empty.newBuilder().build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockSubscriber.setResponses(expectedResponses); + + String formattedSubscription = + SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]"); + PushConfig pushConfig = PushConfig.newBuilder().build(); + + api.modifyPushConfig(formattedSubscription, pushConfig); + + List actualRequests = mockSubscriber.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + ModifyPushConfigRequest actualRequest = (ModifyPushConfigRequest) actualRequests.get(0); + + Assert.assertEquals(formattedSubscription, actualRequest.getSubscription()); + Assert.assertEquals(pushConfig, actualRequest.getPushConfig()); + } + + @Test + @SuppressWarnings("all") + public void setIamPolicyTest() { + int version = 351608024; + ByteString etag = ByteString.copyFromUtf8("21"); + Policy expectedResponse = Policy.newBuilder().setVersion(version).setEtag(etag).build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockIAMPolicy.setResponses(expectedResponses); + + String formattedResource = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]"); + Policy policy = Policy.newBuilder().build(); + + Policy actualResponse = api.setIamPolicy(formattedResource, policy); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockIAMPolicy.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + SetIamPolicyRequest actualRequest = (SetIamPolicyRequest) actualRequests.get(0); + + Assert.assertEquals(formattedResource, actualRequest.getResource()); + Assert.assertEquals(policy, actualRequest.getPolicy()); + } + + @Test + @SuppressWarnings("all") + public void getIamPolicyTest() { + int version = 351608024; + ByteString etag = ByteString.copyFromUtf8("21"); + Policy expectedResponse = Policy.newBuilder().setVersion(version).setEtag(etag).build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockIAMPolicy.setResponses(expectedResponses); + + String formattedResource = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]"); + + Policy actualResponse = api.getIamPolicy(formattedResource); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockIAMPolicy.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + GetIamPolicyRequest actualRequest = (GetIamPolicyRequest) actualRequests.get(0); + + Assert.assertEquals(formattedResource, actualRequest.getResource()); + } + + @Test + @SuppressWarnings("all") + public void testIamPermissionsTest() { + List permissions2 = new ArrayList<>(); + TestIamPermissionsResponse expectedResponse = + TestIamPermissionsResponse.newBuilder().addAllPermissions(permissions2).build(); + List expectedResponses = new ArrayList<>(); + expectedResponses.add(expectedResponse); + mockIAMPolicy.setResponses(expectedResponses); + + String formattedResource = SubscriberApi.formatSubscriptionName("[PROJECT]", "[SUBSCRIPTION]"); + List permissions = new ArrayList<>(); + + TestIamPermissionsResponse actualResponse = + api.testIamPermissions(formattedResource, permissions); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockIAMPolicy.getRequests(); + Assert.assertEquals(1, actualRequests.size()); + TestIamPermissionsRequest actualRequest = (TestIamPermissionsRequest) actualRequests.get(0); + + Assert.assertEquals(formattedResource, actualRequest.getResource()); + Assert.assertEquals(permissions, actualRequest.getPermissionsList()); + } +}