From e1a20b9c713e7ee9cef6a335dbcd5868a00d3741 Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Wed, 29 Mar 2023 15:00:07 -0400 Subject: [PATCH 01/13] chore: Add CRUD showcase test --- .../google/showcase/v1beta1/it/ITCrud.java | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java new file mode 100644 index 0000000000..406a15d8c2 --- /dev/null +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java @@ -0,0 +1,132 @@ +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.api.gax.grpc.InstantiatingGrpcChannelProvider; +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.UpdateUserRequest; +import com.google.showcase.v1beta1.User; +import io.grpc.ManagedChannelBuilder; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.Arrays; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +@RunWith(Parameterized.class) +public class ITCrud { + + @Parameterized.Parameters + public static IdentitySettings[] data() throws IOException, GeneralSecurityException { + return new IdentitySettings[] { + // gRPC Identity Settings + IdentitySettings.newBuilder() + .setCredentialsProvider(NoCredentialsProvider.create()) + .setTransportChannelProvider( + InstantiatingGrpcChannelProvider.newBuilder() + .setChannelConfigurator(ManagedChannelBuilder::usePlaintext) + .build()) + .build(), + // HttpJson Identity Settings + IdentitySettings.newHttpJsonBuilder() + .setCredentialsProvider(NoCredentialsProvider.create()) + .setTransportChannelProvider( + EchoSettings.defaultHttpJsonTransportProviderBuilder() + .setHttpTransport( + new NetHttpTransport.Builder().doNotValidateCertificate().build()) + .setEndpoint("http://localhost:7469") + .build()) + .build() + }; + } + + @Parameterized.Parameter(0) + public IdentitySettings identitySettings; + + 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()); + } + } + } + + @Test + public void testUserCRUD() throws IOException { + try (IdentityClient identityClient = IdentityClient.create(identitySettings)) { + cleanupData(identityClient); + + CreateUserRequest createUserRequest = + CreateUserRequest.newBuilder() + .setUser( + User.newBuilder() + .setDisplayName("Jane Doe") + .setEmail("janedoe@example.com") + .setNickname("Doe") + .setHeightFeet(5) + .build()) + .build(); + User user = identityClient.createUser(createUserRequest); + User expected = createUserRequest.getUser(); + + assertThat(user.getName()).isNotEmpty(); + assertThat(user.getDisplayName()).isEqualTo(expected.getDisplayName()); + assertThat(user.getEmail()).isEqualTo(expected.getEmail()); + assertThat(user.getCreateTime()).isNotNull(); + assertThat(user.getUpdateTime()).isNotNull(); + assertThat(user.getNickname()).isEqualTo(expected.getNickname()); + assertThat(user.getHeightFeet()).isEqualTo(expected.getHeightFeet()); + assertThat(user.getAge()).isNotNull(); + assertThat(user.getEnableNotifications()).isNotNull(); + + ListUsersRequest listUsersRequest = ListUsersRequest.newBuilder().setPageSize(5).build(); + IdentityClient.ListUsersPagedResponse listUsersPagedResponse = + identityClient.listUsers(listUsersRequest); + assertThat(listUsersPagedResponse.getPage().getResponse().getUsersList().size()).isEqualTo(1); + + User listUserResponse = listUsersPagedResponse.getPage().getResponse().getUsers(0); + assertThat(listUserResponse).isEqualTo(user); + + User getUserResponse = identityClient.getUser(user.getName()); + assertThat(getUserResponse).isEqualTo(user); + + UpdateUserRequest updateUserRequest = + UpdateUserRequest.newBuilder() + .setUser( + user.toBuilder() + .setEmail("janedoe@jane.com") + .setHeightFeet(6.0) + .setEnableNotifications(true) + .build()) + .setUpdateMask( + FieldMask.newBuilder() + .addAllPaths(Arrays.asList("email", "height_feet", "enable_notifications")) + .build()) + .build(); + User updatedUser = identityClient.updateUser(updateUserRequest); + + assertThat(updatedUser).isNotEqualTo(user); + assertThat(updatedUser.getEmail()).isNotEqualTo(user.getEmail()); + assertThat(updatedUser.getHeightFeet()).isNotEqualTo(user.getHeightFeet()); + + assertThat(updatedUser.getEnableNotifications()).isNotEqualTo(user.getEnableNotifications()); + assertThat(updatedUser.getAge()).isNotNull(); + + identityClient.deleteUser(DeleteUserRequest.newBuilder().setName(user.getName()).build()); + + listUsersPagedResponse = identityClient.listUsers(listUsersRequest); + assertThat(listUsersPagedResponse.getPage().getResponse().getUsersList().size()).isEqualTo(0); + } + } +} From b7b4846e6cb7d7f57344fe4eb736bd5dbe7a126b Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Thu, 30 Mar 2023 10:49:19 -0400 Subject: [PATCH 02/13] chore: Add test name for parameterized tests --- .../google/showcase/v1beta1/it/ITCrud.java | 56 +++++++++++-------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java index 406a15d8c2..1cfe0f9052 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java @@ -25,32 +25,39 @@ @RunWith(Parameterized.class) public class ITCrud { - @Parameterized.Parameters - public static IdentitySettings[] data() throws IOException, GeneralSecurityException { - return new IdentitySettings[] { - // gRPC Identity Settings - IdentitySettings.newBuilder() - .setCredentialsProvider(NoCredentialsProvider.create()) - .setTransportChannelProvider( - InstantiatingGrpcChannelProvider.newBuilder() - .setChannelConfigurator(ManagedChannelBuilder::usePlaintext) - .build()) - .build(), - // HttpJson Identity Settings - IdentitySettings.newHttpJsonBuilder() - .setCredentialsProvider(NoCredentialsProvider.create()) - .setTransportChannelProvider( - EchoSettings.defaultHttpJsonTransportProviderBuilder() - .setHttpTransport( - new NetHttpTransport.Builder().doNotValidateCertificate().build()) - .setEndpoint("http://localhost:7469") - .build()) - .build() - }; + @Parameterized.Parameters(name = "CRUD Transport: {0}") + public static String[] transports() { + return new String[] {"gRPC", "httpjson"}; } @Parameterized.Parameter(0) - public IdentitySettings identitySettings; + public String transportName; + + private IdentitySettings getTransportSettings(String transportName) + throws IOException, GeneralSecurityException { + switch (transportName) { + case "gRPC": + return IdentitySettings.newBuilder() + .setCredentialsProvider(NoCredentialsProvider.create()) + .setTransportChannelProvider( + InstantiatingGrpcChannelProvider.newBuilder() + .setChannelConfigurator(ManagedChannelBuilder::usePlaintext) + .build()) + .build(); + case "httpjson": + return IdentitySettings.newHttpJsonBuilder() + .setCredentialsProvider(NoCredentialsProvider.create()) + .setTransportChannelProvider( + EchoSettings.defaultHttpJsonTransportProviderBuilder() + .setHttpTransport( + new NetHttpTransport.Builder().doNotValidateCertificate().build()) + .setEndpoint("http://localhost:7469") + .build()) + .build(); + default: + throw new IllegalArgumentException("Invalid transport name: " + transportName); + } + } private void cleanupData(IdentityClient identityClient) { IdentityClient.ListUsersPagedResponse pagedResponse = @@ -63,7 +70,8 @@ private void cleanupData(IdentityClient identityClient) { } @Test - public void testUserCRUD() throws IOException { + public void testUserCRUD() throws IOException, GeneralSecurityException { + IdentitySettings identitySettings = getTransportSettings(transportName); try (IdentityClient identityClient = IdentityClient.create(identitySettings)) { cleanupData(identityClient); From a30be2f5af1e0c0dd556c59b6b429592c480119a Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Thu, 30 Mar 2023 14:26:49 -0400 Subject: [PATCH 03/13] chore: Remove parameterized tests --- .../google/showcase/v1beta1/it/ITCrud.java | 172 ++++++++---------- 1 file changed, 78 insertions(+), 94 deletions(-) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java index 1cfe0f9052..6b6cee17ae 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java @@ -4,7 +4,6 @@ import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.gax.core.NoCredentialsProvider; -import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; import com.google.protobuf.FieldMask; import com.google.showcase.v1beta1.CreateUserRequest; import com.google.showcase.v1beta1.DeleteUserRequest; @@ -14,38 +13,22 @@ import com.google.showcase.v1beta1.ListUsersRequest; import com.google.showcase.v1beta1.UpdateUserRequest; import com.google.showcase.v1beta1.User; -import io.grpc.ManagedChannelBuilder; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Arrays; +import org.junit.After; +import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -@RunWith(Parameterized.class) public class ITCrud { - @Parameterized.Parameters(name = "CRUD Transport: {0}") - public static String[] transports() { - return new String[] {"gRPC", "httpjson"}; - } - - @Parameterized.Parameter(0) - public String transportName; + private IdentityClient httpJsonClient; - private IdentitySettings getTransportSettings(String transportName) - throws IOException, GeneralSecurityException { - switch (transportName) { - case "gRPC": - return IdentitySettings.newBuilder() - .setCredentialsProvider(NoCredentialsProvider.create()) - .setTransportChannelProvider( - InstantiatingGrpcChannelProvider.newBuilder() - .setChannelConfigurator(ManagedChannelBuilder::usePlaintext) - .build()) - .build(); - case "httpjson": - return IdentitySettings.newHttpJsonBuilder() + @Before + public void setup() throws GeneralSecurityException, IOException { + // Create HttpJson IdentityClient + IdentitySettings httpJsonIdentitySettings = + IdentitySettings.newHttpJsonBuilder() .setCredentialsProvider(NoCredentialsProvider.create()) .setTransportChannelProvider( EchoSettings.defaultHttpJsonTransportProviderBuilder() @@ -54,9 +37,15 @@ private IdentitySettings getTransportSettings(String transportName) .setEndpoint("http://localhost:7469") .build()) .build(); - default: - throw new IllegalArgumentException("Invalid transport name: " + transportName); - } + httpJsonClient = IdentityClient.create(httpJsonIdentitySettings); + + // Ensure an empty state before each run + cleanupData(httpJsonClient); + } + + @After + public void cleanup() { + httpJsonClient.close(); } private void cleanupData(IdentityClient identityClient) { @@ -70,71 +59,66 @@ private void cleanupData(IdentityClient identityClient) { } @Test - public void testUserCRUD() throws IOException, GeneralSecurityException { - IdentitySettings identitySettings = getTransportSettings(transportName); - try (IdentityClient identityClient = IdentityClient.create(identitySettings)) { - cleanupData(identityClient); - - CreateUserRequest createUserRequest = - CreateUserRequest.newBuilder() - .setUser( - User.newBuilder() - .setDisplayName("Jane Doe") - .setEmail("janedoe@example.com") - .setNickname("Doe") - .setHeightFeet(5) - .build()) - .build(); - User user = identityClient.createUser(createUserRequest); - User expected = createUserRequest.getUser(); - - assertThat(user.getName()).isNotEmpty(); - assertThat(user.getDisplayName()).isEqualTo(expected.getDisplayName()); - assertThat(user.getEmail()).isEqualTo(expected.getEmail()); - assertThat(user.getCreateTime()).isNotNull(); - assertThat(user.getUpdateTime()).isNotNull(); - assertThat(user.getNickname()).isEqualTo(expected.getNickname()); - assertThat(user.getHeightFeet()).isEqualTo(expected.getHeightFeet()); - assertThat(user.getAge()).isNotNull(); - assertThat(user.getEnableNotifications()).isNotNull(); - - ListUsersRequest listUsersRequest = ListUsersRequest.newBuilder().setPageSize(5).build(); - IdentityClient.ListUsersPagedResponse listUsersPagedResponse = - identityClient.listUsers(listUsersRequest); - assertThat(listUsersPagedResponse.getPage().getResponse().getUsersList().size()).isEqualTo(1); - - User listUserResponse = listUsersPagedResponse.getPage().getResponse().getUsers(0); - assertThat(listUserResponse).isEqualTo(user); - - User getUserResponse = identityClient.getUser(user.getName()); - assertThat(getUserResponse).isEqualTo(user); - - UpdateUserRequest updateUserRequest = - UpdateUserRequest.newBuilder() - .setUser( - user.toBuilder() - .setEmail("janedoe@jane.com") - .setHeightFeet(6.0) - .setEnableNotifications(true) - .build()) - .setUpdateMask( - FieldMask.newBuilder() - .addAllPaths(Arrays.asList("email", "height_feet", "enable_notifications")) - .build()) - .build(); - User updatedUser = identityClient.updateUser(updateUserRequest); - - assertThat(updatedUser).isNotEqualTo(user); - assertThat(updatedUser.getEmail()).isNotEqualTo(user.getEmail()); - assertThat(updatedUser.getHeightFeet()).isNotEqualTo(user.getHeightFeet()); - - assertThat(updatedUser.getEnableNotifications()).isNotEqualTo(user.getEnableNotifications()); - assertThat(updatedUser.getAge()).isNotNull(); - - identityClient.deleteUser(DeleteUserRequest.newBuilder().setName(user.getName()).build()); - - listUsersPagedResponse = identityClient.listUsers(listUsersRequest); - assertThat(listUsersPagedResponse.getPage().getResponse().getUsersList().size()).isEqualTo(0); - } + public void testHttpJson_UserCRUD() { + CreateUserRequest createUserRequest = + CreateUserRequest.newBuilder() + .setUser( + User.newBuilder() + .setDisplayName("Jane Doe") + .setEmail("janedoe@example.com") + .setNickname("Doe") + .setHeightFeet(5) + .build()) + .build(); + User user = httpJsonClient.createUser(createUserRequest); + User expected = createUserRequest.getUser(); + + assertThat(user.getName()).isNotEmpty(); + assertThat(user.getDisplayName()).isEqualTo(expected.getDisplayName()); + assertThat(user.getEmail()).isEqualTo(expected.getEmail()); + assertThat(user.getCreateTime()).isNotNull(); + assertThat(user.getUpdateTime()).isNotNull(); + assertThat(user.getNickname()).isEqualTo(expected.getNickname()); + assertThat(user.getHeightFeet()).isEqualTo(expected.getHeightFeet()); + assertThat(user.getAge()).isNotNull(); + assertThat(user.getEnableNotifications()).isNotNull(); + + ListUsersRequest listUsersRequest = ListUsersRequest.newBuilder().setPageSize(5).build(); + IdentityClient.ListUsersPagedResponse listUsersPagedResponse = + httpJsonClient.listUsers(listUsersRequest); + assertThat(listUsersPagedResponse.getPage().getResponse().getUsersList().size()).isEqualTo(1); + + User listUserResponse = listUsersPagedResponse.getPage().getResponse().getUsers(0); + assertThat(listUserResponse).isEqualTo(user); + + User getUserResponse = httpJsonClient.getUser(user.getName()); + assertThat(getUserResponse).isEqualTo(user); + + UpdateUserRequest updateUserRequest = + UpdateUserRequest.newBuilder() + .setUser( + user.toBuilder() + .setEmail("janedoe@jane.com") + .setHeightFeet(6.0) + .setEnableNotifications(true) + .build()) + .setUpdateMask( + FieldMask.newBuilder() + .addAllPaths(Arrays.asList("email", "height_feet", "enable_notifications")) + .build()) + .build(); + User updatedUser = httpJsonClient.updateUser(updateUserRequest); + + assertThat(updatedUser).isNotEqualTo(user); + assertThat(updatedUser.getEmail()).isNotEqualTo(user.getEmail()); + assertThat(updatedUser.getHeightFeet()).isNotEqualTo(user.getHeightFeet()); + + assertThat(updatedUser.getEnableNotifications()).isNotEqualTo(user.getEnableNotifications()); + assertThat(updatedUser.getAge()).isNotNull(); + + httpJsonClient.deleteUser(DeleteUserRequest.newBuilder().setName(user.getName()).build()); + + listUsersPagedResponse = httpJsonClient.listUsers(listUsersRequest); + assertThat(listUsersPagedResponse.getPage().getResponse().getUsersList().size()).isEqualTo(0); } } From d47cdb96fe5d7b51949b7955bf01b3adb1f5ca2b Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Mon, 3 Apr 2023 12:29:45 -0400 Subject: [PATCH 04/13] chore: Clean up CRUD test --- .../google/showcase/v1beta1/it/ITCrud.java | 126 +++++++++++------- 1 file changed, 78 insertions(+), 48 deletions(-) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java index 6b6cee17ae..010e1f2d9b 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java @@ -1,3 +1,18 @@ +/* + * 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; @@ -11,6 +26,7 @@ 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; @@ -58,67 +74,81 @@ private void cleanupData(IdentityClient identityClient) { } } + // This test runs through the four CRUD operations. The operations + // build off each other and all exist inside this one test case + // The tests run the order of: + // 1. Create (Jane Doe) + // 2. List (Jane Doe has been created) + // 3. Update (Jane Doe has been updated) + // 4. Delete (Jane Doe no longer exists) @Test - public void testHttpJson_UserCRUD() { - CreateUserRequest createUserRequest = - CreateUserRequest.newBuilder() - .setUser( - User.newBuilder() - .setDisplayName("Jane Doe") - .setEmail("janedoe@example.com") - .setNickname("Doe") - .setHeightFeet(5) - .build()) + public void testHttpJson_CRUD() { + User userRequest = + User.newBuilder() + .setDisplayName("Jane Doe") + .setEmail("janedoe@example.com") + .setNickname("Doe") + .setHeightFeet(5) + .setAge(25) .build(); - User user = httpJsonClient.createUser(createUserRequest); - User expected = createUserRequest.getUser(); - - assertThat(user.getName()).isNotEmpty(); - assertThat(user.getDisplayName()).isEqualTo(expected.getDisplayName()); - assertThat(user.getEmail()).isEqualTo(expected.getEmail()); - assertThat(user.getCreateTime()).isNotNull(); - assertThat(user.getUpdateTime()).isNotNull(); - assertThat(user.getNickname()).isEqualTo(expected.getNickname()); - assertThat(user.getHeightFeet()).isEqualTo(expected.getHeightFeet()); - assertThat(user.getAge()).isNotNull(); - assertThat(user.getEnableNotifications()).isNotNull(); - - ListUsersRequest listUsersRequest = ListUsersRequest.newBuilder().setPageSize(5).build(); + CreateUserRequest createUserRequest = + CreateUserRequest.newBuilder().setUser(userRequest).build(); + User createUserResponse = httpJsonClient.createUser(createUserRequest); + + assertThat(createUserResponse.getDisplayName()).isEqualTo(userRequest.getDisplayName()); + assertThat(createUserResponse.getEmail()).isEqualTo(userRequest.getEmail()); + assertThat(createUserResponse.getNickname()).isEqualTo(userRequest.getNickname()); + assertThat(createUserResponse.getHeightFeet()).isEqualTo(userRequest.getHeightFeet()); + assertThat(createUserResponse.getAge()).isEqualTo(userRequest.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 and that the user is Jane Doe + // We run this for both List (Pagination) and Get IdentityClient.ListUsersPagedResponse listUsersPagedResponse = - httpJsonClient.listUsers(listUsersRequest); - assertThat(listUsersPagedResponse.getPage().getResponse().getUsersList().size()).isEqualTo(1); - - User listUserResponse = listUsersPagedResponse.getPage().getResponse().getUsers(0); - assertThat(listUserResponse).isEqualTo(user); - - User getUserResponse = httpJsonClient.getUser(user.getName()); - assertThat(getUserResponse).isEqualTo(user); - + httpJsonClient.listUsers(ListUsersRequest.newBuilder().setPageSize(5).build()); + ListUsersResponse listUsersResponse = listUsersPagedResponse.getPage().getResponse(); + assertThat(listUsersResponse.getUsersList().size()).isEqualTo(1); + User listUserResponse = listUsersResponse.getUsers(0); + assertThat(listUserResponse).isEqualTo(createUserResponse); + + // Get User + User getUserResponse = httpJsonClient.getUser(createUserResponse.getName()); + assertThat(getUserResponse).isEqualTo(createUserResponse); + + User expectedUpdatedUser = + createUserResponse + .toBuilder() + .setEmail("janedoe@jane.com") + .setHeightFeet(6.0) + .setEnableNotifications(true) + .build(); UpdateUserRequest updateUserRequest = UpdateUserRequest.newBuilder() - .setUser( - user.toBuilder() - .setEmail("janedoe@jane.com") - .setHeightFeet(6.0) - .setEnableNotifications(true) - .build()) + .setUser(expectedUpdatedUser) .setUpdateMask( FieldMask.newBuilder() .addAllPaths(Arrays.asList("email", "height_feet", "enable_notifications")) .build()) .build(); - User updatedUser = httpJsonClient.updateUser(updateUserRequest); - - assertThat(updatedUser).isNotEqualTo(user); - assertThat(updatedUser.getEmail()).isNotEqualTo(user.getEmail()); - assertThat(updatedUser.getHeightFeet()).isNotEqualTo(user.getHeightFeet()); + User updateUserResponse = httpJsonClient.updateUser(updateUserRequest); - assertThat(updatedUser.getEnableNotifications()).isNotEqualTo(user.getEnableNotifications()); - assertThat(updatedUser.getAge()).isNotNull(); + // Assert that the fields are updated correctly + assertThat(updateUserResponse).isNotEqualTo(createUserResponse); + assertThat(updateUserResponse.getEmail()).isEqualTo(expectedUpdatedUser.getEmail()); + assertThat(updateUserResponse.getHeightFeet()).isEqualTo(expectedUpdatedUser.getHeightFeet()); + assertThat(updateUserResponse.getEnableNotifications()) + .isEqualTo(expectedUpdatedUser.getEnableNotifications()); - httpJsonClient.deleteUser(DeleteUserRequest.newBuilder().setName(user.getName()).build()); + httpJsonClient.deleteUser( + DeleteUserRequest.newBuilder().setName(createUserResponse.getName()).build()); - listUsersPagedResponse = httpJsonClient.listUsers(listUsersRequest); + listUsersPagedResponse = + httpJsonClient.listUsers(ListUsersRequest.newBuilder().setPageSize(5).build()); assertThat(listUsersPagedResponse.getPage().getResponse().getUsersList().size()).isEqualTo(0); } } From 3f6382622b51fb2b8ff09949e1ec3d1dca6f4167 Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Wed, 5 Apr 2023 10:17:00 -0400 Subject: [PATCH 05/13] chore: Update comment --- .../src/test/java/com/google/showcase/v1beta1/it/ITCrud.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java index 010e1f2d9b..c678ca9980 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java @@ -77,10 +77,7 @@ private void cleanupData(IdentityClient identityClient) { // This test runs through the four CRUD operations. The operations // build off each other and all exist inside this one test case // The tests run the order of: - // 1. Create (Jane Doe) - // 2. List (Jane Doe has been created) - // 3. Update (Jane Doe has been updated) - // 4. Delete (Jane Doe no longer exists) + // Create -> List -> Update -> qDelete @Test public void testHttpJson_CRUD() { User userRequest = From 3003750d6b06b8943629686bcc98fbd7e207aa65 Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Wed, 5 Apr 2023 11:02:00 -0400 Subject: [PATCH 06/13] chore: Add additional test case for CRUD --- .../google/showcase/v1beta1/it/ITCrud.java | 48 ++++++++++--------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java index c678ca9980..3849142b4e 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java @@ -74,10 +74,9 @@ private void cleanupData(IdentityClient identityClient) { } } - // This test runs through the four CRUD operations. The operations - // build off each other and all exist inside this one test case - // The tests run the order of: - // Create -> List -> Update -> qDelete + // This test runs through the four CRUD operations. The operations are + // set to build off each other. They exist inside this one test case + // The tests run the order of: Create -> List -> Update -> Delete @Test public void testHttpJson_CRUD() { User userRequest = @@ -88,9 +87,8 @@ public void testHttpJson_CRUD() { .setHeightFeet(5) .setAge(25) .build(); - CreateUserRequest createUserRequest = - CreateUserRequest.newBuilder().setUser(userRequest).build(); - User createUserResponse = httpJsonClient.createUser(createUserRequest); + User createUserResponse = + httpJsonClient.createUser(CreateUserRequest.newBuilder().setUser(userRequest).build()); assertThat(createUserResponse.getDisplayName()).isEqualTo(userRequest.getDisplayName()); assertThat(createUserResponse.getEmail()).isEqualTo(userRequest.getEmail()); @@ -105,7 +103,7 @@ public void testHttpJson_CRUD() { assertThat(createUserResponse.getEnableNotifications()).isNotNull(); // Assert that only one User exists and that the user is Jane Doe - // We run this for both List (Pagination) and Get + // Run this for both List (Pagination) and Get IdentityClient.ListUsersPagedResponse listUsersPagedResponse = httpJsonClient.listUsers(ListUsersRequest.newBuilder().setPageSize(5).build()); ListUsersResponse listUsersResponse = listUsersPagedResponse.getPage().getResponse(); @@ -117,16 +115,18 @@ public void testHttpJson_CRUD() { User getUserResponse = httpJsonClient.getUser(createUserResponse.getName()); assertThat(getUserResponse).isEqualTo(createUserResponse); - User expectedUpdatedUser = - createUserResponse - .toBuilder() - .setEmail("janedoe@jane.com") - .setHeightFeet(6.0) - .setEnableNotifications(true) - .build(); + // Update multiple fields in the User. Age + Nickname are not included in the FieldMask UpdateUserRequest updateUserRequest = UpdateUserRequest.newBuilder() - .setUser(expectedUpdatedUser) + .setUser( + createUserResponse + .toBuilder() + .setAge(50) + .setNickname("Smith") + .setEmail("janedoe@jane.com") + .setHeightFeet(6.0) + .setEnableNotifications(true) + .build()) .setUpdateMask( FieldMask.newBuilder() .addAllPaths(Arrays.asList("email", "height_feet", "enable_notifications")) @@ -134,15 +134,17 @@ public void testHttpJson_CRUD() { .build(); User updateUserResponse = httpJsonClient.updateUser(updateUserRequest); - // Assert that the fields are updated correctly - assertThat(updateUserResponse).isNotEqualTo(createUserResponse); - assertThat(updateUserResponse.getEmail()).isEqualTo(expectedUpdatedUser.getEmail()); - assertThat(updateUserResponse.getHeightFeet()).isEqualTo(expectedUpdatedUser.getHeightFeet()); - assertThat(updateUserResponse.getEnableNotifications()) - .isEqualTo(expectedUpdatedUser.getEnableNotifications()); + // 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("janedoe@jane.com"); + assertThat(updateUserResponse.getHeightFeet()).isEqualTo(6.0); + assertThat(updateUserResponse.getEnableNotifications()).isEqualTo(true); + // Delete the User httpJsonClient.deleteUser( - DeleteUserRequest.newBuilder().setName(createUserResponse.getName()).build()); + DeleteUserRequest.newBuilder().setName(getUserResponse.getName()).build()); listUsersPagedResponse = httpJsonClient.listUsers(ListUsersRequest.newBuilder().setPageSize(5).build()); From 9ca34fc3210fbf4383da6de63f84f222eef6890f Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Wed, 5 Apr 2023 11:10:09 -0400 Subject: [PATCH 07/13] chore: Update tests --- .../test/java/com/google/showcase/v1beta1/it/ITCrud.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java index 3849142b4e..bcf380d100 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java @@ -102,12 +102,15 @@ public void testHttpJson_CRUD() { assertThat(createUserResponse.getUpdateTime()).isNotNull(); assertThat(createUserResponse.getEnableNotifications()).isNotNull(); - // Assert that only one User exists and that the user is Jane Doe - // Run this for both List (Pagination) and Get + // 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); @@ -119,7 +122,7 @@ public void testHttpJson_CRUD() { UpdateUserRequest updateUserRequest = UpdateUserRequest.newBuilder() .setUser( - createUserResponse + getUserResponse .toBuilder() .setAge(50) .setNickname("Smith") From c91ef9554dd648c092dc4292faad347a4988bfc9 Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Wed, 5 Apr 2023 12:51:34 -0400 Subject: [PATCH 08/13] chore: Clean up tests --- .../google/showcase/v1beta1/it/ITCrud.java | 59 ++++++++++--------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java index bcf380d100..d82e2b56e5 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java @@ -75,11 +75,11 @@ private void cleanupData(IdentityClient identityClient) { } // This test runs through the four CRUD operations. The operations are - // set to build off each other. They exist inside this one test case - // The tests run the order of: Create -> List -> Update -> Delete + // set to build off each other and live inside this one test case. + // The tests run in the order of: Create -> List -> Update -> Delete @Test public void testHttpJson_CRUD() { - User userRequest = + User user = User.newBuilder() .setDisplayName("Jane Doe") .setEmail("janedoe@example.com") @@ -88,13 +88,13 @@ public void testHttpJson_CRUD() { .setAge(25) .build(); User createUserResponse = - httpJsonClient.createUser(CreateUserRequest.newBuilder().setUser(userRequest).build()); + httpJsonClient.createUser(CreateUserRequest.newBuilder().setUser(user).build()); - assertThat(createUserResponse.getDisplayName()).isEqualTo(userRequest.getDisplayName()); - assertThat(createUserResponse.getEmail()).isEqualTo(userRequest.getEmail()); - assertThat(createUserResponse.getNickname()).isEqualTo(userRequest.getNickname()); - assertThat(createUserResponse.getHeightFeet()).isEqualTo(userRequest.getHeightFeet()); - assertThat(createUserResponse.getAge()).isEqualTo(userRequest.getAge()); + 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(); @@ -119,31 +119,34 @@ public void testHttpJson_CRUD() { assertThat(getUserResponse).isEqualTo(createUserResponse); // Update multiple fields in the User. Age + Nickname are not included in the FieldMask - UpdateUserRequest updateUserRequest = - UpdateUserRequest.newBuilder() - .setUser( - getUserResponse - .toBuilder() - .setAge(50) - .setNickname("Smith") - .setEmail("janedoe@jane.com") - .setHeightFeet(6.0) - .setEnableNotifications(true) - .build()) - .setUpdateMask( - FieldMask.newBuilder() - .addAllPaths(Arrays.asList("email", "height_feet", "enable_notifications")) - .build()) + User updateUser = + getUserResponse + .toBuilder() + .setAge(50) + .setNickname("Smith") + .setEmail("janedoe@jane.com") + .setHeightFeet(6.0) + .setEnableNotifications(true) .build(); - User updateUserResponse = httpJsonClient.updateUser(updateUserRequest); + 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("janedoe@jane.com"); - assertThat(updateUserResponse.getHeightFeet()).isEqualTo(6.0); - assertThat(updateUserResponse.getEnableNotifications()).isEqualTo(true); + + assertThat(updateUserResponse.getEmail()).isEqualTo(updateUser.getEmail()); + assertThat(updateUserResponse.getHeightFeet()).isEqualTo(updateUser.getHeightFeet()); + assertThat(updateUserResponse.getEnableNotifications()) + .isEqualTo(updateUser.getEnableNotifications()); // Delete the User httpJsonClient.deleteUser( From c42f728db37ed1e33763b8198376bac59c47581f Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Fri, 7 Apr 2023 15:12:51 -0400 Subject: [PATCH 09/13] chore: Updated to split each RPC to its own test --- .../google/showcase/v1beta1/it/ITCrud.java | 88 +++++++++++-------- 1 file changed, 53 insertions(+), 35 deletions(-) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java index d82e2b56e5..c74996e4f9 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java @@ -38,6 +38,15 @@ public class ITCrud { + private static final User DEFAULT_USER = + User.newBuilder() + .setDisplayName("Jane Doe") + .setEmail("janedoe@example.com") + .setNickname("Doe") + .setHeightFeet(5) + .setAge(25) + .build(); + private IdentityClient httpJsonClient; @Before @@ -72,55 +81,60 @@ private void cleanupData(IdentityClient identityClient) { identityClient.deleteUser(user.getName()); } } + pagedResponse = httpJsonClient.listUsers(ListUsersRequest.newBuilder().setPageSize(5).build()); + assertThat(pagedResponse.getPage().getResponse().getUsersList().size()).isEqualTo(0); + } + + // Helper method to create a user with the DEFAULT_USER configs. Server returns + // a generated name (not username) that is used to identify the individual user + // and each test uses the name for the RPC + private User createDefaultUser() { + return httpJsonClient.createUser(CreateUserRequest.newBuilder().setUser(DEFAULT_USER).build()); } - // 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 @Test - public void testHttpJson_CRUD() { - User user = - User.newBuilder() - .setDisplayName("Jane Doe") - .setEmail("janedoe@example.com") - .setNickname("Doe") - .setHeightFeet(5) - .setAge(25) - .build(); - User createUserResponse = - httpJsonClient.createUser(CreateUserRequest.newBuilder().setUser(user).build()); + public void testHttpJson_Create() { + User userResponse = createDefaultUser(); - 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()); + // These properties should be the same + assertThat(userResponse.getDisplayName()).isEqualTo(DEFAULT_USER.getDisplayName()); + assertThat(userResponse.getEmail()).isEqualTo(DEFAULT_USER.getEmail()); + assertThat(userResponse.getNickname()).isEqualTo(DEFAULT_USER.getNickname()); + assertThat(userResponse.getHeightFeet()).isEqualTo(DEFAULT_USER.getHeightFeet()); + assertThat(userResponse.getAge()).isEqualTo(DEFAULT_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(); + assertThat(userResponse.getName()).isNotEmpty(); + assertThat(userResponse.getCreateTime()).isNotNull(); + assertThat(userResponse.getUpdateTime()).isNotNull(); + assertThat(userResponse.getEnableNotifications()).isNotNull(); + } + @Test + public void testHttpJson_Read() { + User userResponse = createDefaultUser(); // 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 + // Check that the response from both List (pagination) and Get // List Users User listUserResponse = listUsersResponse.getUsers(0); - assertThat(listUserResponse).isEqualTo(createUserResponse); + assertThat(listUserResponse).isEqualTo(userResponse); // Get User - User getUserResponse = httpJsonClient.getUser(createUserResponse.getName()); - assertThat(getUserResponse).isEqualTo(createUserResponse); + User getUserResponse = httpJsonClient.getUser(userResponse.getName()); + assertThat(getUserResponse).isEqualTo(userResponse); + } + @Test + public void testHttpJson_Update() { + User userResponse = createDefaultUser(); // Update multiple fields in the User. Age + Nickname are not included in the FieldMask User updateUser = - getUserResponse + userResponse .toBuilder() .setAge(50) .setNickname("Smith") @@ -139,20 +153,24 @@ public void testHttpJson_CRUD() { .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).isNotEqualTo(userResponse); + assertThat(updateUserResponse.getAge()).isEqualTo(userResponse.getAge()); + assertThat(updateUserResponse.getNickname()).isEqualTo(userResponse.getNickname()); assertThat(updateUserResponse.getEmail()).isEqualTo(updateUser.getEmail()); assertThat(updateUserResponse.getHeightFeet()).isEqualTo(updateUser.getHeightFeet()); assertThat(updateUserResponse.getEnableNotifications()) .isEqualTo(updateUser.getEnableNotifications()); + } + + @Test + public void testHttpJson_Delete() { + User userResponse = createDefaultUser(); - // Delete the User httpJsonClient.deleteUser( - DeleteUserRequest.newBuilder().setName(getUserResponse.getName()).build()); + DeleteUserRequest.newBuilder().setName(userResponse.getName()).build()); - listUsersPagedResponse = + IdentityClient.ListUsersPagedResponse listUsersPagedResponse = httpJsonClient.listUsers(ListUsersRequest.newBuilder().setPageSize(5).build()); assertThat(listUsersPagedResponse.getPage().getResponse().getUsersList().size()).isEqualTo(0); } From 16f834f766651b5e0d58d7085cdf4d261a31a349 Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Tue, 11 Apr 2023 10:17:23 -0400 Subject: [PATCH 10/13] chore: Address PR comments --- .../google/showcase/v1beta1/it/ITCrud.java | 63 +++++++++++-------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java index c74996e4f9..0cd7474fbd 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java @@ -73,25 +73,6 @@ 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()); - } - } - pagedResponse = httpJsonClient.listUsers(ListUsersRequest.newBuilder().setPageSize(5).build()); - assertThat(pagedResponse.getPage().getResponse().getUsersList().size()).isEqualTo(0); - } - - // Helper method to create a user with the DEFAULT_USER configs. Server returns - // a generated name (not username) that is used to identify the individual user - // and each test uses the name for the RPC - private User createDefaultUser() { - return httpJsonClient.createUser(CreateUserRequest.newBuilder().setUser(DEFAULT_USER).build()); - } - @Test public void testHttpJson_Create() { User userResponse = createDefaultUser(); @@ -112,21 +93,30 @@ public void testHttpJson_Create() { @Test public void testHttpJson_Read() { - User userResponse = createDefaultUser(); + User defaultUser = createDefaultUser(); + User otherUser = + createUser( + DEFAULT_USER + .toBuilder() + .setNickname("John Smith") + .setEmail("johnsmith@example.com") + .build()); // 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); + assertThat(listUsersResponse.getUsersList().size()).isEqualTo(2); // Check that the response from both List (pagination) and Get // List Users - User listUserResponse = listUsersResponse.getUsers(0); - assertThat(listUserResponse).isEqualTo(userResponse); + User listDefaultUserResponse = listUsersResponse.getUsers(0); + assertThat(listDefaultUserResponse).isEqualTo(defaultUser); + User listOtherUserResponse = listUsersResponse.getUsers(1); + assertThat(listOtherUserResponse).isEqualTo(otherUser); // Get User - User getUserResponse = httpJsonClient.getUser(userResponse.getName()); - assertThat(getUserResponse).isEqualTo(userResponse); + User getUserResponse = httpJsonClient.getUser(defaultUser.getName()); + assertThat(getUserResponse).isEqualTo(defaultUser); } @Test @@ -174,4 +164,27 @@ public void testHttpJson_Delete() { httpJsonClient.listUsers(ListUsersRequest.newBuilder().setPageSize(5).build()); assertThat(listUsersPagedResponse.getPage().getResponse().getUsersList().size()).isEqualTo(0); } + + 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()); + } + } + pagedResponse = httpJsonClient.listUsers(ListUsersRequest.newBuilder().setPageSize(5).build()); + assertThat(pagedResponse.getPage().getResponse().getUsersList().size()).isEqualTo(0); + } + + // Helper method to create a user with the DEFAULT_USER configs. Server returns + // a generated name (not username) that is used to identify the individual user + // and each test uses the name for the RPC + private User createDefaultUser() { + return createUser(DEFAULT_USER); + } + + private User createUser(User user) { + return httpJsonClient.createUser(CreateUserRequest.newBuilder().setUser(user).build()); + } } From a004e5cc64aff3682c2ae25a2a9dfc06034b4a92 Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Tue, 11 Apr 2023 10:24:30 -0400 Subject: [PATCH 11/13] chore: Address PR comments --- .../google/showcase/v1beta1/it/ITCrud.java | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java index 0cd7474fbd..2691b0c080 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java @@ -19,6 +19,7 @@ import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.gax.core.NoCredentialsProvider; +import com.google.common.collect.ImmutableList; import com.google.protobuf.FieldMask; import com.google.showcase.v1beta1.CreateUserRequest; import com.google.showcase.v1beta1.DeleteUserRequest; @@ -32,6 +33,7 @@ import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Arrays; +import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -93,14 +95,15 @@ public void testHttpJson_Create() { @Test public void testHttpJson_Read() { - User defaultUser = createDefaultUser(); - User otherUser = - createUser( - DEFAULT_USER - .toBuilder() - .setNickname("John Smith") - .setEmail("johnsmith@example.com") - .build()); + List expectedUsersList = + ImmutableList.of( + createDefaultUser(), + createUser( + DEFAULT_USER + .toBuilder() + .setNickname("John Smith") + .setEmail("johnsmith@example.com") + .build())); // Assert that only one User exists IdentityClient.ListUsersPagedResponse listUsersPagedResponse = httpJsonClient.listUsers(ListUsersRequest.newBuilder().setPageSize(5).build()); @@ -109,12 +112,10 @@ public void testHttpJson_Read() { // Check that the response from both List (pagination) and Get // List Users - User listDefaultUserResponse = listUsersResponse.getUsers(0); - assertThat(listDefaultUserResponse).isEqualTo(defaultUser); - User listOtherUserResponse = listUsersResponse.getUsers(1); - assertThat(listOtherUserResponse).isEqualTo(otherUser); + assertThat(listUsersResponse.getUsersList()).containsExactlyElementsIn(expectedUsersList); // Get User + User defaultUser = expectedUsersList.get(0); User getUserResponse = httpJsonClient.getUser(defaultUser.getName()); assertThat(getUserResponse).isEqualTo(defaultUser); } From e852b2eca378a137ddab84558805bff306b0354b Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Tue, 11 Apr 2023 16:22:40 -0400 Subject: [PATCH 12/13] chore: Add some documentation to methods --- .../java/com/google/showcase/v1beta1/it/ITCrud.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java index 2691b0c080..89c4f6a1d1 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java @@ -124,6 +124,7 @@ public void testHttpJson_Read() { public void testHttpJson_Update() { User userResponse = createDefaultUser(); // Update multiple fields in the User. Age + Nickname are not included in the FieldMask + // userResponse's enableNotifications field is populated from the server User updateUser = userResponse .toBuilder() @@ -178,13 +179,19 @@ private void cleanupData(IdentityClient identityClient) { assertThat(pagedResponse.getPage().getResponse().getUsersList().size()).isEqualTo(0); } - // Helper method to create a user with the DEFAULT_USER configs. Server returns - // a generated name (not username) that is used to identify the individual user - // and each test uses the name for the RPC private User createDefaultUser() { return createUser(DEFAULT_USER); } + /** + * Helper method to create a user with the DEFAULT_USER configs. Server returns a generated name + * (not username) that is used to identify the individual user and each test uses the name for the + * RPC. Server also populates a few additional fields (Create Time, Updated Time, and Enable + * Notifications). + * + * @param user User to be created + * @return newly created user + */ private User createUser(User user) { return httpJsonClient.createUser(CreateUserRequest.newBuilder().setUser(user).build()); } From 6a95069c60c4aa52aa4e7f9b567e2ed9367fdc4d Mon Sep 17 00:00:00 2001 From: Lawrence Qiu Date: Tue, 11 Apr 2023 18:04:05 -0400 Subject: [PATCH 13/13] chore: Address PR comments --- showcase/gapic-showcase/pom.xml | 2 +- .../google/showcase/v1beta1/it/ITCrud.java | 24 ++---- .../it/util/TestClientInitializer.java | 75 +++++++++++++------ 3 files changed, 58 insertions(+), 43 deletions(-) diff --git a/showcase/gapic-showcase/pom.xml b/showcase/gapic-showcase/pom.xml index 991b131cfb..76c576460f 100644 --- a/showcase/gapic-showcase/pom.xml +++ b/showcase/gapic-showcase/pom.xml @@ -107,7 +107,7 @@ fmt-maven-plugin 2.9 - (IT.*\.java)|(.*Test.java) + (IT.*\.java)|(.*Test.java)|(TestClientInitializer.java) diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java index 89c4f6a1d1..1600d860a5 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/ITCrud.java @@ -17,21 +17,16 @@ 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.common.collect.ImmutableList; 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 com.google.showcase.v1beta1.it.util.TestClientInitializer; import java.util.Arrays; import java.util.List; import org.junit.After; @@ -49,22 +44,15 @@ public class ITCrud { .setAge(25) .build(); + private IdentityClient grpcClient; private IdentityClient httpJsonClient; @Before - public void setup() throws GeneralSecurityException, IOException { + public void setup() throws Exception { + // Create gRPC IdentityClient + grpcClient = TestClientInitializer.createGrpcIdentityClient(); // 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); + httpJsonClient = TestClientInitializer.createHttpJsonIdentityClient(); // Ensure an empty state before each run cleanupData(httpJsonClient); diff --git a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/util/TestClientInitializer.java b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/util/TestClientInitializer.java index e2248ffb2e..bcd8862236 100644 --- a/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/util/TestClientInitializer.java +++ b/showcase/gapic-showcase/src/test/java/com/google/showcase/v1beta1/it/util/TestClientInitializer.java @@ -20,34 +20,61 @@ import com.google.api.gax.core.NoCredentialsProvider; import com.google.showcase.v1beta1.EchoClient; import com.google.showcase.v1beta1.EchoSettings; +import com.google.showcase.v1beta1.IdentityClient; +import com.google.showcase.v1beta1.IdentitySettings; import io.grpc.ManagedChannelBuilder; public class TestClientInitializer { - public static EchoClient createGrpcEchoClient() throws Exception { - EchoSettings grpcEchoSettings = - EchoSettings.newBuilder() - .setCredentialsProvider(NoCredentialsProvider.create()) - .setTransportChannelProvider( - EchoSettings.defaultGrpcTransportProviderBuilder() - .setChannelConfigurator(ManagedChannelBuilder::usePlaintext) - .build()) + public static EchoClient createGrpcEchoClient() throws Exception { + EchoSettings grpcEchoSettings = + EchoSettings.newBuilder() + .setCredentialsProvider(NoCredentialsProvider.create()) + .setTransportChannelProvider( + EchoSettings.defaultGrpcTransportProviderBuilder() + .setChannelConfigurator(ManagedChannelBuilder::usePlaintext) + .build()) + .build(); + return EchoClient.create(grpcEchoSettings); + } - .build(); - return EchoClient.create(grpcEchoSettings); - } + public static EchoClient createHttpJsonEchoClient() throws Exception { + EchoSettings httpJsonEchoSettings = + EchoSettings.newHttpJsonBuilder() + .setCredentialsProvider(NoCredentialsProvider.create()) + .setTransportChannelProvider( + EchoSettings.defaultHttpJsonTransportProviderBuilder() + .setHttpTransport( + new NetHttpTransport.Builder().doNotValidateCertificate().build()) + .setEndpoint("http://localhost:7469") + .build()) + .build(); + return EchoClient.create(httpJsonEchoSettings); + } - public static EchoClient createHttpJsonEchoClient() throws Exception{ - EchoSettings httpJsonEchoSettings = - EchoSettings.newHttpJsonBuilder() - .setCredentialsProvider(NoCredentialsProvider.create()) - .setTransportChannelProvider( - EchoSettings.defaultHttpJsonTransportProviderBuilder() - .setHttpTransport( - new NetHttpTransport.Builder().doNotValidateCertificate().build()) - .setEndpoint("http://localhost:7469") - .build()) - .build(); - return EchoClient.create(httpJsonEchoSettings); - } + public static IdentityClient createGrpcIdentityClient() throws Exception { + IdentitySettings grpcIdentitySettings = + IdentitySettings.newHttpJsonBuilder() + .setCredentialsProvider(NoCredentialsProvider.create()) + .setTransportChannelProvider( + IdentitySettings.defaultGrpcTransportProviderBuilder() + .setChannelConfigurator(ManagedChannelBuilder::usePlaintext) + .build()) + .build(); + return IdentityClient.create(grpcIdentitySettings); + } + + public static IdentityClient createHttpJsonIdentityClient() throws Exception { + IdentitySettings httpjsonIdentitySettings = + IdentitySettings.newHttpJsonBuilder() + .setCredentialsProvider(NoCredentialsProvider.create()) + .setTransportChannelProvider( + EchoSettings.defaultHttpJsonTransportProviderBuilder() + .setHttpTransport( + new NetHttpTransport.Builder().doNotValidateCertificate().build()) + .setEndpoint("http://localhost:7469") + .build()) + .build(); + return IdentityClient.create(httpjsonIdentitySettings); + } }