dubbo.apache.org/dubbo-go/v3@v3.1.1/config/custom_config_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 config
    19  
    20  import (
    21  	"strings"
    22  	"testing"
    23  )
    24  
    25  import (
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  import (
    30  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    31  )
    32  
    33  func TestCustomInit(t *testing.T) {
    34  	t.Run("empty use default", func(t *testing.T) {
    35  		err := Load(WithPath("./testdata/config/custom/empty.yaml"))
    36  		assert.Nil(t, err)
    37  		assert.NotNil(t, rootConfig)
    38  		customConfig := rootConfig.Custom
    39  		assert.NotNil(t, customConfig)
    40  		assert.Equal(t, customConfig.ConfigMap, map[string]interface{}(nil))
    41  		assert.Equal(t, customConfig.GetDefineValue("test", "test"), "test")
    42  		assert.Equal(t, GetDefineValue("test", "test"), "test")
    43  	})
    44  
    45  	t.Run("use config", func(t *testing.T) {
    46  		err := Load(WithPath("./testdata/config/custom/custom.yaml"))
    47  		assert.Nil(t, err)
    48  		assert.NotNil(t, rootConfig)
    49  		customConfig := rootConfig.Custom
    50  		assert.NotNil(t, customConfig)
    51  		assert.Equal(t, customConfig.ConfigMap, map[string]interface{}{"test-config": true})
    52  		assert.Equal(t, customConfig.GetDefineValue("test-config", false), true)
    53  		assert.Equal(t, customConfig.GetDefineValue("test-no-config", false), false)
    54  		assert.Equal(t, GetDefineValue("test-config", false), true)
    55  		assert.Equal(t, GetDefineValue("test-no-config", false), false)
    56  	})
    57  
    58  	t.Run("config builder", func(t *testing.T) {
    59  		customConfigBuilder := NewCustomConfigBuilder()
    60  		customConfigBuilder.SetDefineConfig("test-build", true)
    61  		customConfig := customConfigBuilder.Build()
    62  		assert.NotNil(t, customConfig)
    63  		assert.Equal(t, customConfig.GetDefineValue("test-build", false), true)
    64  		assert.Equal(t, customConfig.GetDefineValue("test-no-build", false), false)
    65  		// todo @(laurence) now we should guarantee rootConfig ptr can't be changed during test
    66  		tempRootConfig := rootConfig
    67  		rt := NewRootConfigBuilder().SetCustom(customConfig).Build()
    68  		SetRootConfig(*rt)
    69  		assert.Equal(t, GetDefineValue("test-build", false), true)
    70  		assert.Equal(t, GetDefineValue("test-no-build", false), false)
    71  		SetRootConfig(*tempRootConfig)
    72  	})
    73  }
    74  
    75  func TestConfigUtils(t *testing.T) {
    76  	config := NewRegistryConfigWithProtocolDefaultPort("nacos")
    77  
    78  	id := clientNameID(config, config.Protocol, config.Address)
    79  
    80  	assert.Equal(t, id, strings.Join([]string{constant.RegistryConfigPrefix, "nacos", "127.0.0.1:8848"}, "-"))
    81  
    82  	ids := translateIds([]string{"nacos,zk"})
    83  	assert.Equal(t, ids[0], "nacos")
    84  	assert.Equal(t, ids[1], "zk")
    85  
    86  	element := removeDuplicateElement([]string{"nacos", "nacos"})
    87  	assert.Equal(t, len(element), 1)
    88  }