Skip to content

Commit

Permalink
chore: Add HttpJson CRUD Showcase tests (#1589)
Browse files Browse the repository at this point in the history
* chore: Add CRUD showcase test

* chore: Add test name for parameterized tests

* chore: Remove parameterized tests

* chore: Clean up CRUD test

* chore: Update comment

* chore: Add additional test case for CRUD

* chore: Update tests

* chore: Clean up tests

* chore: Updated to split each RPC to its own test

* chore: Address PR comments

* chore: Address PR comments

* chore: Add some documentation to methods

* chore: Address PR comments
  • Loading branch information
lqiu96 authored Apr 12, 2023
1 parent 01cb4aa commit 59efbbb
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 25 deletions.
2 changes: 1 addition & 1 deletion showcase/gapic-showcase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
<artifactId>fmt-maven-plugin</artifactId>
<version>2.9</version>
<configuration>
<filesNamePattern>(IT.*\.java)|(.*Test.java)</filesNamePattern>
<filesNamePattern>(IT.*\.java)|(.*Test.java)|(TestClientInitializer.java)</filesNamePattern>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/*
* 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.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.IdentityClient;
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 com.google.showcase.v1beta1.it.util.TestClientInitializer;
import java.util.Arrays;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ITCrud {

private static final User DEFAULT_USER =
User.newBuilder()
.setDisplayName("Jane Doe")
.setEmail("[email protected]")
.setNickname("Doe")
.setHeightFeet(5)
.setAge(25)
.build();

private IdentityClient grpcClient;
private IdentityClient httpJsonClient;

@Before
public void setup() throws Exception {
// Create gRPC IdentityClient
grpcClient = TestClientInitializer.createGrpcIdentityClient();
// Create HttpJson IdentityClient
httpJsonClient = TestClientInitializer.createHttpJsonIdentityClient();

// Ensure an empty state before each run
cleanupData(httpJsonClient);
}

@After
public void cleanup() {
httpJsonClient.close();
}

@Test
public void testHttpJson_Create() {
User userResponse = createDefaultUser();

// 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(userResponse.getName()).isNotEmpty();
assertThat(userResponse.getCreateTime()).isNotNull();
assertThat(userResponse.getUpdateTime()).isNotNull();
assertThat(userResponse.getEnableNotifications()).isNotNull();
}

@Test
public void testHttpJson_Read() {
List<User> expectedUsersList =
ImmutableList.of(
createDefaultUser(),
createUser(
DEFAULT_USER
.toBuilder()
.setNickname("John Smith")
.setEmail("[email protected]")
.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(2);

// Check that the response from both List (pagination) and Get
// List Users
assertThat(listUsersResponse.getUsersList()).containsExactlyElementsIn(expectedUsersList);

// Get User
User defaultUser = expectedUsersList.get(0);
User getUserResponse = httpJsonClient.getUser(defaultUser.getName());
assertThat(getUserResponse).isEqualTo(defaultUser);
}

@Test
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()
.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(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();

httpJsonClient.deleteUser(
DeleteUserRequest.newBuilder().setName(userResponse.getName()).build());

IdentityClient.ListUsersPagedResponse listUsersPagedResponse =
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);
}

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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 59efbbb

Please sign in to comment.