1// Copyright 2019 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Code generated by gapic-generator. DO NOT EDIT.
16
17package expr
18
19import (
20	exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
21)
22
23import (
24	"context"
25	"flag"
26	"fmt"
27	"io"
28	"log"
29	"net"
30	"os"
31	"strings"
32	"testing"
33
34	"github.com/golang/protobuf/proto"
35	"github.com/golang/protobuf/ptypes"
36	"google.golang.org/api/option"
37	status "google.golang.org/genproto/googleapis/rpc/status"
38	"google.golang.org/grpc"
39	"google.golang.org/grpc/codes"
40	"google.golang.org/grpc/metadata"
41	gstatus "google.golang.org/grpc/status"
42)
43
44var _ = io.EOF
45var _ = ptypes.MarshalAny
46var _ status.Status
47
48type mockConformanceServer struct {
49	// Embed for forward compatibility.
50	// Tests will keep working if more methods are added
51	// in the future.
52	exprpb.ConformanceServiceServer
53
54	reqs []proto.Message
55
56	// If set, all calls return this error.
57	err error
58
59	// responses to return if err == nil
60	resps []proto.Message
61}
62
63func (s *mockConformanceServer) Parse(ctx context.Context, req *exprpb.ParseRequest) (*exprpb.ParseResponse, error) {
64	md, _ := metadata.FromIncomingContext(ctx)
65	if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
66		return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
67	}
68	s.reqs = append(s.reqs, req)
69	if s.err != nil {
70		return nil, s.err
71	}
72	return s.resps[0].(*exprpb.ParseResponse), nil
73}
74
75func (s *mockConformanceServer) Check(ctx context.Context, req *exprpb.CheckRequest) (*exprpb.CheckResponse, error) {
76	md, _ := metadata.FromIncomingContext(ctx)
77	if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
78		return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
79	}
80	s.reqs = append(s.reqs, req)
81	if s.err != nil {
82		return nil, s.err
83	}
84	return s.resps[0].(*exprpb.CheckResponse), nil
85}
86
87func (s *mockConformanceServer) Eval(ctx context.Context, req *exprpb.EvalRequest) (*exprpb.EvalResponse, error) {
88	md, _ := metadata.FromIncomingContext(ctx)
89	if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
90		return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
91	}
92	s.reqs = append(s.reqs, req)
93	if s.err != nil {
94		return nil, s.err
95	}
96	return s.resps[0].(*exprpb.EvalResponse), nil
97}
98
99type mockCelServer struct {
100	// Embed for forward compatibility.
101	// Tests will keep working if more methods are added
102	// in the future.
103	exprpb.CelServiceServer
104
105	reqs []proto.Message
106
107	// If set, all calls return this error.
108	err error
109
110	// responses to return if err == nil
111	resps []proto.Message
112}
113
114func (s *mockCelServer) Parse(ctx context.Context, req *exprpb.ParseRequest) (*exprpb.ParseResponse, error) {
115	md, _ := metadata.FromIncomingContext(ctx)
116	if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
117		return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
118	}
119	s.reqs = append(s.reqs, req)
120	if s.err != nil {
121		return nil, s.err
122	}
123	return s.resps[0].(*exprpb.ParseResponse), nil
124}
125
126func (s *mockCelServer) Check(ctx context.Context, req *exprpb.CheckRequest) (*exprpb.CheckResponse, error) {
127	md, _ := metadata.FromIncomingContext(ctx)
128	if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
129		return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
130	}
131	s.reqs = append(s.reqs, req)
132	if s.err != nil {
133		return nil, s.err
134	}
135	return s.resps[0].(*exprpb.CheckResponse), nil
136}
137
138func (s *mockCelServer) Eval(ctx context.Context, req *exprpb.EvalRequest) (*exprpb.EvalResponse, error) {
139	md, _ := metadata.FromIncomingContext(ctx)
140	if xg := md["x-goog-api-client"]; len(xg) == 0 || !strings.Contains(xg[0], "gl-go/") {
141		return nil, fmt.Errorf("x-goog-api-client = %v, expected gl-go key", xg)
142	}
143	s.reqs = append(s.reqs, req)
144	if s.err != nil {
145		return nil, s.err
146	}
147	return s.resps[0].(*exprpb.EvalResponse), nil
148}
149
150// clientOpt is the option tests should use to connect to the test server.
151// It is initialized by TestMain.
152var clientOpt option.ClientOption
153
154var (
155	mockConformance mockConformanceServer
156	mockCel         mockCelServer
157)
158
159func TestMain(m *testing.M) {
160	flag.Parse()
161
162	serv := grpc.NewServer()
163	exprpb.RegisterConformanceServiceServer(serv, &mockConformance)
164	exprpb.RegisterCelServiceServer(serv, &mockCel)
165
166	lis, err := net.Listen("tcp", "localhost:0")
167	if err != nil {
168		log.Fatal(err)
169	}
170	go serv.Serve(lis)
171
172	conn, err := grpc.Dial(lis.Addr().String(), grpc.WithInsecure())
173	if err != nil {
174		log.Fatal(err)
175	}
176	clientOpt = option.WithGRPCConn(conn)
177
178	os.Exit(m.Run())
179}
180
181func TestConformanceServiceParse(t *testing.T) {
182	var expectedResponse *exprpb.ParseResponse = &exprpb.ParseResponse{}
183
184	mockConformance.err = nil
185	mockConformance.reqs = nil
186
187	mockConformance.resps = append(mockConformance.resps[:0], expectedResponse)
188
189	var celSource string = "celSource912645552"
190	var request = &exprpb.ParseRequest{
191		CelSource: celSource,
192	}
193
194	c, err := NewConformanceClient(context.Background(), clientOpt)
195	if err != nil {
196		t.Fatal(err)
197	}
198
199	resp, err := c.Parse(context.Background(), request)
200
201	if err != nil {
202		t.Fatal(err)
203	}
204
205	if want, got := request, mockConformance.reqs[0]; !proto.Equal(want, got) {
206		t.Errorf("wrong request %q, want %q", got, want)
207	}
208
209	if want, got := expectedResponse, resp; !proto.Equal(want, got) {
210		t.Errorf("wrong response %q, want %q)", got, want)
211	}
212}
213
214func TestConformanceServiceParseError(t *testing.T) {
215	errCode := codes.PermissionDenied
216	mockConformance.err = gstatus.Error(errCode, "test error")
217
218	var celSource string = "celSource912645552"
219	var request = &exprpb.ParseRequest{
220		CelSource: celSource,
221	}
222
223	c, err := NewConformanceClient(context.Background(), clientOpt)
224	if err != nil {
225		t.Fatal(err)
226	}
227
228	resp, err := c.Parse(context.Background(), request)
229
230	if st, ok := gstatus.FromError(err); !ok {
231		t.Errorf("got error %v, expected grpc error", err)
232	} else if c := st.Code(); c != errCode {
233		t.Errorf("got error code %q, want %q", c, errCode)
234	}
235	_ = resp
236}
237func TestConformanceServiceCheck(t *testing.T) {
238	var expectedResponse *exprpb.CheckResponse = &exprpb.CheckResponse{}
239
240	mockConformance.err = nil
241	mockConformance.reqs = nil
242
243	mockConformance.resps = append(mockConformance.resps[:0], expectedResponse)
244
245	var parsedExpr *exprpb.ParsedExpr = &exprpb.ParsedExpr{}
246	var request = &exprpb.CheckRequest{
247		ParsedExpr: parsedExpr,
248	}
249
250	c, err := NewConformanceClient(context.Background(), clientOpt)
251	if err != nil {
252		t.Fatal(err)
253	}
254
255	resp, err := c.Check(context.Background(), request)
256
257	if err != nil {
258		t.Fatal(err)
259	}
260
261	if want, got := request, mockConformance.reqs[0]; !proto.Equal(want, got) {
262		t.Errorf("wrong request %q, want %q", got, want)
263	}
264
265	if want, got := expectedResponse, resp; !proto.Equal(want, got) {
266		t.Errorf("wrong response %q, want %q)", got, want)
267	}
268}
269
270func TestConformanceServiceCheckError(t *testing.T) {
271	errCode := codes.PermissionDenied
272	mockConformance.err = gstatus.Error(errCode, "test error")
273
274	var parsedExpr *exprpb.ParsedExpr = &exprpb.ParsedExpr{}
275	var request = &exprpb.CheckRequest{
276		ParsedExpr: parsedExpr,
277	}
278
279	c, err := NewConformanceClient(context.Background(), clientOpt)
280	if err != nil {
281		t.Fatal(err)
282	}
283
284	resp, err := c.Check(context.Background(), request)
285
286	if st, ok := gstatus.FromError(err); !ok {
287		t.Errorf("got error %v, expected grpc error", err)
288	} else if c := st.Code(); c != errCode {
289		t.Errorf("got error code %q, want %q", c, errCode)
290	}
291	_ = resp
292}
293func TestConformanceServiceEval(t *testing.T) {
294	var expectedResponse *exprpb.EvalResponse = &exprpb.EvalResponse{}
295
296	mockConformance.err = nil
297	mockConformance.reqs = nil
298
299	mockConformance.resps = append(mockConformance.resps[:0], expectedResponse)
300
301	var request *exprpb.EvalRequest = &exprpb.EvalRequest{}
302
303	c, err := NewConformanceClient(context.Background(), clientOpt)
304	if err != nil {
305		t.Fatal(err)
306	}
307
308	resp, err := c.Eval(context.Background(), request)
309
310	if err != nil {
311		t.Fatal(err)
312	}
313
314	if want, got := request, mockConformance.reqs[0]; !proto.Equal(want, got) {
315		t.Errorf("wrong request %q, want %q", got, want)
316	}
317
318	if want, got := expectedResponse, resp; !proto.Equal(want, got) {
319		t.Errorf("wrong response %q, want %q)", got, want)
320	}
321}
322
323func TestConformanceServiceEvalError(t *testing.T) {
324	errCode := codes.PermissionDenied
325	mockConformance.err = gstatus.Error(errCode, "test error")
326
327	var request *exprpb.EvalRequest = &exprpb.EvalRequest{}
328
329	c, err := NewConformanceClient(context.Background(), clientOpt)
330	if err != nil {
331		t.Fatal(err)
332	}
333
334	resp, err := c.Eval(context.Background(), request)
335
336	if st, ok := gstatus.FromError(err); !ok {
337		t.Errorf("got error %v, expected grpc error", err)
338	} else if c := st.Code(); c != errCode {
339		t.Errorf("got error code %q, want %q", c, errCode)
340	}
341	_ = resp
342}
343func TestCelServiceParse(t *testing.T) {
344	var expectedResponse *exprpb.ParseResponse = &exprpb.ParseResponse{}
345
346	mockCel.err = nil
347	mockCel.reqs = nil
348
349	mockCel.resps = append(mockCel.resps[:0], expectedResponse)
350
351	var celSource string = "celSource912645552"
352	var request = &exprpb.ParseRequest{
353		CelSource: celSource,
354	}
355
356	c, err := NewCelClient(context.Background(), clientOpt)
357	if err != nil {
358		t.Fatal(err)
359	}
360
361	resp, err := c.Parse(context.Background(), request)
362
363	if err != nil {
364		t.Fatal(err)
365	}
366
367	if want, got := request, mockCel.reqs[0]; !proto.Equal(want, got) {
368		t.Errorf("wrong request %q, want %q", got, want)
369	}
370
371	if want, got := expectedResponse, resp; !proto.Equal(want, got) {
372		t.Errorf("wrong response %q, want %q)", got, want)
373	}
374}
375
376func TestCelServiceParseError(t *testing.T) {
377	errCode := codes.PermissionDenied
378	mockCel.err = gstatus.Error(errCode, "test error")
379
380	var celSource string = "celSource912645552"
381	var request = &exprpb.ParseRequest{
382		CelSource: celSource,
383	}
384
385	c, err := NewCelClient(context.Background(), clientOpt)
386	if err != nil {
387		t.Fatal(err)
388	}
389
390	resp, err := c.Parse(context.Background(), request)
391
392	if st, ok := gstatus.FromError(err); !ok {
393		t.Errorf("got error %v, expected grpc error", err)
394	} else if c := st.Code(); c != errCode {
395		t.Errorf("got error code %q, want %q", c, errCode)
396	}
397	_ = resp
398}
399func TestCelServiceCheck(t *testing.T) {
400	var expectedResponse *exprpb.CheckResponse = &exprpb.CheckResponse{}
401
402	mockCel.err = nil
403	mockCel.reqs = nil
404
405	mockCel.resps = append(mockCel.resps[:0], expectedResponse)
406
407	var parsedExpr *exprpb.ParsedExpr = &exprpb.ParsedExpr{}
408	var request = &exprpb.CheckRequest{
409		ParsedExpr: parsedExpr,
410	}
411
412	c, err := NewCelClient(context.Background(), clientOpt)
413	if err != nil {
414		t.Fatal(err)
415	}
416
417	resp, err := c.Check(context.Background(), request)
418
419	if err != nil {
420		t.Fatal(err)
421	}
422
423	if want, got := request, mockCel.reqs[0]; !proto.Equal(want, got) {
424		t.Errorf("wrong request %q, want %q", got, want)
425	}
426
427	if want, got := expectedResponse, resp; !proto.Equal(want, got) {
428		t.Errorf("wrong response %q, want %q)", got, want)
429	}
430}
431
432func TestCelServiceCheckError(t *testing.T) {
433	errCode := codes.PermissionDenied
434	mockCel.err = gstatus.Error(errCode, "test error")
435
436	var parsedExpr *exprpb.ParsedExpr = &exprpb.ParsedExpr{}
437	var request = &exprpb.CheckRequest{
438		ParsedExpr: parsedExpr,
439	}
440
441	c, err := NewCelClient(context.Background(), clientOpt)
442	if err != nil {
443		t.Fatal(err)
444	}
445
446	resp, err := c.Check(context.Background(), request)
447
448	if st, ok := gstatus.FromError(err); !ok {
449		t.Errorf("got error %v, expected grpc error", err)
450	} else if c := st.Code(); c != errCode {
451		t.Errorf("got error code %q, want %q", c, errCode)
452	}
453	_ = resp
454}
455func TestCelServiceEval(t *testing.T) {
456	var expectedResponse *exprpb.EvalResponse = &exprpb.EvalResponse{}
457
458	mockCel.err = nil
459	mockCel.reqs = nil
460
461	mockCel.resps = append(mockCel.resps[:0], expectedResponse)
462
463	var request *exprpb.EvalRequest = &exprpb.EvalRequest{}
464
465	c, err := NewCelClient(context.Background(), clientOpt)
466	if err != nil {
467		t.Fatal(err)
468	}
469
470	resp, err := c.Eval(context.Background(), request)
471
472	if err != nil {
473		t.Fatal(err)
474	}
475
476	if want, got := request, mockCel.reqs[0]; !proto.Equal(want, got) {
477		t.Errorf("wrong request %q, want %q", got, want)
478	}
479
480	if want, got := expectedResponse, resp; !proto.Equal(want, got) {
481		t.Errorf("wrong response %q, want %q)", got, want)
482	}
483}
484
485func TestCelServiceEvalError(t *testing.T) {
486	errCode := codes.PermissionDenied
487	mockCel.err = gstatus.Error(errCode, "test error")
488
489	var request *exprpb.EvalRequest = &exprpb.EvalRequest{}
490
491	c, err := NewCelClient(context.Background(), clientOpt)
492	if err != nil {
493		t.Fatal(err)
494	}
495
496	resp, err := c.Eval(context.Background(), request)
497
498	if st, ok := gstatus.FromError(err); !ok {
499		t.Errorf("got error %v, expected grpc error", err)
500	} else if c := st.Code(); c != errCode {
501		t.Errorf("got error code %q, want %q", c, errCode)
502	}
503	_ = resp
504}
505