|
| 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 | +} |
0 commit comments