Skip to content

Commit

Permalink
basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aozarov committed May 15, 2015
1 parent b941c08 commit ed351d4
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/google/gcloud/storage/BatchResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public static class Result<T extends Serializable> implements Serializable {
this.value = null;
}

static <T extends Serializable> Result<T> of(T value) {
return new Result<>(value);
}

/**
* Returns the result.
*
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/google/gcloud/storage/ListResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package com.google.gcloud.storage;


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

Expand All @@ -41,7 +43,7 @@ public String nextPageCursor() {

@Override
public Iterator<T> iterator() {
return results.iterator();
return results == null ? Collections.<T>emptyIterator() : results.iterator();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,14 +345,14 @@ private <I, O extends Serializable> List<BatchResponse.Result<O>> transformBatch
for (Tuple<StorageObject, ?> tuple : request) {
Tuple<I, StorageServiceException> result = results.get(tuple.x());
if (result.x() != null) {
response.add(new BatchResponse.Result<>(transform.apply(result.x())));
response.add(BatchResponse.Result.of(transform.apply(result.x())));
} else {
StorageServiceException exception = result.y();
if (nullOnErrorCodesSet.contains(exception.code())) {
//noinspection unchecked
response.add(BatchResponse.Result.<O>empty());
} else {
response.add(new BatchResponse.Result<O>(result.y()));
response.add(new BatchResponse.Result<O>(exception));
}
}
}
Expand Down
32 changes: 24 additions & 8 deletions src/test/java/com/google/gcloud/storage/BatchRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
public class BatchRequestTest {

@Test
public void testRequest() throws Exception {
public void testBatchRequest() {
BatchRequest request = BatchRequest.builder()
.delete("b1", "o1")
.delete("b1", "o2", BlobSourceOption.generationMatch(1),
Expand All @@ -55,15 +55,31 @@ public void testRequest() throws Exception {
assertEquals(2, Iterables.size(delete.getValue()));
assertFalse(deletes.hasNext());

Iterator<Entry<Blob, Iterable<BlobSourceOption>>> updates = request
.toDelete().entrySet().iterator();
Entry<Blob, Iterable<BlobSourceOption>> update = updates.next();
assertEquals(Blob.of("b1", "o1"), update.getKey());
assertTrue(Iterables.isEmpty(update.getValue()));
Iterator<Entry<Blob, Iterable<BlobTargetOption>>> updates = request
.toUpdate().entrySet().iterator();
Entry<Blob, Iterable<BlobTargetOption>> update = updates.next();
assertEquals(Blob.of("b2", "o1"), update.getKey());
assertEquals(1, Iterables.size(update.getValue()));
assertEquals(BlobTargetOption.predefinedAcl(PUBLIC_READ),
Iterables.getFirst(update.getValue(), null));
update = updates.next();
assertEquals(Blob.of("b1", "o2"), update.getKey());
assertEquals(2, Iterables.size(update.getValue()));
assertEquals(Blob.of("b2", "o2"), update.getKey());
assertTrue(Iterables.isEmpty(update.getValue()));
assertFalse(updates.hasNext());

Iterator<Entry<Blob, Iterable<BlobSourceOption>>> gets = request
.toGet().entrySet().iterator();
Entry<Blob, Iterable<BlobSourceOption>> get = gets.next();
assertEquals(Blob.of("b3", "o1"), get.getKey());
assertTrue(Iterables.isEmpty(get.getValue()));
get = gets.next();
assertEquals(Blob.of("b3", "o2"), get.getKey());
assertEquals(1, Iterables.size(get.getValue()));
assertEquals(BlobSourceOption.generationMatch(1),
Iterables.getFirst(get.getValue(), null));
get = gets.next();
assertEquals(Blob.of("b3", "o3"), get.getKey());
assertTrue(Iterables.isEmpty(get.getValue()));
assertFalse(gets.hasNext());
}
}
45 changes: 45 additions & 0 deletions src/test/java/com/google/gcloud/storage/BatchResponseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* 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.gcloud.storage;

import static junit.framework.TestCase.assertEquals;

import com.google.common.collect.ImmutableList;
import com.google.gcloud.storage.BatchResponse.Result;

import org.junit.Test;

import java.util.List;

public class BatchResponseTest {

private static final Blob BLOB1 = Blob.of("b", "o1");
private static final Blob BLOB2 = Blob.of("b", "o2");
private static final Blob BLOB3 = Blob.of("b", "o3");

@Test
public void testBatchResponse() {
List<Result<Boolean>> deletes = ImmutableList.of(Result.of(true), Result.of(false));
List<Result<Blob>> updates = ImmutableList.of(Result.of(BLOB1), Result.of(BLOB2));
List<Result<Blob>> gets = ImmutableList.of(Result.of(BLOB2), Result.of(BLOB3));
BatchResponse response = new BatchResponse(deletes, updates, gets);

assertEquals(deletes, response.deletes());
assertEquals(updates, response.updates());
assertEquals(gets, response.gets());
}
}
55 changes: 55 additions & 0 deletions src/test/java/com/google/gcloud/storage/CorsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* 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.gcloud.storage;

import static junit.framework.TestCase.assertEquals;

import com.google.common.collect.ImmutableList;
import com.google.gcloud.storage.Cors.Method;
import com.google.gcloud.storage.Cors.Origin;

import org.junit.Test;

import java.util.List;

public class CorsTest {

@Test
public void testOrigin() {
assertEquals("bla", Origin.of("bla").value());
assertEquals("http://host:8080", Origin.of("http", "host", 8080).toString());
assertEquals(Origin.of("*"), Origin.any());
}

@Test
public void corsTest() {
List<Origin> origins = ImmutableList.of(Origin.any(), Origin.of("o"));
List<String> headers = ImmutableList.of("h1", "h2");
List<Method> methods = ImmutableList.of(Method.ANY);
Cors cors = Cors.builder()
.maxAgeSeconds(100)
.origins(origins)
.responseHeaders(headers)
.methods(methods)
.build();

assertEquals(Integer.valueOf(100), cors.maxAgeSeconds());
assertEquals(origins, cors.origins());
assertEquals(methods, cors.methods());
assertEquals(headers, cors.responseHeaders());
}
}
34 changes: 34 additions & 0 deletions src/test/java/com/google/gcloud/storage/ListResultTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* 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.gcloud.storage;

import static junit.framework.TestCase.assertEquals;

import com.google.common.collect.ImmutableList;

import org.junit.Test;

public class ListResultTest {

@Test
public void testListResult() throws Exception {
ImmutableList<String> values = ImmutableList.of("1", "2");
ListResult<String> result = new ListResult("c", values);
assertEquals("c", result.nextPageCursor());
assertEquals(values, ImmutableList.copyOf(result.iterator()));
}
}
38 changes: 38 additions & 0 deletions src/test/java/com/google/gcloud/storage/OptionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2015 Google Inc. All Rights Reserved.
*
* 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.gcloud.storage;

import static junit.framework.TestCase.assertEquals;

import com.google.gcloud.spi.StorageRpc;

import org.junit.Test;

public class OptionTest {

@Test
public void testOption() {
Option option = new Option(StorageRpc.Option.DELIMITER, "/");
assertEquals(StorageRpc.Option.DELIMITER, option.rpcOption());
assertEquals("/", option.value());
}

@Test(expected=NullPointerException.class)
public void testIndexOutOfBoundsException() {
new Option(null, "/");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class SerializationTest {
Cors.builder().maxAgeSeconds(1).origins(Collections.singleton(ORIGIN)).build();
private static final BatchRequest BATCH_REQUEST = BatchRequest.builder().delete("B", "N").build();
private static final BatchResponse BATCH_RESPONSE = new BatchResponse(
Collections.singletonList(new BatchResponse.Result<>(true)),
Collections.singletonList(BatchResponse.Result.of(true)),
Collections.<BatchResponse.Result<Blob>>emptyList(),
Collections.<BatchResponse.Result<Blob>>emptyList());
private static final ListResult<Blob> LIST_RESULT =
Expand Down

0 comments on commit ed351d4

Please sign in to comment.