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