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.v1p2beta1; 18 19 import com.google.api.core.BetaApi; 20 import com.google.cloud.asset.v1p2beta1.AssetServiceGrpc.AssetServiceImplBase; 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 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 createFeed(CreateFeedRequest request, StreamObserver<Feed> responseObserver)63 public void createFeed(CreateFeedRequest request, StreamObserver<Feed> responseObserver) { 64 Object response = responses.poll(); 65 if (response instanceof Feed) { 66 requests.add(request); 67 responseObserver.onNext(((Feed) response)); 68 responseObserver.onCompleted(); 69 } else if (response instanceof Exception) { 70 responseObserver.onError(((Exception) response)); 71 } else { 72 responseObserver.onError( 73 new IllegalArgumentException( 74 String.format( 75 "Unrecognized response type %s for method CreateFeed, expected %s or %s", 76 response == null ? "null" : response.getClass().getName(), 77 Feed.class.getName(), 78 Exception.class.getName()))); 79 } 80 } 81 82 @Override getFeed(GetFeedRequest request, StreamObserver<Feed> responseObserver)83 public void getFeed(GetFeedRequest request, StreamObserver<Feed> responseObserver) { 84 Object response = responses.poll(); 85 if (response instanceof Feed) { 86 requests.add(request); 87 responseObserver.onNext(((Feed) response)); 88 responseObserver.onCompleted(); 89 } else if (response instanceof Exception) { 90 responseObserver.onError(((Exception) response)); 91 } else { 92 responseObserver.onError( 93 new IllegalArgumentException( 94 String.format( 95 "Unrecognized response type %s for method GetFeed, expected %s or %s", 96 response == null ? "null" : response.getClass().getName(), 97 Feed.class.getName(), 98 Exception.class.getName()))); 99 } 100 } 101 102 @Override listFeeds( ListFeedsRequest request, StreamObserver<ListFeedsResponse> responseObserver)103 public void listFeeds( 104 ListFeedsRequest request, StreamObserver<ListFeedsResponse> responseObserver) { 105 Object response = responses.poll(); 106 if (response instanceof ListFeedsResponse) { 107 requests.add(request); 108 responseObserver.onNext(((ListFeedsResponse) response)); 109 responseObserver.onCompleted(); 110 } else if (response instanceof Exception) { 111 responseObserver.onError(((Exception) response)); 112 } else { 113 responseObserver.onError( 114 new IllegalArgumentException( 115 String.format( 116 "Unrecognized response type %s for method ListFeeds, expected %s or %s", 117 response == null ? "null" : response.getClass().getName(), 118 ListFeedsResponse.class.getName(), 119 Exception.class.getName()))); 120 } 121 } 122 123 @Override updateFeed(UpdateFeedRequest request, StreamObserver<Feed> responseObserver)124 public void updateFeed(UpdateFeedRequest request, StreamObserver<Feed> responseObserver) { 125 Object response = responses.poll(); 126 if (response instanceof Feed) { 127 requests.add(request); 128 responseObserver.onNext(((Feed) response)); 129 responseObserver.onCompleted(); 130 } else if (response instanceof Exception) { 131 responseObserver.onError(((Exception) response)); 132 } else { 133 responseObserver.onError( 134 new IllegalArgumentException( 135 String.format( 136 "Unrecognized response type %s for method UpdateFeed, expected %s or %s", 137 response == null ? "null" : response.getClass().getName(), 138 Feed.class.getName(), 139 Exception.class.getName()))); 140 } 141 } 142 143 @Override deleteFeed(DeleteFeedRequest request, StreamObserver<Empty> responseObserver)144 public void deleteFeed(DeleteFeedRequest request, StreamObserver<Empty> responseObserver) { 145 Object response = responses.poll(); 146 if (response instanceof Empty) { 147 requests.add(request); 148 responseObserver.onNext(((Empty) response)); 149 responseObserver.onCompleted(); 150 } else if (response instanceof Exception) { 151 responseObserver.onError(((Exception) response)); 152 } else { 153 responseObserver.onError( 154 new IllegalArgumentException( 155 String.format( 156 "Unrecognized response type %s for method DeleteFeed, expected %s or %s", 157 response == null ? "null" : response.getClass().getName(), 158 Empty.class.getName(), 159 Exception.class.getName()))); 160 } 161 } 162 } 163