Skip to content

Commit

Permalink
feat: fix review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
athakor committed Feb 26, 2020
1 parent 4758f81 commit 3bf7e8d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 43 deletions.
13 changes: 5 additions & 8 deletions google-cloud-storage/pom.xml
Expand Up @@ -80,14 +80,6 @@
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java-util</artifactId>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
</dependency>
<dependency>
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-google-cloud-pubsub-v1</artifactId>
</dependency>
<dependency>
<groupId>org.threeten</groupId>
<artifactId>threetenbp</artifactId>
Expand Down Expand Up @@ -159,6 +151,11 @@
<artifactId>truth</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
Expand Down
Expand Up @@ -17,12 +17,12 @@

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

import com.google.api.pathtemplate.PathTemplate;
import com.google.api.services.storage.model.Notification;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.pubsub.v1.ProjectTopicName;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
Expand All @@ -37,6 +37,8 @@
public class NotificationInfo implements Serializable {

private static final long serialVersionUID = 5725883368559753810L;
private static final PathTemplate PATH_TEMPLATE =
PathTemplate.createWithoutUrlEncoding("projects/{project}/topics/{topic}");

public enum PayloadFormat {
JSON_API_V1,
Expand All @@ -58,7 +60,7 @@ public Notification apply(NotificationInfo NotificationInfo) {
}
};
private final String generatedId;
private final ProjectTopicName topic;
private final String topic;
private final List<String> eventTypes;
private final Map<String, String> customAttributes;
private final PayloadFormat payloadFormat;
Expand All @@ -69,15 +71,15 @@ public Notification apply(NotificationInfo NotificationInfo) {
public static final class Builder {

private String generatedId;
private ProjectTopicName topic;
private String topic;
private List<String> eventTypes;
private Map<String, String> customAttributes;
private PayloadFormat payloadFormat;
private String objectNamePrefix;
private String etag;
private String selfLink;

Builder(ProjectTopicName topic) {
Builder(String topic) {
this.topic = topic;
}

Expand All @@ -102,7 +104,8 @@ Builder setSelfLink(String selfLink) {
return this;
}

public Builder setTopic(ProjectTopicName topic) {
/** The name of the topic. It must have the format "projects/{project}/topics/{topic}". */
public Builder setTopic(String topic) {
this.topic = topic;
return this;
}
Expand Down Expand Up @@ -155,8 +158,8 @@ public String getGeneratedId() {
return generatedId;
}

/** Returns the Cloud PubSub topic to which this subscription publishes. */
public ProjectTopicName getTopic() {
/** Returns the topic to which this subscription publishes. */
public String getTopic() {
return topic;
}

Expand Down Expand Up @@ -248,25 +251,35 @@ Notification toPb() {
notificationPb.setPayloadFormat(PayloadFormat.NONE.toString());
}
notificationPb.setSelfLink(selfLink);
notificationPb.setTopic(topic.toString());
notificationPb.setTopic(topic);

return notificationPb;
}

/** Creates a {@code NotificationInfo} object for the provided topic name. */
public static NotificationInfo of(ProjectTopicName topic) {
/**
* Creates a {@code NotificationInfo} object for the provided topic name.
*
* @param topic The name of the topic. It must have the format
* "projects/{project}/topics/{topic}".
*/
public static NotificationInfo of(String topic) {
PATH_TEMPLATE.validatedMatch(topic, "topic name must be in valid format");
return newBuilder(topic).build();
}

/**
* Returns a {@code NotificationInfo} builder where the topic's name is set to the provided name.
*
* @param topic The name of the topic. It must have the format
* "projects/{project}/topics/{topic}".
*/
public static Builder newBuilder(ProjectTopicName topic) {
public static Builder newBuilder(String topic) {
PATH_TEMPLATE.validatedMatch(topic, "topic name must be in valid format");
return new Builder(topic);
}

static NotificationInfo fromPb(Notification notificationPb) {
Builder builder = newBuilder(ProjectTopicName.parse(notificationPb.getTopic()));
Builder builder = newBuilder(notificationPb.getTopic());
if (notificationPb.getId() != null) {
builder.setGeneratedId(notificationPb.getId());
}
Expand All @@ -283,7 +296,7 @@ static NotificationInfo fromPb(Notification notificationPb) {
builder.setObjectNamePrefix(notificationPb.getObjectNamePrefix());
}
if (notificationPb.getTopic() != null) {
builder.setTopic(ProjectTopicName.parse(notificationPb.getTopic()));
builder.setTopic(notificationPb.getTopic());
}
if (notificationPb.getEventTypes() != null) {
builder.setEventTypes(notificationPb.getEventTypes());
Expand Down
Expand Up @@ -21,7 +21,6 @@
import com.google.cloud.storage.NotificationInfo.PayloadFormat;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.pubsub.v1.ProjectTopicName;
import java.util.List;
import java.util.Map;
import org.junit.Test;
Expand All @@ -35,7 +34,7 @@ public class NotificationInfoTest {
ImmutableList.of("OBJECT_FINALIZE", "OBJECT_METADATA_UPDATE");
private static final String OBJECT_NAME_PREFIX = "index.html";
private static final PayloadFormat PAYLOAD_FORMAT = PayloadFormat.JSON_API_V1.JSON_API_V1;
private static final ProjectTopicName TOPIC = ProjectTopicName.of("myProject", "topic1");
private static final String TOPIC = "projects/myProject/topics/topic1";
private static final Map<String, String> CUSTOM_ATTRIBUTES = ImmutableMap.of("label1", "value1");
private static final NotificationInfo NOTIFICATION_INFO =
NotificationInfo.newBuilder(TOPIC)
Expand Down
Expand Up @@ -58,7 +58,6 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.io.BaseEncoding;
import com.google.pubsub.v1.ProjectTopicName;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
Expand Down Expand Up @@ -383,7 +382,7 @@ public long millisTime() {
private static final String OBJECT_NAME_PREFIX = "index.html";
private static final NotificationInfo.PayloadFormat PAYLOAD_FORMAT =
NotificationInfo.PayloadFormat.JSON_API_V1.JSON_API_V1;
private static final ProjectTopicName TOPIC = ProjectTopicName.of("myProject", "topic1");
private static final String TOPIC = "projects/myProject/topics/topic1";
private static final Map<String, String> CUSTOM_ATTRIBUTES = ImmutableMap.of("label1", "value1");
private static final NotificationInfo NOTIFICATION_INFO_01 =
NotificationInfo.newBuilder(TOPIC)
Expand Down
Expand Up @@ -92,7 +92,6 @@
import com.google.iam.v1.Binding;
import com.google.iam.v1.IAMPolicyGrpc;
import com.google.iam.v1.SetIamPolicyRequest;
import com.google.pubsub.v1.ProjectTopicName;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Metadata;
Expand Down Expand Up @@ -180,8 +179,9 @@ public class ITStorageTest {
private static final String ID = UUID.randomUUID().toString().substring(0, 8);
private static final NotificationInfo.PayloadFormat PAYLOAD_FORMAT =
NotificationInfo.PayloadFormat.JSON_API_V1.JSON_API_V1;
private static final ProjectTopicName TOPIC =
ProjectTopicName.of(PROJECT, String.format("test_topic_foo_%s", ID));

private static final String TOPIC =
String.format("projects/%s/topics/test_topic_foo_%s", PROJECT, ID);
private static final Map<String, String> CUSTOM_ATTRIBUTES = ImmutableMap.of("label1", "value1");

@BeforeClass
Expand All @@ -205,11 +205,10 @@ public static void beforeClass() throws IOException {
// Create pubsub topic for notification.
topicAdminClient = TopicAdminClient.create();
topicAdminClient.createTopic(TOPIC);
com.google.iam.v1.Policy policy = topicAdminClient.getIamPolicy(TOPIC.toString());
com.google.iam.v1.Policy policy = topicAdminClient.getIamPolicy(TOPIC);
Binding binding =
Binding.newBuilder().setRole("roles/owner").addMembers("allAuthenticatedUsers").build();
topicAdminClient.setIamPolicy(
TOPIC.toString(), policy.toBuilder().addBindings(binding).build());
topicAdminClient.setIamPolicy(TOPIC, policy.toBuilder().addBindings(binding).build());

// Create a notification on a bucket.
NotificationInfo notificationInfo =
Expand Down Expand Up @@ -3000,7 +2999,7 @@ public void testGetNotification() {
Notification actualNotification = storage.getNotification(BUCKET, notification.getId());
assertEquals(CUSTOM_ATTRIBUTES, actualNotification.getCustomAttributes());
assertEquals(PAYLOAD_FORMAT.name(), actualNotification.getPayloadFormat());
assertTrue(actualNotification.getTopic().contains(TOPIC.toString()));
assertTrue(actualNotification.getTopic().contains(TOPIC));
}

@Test
Expand All @@ -3010,7 +3009,7 @@ public void testListNotification() {
if (actualNotification.getId().equals(notification.getId())) {
assertEquals(CUSTOM_ATTRIBUTES, actualNotification.getCustomAttributes());
assertEquals(PAYLOAD_FORMAT.name(), actualNotification.getPayloadFormat());
assertTrue(actualNotification.getTopic().contains(TOPIC.toString()));
assertTrue(actualNotification.getTopic().contains(TOPIC));
}
}
}
Expand Down
16 changes: 6 additions & 10 deletions pom.xml
Expand Up @@ -151,16 +151,6 @@
<artifactId>proto-google-common-protos</artifactId>
<version>1.17.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
<version>1.102.1</version>
</dependency>
<dependency>
<groupId>com.google.api.grpc</groupId>
<artifactId>proto-google-cloud-pubsub-v1</artifactId>
<version>1.84.1</version>
</dependency>
<dependency>
<groupId>org.threeten</groupId>
<artifactId>threetenbp</artifactId>
Expand Down Expand Up @@ -194,6 +184,12 @@
<version>1.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-pubsub</artifactId>
<version>1.102.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
Expand Down

0 comments on commit 3bf7e8d

Please sign in to comment.