-
Notifications
You must be signed in to change notification settings - Fork 68
chore: Add HttpJson CRUD Showcase tests #1589
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
Changes from 9 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e1a20b9
chore: Add CRUD showcase test
lqiu96 b7b4846
chore: Add test name for parameterized tests
lqiu96 a30be2f
chore: Remove parameterized tests
lqiu96 a4281f3
Merge remote-tracking branch 'origin' into main-showcase_crud
lqiu96 d47cdb9
chore: Clean up CRUD test
lqiu96 3f63826
chore: Update comment
lqiu96 3003750
chore: Add additional test case for CRUD
lqiu96 9ca34fc
chore: Update tests
lqiu96 c91ef95
chore: Clean up tests
lqiu96 221c414
Merge remote-tracking branch 'origin' into main-showcase_crud
lqiu96 d0234c7
Merge branch 'main' into main-showcase_crud
lqiu96 c42f728
chore: Updated to split each RPC to its own test
lqiu96 16f834f
chore: Address PR comments
lqiu96 a004e5c
chore: Address PR comments
lqiu96 e852b2e
chore: Add some documentation to methods
lqiu96 6a95069
chore: Address PR comments
lqiu96 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
159 changes: 159 additions & 0 deletions
159
showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| /* | ||
| * Copyright 2023 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.google.showcase.v1beta1.it; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
|
|
||
| import com.google.api.client.http.javanet.NetHttpTransport; | ||
| import com.google.api.gax.core.NoCredentialsProvider; | ||
| import com.google.protobuf.FieldMask; | ||
| import com.google.showcase.v1beta1.CreateUserRequest; | ||
| import com.google.showcase.v1beta1.DeleteUserRequest; | ||
| import com.google.showcase.v1beta1.EchoSettings; | ||
| import com.google.showcase.v1beta1.IdentityClient; | ||
| import com.google.showcase.v1beta1.IdentitySettings; | ||
| import com.google.showcase.v1beta1.ListUsersRequest; | ||
| import com.google.showcase.v1beta1.ListUsersResponse; | ||
| import com.google.showcase.v1beta1.UpdateUserRequest; | ||
| import com.google.showcase.v1beta1.User; | ||
| import java.io.IOException; | ||
| import java.security.GeneralSecurityException; | ||
| import java.util.Arrays; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| public class ITCrud { | ||
|
|
||
| private IdentityClient httpJsonClient; | ||
|
|
||
| @Before | ||
| public void setup() throws GeneralSecurityException, IOException { | ||
| // Create HttpJson IdentityClient | ||
| IdentitySettings httpJsonIdentitySettings = | ||
| IdentitySettings.newHttpJsonBuilder() | ||
| .setCredentialsProvider(NoCredentialsProvider.create()) | ||
| .setTransportChannelProvider( | ||
| EchoSettings.defaultHttpJsonTransportProviderBuilder() | ||
| .setHttpTransport( | ||
| new NetHttpTransport.Builder().doNotValidateCertificate().build()) | ||
| .setEndpoint("http://localhost:7469") | ||
| .build()) | ||
| .build(); | ||
| httpJsonClient = IdentityClient.create(httpJsonIdentitySettings); | ||
|
|
||
| // Ensure an empty state before each run | ||
| cleanupData(httpJsonClient); | ||
| } | ||
|
|
||
| @After | ||
| public void cleanup() { | ||
| httpJsonClient.close(); | ||
| } | ||
|
|
||
| private void cleanupData(IdentityClient identityClient) { | ||
| IdentityClient.ListUsersPagedResponse pagedResponse = | ||
| identityClient.listUsers(ListUsersRequest.newBuilder().setPageSize(5).build()); | ||
| for (IdentityClient.ListUsersPage listUsersPage : pagedResponse.iteratePages()) { | ||
| for (User user : listUsersPage.getResponse().getUsersList()) { | ||
| identityClient.deleteUser(user.getName()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // This test runs through the four CRUD operations. The operations are | ||
| // set to build off each other and live inside this one test case. | ||
| // The tests run in the order of: Create -> List -> Update -> Delete | ||
lqiu96 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @Test | ||
| public void testHttpJson_CRUD() { | ||
| User user = | ||
| User.newBuilder() | ||
| .setDisplayName("Jane Doe") | ||
| .setEmail("[email protected]") | ||
| .setNickname("Doe") | ||
| .setHeightFeet(5) | ||
| .setAge(25) | ||
| .build(); | ||
| User createUserResponse = | ||
| httpJsonClient.createUser(CreateUserRequest.newBuilder().setUser(user).build()); | ||
|
|
||
| assertThat(createUserResponse.getDisplayName()).isEqualTo(user.getDisplayName()); | ||
| assertThat(createUserResponse.getEmail()).isEqualTo(user.getEmail()); | ||
| assertThat(createUserResponse.getNickname()).isEqualTo(user.getNickname()); | ||
| assertThat(createUserResponse.getHeightFeet()).isEqualTo(user.getHeightFeet()); | ||
| assertThat(createUserResponse.getAge()).isEqualTo(user.getAge()); | ||
|
|
||
| // Assert that the server populates these fields | ||
| assertThat(createUserResponse.getName()).isNotEmpty(); | ||
| assertThat(createUserResponse.getCreateTime()).isNotNull(); | ||
| assertThat(createUserResponse.getUpdateTime()).isNotNull(); | ||
| assertThat(createUserResponse.getEnableNotifications()).isNotNull(); | ||
|
|
||
| // Assert that only one User exists | ||
| IdentityClient.ListUsersPagedResponse listUsersPagedResponse = | ||
| httpJsonClient.listUsers(ListUsersRequest.newBuilder().setPageSize(5).build()); | ||
| ListUsersResponse listUsersResponse = listUsersPagedResponse.getPage().getResponse(); | ||
| assertThat(listUsersResponse.getUsersList().size()).isEqualTo(1); | ||
|
|
||
| // Assert that the user that exists is Jane Doe. Check that the response | ||
| // from both List (pagination) and Get returns Jane Doe | ||
| // List Users | ||
| User listUserResponse = listUsersResponse.getUsers(0); | ||
| assertThat(listUserResponse).isEqualTo(createUserResponse); | ||
|
|
||
| // Get User | ||
| User getUserResponse = httpJsonClient.getUser(createUserResponse.getName()); | ||
| assertThat(getUserResponse).isEqualTo(createUserResponse); | ||
|
|
||
| // Update multiple fields in the User. Age + Nickname are not included in the FieldMask | ||
| User updateUser = | ||
| getUserResponse | ||
| .toBuilder() | ||
| .setAge(50) | ||
| .setNickname("Smith") | ||
| .setEmail("[email protected]") | ||
| .setHeightFeet(6.0) | ||
| .setEnableNotifications(true) | ||
| .build(); | ||
| User updateUserResponse = | ||
| httpJsonClient.updateUser( | ||
| UpdateUserRequest.newBuilder() | ||
| .setUser(updateUser) | ||
| .setUpdateMask( | ||
| FieldMask.newBuilder() | ||
| .addAllPaths(Arrays.asList("email", "height_feet", "enable_notifications")) | ||
| .build()) | ||
| .build()); | ||
|
|
||
| // Assert that only the fields in the FieldMask are updated correctly | ||
| assertThat(updateUserResponse).isNotEqualTo(getUserResponse); | ||
| assertThat(updateUserResponse.getAge()).isEqualTo(getUserResponse.getAge()); | ||
| assertThat(updateUserResponse.getNickname()).isEqualTo(getUserResponse.getNickname()); | ||
|
|
||
| assertThat(updateUserResponse.getEmail()).isEqualTo(updateUser.getEmail()); | ||
| assertThat(updateUserResponse.getHeightFeet()).isEqualTo(updateUser.getHeightFeet()); | ||
| assertThat(updateUserResponse.getEnableNotifications()) | ||
| .isEqualTo(updateUser.getEnableNotifications()); | ||
|
|
||
| // Delete the User | ||
| httpJsonClient.deleteUser( | ||
| DeleteUserRequest.newBuilder().setName(getUserResponse.getName()).build()); | ||
|
|
||
| listUsersPagedResponse = | ||
| httpJsonClient.listUsers(ListUsersRequest.newBuilder().setPageSize(5).build()); | ||
| assertThat(listUsersPagedResponse.getPage().getResponse().getUsersList().size()).isEqualTo(0); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.