Skip to content

Commit

Permalink
Merge pull request #159 from GoogleCloudPlatform/update-gcloud-java-d…
Browse files Browse the repository at this point in the history
…ependency

Update gcloud-java dependency and imports
  • Loading branch information
Shun Fan committed Apr 12, 2016
2 parents db10bd9 + 16375ec commit f78394c
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 118 deletions.
4 changes: 2 additions & 2 deletions datastore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
</description>
<dependencies>
<dependency>
<groupId>com.google.gcloud</groupId>
<groupId>com.google.cloud</groupId>
<artifactId>gcloud-java-datastore</artifactId>
<version>0.1.7</version>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
66 changes: 27 additions & 39 deletions datastore/src/main/java/com/google/datastore/snippets/Concepts.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,33 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import com.google.cloud.datastore.Cursor;
import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.DatastoreException;
import com.google.cloud.datastore.DateTime;
import com.google.cloud.datastore.Entity;
import com.google.cloud.datastore.EntityQuery;
import com.google.cloud.datastore.FullEntity;
import com.google.cloud.datastore.IncompleteKey;
import com.google.cloud.datastore.Key;
import com.google.cloud.datastore.KeyFactory;
import com.google.cloud.datastore.PathElement;
import com.google.cloud.datastore.ProjectionEntity;
import com.google.cloud.datastore.Query;
import com.google.cloud.datastore.Query.ResultType;
import com.google.cloud.datastore.QueryResults;
import com.google.cloud.datastore.ReadOption;
import com.google.cloud.datastore.StringValue;
import com.google.cloud.datastore.StructuredQuery;
import com.google.cloud.datastore.StructuredQuery.CompositeFilter;
import com.google.cloud.datastore.StructuredQuery.OrderBy;
import com.google.cloud.datastore.StructuredQuery.PropertyFilter;
import com.google.cloud.datastore.Transaction;
import com.google.cloud.datastore.testing.LocalDatastoreHelper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterators;
import com.google.gcloud.datastore.Cursor;
import com.google.gcloud.datastore.Datastore;
import com.google.gcloud.datastore.DatastoreException;
import com.google.gcloud.datastore.DatastoreOptions;
import com.google.gcloud.datastore.DateTime;
import com.google.gcloud.datastore.Entity;
import com.google.gcloud.datastore.EntityQuery;
import com.google.gcloud.datastore.FullEntity;
import com.google.gcloud.datastore.IncompleteKey;
import com.google.gcloud.datastore.Key;
import com.google.gcloud.datastore.KeyFactory;
import com.google.gcloud.datastore.PathElement;
import com.google.gcloud.datastore.ProjectionEntity;
import com.google.gcloud.datastore.Query;
import com.google.gcloud.datastore.Query.ResultType;
import com.google.gcloud.datastore.QueryResults;
import com.google.gcloud.datastore.ReadOption;
import com.google.gcloud.datastore.StringValue;
import com.google.gcloud.datastore.StructuredQuery;
import com.google.gcloud.datastore.StructuredQuery.CompositeFilter;
import com.google.gcloud.datastore.StructuredQuery.OrderBy;
import com.google.gcloud.datastore.StructuredQuery.PropertyFilter;
import com.google.gcloud.datastore.Transaction;
import com.google.gcloud.datastore.testing.LocalGcdHelper;

import org.junit.AfterClass;
import org.junit.Before;
Expand Down Expand Up @@ -79,9 +78,7 @@
*/
public class Concepts {

private static final String PROJECT_ID = LocalGcdHelper.DEFAULT_PROJECT_ID;
private static LocalGcdHelper gcdHelper;
private static final int PORT = LocalGcdHelper.findAvailablePort(LocalGcdHelper.DEFAULT_PORT);
private static final LocalDatastoreHelper HELPER = LocalDatastoreHelper.create(1.0);
private static final FullEntity<IncompleteKey> TEST_FULL_ENTITY = FullEntity.builder().build();

private Datastore datastore;
Expand All @@ -103,9 +100,7 @@ public class Concepts {
*/
@BeforeClass
public static void beforeClass() throws IOException, InterruptedException {
if (!LocalGcdHelper.isActive(PROJECT_ID, PORT)) {
gcdHelper = LocalGcdHelper.start(PROJECT_ID, PORT, 1.0);
}
HELPER.start();
}

/**
Expand All @@ -114,12 +109,7 @@ public static void beforeClass() throws IOException, InterruptedException {
*/
@Before
public void setUp() {
datastore = DatastoreOptions.builder()
.projectId(PROJECT_ID)
.namespace("ghijklmnop")
.host("http://localhost:" + PORT)
.build()
.service();
datastore = HELPER.options().toBuilder().namespace("ghijklmnop").build().service();
StructuredQuery<Key> query = Query.keyQueryBuilder().build();
QueryResults<Key> result = datastore.run(query);
datastore.delete(Iterators.toArray(result, Key.class));
Expand All @@ -143,9 +133,7 @@ public void setUp() {
*/
@AfterClass
public static void afterClass() throws IOException, InterruptedException {
if (gcdHelper != null) {
gcdHelper.stop();
}
HELPER.stop();
}

private void assertValidKey(Key taskKey) {
Expand Down
20 changes: 10 additions & 10 deletions datastore/src/main/java/com/google/datastore/snippets/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@

package com.google.datastore.snippets;

import com.google.gcloud.datastore.Datastore;
import com.google.gcloud.datastore.DatastoreOptions;
import com.google.gcloud.datastore.DateTime;
import com.google.gcloud.datastore.Entity;
import com.google.gcloud.datastore.Key;
import com.google.gcloud.datastore.KeyFactory;
import com.google.gcloud.datastore.Query;
import com.google.gcloud.datastore.StringValue;
import com.google.gcloud.datastore.StructuredQuery.OrderBy;
import com.google.gcloud.datastore.Transaction;
import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.DatastoreOptions;
import com.google.cloud.datastore.DateTime;
import com.google.cloud.datastore.Entity;
import com.google.cloud.datastore.Key;
import com.google.cloud.datastore.KeyFactory;
import com.google.cloud.datastore.Query;
import com.google.cloud.datastore.StringValue;
import com.google.cloud.datastore.StructuredQuery.OrderBy;
import com.google.cloud.datastore.Transaction;

import java.util.ArrayList;
import java.util.Iterator;
Expand Down
4 changes: 2 additions & 2 deletions managed_vms/cloudstorage/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
</dependency>
<!-- [START dependencies] -->
<dependency>
<groupId>com.google.gcloud</groupId>
<groupId>com.google.cloud</groupId>
<artifactId>gcloud-java-storage</artifactId>
<version>0.1.7</version>
<version>0.2.0</version>
</dependency>
<!-- [END dependencies] -->
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package com.example.managedvms.cloudstorage;

import com.google.gcloud.storage.Acl;
import com.google.gcloud.storage.Blob;
import com.google.gcloud.storage.BlobInfo;
import com.google.gcloud.storage.Storage;
import com.google.gcloud.storage.StorageOptions;
import com.google.cloud.storage.Acl;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobInfo;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

import java.io.IOException;
import java.util.ArrayList;
Expand Down
4 changes: 2 additions & 2 deletions managed_vms/datastore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
</dependency>
<!-- [START dependencies] -->
<dependency>
<groupId>com.google.gcloud</groupId>
<groupId>com.google.cloud</groupId>
<artifactId>gcloud-java-datastore</artifactId>
<version>0.1.7</version>
<version>0.2.0</version>
</dependency>
<!-- [END dependencies] -->
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@

package com.example.managedvms.datastore;

import com.google.gcloud.datastore.Datastore;
import com.google.gcloud.datastore.DatastoreOptions;
import com.google.gcloud.datastore.DateTime;
import com.google.gcloud.datastore.Entity;
import com.google.gcloud.datastore.FullEntity;
import com.google.gcloud.datastore.IncompleteKey;
import com.google.gcloud.datastore.KeyFactory;
import com.google.gcloud.datastore.Query;
import com.google.gcloud.datastore.QueryResults;
import com.google.gcloud.datastore.StructuredQuery;
import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.DatastoreOptions;
import com.google.cloud.datastore.DateTime;
import com.google.cloud.datastore.Entity;
import com.google.cloud.datastore.FullEntity;
import com.google.cloud.datastore.IncompleteKey;
import com.google.cloud.datastore.KeyFactory;
import com.google.cloud.datastore.Query;
import com.google.cloud.datastore.QueryResults;
import com.google.cloud.datastore.StructuredQuery;

import java.io.IOException;
import java.io.PrintWriter;
Expand Down
4 changes: 2 additions & 2 deletions managed_vms/sparkjava/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
<version>4.12</version>
</dependency>
<dependency>
<groupId>com.google.gcloud</groupId>
<groupId>com.google.cloud</groupId>
<artifactId>gcloud-java</artifactId>
<version>0.1.7</version>
<version>0.2.0</version>
</dependency>
</dependencies>
<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import static spark.Spark.port;

import com.google.gcloud.datastore.DatastoreOptions;
import com.google.cloud.datastore.DatastoreOptions;

public class Main {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@

import static com.google.common.base.Preconditions.checkArgument;

import com.google.gcloud.datastore.Datastore;
import com.google.gcloud.datastore.Entity;
import com.google.gcloud.datastore.Key;
import com.google.gcloud.datastore.KeyFactory;
import com.google.gcloud.datastore.Query;
import com.google.gcloud.datastore.QueryResults;
import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.Entity;
import com.google.cloud.datastore.Key;
import com.google.cloud.datastore.KeyFactory;
import com.google.cloud.datastore.Query;
import com.google.cloud.datastore.QueryResults;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import com.google.cloud.datastore.Datastore;
import com.google.cloud.datastore.DatastoreOptions;
import com.google.cloud.datastore.Entity;
import com.google.cloud.datastore.Key;
import com.google.cloud.datastore.Query;
import com.google.cloud.datastore.QueryResults;
import com.google.cloud.datastore.StructuredQuery;
import com.google.cloud.datastore.testing.LocalDatastoreHelper;
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.Query;
import com.google.gcloud.datastore.QueryResults;
import com.google.gcloud.datastore.StructuredQuery;
import com.google.gcloud.datastore.testing.LocalGcdHelper;

import org.junit.AfterClass;
import org.junit.Before;
Expand All @@ -41,54 +41,44 @@

public class UserServiceTest {

private static final int PORT = LocalGcdHelper.findAvailablePort(LocalGcdHelper.DEFAULT_PORT);
private static final String PROJECT_ID = LocalGcdHelper.DEFAULT_PROJECT_ID;
private static final LocalDatastoreHelper HELPER = LocalDatastoreHelper.create(1.0);
private static final DatastoreOptions DATASTORE_OPTIONS = HELPER.options();
private static final Datastore DATASTORE = DATASTORE_OPTIONS.service();
private static final String KIND = "DemoUser";
private static final UserService USER_SERVICE = new UserService(DATASTORE, KIND);
private static final String USER_ID = "myId";
private static final String USER_NAME = "myName";
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(DATASTORE_OPTIONS.projectId(), KIND, USER_ID).build();
private static final Entity USER_RECORD = Entity.builder(USER_KEY)
.set("id", USER_ID)
.set("name", USER_NAME)
.set("email", USER_EMAIL)
.build();
private static LocalGcdHelper gcdHelper;
private static Datastore datastore;
private static UserService userService;

@BeforeClass
public static void beforeClass() throws IOException, InterruptedException {
if (!LocalGcdHelper.isActive(PROJECT_ID, PORT)) {
gcdHelper = LocalGcdHelper.start(PROJECT_ID, PORT, 1.0);
}
datastore = DatastoreOptions.builder()
.projectId(PROJECT_ID)
.host("http://localhost:" + PORT)
.build()
.service();
userService = new UserService(datastore, KIND);
HELPER.start();
}

@Before
public void setUp() {
StructuredQuery<Key> query = Query.keyQueryBuilder().build();
QueryResults<Key> result = datastore.run(query);
datastore.delete(Iterators.toArray(result, Key.class));
datastore.add(USER_RECORD);
QueryResults<Key> result = DATASTORE.run(query);
DATASTORE.delete(Iterators.toArray(result, Key.class));
DATASTORE.add(USER_RECORD);
}

@AfterClass
public static void afterClass() throws IOException, InterruptedException {
if (gcdHelper != null) {
gcdHelper.stop();
}
HELPER.stop();
}

@Test
public void testGetAllUsers() {
List<User> allUsers = userService.getAllUsers();
List<User> allUsers = USER_SERVICE.getAllUsers();
assertEquals(1, allUsers.size());
User actualUser = allUsers.get(0);
assertEquals(USER.getId(), actualUser.getId());
Expand All @@ -100,18 +90,18 @@ public void testGetAllUsers() {
public void testCreateUser() {
String name = "myNewName";
String email = "[email protected]";
User actualUser = userService.createUser(name, email);
User actualUser = USER_SERVICE.createUser(name, email);
assertEquals(name, actualUser.getName());
assertEquals(email, actualUser.getEmail());
assertNotNull(actualUser.getId());
try {
userService.createUser(null, email);
USER_SERVICE.createUser(null, email);
fail("Expected to fail because name is null.");
} catch (IllegalArgumentException e) {
assertEquals("Parameter 'name' cannot be empty", e.getMessage());
}
try {
userService.createUser(name, null);
USER_SERVICE.createUser(name, null);
fail("Expected to fail because email is null.");
} catch (IllegalArgumentException e) {
assertEquals("Parameter 'email' cannot be empty", e.getMessage());
Expand All @@ -120,27 +110,27 @@ public void testCreateUser() {

@Test
public void testDeleteUser() {
String result = userService.deleteUser(USER_ID);
String result = USER_SERVICE.deleteUser(USER_ID);
assertEquals("ok", result);
assertNull(datastore.get(USER_KEY));
assertNull(DATASTORE.get(USER_KEY));
}

@Test
public void testUpdateUser() {
String newName = "myNewName";
String newEmail = "[email protected]";
User updatedUser = userService.updateUser(USER_ID, newName, newEmail);
User updatedUser = USER_SERVICE.updateUser(USER_ID, newName, newEmail);
assertEquals(USER_ID, updatedUser.getId());
assertEquals(newName, updatedUser.getName());
assertEquals(newEmail, updatedUser.getEmail());
try {
userService.updateUser(USER_ID, null, USER_EMAIL);
USER_SERVICE.updateUser(USER_ID, null, USER_EMAIL);
fail("Expected to fail because name is null.");
} catch (IllegalArgumentException e) {
assertEquals("Parameter 'name' cannot be empty", e.getMessage());
}
try {
userService.updateUser(USER_ID, USER_NAME, null);
USER_SERVICE.updateUser(USER_ID, USER_NAME, null);
fail("Expected to fail because email is null.");
} catch (IllegalArgumentException e) {
assertEquals("Parameter 'email' cannot be empty", e.getMessage());
Expand Down

0 comments on commit f78394c

Please sign in to comment.