-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Update gcloud-java dependency and imports #159
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = "[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()); | ||
|
@@ -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()); | ||
|
@@ -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()); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ajkannan I think it would be nice if we overloaded
options
in the helper with adefaultNamespace
.Also, we should probably document the namespace setter in the Builder and getter in the options to suggests
that this is the "default namespace".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I just made issue googleapis/google-cloud-java#910 in our repo to do that.