Skip to content

Commit

Permalink
add serialization test
Browse files Browse the repository at this point in the history
  • Loading branch information
aozarov committed May 13, 2015
1 parent 6c59a54 commit 17ec594
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 34 deletions.
25 changes: 19 additions & 6 deletions src/main/java/com/google/gcloud/storage/Acl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.api.services.storage.model.ObjectAccessControl;

import java.io.Serializable;
import java.util.Objects;

/**
* Access Control List on for buckets or blobs.
Expand Down Expand Up @@ -59,6 +60,19 @@ protected String value() {
return value;
}

@Override
public int hashCode() {
return Objects.hash(type, value);
}

@Override
public boolean equals(Object obj) {
if (obj == null || !getClass().isAssignableFrom(obj.getClass())) {
return false;
}
return Objects.equals(toPb(), ((Entity)obj).toPb());
}

@Override
public String toString() {
return toPb();
Expand Down Expand Up @@ -91,21 +105,20 @@ static Entity fromPb(String entity) {
}
}

public static class Domain extends Entity {
public static final class Domain extends Entity {

private static final long serialVersionUID = -3033025857280447253L;

public Domain(String domain) {
super(Type.DOMAIN, domain);

}

public String domain() {
return value();
}
}

public static class Group extends Entity {
public static final class Group extends Entity {

private static final long serialVersionUID = -1660987136294408826L;

Expand All @@ -118,7 +131,7 @@ public String email() {
}
}

public static class User extends Entity {
public static final class User extends Entity {

private static final long serialVersionUID = 3076518036392737008L;
private static final String ALL_USERS = "allUsers";
Expand Down Expand Up @@ -154,7 +167,7 @@ public static User ofAllAuthenticatedUsers() {
}
}

public static class Project extends Entity {
public static final class Project extends Entity {

private static final long serialVersionUID = 7933776866530023027L;

Expand All @@ -180,7 +193,7 @@ public String projectId() {
}
}

public static class RawEntity extends Entity {
public static final class RawEntity extends Entity {

private static final long serialVersionUID = 3966205614223053950L;

Expand Down
53 changes: 37 additions & 16 deletions src/main/java/com/google/gcloud/storage/BatchRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,56 @@
package com.google.gcloud.storage;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.gcloud.storage.StorageService.BlobSourceOption;
import com.google.gcloud.storage.StorageService.BlobTargetOption;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

/**
* Google storage batch request.
*/
public class BatchRequest implements Serializable {
public final class BatchRequest implements Serializable {

private static final long serialVersionUID = -1527992265939800345L;

private final Map<Blob, BlobSourceOption[]> toDelete;
private final Map<Blob, BlobTargetOption[]> toUpdate;
private final Map<Blob, BlobSourceOption[]> toGet;
private final Map<Blob, Iterable<BlobSourceOption>> toDelete;
private final Map<Blob, Iterable<BlobTargetOption>> toUpdate;
private final Map<Blob, Iterable<BlobSourceOption>> toGet;

public static class Builder {

private Map<Blob, BlobSourceOption[]> toDelete = new LinkedHashMap<>();
private Map<Blob, BlobTargetOption[]> toUpdate = new LinkedHashMap<>();
private Map<Blob, BlobSourceOption[]> toGet = new LinkedHashMap<>();
private Map<Blob, Iterable<BlobSourceOption>> toDelete = new LinkedHashMap<>();
private Map<Blob, Iterable<BlobTargetOption>> toUpdate = new LinkedHashMap<>();
private Map<Blob, Iterable<BlobSourceOption>> toGet = new LinkedHashMap<>();

private Builder() {}

/**
* Delete the given blob.
*/
public void delete(String bucket, String blob, BlobSourceOption... options) {
toDelete.put(Blob.of(bucket, blob), options);
public Builder delete(String bucket, String blob, BlobSourceOption... options) {
toDelete.put(Blob.of(bucket, blob), Lists.newArrayList(options));
return this;
}

/**
* Update the given blob.
*/
public void update(Blob blob, BlobTargetOption... options) {
toUpdate.put(blob, options);
public Builder update(Blob blob, BlobTargetOption... options) {
toUpdate.put(blob, Lists.newArrayList(options));
return this;
}

/**
* Retrieve metadata for the given blob.
*/
public void get(String bucket, String blob, BlobSourceOption... options) {
toGet.put(Blob.of(bucket, blob), options);
public Builder get(String bucket, String blob, BlobSourceOption... options) {
toGet.put(Blob.of(bucket, blob), Lists.newArrayList(options));
return this;
}

public BatchRequest build() {
Expand All @@ -75,15 +80,31 @@ private BatchRequest(Builder builder) {
toGet = ImmutableMap.copyOf(builder.toGet);
}

Map<Blob, BlobSourceOption[]> toDelete() {
@Override
public int hashCode() {
return Objects.hash(toDelete, toUpdate, toGet);
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof BatchRequest)) {
return false;
}
BatchRequest other = (BatchRequest) obj;
return Objects.equals(toDelete, other.toDelete)
&& Objects.equals(toUpdate, other.toUpdate)
&& Objects.equals(toGet, other.toGet);
}

Map<Blob, Iterable<BlobSourceOption>> toDelete() {
return toDelete;
}

Map<Blob, BlobTargetOption[]> toUpdate() {
Map<Blob, Iterable<BlobTargetOption>> toUpdate() {
return toUpdate;
}

Map<Blob, BlobSourceOption[]> toGet() {
Map<Blob, Iterable<BlobSourceOption>> toGet() {
return toGet;
}

Expand Down
34 changes: 33 additions & 1 deletion src/main/java/com/google/gcloud/storage/BatchResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
* Google Storage batch response.
*/
public class BatchResponse implements Serializable {
public final class BatchResponse implements Serializable {

private static final long serialVersionUID = 1057416839397037706L;

Expand Down Expand Up @@ -78,6 +79,21 @@ public boolean failed() {
return exception != null;
}

@Override
public int hashCode() {
return Objects.hash(value, exception);
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof Result)) {
return false;
}
Result<?> other = (Result<?>) obj;
return Objects.equals(value, other.value)
&& Objects.equals(exception, other.exception);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
Expand All @@ -99,6 +115,22 @@ static <T extends Serializable> Result<T> empty() {
this.getResult = ImmutableList.copyOf(getResult);
}

@Override
public int hashCode() {
return Objects.hash(deleteResult, updateResult, getResult);
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof BatchResponse)) {
return false;
}
BatchResponse other = (BatchResponse) obj;
return Objects.equals(deleteResult, other.deleteResult)
&& Objects.equals(updateResult, other.updateResult)
&& Objects.equals(updateResult, other.updateResult);
}

/**
* Returns the results for the delete operations using the request order.
*/
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/com/google/gcloud/storage/Blob.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* A Google Storage object.
*
* @see <a href="https://cloud.google.com/storage/docs/concepts-techniques#concepts">Concepts and Terminology</a>
*/
public class Blob implements Serializable {
public final class Blob implements Serializable {

private static final long serialVersionUID = 2228487739943277159L;

Expand Down Expand Up @@ -386,6 +387,19 @@ public static Builder builder(String bucket, String name) {
return new Builder().bucket(bucket).name(name);
}

@Override
public int hashCode() {
return Objects.hash(bucket, name);
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof Blob)) {
return false;
}
return Objects.equals(toPb(), ((Blob)obj).toPb());
}

StorageObject toPb() {
StorageObject storageObject = new StorageObject();
if (acl != null) {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/google/gcloud/storage/Bucket.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
* A Google Storage bucket.
Expand Down Expand Up @@ -559,6 +560,19 @@ public Builder toBuilder() {
.deleteRules(deleteRules);
}

@Override
public int hashCode() {
return Objects.hash(name);
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof Bucket)) {
return false;
}
return Objects.equals(toPb(), ((Bucket) obj).toPb());
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
Expand Down
33 changes: 32 additions & 1 deletion src/main/java/com/google/gcloud/storage/Cors.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Objects;

/**
* Cross-Origin Resource Sharing (CORS) configuration for a bucket.
Expand Down Expand Up @@ -60,7 +61,7 @@ public enum Method {
ANY, GET, HEAD, PUT, POST, DELETE
}

public static class Origin implements Serializable {
public static final class Origin implements Serializable {

private static final long serialVersionUID = -4447958124895577993L;
private static final String ANY_URI = "*";
Expand Down Expand Up @@ -91,6 +92,19 @@ public static Origin of(String value) {
return new Origin(value);
}

@Override
public int hashCode() {
return value.hashCode();
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof Origin)) {
return false;
}
return value.equals(((Origin)obj).value);
}

@Override
public String toString() {
return value();
Expand Down Expand Up @@ -166,6 +180,23 @@ public Builder toBuilder() {
.responseHeaders(responseHeaders);
}

@Override
public int hashCode() {
return Objects.hash(maxAgeSeconds, methods, origins, responseHeaders);
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof Cors)) {
return false;
}
Cors other = (Cors) obj;
return Objects.equals(maxAgeSeconds, other.maxAgeSeconds)
&& Objects.equals(methods, other.methods)
&& Objects.equals(origins, other.origins)
&& Objects.equals(responseHeaders, other.responseHeaders);
}

public static Builder builder() {
return new Builder();
}
Expand Down
Loading

0 comments on commit 17ec594

Please sign in to comment.