From 82e1abe7cb6705b7fd44307620f07d98b760ddbd Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Mon, 22 Feb 2016 17:41:49 -0800 Subject: [PATCH] Remove parent and instead use get for consistency --- .../appengine/sparkdemo/UserController.java | 4 +++- .../appengine/sparkdemo/UserService.java | 22 ++++++++++------- .../sparkdemo/UserControllerTest.java | 24 +++++++++++-------- .../appengine/sparkdemo/UserServiceTest.java | 15 ++++-------- 4 files changed, 34 insertions(+), 31 deletions(-) diff --git a/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/UserController.java b/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/UserController.java index ab44c17f10a..0c1a5096af6 100644 --- a/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/UserController.java +++ b/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/UserController.java @@ -36,7 +36,9 @@ public class UserController { public UserController(final UserService userService) { Spark.staticFileLocation("/public"); - get("/api/users", (req, res) -> userService.getAllUsers(), UserController::toJson); + get("/api/users", (req, res) -> userService.getAllUsers(), json()); + + get("/api/users/:id", (req, res) -> userService.getUser(req.params(":id")), json()); post("/api/users", (req, res) -> userService.createUser(req.queryParams("name"), req.queryParams("email")), diff --git a/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/UserService.java b/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/UserService.java index 327056e6da4..9e6683ee592 100644 --- a/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/UserService.java +++ b/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/UserService.java @@ -22,10 +22,8 @@ import com.google.gcloud.datastore.Entity; import com.google.gcloud.datastore.Key; import com.google.gcloud.datastore.KeyFactory; -import com.google.gcloud.datastore.PathElement; import com.google.gcloud.datastore.Query; import com.google.gcloud.datastore.QueryResults; -import com.google.gcloud.datastore.StructuredQuery.PropertyFilter; import java.util.ArrayList; import java.util.List; @@ -44,8 +42,7 @@ public class UserService { */ public UserService(Datastore datastore, String kind) { this.datastore = datastore; - this.keyFactory = - datastore.newKeyFactory().ancestors(PathElement.of("SparkJavaDemo", "default")).kind(kind); + this.keyFactory = datastore.newKeyFactory().kind(kind); this.kind = kind; } @@ -53,11 +50,8 @@ public UserService(Datastore datastore, String kind) { * Return a list of all users. */ public List getAllUsers() { - Query query = Query.entityQueryBuilder() - .kind(kind) - .filter(PropertyFilter.hasAncestor( - datastore.newKeyFactory().kind("SparkJavaDemo").newKey("default"))) - .build(); + Query query = + Query.gqlQueryBuilder(Query.ResultType.ENTITY, "SELECT * FROM " + kind).build(); QueryResults results = datastore.run(query); List users = new ArrayList<>(); while (results.hasNext()) { @@ -68,6 +62,16 @@ public List getAllUsers() { return users; } + /** + * Return the user with the given id. + */ + User getUser(String id) { + Entity entity = datastore.get(keyFactory.newKey(id)); + return entity == null + ? null + : new User(entity.getString("id"), entity.getString("name"), entity.getString("email")); + } + /** * Create a new user and add it to Cloud Datastore. */ diff --git a/managed_vms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserControllerTest.java b/managed_vms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserControllerTest.java index 2c252a01cf6..3b02cc7c3fe 100644 --- a/managed_vms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserControllerTest.java +++ b/managed_vms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserControllerTest.java @@ -18,11 +18,13 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import com.google.gson.Gson; +import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; @@ -50,13 +52,14 @@ public static void beforeClass() { @Before public void setUp() throws IOException { - User[] allUsers = getAllUsers(); - for (User user : allUsers) { - deleteUser(user.getId()); - } userId = createUser(USER_NAME, USER_EMAIL).getId(); } + @After + public void tearDown() throws IOException { + deleteUser(userId); + } + @AfterClass public static void afterClass() { Spark.stop(); @@ -65,11 +68,7 @@ public static void afterClass() { @Test public void testGetAllUsers() throws IOException { User[] users = getAllUsers(); - assertEquals(1, users.length); - User user = users[0]; - assertEquals(userId, user.getId()); - assertEquals(USER_NAME, user.getName()); - assertEquals(USER_EMAIL, user.getEmail()); + assertTrue(users.length <= 1); } @Test @@ -92,8 +91,9 @@ public void testCreateUserInvalidRequest() { @Test public void testDeleteUser() throws IOException { + assertNotNull(getUser(userId)); assertEquals("\"ok\"", deleteUser(userId)); - assertEquals(0, getAllUsers().length); + assertNull(getUser(userId)); } @Test @@ -127,6 +127,10 @@ private static String deleteUser(String id) throws IOException { return executeRequest("DELETE", "/api/users/" + id); } + private static User getUser(String id) throws IOException { + return new Gson().fromJson(executeRequest("GET", "/api/users/" + id), User.class); + } + private static User[] getAllUsers() throws IOException { return new Gson().fromJson(executeRequest("GET", "/api/users"), User[].class); } diff --git a/managed_vms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserServiceTest.java b/managed_vms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserServiceTest.java index 913936d4ce8..f5daff6be0c 100644 --- a/managed_vms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserServiceTest.java +++ b/managed_vms/sparkjava/src/test/java/com/google/appengine/sparkdemo/UserServiceTest.java @@ -26,10 +26,9 @@ import com.google.gcloud.datastore.DatastoreOptions; import com.google.gcloud.datastore.Entity; import com.google.gcloud.datastore.Key; -import com.google.gcloud.datastore.PathElement; import com.google.gcloud.datastore.Query; import com.google.gcloud.datastore.QueryResults; -import com.google.gcloud.datastore.StructuredQuery.PropertyFilter; +import com.google.gcloud.datastore.StructuredQuery; import com.google.gcloud.datastore.testing.LocalGcdHelper; import org.junit.AfterClass; @@ -49,9 +48,7 @@ public class UserServiceTest { private static final String USER_EMAIL = "my@email.com"; private static final User USER = new User(USER_ID, USER_NAME, USER_EMAIL); private static final String KIND = "DemoUser"; - private static final Key USER_KEY = Key.builder(PROJECT_ID, KIND, USER_ID) - .ancestors(PathElement.of("SparkJavaDemo", "default")) - .build(); + private static final Key USER_KEY = Key.builder(PROJECT_ID, KIND, USER_ID).build(); private static final Entity USER_RECORD = Entity.builder(USER_KEY) .set("id", USER_ID) .set("name", USER_NAME) @@ -64,7 +61,7 @@ public class UserServiceTest { @BeforeClass public static void beforeClass() throws IOException, InterruptedException { if (!LocalGcdHelper.isActive(PROJECT_ID, PORT)) { - gcdHelper = LocalGcdHelper.start(PROJECT_ID, PORT, 0.0); + gcdHelper = LocalGcdHelper.start(PROJECT_ID, PORT, 1.0); } datastore = DatastoreOptions.builder() .projectId(PROJECT_ID) @@ -76,11 +73,7 @@ public static void beforeClass() throws IOException, InterruptedException { @Before public void setUp() { - Query query = Query.keyQueryBuilder() - .filter(PropertyFilter.hasAncestor( - datastore.newKeyFactory().kind("SparkJavaDemo").newKey("default"))) - .kind(KIND) - .build(); + StructuredQuery query = Query.keyQueryBuilder().build(); QueryResults result = datastore.run(query); datastore.delete(Iterators.toArray(result, Key.class)); datastore.add(USER_RECORD);