From 8489ff2c0d6ffab4cac305e0104263d883d58689 Mon Sep 17 00:00:00 2001 From: athakor Date: Thu, 2 Jul 2020 15:26:52 +0530 Subject: [PATCH] feat: simplified the code and fixed the review changes --- .../clirr-ignored-differences.xml | 2 +- .../google/cloud/storage/Notification.java | 286 ++++++++++++--- .../cloud/storage/NotificationInfo.java | 342 ------------------ .../com/google/cloud/storage/Storage.java | 23 +- .../com/google/cloud/storage/StorageImpl.java | 21 +- .../cloud/storage/spi/v1/StorageRpc.java | 10 +- .../cloud/storage/NotificationInfoTest.java | 102 ------ .../cloud/storage/NotificationTest.java | 112 +++--- .../cloud/storage/StorageImplMockitoTest.java | 22 +- .../cloud/storage/it/ITStorageTest.java | 11 +- 10 files changed, 315 insertions(+), 616 deletions(-) delete mode 100644 google-cloud-storage/src/main/java/com/google/cloud/storage/NotificationInfo.java delete mode 100644 google-cloud-storage/src/test/java/com/google/cloud/storage/NotificationInfoTest.java diff --git a/google-cloud-storage/clirr-ignored-differences.xml b/google-cloud-storage/clirr-ignored-differences.xml index 9f137f62c..04d2052ad 100644 --- a/google-cloud-storage/clirr-ignored-differences.xml +++ b/google-cloud-storage/clirr-ignored-differences.xml @@ -13,7 +13,7 @@ com/google/cloud/storage/Storage - com.google.cloud.storage.Notification createNotification(java.lang.String, com.google.cloud.storage.NotificationInfo) + com.google.cloud.storage.Notification createNotification(java.lang.String, com.google.cloud.storage.Notification) 7012 diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/Notification.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/Notification.java index 33532f558..78a5dcc79 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/Notification.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/Notification.java @@ -13,127 +13,301 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package com.google.cloud.storage; import static com.google.common.base.Preconditions.checkNotNull; +import com.google.api.pathtemplate.PathTemplate; +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 java.io.Serializable; +import java.util.List; import java.util.Map; import java.util.Objects; /** - * Google Storage Notification object; + * Google Storage Notification metadata; * - * @see notifications + * @see Concepts and + * Terminology */ -public class Notification extends NotificationInfo { +public class Notification 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, + NONE + } + + static final Function + FROM_PB_FUNCTION = + new Function() { + @Override + public Notification apply(com.google.api.services.storage.model.Notification pb) { + return Notification.fromPb(pb); + } + }; + static final Function + TO_PB_FUNCTION = + new Function() { + @Override + public com.google.api.services.storage.model.Notification apply( + Notification notification) { + return notification.toPb(); + } + }; + private final String generatedId; + private final String topic; + private final List eventTypes; + private final Map customAttributes; + private final PayloadFormat payloadFormat; + private final String objectNamePrefix; + private final String etag; + private final String selfLink; + + /** Builder for {@code NotificatioInfo}. */ + public static class Builder { - private final StorageOptions options; - private transient Storage storage; + private String generatedId; + private String topic; + private List eventTypes; + private Map customAttributes; + private PayloadFormat payloadFormat; + private String objectNamePrefix; + private String etag; + private String selfLink; - /** Builder for {@code Notification}. */ - public static class Builder extends NotificationInfo.Builder { - private final Storage storage; - private final NotificationInfo.BuilderImpl infoBuilder; + private Builder(String topic) { + this.topic = topic; + } - Builder(Notification notification) { - this.storage = notification.storage; - this.infoBuilder = new NotificationInfo.BuilderImpl(notification); + private Builder(Notification notification) { + generatedId = notification.generatedId; + etag = notification.etag; + selfLink = notification.selfLink; + topic = notification.topic; + eventTypes = notification.eventTypes; + customAttributes = notification.customAttributes; + payloadFormat = notification.payloadFormat; + objectNamePrefix = notification.objectNamePrefix; } - @Override public Builder setGeneratedId(String generatedId) { - infoBuilder.setGeneratedId(generatedId); + this.generatedId = generatedId; return this; } - @Override public Builder setSelfLink(String selfLink) { - infoBuilder.setSelfLink(selfLink); + this.selfLink = selfLink; return this; } - @Override + /** The name of the topic. It must have the format "projects/{project}/topics/{topic}". */ public Builder setTopic(String topic) { - infoBuilder.setTopic(topic); + this.topic = topic; return this; } - @Override public Builder setPayloadFormat(PayloadFormat payloadFormat) { - infoBuilder.setPayloadFormat(payloadFormat); + this.payloadFormat = payloadFormat; return this; } - @Override public Builder setObjectNamePrefix(String objectNamePrefix) { - infoBuilder.setObjectNamePrefix(objectNamePrefix); + this.objectNamePrefix = objectNamePrefix; return this; } - @Override public Builder setEventTypes(Iterable eventTypes) { - infoBuilder.setEventTypes(eventTypes); + this.eventTypes = eventTypes != null ? ImmutableList.copyOf(eventTypes) : null; return this; } - @Override public Builder setEtag(String etag) { - infoBuilder.setEtag(etag); + this.etag = etag; return this; } - @Override public Builder setCustomAttributes(Map customAttributes) { - infoBuilder.setCustomAttributes(customAttributes); + this.customAttributes = + customAttributes != null ? ImmutableMap.copyOf(customAttributes) : null; return this; } - @Override public Notification build() { - return new Notification(storage, infoBuilder); + checkNotNull(topic); + return new Notification(this); } } - Notification(Storage storage, NotificationInfo.BuilderImpl infoBuilder) { - super(infoBuilder); - this.storage = checkNotNull(storage); - this.options = storage.getOptions(); + private Notification(Builder builder) { + generatedId = builder.generatedId; + etag = builder.etag; + selfLink = builder.selfLink; + topic = builder.topic; + eventTypes = builder.eventTypes; + customAttributes = builder.customAttributes; + payloadFormat = builder.payloadFormat; + objectNamePrefix = builder.objectNamePrefix; + } + + /** Returns the service-generated id for the notification. */ + public String getGeneratedId() { + return generatedId; } - /** Returns the notification's {@code Storage} object used to issue requests. */ - public Storage getStorage() { - return storage; + /** Returns the topic to which this subscription publishes. */ + public String getTopic() { + return topic; + } + + /** Returns the canonical URI of this topic as a string. */ + public String getSelfLink() { + return selfLink; + } + + /** Returns the desired content of the Payload. */ + public PayloadFormat getPayloadFormat() { + return payloadFormat; + } + + /** Returns the object name prefix for which this notification configuration applies. */ + public String getObjectNamePrefix() { + return objectNamePrefix; + } + + /** + * Returns HTTP 1.1 Entity tag for the notification. + * + * @see Entity Tags + */ + public String getEtag() { + return etag; + } + + /** + * Returns the list of event types that this notification will apply to. If empty, notifications + * will be sent on all event types. + * + * @see Cross-Origin Resource Sharing + * (CORS) + */ + public List getEventTypes() { + return eventTypes; + } + + /** + * Returns the list of additional attributes to attach to each Cloud PubSub message published for + * this notification subscription. + * + * @see + * About Access Control Lists + */ + public Map getCustomAttributes() { + return customAttributes; } @Override - public Builder toBuilder() { - return new Notification.Builder(this); + public int hashCode() { + return Objects.hash(getTopic()); } @Override - public boolean equals(Object o) { - if (this == o) { - return true; + public boolean equals(Object obj) { + return obj == this + || obj != null + && obj.getClass().equals(Notification.class) + && Objects.equals(toPb(), ((Notification) obj).toPb()); + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this).add("topic", getTopic()).toString(); + } + + com.google.api.services.storage.model.Notification toPb() { + com.google.api.services.storage.model.Notification notificationPb = + new com.google.api.services.storage.model.Notification(); + notificationPb.setId(generatedId); + notificationPb.setEtag(etag); + if (customAttributes != null) { + notificationPb.setCustomAttributes(customAttributes); + } + if (eventTypes != null) { + notificationPb.setEventTypes(eventTypes); } - if (o == null || getClass() != o.getClass()) { - return false; + if (objectNamePrefix != null) { + notificationPb.setObjectNamePrefix(objectNamePrefix); } - if (!super.equals(o)) { - return false; + if (payloadFormat != null) { + notificationPb.setPayloadFormat(payloadFormat.toString()); + } else { + notificationPb.setPayloadFormat(PayloadFormat.NONE.toString()); } - Notification that = (Notification) o; - return Objects.equals(toPb(), that.toPb()) && Objects.equals(options, that.options); + notificationPb.setSelfLink(selfLink); + notificationPb.setTopic(topic); + + return notificationPb; } - @Override - public int hashCode() { - return Objects.hash(super.hashCode(), options, storage); + /** + * Creates a {@code Notification} object for the provided topic name. + * + * @param topic The name of the topic. It must have the format + * "projects/{project}/topics/{topic}". + */ + public static Notification of(String topic) { + PATH_TEMPLATE.validatedMatch(topic, "topic name must be in valid format"); + return newBuilder(topic).build(); + } + + /** Returns a builder for the current notification. */ + public Builder toBuilder() { + return new Builder(this); + } + + /** + * Returns a {@code Notification} 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(String topic) { + PATH_TEMPLATE.validatedMatch(topic, "topic name must be in valid format"); + return new Builder(topic); } - static Notification fromPb( - Storage storage, com.google.api.services.storage.model.Notification notificationPb) { - return new Notification( - storage, new NotificationInfo.BuilderImpl(NotificationInfo.fromPb(notificationPb))); + static Notification fromPb(com.google.api.services.storage.model.Notification notificationPb) { + Builder builder = newBuilder(notificationPb.getTopic()); + if (notificationPb.getId() != null) { + builder.setGeneratedId(notificationPb.getId()); + } + if (notificationPb.getEtag() != null) { + builder.setEtag(notificationPb.getEtag()); + } + if (notificationPb.getCustomAttributes() != null) { + builder.setCustomAttributes(notificationPb.getCustomAttributes()); + } + if (notificationPb.getSelfLink() != null) { + builder.setSelfLink(notificationPb.getSelfLink()); + } + if (notificationPb.getObjectNamePrefix() != null) { + builder.setObjectNamePrefix(notificationPb.getObjectNamePrefix()); + } + if (notificationPb.getTopic() != null) { + builder.setTopic(notificationPb.getTopic()); + } + if (notificationPb.getEventTypes() != null) { + builder.setEventTypes(notificationPb.getEventTypes()); + } + if (notificationPb.getPayloadFormat() != null) { + builder.setPayloadFormat(PayloadFormat.valueOf(notificationPb.getPayloadFormat())); + } + return builder.build(); } } diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/NotificationInfo.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/NotificationInfo.java deleted file mode 100644 index d9653fec7..000000000 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/NotificationInfo.java +++ /dev/null @@ -1,342 +0,0 @@ -/* - * Copyright 2020 Google LLC - * - * 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.storage; - -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 java.io.Serializable; -import java.util.List; -import java.util.Map; -import java.util.Objects; - -/** - * Google Storage Notification metadata; - * - * @see Concepts and - * Terminology - */ -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, - NONE - } - - static final Function FROM_PB_FUNCTION = - new Function() { - @Override - public NotificationInfo apply(Notification pb) { - return NotificationInfo.fromPb(pb); - } - }; - static final Function TO_PB_FUNCTION = - new Function() { - @Override - public Notification apply(NotificationInfo NotificationInfo) { - return NotificationInfo.toPb(); - } - }; - private final String generatedId; - private final String topic; - private final List eventTypes; - private final Map customAttributes; - private final PayloadFormat payloadFormat; - private final String objectNamePrefix; - private final String etag; - private final String selfLink; - - /** Builder for {@code NotificatioInfo}. */ - public abstract static class Builder { - Builder() {} - - public abstract Builder setGeneratedId(String generatedId); - - public abstract Builder setSelfLink(String selfLink); - - public abstract Builder setTopic(String topic); - - public abstract Builder setPayloadFormat(PayloadFormat payloadFormat); - - public abstract Builder setObjectNamePrefix(String objectNamePrefix); - - public abstract Builder setEventTypes(Iterable eventTypes); - - public abstract Builder setEtag(String etag); - - public abstract Builder setCustomAttributes(Map customAttributes); - - /** Creates a {@code NotificationInfo} object. */ - public abstract NotificationInfo build(); - } - - public static final class BuilderImpl extends Builder { - - private String generatedId; - private String topic; - private List eventTypes; - private Map customAttributes; - private PayloadFormat payloadFormat; - private String objectNamePrefix; - private String etag; - private String selfLink; - - BuilderImpl(String topic) { - this.topic = topic; - } - - BuilderImpl(NotificationInfo NotificationInfo) { - generatedId = NotificationInfo.generatedId; - etag = NotificationInfo.etag; - selfLink = NotificationInfo.selfLink; - topic = NotificationInfo.topic; - eventTypes = NotificationInfo.eventTypes; - customAttributes = NotificationInfo.customAttributes; - payloadFormat = NotificationInfo.payloadFormat; - objectNamePrefix = NotificationInfo.objectNamePrefix; - } - - @Override - public Builder setGeneratedId(String generatedId) { - this.generatedId = generatedId; - return this; - } - - @Override - public Builder setSelfLink(String selfLink) { - this.selfLink = selfLink; - return this; - } - - /** The name of the topic. It must have the format "projects/{project}/topics/{topic}". */ - @Override - public Builder setTopic(String topic) { - this.topic = topic; - return this; - } - - @Override - public Builder setPayloadFormat(PayloadFormat payloadFormat) { - this.payloadFormat = payloadFormat; - return this; - } - - @Override - public Builder setObjectNamePrefix(String objectNamePrefix) { - this.objectNamePrefix = objectNamePrefix; - return this; - } - - @Override - public Builder setEventTypes(Iterable eventTypes) { - this.eventTypes = eventTypes != null ? ImmutableList.copyOf(eventTypes) : null; - return this; - } - - @Override - public Builder setEtag(String etag) { - this.etag = etag; - return this; - } - - @Override - public Builder setCustomAttributes(Map customAttributes) { - this.customAttributes = - customAttributes != null ? ImmutableMap.copyOf(customAttributes) : null; - return this; - } - - @Override - public NotificationInfo build() { - checkNotNull(topic); - return new NotificationInfo(this); - } - } - - NotificationInfo(BuilderImpl builder) { - generatedId = builder.generatedId; - etag = builder.etag; - selfLink = builder.selfLink; - topic = builder.topic; - eventTypes = builder.eventTypes; - customAttributes = builder.customAttributes; - payloadFormat = builder.payloadFormat; - objectNamePrefix = builder.objectNamePrefix; - } - - /** Returns the service-generated id for the notification. */ - public String getGeneratedId() { - return generatedId; - } - - /** Returns the topic to which this subscription publishes. */ - public String getTopic() { - return topic; - } - - /** Returns the canonical URI of this topic as a string. */ - public String getSelfLink() { - return selfLink; - } - - /** Returns the desired content of the Payload. */ - public PayloadFormat getPayloadFormat() { - return payloadFormat; - } - - /** Returns the object name prefix for which this notification configuration applies. */ - public String getObjectNamePrefix() { - return objectNamePrefix; - } - - /** - * Returns HTTP 1.1 Entity tag for the notification. - * - * @see Entity Tags - */ - public String getEtag() { - return etag; - } - - /** - * Returns the list of event types that this notification will apply to. If empty, notifications - * will be sent on all event types. - * - * @see Cross-Origin Resource Sharing - * (CORS) - */ - public List getEventTypes() { - return eventTypes; - } - - /** - * Returns the list of additional attributes to attach to each Cloud PubSub message published for - * this notification subscription. - * - * @see - * About Access Control Lists - */ - public Map getCustomAttributes() { - return customAttributes; - } - - /** Returns a builder for the current notification. */ - public Builder toBuilder() { - return new BuilderImpl(this); - } - - @Override - public int hashCode() { - return Objects.hash(getTopic()); - } - - @Override - public boolean equals(Object obj) { - return obj == this - || obj != null - && obj.getClass().equals(NotificationInfo.class) - && Objects.equals(toPb(), ((NotificationInfo) obj).toPb()); - } - - @Override - public String toString() { - return MoreObjects.toStringHelper(this).add("topic", getTopic()).toString(); - } - - Notification toPb() { - Notification notificationPb = new Notification(); - notificationPb.setId(generatedId); - notificationPb.setEtag(etag); - if (customAttributes != null) { - notificationPb.setCustomAttributes(customAttributes); - } - if (eventTypes != null) { - notificationPb.setEventTypes(eventTypes); - } - if (objectNamePrefix != null) { - notificationPb.setObjectNamePrefix(objectNamePrefix); - } - if (payloadFormat != null) { - notificationPb.setPayloadFormat(payloadFormat.toString()); - } else { - notificationPb.setPayloadFormat(PayloadFormat.NONE.toString()); - } - notificationPb.setSelfLink(selfLink); - notificationPb.setTopic(topic); - - return notificationPb; - } - - /** - * 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(String topic) { - PATH_TEMPLATE.validatedMatch(topic, "topic name must be in valid format"); - return new BuilderImpl(topic); - } - - static NotificationInfo fromPb(Notification notificationPb) { - Builder builder = newBuilder(notificationPb.getTopic()); - if (notificationPb.getId() != null) { - builder.setGeneratedId(notificationPb.getId()); - } - if (notificationPb.getEtag() != null) { - builder.setEtag(notificationPb.getEtag()); - } - if (notificationPb.getCustomAttributes() != null) { - builder.setCustomAttributes(notificationPb.getCustomAttributes()); - } - if (notificationPb.getSelfLink() != null) { - builder.setSelfLink(notificationPb.getSelfLink()); - } - if (notificationPb.getObjectNamePrefix() != null) { - builder.setObjectNamePrefix(notificationPb.getObjectNamePrefix()); - } - if (notificationPb.getTopic() != null) { - builder.setTopic(notificationPb.getTopic()); - } - if (notificationPb.getEventTypes() != null) { - builder.setEventTypes(notificationPb.getEventTypes()); - } - if (notificationPb.getPayloadFormat() != null) { - builder.setPayloadFormat(PayloadFormat.valueOf(notificationPb.getPayloadFormat())); - } - return builder.build(); - } -} diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java index d0cc60518..34761ef90 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/Storage.java @@ -3627,33 +3627,40 @@ List testIamPermissions( ServiceAccount getServiceAccount(String projectId); /** - * Creates a notification with the specified entity on the specified bucket. + * Creates a notification subscription for a given bucket. * - * @return the notification that was created. + * @param bucket name of the bucket + * @param notification a {@code Notification} object + * @return the notification that was created * @throws StorageException upon failure */ - Notification createNotification(String bucket, NotificationInfo notificationInfo); + Notification createNotification(String bucket, Notification notification); /** - * Get the notification with the specified name on the specified object. + * Gets the notification with the specified name on the bucket. * - * @return the notification object that exist on the bucket. + * @param bucket name of the bucket + * @param notification notification ID + * @return the notification object that exist on the bucket or {@code null} if not found * @throws StorageException upon failure */ Notification getNotification(String bucket, String notification); /** - * List the notifications for the provided bucket. + * Retrieves a list of notification subscriptions for a given bucket. * + * @param bucket name of the bucket * @return a list of {@link Notification} objects that exist on the bucket. * @throws StorageException upon failure */ List listNotifications(String bucket); /** - * Deletes the notification with the specified name on the specified object. + * Deletes the notification subscription with the specified name on the bucket. * - * @return {@code true} if the notification was deleted, {@code false} if it was not found + * @param bucket name of the bucket + * @param notification ID of the notification to delete + * @return {@code true} if the notification has been deleted, {@code false} if not found * @throws StorageException upon failure */ boolean deleteNotification(String bucket, String notification); diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java index a4345e572..1cbf2957d 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/StorageImpl.java @@ -1375,13 +1375,10 @@ private U run(ResultRetryAlgorithm algorithm, Callable c, Function< } @Override - public Notification createNotification( - final String bucket, final NotificationInfo notificationInfo) { - final com.google.api.services.storage.model.Notification notificationPb = - notificationInfo.toPb(); + public Notification createNotification(final String bucket, final Notification notification) { + final com.google.api.services.storage.model.Notification notificationPb = notification.toPb(); try { return Notification.fromPb( - this, runWithRetries( new Callable() { @Override @@ -1411,7 +1408,7 @@ public com.google.api.services.storage.model.Notification call() { getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); - return answer == null ? null : Notification.fromPb(this, answer); + return answer == null ? null : Notification.fromPb(answer); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); } @@ -1431,17 +1428,7 @@ public List call() { getOptions().getRetrySettings(), EXCEPTION_HANDLER, getOptions().getClock()); - return answer == null - ? null - : Lists.transform( - answer, - new Function() { - @Override - public Notification apply( - com.google.api.services.storage.model.Notification notificationPb) { - return Notification.fromPb(getOptions().getService(), notificationPb); - } - }); + return Lists.transform(answer, Notification.FROM_PB_FUNCTION); } catch (RetryHelperException e) { throw StorageException.translateAndThrow(e); } diff --git a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java index f9fbb1b51..932c11581 100644 --- a/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java +++ b/google-cloud-storage/src/main/java/com/google/cloud/storage/spi/v1/StorageRpc.java @@ -561,15 +561,15 @@ TestIamPermissionsResponse testIamPermissions( String bucket, List permissions, Map options); /** - * Deletes the notification with the specified name on the specified object. + * Deletes the notification subscription with the specified name on the bucket. * - * @return {@code true} if the notification was deleted, {@code false} if it was not found + * @return {@code true} if the notification has been deleted, {@code false} if not found * @throws StorageException upon failure */ boolean deleteNotification(String bucket, String notification); /** - * List the notifications for the provided bucket. + * Retrieves a list of notification subscriptions for a given bucket. * * @return a list of {@link Notification} objects that exist on the bucket. * @throws StorageException upon failure @@ -577,7 +577,7 @@ TestIamPermissionsResponse testIamPermissions( List listNotifications(String bucket); /** - * Creates a notification with the specified entity on the specified bucket. + * Creates a notification subscription for a given bucket. * * @return the notification that was created. * @throws StorageException upon failure @@ -585,7 +585,7 @@ TestIamPermissionsResponse testIamPermissions( Notification createNotification(String bucket, Notification notification); /** - * Get the notification with the specified name on the specified object. + * Gets the notification with the specified name on the bucket. * * @return the notification object that exist on the bucket. * @throws StorageException upon failure diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/NotificationInfoTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/NotificationInfoTest.java deleted file mode 100644 index 40b8a5ccf..000000000 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/NotificationInfoTest.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright 2020 Google LLC - * - * 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.storage; - -import static org.junit.Assert.assertEquals; - -import com.google.cloud.storage.NotificationInfo.PayloadFormat; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import java.util.List; -import java.util.Map; -import org.junit.Test; - -public class NotificationInfoTest { - - private static final String ETAG = "0xFF00"; - private static final String GENERATED_ID = "B/N:1"; - private static final String SELF_LINK = "http://storage/b/n"; - private static final List EVENT_TYPES = - 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 String TOPIC = "projects/myProject/topics/topic1"; - private static final Map CUSTOM_ATTRIBUTES = ImmutableMap.of("label1", "value1"); - private static final NotificationInfo NOTIFICATION_INFO = - NotificationInfo.newBuilder(TOPIC) - .setEtag(ETAG) - .setCustomAttributes(CUSTOM_ATTRIBUTES) - .setSelfLink(SELF_LINK) - .setEventTypes(EVENT_TYPES) - .setObjectNamePrefix(OBJECT_NAME_PREFIX) - .setPayloadFormat(PAYLOAD_FORMAT) - .setGeneratedId(GENERATED_ID) - .build(); - - @Test - public void testToBuilder() { - compareBuckets(NOTIFICATION_INFO, NOTIFICATION_INFO.toBuilder().build()); - NotificationInfo bucketInfo = NOTIFICATION_INFO.toBuilder().setGeneratedId("id").build(); - assertEquals("id", bucketInfo.getGeneratedId()); - bucketInfo = bucketInfo.toBuilder().setGeneratedId(GENERATED_ID).build(); - compareBuckets(NOTIFICATION_INFO, bucketInfo); - } - - @Test - public void testToBuilderIncomplete() { - NotificationInfo incompleteBucketInfo = NotificationInfo.newBuilder(TOPIC).build(); - compareBuckets(incompleteBucketInfo, incompleteBucketInfo.toBuilder().build()); - } - - @Test - public void testOf() { - NotificationInfo bucketInfo = NotificationInfo.of(TOPIC); - assertEquals(TOPIC, bucketInfo.getTopic()); - } - - @Test - public void testBuilder() { - assertEquals(ETAG, NOTIFICATION_INFO.getEtag()); - assertEquals(GENERATED_ID, NOTIFICATION_INFO.getGeneratedId()); - assertEquals(SELF_LINK, NOTIFICATION_INFO.getSelfLink()); - assertEquals(EVENT_TYPES, NOTIFICATION_INFO.getEventTypes()); - assertEquals(OBJECT_NAME_PREFIX, NOTIFICATION_INFO.getObjectNamePrefix()); - assertEquals(PAYLOAD_FORMAT, NOTIFICATION_INFO.getPayloadFormat()); - assertEquals(TOPIC, NOTIFICATION_INFO.getTopic()); - assertEquals(CUSTOM_ATTRIBUTES, NOTIFICATION_INFO.getCustomAttributes()); - } - - @Test - public void testToPbAndFromPb() { - compareBuckets(NOTIFICATION_INFO, NotificationInfo.fromPb(NOTIFICATION_INFO.toPb())); - NotificationInfo bucketInfo = - NotificationInfo.of(TOPIC).toBuilder().setPayloadFormat(PayloadFormat.NONE).build(); - compareBuckets(bucketInfo, NotificationInfo.fromPb(bucketInfo.toPb())); - } - - private void compareBuckets(NotificationInfo expected, NotificationInfo value) { - assertEquals(expected, value); - assertEquals(expected.getGeneratedId(), value.getGeneratedId()); - assertEquals(expected.getCustomAttributes(), value.getCustomAttributes()); - assertEquals(expected.getEtag(), value.getEtag()); - assertEquals(expected.getSelfLink(), value.getSelfLink()); - assertEquals(expected.getEventTypes(), value.getEventTypes()); - assertEquals(expected.getObjectNamePrefix(), value.getObjectNamePrefix()); - assertEquals(expected.getPayloadFormat(), value.getPayloadFormat()); - assertEquals(expected.getTopic(), value.getTopic()); - } -} diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/NotificationTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/NotificationTest.java index 87f1a37b7..abfa97b98 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/NotificationTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/NotificationTest.java @@ -16,24 +16,14 @@ package com.google.cloud.storage; -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.createStrictMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; import static org.junit.Assert.assertEquals; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import java.util.List; import java.util.Map; -import org.junit.After; -import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; -@RunWith(JUnit4.class) public class NotificationTest { private static final String ETAG = "0xFF00"; @@ -42,12 +32,12 @@ public class NotificationTest { private static final List EVENT_TYPES = ImmutableList.of("OBJECT_FINALIZE", "OBJECT_METADATA_UPDATE"); 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 Notification.PayloadFormat PAYLOAD_FORMAT = + Notification.PayloadFormat.JSON_API_V1.JSON_API_V1; private static final String TOPIC = "projects/myProject/topics/topic1"; private static final Map CUSTOM_ATTRIBUTES = ImmutableMap.of("label1", "value1"); - private static final NotificationInfo FULL_NOTIFICATION_INFO = - NotificationInfo.newBuilder(TOPIC) + private static final Notification NOTIFICATION = + Notification.newBuilder(TOPIC) .setEtag(ETAG) .setCustomAttributes(CUSTOM_ATTRIBUTES) .setSelfLink(SELF_LINK) @@ -56,74 +46,60 @@ public class NotificationTest { .setPayloadFormat(PAYLOAD_FORMAT) .setGeneratedId(GENERATED_ID) .build(); - private static final NotificationInfo NOTIFICATION_INFO = - NotificationInfo.newBuilder(TOPIC).build(); - private Storage storage; - private StorageOptions mockOptions = createMock(StorageOptions.class); - - @Before - public void setUp() { - storage = createStrictMock(Storage.class); + @Test + public void testToBuilder() { + compareBucketsNotification(NOTIFICATION, NOTIFICATION.toBuilder().build()); + Notification notification = NOTIFICATION.toBuilder().setGeneratedId("id").build(); + assertEquals("id", notification.getGeneratedId()); + notification = notification.toBuilder().setGeneratedId(GENERATED_ID).build(); + compareBucketsNotification(NOTIFICATION, notification); } - @After - public void tearDown() { - verify(storage); + @Test + public void testToBuilderIncomplete() { + Notification incompleteNotification = Notification.newBuilder(TOPIC).build(); + compareBucketsNotification(incompleteNotification, incompleteNotification.toBuilder().build()); } @Test - public void testBuilder() { - expect(storage.getOptions()).andReturn(mockOptions).times(2); - replay(storage); - Notification.Builder builder = - new Notification.Builder( - new Notification(storage, new NotificationInfo.BuilderImpl(NOTIFICATION_INFO))); - Notification notification = - builder - .setEtag(ETAG) - .setCustomAttributes(CUSTOM_ATTRIBUTES) - .setSelfLink(SELF_LINK) - .setEventTypes(EVENT_TYPES) - .setObjectNamePrefix(OBJECT_NAME_PREFIX) - .setPayloadFormat(PAYLOAD_FORMAT) - .setGeneratedId(GENERATED_ID) - .build(); - assertEquals(ETAG, notification.getEtag()); - assertEquals(GENERATED_ID, notification.getGeneratedId()); - assertEquals(SELF_LINK, notification.getSelfLink()); - assertEquals(EVENT_TYPES, notification.getEventTypes()); - assertEquals(OBJECT_NAME_PREFIX, notification.getObjectNamePrefix()); - assertEquals(PAYLOAD_FORMAT, notification.getPayloadFormat()); + public void testOf() { + Notification notification = Notification.of(TOPIC); assertEquals(TOPIC, notification.getTopic()); - assertEquals(CUSTOM_ATTRIBUTES, notification.getCustomAttributes()); } @Test - public void testToBuilder() { - expect(storage.getOptions()).andReturn(mockOptions).times(2); - replay(storage); - Notification notification = - new Notification(storage, new NotificationInfo.BuilderImpl(FULL_NOTIFICATION_INFO)); - compareBuckets(notification, notification.toBuilder().build()); + public void testBuilder() { + assertEquals(ETAG, NOTIFICATION.getEtag()); + assertEquals(GENERATED_ID, NOTIFICATION.getGeneratedId()); + assertEquals(SELF_LINK, NOTIFICATION.getSelfLink()); + assertEquals(EVENT_TYPES, NOTIFICATION.getEventTypes()); + assertEquals(OBJECT_NAME_PREFIX, NOTIFICATION.getObjectNamePrefix()); + assertEquals(PAYLOAD_FORMAT, NOTIFICATION.getPayloadFormat()); + assertEquals(TOPIC, NOTIFICATION.getTopic()); + assertEquals(CUSTOM_ATTRIBUTES, NOTIFICATION.getCustomAttributes()); } @Test - public void testFromPb() { - expect(storage.getOptions()).andReturn(mockOptions).times(1); - replay(storage); - compareBuckets( - FULL_NOTIFICATION_INFO, Notification.fromPb(storage, FULL_NOTIFICATION_INFO.toPb())); + public void testToPbAndFromPb() { + compareBucketsNotification(NOTIFICATION, Notification.fromPb(NOTIFICATION.toPb())); + Notification notification = + Notification.of(TOPIC) + .toBuilder() + .setPayloadFormat(Notification.PayloadFormat.NONE) + .build(); + compareBucketsNotification(notification, Notification.fromPb(notification.toPb())); } - private void compareBuckets(NotificationInfo expected, NotificationInfo value) { - assertEquals(expected.getGeneratedId(), value.getGeneratedId()); - assertEquals(expected.getCustomAttributes(), value.getCustomAttributes()); - assertEquals(expected.getEtag(), value.getEtag()); - assertEquals(expected.getSelfLink(), value.getSelfLink()); - assertEquals(expected.getEventTypes(), value.getEventTypes()); - assertEquals(expected.getObjectNamePrefix(), value.getObjectNamePrefix()); - assertEquals(expected.getPayloadFormat(), value.getPayloadFormat()); - assertEquals(expected.getTopic().trim(), value.getTopic().trim()); + private void compareBucketsNotification(Notification expected, Notification actual) { + assertEquals(expected, actual); + assertEquals(expected.getGeneratedId(), actual.getGeneratedId()); + assertEquals(expected.getCustomAttributes(), actual.getCustomAttributes()); + assertEquals(expected.getEtag(), actual.getEtag()); + assertEquals(expected.getSelfLink(), actual.getSelfLink()); + assertEquals(expected.getEventTypes(), actual.getEventTypes()); + assertEquals(expected.getObjectNamePrefix(), actual.getObjectNamePrefix()); + assertEquals(expected.getPayloadFormat(), actual.getPayloadFormat()); + assertEquals(expected.getTopic(), actual.getTopic()); } } diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplMockitoTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplMockitoTest.java index 7ff8372fb..d038e832d 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplMockitoTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/StorageImplMockitoTest.java @@ -374,12 +374,12 @@ public long millisTime() { private static final List EVENT_TYPES = ImmutableList.of("OBJECT_FINALIZE", "OBJECT_METADATA_UPDATE"); 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 Notification.PayloadFormat PAYLOAD_FORMAT = + Notification.PayloadFormat.JSON_API_V1.JSON_API_V1; private static final String TOPIC = "projects/myProject/topics/topic1"; private static final Map CUSTOM_ATTRIBUTES = ImmutableMap.of("label1", "value1"); - private static final NotificationInfo NOTIFICATION_INFO_01 = - NotificationInfo.newBuilder(TOPIC) + private static final Notification NOTIFICATION_01 = + Notification.newBuilder(TOPIC) .setEtag(ETAG) .setCustomAttributes(CUSTOM_ATTRIBUTES) .setSelfLink(SELF_LINK) @@ -388,8 +388,8 @@ public long millisTime() { .setPayloadFormat(PAYLOAD_FORMAT) .setGeneratedId(GENERATED_ID) .build(); - private static final NotificationInfo NOTIFICATION_INFO_02 = - NotificationInfo.newBuilder(TOPIC) + private static final Notification NOTIFICATION_02 = + Notification.newBuilder(TOPIC) .setEtag(ETAG) .setCustomAttributes(CUSTOM_ATTRIBUTES) .setSelfLink(SELF_LINK) @@ -1679,17 +1679,17 @@ public void testWriterFailure() { @Test public void testCreateNotification() { - doReturn(NOTIFICATION_INFO_01.toPb()) + doReturn(NOTIFICATION_01.toPb()) .when(storageRpcMock) - .createNotification(BUCKET_NAME1, NOTIFICATION_INFO_01.toPb()); + .createNotification(BUCKET_NAME1, NOTIFICATION_01.toPb()); initializeService(); - Notification notification = storage.createNotification(BUCKET_NAME1, NOTIFICATION_INFO_01); + Notification notification = storage.createNotification(BUCKET_NAME1, NOTIFICATION_01); compareBucketsNotification(notification); } @Test public void testGetNotification() { - doReturn(NOTIFICATION_INFO_01.toPb()) + doReturn(NOTIFICATION_01.toPb()) .when(storageRpcMock) .getNotification(BUCKET_NAME1, GENERATED_ID); initializeService(); @@ -1699,7 +1699,7 @@ public void testGetNotification() { @Test public void testListNotification() { - doReturn(Arrays.asList(NOTIFICATION_INFO_01.toPb(), NOTIFICATION_INFO_02.toPb())) + doReturn(Arrays.asList(NOTIFICATION_01.toPb(), NOTIFICATION_02.toPb())) .when(storageRpcMock) .listNotifications(BUCKET_NAME1); initializeService(); diff --git a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java index af8e47d3e..538752a6a 100644 --- a/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java +++ b/google-cloud-storage/src/test/java/com/google/cloud/storage/it/ITStorageTest.java @@ -74,7 +74,6 @@ import com.google.cloud.storage.HmacKey.HmacKeyState; import com.google.cloud.storage.HttpMethod; import com.google.cloud.storage.Notification; -import com.google.cloud.storage.NotificationInfo; import com.google.cloud.storage.PostPolicyV4; import com.google.cloud.storage.PostPolicyV4.PostFieldsV4; import com.google.cloud.storage.Rpo; @@ -231,8 +230,8 @@ public class ITStorageTest { private static final String ID = UUID.randomUUID().toString().substring(0, 8); private static final String TOPIC = String.format("projects/%s/topics/test_topic_foo_%s", PROJECT, ID); - private static final NotificationInfo.PayloadFormat PAYLOAD_FORMAT = - NotificationInfo.PayloadFormat.JSON_API_V1.JSON_API_V1; + private static final Notification.PayloadFormat PAYLOAD_FORMAT = + Notification.PayloadFormat.JSON_API_V1.JSON_API_V1; private static final Map CUSTOM_ATTRIBUTES = ImmutableMap.of("label1", "value1"); @BeforeClass @@ -454,12 +453,12 @@ private static Notification createNotification(TopicAdminClient topicAdminClient topicAdminClient.setIamPolicy(setIamPolicyRequest); // Create a notification on a bucket. - NotificationInfo notificationInfo = - NotificationInfo.newBuilder(TOPIC) + Notification bucketNotification = + Notification.newBuilder(TOPIC) .setCustomAttributes(CUSTOM_ATTRIBUTES) .setPayloadFormat(PAYLOAD_FORMAT) .build(); - notification = storage.createNotification(BUCKET, notificationInfo); + notification = storage.createNotification(BUCKET, bucketNotification); return notification; }