1 /* 2 * Copyright 2022 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.cloud.billing.v1; 18 19 import com.google.api.core.BetaApi; 20 import com.google.cloud.billing.v1.CloudBillingGrpc.CloudBillingImplBase; 21 import com.google.iam.v1.GetIamPolicyRequest; 22 import com.google.iam.v1.Policy; 23 import com.google.iam.v1.SetIamPolicyRequest; 24 import com.google.iam.v1.TestIamPermissionsRequest; 25 import com.google.iam.v1.TestIamPermissionsResponse; 26 import com.google.protobuf.AbstractMessage; 27 import io.grpc.stub.StreamObserver; 28 import java.util.ArrayList; 29 import java.util.LinkedList; 30 import java.util.List; 31 import java.util.Queue; 32 import javax.annotation.Generated; 33 34 @BetaApi 35 @Generated("by gapic-generator-java") 36 public class MockCloudBillingImpl extends CloudBillingImplBase { 37 private List<AbstractMessage> requests; 38 private Queue<Object> responses; 39 MockCloudBillingImpl()40 public MockCloudBillingImpl() { 41 requests = new ArrayList<>(); 42 responses = new LinkedList<>(); 43 } 44 getRequests()45 public List<AbstractMessage> getRequests() { 46 return requests; 47 } 48 addResponse(AbstractMessage response)49 public void addResponse(AbstractMessage response) { 50 responses.add(response); 51 } 52 setResponses(List<AbstractMessage> responses)53 public void setResponses(List<AbstractMessage> responses) { 54 this.responses = new LinkedList<Object>(responses); 55 } 56 addException(Exception exception)57 public void addException(Exception exception) { 58 responses.add(exception); 59 } 60 reset()61 public void reset() { 62 requests = new ArrayList<>(); 63 responses = new LinkedList<>(); 64 } 65 66 @Override getBillingAccount( GetBillingAccountRequest request, StreamObserver<BillingAccount> responseObserver)67 public void getBillingAccount( 68 GetBillingAccountRequest request, StreamObserver<BillingAccount> responseObserver) { 69 Object response = responses.poll(); 70 if (response instanceof BillingAccount) { 71 requests.add(request); 72 responseObserver.onNext(((BillingAccount) response)); 73 responseObserver.onCompleted(); 74 } else if (response instanceof Exception) { 75 responseObserver.onError(((Exception) response)); 76 } else { 77 responseObserver.onError( 78 new IllegalArgumentException( 79 String.format( 80 "Unrecognized response type %s for method GetBillingAccount, expected %s or %s", 81 response == null ? "null" : response.getClass().getName(), 82 BillingAccount.class.getName(), 83 Exception.class.getName()))); 84 } 85 } 86 87 @Override listBillingAccounts( ListBillingAccountsRequest request, StreamObserver<ListBillingAccountsResponse> responseObserver)88 public void listBillingAccounts( 89 ListBillingAccountsRequest request, 90 StreamObserver<ListBillingAccountsResponse> responseObserver) { 91 Object response = responses.poll(); 92 if (response instanceof ListBillingAccountsResponse) { 93 requests.add(request); 94 responseObserver.onNext(((ListBillingAccountsResponse) response)); 95 responseObserver.onCompleted(); 96 } else if (response instanceof Exception) { 97 responseObserver.onError(((Exception) response)); 98 } else { 99 responseObserver.onError( 100 new IllegalArgumentException( 101 String.format( 102 "Unrecognized response type %s for method ListBillingAccounts, expected %s or %s", 103 response == null ? "null" : response.getClass().getName(), 104 ListBillingAccountsResponse.class.getName(), 105 Exception.class.getName()))); 106 } 107 } 108 109 @Override updateBillingAccount( UpdateBillingAccountRequest request, StreamObserver<BillingAccount> responseObserver)110 public void updateBillingAccount( 111 UpdateBillingAccountRequest request, StreamObserver<BillingAccount> responseObserver) { 112 Object response = responses.poll(); 113 if (response instanceof BillingAccount) { 114 requests.add(request); 115 responseObserver.onNext(((BillingAccount) response)); 116 responseObserver.onCompleted(); 117 } else if (response instanceof Exception) { 118 responseObserver.onError(((Exception) response)); 119 } else { 120 responseObserver.onError( 121 new IllegalArgumentException( 122 String.format( 123 "Unrecognized response type %s for method UpdateBillingAccount, expected %s or %s", 124 response == null ? "null" : response.getClass().getName(), 125 BillingAccount.class.getName(), 126 Exception.class.getName()))); 127 } 128 } 129 130 @Override createBillingAccount( CreateBillingAccountRequest request, StreamObserver<BillingAccount> responseObserver)131 public void createBillingAccount( 132 CreateBillingAccountRequest request, StreamObserver<BillingAccount> responseObserver) { 133 Object response = responses.poll(); 134 if (response instanceof BillingAccount) { 135 requests.add(request); 136 responseObserver.onNext(((BillingAccount) response)); 137 responseObserver.onCompleted(); 138 } else if (response instanceof Exception) { 139 responseObserver.onError(((Exception) response)); 140 } else { 141 responseObserver.onError( 142 new IllegalArgumentException( 143 String.format( 144 "Unrecognized response type %s for method CreateBillingAccount, expected %s or %s", 145 response == null ? "null" : response.getClass().getName(), 146 BillingAccount.class.getName(), 147 Exception.class.getName()))); 148 } 149 } 150 151 @Override listProjectBillingInfo( ListProjectBillingInfoRequest request, StreamObserver<ListProjectBillingInfoResponse> responseObserver)152 public void listProjectBillingInfo( 153 ListProjectBillingInfoRequest request, 154 StreamObserver<ListProjectBillingInfoResponse> responseObserver) { 155 Object response = responses.poll(); 156 if (response instanceof ListProjectBillingInfoResponse) { 157 requests.add(request); 158 responseObserver.onNext(((ListProjectBillingInfoResponse) response)); 159 responseObserver.onCompleted(); 160 } else if (response instanceof Exception) { 161 responseObserver.onError(((Exception) response)); 162 } else { 163 responseObserver.onError( 164 new IllegalArgumentException( 165 String.format( 166 "Unrecognized response type %s for method ListProjectBillingInfo, expected %s or %s", 167 response == null ? "null" : response.getClass().getName(), 168 ListProjectBillingInfoResponse.class.getName(), 169 Exception.class.getName()))); 170 } 171 } 172 173 @Override getProjectBillingInfo( GetProjectBillingInfoRequest request, StreamObserver<ProjectBillingInfo> responseObserver)174 public void getProjectBillingInfo( 175 GetProjectBillingInfoRequest request, StreamObserver<ProjectBillingInfo> responseObserver) { 176 Object response = responses.poll(); 177 if (response instanceof ProjectBillingInfo) { 178 requests.add(request); 179 responseObserver.onNext(((ProjectBillingInfo) response)); 180 responseObserver.onCompleted(); 181 } else if (response instanceof Exception) { 182 responseObserver.onError(((Exception) response)); 183 } else { 184 responseObserver.onError( 185 new IllegalArgumentException( 186 String.format( 187 "Unrecognized response type %s for method GetProjectBillingInfo, expected %s or %s", 188 response == null ? "null" : response.getClass().getName(), 189 ProjectBillingInfo.class.getName(), 190 Exception.class.getName()))); 191 } 192 } 193 194 @Override updateProjectBillingInfo( UpdateProjectBillingInfoRequest request, StreamObserver<ProjectBillingInfo> responseObserver)195 public void updateProjectBillingInfo( 196 UpdateProjectBillingInfoRequest request, 197 StreamObserver<ProjectBillingInfo> responseObserver) { 198 Object response = responses.poll(); 199 if (response instanceof ProjectBillingInfo) { 200 requests.add(request); 201 responseObserver.onNext(((ProjectBillingInfo) response)); 202 responseObserver.onCompleted(); 203 } else if (response instanceof Exception) { 204 responseObserver.onError(((Exception) response)); 205 } else { 206 responseObserver.onError( 207 new IllegalArgumentException( 208 String.format( 209 "Unrecognized response type %s for method UpdateProjectBillingInfo, expected %s or %s", 210 response == null ? "null" : response.getClass().getName(), 211 ProjectBillingInfo.class.getName(), 212 Exception.class.getName()))); 213 } 214 } 215 216 @Override getIamPolicy(GetIamPolicyRequest request, StreamObserver<Policy> responseObserver)217 public void getIamPolicy(GetIamPolicyRequest request, StreamObserver<Policy> responseObserver) { 218 Object response = responses.poll(); 219 if (response instanceof Policy) { 220 requests.add(request); 221 responseObserver.onNext(((Policy) response)); 222 responseObserver.onCompleted(); 223 } else if (response instanceof Exception) { 224 responseObserver.onError(((Exception) response)); 225 } else { 226 responseObserver.onError( 227 new IllegalArgumentException( 228 String.format( 229 "Unrecognized response type %s for method GetIamPolicy, expected %s or %s", 230 response == null ? "null" : response.getClass().getName(), 231 Policy.class.getName(), 232 Exception.class.getName()))); 233 } 234 } 235 236 @Override setIamPolicy(SetIamPolicyRequest request, StreamObserver<Policy> responseObserver)237 public void setIamPolicy(SetIamPolicyRequest request, StreamObserver<Policy> responseObserver) { 238 Object response = responses.poll(); 239 if (response instanceof Policy) { 240 requests.add(request); 241 responseObserver.onNext(((Policy) response)); 242 responseObserver.onCompleted(); 243 } else if (response instanceof Exception) { 244 responseObserver.onError(((Exception) response)); 245 } else { 246 responseObserver.onError( 247 new IllegalArgumentException( 248 String.format( 249 "Unrecognized response type %s for method SetIamPolicy, expected %s or %s", 250 response == null ? "null" : response.getClass().getName(), 251 Policy.class.getName(), 252 Exception.class.getName()))); 253 } 254 } 255 256 @Override testIamPermissions( TestIamPermissionsRequest request, StreamObserver<TestIamPermissionsResponse> responseObserver)257 public void testIamPermissions( 258 TestIamPermissionsRequest request, 259 StreamObserver<TestIamPermissionsResponse> responseObserver) { 260 Object response = responses.poll(); 261 if (response instanceof TestIamPermissionsResponse) { 262 requests.add(request); 263 responseObserver.onNext(((TestIamPermissionsResponse) response)); 264 responseObserver.onCompleted(); 265 } else if (response instanceof Exception) { 266 responseObserver.onError(((Exception) response)); 267 } else { 268 responseObserver.onError( 269 new IllegalArgumentException( 270 String.format( 271 "Unrecognized response type %s for method TestIamPermissions, expected %s or %s", 272 response == null ? "null" : response.getClass().getName(), 273 TestIamPermissionsResponse.class.getName(), 274 Exception.class.getName()))); 275 } 276 } 277 } 278