Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky test #88

Merged
merged 1 commit into from
Feb 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ public class Main {
*/
public static void main(String[] args) {
port(8080);
String kind = "DemoUser";
if (args != null) {
for (String arg : args) {
if (arg.startsWith("kind=")) {
kind = arg.substring("kind=".length());
}
}
}
UserController userController =
new UserController(new UserService(DatastoreOptions.defaultInstance().service()));
new UserController(new UserService(DatastoreOptions.defaultInstance().service(), kind));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import com.google.gcloud.datastore.Datastore;
import com.google.gcloud.datastore.Entity;
import com.google.gcloud.datastore.FullEntity;
import com.google.gcloud.datastore.Key;
import com.google.gcloud.datastore.KeyFactory;
import com.google.gcloud.datastore.Query;
Expand All @@ -31,19 +30,28 @@

public class UserService {

private static final String KINDNAME = "DEMO_USER";
private Datastore datastore;
private final Datastore datastore;
private final KeyFactory keyFactory;
private final String kind;

public UserService(Datastore datastore) {
/**
* Constructor for UserService.
*
* @param datastore gcloud-java Datastore service object to execute requests
* @param kind the kind for the Datastore entities in this demo
*/
public UserService(Datastore datastore, String kind) {
this.datastore = datastore;
this.keyFactory = datastore.newKeyFactory().kind(kind);
this.kind = kind;
}

/**
* Return a list of all users.
*/
public List<User> getAllUsers() {
Query<Entity> query =
Query.gqlQueryBuilder(Query.ResultType.ENTITY, "SELECT * FROM " + KINDNAME).build();
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 @@ -60,9 +68,8 @@ public List<User> getAllUsers() {
public User createUser(String name, String email) {
failIfInvalid(name, email);
User user = new User(name, email);
KeyFactory keyFactory = datastore.newKeyFactory().kind(KINDNAME);
Key key = keyFactory.newKey(user.getId());
FullEntity entity = Entity.builder(key)
Entity entity = Entity.builder(key)
.set("id", user.getId())
.set("name", name)
.set("email", email)
Expand All @@ -75,7 +82,6 @@ public User createUser(String name, String email) {
* Delete a user from Cloud Datastore.
*/
public String deleteUser(String id) {
KeyFactory keyFactory = datastore.newKeyFactory().kind(KINDNAME);
Key key = keyFactory.newKey(id);
datastore.delete(key);
return "ok";
Expand All @@ -86,7 +92,6 @@ public String deleteUser(String id) {
*/
public User updateUser(String id, String name, String email) {
failIfInvalid(name, email);
KeyFactory keyFactory = datastore.newKeyFactory().kind(KINDNAME);
Key key = keyFactory.newKey(id);
Entity entity = datastore.get(key);
if (entity == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;

public class UserControllerTest {

Expand All @@ -43,7 +44,7 @@ public class UserControllerTest {

@BeforeClass
public static void beforeClass() {
Main.main(null);
Main.main(new String[] {"kind=DemoUser" + UUID.randomUUID().toString().replaceAll("-", "")});
Spark.awaitInitialization();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public class UserServiceTest {
private static final String USER_NAME = "myName";
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 Key USER_KEY = Key.builder(PROJECT_ID, "DEMO_USER", USER_ID).build();
private static final String KIND = "DemoUser";
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 @@ -67,7 +68,7 @@ public static void beforeClass() throws IOException, InterruptedException {
.host("http://localhost:" + PORT)
.build()
.service();
userService = new UserService(datastore);
userService = new UserService(datastore, KIND);
}

@Before
Expand Down