|
| 1 | +/* |
| 2 | + * Copyright 2016 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.cloud.examples.datastore.snippets; |
| 18 | + |
| 19 | +import com.google.cloud.datastore.Datastore; |
| 20 | +import com.google.cloud.datastore.DatastoreOptions; |
| 21 | +import com.google.cloud.datastore.Entity; |
| 22 | +import com.google.cloud.datastore.Key; |
| 23 | +import com.google.cloud.datastore.ProjectionEntity; |
| 24 | +import com.google.cloud.datastore.QueryResults; |
| 25 | +import com.google.common.base.Function; |
| 26 | +import com.google.common.collect.Iterators; |
| 27 | +import com.google.common.collect.Sets; |
| 28 | + |
| 29 | +import org.junit.AfterClass; |
| 30 | +import org.junit.BeforeClass; |
| 31 | +import org.junit.Rule; |
| 32 | +import org.junit.Test; |
| 33 | +import org.junit.rules.Timeout; |
| 34 | + |
| 35 | +import java.util.Set; |
| 36 | +import java.util.UUID; |
| 37 | + |
| 38 | +public class ITQuerySnippets { |
| 39 | + |
| 40 | + private static Datastore datastore; |
| 41 | + private static Entity entity1; |
| 42 | + private static Entity entity2; |
| 43 | + private static final String KIND = "kind_" + UUID.randomUUID().toString().replace("-", ""); |
| 44 | + private static final Function<ProjectionEntity, String> ENTITY_TO_DESCRIPTION_FUNCTION = |
| 45 | + new Function<ProjectionEntity, String>() { |
| 46 | + @Override |
| 47 | + public String apply(ProjectionEntity entity) { |
| 48 | + return entity.getString("description"); |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + @Rule |
| 53 | + public Timeout globalTimeout = Timeout.seconds(60); |
| 54 | + |
| 55 | + @BeforeClass |
| 56 | + public static void beforeClass() { |
| 57 | + datastore = DatastoreOptions.defaultInstance().service(); |
| 58 | + Key key1 = Key.builder(datastore.options().projectId(), KIND, "key1").build(); |
| 59 | + Key key2 = Key.builder(datastore.options().projectId(), KIND, "key2").build(); |
| 60 | + entity1 = Entity.builder(key1).set("description", "entity1").build(); |
| 61 | + entity2 = Entity.builder(key2).set("description", "entity2").build(); |
| 62 | + datastore.put(entity1, entity2); |
| 63 | + } |
| 64 | + |
| 65 | + @AfterClass |
| 66 | + public static void afterClass() { |
| 67 | + datastore.delete(entity1.key(), entity2.key()); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testNewQuery() throws InterruptedException { |
| 72 | + QuerySnippets transactionSnippets = new QuerySnippets(datastore); |
| 73 | + QueryResults<?> results = transactionSnippets.newQuery(KIND); |
| 74 | + Set<?> resultSet = Sets.newHashSet(results); |
| 75 | + while (!resultSet.contains(entity1) || !resultSet.contains(entity2)) { |
| 76 | + Thread.sleep(500); |
| 77 | + resultSet = Sets.newHashSet(results); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testNewTypedQuery() throws InterruptedException { |
| 83 | + QuerySnippets transactionSnippets = new QuerySnippets(datastore); |
| 84 | + QueryResults<Entity> results = transactionSnippets.newTypedQuery(KIND); |
| 85 | + Set<Entity> resultSet = Sets.newHashSet(results); |
| 86 | + while (!resultSet.contains(entity1) || !resultSet.contains(entity2)) { |
| 87 | + Thread.sleep(500); |
| 88 | + resultSet = Sets.newHashSet(results); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testNewEntityQuery() throws InterruptedException { |
| 94 | + QuerySnippets transactionSnippets = new QuerySnippets(datastore); |
| 95 | + QueryResults<Entity> results = transactionSnippets.newEntityQuery(KIND); |
| 96 | + Set<Entity> resultSet = Sets.newHashSet(results); |
| 97 | + while (!resultSet.contains(entity1) || !resultSet.contains(entity2)) { |
| 98 | + Thread.sleep(500); |
| 99 | + resultSet = Sets.newHashSet(results); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testNewKeyQuery() throws InterruptedException { |
| 105 | + QuerySnippets transactionSnippets = new QuerySnippets(datastore); |
| 106 | + QueryResults<Key> results = transactionSnippets.newKeyQuery(KIND); |
| 107 | + Set<Key> resultSet = Sets.newHashSet(results); |
| 108 | + while (!resultSet.contains(entity1.key()) || !resultSet.contains(entity2.key())) { |
| 109 | + Thread.sleep(500); |
| 110 | + resultSet = Sets.newHashSet(results); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testNewProjectionEntityQuery() throws InterruptedException { |
| 116 | + QuerySnippets transactionSnippets = new QuerySnippets(datastore); |
| 117 | + QueryResults<ProjectionEntity> results = |
| 118 | + transactionSnippets.newProjectionEntityQuery(KIND, "description"); |
| 119 | + Set<String> resultSet = |
| 120 | + Sets.newHashSet(Iterators.transform(results, ENTITY_TO_DESCRIPTION_FUNCTION)); |
| 121 | + while (!resultSet.contains(entity1.getString("description")) |
| 122 | + || !resultSet.contains(entity2.getString("description"))) { |
| 123 | + Thread.sleep(500); |
| 124 | + resultSet = Sets.newHashSet(Iterators.transform(results, ENTITY_TO_DESCRIPTION_FUNCTION)); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments