diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/PubSubExample.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/PubSubExample.java index 13368d65a2f8..82011c672edf 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/PubSubExample.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/PubSubExample.java @@ -21,22 +21,18 @@ import com.google.cloud.Role; import com.google.cloud.pubsub.PubSub; import com.google.cloud.pubsub.PubSubOptions; -import com.google.cloud.pubsub.PushConfig; import com.google.cloud.pubsub.Subscription; import com.google.cloud.pubsub.SubscriptionId; import com.google.cloud.pubsub.SubscriptionInfo; import com.google.cloud.pubsub.Topic; import com.google.cloud.pubsub.TopicInfo; import com.google.common.collect.ImmutableMap; - -import java.util.ArrayList; +import com.google.pubsub.v1.PushConfig; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; /** * An example of using Google BigQuery. @@ -336,7 +332,7 @@ SubscriptionInfo parse(String... args) throws Exception { } else { SubscriptionInfo.Builder builder = SubscriptionInfo.newBuilder(args[0], args[1]); if (args.length == 3) { - builder.setPushConfig(PushConfig.of(args[2])); + builder.setPushConfig(PushConfig.newBuilder().setPushEndpoint(args[2]).build()); } return builder.build(); } @@ -387,7 +383,7 @@ Tuple parse(String... args) throws Exception { String subscription = args[0]; PushConfig pushConfig = null; if (args.length == 2) { - pushConfig = PushConfig.of(args[1]); + pushConfig = PushConfig.newBuilder().setPushEndpoint(args[2]).build(); } return Tuple.of(subscription, pushConfig); } diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PubSubSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PubSubSnippets.java index 7b278f06e2e7..f12b1f406e65 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PubSubSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/PubSubSnippets.java @@ -29,19 +29,17 @@ import com.google.cloud.Role; import com.google.cloud.pubsub.PubSub; import com.google.cloud.pubsub.PubSub.ListOption; -import com.google.cloud.pubsub.PushConfig; import com.google.cloud.pubsub.Subscription; import com.google.cloud.pubsub.SubscriptionId; import com.google.cloud.pubsub.SubscriptionInfo; import com.google.cloud.pubsub.Topic; import com.google.cloud.pubsub.TopicInfo; - +import com.google.pubsub.v1.PushConfig; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; /** * This class contains a number of snippets for the {@link PubSub} interface. @@ -224,7 +222,7 @@ public Subscription createSubscriptionAsync(String topicName, String subscriptio // [VARIABLE "https://www.example.com/push"] public void replacePushConfig(String subscriptionName, String endpoint) { // [START replacePushConfig] - PushConfig pushConfig = PushConfig.of(endpoint); + PushConfig pushConfig = PushConfig.newBuilder().setPushEndpoint(endpoint).build(); pubsub.replacePushConfig(subscriptionName, pushConfig); // [END replacePushConfig] } @@ -251,7 +249,7 @@ public void replacePushConfigToPull(String subscriptionName) { public void replacePushConfigAsync(String subscriptionName, String endpoint) throws ExecutionException, InterruptedException { // [START replacePushConfigAsync] - PushConfig pushConfig = PushConfig.of(endpoint); + PushConfig pushConfig = PushConfig.newBuilder().setPushEndpoint(endpoint).build(); Future future = pubsub.replacePushConfigAsync(subscriptionName, pushConfig); // ... future.get(); diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriptionSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriptionSnippets.java index fb408f53b0b4..60ab1b24e326 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriptionSnippets.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/SubscriptionSnippets.java @@ -25,10 +25,8 @@ import com.google.cloud.Identity; import com.google.cloud.Policy; import com.google.cloud.Role; -import com.google.cloud.pubsub.PushConfig; import com.google.cloud.pubsub.Subscription; - -import java.util.Iterator; +import com.google.pubsub.v1.PushConfig; import java.util.LinkedList; import java.util.List; import java.util.concurrent.ExecutionException; @@ -116,7 +114,7 @@ public boolean deleteAsync() throws ExecutionException, InterruptedException { // [VARIABLE "https://www.example.com/push"] public void replacePushConfig(String endpoint) { // [START replacePushConfig] - PushConfig pushConfig = PushConfig.of(endpoint); + PushConfig pushConfig = PushConfig.newBuilder().setPushEndpoint(endpoint).build(); subscription.replacePushConfig(pushConfig); // [END replacePushConfig] } @@ -141,7 +139,7 @@ public void replacePushConfigToPull() { public void replacePushConfigAsync(String endpoint) throws ExecutionException, InterruptedException { // [START replacePushConfigAsync] - PushConfig pushConfig = PushConfig.of(endpoint); + PushConfig pushConfig = PushConfig.newBuilder().setPushEndpoint(endpoint).build(); Future future = subscription.replacePushConfigAsync(pushConfig); // ... future.get(); diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITPubSubSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITPubSubSnippets.java index cbe75a5ca3fe..3a503c07963b 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITPubSubSnippets.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITPubSubSnippets.java @@ -29,25 +29,19 @@ import com.google.cloud.pubsub.PubSubOptions; import com.google.cloud.pubsub.Subscription; import com.google.cloud.pubsub.SubscriptionId; -import com.google.cloud.pubsub.SubscriptionInfo; import com.google.cloud.pubsub.Topic; -import com.google.cloud.pubsub.TopicInfo; import com.google.common.collect.Iterators; import com.google.common.collect.Sets; - +import java.util.List; +import java.util.Set; +import java.util.UUID; +import java.util.concurrent.ExecutionException; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.Timeout; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Set; -import java.util.UUID; -import java.util.concurrent.ExecutionException; - public class ITPubSubSnippets { private static final String NAME_SUFFIX = UUID.randomUUID().toString(); @@ -120,8 +114,8 @@ public void testTopicAndSubscription() throws ExecutionException, InterruptedExc pubsubSnippets.replacePushConfigAsync(subscriptionName2, endpoint); subscription1 = pubsubSnippets.getSubscription(subscriptionName1); subscription2 = pubsubSnippets.getSubscriptionAsync(subscriptionName2); - assertEquals(endpoint, subscription1.getPushConfig().getEndpoint()); - assertEquals(endpoint, subscription2.getPushConfig().getEndpoint()); + assertEquals(endpoint, subscription1.getPushConfig().getPushEndpoint()); + assertEquals(endpoint, subscription2.getPushConfig().getPushEndpoint()); pubsubSnippets.replacePushConfigToPull(subscriptionName1); pubsubSnippets.replacePushConfigToPullAsync(subscriptionName2); subscription1 = pubsubSnippets.getSubscription(subscriptionName1); diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriptionSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriptionSnippets.java index 07e456aaafb8..4bc23538912e 100644 --- a/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriptionSnippets.java +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/pubsub/snippets/ITSubscriptionSnippets.java @@ -31,15 +31,12 @@ import com.google.cloud.pubsub.SubscriptionInfo; import com.google.cloud.pubsub.Topic; import com.google.cloud.pubsub.TopicInfo; - +import java.util.UUID; +import java.util.concurrent.ExecutionException; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; -import java.util.Iterator; -import java.util.UUID; -import java.util.concurrent.ExecutionException; - public class ITSubscriptionSnippets { private static final String TOPIC = @@ -73,13 +70,13 @@ public void testPushConfig() throws ExecutionException, InterruptedException { String endpoint = "https://" + pubsub.getOptions().getProjectId() + ".appspot.com/push"; subscriptionSnippets.replacePushConfig(endpoint); Subscription updatedSubscription = pubsub.getSubscription(SUBSCRIPTION); - assertEquals(endpoint, updatedSubscription.getPushConfig().getEndpoint()); + assertEquals(endpoint, updatedSubscription.getPushConfig().getPushEndpoint()); subscriptionSnippets.replacePushConfigToPull(); updatedSubscription = pubsub.getSubscription(SUBSCRIPTION); assertNull(updatedSubscription.getPushConfig()); subscriptionSnippets.replacePushConfigAsync(endpoint); updatedSubscription = pubsub.getSubscription(SUBSCRIPTION); - assertEquals(endpoint, updatedSubscription.getPushConfig().getEndpoint()); + assertEquals(endpoint, updatedSubscription.getPushConfig().getPushEndpoint()); subscriptionSnippets.replacePushConfigToPullAsync(); updatedSubscription = pubsub.getSubscription(SUBSCRIPTION); assertNull(updatedSubscription.getPushConfig()); diff --git a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/PubSub.java b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/PubSub.java index 43e558347551..b53b8a497371 100644 --- a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/PubSub.java +++ b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/PubSub.java @@ -17,14 +17,13 @@ package com.google.cloud.pubsub; import com.google.cloud.AsyncPage; -import com.google.cloud.GrpcServiceOptions.ExecutorFactory; import com.google.cloud.Page; import com.google.cloud.Policy; import com.google.cloud.Service; +import com.google.pubsub.v1.PushConfig; import java.io.IOException; import java.util.List; import java.util.Map; -import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; /** diff --git a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/PubSubImpl.java b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/PubSubImpl.java index dc0d56b7cfdb..e0e03af5c971 100644 --- a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/PubSubImpl.java +++ b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/PubSubImpl.java @@ -54,6 +54,7 @@ import com.google.pubsub.v1.ListTopicsRequest; import com.google.pubsub.v1.ListTopicsResponse; import com.google.pubsub.v1.ModifyPushConfigRequest; +import com.google.pubsub.v1.PushConfig; import java.io.IOException; import java.util.List; import java.util.Map; @@ -326,12 +327,12 @@ public void replacePushConfig(String subscription, PushConfig pushConfig) { @Override public Future replacePushConfigAsync(String subscription, PushConfig pushConfig) { - ModifyPushConfigRequest request = ModifyPushConfigRequest.newBuilder() - .setSubscription( - SubscriberClient.formatSubscriptionName(getOptions().getProjectId(), subscription)) - .setPushConfig(pushConfig != null ? pushConfig.toPb() - : com.google.pubsub.v1.PushConfig.getDefaultInstance()) - .build(); + ModifyPushConfigRequest request = + ModifyPushConfigRequest.newBuilder() + .setSubscription( + SubscriberClient.formatSubscriptionName(getOptions().getProjectId(), subscription)) + .setPushConfig(pushConfig != null ? pushConfig : PushConfig.getDefaultInstance()) + .build(); return transform(rpc.modify(request), EMPTY_TO_VOID_FUNCTION); } diff --git a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/PushConfig.java b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/PushConfig.java deleted file mode 100644 index cd5c4aa06cda..000000000000 --- a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/PushConfig.java +++ /dev/null @@ -1,363 +0,0 @@ -/* - * 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; - -import static com.google.common.base.Preconditions.checkNotNull; - -import com.google.common.base.MoreObjects; -import com.google.common.collect.ImmutableMap; - -import java.io.Serializable; -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; - -/** - * Google Cloud Pub/Sub configuration for a push subscription. - * - *

In a push subscription, the Pub/Sub server sends a request to the subscriber application. A - * {@code PushConfig} object can be used to configure the application endpoint. The subscriber's - * HTTP response serves as an implicit acknowledgement: a success response indicates that the - * message has been succesfully processed and the Pub/Sub system can delete it from the - * subscription; a non-success response indicates that the Pub/Sub server should resend it - * (implicit "nack"). - * - * @see Subscriber Guide - */ -public final class PushConfig implements Serializable { - - private static final long serialVersionUID = 4408885787064092231L; - - private final String endpoint; - private final ImmutableMap attributes; - - /** - * Builder for {@code PushConfig} objects. - */ - public static final class Builder { - - private String endpoint; - private Map attributes = new HashMap<>(); - - private Builder() { - } - - /** - * Sets the URL locating the endpoint to which messages should be pushed. For example, an - * endpoint might use {@code https://example.com/push}. - */ - @Deprecated - public Builder endpoint(String endpoint) { - return setEndpoint(endpoint); - } - - /** - * Sets the URL locating the endpoint to which messages should be pushed. For example, an - * endpoint might use {@code https://example.com/push}. - */ - public Builder setEndpoint(String endpoint) { - this.endpoint = checkNotNull(endpoint); - return this; - } - - /** - * Adds an API-supported attribute that can be used to control different aspects of the message - * delivery. - * - *

The currently supported attribute is {@code x-goog-version}, which can be used to change - * the format of the push message. This attribute indicates the version of the data expected by - * the endpoint. The endpoint version is based on the version of the Pub/Sub API. Possible - * values for this attribute are: - *

    - *
  • {@code v1beta1}: uses the push format defined in the v1beta1 Pub/Sub API - *
  • {@code v1} or {@code v1beta2}: uses the push format defined in the v1 Pub/Sub API - *
- * - *

If the {@code x-goog-version} attribute is not present when a subscription is created (see - * {@link PubSub#create(SubscriptionInfo)} and {@link PubSub#createAsync(SubscriptionInfo)}), it - * will default to {@code v1}. If it is not present when modifying the push config (see - * {@link PubSub#replacePushConfig(String, PushConfig)} and - * {@link PubSub#replacePushConfigAsync(String, PushConfig)}), its value will not be changed. - * - * @see Message Format - */ - public Builder addAttribute(String name, String value) { - attributes.put(name, value); - return this; - } - - /** - * Sets the API-supported attributes that can be used to control different aspects of the - * message delivery. - * - *

The currently supported attribute is {@code x-goog-version}, which can be used to change - * the format of the push message. This attribute indicates the version of the data expected by - * the endpoint. The endpoint version is based on the version of the Pub/Sub API. Possible - * values for this attribute are: - *

    - *
  • {@code v1beta1}: uses the push format defined in the v1beta1 Pub/Sub API - *
  • {@code v1} or {@code v1beta2}: uses the push format defined in the v1 Pub/Sub API - *
- * - *

If the {@code x-goog-version} attribute is not present when a subscription is created (see - * {@link PubSub#create(SubscriptionInfo)} and {@link PubSub#createAsync(SubscriptionInfo)}), it - * will default to {@code v1}. If it is not present when modifying the push config (see - * {@link PubSub#replacePushConfig(String, PushConfig)} and - * {@link PubSub#replacePushConfigAsync(String, PushConfig)}), its value will not be changed. - * - * @see Message Format - */ - @Deprecated - public Builder attributes(Map attributes) { - return setAttributes(attributes); - } - - /** - * Sets the API-supported attributes that can be used to control different aspects of the - * message delivery. - * - *

The currently supported attribute is {@code x-goog-version}, which can be used to change - * the format of the push message. This attribute indicates the version of the data expected by - * the endpoint. The endpoint version is based on the version of the Pub/Sub API. Possible - * values for this attribute are: - *

    - *
  • {@code v1beta1}: uses the push format defined in the v1beta1 Pub/Sub API - *
  • {@code v1} or {@code v1beta2}: uses the push format defined in the v1 Pub/Sub API - *
- * - *

If the {@code x-goog-version} attribute is not present when a subscription is created (see - * {@link PubSub#create(SubscriptionInfo)} and {@link PubSub#createAsync(SubscriptionInfo)}), it - * will default to {@code v1}. If it is not present when modifying the push config (see - * {@link PubSub#replacePushConfig(String, PushConfig)} and - * {@link PubSub#replacePushConfigAsync(String, PushConfig)}), its value will not be changed. - * - * @see Message Format - */ - public Builder setAttributes(Map attributes) { - this.attributes = new HashMap<>(attributes); - return this; - } - - /** - * Removes an API-supported attribute. - */ - public Builder removeAttribute(String name) { - attributes.remove(name); - return this; - } - - /** - * Clears all API-supported attributes. - */ - public Builder clearAttributes() { - attributes.clear(); - return this; - } - - /** - * Creates a {@code PushConfig} object. - */ - public PushConfig build() { - return new PushConfig(this); - } - } - - private PushConfig(Builder builder) { - endpoint = builder.endpoint; - attributes = ImmutableMap.copyOf(builder.attributes); - } - - /** - * Returns the URL locating the endpoint to which messages should be pushed. For example, an - * endpoint might use {@code https://example.com/push}. - */ - @Deprecated - public String endpoint() { - return getEndpoint(); - } - - /** - * Returns the URL locating the endpoint to which messages should be pushed. For example, an - * endpoint might use {@code https://example.com/push}. - */ - public String getEndpoint() { - return endpoint; - } - - /** - * Returns the API-supported attributes that can be used to control different aspects of the - * message delivery. - * - *

The currently supported attribute is {@code x-goog-version}, which can be used to change - * the format of the push message. This attribute indicates the version of the data expected by - * the endpoint. The endpoint version is based on the version of the Pub/Sub API. Possible - * values for this attribute are: - *

    - *
  • {@code v1beta1}: uses the push format defined in the v1beta1 Pub/Sub API - *
  • {@code v1} or {@code v1beta2}: uses the push format defined in the v1 Pub/Sub API - *
- * - *

If the {@code x-goog-version} attribute is not present when a subscription is created (see - * {@link PubSub#create(SubscriptionInfo)} and {@link PubSub#createAsync(SubscriptionInfo)}), it - * will default to {@code v1}. If it is not present when modifying the push config (see - * {@link PubSub#replacePushConfig(String, PushConfig)} and - * {@link PubSub#replacePushConfigAsync(String, PushConfig)}), its value will not be changed. - * - * @see Message Format - */ - @Deprecated - public Map attributes() { - return getAttributes(); - } - - /** - * Returns the API-supported attributes that can be used to control different aspects of the - * message delivery. - * - *

The currently supported attribute is {@code x-goog-version}, which can be used to change - * the format of the push message. This attribute indicates the version of the data expected by - * the endpoint. The endpoint version is based on the version of the Pub/Sub API. Possible - * values for this attribute are: - *

    - *
  • {@code v1beta1}: uses the push format defined in the v1beta1 Pub/Sub API - *
  • {@code v1} or {@code v1beta2}: uses the push format defined in the v1 Pub/Sub API - *
- * - *

If the {@code x-goog-version} attribute is not present when a subscription is created (see - * {@link PubSub#create(SubscriptionInfo)} and {@link PubSub#createAsync(SubscriptionInfo)}), it - * will default to {@code v1}. If it is not present when modifying the push config (see - * {@link PubSub#replacePushConfig(String, PushConfig)} and - * {@link PubSub#replacePushConfigAsync(String, PushConfig)}), its value will not be changed. - * - * @see Message Format - */ - public Map getAttributes() { - return attributes; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (!(obj instanceof PushConfig)) { - return false; - } - PushConfig other = (PushConfig) obj; - return Objects.equals(endpoint, other.endpoint) && Objects.equals(attributes, other.attributes); - } - - @Override - public int hashCode() { - return Objects.hash(endpoint, attributes); - } - - @Override - public String toString() { - return MoreObjects.toStringHelper(this) - .add("attributes", attributes) - .add("endpoint", endpoint) - .toString(); - } - - /** - * Returns a builder for the {@code PushConfig} object. - */ - public Builder toBuilder() { - return newBuilder(endpoint, attributes); - } - - /** - * Creates a {@code PushConfig} object given the push endpoint. - * - * @param endpoint the URL locating the endpoint to which messages should be pushed. For example, - * an endpoint might use {@code https://example.com/push}. - */ - public static PushConfig of(String endpoint) { - return newBuilder(endpoint).build(); - } - - /** - * Creates a {@code PushConfig} object given the push endpoint and the API-supported attributes - * that can be used to control different aspects of the message delivery. - * - * @param endpoint the URL locating the endpoint to which messages should be pushed. For example, - * an endpoint might use {@code https://example.com/push}. - * @param attributes API supported attributes used to control message delivery. See - * {@link Builder#attributes(Map)} for more details. - */ - public static PushConfig of(String endpoint, Map attributes) { - return newBuilder(endpoint, attributes).build(); - } - - /** - * Creates a builder for {@code PushConfig} objects given the push endpoint. - * - * @param endpoint the URL locating the endpoint to which messages should be pushed. For example, - * an endpoint might use {@code https://example.com/push}. - */ - @Deprecated - public static Builder builder(String endpoint) { - return newBuilder(endpoint); - } - - /** - * Creates a builder for {@code PushConfig} objects given the push endpoint. - * - * @param endpoint the URL locating the endpoint to which messages should be pushed. For example, - * an endpoint might use {@code https://example.com/push}. - */ - public static Builder newBuilder(String endpoint) { - return new Builder().setEndpoint(endpoint); - } - - /** - * Creates a builder for {@code PushConfig} objects given the push endpoint and the API-supported - * attributes that can be used to control different aspects of the message delivery. - * - * @param endpoint the URL locating the endpoint to which messages should be pushed. For example, - * an endpoint might use {@code https://example.com/push}. - * @param attributes API supported attributes used to control message delivery. See - * {@link Builder#attributes(Map)} for more details. - */ - @Deprecated - public static Builder builder(String endpoint, Map attributes) { - return newBuilder(endpoint, attributes); - } - - /** - * Creates a builder for {@code PushConfig} objects given the push endpoint and the API-supported - * attributes that can be used to control different aspects of the message delivery. - * - * @param endpoint the URL locating the endpoint to which messages should be pushed. For example, - * an endpoint might use {@code https://example.com/push}. - * @param attributes API supported attributes used to control message delivery. See - * {@link Builder#attributes(Map)} for more details. - */ - public static Builder newBuilder(String endpoint, Map attributes) { - return newBuilder(endpoint).setAttributes(attributes); - } - - com.google.pubsub.v1.PushConfig toPb() { - return com.google.pubsub.v1.PushConfig.newBuilder().setPushEndpoint(endpoint) - .putAllAttributes(attributes).build(); - } - - static PushConfig fromPb(com.google.pubsub.v1.PushConfig pushConfigPb) { - return newBuilder(pushConfigPb.getPushEndpoint(), pushConfigPb.getAttributesMap()).build(); - } -} diff --git a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/Subscription.java b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/Subscription.java index 6f07c120c66c..45c3974b254a 100644 --- a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/Subscription.java +++ b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/Subscription.java @@ -20,6 +20,7 @@ import com.google.cloud.Policy; import com.google.common.base.Function; +import com.google.pubsub.v1.PushConfig; import java.io.IOException; import java.io.ObjectInputStream; import java.util.List; diff --git a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/SubscriptionInfo.java b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/SubscriptionInfo.java index b7ddc5bea16f..f408e604c6bb 100644 --- a/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/SubscriptionInfo.java +++ b/google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/SubscriptionInfo.java @@ -20,6 +20,7 @@ import com.google.cloud.pubsub.spi.v1.SubscriberClient; import com.google.common.base.MoreObjects; +import com.google.pubsub.v1.PushConfig; import java.io.Serializable; import java.util.Objects; @@ -389,7 +390,7 @@ com.google.pubsub.v1.Subscription toPb(String projectId) { builder.setName(SubscriberClient.formatSubscriptionName(projectId, name)); builder.setAckDeadlineSeconds(ackDeadlineSeconds); if (pushConfig != null) { - builder.setPushConfig(pushConfig.toPb()); + builder.setPushConfig(pushConfig); } return builder.build(); } @@ -401,7 +402,7 @@ static SubscriptionInfo fromPb(com.google.pubsub.v1.Subscription subscription) { // A subscription with an "empty" push config is a pull subscription if (subscription.hasPushConfig() && !subscription.getPushConfig().getPushEndpoint().equals("")) { - builder.setPushConfig(PushConfig.fromPb(subscription.getPushConfig())); + builder.setPushConfig(subscription.getPushConfig()); } return builder.build(); } @@ -460,7 +461,9 @@ public static SubscriptionInfo of(TopicId topic, String name) { * an endpoint might use {@code https://example.com/push}. */ public static SubscriptionInfo of(String topic, String name, String endpoint) { - return newBuilder(topic, name).setPushConfig(PushConfig.of(endpoint)).build(); + return newBuilder(topic, name) + .setPushConfig(PushConfig.newBuilder().setPushEndpoint(endpoint).build()) + .build(); } /** @@ -478,7 +481,9 @@ public static SubscriptionInfo of(String topic, String name, String endpoint) { * an endpoint might use {@code https://example.com/push}. */ public static SubscriptionInfo of(TopicId topic, String name, String endpoint) { - return newBuilder(topic, name).setPushConfig(PushConfig.of(endpoint)).build(); + return newBuilder(topic, name) + .setPushConfig(PushConfig.newBuilder().setPushEndpoint(endpoint).build()) + .build(); } /** diff --git a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/BaseSystemTest.java b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/BaseSystemTest.java index eac041476f34..f9636c521965 100644 --- a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/BaseSystemTest.java +++ b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/BaseSystemTest.java @@ -18,31 +18,21 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import com.google.cloud.AsyncPage; import com.google.cloud.Page; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Iterators; -import com.google.common.collect.Lists; import com.google.common.collect.Sets; - -import org.junit.Ignore; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -import java.util.ArrayList; -import java.util.Collections; +import com.google.pubsub.v1.PushConfig; import java.util.Iterator; -import java.util.List; import java.util.Set; -import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; +import org.junit.Ignore; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; /** * A base class for system tests. This class can be extended to run system tests in different @@ -173,7 +163,7 @@ public void testCreateGetAndDeleteSubscriptionAsync() pubsub().create(TopicInfo.of(topic)); String name = formatForTest("test-create-get-delete-async-subscription"); String endpoint = "https://" + pubsub().getOptions().getProjectId() + ".appspot.com/push"; - PushConfig pushConfig = PushConfig.of(endpoint); + PushConfig pushConfig = PushConfig.newBuilder().setPushEndpoint(endpoint).build(); Future subscriptionFuture = pubsub().createAsync( SubscriptionInfo.newBuilder(topic, name).setPushConfig(pushConfig).build()); Subscription subscription = subscriptionFuture.get(); @@ -216,7 +206,7 @@ public void testReplaceSubscriptionPushConfig() { pubsub().create(TopicInfo.of(topic)); String name = formatForTest("test-replace-push-config-subscription"); String endpoint = "https://" + pubsub().getOptions().getProjectId() + ".appspot.com/push"; - PushConfig pushConfig = PushConfig.of(endpoint); + PushConfig pushConfig = PushConfig.newBuilder().setPushEndpoint(endpoint).build(); Subscription subscription = pubsub().create(SubscriptionInfo.newBuilder(topic, name).setPushConfig(pushConfig).build()); assertEquals(TopicId.of(pubsub().getOptions().getProjectId(), topic), subscription.getTopic()); @@ -258,7 +248,7 @@ public void testReplaceSubscriptionPushConfigAsync() // todo(mziccard) seems not to work on the emulator (returns 60) - see #989 // assertEquals(10, subscription.ackDeadlineSeconds()); String endpoint = "https://" + pubsub().getOptions().getProjectId() + ".appspot.com/push"; - PushConfig pushConfig = PushConfig.of(endpoint); + PushConfig pushConfig = PushConfig.newBuilder().setPushEndpoint(endpoint).build(); pubsub().replacePushConfigAsync(name, pushConfig).get(); Subscription remoteSubscription = pubsub().getSubscriptionAsync(name).get(); assertEquals(TopicId.of(pubsub().getOptions().getProjectId(), topic), diff --git a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/PubSubImplTest.java b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/PubSubImplTest.java index e314c720761f..5cff4c934bd3 100644 --- a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/PubSubImplTest.java +++ b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/PubSubImplTest.java @@ -54,8 +54,7 @@ import com.google.pubsub.v1.ListTopicsRequest; import com.google.pubsub.v1.ListTopicsResponse; import com.google.pubsub.v1.ModifyPushConfigRequest; -import com.google.pubsub.v1.PublishRequest; -import com.google.pubsub.v1.PublishResponse; +import com.google.pubsub.v1.PushConfig; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; @@ -82,7 +81,8 @@ public com.google.pubsub.v1.Topic apply(TopicInfo topicInfo) { }; private static final String SUBSCRIPTION = "subscription"; private static final String SUBSCRIPTION_NAME_PB = "projects/project/subscriptions/subscription"; - private static final PushConfig PUSH_CONFIG = PushConfig.of("endpoint"); + private static final PushConfig PUSH_CONFIG = + PushConfig.newBuilder().setPushEndpoint("endpoint").build(); private static final SubscriptionInfo SUBSCRIPTION_INFO = SubscriptionInfo.newBuilder(TOPIC, SUBSCRIPTION) .setAckDeadLineSeconds(42) @@ -599,10 +599,11 @@ public void testDeleteSubscriptionAsync_Null() throws ExecutionException, Interr @Test public void testReplacePushConfig() { - ModifyPushConfigRequest request = ModifyPushConfigRequest.newBuilder() - .setSubscription(SUBSCRIPTION_NAME_PB) - .setPushConfig(PUSH_CONFIG.toPb()) - .build(); + ModifyPushConfigRequest request = + ModifyPushConfigRequest.newBuilder() + .setSubscription(SUBSCRIPTION_NAME_PB) + .setPushConfig(PUSH_CONFIG) + .build(); Future response = Futures.immediateFuture(Empty.getDefaultInstance()); EasyMock.expect(pubsubRpcMock.modify(request)).andReturn(response); EasyMock.replay(pubsubRpcMock); @@ -625,10 +626,11 @@ public void testReplacePushConfig_Null() { @Test public void testReplacePushConfigAsync() throws ExecutionException, InterruptedException { - ModifyPushConfigRequest request = ModifyPushConfigRequest.newBuilder() - .setSubscription(SUBSCRIPTION_NAME_PB) - .setPushConfig(PUSH_CONFIG.toPb()) - .build(); + ModifyPushConfigRequest request = + ModifyPushConfigRequest.newBuilder() + .setSubscription(SUBSCRIPTION_NAME_PB) + .setPushConfig(PUSH_CONFIG) + .build(); Future response = Futures.immediateFuture(Empty.getDefaultInstance()); EasyMock.expect(pubsubRpcMock.modify(request)).andReturn(response); EasyMock.replay(pubsubRpcMock); diff --git a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/PushConfigTest.java b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/PushConfigTest.java deleted file mode 100644 index baf8ea4d3535..000000000000 --- a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/PushConfigTest.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * 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; - -import static org.junit.Assert.assertEquals; - -import com.google.common.collect.ImmutableMap; - -import org.junit.Test; - -import java.util.Map; - -public class PushConfigTest { - - private static final String ENDPOINT = "https://example.com/push"; - private static final Map ATTRIBUTES = - ImmutableMap.of("key1", "value1", "key2", "value2"); - private static final PushConfig PUSH_CONFIG = PushConfig.newBuilder(ENDPOINT, ATTRIBUTES).build(); - private static final PushConfig DEPRECATED_PUSH_CONFIG = - PushConfig.builder(ENDPOINT, ATTRIBUTES).build(); - - @Test - public void testToBuilder() { - comparePushConfig(PUSH_CONFIG, PUSH_CONFIG.toBuilder().build()); - PushConfig pushConfig = PUSH_CONFIG.toBuilder() - .setEndpoint("https://example2.com/push") - .clearAttributes() - .addAttribute("key1", "value1") - .build(); - assertEquals("https://example2.com/push", pushConfig.getEndpoint()); - assertEquals(ImmutableMap.of("key1", "value1"), pushConfig.getAttributes()); - pushConfig = pushConfig.toBuilder() - .setEndpoint(ENDPOINT) - .removeAttribute("key1") - .setAttributes(ATTRIBUTES) - .build(); - comparePushConfig(PUSH_CONFIG, pushConfig); - } - - @Test - public void testBuilder() { - assertEquals(ENDPOINT, DEPRECATED_PUSH_CONFIG.endpoint()); - assertEquals(ATTRIBUTES, DEPRECATED_PUSH_CONFIG.attributes()); - PushConfig pushConfig = PushConfig.builder("https://example2.com/push") - .endpoint(ENDPOINT) - .attributes(ATTRIBUTES) - .clearAttributes() - .addAttribute("key1", "value1") - .addAttribute("key2", "value2") - .build(); - assertEquals(ENDPOINT, pushConfig.endpoint()); - assertEquals(ATTRIBUTES, pushConfig.attributes()); - comparePushConfig(PUSH_CONFIG, pushConfig); - } - - @Test - public void testBuilderDeprecated() { - assertEquals(ENDPOINT, PUSH_CONFIG.getEndpoint()); - assertEquals(ATTRIBUTES, PUSH_CONFIG.getAttributes()); - PushConfig pushConfig = PushConfig.newBuilder("https://example2.com/push") - .setEndpoint(ENDPOINT) - .setAttributes(ATTRIBUTES) - .clearAttributes() - .addAttribute("key1", "value1") - .addAttribute("key2", "value2") - .build(); - assertEquals(ENDPOINT, pushConfig.getEndpoint()); - assertEquals(ATTRIBUTES, pushConfig.getAttributes()); - comparePushConfig(PUSH_CONFIG, pushConfig); - } - - @Test - public void testOf() { - PushConfig pushConfig = PushConfig.of(ENDPOINT); - assertEquals(ENDPOINT, pushConfig.getEndpoint()); - assertEquals(ImmutableMap.of(), pushConfig.getAttributes()); - pushConfig = PushConfig.of(ENDPOINT, ATTRIBUTES); - assertEquals(ENDPOINT, pushConfig.getEndpoint()); - assertEquals(ATTRIBUTES, pushConfig.getAttributes()); - comparePushConfig(PUSH_CONFIG, pushConfig); - } - - @Test - public void testToAndFromPb() { - comparePushConfig(PUSH_CONFIG, PushConfig.fromPb(PUSH_CONFIG.toPb())); - } - - @Test - public void testToAndFromPbIncomplete() { - PushConfig pushConfig = PushConfig.of(ENDPOINT); - comparePushConfig(pushConfig, PushConfig.fromPb(pushConfig.toPb())); - } - - private void comparePushConfig(PushConfig expected, PushConfig value) { - assertEquals(expected, value); - assertEquals(expected.getEndpoint(), value.getEndpoint()); - assertEquals(expected.getAttributes(), value.getAttributes()); - assertEquals(expected.hashCode(), value.hashCode()); - } -} diff --git a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/SubscriptionInfoTest.java b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/SubscriptionInfoTest.java index d17f29e78dbf..f8fec502d23a 100644 --- a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/SubscriptionInfoTest.java +++ b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/SubscriptionInfoTest.java @@ -19,6 +19,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import com.google.pubsub.v1.PushConfig; import org.junit.Test; public class SubscriptionInfoTest { @@ -26,7 +27,8 @@ public class SubscriptionInfoTest { private static final TopicId TOPIC = TopicId.of("project", "topic"); private static final String NAME = "subscription"; private static final String ENDPOINT = "https://example.com/push"; - private static final PushConfig PUSH_CONFIG = PushConfig.of(ENDPOINT); + private static final PushConfig PUSH_CONFIG = + PushConfig.newBuilder().setPushEndpoint(ENDPOINT).build(); private static final int ACK_DEADLINE = 42; private static final SubscriptionInfo SUBSCRIPTION_INFO = SubscriptionInfo.newBuilder(TOPIC, NAME) .setPushConfig(PUSH_CONFIG) @@ -117,12 +119,16 @@ public void testOf() { subscriptionInfo = SubscriptionInfo.of(TOPIC, NAME, ENDPOINT); assertEquals(TOPIC, subscriptionInfo.getTopic()); assertEquals(NAME, subscriptionInfo.getName()); - assertEquals(PushConfig.of(ENDPOINT), subscriptionInfo.getPushConfig()); + assertEquals( + PushConfig.newBuilder().setPushEndpoint(ENDPOINT).build(), + subscriptionInfo.getPushConfig()); assertEquals(0, subscriptionInfo.getAckDeadlineSeconds()); subscriptionInfo = SubscriptionInfo.of("topic", NAME, ENDPOINT); assertEquals(TopicId.of("topic"), subscriptionInfo.getTopic()); assertEquals(NAME, subscriptionInfo.getName()); - assertEquals(PushConfig.of(ENDPOINT), subscriptionInfo.getPushConfig()); + assertEquals( + PushConfig.newBuilder().setPushEndpoint(ENDPOINT).build(), + subscriptionInfo.getPushConfig()); assertEquals(0, subscriptionInfo.getAckDeadlineSeconds()); } diff --git a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/SubscriptionTest.java b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/SubscriptionTest.java index 0df868983d2b..dc75d8901634 100644 --- a/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/SubscriptionTest.java +++ b/google-cloud-pubsub/src/test/java/com/google/cloud/pubsub/SubscriptionTest.java @@ -19,7 +19,6 @@ import static org.easymock.EasyMock.createStrictMock; import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.reset; import static org.easymock.EasyMock.verify; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -31,22 +30,21 @@ import com.google.cloud.Policy; import com.google.cloud.Role; import com.google.common.collect.ImmutableList; -import com.google.common.collect.Lists; import com.google.common.util.concurrent.Futures; - +import com.google.pubsub.v1.PushConfig; +import java.util.List; +import java.util.concurrent.ExecutionException; import org.easymock.EasyMock; import org.junit.After; import org.junit.Test; -import java.util.List; -import java.util.concurrent.ExecutionException; - public class SubscriptionTest { private static final TopicId TOPIC_ID = TopicId.of("project", "topic"); private static final String NAME = "subscription"; private static final String ENDPOINT = "https://example.com/push"; - private static final PushConfig PUSH_CONFIG = PushConfig.of(ENDPOINT); + private static final PushConfig PUSH_CONFIG = + PushConfig.newBuilder().setPushEndpoint(ENDPOINT).build(); private static final int ACK_DEADLINE = 42; private static final SubscriptionInfo SUBSCRIPTION_INFO = SubscriptionInfo.newBuilder(TOPIC_ID, NAME) @@ -225,7 +223,8 @@ public void testDeleteAsyncFalse() throws ExecutionException, InterruptedExcepti public void testReplacePushConfig() { initializeExpectedSubscription(1); expect(pubsub.getOptions()).andReturn(mockOptions); - PushConfig pushConfig = PushConfig.of("https://example.com/newPush"); + PushConfig pushConfig = + PushConfig.newBuilder().setPushEndpoint("https://example.com/newPush").build(); pubsub.replacePushConfig(NAME, pushConfig); EasyMock.expectLastCall(); replay(pubsub); @@ -248,7 +247,8 @@ public void testReplacePushConfig_Null() { public void testReplacePushConfig_Async() throws ExecutionException, InterruptedException { initializeExpectedSubscription(1); expect(pubsub.getOptions()).andReturn(mockOptions); - PushConfig pushConfig = PushConfig.of("https://example.com/newPush"); + PushConfig pushConfig = + PushConfig.newBuilder().setPushEndpoint("https://example.com/newPush").build(); expect(pubsub.replacePushConfigAsync(NAME, pushConfig)) .andReturn(Futures.immediateFuture(null)); EasyMock.expectLastCall();