Skip to content

Commit 9ec67ad

Browse files
committed
Add javadoc and unit tests for TopicInfo (googleapis#976)
1 parent 2cd4b0c commit 9ec67ad

File tree

4 files changed

+117
-11
lines changed

4 files changed

+117
-11
lines changed

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.cloud.BaseService;
2323
import com.google.cloud.Page;
2424
import com.google.cloud.pubsub.spi.PubSubRpc;
25+
import com.google.cloud.pubsub.spi.v1.PublisherApi;
2526
import com.google.common.base.Function;
2627
import com.google.common.base.Throwables;
2728
import com.google.common.util.concurrent.Uninterruptibles;
@@ -59,7 +60,7 @@ public Topic create(TopicInfo topic) {
5960

6061
@Override
6162
public Future<Topic> createAsync(TopicInfo topic) {
62-
return lazyTransform(rpc.create(topic.toPb()), Topic.fromPbFunction(this));
63+
return lazyTransform(rpc.create(topic.toPb(options().projectId())), Topic.fromPbFunction(this));
6364
}
6465

6566
@Override
@@ -69,7 +70,9 @@ public Topic getTopic(String topic) {
6970

7071
@Override
7172
public Future<Topic> getTopicAsync(String topic) {
72-
GetTopicRequest request = GetTopicRequest.newBuilder().setTopic(topic).build();
73+
GetTopicRequest request = GetTopicRequest.newBuilder()
74+
.setTopic(PublisherApi.formatTopicName(options().projectId(), topic))
75+
.build();
7376
return lazyTransform(rpc.get(request), Topic.fromPbFunction(this));
7477
}
7578

@@ -80,7 +83,9 @@ public boolean deleteTopic(String topic) {
8083

8184
@Override
8285
public Future<Boolean> deleteTopicAsync(String topic) {
83-
DeleteTopicRequest request = DeleteTopicRequest.newBuilder().setTopic(topic).build();
86+
DeleteTopicRequest request = DeleteTopicRequest.newBuilder()
87+
.setTopic(PublisherApi.formatTopicName(options().projectId(), topic))
88+
.build();
8489
return lazyTransform(rpc.delete(request), new Function<Empty, Boolean>() {
8590
@Override
8691
public Boolean apply(Empty input) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public boolean equals(Object obj) {
8686
return false;
8787
}
8888
Topic other = (Topic) obj;
89-
return Objects.equals(toPb(), other.toPb()) && Objects.equals(options, other.options);
89+
return Objects.equals(name(), other.name()) && Objects.equals(options, other.options);
9090
}
9191

9292
public PubSub pubSub() {

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

+46-7
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818

1919
import static com.google.common.base.Preconditions.checkNotNull;
2020

21+
import com.google.cloud.pubsub.spi.v1.PublisherApi;
2122
import com.google.common.base.MoreObjects;
2223

2324
import java.io.Serializable;
2425
import java.util.Objects;
2526

2627
/**
27-
* Pub/Sub Topic information.
28+
* A Google Cloud Pub/Sub topic. A topic is a named resource to which messages are sent by
29+
* publishers. Subscribers can receive messages sent to a topic by creating subscriptions.
30+
*
31+
* @see <a href="https://cloud.google.com/pubsub/overview#data_model">Pub/Sub Data Model</a>
2832
*/
2933
public class TopicInfo implements Serializable {
3034

@@ -33,12 +37,21 @@ public class TopicInfo implements Serializable {
3337
private final String name;
3438

3539
/**
36-
* Builder for TopicInfo.
40+
* Builder for {@code TopicInfo} objects.
3741
*/
3842
public abstract static class Builder {
3943

44+
/**
45+
* Sets the name of the topic. The name must start with a letter, and contain only letters
46+
* ({@code [A-Za-z]}), numbers ({@code [0-9]}), dashes ({@code -}), underscores ({@code _}),
47+
* periods ({@code .}), tildes ({@code ~}), plus ({@code +}) or percent signs ({@code %}). It
48+
* must be between 3 and 255 characters in length and cannot begin with the string {@code goog}.
49+
*/
4050
public abstract Builder name(String name);
4151

52+
/**
53+
* Creates a topic object.
54+
*/
4255
public abstract TopicInfo build();
4356
}
4457

@@ -70,6 +83,12 @@ public TopicInfo build() {
7083
name = builder.name;
7184
}
7285

86+
/**
87+
* Returns the name of the topic. The name must start with a letter, and contain only letters
88+
* ({@code [A-Za-z]}), numbers ({@code [0-9]}), dashes ({@code -}), underscores ({@code _}),
89+
* periods ({@code .}), tildes ({@code ~}), plus ({@code +}) or percent signs ({@code %}). It
90+
* must be between 3 and 255 characters in length and cannot begin with the string {@code goog}.
91+
*/
7392
public String name() {
7493
return name;
7594
}
@@ -84,10 +103,10 @@ public boolean equals(Object obj) {
84103
if (this == obj) {
85104
return true;
86105
}
87-
if (obj == null || getClass() != obj.getClass()) {
106+
if (obj == null || !obj.getClass().equals(this.getClass())) {
88107
return false;
89108
}
90-
return Objects.equals(toPb(), ((TopicInfo) obj).toPb());
109+
return Objects.equals(name, ((TopicInfo) obj).name);
91110
}
92111

93112
@Override
@@ -97,22 +116,42 @@ public String toString() {
97116
.toString();
98117
}
99118

100-
com.google.pubsub.v1.Topic toPb() {
101-
return com.google.pubsub.v1.Topic.newBuilder().setName(name).build();
119+
com.google.pubsub.v1.Topic toPb(String projectId) {
120+
return com.google.pubsub.v1.Topic.newBuilder()
121+
.setName(PublisherApi.formatTopicName(projectId, name))
122+
.build();
102123
}
103124

104125
static TopicInfo fromPb(com.google.pubsub.v1.Topic topicPb) {
105-
return builder(topicPb.getName()).build();
126+
return builder(PublisherApi.parseTopicFromTopicName(topicPb.getName())).build();
106127
}
107128

108129
public Builder toBuilder() {
109130
return new BuilderImpl(this);
110131
}
111132

133+
/**
134+
* Creates a {@code TopicInfo} object given the name of the topic.
135+
*
136+
* @param name the name of the topic. The name must start with a letter, and contain only letters
137+
* ({@code [A-Za-z]}), numbers ({@code [0-9]}), dashes ({@code -}), underscores ({@code _}),
138+
* periods ({@code .}), tildes ({@code ~}), plus ({@code +}) or percent signs ({@code %}).
139+
* It must be between 3 and 255 characters in length and cannot begin with the string
140+
* {@code goog}.
141+
*/
112142
public static TopicInfo of(String name) {
113143
return builder(name).build();
114144
}
115145

146+
/**
147+
* Creates a builder for {@code TopicInfo} objects given the name of the topic.
148+
*
149+
* @param name the name of the topic. The name must start with a letter, and contain only letters
150+
* ({@code [A-Za-z]}), numbers ({@code [0-9]}), dashes ({@code -}), underscores ({@code _}),
151+
* periods ({@code .}), tildes ({@code ~}), plus ({@code +}) or percent signs ({@code %}).
152+
* It must be between 3 and 255 characters in length and cannot begin with the string
153+
* {@code goog}.
154+
*/
116155
public static Builder builder(String name) {
117156
return new BuilderImpl(name);
118157
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 org.junit.Test;
22+
23+
public class TopicInfoTest {
24+
25+
private static final String NAME = "topic";
26+
private static final TopicInfo TOPIC_INFO = TopicInfo.builder("topic").build();
27+
28+
@Test
29+
public void testToBuilder() {
30+
compareTopicInfo(TOPIC_INFO, TOPIC_INFO.toBuilder().build());
31+
TopicInfo topicInfo = TOPIC_INFO.toBuilder()
32+
.name("newTopic")
33+
.build();
34+
assertEquals("newTopic", topicInfo.name());
35+
topicInfo = topicInfo.toBuilder().name(NAME).build();
36+
compareTopicInfo(TOPIC_INFO, topicInfo);
37+
}
38+
39+
@Test
40+
public void testBuilder() {
41+
assertEquals(NAME, TOPIC_INFO.name());
42+
TopicInfo topicInfo = TopicInfo.builder("wrongName").name(NAME).build();
43+
compareTopicInfo(TOPIC_INFO, topicInfo);
44+
}
45+
46+
@Test
47+
public void testOf() {
48+
compareTopicInfo(TOPIC_INFO, TopicInfo.of(NAME));
49+
}
50+
51+
@Test
52+
public void testToAndFromPb() {
53+
compareTopicInfo(TOPIC_INFO, TopicInfo.fromPb(TOPIC_INFO.toPb("project")));
54+
assertEquals("projects/project/topics/topic", TOPIC_INFO.toPb("project").getName());
55+
}
56+
57+
private void compareTopicInfo(TopicInfo expected, TopicInfo value) {
58+
assertEquals(expected, value);
59+
assertEquals(expected.name(), value.name());
60+
assertEquals(expected.hashCode(), value.hashCode());
61+
}
62+
}

0 commit comments

Comments
 (0)