/*
 * Copyright 2021 CloudWeGo Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package codec

import (
	"context"
	"testing"

	"github.com/cloudwego/kitex/internal/mocks"
	mocksremote "github.com/cloudwego/kitex/internal/mocks/remote"
	"github.com/cloudwego/kitex/internal/test"
	"github.com/cloudwego/kitex/pkg/remote"
	"github.com/cloudwego/kitex/pkg/rpcinfo"
)

func TestSetOrCheckMethodName(t *testing.T) {
	svcSearcher := mocksremote.NewDefaultSvcSearcher()
	ctx := remote.WithServiceSearcher(context.Background(), svcSearcher)

	ri := rpcinfo.NewRPCInfo(nil, rpcinfo.NewEndpointInfo("", "mock", nil, nil),
		rpcinfo.NewServerInvocation(), rpcinfo.NewRPCConfig(), rpcinfo.NewRPCStats())
	msg := remote.NewMessage(nil, ri, remote.Call, remote.Server)
	err := SetOrCheckMethodName(ctx, "mock", msg)
	test.Assert(t, err == nil)
	ri = msg.RPCInfo()
	test.Assert(t, ri.Invocation().ServiceName() == mocks.MockServiceName)
	test.Assert(t, ri.Invocation().PackageName() == "mock")
	test.Assert(t, ri.Invocation().MethodName() == "mock")
	test.Assert(t, ri.To().Method() == "mock")

	ri = rpcinfo.NewRPCInfo(nil, rpcinfo.NewEndpointInfo("", "mock", nil, nil),
		rpcinfo.NewServerInvocation(), rpcinfo.NewRPCConfig(), rpcinfo.NewRPCStats())
	ri.Invocation().(rpcinfo.InvocationSetter).SetServiceName(mocks.MockServiceName)
	msg = remote.NewMessage(nil, ri, remote.Call, remote.Server)
	err = SetOrCheckMethodName(ctx, "dummy", msg)
	test.Assert(t, err != nil)
	test.Assert(t, err.Error() == "unknown method dummy (service MockService)")

	ri = rpcinfo.NewRPCInfo(nil, rpcinfo.NewEndpointInfo("", "mock", nil, nil),
		rpcinfo.NewServerInvocation(), rpcinfo.NewRPCConfig(), rpcinfo.NewRPCStats())
	msg = remote.NewMessage(nil, ri, remote.Call, remote.Server)
	err = SetOrCheckMethodName(ctx, "dummy", msg)
	test.Assert(t, err != nil)
	test.Assert(t, err.Error() == "unknown method dummy (service )")
}
