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.asset.v1p7beta1; 18 19 import com.google.api.core.BetaApi; 20 import com.google.cloud.asset.v1p7beta1.AssetServiceGrpc.AssetServiceImplBase; 21 import com.google.longrunning.Operation; 22 import com.google.protobuf.AbstractMessage; 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 MockAssetServiceImpl extends AssetServiceImplBase { 33 private List<AbstractMessage> requests; 34 private Queue<Object> responses; 35 MockAssetServiceImpl()36 public MockAssetServiceImpl() { 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 exportAssets( ExportAssetsRequest request, StreamObserver<Operation> responseObserver)63 public void exportAssets( 64 ExportAssetsRequest request, StreamObserver<Operation> responseObserver) { 65 Object response = responses.poll(); 66 if (response instanceof Operation) { 67 requests.add(request); 68 responseObserver.onNext(((Operation) 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 ExportAssets, expected %s or %s", 77 response == null ? "null" : response.getClass().getName(), 78 Operation.class.getName(), 79 Exception.class.getName()))); 80 } 81 } 82 } 83