Skip to content

Commit 2f3be49

Browse files
committed
more tests
1 parent ffa1b5b commit 2f3be49

9 files changed

Lines changed: 372 additions & 22 deletions

File tree

src/main/java/com/google/gcloud/datastore/DatastoreServiceOptions.java

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ public class DatastoreServiceOptions extends ServiceOptions {
4646
private final String namespace;
4747
private final boolean force;
4848
private final Datastore datastore;
49+
private final boolean normalizeDataset;
4950

5051
public static class Builder extends ServiceOptions.Builder<Builder> {
5152

5253
private String dataset;
5354
private String namespace;
5455
private boolean force;
5556
private Datastore datastore;
57+
private boolean normalizeDataset = true;
5658

5759
private Builder() {
5860
}
@@ -61,6 +63,9 @@ private Builder(DatastoreServiceOptions options) {
6163
super(options);
6264
dataset = options.dataset;
6365
force = options.force;
66+
namespace = options.namespace;
67+
datastore = options.datastore;
68+
normalizeDataset = options.normalizeDataset;
6469
}
6570

6671
@Override
@@ -87,39 +92,50 @@ public Builder force(boolean force) {
8792
this.force = force;
8893
return this;
8994
}
95+
96+
Builder normalizeDataset(boolean normalizeDataset) {
97+
this.normalizeDataset = normalizeDataset;
98+
return this;
99+
}
90100
}
91101

92102
private DatastoreServiceOptions(Builder builder) {
93103
super(builder);
104+
normalizeDataset = builder.normalizeDataset;
94105
namespace = builder.namespace != null ? builder.namespace : defaultNamespace();
95106
force = builder.force;
96107

97108
// Replace provided dataset with full dataset (s~xxx, e~xxx,...)
98109
String tempDataset = firstNonNull(builder.dataset, defaultDataset());
99110
Datastore tempDatastore = firstNonNull(builder.datastore,
100111
defaultDatastore(tempDataset, host(), httpRequestInitializer()));
101-
DatastoreV1.LookupRequest.Builder requestPb = DatastoreV1.LookupRequest.newBuilder();
102-
DatastoreV1.Key key = DatastoreV1.Key.newBuilder()
103-
.addPathElement(DatastoreV1.Key.PathElement.newBuilder().setKind("__foo__").setName("bar"))
104-
.build();
105-
requestPb.addKey(key);
106-
try {
107-
LookupResponse responsePb = tempDatastore.lookup(requestPb.build());
108-
if (responsePb.getDeferredCount() > 0) {
109-
key = responsePb.getDeferred(0);
110-
} else {
111-
Iterator<EntityResult> combinedIter =
112-
Iterables.concat(responsePb.getMissingList(), responsePb.getFoundList()).iterator();
113-
key = combinedIter.next().getEntity().getKey();
114-
}
115-
dataset = key.getPartitionId().getDatasetId();
116-
if (builder.datastore == null && !dataset.equals(tempDataset)) {
117-
datastore = defaultDatastore(dataset, host(), httpRequestInitializer());
118-
} else {
119-
datastore = tempDatastore;
112+
if (builder.normalizeDataset) {
113+
DatastoreV1.LookupRequest.Builder requestPb = DatastoreV1.LookupRequest.newBuilder();
114+
DatastoreV1.Key key = DatastoreV1.Key.newBuilder()
115+
.addPathElement(DatastoreV1.Key.PathElement.newBuilder().setKind("__foo__").setName("bar"))
116+
.build();
117+
requestPb.addKey(key);
118+
try {
119+
LookupResponse responsePb = tempDatastore.lookup(requestPb.build());
120+
if (responsePb.getDeferredCount() > 0) {
121+
key = responsePb.getDeferred(0);
122+
} else {
123+
Iterator<EntityResult> combinedIter =
124+
Iterables.concat(responsePb.getMissingList(), responsePb.getFoundList()).iterator();
125+
key = combinedIter.next().getEntity().getKey();
126+
}
127+
dataset = key.getPartitionId().getDatasetId();
128+
if (builder.datastore == null && !dataset.equals(tempDataset)) {
129+
datastore = defaultDatastore(dataset, host(), httpRequestInitializer());
130+
} else {
131+
datastore = tempDatastore;
132+
}
133+
} catch (DatastoreException e) {
134+
throw DatastoreServiceException.translateAndThrow(e);
120135
}
121-
} catch (DatastoreException e) {
122-
throw DatastoreServiceException.translateAndThrow(e);
136+
} else {
137+
dataset = tempDataset;
138+
datastore = tempDatastore;
123139
}
124140
}
125141

src/main/java/com/google/gcloud/datastore/KeyFactory.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,8 @@ public Key allocateId() {
6464
protected PartialKey build() {
6565
return newKey();
6666
}
67+
68+
DatastoreService datastore() {
69+
return service;
70+
}
6771
}

src/main/java/com/google/gcloud/datastore/ProjectionEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static final class Builder extends BaseEntity.Builder<Builder> {
4141

4242
private Key key;
4343

44-
private Builder() {
44+
Builder() {
4545
}
4646

4747
private Builder(ProjectionEntity entity) {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.google.gcloud.datastore;
2+
3+
import static junit.framework.TestCase.*;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import com.google.api.services.datastore.client.Datastore;
7+
import org.easymock.EasyMock;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
import java.io.IOException;
12+
13+
public class DatastoreServiceOptionsTest {
14+
15+
private static final String DATASET = "dataset";
16+
private Datastore datastore;
17+
private DatastoreServiceOptions.Builder options;
18+
19+
@Before
20+
public void setUp() throws IOException, InterruptedException {
21+
datastore = EasyMock.createMock(Datastore.class);
22+
options = DatastoreServiceOptions.builder()
23+
.normalizeDataset(false)
24+
.datastore(datastore)
25+
.dataset(DATASET)
26+
.host("http://localhost:" + LocalGcdHelper.PORT);
27+
}
28+
29+
@Test
30+
public void testDataset() throws Exception {
31+
assertEquals(DATASET, options.build().dataset());
32+
}
33+
34+
@Test
35+
public void testHost() throws Exception {
36+
assertEquals("http://localhost:" + LocalGcdHelper.PORT, options.build().host());
37+
}
38+
39+
@Test
40+
public void testNamespace() throws Exception {
41+
assertNull(options.build().namespace());
42+
assertEquals("ns1", options.namespace("ns1").build().namespace());
43+
}
44+
45+
@Test
46+
public void testForce() throws Exception {
47+
assertFalse(options.build().force());
48+
assertTrue(options.force(true).build().force());
49+
}
50+
51+
@Test
52+
public void testDatastore() throws Exception {
53+
assertSame(datastore, options.build().datastore());
54+
}
55+
56+
@Test
57+
public void testToBuilder() throws Exception {
58+
DatastoreServiceOptions original = options.namespace("ns1").force(true).build();
59+
DatastoreServiceOptions copy = original.toBuilder().build();
60+
assertEquals(original.dataset(), copy.dataset());
61+
assertEquals(original.namespace(), copy.namespace());
62+
assertEquals(original.host(), copy.host());
63+
assertEquals(original.force(), copy.force());
64+
assertEquals(original.retryParams(), copy.retryParams());
65+
assertEquals(original.authConfig(), copy.authConfig());
66+
}
67+
}

src/test/java/com/google/gcloud/datastore/EntityValueTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public void testOf() throws Exception {
4242
assertFalse(value.hasMeaning());
4343
}
4444

45+
@Test(expected = IllegalArgumentException.class)
46+
public void testIndexedNotAllowed() {
47+
EntityValue.builder(CONTENT).indexed(true);
48+
}
49+
4550
@Test
4651
public void testBuilder() throws Exception {
4752
EntityValue.Builder builder = EntityValue.builder(CONTENT);
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.google.gcloud.datastore;
2+
3+
import static junit.framework.TestCase.assertEquals;
4+
5+
import com.google.api.services.datastore.DatastoreV1;
6+
import com.google.api.services.datastore.client.Datastore;
7+
import org.easymock.EasyMock;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
import java.util.Iterator;
12+
13+
public class KeyFactoryTest {
14+
15+
private static final String DATASET = "dataset";
16+
17+
private KeyFactory keyFactory;
18+
private Datastore mock;
19+
20+
@Before
21+
public void setUp() {
22+
mock = EasyMock.createMock(Datastore.class);
23+
DatastoreServiceOptions options =
24+
DatastoreServiceOptions.builder().normalizeDataset(false).datastore(mock).dataset(DATASET).build();
25+
DatastoreService datastore = DatastoreServiceFactory.getDefault(options);
26+
keyFactory = new KeyFactory(datastore).kind("k");
27+
}
28+
29+
@Test
30+
public void testNewKey() throws Exception {
31+
Key key = keyFactory.newKey(1);
32+
verifyKey(key, 1L, null);
33+
key = keyFactory.newKey("n");
34+
verifyKey(key, "n", null);
35+
PathElement p1 = PathElement.of("k1", "n");
36+
PathElement p2 = PathElement.of("k2", 10);
37+
key = keyFactory.namespace("ns").ancestors(p1, p2).newKey("k3");
38+
verifyKey(key, "k3", "ns", p1, p2);
39+
}
40+
41+
@Test
42+
public void testNewPartialKey() throws Exception {
43+
PartialKey key = keyFactory.newKey();
44+
verifyPartialKey(key, null);
45+
PathElement p1 = PathElement.of("k1", "n");
46+
PathElement p2 = PathElement.of("k2", 10);
47+
key = keyFactory.namespace("ns").ancestors(p1, p2).newKey();
48+
verifyPartialKey(key, "ns", p1, p2);
49+
}
50+
51+
@Test(expected = NullPointerException.class)
52+
public void testNewPartialWithNoKind() {
53+
new KeyFactory(keyFactory.datastore()).build();
54+
}
55+
56+
private void verifyKey(Key key, String name, String namespace, PathElement... ancestors) {
57+
assertEquals(name, key.name());
58+
verifyPartialKey(key, namespace, ancestors);
59+
}
60+
61+
private void verifyKey(Key key, Long id, String namespace, PathElement... ancestors) {
62+
assertEquals(id, key.id());
63+
verifyPartialKey(key, namespace, ancestors);
64+
}
65+
66+
private void verifyPartialKey(PartialKey key, String namespace, PathElement... ancestors) {
67+
assertEquals("k", key.kind());
68+
assertEquals(DATASET, key.dataset());
69+
assertEquals(namespace, key.namespace());
70+
assertEquals(ancestors.length, key.ancestors().size());
71+
Iterator<PathElement> iter = key.ancestors().iterator();
72+
for (PathElement ancestor : ancestors) {
73+
assertEquals(ancestor, iter.next());
74+
}
75+
}
76+
77+
@Test
78+
public void testAllocateId() throws Exception {
79+
PartialKey pk = keyFactory.newKey();
80+
Key key = keyFactory.newKey(1);
81+
DatastoreV1.AllocateIdsRequest.Builder requestPb = DatastoreV1.AllocateIdsRequest.newBuilder();
82+
requestPb.addKey(pk.toPb());
83+
DatastoreV1.AllocateIdsResponse.Builder responsePb = DatastoreV1.AllocateIdsResponse.newBuilder();
84+
responsePb.addKey(key.toPb());
85+
EasyMock.expect(mock.allocateIds(requestPb.build())).andReturn(responsePb.build());
86+
EasyMock.replay(mock);
87+
assertEquals(key, keyFactory.allocateId());
88+
EasyMock.verify(mock);
89+
}
90+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2015 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.gcloud.datastore;
18+
19+
import com.google.common.collect.ImmutableList;
20+
import org.junit.Test;
21+
22+
import java.util.List;
23+
24+
import static junit.framework.TestCase.assertFalse;
25+
import static org.junit.Assert.assertEquals;
26+
import static org.junit.Assert.assertTrue;
27+
28+
public class ListValueTest {
29+
30+
private static final List<Value<?>> CONTENT = ImmutableList.of(NullValue.of(), StringValue.of("foo"));
31+
32+
@Test
33+
public void testToBuilder() throws Exception {
34+
ListValue value = ListValue.of(CONTENT);
35+
assertEquals(value, value.toBuilder().build());
36+
}
37+
38+
@Test
39+
public void testOf() throws Exception {
40+
ListValue value = ListValue.of(CONTENT);
41+
assertEquals(CONTENT, value.get());
42+
assertFalse(value.hasIndexed());
43+
assertFalse(value.hasMeaning());
44+
}
45+
46+
@Test(expected = DatastoreServiceException.class)
47+
public void testIndexedCannotBeSpecified() {
48+
ListValue.builder().indexed(false);
49+
}
50+
51+
@Test
52+
public void testBuilder() throws Exception {
53+
ListValue.Builder builder = ListValue.builder().set(CONTENT);
54+
ListValue value = builder.meaning(1).build();
55+
assertEquals(CONTENT, value.get());
56+
assertTrue(value.hasMeaning());
57+
assertEquals(Integer.valueOf(1), value.meaning());
58+
assertFalse(value.hasIndexed());
59+
60+
builder = ListValue.builder();
61+
for (Value<?> v : CONTENT) {
62+
builder.addValue(v);
63+
}
64+
assertEquals(CONTENT, builder.build().get());
65+
}
66+
}

0 commit comments

Comments
 (0)