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.orgpolicy.v2; 18 19 import com.google.api.core.BetaApi; 20 import com.google.cloud.orgpolicy.v2.OrgPolicyGrpc.OrgPolicyImplBase; 21 import com.google.protobuf.AbstractMessage; 22 import com.google.protobuf.Empty; 23 import io.grpc.stub.StreamObserver; 24 import java.util.ArrayList; 25 import java.util.LinkedList; 26 import java.util.List; 27 import java.util.Queue; 28 import javax.annotation.Generated; 29 30 @BetaApi 31 @Generated("by gapic-generator-java") 32 public class MockOrgPolicyImpl extends OrgPolicyImplBase { 33 private List<AbstractMessage> requests; 34 private Queue<Object> responses; 35 MockOrgPolicyImpl()36 public MockOrgPolicyImpl() { 37 requests = new ArrayList<>(); 38 responses = new LinkedList<>(); 39 } 40 getRequests()41 public List<AbstractMessage> getRequests() { 42 return requests; 43 } 44 addResponse(AbstractMessage response)45 public void addResponse(AbstractMessage response) { 46 responses.add(response); 47 } 48 setResponses(List<AbstractMessage> responses)49 public void setResponses(List<AbstractMessage> responses) { 50 this.responses = new LinkedList<Object>(responses); 51 } 52 addException(Exception exception)53 public void addException(Exception exception) { 54 responses.add(exception); 55 } 56 reset()57 public void reset() { 58 requests = new ArrayList<>(); 59 responses = new LinkedList<>(); 60 } 61 62 @Override listConstraints( ListConstraintsRequest request, StreamObserver<ListConstraintsResponse> responseObserver)63 public void listConstraints( 64 ListConstraintsRequest request, StreamObserver<ListConstraintsResponse> responseObserver) { 65 Object response = responses.poll(); 66 if (response instanceof ListConstraintsResponse) { 67 requests.add(request); 68 responseObserver.onNext(((ListConstraintsResponse) response)); 69 responseObserver.onCompleted(); 70 } else if (response instanceof Exception) { 71 responseObserver.onError(((Exception) response)); 72 } else { 73 responseObserver.onError( 74 new IllegalArgumentException( 75 String.format( 76 "Unrecognized response type %s for method ListConstraints, expected %s or %s", 77 response == null ? "null" : response.getClass().getName(), 78 ListConstraintsResponse.class.getName(), 79 Exception.class.getName()))); 80 } 81 } 82 83 @Override listPolicies( ListPoliciesRequest request, StreamObserver<ListPoliciesResponse> responseObserver)84 public void listPolicies( 85 ListPoliciesRequest request, StreamObserver<ListPoliciesResponse> responseObserver) { 86 Object response = responses.poll(); 87 if (response instanceof ListPoliciesResponse) { 88 requests.add(request); 89 responseObserver.onNext(((ListPoliciesResponse) response)); 90 responseObserver.onCompleted(); 91 } else if (response instanceof Exception) { 92 responseObserver.onError(((Exception) response)); 93 } else { 94 responseObserver.onError( 95 new IllegalArgumentException( 96 String.format( 97 "Unrecognized response type %s for method ListPolicies, expected %s or %s", 98 response == null ? "null" : response.getClass().getName(), 99 ListPoliciesResponse.class.getName(), 100 Exception.class.getName()))); 101 } 102 } 103 104 @Override getPolicy(GetPolicyRequest request, StreamObserver<Policy> responseObserver)105 public void getPolicy(GetPolicyRequest request, StreamObserver<Policy> responseObserver) { 106 Object response = responses.poll(); 107 if (response instanceof Policy) { 108 requests.add(request); 109 responseObserver.onNext(((Policy) response)); 110 responseObserver.onCompleted(); 111 } else if (response instanceof Exception) { 112 responseObserver.onError(((Exception) response)); 113 } else { 114 responseObserver.onError( 115 new IllegalArgumentException( 116 String.format( 117 "Unrecognized response type %s for method GetPolicy, expected %s or %s", 118 response == null ? "null" : response.getClass().getName(), 119 Policy.class.getName(), 120 Exception.class.getName()))); 121 } 122 } 123 124 @Override getEffectivePolicy( GetEffectivePolicyRequest request, StreamObserver<Policy> responseObserver)125 public void getEffectivePolicy( 126 GetEffectivePolicyRequest request, StreamObserver<Policy> responseObserver) { 127 Object response = responses.poll(); 128 if (response instanceof Policy) { 129 requests.add(request); 130 responseObserver.onNext(((Policy) response)); 131 responseObserver.onCompleted(); 132 } else if (response instanceof Exception) { 133 responseObserver.onError(((Exception) response)); 134 } else { 135 responseObserver.onError( 136 new IllegalArgumentException( 137 String.format( 138 "Unrecognized response type %s for method GetEffectivePolicy, expected %s or %s", 139 response == null ? "null" : response.getClass().getName(), 140 Policy.class.getName(), 141 Exception.class.getName()))); 142 } 143 } 144 145 @Override createPolicy(CreatePolicyRequest request, StreamObserver<Policy> responseObserver)146 public void createPolicy(CreatePolicyRequest request, StreamObserver<Policy> responseObserver) { 147 Object response = responses.poll(); 148 if (response instanceof Policy) { 149 requests.add(request); 150 responseObserver.onNext(((Policy) response)); 151 responseObserver.onCompleted(); 152 } else if (response instanceof Exception) { 153 responseObserver.onError(((Exception) response)); 154 } else { 155 responseObserver.onError( 156 new IllegalArgumentException( 157 String.format( 158 "Unrecognized response type %s for method CreatePolicy, expected %s or %s", 159 response == null ? "null" : response.getClass().getName(), 160 Policy.class.getName(), 161 Exception.class.getName()))); 162 } 163 } 164 165 @Override updatePolicy(UpdatePolicyRequest request, StreamObserver<Policy> responseObserver)166 public void updatePolicy(UpdatePolicyRequest request, StreamObserver<Policy> responseObserver) { 167 Object response = responses.poll(); 168 if (response instanceof Policy) { 169 requests.add(request); 170 responseObserver.onNext(((Policy) response)); 171 responseObserver.onCompleted(); 172 } else if (response instanceof Exception) { 173 responseObserver.onError(((Exception) response)); 174 } else { 175 responseObserver.onError( 176 new IllegalArgumentException( 177 String.format( 178 "Unrecognized response type %s for method UpdatePolicy, expected %s or %s", 179 response == null ? "null" : response.getClass().getName(), 180 Policy.class.getName(), 181 Exception.class.getName()))); 182 } 183 } 184 185 @Override deletePolicy(DeletePolicyRequest request, StreamObserver<Empty> responseObserver)186 public void deletePolicy(DeletePolicyRequest request, StreamObserver<Empty> responseObserver) { 187 Object response = responses.poll(); 188 if (response instanceof Empty) { 189 requests.add(request); 190 responseObserver.onNext(((Empty) response)); 191 responseObserver.onCompleted(); 192 } else if (response instanceof Exception) { 193 responseObserver.onError(((Exception) response)); 194 } else { 195 responseObserver.onError( 196 new IllegalArgumentException( 197 String.format( 198 "Unrecognized response type %s for method DeletePolicy, expected %s or %s", 199 response == null ? "null" : response.getClass().getName(), 200 Empty.class.getName(), 201 Exception.class.getName()))); 202 } 203 } 204 } 205