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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
24 "github.com/golang/mock/gomock"
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)
36 t.Fatalf("failed to serialize %T: %v", source, err)
39 d := thrift.NewTDeserializer()
40 err = d.Read(&requiredfieldtest.RequiredField{}, sourceData)
42 t.Fatalf("Did not expect an error when trying to deserialize the requiredfieldtest.RequiredField: %v", err)
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)
51 t.Fatalf("failed to serialize %T: %v", source, err)
54 // attempt to deserialize into a different type (which should fail)
55 d := thrift.NewTDeserializer()
56 err = d.Read(&requiredfieldtest.RequiredField{}, sourceData)
58 t.Fatal("Expected an error when trying to deserialize an object which is missing a required field")
62 func TestStructReadRequiredFields(t *testing.T) {
63 mockCtrl := gomock.NewController(t)
64 protocol := NewMockTProtocol(mockCtrl)
65 testStruct := optionalfieldstest.NewStructC()
67 // None of required fields are set
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),
74 err := testStruct.Read(protocol)
76 mockCtrl = gomock.NewController(t)
78 t.Fatal("Expected read to fail")
80 err2, ok := err.(thrift.TProtocolException)
82 t.Fatal("Expected a TProtocolException")
84 if err2.TypeId() != thrift.INVALID_DATA {
85 t.Fatal("Expected INVALID_DATA TProtocolException")
88 // One of the required fields is set
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),
98 err = testStruct.Read(protocol)
100 mockCtrl = gomock.NewController(t)
102 t.Fatal("Expected read to fail")
104 err2, ok = err.(thrift.TProtocolException)
106 t.Fatal("Expected a TProtocolException")
108 if err2.TypeId() != thrift.INVALID_DATA {
109 t.Fatal("Expected INVALID_DATA TProtocolException")
112 // Both of the required fields are set
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),
125 err = testStruct.Read(protocol)
128 t.Fatal("Expected read to succeed")