1 /* 2 * Copyright 2023 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.showcase.v1beta1.it.util; 18 19 import com.google.api.client.http.javanet.NetHttpTransport; 20 import com.google.api.gax.core.NoCredentialsProvider; 21 import com.google.api.gax.longrunning.OperationSnapshot; 22 import com.google.api.gax.longrunning.OperationTimedPollAlgorithm; 23 import com.google.api.gax.retrying.RetrySettings; 24 import com.google.api.gax.rpc.StatusCode; 25 import com.google.api.gax.rpc.UnaryCallSettings; 26 import com.google.showcase.v1beta1.EchoClient; 27 import com.google.showcase.v1beta1.EchoSettings; 28 import com.google.showcase.v1beta1.IdentityClient; 29 import com.google.showcase.v1beta1.IdentitySettings; 30 import com.google.showcase.v1beta1.WaitRequest; 31 import com.google.showcase.v1beta1.stub.EchoStubSettings; 32 import io.grpc.ManagedChannelBuilder; 33 import java.util.Set; 34 35 public class TestClientInitializer { 36 createGrpcEchoClient()37 public static EchoClient createGrpcEchoClient() throws Exception { 38 EchoSettings grpcEchoSettings = 39 EchoSettings.newBuilder() 40 .setCredentialsProvider(NoCredentialsProvider.create()) 41 .setTransportChannelProvider( 42 EchoSettings.defaultGrpcTransportProviderBuilder() 43 .setChannelConfigurator(ManagedChannelBuilder::usePlaintext) 44 .build()) 45 .build(); 46 return EchoClient.create(grpcEchoSettings); 47 } 48 createHttpJsonEchoClient()49 public static EchoClient createHttpJsonEchoClient() throws Exception { 50 EchoSettings httpJsonEchoSettings = 51 EchoSettings.newHttpJsonBuilder() 52 .setCredentialsProvider(NoCredentialsProvider.create()) 53 .setTransportChannelProvider( 54 EchoSettings.defaultHttpJsonTransportProviderBuilder() 55 .setHttpTransport( 56 new NetHttpTransport.Builder().doNotValidateCertificate().build()) 57 .setEndpoint("http://localhost:7469") 58 .build()) 59 .build(); 60 return EchoClient.create(httpJsonEchoSettings); 61 } 62 createGrpcEchoClientCustomBlockSettings( RetrySettings retrySettings, Set<StatusCode.Code> retryableCodes)63 public static EchoClient createGrpcEchoClientCustomBlockSettings( 64 RetrySettings retrySettings, Set<StatusCode.Code> retryableCodes) throws Exception { 65 EchoStubSettings.Builder grpcEchoSettingsBuilder = EchoStubSettings.newBuilder(); 66 grpcEchoSettingsBuilder 67 .blockSettings() 68 .setRetrySettings(retrySettings) 69 .setRetryableCodes(retryableCodes); 70 EchoSettings grpcEchoSettings = EchoSettings.create(grpcEchoSettingsBuilder.build()); 71 grpcEchoSettings = 72 grpcEchoSettings 73 .toBuilder() 74 .setCredentialsProvider(NoCredentialsProvider.create()) 75 .setTransportChannelProvider( 76 EchoSettings.defaultGrpcTransportProviderBuilder() 77 .setChannelConfigurator(ManagedChannelBuilder::usePlaintext) 78 .build()) 79 .build(); 80 return EchoClient.create(grpcEchoSettings); 81 } 82 createHttpJsonEchoClientCustomBlockSettings( RetrySettings retrySettings, Set<StatusCode.Code> retryableCodes)83 public static EchoClient createHttpJsonEchoClientCustomBlockSettings( 84 RetrySettings retrySettings, Set<StatusCode.Code> retryableCodes) throws Exception { 85 EchoStubSettings.Builder httpJsonEchoSettingsBuilder = EchoStubSettings.newHttpJsonBuilder(); 86 httpJsonEchoSettingsBuilder 87 .blockSettings() 88 .setRetrySettings(retrySettings) 89 .setRetryableCodes(retryableCodes); 90 EchoSettings httpJsonEchoSettings = EchoSettings.create(httpJsonEchoSettingsBuilder.build()); 91 httpJsonEchoSettings = 92 httpJsonEchoSettings 93 .toBuilder() 94 .setCredentialsProvider(NoCredentialsProvider.create()) 95 .setTransportChannelProvider( 96 EchoSettings.defaultHttpJsonTransportProviderBuilder() 97 .setHttpTransport( 98 new NetHttpTransport.Builder().doNotValidateCertificate().build()) 99 .setEndpoint("http://localhost:7469") 100 .build()) 101 .build(); 102 return EchoClient.create(httpJsonEchoSettings); 103 } 104 createGrpcEchoClientCustomWaitSettings( RetrySettings initialUnaryRetrySettings, RetrySettings pollingRetrySettings)105 public static EchoClient createGrpcEchoClientCustomWaitSettings( 106 RetrySettings initialUnaryRetrySettings, RetrySettings pollingRetrySettings) 107 throws Exception { 108 EchoStubSettings.Builder grpcEchoSettingsBuilder = EchoStubSettings.newBuilder(); 109 grpcEchoSettingsBuilder 110 .waitOperationSettings() 111 .setInitialCallSettings( 112 UnaryCallSettings.<WaitRequest, OperationSnapshot>newUnaryCallSettingsBuilder() 113 .setRetrySettings(initialUnaryRetrySettings) 114 .build()) 115 .setPollingAlgorithm(OperationTimedPollAlgorithm.create(pollingRetrySettings)); 116 EchoSettings grpcEchoSettings = EchoSettings.create(grpcEchoSettingsBuilder.build()); 117 grpcEchoSettings = 118 grpcEchoSettings 119 .toBuilder() 120 .setCredentialsProvider(NoCredentialsProvider.create()) 121 .setTransportChannelProvider( 122 EchoSettings.defaultGrpcTransportProviderBuilder() 123 .setChannelConfigurator(ManagedChannelBuilder::usePlaintext) 124 .build()) 125 .build(); 126 return EchoClient.create(grpcEchoSettings); 127 } 128 createHttpJsonEchoClientCustomWaitSettings( RetrySettings initialUnaryRetrySettings, RetrySettings pollingRetrySettings)129 public static EchoClient createHttpJsonEchoClientCustomWaitSettings( 130 RetrySettings initialUnaryRetrySettings, RetrySettings pollingRetrySettings) 131 throws Exception { 132 EchoStubSettings.Builder httpJsonEchoSettingsBuilder = EchoStubSettings.newHttpJsonBuilder(); 133 httpJsonEchoSettingsBuilder 134 .waitOperationSettings() 135 .setInitialCallSettings( 136 UnaryCallSettings.<WaitRequest, OperationSnapshot>newUnaryCallSettingsBuilder() 137 .setRetrySettings(initialUnaryRetrySettings) 138 .build()) 139 .setPollingAlgorithm(OperationTimedPollAlgorithm.create(pollingRetrySettings)); 140 EchoSettings httpJsonEchoSettings = EchoSettings.create(httpJsonEchoSettingsBuilder.build()); 141 httpJsonEchoSettings = 142 httpJsonEchoSettings 143 .toBuilder() 144 .setCredentialsProvider(NoCredentialsProvider.create()) 145 .setTransportChannelProvider( 146 EchoSettings.defaultHttpJsonTransportProviderBuilder() 147 .setHttpTransport( 148 new NetHttpTransport.Builder().doNotValidateCertificate().build()) 149 .setEndpoint("http://localhost:7469") 150 .build()) 151 .build(); 152 return EchoClient.create(httpJsonEchoSettings); 153 } 154 createGrpcIdentityClient()155 public static IdentityClient createGrpcIdentityClient() throws Exception { 156 IdentitySettings grpcIdentitySettings = 157 IdentitySettings.newHttpJsonBuilder() 158 .setCredentialsProvider(NoCredentialsProvider.create()) 159 .setTransportChannelProvider( 160 IdentitySettings.defaultGrpcTransportProviderBuilder() 161 .setChannelConfigurator(ManagedChannelBuilder::usePlaintext) 162 .build()) 163 .build(); 164 return IdentityClient.create(grpcIdentitySettings); 165 } 166 createHttpJsonIdentityClient()167 public static IdentityClient createHttpJsonIdentityClient() throws Exception { 168 IdentitySettings httpjsonIdentitySettings = 169 IdentitySettings.newHttpJsonBuilder() 170 .setCredentialsProvider(NoCredentialsProvider.create()) 171 .setTransportChannelProvider( 172 EchoSettings.defaultHttpJsonTransportProviderBuilder() 173 .setHttpTransport( 174 new NetHttpTransport.Builder().doNotValidateCertificate().build()) 175 .setEndpoint("http://localhost:7469") 176 .build()) 177 .build(); 178 return IdentityClient.create(httpjsonIdentitySettings); 179 } 180 } 181