Skip to content

Commit 82e1abe

Browse files
author
Ajay Kannan
committed
Remove parent and instead use get for consistency
1 parent 6468183 commit 82e1abe

File tree

4 files changed

+34
-31
lines changed

4 files changed

+34
-31
lines changed

managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/UserController.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ public class UserController {
3636
public UserController(final UserService userService) {
3737
Spark.staticFileLocation("/public");
3838

39-
get("/api/users", (req, res) -> userService.getAllUsers(), UserController::toJson);
39+
get("/api/users", (req, res) -> userService.getAllUsers(), json());
40+
41+
get("/api/users/:id", (req, res) -> userService.getUser(req.params(":id")), json());
4042

4143
post("/api/users",
4244
(req, res) -> userService.createUser(req.queryParams("name"), req.queryParams("email")),

managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/UserService.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
2222
import com.google.gcloud.datastore.Entity;
2323
import com.google.gcloud.datastore.Key;
2424
import com.google.gcloud.datastore.KeyFactory;
25-
import com.google.gcloud.datastore.PathElement;
2625
import com.google.gcloud.datastore.Query;
2726
import com.google.gcloud.datastore.QueryResults;
28-
import com.google.gcloud.datastore.StructuredQuery.PropertyFilter;
2927

3028
import java.util.ArrayList;
3129
import java.util.List;
@@ -44,20 +42,16 @@ public class UserService {
4442
*/
4543
public UserService(Datastore datastore, String kind) {
4644
this.datastore = datastore;
47-
this.keyFactory =
48-
datastore.newKeyFactory().ancestors(PathElement.of("SparkJavaDemo", "default")).kind(kind);
45+
this.keyFactory = datastore.newKeyFactory().kind(kind);
4946
this.kind = kind;
5047
}
5148

5249
/**
5350
* Return a list of all users.
5451
*/
5552
public List<User> getAllUsers() {
56-
Query<Entity> query = Query.entityQueryBuilder()
57-
.kind(kind)
58-
.filter(PropertyFilter.hasAncestor(
59-
datastore.newKeyFactory().kind("SparkJavaDemo").newKey("default")))
60-
.build();
53+
Query<Entity> query =
54+
Query.gqlQueryBuilder(Query.ResultType.ENTITY, "SELECT * FROM " + kind).build();
6155
QueryResults<Entity> results = datastore.run(query);
6256
List<User> users = new ArrayList<>();
6357
while (results.hasNext()) {
@@ -68,6 +62,16 @@ public List<User> getAllUsers() {
6862
return users;
6963
}
7064

65+
/**
66+
* Return the user with the given id.
67+
*/
68+
User getUser(String id) {
69+
Entity entity = datastore.get(keyFactory.newKey(id));
70+
return entity == null
71+
? null
72+
: new User(entity.getString("id"), entity.getString("name"), entity.getString("email"));
73+
}
74+
7175
/**
7276
* Create a new user and add it to Cloud Datastore.
7377
*/

managed_vms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserControllerTest.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818

1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertNotNull;
21+
import static org.junit.Assert.assertNull;
2122
import static org.junit.Assert.assertTrue;
2223
import static org.junit.Assert.fail;
2324

2425
import com.google.gson.Gson;
2526

27+
import org.junit.After;
2628
import org.junit.AfterClass;
2729
import org.junit.Before;
2830
import org.junit.BeforeClass;
@@ -50,13 +52,14 @@ public static void beforeClass() {
5052

5153
@Before
5254
public void setUp() throws IOException {
53-
User[] allUsers = getAllUsers();
54-
for (User user : allUsers) {
55-
deleteUser(user.getId());
56-
}
5755
userId = createUser(USER_NAME, USER_EMAIL).getId();
5856
}
5957

58+
@After
59+
public void tearDown() throws IOException {
60+
deleteUser(userId);
61+
}
62+
6063
@AfterClass
6164
public static void afterClass() {
6265
Spark.stop();
@@ -65,11 +68,7 @@ public static void afterClass() {
6568
@Test
6669
public void testGetAllUsers() throws IOException {
6770
User[] users = getAllUsers();
68-
assertEquals(1, users.length);
69-
User user = users[0];
70-
assertEquals(userId, user.getId());
71-
assertEquals(USER_NAME, user.getName());
72-
assertEquals(USER_EMAIL, user.getEmail());
71+
assertTrue(users.length <= 1);
7372
}
7473

7574
@Test
@@ -92,8 +91,9 @@ public void testCreateUserInvalidRequest() {
9291

9392
@Test
9493
public void testDeleteUser() throws IOException {
94+
assertNotNull(getUser(userId));
9595
assertEquals("\"ok\"", deleteUser(userId));
96-
assertEquals(0, getAllUsers().length);
96+
assertNull(getUser(userId));
9797
}
9898

9999
@Test
@@ -127,6 +127,10 @@ private static String deleteUser(String id) throws IOException {
127127
return executeRequest("DELETE", "/api/users/" + id);
128128
}
129129

130+
private static User getUser(String id) throws IOException {
131+
return new Gson().fromJson(executeRequest("GET", "/api/users/" + id), User.class);
132+
}
133+
130134
private static User[] getAllUsers() throws IOException {
131135
return new Gson().fromJson(executeRequest("GET", "/api/users"), User[].class);
132136
}

managed_vms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserServiceTest.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@
2626
import com.google.gcloud.datastore.DatastoreOptions;
2727
import com.google.gcloud.datastore.Entity;
2828
import com.google.gcloud.datastore.Key;
29-
import com.google.gcloud.datastore.PathElement;
3029
import com.google.gcloud.datastore.Query;
3130
import com.google.gcloud.datastore.QueryResults;
32-
import com.google.gcloud.datastore.StructuredQuery.PropertyFilter;
31+
import com.google.gcloud.datastore.StructuredQuery;
3332
import com.google.gcloud.datastore.testing.LocalGcdHelper;
3433

3534
import org.junit.AfterClass;
@@ -49,9 +48,7 @@ public class UserServiceTest {
4948
private static final String USER_EMAIL = "my@email.com";
5049
private static final User USER = new User(USER_ID, USER_NAME, USER_EMAIL);
5150
private static final String KIND = "DemoUser";
52-
private static final Key USER_KEY = Key.builder(PROJECT_ID, KIND, USER_ID)
53-
.ancestors(PathElement.of("SparkJavaDemo", "default"))
54-
.build();
51+
private static final Key USER_KEY = Key.builder(PROJECT_ID, KIND, USER_ID).build();
5552
private static final Entity USER_RECORD = Entity.builder(USER_KEY)
5653
.set("id", USER_ID)
5754
.set("name", USER_NAME)
@@ -64,7 +61,7 @@ public class UserServiceTest {
6461
@BeforeClass
6562
public static void beforeClass() throws IOException, InterruptedException {
6663
if (!LocalGcdHelper.isActive(PROJECT_ID, PORT)) {
67-
gcdHelper = LocalGcdHelper.start(PROJECT_ID, PORT, 0.0);
64+
gcdHelper = LocalGcdHelper.start(PROJECT_ID, PORT, 1.0);
6865
}
6966
datastore = DatastoreOptions.builder()
7067
.projectId(PROJECT_ID)
@@ -76,11 +73,7 @@ public static void beforeClass() throws IOException, InterruptedException {
7673

7774
@Before
7875
public void setUp() {
79-
Query<Key> query = Query.keyQueryBuilder()
80-
.filter(PropertyFilter.hasAncestor(
81-
datastore.newKeyFactory().kind("SparkJavaDemo").newKey("default")))
82-
.kind(KIND)
83-
.build();
76+
StructuredQuery<Key> query = Query.keyQueryBuilder().build();
8477
QueryResults<Key> result = datastore.run(query);
8578
datastore.delete(Iterators.toArray(result, Key.class));
8679
datastore.add(USER_RECORD);

0 commit comments

Comments
 (0)