From 16375ec7a3cd2de9806969f8ad87ff96cd03f1b1 Mon Sep 17 00:00:00 2001 From: Ajay Kannan Date: Tue, 12 Apr 2016 13:11:42 -0700 Subject: [PATCH] Update gcloud-java dependency and imports --- datastore/pom.xml | 4 +- .../google/datastore/snippets/Concepts.java | 66 ++++++++---------- .../google/datastore/snippets/TaskList.java | 20 +++--- managed_vms/cloudstorage/pom.xml | 4 +- .../cloudstorage/UploadServlet.java | 10 +-- managed_vms/datastore/pom.xml | 4 +- .../datastore/DatastoreServlet.java | 20 +++--- managed_vms/sparkjava/pom.xml | 4 +- .../com/google/appengine/sparkdemo/Main.java | 2 +- .../appengine/sparkdemo/UserService.java | 12 ++-- .../appengine/sparkdemo/UserServiceTest.java | 68 ++++++++----------- 11 files changed, 96 insertions(+), 118 deletions(-) diff --git a/datastore/pom.xml b/datastore/pom.xml index d041997670f..b407cf8fa21 100644 --- a/datastore/pom.xml +++ b/datastore/pom.xml @@ -21,9 +21,9 @@ - com.google.gcloud + com.google.cloud gcloud-java-datastore - 0.1.7 + 0.2.0 junit diff --git a/datastore/src/main/java/com/google/datastore/snippets/Concepts.java b/datastore/src/main/java/com/google/datastore/snippets/Concepts.java index 67d32a11c5d..1817cee78de 100644 --- a/datastore/src/main/java/com/google/datastore/snippets/Concepts.java +++ b/datastore/src/main/java/com/google/datastore/snippets/Concepts.java @@ -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; @@ -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 TEST_FULL_ENTITY = FullEntity.builder().build(); private Datastore datastore; @@ -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(); } /** @@ -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 query = Query.keyQueryBuilder().build(); QueryResults result = datastore.run(query); datastore.delete(Iterators.toArray(result, Key.class)); @@ -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) { diff --git a/datastore/src/main/java/com/google/datastore/snippets/TaskList.java b/datastore/src/main/java/com/google/datastore/snippets/TaskList.java index 371d41e35f4..8ff5b53059a 100644 --- a/datastore/src/main/java/com/google/datastore/snippets/TaskList.java +++ b/datastore/src/main/java/com/google/datastore/snippets/TaskList.java @@ -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; diff --git a/managed_vms/cloudstorage/pom.xml b/managed_vms/cloudstorage/pom.xml index aeef1bd9e61..e09a2a27d91 100644 --- a/managed_vms/cloudstorage/pom.xml +++ b/managed_vms/cloudstorage/pom.xml @@ -24,9 +24,9 @@ - com.google.gcloud + com.google.cloud gcloud-java-storage - 0.1.7 + 0.2.0 diff --git a/managed_vms/cloudstorage/src/main/java/com/example/managedvms/cloudstorage/UploadServlet.java b/managed_vms/cloudstorage/src/main/java/com/example/managedvms/cloudstorage/UploadServlet.java index 03ba55f9a12..fc3c39e26dc 100644 --- a/managed_vms/cloudstorage/src/main/java/com/example/managedvms/cloudstorage/UploadServlet.java +++ b/managed_vms/cloudstorage/src/main/java/com/example/managedvms/cloudstorage/UploadServlet.java @@ -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; diff --git a/managed_vms/datastore/pom.xml b/managed_vms/datastore/pom.xml index e2e0f1a7943..9bd94037992 100644 --- a/managed_vms/datastore/pom.xml +++ b/managed_vms/datastore/pom.xml @@ -24,9 +24,9 @@ - com.google.gcloud + com.google.cloud gcloud-java-datastore - 0.1.7 + 0.2.0 diff --git a/managed_vms/datastore/src/main/java/com/example/managedvms/datastore/DatastoreServlet.java b/managed_vms/datastore/src/main/java/com/example/managedvms/datastore/DatastoreServlet.java index 363c2263c48..37aff9d5a34 100644 --- a/managed_vms/datastore/src/main/java/com/example/managedvms/datastore/DatastoreServlet.java +++ b/managed_vms/datastore/src/main/java/com/example/managedvms/datastore/DatastoreServlet.java @@ -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; diff --git a/managed_vms/sparkjava/pom.xml b/managed_vms/sparkjava/pom.xml index 0d810b46f21..cdd77b4ed7d 100644 --- a/managed_vms/sparkjava/pom.xml +++ b/managed_vms/sparkjava/pom.xml @@ -36,9 +36,9 @@ 4.12 - com.google.gcloud + com.google.cloud gcloud-java - 0.1.7 + 0.2.0 diff --git a/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/Main.java b/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/Main.java index 9864d2ef7c2..745a31f6945 100644 --- a/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/Main.java +++ b/managed_vms/sparkjava/src/main/java/com/google/appengine/sparkdemo/Main.java @@ -18,7 +18,7 @@ import static spark.Spark.port; -import com.google.gcloud.datastore.DatastoreOptions; +import com.google.cloud.datastore.DatastoreOptions; public class Main { 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 9e6683ee592..dce6dc7baeb 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 @@ -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; 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 f5daff6be0c..5d4ba6797a7 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 @@ -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; @@ -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 = "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).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 query = Query.keyQueryBuilder().build(); - QueryResults result = datastore.run(query); - datastore.delete(Iterators.toArray(result, Key.class)); - datastore.add(USER_RECORD); + QueryResults 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 allUsers = userService.getAllUsers(); + List allUsers = USER_SERVICE.getAllUsers(); assertEquals(1, allUsers.size()); User actualUser = allUsers.get(0); assertEquals(USER.getId(), actualUser.getId()); @@ -100,18 +90,18 @@ public void testGetAllUsers() { public void testCreateUser() { String name = "myNewName"; String email = "mynew@email.com"; - 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()); @@ -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 = "mynew@email.com"; - 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());