Skip to content

Commit

Permalink
Remove parent and instead use get for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajay Kannan committed Feb 23, 2016
1 parent 6468183 commit de0cac4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,20 +42,16 @@ 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;
}

/**
* Return a list of all users.
*/
public List<User> getAllUsers() {
Query<Entity> query = Query.entityQueryBuilder()
.kind(kind)
.filter(PropertyFilter.hasAncestor(
datastore.newKeyFactory().kind("SparkJavaDemo").newKey("default")))
.build();
Query<Entity> query =
Query.gqlQueryBuilder(Query.ResultType.ENTITY, "SELECT * FROM " + kind).build();
QueryResults<Entity> results = datastore.run(query);
List<User> users = new ArrayList<>();
while (results.hasNext()) {
Expand All @@ -68,6 +62,16 @@ public List<User> 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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,13 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import com.google.common.collect.Iterators;
import com.google.gcloud.datastore.Datastore;
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.testing.LocalGcdHelper;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
Expand All @@ -49,9 +45,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)
Expand All @@ -64,7 +58,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)
Expand All @@ -76,16 +70,14 @@ public static void beforeClass() throws IOException, InterruptedException {

@Before
public void setUp() {
Query<Key> query = Query.keyQueryBuilder()
.filter(PropertyFilter.hasAncestor(
datastore.newKeyFactory().kind("SparkJavaDemo").newKey("default")))
.kind(KIND)
.build();
QueryResults<Key> result = datastore.run(query);
datastore.delete(Iterators.toArray(result, Key.class));
datastore.add(USER_RECORD);
}

@After
public void tearDown() {
datastore.delete(USER_RECORD.key());
}

@AfterClass
public static void afterClass() throws IOException, InterruptedException {
if (gcdHelper != null) {
Expand Down

0 comments on commit de0cac4

Please sign in to comment.