1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you
5  * may not use this file except in compliance with the License. You may
6  * 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
13  * implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 
17 package com.android.vts.util;
18 
19 import com.google.cloud.datastore.Datastore;
20 import com.google.cloud.datastore.EntityValue;
21 import com.google.cloud.datastore.FullEntity;
22 import com.google.cloud.datastore.IncompleteKey;
23 import com.google.cloud.datastore.Value;
24 import com.googlecode.objectify.Key;
25 import com.googlecode.objectify.cache.MemcacheService;
26 import com.googlecode.objectify.impl.AsyncDatastore;
27 import org.junit.jupiter.api.extension.ExtendWith;
28 
29 import static com.googlecode.objectify.ObjectifyService.factory;
30 import static com.googlecode.objectify.ObjectifyService.ofy;
31 
32 /** All tests should extend this class to set up the GAE environment. */
33 @ExtendWith({
34     MockitoExtension.class,
35     LocalDatastoreExtension.class,
36     ObjectifyExtension.class,
37 })
38 public class ObjectifyTestBase {
39     /** Set embedded entity with property name */
makeEmbeddedEntityWithProperty( final String name, final Value<?> value)40     protected Value<FullEntity<?>> makeEmbeddedEntityWithProperty(
41             final String name, final Value<?> value) {
42         return EntityValue.of(FullEntity.newBuilder().set(name, value).build());
43     }
44 
45     /** Get datastore instance */
datastore()46     protected Datastore datastore() {
47         return factory().datastore();
48     }
49 
50     /** Get memcache instance */
memcache()51     protected MemcacheService memcache() {
52         return factory().memcache();
53     }
54 
55     /** Get asynchronous datastore instance */
asyncDatastore()56     protected AsyncDatastore asyncDatastore() {
57         return factory().asyncDatastore();
58     }
59 
60     /** Save an entity and clear cache data and return entity by finding the key */
saveClearLoad(final E thing)61     protected <E> E saveClearLoad(final E thing) {
62         final Key<E> key = ofy().save().entity(thing).now();
63         ofy().clear();
64         return ofy().load().key(key).now();
65     }
66 
67     /** Get the entity instance from class type */
makeEntity(final Class<?> kind)68     protected FullEntity.Builder<?> makeEntity(final Class<?> kind) {
69         return makeEntity(Key.getKind(kind));
70     }
71 
72     /** Get the entity instance from class name */
makeEntity(final String kind)73     protected FullEntity.Builder<?> makeEntity(final String kind) {
74         final IncompleteKey incompleteKey =
75                 factory().datastore().newKeyFactory().setKind(kind).newKey();
76         return FullEntity.newBuilder(incompleteKey);
77     }
78 }
79