dubbo.apache.org/dubbo-go/v3@v3.1.1/filter/generic/filter_test.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package generic 19 20 import ( 21 "context" 22 "net/url" 23 "testing" 24 ) 25 26 import ( 27 hessian "github.com/apache/dubbo-go-hessian2" 28 29 "github.com/golang/mock/gomock" 30 31 "github.com/stretchr/testify/assert" 32 ) 33 34 import ( 35 "dubbo.apache.org/dubbo-go/v3/common" 36 "dubbo.apache.org/dubbo-go/v3/common/constant" 37 "dubbo.apache.org/dubbo-go/v3/protocol" 38 "dubbo.apache.org/dubbo-go/v3/protocol/invocation" 39 "dubbo.apache.org/dubbo-go/v3/protocol/mock" 40 ) 41 42 // test isCallingToGenericService branch 43 func TestFilter_Invoke(t *testing.T) { 44 invokeUrl := common.NewURLWithOptions( 45 common.WithParams(url.Values{}), 46 common.WithParamsValue(constant.GenericKey, constant.GenericSerializationDefault)) 47 filter := &genericFilter{} 48 49 ctrl := gomock.NewController(t) 50 defer ctrl.Finish() 51 52 normalInvocation := invocation.NewRPCInvocation("Hello", []interface{}{"arg1"}, make(map[string]interface{})) 53 54 mockInvoker := mock.NewMockInvoker(ctrl) 55 mockInvoker.EXPECT().GetURL().Return(invokeUrl).Times(2) 56 mockInvoker.EXPECT().Invoke(gomock.Any(), gomock.Not(normalInvocation)).DoAndReturn( 57 func(ctx context.Context, invocation protocol.Invocation) protocol.Result { 58 assert.Equal(t, constant.Generic, invocation.MethodName()) 59 args := invocation.Arguments() 60 assert.Equal(t, "Hello", args[0]) 61 assert.Equal(t, "java.lang.String", args[1].([]string)[0]) 62 assert.Equal(t, "arg1", args[2].([]hessian.Object)[0].(string)) 63 assert.Equal(t, constant.GenericSerializationDefault, invocation.GetAttachmentWithDefaultValue(constant.GenericKey, "")) 64 return &protocol.RPCResult{} 65 }) 66 67 result := filter.Invoke(context.Background(), mockInvoker, normalInvocation) 68 assert.NotNil(t, result) 69 } 70 71 // test isMakingAGenericCall branch 72 func TestFilter_InvokeWithGenericCall(t *testing.T) { 73 invokeUrl := common.NewURLWithOptions( 74 common.WithParams(url.Values{}), 75 common.WithParamsValue(constant.GenericKey, constant.GenericSerializationDefault)) 76 filter := &genericFilter{} 77 78 ctrl := gomock.NewController(t) 79 defer ctrl.Finish() 80 81 genericInvocation := invocation.NewRPCInvocation(constant.Generic, []interface{}{ 82 "hello", 83 []string{"java.lang.String"}, 84 []string{"arg1"}, 85 }, make(map[string]interface{})) 86 87 mockInvoker := mock.NewMockInvoker(ctrl) 88 mockInvoker.EXPECT().GetURL().Return(invokeUrl).Times(3) 89 mockInvoker.EXPECT().Invoke(gomock.Any(), gomock.Any()).DoAndReturn( 90 func(arg0 context.Context, invocation protocol.Invocation) protocol.Result { 91 assert.Equal(t, constant.Generic, invocation.MethodName()) 92 args := invocation.Arguments() 93 assert.Equal(t, "hello", args[0]) 94 assert.Equal(t, "java.lang.String", args[1].([]string)[0]) 95 assert.Equal(t, "arg1", args[2].([]string)[0]) 96 assert.Equal(t, constant.GenericSerializationDefault, invocation.GetAttachmentWithDefaultValue(constant.GenericKey, "")) 97 return &protocol.RPCResult{} 98 }) 99 100 result := filter.Invoke(context.Background(), mockInvoker, genericInvocation) 101 assert.NotNil(t, result) 102 }