1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20package tests
21
22import (
23	"encoding/json"
24	"errors"
25	"strconv"
26	"testing"
27
28	"github.com/apache/thrift/lib/go/test/gopath/src/validatetest"
29	thrift "github.com/apache/thrift/lib/go/thrift"
30)
31
32func TestBasicValidator(t *testing.T) {
33	bt := validatetest.NewBasicTest()
34	if err := bt.Validate(); err != nil {
35		t.Error(err)
36	}
37	var ve *thrift.ValidationError
38	bt = validatetest.NewBasicTest()
39	bt.Bool1 = thrift.BoolPtr(false)
40	if err := bt.Validate(); err == nil {
41		t.Error("Expected vt.const error for Bool1")
42	} else if errors.As(err, &ve) {
43		if ve.Check() != "vt.const" {
44			t.Errorf("Expected vt.const check error, but got %v", ve.Check())
45		}
46		if ve.Field() != "Bool1" {
47			t.Errorf("Expected error for Bool1, but got %v", ve.Field())
48		}
49	} else {
50		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
51	}
52	bt = validatetest.NewBasicTest()
53	bt.Byte1 = thrift.Int8Ptr(3)
54	if err := bt.Validate(); err == nil {
55		t.Errorf("Expected vt.lt error for Byte1")
56	} else if errors.As(err, &ve) {
57		if ve.Check() != "vt.lt" {
58			t.Errorf("Expected vt.lt check error, but got %v", ve.Check())
59		}
60		if ve.Field() != "Byte1" {
61			t.Errorf("Expected error for Byte1, but got %v", ve.Field())
62		}
63	} else {
64		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
65	}
66	bt = validatetest.NewBasicTest()
67	bt.Double1 = thrift.Float64Ptr(3.0)
68	if err := bt.Validate(); err == nil {
69		t.Errorf("Expected vt.lt error for Double1")
70	} else if errors.As(err, &ve) {
71		if ve.Check() != "vt.lt" {
72			t.Errorf("Expected vt.lt check error, but got %v", ve.Check())
73		}
74		if ve.Field() != "Double1" {
75			t.Errorf("Expected error for Double1, but got %v", ve.Field())
76		}
77	} else {
78		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
79	}
80	bt = validatetest.NewBasicTest()
81	bt.String1 = thrift.StringPtr("other string")
82	if err := bt.Validate(); err == nil {
83		t.Errorf("Expected vt.const error for String1")
84	} else if errors.As(err, &ve) {
85		if ve.Check() != "vt.const" {
86			t.Errorf("Expected vt.const check error, but got %v", ve.Check())
87		}
88		if ve.Field() != "String1" {
89			t.Errorf("Expected error for String1, but got %v", ve.Field())
90		}
91	} else {
92		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
93	}
94	bt = validatetest.NewBasicTest()
95	bt.Binary1 = []byte("other binary")
96	if err := bt.Validate(); err == nil {
97		t.Errorf("Expected vt.const error for Binary1")
98	} else if errors.As(err, &ve) {
99		if ve.Check() != "vt.const" {
100			t.Errorf("Expected vt.const check error, but got %v", ve.Check())
101		}
102		if ve.Field() != "Binary1" {
103			t.Errorf("Expected error for Binary1, but got %v", ve.Field())
104		}
105	} else {
106		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
107	}
108	bt = validatetest.NewBasicTest()
109	bt.Map1 = make(map[string]string)
110	for i := 0; i < 11; i++ {
111		bt.Map1[strconv.Itoa(i)] = strconv.Itoa(i)
112	}
113	if err := bt.Validate(); err == nil {
114		t.Errorf("Expected vt.max_size error for Map1")
115	} else if errors.As(err, &ve) {
116		if ve.Check() != "vt.max_size" {
117			t.Errorf("Expected vt.max_size check error, but got %v", ve.Check())
118		}
119		if ve.Field() != "Map1" {
120			t.Errorf("Expected error for Map1, but got %v", ve.Field())
121		}
122	} else {
123		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
124	}
125	bt.Map1 = map[string]string{"012345678910": "0"}
126	if err := bt.Validate(); err == nil {
127		t.Errorf("Expected vt.max_size error for Map1")
128	} else if errors.As(err, &ve) {
129		if ve.Check() != "vt.max_size" {
130			t.Errorf("Expected vt.max_size check error, but got %v", ve.Check())
131		}
132		if ve.Field() != "Map1" {
133			t.Errorf("Expected error for Map1, but got %v", ve.Field())
134		}
135	} else {
136		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
137	}
138	bt.Map1 = map[string]string{"0": "012345678910"}
139	if err := bt.Validate(); err == nil {
140		t.Errorf("Expected vt.max_size error for Map1")
141	} else if errors.As(err, &ve) {
142		if ve.Check() != "vt.max_size" {
143			t.Errorf("Expected vt.max_size check error, but got %v", ve.Check())
144		}
145		if ve.Field() != "Map1" {
146			t.Errorf("Expected error for Map1, but got %v", ve.Field())
147		}
148	} else {
149		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
150	}
151	bt = validatetest.NewBasicTest()
152	for i := 0; i < 11; i++ {
153		bt.Set1 = append(bt.Set1, "0")
154	}
155	if err := bt.Validate(); err == nil {
156		t.Errorf("Expected vt.max_size error for Set1")
157	} else if errors.As(err, &ve) {
158		if ve.Check() != "vt.max_size" {
159			t.Errorf("Expected vt.max_size check error, but got %v", ve.Check())
160		}
161		if ve.Field() != "Set1" {
162			t.Errorf("Expected error for Set1, but got %v", ve.Field())
163		}
164	} else {
165		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
166	}
167	bt.Set1 = []string{"0"}
168	if err := bt.Validate(); err == nil {
169		t.Errorf("Expected vt.min_size error for Set1")
170	} else if errors.As(err, &ve) {
171		if ve.Check() != "vt.min_size" {
172			t.Errorf("Expected vt.min_size check error, but got %v", ve.Check())
173		}
174		if ve.Field() != "Set1" {
175			t.Errorf("Expected error for Set1, but got %v", ve.Field())
176		}
177	} else {
178		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
179	}
180	bt = validatetest.NewBasicTest()
181	bt.Enum1 = (*validatetest.EnumFoo)(thrift.Int64Ptr(int64(validatetest.EnumFoo_e2)))
182	if err := bt.Validate(); err == nil {
183		t.Errorf("Expected vt.in error for Enum1")
184	} else if errors.As(err, &ve) {
185		if ve.Check() != "vt.in" {
186			t.Errorf("Expected vt.in check error, but got %v", ve.Check())
187		}
188		if ve.Field() != "Enum1" {
189			t.Errorf("Expected error for Enum1, but got %v", ve.Field())
190		}
191	} else {
192		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
193	}
194}
195
196func TestFieldReference(t *testing.T) {
197	frt := validatetest.NewFieldReferenceTest()
198	if err := frt.Validate(); err != nil {
199		t.Error(err)
200	}
201	var ve *thrift.ValidationError
202	frt = validatetest.NewFieldReferenceTest()
203	frt.Bool2 = true
204	if err := frt.Validate(); err == nil {
205		t.Errorf("Expected vt.const error for Bool0")
206	} else if errors.As(err, &ve) {
207		if ve.Check() != "vt.const" {
208			t.Errorf("Expected vt.const check error, but got %v", ve.Check())
209		}
210		if ve.Field() != "Bool0" {
211			t.Errorf("Expected error for Bool0, but got %v", ve.Field())
212		}
213	} else {
214		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
215	}
216	frt = validatetest.NewFieldReferenceTest()
217	frt.Byte4 = 9
218	if err := frt.Validate(); err == nil {
219		t.Errorf("Expected vt.lt error for Byte0")
220	} else if errors.As(err, &ve) {
221		if ve.Check() != "vt.lt" {
222			t.Errorf("Expected vt.lt check error, but got %v", ve.Check())
223		}
224		if ve.Field() != "Byte0" {
225			t.Errorf("Expected error for Byte0, but got %v", ve.Field())
226		}
227	} else {
228		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
229	}
230	frt = validatetest.NewFieldReferenceTest()
231	frt.Double4 = 9
232	if err := frt.Validate(); err == nil {
233		t.Errorf("Expected vt.lt error for Double0")
234	} else if errors.As(err, &ve) {
235		if ve.Check() != "vt.lt" {
236			t.Errorf("Expected vt.lt check error, but got %v", ve.Check())
237		}
238		if ve.Field() != "Double0" {
239			t.Errorf("Expected error for Double0, but got %v", ve.Field())
240		}
241	} else {
242		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
243	}
244	frt = validatetest.NewFieldReferenceTest()
245	frt.String2 = "other string"
246	if err := frt.Validate(); err == nil {
247		t.Errorf("Expected vt.const error for String0")
248	} else if errors.As(err, &ve) {
249		if ve.Check() != "vt.const" {
250			t.Errorf("Expected vt.const check error, but got %v", ve.Check())
251		}
252		if ve.Field() != "String0" {
253			t.Errorf("Expected error for String0, but got %v", ve.Field())
254		}
255	} else {
256		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
257	}
258	frt = validatetest.NewFieldReferenceTest()
259	frt.Binary2 = []byte("other string")
260	if err := frt.Validate(); err == nil {
261		t.Errorf("Expected vt.const error for Binary0")
262	} else if errors.As(err, &ve) {
263		if ve.Check() != "vt.const" {
264			t.Errorf("Expected vt.const check error, but got %v", ve.Check())
265		}
266		if ve.Field() != "Binary0" {
267			t.Errorf("Expected error for Binary0, but got %v", ve.Field())
268		}
269	} else {
270		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
271	}
272	frt = validatetest.NewFieldReferenceTest()
273	frt.MaxSize = 8
274	frt.Map0 = make(map[string]string)
275	for i := 0; i < 9; i++ {
276		frt.Map0[strconv.Itoa(i)] = strconv.Itoa(i)
277	}
278	if err := frt.Validate(); err == nil {
279		t.Errorf("Expected vt.max_size error for Map0")
280	} else if errors.As(err, &ve) {
281		if ve.Check() != "vt.max_size" {
282			t.Errorf("Expected vt.max_size check error, but got %v", ve.Check())
283		}
284		if ve.Field() != "Map0" {
285			t.Errorf("Expected error for Map0, but got %v", ve.Field())
286		}
287	} else {
288		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
289	}
290	frt = validatetest.NewFieldReferenceTest()
291	frt.MaxSize = 8
292	for i := 0; i < 9; i++ {
293		frt.List0 = append(frt.List0, "0")
294	}
295	if err := frt.Validate(); err == nil {
296		t.Errorf("Expected vt.max_size error for List0")
297	} else if errors.As(err, &ve) {
298		if ve.Check() != "vt.max_size" {
299			t.Errorf("Expected vt.max_size check error, but got %v", ve.Check())
300		}
301		if ve.Field() != "List0" {
302			t.Errorf("Expected error for List0, but got %v", ve.Field())
303		}
304	} else {
305		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
306	}
307	frt = validatetest.NewFieldReferenceTest()
308	frt.MaxSize = 8
309	for i := 0; i < 9; i++ {
310		frt.Set0 = append(frt.Set0, "0")
311	}
312	if err := frt.Validate(); err == nil {
313		t.Errorf("Expected vt.max_size error for Set0")
314	} else if errors.As(err, &ve) {
315		if ve.Check() != "vt.max_size" {
316			t.Errorf("Expected vt.max_size check error, but got %v", ve.Check())
317		}
318		if ve.Field() != "Set0" {
319			t.Errorf("Expected error for Set0, but got %v", ve.Field())
320		}
321	} else {
322		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
323	}
324}
325
326func TestValidationFunction(t *testing.T) {
327	vft := validatetest.NewValidationFunctionTest()
328	if err := vft.Validate(); err != nil {
329		t.Error(err)
330	}
331	var ve *thrift.ValidationError
332	vft = validatetest.NewValidationFunctionTest()
333	vft.StringFoo = "some string"
334	if err := vft.Validate(); err == nil {
335		t.Errorf("Expected vt.in error for StringLength")
336	} else if errors.As(err, &ve) {
337		if ve.Check() != "vt.in" {
338			t.Errorf("Expected vt.in check error, but got %v", ve.Check())
339		}
340		if ve.Field() != "StringLength" {
341			t.Errorf("Expected error for StringLength, but got %v", ve.Field())
342		}
343	} else {
344		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
345	}
346}
347
348func TestAnnotationCompatibleTest(t *testing.T) {
349	act := validatetest.NewAnnotationCompatibleTest()
350	if err := act.Validate(); err != nil {
351		t.Error(err)
352	}
353	var ve *thrift.ValidationError
354	act = validatetest.NewAnnotationCompatibleTest()
355	act.Bool0 = false
356	if err := act.Validate(); err == nil {
357		t.Errorf("Expected vt.const error for Bool0")
358	} else if errors.As(err, &ve) {
359		if ve.Check() != "vt.const" {
360			t.Errorf("Expected vt.const check error, but got %v", ve.Check())
361		}
362		if ve.Field() != "Bool0" {
363			t.Errorf("Expected error for Bool0, but got %v", ve.Field())
364		}
365	} else {
366		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
367	}
368	act = validatetest.NewAnnotationCompatibleTest()
369	act.Byte0 = 3
370	if err := act.Validate(); err == nil {
371		t.Errorf("Expected vt.lt error for Byte0")
372	} else if errors.As(err, &ve) {
373		if ve.Check() != "vt.lt" {
374			t.Errorf("Expected vt.lt check error, but got %v", ve.Check())
375		}
376		if ve.Field() != "Byte0" {
377			t.Errorf("Expected error for Byte0, but got %v", ve.Field())
378		}
379	} else {
380		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
381	}
382	act = validatetest.NewAnnotationCompatibleTest()
383	act.Double0 = 3
384	if err := act.Validate(); err == nil {
385		t.Errorf("Expected vt.lt error for Double0")
386	} else if errors.As(err, &ve) {
387		if ve.Check() != "vt.lt" {
388			t.Errorf("Expected vt.lt check error, but got %v", ve.Check())
389		}
390		if ve.Field() != "Double0" {
391			t.Errorf("Expected error for Double0, but got %v", ve.Field())
392		}
393	} else {
394		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
395	}
396	act = validatetest.NewAnnotationCompatibleTest()
397	act.String0 = "other string"
398	if err := act.Validate(); err == nil {
399		t.Errorf("Expected vt.const error for String0")
400	} else if errors.As(err, &ve) {
401		if ve.Check() != "vt.const" {
402			t.Errorf("Expected vt.const check error, but got %v", ve.Check())
403		}
404		if ve.Field() != "String0" {
405			t.Errorf("Expected error for String0, but got %v", ve.Field())
406		}
407	} else {
408		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
409	}
410	act = validatetest.NewAnnotationCompatibleTest()
411	act.Binary0 = []byte("other string")
412	if err := act.Validate(); err == nil {
413		t.Errorf("Expected vt.const error for Binary0")
414	} else if errors.As(err, &ve) {
415		if ve.Check() != "vt.const" {
416			t.Errorf("Expected vt.const check error, but got %v", ve.Check())
417		}
418		if ve.Field() != "Binary0" {
419			t.Errorf("Expected error for Binary0, but got %v", ve.Field())
420		}
421	} else {
422		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
423	}
424	act = validatetest.NewAnnotationCompatibleTest()
425	act.Map0 = map[string]string{"0": "0", "1": "1", "2": "2"}
426	if err := act.Validate(); err == nil {
427		t.Errorf("Expected vt.max_size error for Map0")
428	} else if errors.As(err, &ve) {
429		if ve.Check() != "vt.max_size" {
430			t.Errorf("Expected vt.max_size check error, but got %v", ve.Check())
431		}
432		if ve.Field() != "Map0" {
433			t.Errorf("Expected error for Map0, but got %v", ve.Field())
434		}
435	} else {
436		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
437	}
438	act = validatetest.NewAnnotationCompatibleTest()
439	act.Set0 = []string{"0", "1", "2"}
440	if err := act.Validate(); err == nil {
441		t.Errorf("Expected vt.max_size error for Set0")
442	} else if errors.As(err, &ve) {
443		if ve.Check() != "vt.max_size" {
444			t.Errorf("Expected vt.max_size check error, but got %v", ve.Check())
445		}
446		if ve.Field() != "Set0" {
447			t.Errorf("Expected error for Set0, but got %v", ve.Field())
448		}
449	} else {
450		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
451	}
452	act = validatetest.NewAnnotationCompatibleTest()
453	act.List0 = []string{"0", "1", "2"}
454	if err := act.Validate(); err == nil {
455		t.Errorf("Expected vt.max_size error for List0")
456	} else if errors.As(err, &ve) {
457		if ve.Check() != "vt.max_size" {
458			t.Errorf("Expected vt.max_size check error, but got %v", ve.Check())
459		}
460		if ve.Field() != "List0" {
461			t.Errorf("Expected error for List0, but got %v", ve.Field())
462		}
463	} else {
464		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
465	}
466	act = validatetest.NewAnnotationCompatibleTest()
467	act.Enum0 = validatetest.EnumFoo_e1
468	if err := act.Validate(); err == nil {
469		t.Errorf("Expected vt.in error for Enum0")
470	} else if errors.As(err, &ve) {
471		if ve.Check() != "vt.in" {
472			t.Errorf("Expected vt.in check error, but got %v", ve.Check())
473		}
474		if ve.Field() != "Enum0" {
475			t.Errorf("Expected error for Enum0, but got %v", ve.Field())
476		}
477	} else {
478		t.Errorf("Error cannot be unwrapped into *ValidationError: %v", err)
479	}
480	fields := []string{"bool1", "byte1", "double1", "string1", "binary1", "enum1", "struct1", "list1", "set1", "map1"}
481	b, err := json.Marshal(act)
482	if err != nil {
483		t.Error(err)
484	}
485	jsonMap := make(map[string]interface{})
486	if err = json.Unmarshal(b, &jsonMap); err != nil {
487		t.Error(err)
488	}
489	for _, field := range fields {
490		if _, ok := jsonMap[field]; !ok {
491			t.Errorf("Expected field %s in JSON, but not found", field)
492		}
493	}
494}
495