]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
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 | ||
20 | package tests | |
21 | ||
22 | import ( | |
23 | "context" | |
24 | "github.com/golang/mock/gomock" | |
25 | "optionalfieldstest" | |
26 | "requiredfieldtest" | |
27 | "testing" | |
28 | "thrift" | |
29 | ) | |
30 | ||
31 | func TestRequiredField_SucecssWhenSet(t *testing.T) { | |
32 | // create a new RequiredField instance with the required field set | |
33 | source := &requiredfieldtest.RequiredField{Name: "this is a test"} | |
34 | sourceData, err := thrift.NewTSerializer().Write(context.Background(), source) | |
35 | if err != nil { | |
36 | t.Fatalf("failed to serialize %T: %v", source, err) | |
37 | } | |
38 | ||
39 | d := thrift.NewTDeserializer() | |
40 | err = d.Read(&requiredfieldtest.RequiredField{}, sourceData) | |
41 | if err != nil { | |
42 | t.Fatalf("Did not expect an error when trying to deserialize the requiredfieldtest.RequiredField: %v", err) | |
43 | } | |
44 | } | |
45 | ||
46 | func TestRequiredField_ErrorWhenMissing(t *testing.T) { | |
47 | // create a new OtherThing instance, without setting the required field | |
48 | source := &requiredfieldtest.OtherThing{} | |
49 | sourceData, err := thrift.NewTSerializer().Write(context.Background(), source) | |
50 | if err != nil { | |
51 | t.Fatalf("failed to serialize %T: %v", source, err) | |
52 | } | |
53 | ||
54 | // attempt to deserialize into a different type (which should fail) | |
55 | d := thrift.NewTDeserializer() | |
56 | err = d.Read(&requiredfieldtest.RequiredField{}, sourceData) | |
57 | if err == nil { | |
58 | t.Fatal("Expected an error when trying to deserialize an object which is missing a required field") | |
59 | } | |
60 | } | |
61 | ||
62 | func TestStructReadRequiredFields(t *testing.T) { | |
63 | mockCtrl := gomock.NewController(t) | |
64 | protocol := NewMockTProtocol(mockCtrl) | |
65 | testStruct := optionalfieldstest.NewStructC() | |
66 | ||
67 | // None of required fields are set | |
68 | gomock.InOrder( | |
69 | protocol.EXPECT().ReadStructBegin().Return("StructC", nil), | |
70 | protocol.EXPECT().ReadFieldBegin().Return("_", thrift.TType(thrift.STOP), int16(1), nil), | |
71 | protocol.EXPECT().ReadStructEnd().Return(nil), | |
72 | ) | |
73 | ||
74 | err := testStruct.Read(protocol) | |
75 | mockCtrl.Finish() | |
76 | mockCtrl = gomock.NewController(t) | |
77 | if err == nil { | |
78 | t.Fatal("Expected read to fail") | |
79 | } | |
80 | err2, ok := err.(thrift.TProtocolException) | |
81 | if !ok { | |
82 | t.Fatal("Expected a TProtocolException") | |
83 | } | |
84 | if err2.TypeId() != thrift.INVALID_DATA { | |
85 | t.Fatal("Expected INVALID_DATA TProtocolException") | |
86 | } | |
87 | ||
88 | // One of the required fields is set | |
89 | gomock.InOrder( | |
90 | protocol.EXPECT().ReadStructBegin().Return("StructC", nil), | |
91 | protocol.EXPECT().ReadFieldBegin().Return("I", thrift.TType(thrift.I32), int16(2), nil), | |
92 | protocol.EXPECT().ReadI32().Return(int32(1), nil), | |
93 | protocol.EXPECT().ReadFieldEnd().Return(nil), | |
94 | protocol.EXPECT().ReadFieldBegin().Return("_", thrift.TType(thrift.STOP), int16(1), nil), | |
95 | protocol.EXPECT().ReadStructEnd().Return(nil), | |
96 | ) | |
97 | ||
98 | err = testStruct.Read(protocol) | |
99 | mockCtrl.Finish() | |
100 | mockCtrl = gomock.NewController(t) | |
101 | if err == nil { | |
102 | t.Fatal("Expected read to fail") | |
103 | } | |
104 | err2, ok = err.(thrift.TProtocolException) | |
105 | if !ok { | |
106 | t.Fatal("Expected a TProtocolException") | |
107 | } | |
108 | if err2.TypeId() != thrift.INVALID_DATA { | |
109 | t.Fatal("Expected INVALID_DATA TProtocolException") | |
110 | } | |
111 | ||
112 | // Both of the required fields are set | |
113 | gomock.InOrder( | |
114 | protocol.EXPECT().ReadStructBegin().Return("StructC", nil), | |
115 | protocol.EXPECT().ReadFieldBegin().Return("i", thrift.TType(thrift.I32), int16(2), nil), | |
116 | protocol.EXPECT().ReadI32().Return(int32(1), nil), | |
117 | protocol.EXPECT().ReadFieldEnd().Return(nil), | |
118 | protocol.EXPECT().ReadFieldBegin().Return("s2", thrift.TType(thrift.STRING), int16(4), nil), | |
119 | protocol.EXPECT().ReadString().Return("test", nil), | |
120 | protocol.EXPECT().ReadFieldEnd().Return(nil), | |
121 | protocol.EXPECT().ReadFieldBegin().Return("_", thrift.TType(thrift.STOP), int16(1), nil), | |
122 | protocol.EXPECT().ReadStructEnd().Return(nil), | |
123 | ) | |
124 | ||
125 | err = testStruct.Read(protocol) | |
126 | mockCtrl.Finish() | |
127 | if err != nil { | |
128 | t.Fatal("Expected read to succeed") | |
129 | } | |
130 | } |