|
| 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 static junit.framework.TestCase.assertTrue; |
| 20 | +import static org.easymock.EasyMock.createMock; |
| 21 | +import static org.easymock.EasyMock.createStrictMock; |
| 22 | +import static org.easymock.EasyMock.expect; |
| 23 | +import static org.easymock.EasyMock.replay; |
| 24 | +import static org.easymock.EasyMock.verify; |
| 25 | +import static org.junit.Assert.assertArrayEquals; |
| 26 | +import static org.junit.Assert.assertEquals; |
| 27 | +import static org.junit.Assert.assertSame; |
| 28 | + |
| 29 | +import com.google.api.services.datastore.client.Datastore; |
| 30 | +import com.google.common.collect.Iterators; |
| 31 | +import com.google.common.collect.Sets; |
| 32 | + |
| 33 | +import org.junit.Test; |
| 34 | + |
| 35 | +import java.lang.reflect.InvocationHandler; |
| 36 | +import java.lang.reflect.Method; |
| 37 | +import java.lang.reflect.Proxy; |
| 38 | +import java.util.HashMap; |
| 39 | +import java.util.List; |
| 40 | +import java.util.Map; |
| 41 | +import java.util.Set; |
| 42 | + |
| 43 | +public class DatastoreHelperTest { |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testDelegate() throws Exception { |
| 47 | + final Set<Method> methods = Sets.newHashSet(DatastoreService.class.getMethods()); |
| 48 | + final Map<String, Object[]> params = new HashMap<>(); |
| 49 | + InvocationHandler handler = new InvocationHandler() { |
| 50 | + @Override |
| 51 | + public Object invoke(Object o, Method method, Object[] objects) throws Throwable { |
| 52 | + String methodName = Thread.currentThread().getStackTrace()[3].getMethodName(); |
| 53 | + if (method.getParameterTypes().length > 0) { |
| 54 | + Class paramClass = method.getParameterTypes()[0]; |
| 55 | + methodName += paramClass.getSimpleName(); |
| 56 | + if (method.isVarArgs()) { |
| 57 | + objects = (Object[]) objects[0]; |
| 58 | + } |
| 59 | + assertArrayEquals(objects, params.get(methodName)); |
| 60 | + } |
| 61 | + assertTrue(methods.remove(method)); |
| 62 | + return null; |
| 63 | + } |
| 64 | + }; |
| 65 | + PartialKey pKey1 = PartialKey.builder("ds", "k").build(); |
| 66 | + PartialKey pKey2 = PartialKey.builder("ds", "k").build(); |
| 67 | + PartialEntity pEntity1 = PartialEntity.builder(pKey1).build(); |
| 68 | + PartialEntity pEntity2 = PartialEntity.builder(pKey2).build(); |
| 69 | + Key key1 = Key.builder(pKey1, 1).build(); |
| 70 | + Key key2 = Key.builder(pKey1, "a").build(); |
| 71 | + Entity entity1 = Entity.builder(key1).build(); |
| 72 | + Entity entity2 = Entity.builder(key2).build(); |
| 73 | + ClassLoader cl = DatastoreService.class.getClassLoader(); |
| 74 | + Class<?>[] interfaces = DatastoreHelper.class.getInterfaces(); |
| 75 | + DatastoreService delegate = (DatastoreService) Proxy.newProxyInstance(cl, interfaces, handler); |
| 76 | + DatastoreHelper helper = DatastoreHelper.createFor(delegate); |
| 77 | + params.put("getKey", new Object[] {key1}); |
| 78 | + helper.get(key1); |
| 79 | + params.put("getKey[]", new Object[] {key1, key2}); |
| 80 | + helper.get(key1, key2); |
| 81 | + params.put("addEntity[]", new Object[] {entity1, entity2}); |
| 82 | + helper.add(entity1, entity2); |
| 83 | + params.put("updateEntity[]", new Object[] {entity1}); |
| 84 | + helper.update(entity1); |
| 85 | + params.put("addPartialEntity", new Object[] {pEntity1}); |
| 86 | + helper.add(pEntity1); |
| 87 | + params.put("addPartialEntity[]", new Object[] {pEntity2, entity1}); |
| 88 | + helper.add(pEntity2, entity1); |
| 89 | + params.put("allocateIdPartialKey", new Object[] {pKey1}); |
| 90 | + helper.allocateId(pKey1); |
| 91 | + params.put("allocateIdPartialKey[]", new Object[] {pKey1, pKey2}); |
| 92 | + helper.allocateId(pKey1, pKey2); |
| 93 | + params.put("deleteKey[]", new Object[] {key1}); |
| 94 | + helper.delete(key1); |
| 95 | + params.put("putEntity[]", new Object[] {entity1}); |
| 96 | + helper.put(entity1); |
| 97 | + helper.options(); |
| 98 | + params.put("newBatchBatchOption[]", new Object[] {}); |
| 99 | + helper.newBatch(); |
| 100 | + params.put("newTransactionTransactionOption[]", new Object[] {}); |
| 101 | + helper.newTransaction(); |
| 102 | + Query query = createMock(Query.class); |
| 103 | + params.put("runQuery", new Object[] {query}); |
| 104 | + helper.run(query); |
| 105 | + assertTrue(methods.isEmpty()); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testNewKeyFactory() throws Exception { |
| 110 | + DatastoreService datastoreService = createStrictMock(DatastoreService.class); |
| 111 | + Datastore datastore = createStrictMock(Datastore.class); |
| 112 | + DatastoreServiceOptions options = |
| 113 | + DatastoreServiceOptions.builder().normalizeDataset(false).datastore(datastore) |
| 114 | + .dataset("ds").build(); |
| 115 | + expect(datastoreService.options()).andReturn(options).atLeastOnce(); |
| 116 | + replay(datastore, datastoreService); |
| 117 | + DatastoreHelper helper = DatastoreHelper.createFor(datastoreService); |
| 118 | + KeyFactory keyFactory = helper.newKeyFactory(); |
| 119 | + assertSame(datastoreService, keyFactory.datastore()); |
| 120 | + verify(datastore, datastoreService); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void testFetch() throws Exception { |
| 125 | + DatastoreService datastoreService = createStrictMock(DatastoreService.class); |
| 126 | + PartialKey pKey1 = PartialKey.builder("ds", "k").build(); |
| 127 | + Key key1 = Key.builder(pKey1, 1).build(); |
| 128 | + Key key2 = Key.builder(pKey1, "a").build(); |
| 129 | + Entity entity1 = Entity.builder(key1).build(); |
| 130 | + Entity entity2 = Entity.builder(key2).build(); |
| 131 | + expect(datastoreService.get(key1, key2)).andReturn(Iterators.forArray(entity1, entity2)).once(); |
| 132 | + replay(datastoreService); |
| 133 | + DatastoreHelper helper = DatastoreHelper.createFor(datastoreService); |
| 134 | + List<Entity> values = helper.fetch(key1, key2); |
| 135 | + assertEquals(2, values.size()); |
| 136 | + assertEquals(entity1, values.get(0)); |
| 137 | + assertEquals(entity2, values.get(1)); |
| 138 | + verify(datastoreService); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testRunInTransaction() throws Exception { |
| 143 | + final DatastoreService datastoreService = createStrictMock(DatastoreService.class); |
| 144 | + final Transaction transaction = createStrictMock(Transaction.class); |
| 145 | + expect(datastoreService.newTransaction()).andReturn(transaction).once(); |
| 146 | + expect(transaction.active()).andReturn(true).once(); |
| 147 | + expect(transaction.commit()).andReturn(null).once(); |
| 148 | + expect(transaction.active()).andReturn(false).once(); |
| 149 | + replay(datastoreService, transaction); |
| 150 | + DatastoreHelper helper = DatastoreHelper.createFor(datastoreService); |
| 151 | + helper.runInTransaction(new DatastoreHelper.RunInTransaction() { |
| 152 | + @Override |
| 153 | + public void run(DatastoreReaderWriter readerWriter) { |
| 154 | + assertTrue(transaction.active()); |
| 155 | + assertSame(transaction, readerWriter); |
| 156 | + } |
| 157 | + }); |
| 158 | + verify(datastoreService, transaction); |
| 159 | + } |
| 160 | +} |
0 commit comments