Skip to content

Commit 1eded7e

Browse files
committed
Add javadoc and tests for PushConfig (#1004)
1 parent 6e0c6c0 commit 1eded7e

File tree

4 files changed

+243
-17
lines changed

4 files changed

+243
-17
lines changed

gcloud-java-pubsub/src/main/java/com/google/cloud/pubsub/Message.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public abstract static class Builder {
122122
abstract Builder publishTime(long publishTime);
123123

124124
/**
125-
* Creates a topic object.
125+
* Creates a message object.
126126
*/
127127
public abstract Message build();
128128
}

gcloud-java-pubsub/src/main/java/com/google/cloud/pubsub/PushConfig.java

+141-15
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,16 @@
2727
import java.util.Objects;
2828

2929
/**
30-
* PubSub subscription push configuration.
30+
* Google Cloud Pub/Sub configuration for a push subscription.
31+
*
32+
* <p>In a push subscription, the Pub/Sub server sends a request to the subscriber application. A
33+
* {@code PushConfig} object can be used to configure the application endpoint. The subscriber's
34+
* HTTP response serves as an implicit acknowledgement: a success response indicates that the
35+
* message has been succesfully processed and the Pub/Sub system can delete it from the
36+
* subscription; a non-success response indicates that the Pub/Sub server should resend it
37+
* (implicit "nack").
38+
*
39+
* @see <a href="https://cloud.google.com/pubsub/subscriber">Subscriber Guide</a>
3140
*/
3241
public final class PushConfig implements Serializable {
3342

@@ -36,34 +45,97 @@ public final class PushConfig implements Serializable {
3645
private final String endpoint;
3746
private final ImmutableMap<String, String> attributes;
3847

48+
/**
49+
* Builder for {@code PushConfig} objects.
50+
*/
3951
public static final class Builder {
4052

4153
private String endpoint;
42-
private final Map<String, String> attributes = new HashMap<>();
54+
private Map<String, String> attributes = new HashMap<>();
4355

4456
private Builder() {
4557
}
4658

47-
public Builder endPoint(String endpoint) {
59+
/**
60+
* Sets the URL locating the endpoint to which messages should be pushed. For example, an
61+
* endpoint might use {@code https://example.com/push}.
62+
*/
63+
public Builder endpoint(String endpoint) {
4864
this.endpoint = checkNotNull(endpoint);
4965
return this;
5066
}
5167

68+
/**
69+
* Adds an API-supported attribute that can be used to control different aspects of the message
70+
* delivery.
71+
*
72+
* <p>The currently supported attribute is {@code x-goog-version}, which can be used to change
73+
* the format of the push message. This attribute indicates the version of the data expected by
74+
* the endpoint. The endpoint version is based on the version of the Pub/Sub API. Possible
75+
* values for this attribute are:
76+
* <ul>
77+
* <li>{@code v1beta1}: uses the push format defined in the v1beta1 Pub/Sub API
78+
* <li>{@code v1} or {@code v1beta2}: uses the push format defined in the v1 Pub/Sub API
79+
* </ul>
80+
*
81+
* <p>If the {@code x-goog-version} attribute is not present when a subscription is created (see
82+
* {@link PubSub#create(SubscriptionInfo)} and {@link PubSub#createAsync(SubscriptionInfo)}), it
83+
* will default to {@code v1}. If it is not present when modifying the push config (see
84+
* {@link PubSub#replacePushConfig(String, PushConfig)} and
85+
* {@link PubSub#replacePushConfigAsync(String, PushConfig)}), its value will not be changed.
86+
*
87+
* @see <a href="https://cloud.google.com/pubsub/subscriber#msg-format">Message Format</a>
88+
*/
5289
public Builder addAttribute(String name, String value) {
5390
attributes.put(name, value);
5491
return this;
5592
}
5693

94+
/**
95+
* Sets the API-supported attributes that can be used to control different aspects of the
96+
* message delivery.
97+
*
98+
* <p>The currently supported attribute is {@code x-goog-version}, which can be used to change
99+
* the format of the push message. This attribute indicates the version of the data expected by
100+
* the endpoint. The endpoint version is based on the version of the Pub/Sub API. Possible
101+
* values for this attribute are:
102+
* <ul>
103+
* <li>{@code v1beta1}: uses the push format defined in the v1beta1 Pub/Sub API
104+
* <li>{@code v1} or {@code v1beta2}: uses the push format defined in the v1 Pub/Sub API
105+
* </ul>
106+
*
107+
* <p>If the {@code x-goog-version} attribute is not present when a subscription is created (see
108+
* {@link PubSub#create(SubscriptionInfo)} and {@link PubSub#createAsync(SubscriptionInfo)}), it
109+
* will default to {@code v1}. If it is not present when modifying the push config (see
110+
* {@link PubSub#replacePushConfig(String, PushConfig)} and
111+
* {@link PubSub#replacePushConfigAsync(String, PushConfig)}), its value will not be changed.
112+
*
113+
* @see <a href="https://cloud.google.com/pubsub/subscriber#msg-format">Message Format</a>
114+
*/
115+
public Builder attributes(Map<String, String> attributes) {
116+
this.attributes = new HashMap<>(attributes);
117+
return this;
118+
}
119+
120+
/**
121+
* Removes an API-supported attribute.
122+
*/
57123
public Builder removeAttribute(String name) {
58124
attributes.remove(name);
59125
return this;
60126
}
61127

128+
/**
129+
* Clears all API-supported attributes.
130+
*/
62131
public Builder clearAttributes() {
63132
attributes.clear();
64133
return this;
65134
}
66135

136+
/**
137+
* Creates a {@code PushConfig} object.
138+
*/
67139
public PushConfig build() {
68140
return new PushConfig(this);
69141
}
@@ -74,24 +146,49 @@ private PushConfig(Builder builder) {
74146
attributes = ImmutableMap.copyOf(builder.attributes);
75147
}
76148

149+
/**
150+
* Returns the URL locating the endpoint to which messages should be pushed. For example, an
151+
* endpoint might use {@code https://example.com/push}.
152+
*/
77153
public String endpoint() {
78154
return endpoint;
79155
}
80156

157+
/**
158+
* Returns the API-supported attributes that can be used to control different aspects of the
159+
* message delivery.
160+
*
161+
* <p>The currently supported attribute is {@code x-goog-version}, which can be used to change
162+
* the format of the push message. This attribute indicates the version of the data expected by
163+
* the endpoint. The endpoint version is based on the version of the Pub/Sub API. Possible
164+
* values for this attribute are:
165+
* <ul>
166+
* <li>{@code v1beta1}: uses the push format defined in the v1beta1 Pub/Sub API
167+
* <li>{@code v1} or {@code v1beta2}: uses the push format defined in the v1 Pub/Sub API
168+
* </ul>
169+
*
170+
* <p>If the {@code x-goog-version} attribute is not present when a subscription is created (see
171+
* {@link PubSub#create(SubscriptionInfo)} and {@link PubSub#createAsync(SubscriptionInfo)}), it
172+
* will default to {@code v1}. If it is not present when modifying the push config (see
173+
* {@link PubSub#replacePushConfig(String, PushConfig)} and
174+
* {@link PubSub#replacePushConfigAsync(String, PushConfig)}), its value will not be changed.
175+
*
176+
* @see <a href="https://cloud.google.com/pubsub/subscriber#msg-format">Message Format</a>
177+
*/
81178
public Map<String, String> attributes() {
82179
return attributes;
83180
}
84181

85182
@Override
86-
public boolean equals(Object o) {
87-
if (this == o) {
183+
public boolean equals(Object obj) {
184+
if (this == obj) {
88185
return true;
89186
}
90-
if (o == null || getClass() != o.getClass()) {
187+
if (!(obj instanceof PushConfig)) {
91188
return false;
92189
}
93-
PushConfig that = (PushConfig) o;
94-
return Objects.equals(endpoint, that.endpoint) && Objects.equals(attributes, that.attributes);
190+
PushConfig other = (PushConfig) obj;
191+
return Objects.equals(endpoint, other.endpoint) && Objects.equals(attributes, other.attributes);
95192
}
96193

97194
@Override
@@ -107,28 +204,57 @@ public String toString() {
107204
.toString();
108205
}
109206

207+
/**
208+
* Returns a builder for the {@code PushConfig} object.
209+
*/
110210
public Builder toBuilder() {
111211
return builder(endpoint, attributes);
112212
}
113213

214+
/**
215+
* Creates a {@code PushConfig} object given the push endpoint.
216+
*
217+
* @param endpoint the URL locating the endpoint to which messages should be pushed. For example,
218+
* an endpoint might use {@code https://example.com/push}.
219+
*/
114220
public static PushConfig of(String endpoint) {
115221
return builder(endpoint).build();
116222
}
117223

224+
/**
225+
* Creates a {@code PushConfig} object given the push endpoint and the API-supported attributes
226+
* that can be used to control different aspects of the message delivery.
227+
*
228+
* @param endpoint the URL locating the endpoint to which messages should be pushed. For example,
229+
* an endpoint might use {@code https://example.com/push}.
230+
* @param attributes API supported attributes used to control message delivery. See
231+
* {@link Builder#attributes(Map)} for more details.
232+
*/
118233
public static PushConfig of(String endpoint, Map<String, String> attributes) {
119234
return builder(endpoint, attributes).build();
120235
}
121236

122-
public static Builder builder(String endPoint) {
123-
return new Builder().endPoint(endPoint);
237+
/**
238+
* Creates a builder for {@code PushConfig} objects given the push endpoint.
239+
*
240+
* @param endpoint the URL locating the endpoint to which messages should be pushed. For example,
241+
* an endpoint might use {@code https://example.com/push}.
242+
*/
243+
public static Builder builder(String endpoint) {
244+
return new Builder().endpoint(endpoint);
124245
}
125246

247+
/**
248+
* Creates a builder for {@code PushConfig} objects given the push endpoint and the API-supported
249+
* attributes that can be used to control different aspects of the message delivery.
250+
*
251+
* @param endpoint the URL locating the endpoint to which messages should be pushed. For example,
252+
* an endpoint might use {@code https://example.com/push}.
253+
* @param attributes API supported attributes used to control message delivery. See
254+
* {@link Builder#attributes(Map)} for more details.
255+
*/
126256
public static Builder builder(String endpoint, Map<String, String> attributes) {
127-
Builder builder = builder(endpoint);
128-
for (Map.Entry<String, String> entry : attributes.entrySet()) {
129-
builder.addAttribute(entry.getKey(), entry.getValue());
130-
}
131-
return builder;
257+
return builder(endpoint).attributes(attributes);
132258
}
133259

134260
com.google.pubsub.v1.PushConfig toPb() {

gcloud-java-pubsub/src/test/java/com/google/cloud/pubsub/MessageTest.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ public void testToBuilder() {
5757
.build();
5858
assertEquals("newPayload", message.payloadAsString());
5959
assertEquals(ImmutableMap.of("key1", "value1"), message.attributes());
60-
message = MESSAGE.toBuilder().payload(PAYLOAD_STRING).attributes(ATTRIBUTES).build();
60+
message = message.toBuilder()
61+
.payload(PAYLOAD_STRING)
62+
.removeAttribute("key1")
63+
.attributes(ATTRIBUTES)
64+
.build();
6165
compareMessage(MESSAGE, message);
6266
}
6367

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.pubsub;
18+
19+
import static org.junit.Assert.assertEquals;
20+
21+
import com.google.common.collect.ImmutableMap;
22+
23+
import org.junit.Test;
24+
25+
import java.util.Map;
26+
27+
public class PushConfigTest {
28+
29+
private static final String ENDPOINT = "https://example.com/push";
30+
private static final Map<String, String> ATTRIBUTES =
31+
ImmutableMap.of("key1", "value1", "key2", "value2");
32+
private static final PushConfig PUSH_CONFIG = PushConfig.builder(ENDPOINT, ATTRIBUTES).build();
33+
34+
@Test
35+
public void testToBuilder() {
36+
comparePushConfig(PUSH_CONFIG, PUSH_CONFIG.toBuilder().build());
37+
PushConfig pushConfig = PUSH_CONFIG.toBuilder()
38+
.endpoint("https://example2.com/push")
39+
.clearAttributes()
40+
.addAttribute("key1", "value1")
41+
.build();
42+
assertEquals("https://example2.com/push", pushConfig.endpoint());
43+
assertEquals(ImmutableMap.of("key1", "value1"), pushConfig.attributes());
44+
pushConfig = pushConfig.toBuilder()
45+
.endpoint(ENDPOINT)
46+
.removeAttribute("key1")
47+
.attributes(ATTRIBUTES)
48+
.build();
49+
comparePushConfig(PUSH_CONFIG, pushConfig);
50+
}
51+
52+
@Test
53+
public void testBuilder() {
54+
assertEquals(ENDPOINT, PUSH_CONFIG.endpoint());
55+
assertEquals(ATTRIBUTES, PUSH_CONFIG.attributes());
56+
PushConfig pushConfig = PushConfig.builder("https://example2.com/push")
57+
.endpoint(ENDPOINT)
58+
.attributes(ATTRIBUTES)
59+
.clearAttributes()
60+
.addAttribute("key1", "value1")
61+
.addAttribute("key2", "value2")
62+
.build();
63+
assertEquals(ENDPOINT, pushConfig.endpoint());
64+
assertEquals(ATTRIBUTES, pushConfig.attributes());
65+
comparePushConfig(PUSH_CONFIG, pushConfig);
66+
}
67+
68+
@Test
69+
public void testOf() {
70+
PushConfig pushConfig = PushConfig.of(ENDPOINT);
71+
assertEquals(ENDPOINT, pushConfig.endpoint());
72+
assertEquals(ImmutableMap.of(), pushConfig.attributes());
73+
pushConfig = PushConfig.of(ENDPOINT, ATTRIBUTES);
74+
assertEquals(ENDPOINT, pushConfig.endpoint());
75+
assertEquals(ATTRIBUTES, pushConfig.attributes());
76+
comparePushConfig(PUSH_CONFIG, pushConfig);
77+
}
78+
79+
@Test
80+
public void testToAndFromPb() {
81+
comparePushConfig(PUSH_CONFIG, PushConfig.fromPb(PUSH_CONFIG.toPb()));
82+
}
83+
84+
@Test
85+
public void testToAndFromPbIncomplete() {
86+
PushConfig pushConfig = PushConfig.of(ENDPOINT);
87+
comparePushConfig(pushConfig, PushConfig.fromPb(pushConfig.toPb()));
88+
}
89+
90+
private void comparePushConfig(PushConfig expected, PushConfig value) {
91+
assertEquals(expected, value);
92+
assertEquals(expected.endpoint(), value.endpoint());
93+
assertEquals(expected.attributes(), value.attributes());
94+
assertEquals(expected.hashCode(), value.hashCode());
95+
}
96+
}

0 commit comments

Comments
 (0)