github.com/cloudwego/kitex@v0.9.0/pkg/generic/jsonpb_test/generic_test.go (about) 1 /* 2 * Copyright 2023 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package test 18 19 import ( 20 "context" 21 "encoding/json" 22 "fmt" 23 "net" 24 "reflect" 25 "testing" 26 27 "github.com/cloudwego/dynamicgo/proto" 28 29 "github.com/cloudwego/kitex/client/genericclient" 30 "github.com/cloudwego/kitex/internal/test" 31 "github.com/cloudwego/kitex/pkg/generic" 32 "github.com/cloudwego/kitex/pkg/kerrors" 33 "github.com/cloudwego/kitex/server" 34 ) 35 36 func TestRun(t *testing.T) { 37 t.Run("TestEcho", TestEcho) 38 t.Run("TestExampleMethod", TestExampleMethod) 39 t.Run("TestVoid", TestVoid) 40 t.Run("TestExampleMethod2", TestExampleMethod2) 41 t.Run("TestInt2FloatMethod", TestInt2FloatMethod) 42 t.Run("TestInt2FloatMethod2", TestInt2FloatMethod2) 43 } 44 45 func initPbServerByIDLDynamicGo(t *testing.T, address string, handler generic.Service, pbIdl string) server.Server { 46 // initialise DescriptorProvider for DynamicGo 47 opts := proto.Options{} 48 p, err := generic.NewPbFileProviderWithDynamicGo(pbIdl, context.Background(), opts) 49 50 test.Assert(t, err == nil) 51 52 addr, _ := net.ResolveTCPAddr("tcp", address) 53 54 g, err := generic.JSONPbGeneric(p) 55 test.Assert(t, err == nil) 56 svr := newGenericServer(g, addr, handler) 57 return svr 58 } 59 60 func initPbClientByIDLDynamicGo(t *testing.T, addr, destSvcName, pbIdl string) genericclient.Client { 61 // initialise dynamicgo proto.ServiceDescriptor 62 opts := proto.Options{} 63 p, err := generic.NewPbFileProviderWithDynamicGo(pbIdl, context.Background(), opts) 64 65 test.Assert(t, err == nil) 66 67 g, err := generic.JSONPbGeneric(p) 68 test.Assert(t, err == nil) 69 cli := newGenericClient(destSvcName, g, addr) 70 71 return cli 72 } 73 74 func TestEcho(t *testing.T) { 75 addr := test.GetLocalAddress() 76 svr := initPbServerByIDLDynamicGo(t, addr, new(TestEchoService), "./idl/echo.proto") 77 cli := initPbClientByIDLDynamicGo(t, addr, "EchoService", "./idl/echo.proto") 78 79 ctx := context.Background() 80 81 // 'Echo' method name must be passed as param 82 resp, err := cli.GenericCall(ctx, "Echo", getEchoReq()) 83 // resp is a JSON string 84 test.Assert(t, err == nil) 85 respMap, ok := resp.(string) 86 test.Assert(t, ok) 87 88 fmt.Println(respMap) 89 test.Assert(t, reflect.DeepEqual(respMap, getEchoRes()), respMap) 90 91 svr.Stop() 92 } 93 94 func TestBizErr(t *testing.T) { 95 addr := test.GetLocalAddress() 96 svr := initPbServerByIDLDynamicGo(t, addr, new(TestEchoService), "./idl/echo.proto") 97 cli := initPbClientByIDLDynamicGo(t, addr, "EchoService", "./idl/echo.proto") 98 99 ctx := context.Background() 100 101 // 'Echo' method name must be passed as param 102 _, err := cli.GenericCall(ctx, "Echo", getBizErrReq()) 103 // resp is a JSON string 104 bizerr, ok := kerrors.FromBizStatusError(err) 105 test.Assert(t, ok) 106 test.Assert(t, bizerr.BizStatusCode() == 404) 107 test.Assert(t, bizerr.BizMessage() == "not found") 108 109 svr.Stop() 110 } 111 112 func TestExampleMethod(t *testing.T) { 113 addr := test.GetLocalAddress() 114 svr := initPbServerByIDLDynamicGo(t, addr, new(TestExampleMethodService), "./idl/example.proto") 115 cli := initPbClientByIDLDynamicGo(t, addr, "ExampleService", "./idl/example.proto") 116 117 ctx := context.Background() 118 119 // 'ExampleMethod' method name must be passed as param 120 resp, err := cli.GenericCall(ctx, "ExampleMethod", getExampleMethodReq()) 121 // resp is a JSON string 122 test.Assert(t, err == nil) 123 respMap, ok := resp.(string) 124 test.Assert(t, ok) 125 126 fmt.Println(respMap) 127 test.Assert(t, reflect.DeepEqual(respMap, getExampleMethodRes()), respMap) 128 129 svr.Stop() 130 } 131 132 func TestVoid(t *testing.T) { 133 addr := test.GetLocalAddress() 134 svr := initPbServerByIDLDynamicGo(t, addr, new(TestVoidService), "./idl/example.proto") 135 cli := initPbClientByIDLDynamicGo(t, addr, "ExampleService", "./idl/example.proto") 136 137 ctx := context.Background() 138 139 // 'VoidMethod' method name must be passed as param 140 resp, err := cli.GenericCall(ctx, "VoidMethod", getVoidReq()) 141 test.Assert(t, err == nil) 142 respMap, ok := resp.(string) 143 test.Assert(t, ok) 144 145 fmt.Println(respMap) 146 test.Assert(t, reflect.DeepEqual(respMap, getVoidRes()), respMap) 147 148 svr.Stop() 149 } 150 151 func TestExampleMethod2(t *testing.T) { 152 addr := test.GetLocalAddress() 153 svr := initPbServerByIDLDynamicGo(t, addr, new(TestExampleMethod2Service), "./idl/example2.proto") 154 cli := initPbClientByIDLDynamicGo(t, addr, "ExampleMethod", "./idl/example2.proto") 155 156 ctx := context.Background() 157 158 // 'ExampleMethod' method name must be passed as param 159 resp, err := cli.GenericCall(ctx, "ExampleMethod", getExampleMethod2Req()) 160 // resp is a JSON string 161 test.Assert(t, err == nil, err) 162 163 fmt.Println(resp) 164 165 var jsonMapResp map[string]interface{} 166 var jsonMapOriginal map[string]interface{} 167 err = json.Unmarshal([]byte(resp.(string)), &jsonMapResp) 168 test.Assert(t, err == nil) 169 err = json.Unmarshal([]byte(getExampleMethod2Res()), &jsonMapOriginal) 170 test.Assert(t, err == nil) 171 test.Assert(t, MapsEqual(jsonMapResp, jsonMapOriginal)) 172 173 svr.Stop() 174 } 175 176 func TestInt2FloatMethod(t *testing.T) { 177 addr := test.GetLocalAddress() 178 svr := initPbServerByIDLDynamicGo(t, addr, new(TestInt2FloatMethodService), "./idl/example2.proto") 179 cli := initPbClientByIDLDynamicGo(t, addr, "Int2FloatMethod", "./idl/example2.proto") 180 181 ctx := context.Background() 182 183 // 'Int2FloatMethod' method name must be passed as param 184 resp, err := cli.GenericCall(ctx, "Int2FloatMethod", getInt2FloatMethodReq()) 185 // resp is a JSON string 186 test.Assert(t, err == nil) 187 respMap, ok := resp.(string) 188 test.Assert(t, ok) 189 190 fmt.Println(respMap) 191 test.Assert(t, reflect.DeepEqual(respMap, getInt2FloatMethodRes()), respMap) 192 193 svr.Stop() 194 } 195 196 func TestInt2FloatMethod2(t *testing.T) { 197 addr := test.GetLocalAddress() 198 svr := initPbServerByIDLDynamicGo(t, addr, new(TestInt2FloatMethod2Service), "./idl/example2.proto") 199 cli := initPbClientByIDLDynamicGo(t, addr, "Int2FloatMethod2", "./idl/example2.proto") 200 201 ctx := context.Background() 202 203 // 'Int2FloatMethod' method name must be passed as param 204 resp, err := cli.GenericCall(ctx, "Int2FloatMethod", getInt2FloatMethod2Req()) 205 test.Assert(t, err == nil) 206 respMap, ok := resp.(string) 207 test.Assert(t, ok) 208 209 fmt.Println(respMap) 210 test.Assert(t, reflect.DeepEqual(respMap, getInt2FloatMethod2Res()), respMap) 211 212 svr.Stop() 213 } 214 215 func MapsEqual(map1, map2 map[string]interface{}) bool { 216 if len(map1) != len(map2) { 217 return false 218 } 219 220 for key, value1 := range map1 { 221 value2, ok := map2[key] 222 if !ok || !reflect.DeepEqual(value1, value2) { 223 return false 224 } 225 } 226 227 return true 228 }