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.dialogflow.v2; 18 19 import com.google.api.core.BetaApi; 20 import com.google.cloud.dialogflow.v2.ContextsGrpc.ContextsImplBase; 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 MockContextsImpl extends ContextsImplBase { 33 private List<AbstractMessage> requests; 34 private Queue<Object> responses; 35 MockContextsImpl()36 public MockContextsImpl() { 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 listContexts( ListContextsRequest request, StreamObserver<ListContextsResponse> responseObserver)63 public void listContexts( 64 ListContextsRequest request, StreamObserver<ListContextsResponse> responseObserver) { 65 Object response = responses.poll(); 66 if (response instanceof ListContextsResponse) { 67 requests.add(request); 68 responseObserver.onNext(((ListContextsResponse) 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 ListContexts, expected %s or %s", 77 response == null ? "null" : response.getClass().getName(), 78 ListContextsResponse.class.getName(), 79 Exception.class.getName()))); 80 } 81 } 82 83 @Override getContext(GetContextRequest request, StreamObserver<Context> responseObserver)84 public void getContext(GetContextRequest request, StreamObserver<Context> responseObserver) { 85 Object response = responses.poll(); 86 if (response instanceof Context) { 87 requests.add(request); 88 responseObserver.onNext(((Context) response)); 89 responseObserver.onCompleted(); 90 } else if (response instanceof Exception) { 91 responseObserver.onError(((Exception) response)); 92 } else { 93 responseObserver.onError( 94 new IllegalArgumentException( 95 String.format( 96 "Unrecognized response type %s for method GetContext, expected %s or %s", 97 response == null ? "null" : response.getClass().getName(), 98 Context.class.getName(), 99 Exception.class.getName()))); 100 } 101 } 102 103 @Override createContext( CreateContextRequest request, StreamObserver<Context> responseObserver)104 public void createContext( 105 CreateContextRequest request, StreamObserver<Context> responseObserver) { 106 Object response = responses.poll(); 107 if (response instanceof Context) { 108 requests.add(request); 109 responseObserver.onNext(((Context) 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 CreateContext, expected %s or %s", 118 response == null ? "null" : response.getClass().getName(), 119 Context.class.getName(), 120 Exception.class.getName()))); 121 } 122 } 123 124 @Override updateContext( UpdateContextRequest request, StreamObserver<Context> responseObserver)125 public void updateContext( 126 UpdateContextRequest request, StreamObserver<Context> responseObserver) { 127 Object response = responses.poll(); 128 if (response instanceof Context) { 129 requests.add(request); 130 responseObserver.onNext(((Context) 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 UpdateContext, expected %s or %s", 139 response == null ? "null" : response.getClass().getName(), 140 Context.class.getName(), 141 Exception.class.getName()))); 142 } 143 } 144 145 @Override deleteContext(DeleteContextRequest request, StreamObserver<Empty> responseObserver)146 public void deleteContext(DeleteContextRequest request, StreamObserver<Empty> responseObserver) { 147 Object response = responses.poll(); 148 if (response instanceof Empty) { 149 requests.add(request); 150 responseObserver.onNext(((Empty) 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 DeleteContext, expected %s or %s", 159 response == null ? "null" : response.getClass().getName(), 160 Empty.class.getName(), 161 Exception.class.getName()))); 162 } 163 } 164 165 @Override deleteAllContexts( DeleteAllContextsRequest request, StreamObserver<Empty> responseObserver)166 public void deleteAllContexts( 167 DeleteAllContextsRequest request, StreamObserver<Empty> responseObserver) { 168 Object response = responses.poll(); 169 if (response instanceof Empty) { 170 requests.add(request); 171 responseObserver.onNext(((Empty) response)); 172 responseObserver.onCompleted(); 173 } else if (response instanceof Exception) { 174 responseObserver.onError(((Exception) response)); 175 } else { 176 responseObserver.onError( 177 new IllegalArgumentException( 178 String.format( 179 "Unrecognized response type %s for method DeleteAllContexts, expected %s or %s", 180 response == null ? "null" : response.getClass().getName(), 181 Empty.class.getName(), 182 Exception.class.getName()))); 183 } 184 } 185 } 186