Skip to content

Fix consistency issue with sparkjava demo tests. #96

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

Merged
merged 3 commits into from
Feb 23, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion managed_vms/sparkjava/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<dependency>
<groupId>com.google.gcloud</groupId>
<artifactId>gcloud-java</artifactId>
<version>0.1.3</version>
<version>0.1.4</version>
</dependency>
</dependencies>
<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
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 @@ -42,16 +44,20 @@ public class UserService {
*/
public UserService(Datastore datastore, String kind) {
this.datastore = datastore;
this.keyFactory = datastore.newKeyFactory().kind(kind);
this.keyFactory =
datastore.newKeyFactory().ancestors(PathElement.of("SparkJavaDemo", "default")).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 " + kind).build();
Query<Entity> query = Query.entityQueryBuilder()
.kind(kind)
.filter(PropertyFilter.hasAncestor(
datastore.newKeyFactory().kind("SparkJavaDemo").newKey("default")))
.build();
QueryResults<Entity> results = datastore.run(query);
List<User> users = new ArrayList<>();
while (results.hasNext()) {
Expand Down Expand Up @@ -83,7 +89,9 @@ public User createUser(String name, String email) {
*/
public String deleteUser(String id) {
Key key = keyFactory.newKey(id);
datastore.delete(key);
if (datastore.get(key) != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just delete?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

datastore.delete(key);
}
return "ok";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
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;
import com.google.gcloud.datastore.StructuredQuery.PropertyFilter;
import com.google.gcloud.datastore.testing.LocalGcdHelper;

import org.junit.AfterClass;
Expand All @@ -48,7 +49,9 @@ public class UserServiceTest {
private static final String USER_EMAIL = "[email protected]";
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).build();
private static final Key USER_KEY = Key.builder(PROJECT_ID, KIND, USER_ID)
.ancestors(PathElement.of("SparkJavaDemo", "default"))
.build();
private static final Entity USER_RECORD = Entity.builder(USER_KEY)
.set("id", USER_ID)
.set("name", USER_NAME)
Expand All @@ -61,7 +64,7 @@ public class UserServiceTest {
@BeforeClass
public static void beforeClass() throws IOException, InterruptedException {
if (!LocalGcdHelper.isActive(PROJECT_ID, PORT)) {
gcdHelper = LocalGcdHelper.start(PROJECT_ID, PORT);
gcdHelper = LocalGcdHelper.start(PROJECT_ID, PORT, 0.0);
}
datastore = DatastoreOptions.builder()
.projectId(PROJECT_ID)
Expand All @@ -73,7 +76,11 @@ public static void beforeClass() throws IOException, InterruptedException {

@Before
public void setUp() {
StructuredQuery<Key> query = Query.keyQueryBuilder().build();
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);
Expand Down