• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.cx.v3;
18 
19 import com.google.api.core.BetaApi;
20 import com.google.cloud.dialogflow.cx.v3.PagesGrpc.PagesImplBase;
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 MockPagesImpl extends PagesImplBase {
33   private List<AbstractMessage> requests;
34   private Queue<Object> responses;
35 
MockPagesImpl()36   public MockPagesImpl() {
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
listPages( ListPagesRequest request, StreamObserver<ListPagesResponse> responseObserver)63   public void listPages(
64       ListPagesRequest request, StreamObserver<ListPagesResponse> responseObserver) {
65     Object response = responses.poll();
66     if (response instanceof ListPagesResponse) {
67       requests.add(request);
68       responseObserver.onNext(((ListPagesResponse) 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 ListPages, expected %s or %s",
77                   response == null ? "null" : response.getClass().getName(),
78                   ListPagesResponse.class.getName(),
79                   Exception.class.getName())));
80     }
81   }
82 
83   @Override
getPage(GetPageRequest request, StreamObserver<Page> responseObserver)84   public void getPage(GetPageRequest request, StreamObserver<Page> responseObserver) {
85     Object response = responses.poll();
86     if (response instanceof Page) {
87       requests.add(request);
88       responseObserver.onNext(((Page) 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 GetPage, expected %s or %s",
97                   response == null ? "null" : response.getClass().getName(),
98                   Page.class.getName(),
99                   Exception.class.getName())));
100     }
101   }
102 
103   @Override
createPage(CreatePageRequest request, StreamObserver<Page> responseObserver)104   public void createPage(CreatePageRequest request, StreamObserver<Page> responseObserver) {
105     Object response = responses.poll();
106     if (response instanceof Page) {
107       requests.add(request);
108       responseObserver.onNext(((Page) 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 CreatePage, expected %s or %s",
117                   response == null ? "null" : response.getClass().getName(),
118                   Page.class.getName(),
119                   Exception.class.getName())));
120     }
121   }
122 
123   @Override
updatePage(UpdatePageRequest request, StreamObserver<Page> responseObserver)124   public void updatePage(UpdatePageRequest request, StreamObserver<Page> responseObserver) {
125     Object response = responses.poll();
126     if (response instanceof Page) {
127       requests.add(request);
128       responseObserver.onNext(((Page) 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 UpdatePage, expected %s or %s",
137                   response == null ? "null" : response.getClass().getName(),
138                   Page.class.getName(),
139                   Exception.class.getName())));
140     }
141   }
142 
143   @Override
deletePage(DeletePageRequest request, StreamObserver<Empty> responseObserver)144   public void deletePage(DeletePageRequest 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 DeletePage, expected %s or %s",
157                   response == null ? "null" : response.getClass().getName(),
158                   Empty.class.getName(),
159                   Exception.class.getName())));
160     }
161   }
162 }
163