/*
 * 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 rpcinfo_test

import (
	"testing"

	"github.com/cloudwego/kitex/internal/test"
	"github.com/cloudwego/kitex/pkg/rpcinfo"
	"github.com/cloudwego/kitex/pkg/utils"
)

func TestEndpointInfo(t *testing.T) {
	na := utils.NewNetAddr("nnn", "aaa")
	ts := map[string]string{"a": "b", "1": "2"}
	ei := rpcinfo.NewEndpointInfo("s", "m", na, ts)

	test.Assert(t, ei.ServiceName() == "s")
	test.Assert(t, ei.Method() == "m")
	test.Assert(t, ei.Address().Network() == "nnn")
	test.Assert(t, ei.Address().String() == "aaa")
	test.Assert(t, ei.DefaultTag("a", "-") == "b")
	test.Assert(t, ei.DefaultTag("1", "-") == "2")
	test.Assert(t, ei.DefaultTag("x", "-") == "-")

	tag, ok := ei.Tag("a")
	test.Assert(t, ok && tag == "b")
	tag, ok = ei.Tag("1")
	test.Assert(t, ok && tag == "2")
	tag, ok = ei.Tag("x")
	test.Assert(t, !ok && tag == "")
}

func TestFromBasicInfo(t *testing.T) {
	ebi := &rpcinfo.EndpointBasicInfo{
		ServiceName: "svc",
		Method:      "mtd",
		Tags: map[string]string{
			"a": "b", "1": "2",
		},
	}

	ei := rpcinfo.FromBasicInfo(ebi)
	test.Assert(t, ei != nil)
	test.Assert(t, ei.ServiceName() == "svc")
	test.Assert(t, ei.Method() == "mtd")
	test.Assert(t, ei.Address() == nil)
	test.Assert(t, ei.DefaultTag("a", "-") == "b")
	test.Assert(t, ei.DefaultTag("1", "-") == "2")
	test.Assert(t, ei.DefaultTag("x", "-") == "-")
}

func TestEmptyEndpointInfo(t *testing.T) {
	ei := rpcinfo.EmptyEndpointInfo()
	test.Assert(t, ei != nil)
	test.Assert(t, ei.ServiceName() == "")
	test.Assert(t, ei.Method() == "")
	test.Assert(t, ei.Address() == nil)
	test.Assert(t, ei.DefaultTag("!", "?") == "?")

	ei2 := rpcinfo.EmptyEndpointInfo()
	test.Assert(t, ei != ei2)
}
