dubbo.apache.org/dubbo-go/v3@v3.1.1/remoting/nacos/builder_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 nacos
    19  
    20  /*
    21  import (
    22  	"net/url"
    23  	"reflect"
    24  	"testing"
    25  )
    26  
    27  import (
    28  	"github.com/agiledragon/gomonkey"
    29  
    30  	nacosClient "github.com/dubbogo/gost/database/kv/nacos"
    31  
    32  	nacosConstant "github.com/nacos-group/nacos-sdk-go/v2common/constant"
    33  
    34  	"github.com/stretchr/testify/assert"
    35  )
    36  
    37  import (
    38  	"dubbo.apache.org/dubbo-go/v3/common"
    39  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    40  	"dubbo.apache.org/dubbo-go/v3/config"
    41  )
    42  
    43  func getRegURL() *common.URL {
    44  	regURLMap := url.Values{}
    45  	regURLMap.Set(constant.NacosNotLoadLocalCache, "true")
    46  	regURLMap.Set(constant.NacosNamespaceID, "nacos")
    47  	regURLMap.Set(constant.TimeoutKey, "5s")
    48  	regURLMap.Set(constant.ClientNameKey, "nacos-client")
    49  	regURL, _ := common.NewURL("registry://test.nacos.io:80", common.WithParams(regURLMap))
    50  
    51  	return regURL
    52  }
    53  
    54  type args struct {
    55  	url *common.URL
    56  	rc  *config.RemoteConfig
    57  }
    58  
    59  func TestNewNacosClientByURL(t *testing.T) {
    60  	patches := gomonkey.NewPatches()
    61  	patches = patches.ApplyFunc(nacosClient.NewNacosNamingClient, func(name string, share bool, sc []nacosConstant.ServerConfig,
    62  		cc nacosConstant.ClientConfig) (*nacosClient.NacosNamingClient, error) {
    63  		return &nacosClient.NacosNamingClient{}, nil
    64  	})
    65  	defer patches.Reset()
    66  
    67  	tests := []struct {
    68  		name    string
    69  		args    args
    70  		want    *nacosClient.NacosNamingClient
    71  		wantErr bool
    72  	}{
    73  		{
    74  			name: "test",
    75  			args: args{
    76  				url: getRegURL(),
    77  			},
    78  			want:    &nacosClient.NacosNamingClient{},
    79  			wantErr: false,
    80  		},
    81  	}
    82  	for _, tt := range tests {
    83  		t.Run(tt.name, func(t *testing.T) {
    84  			got, err := NewNacosClientByURL(tt.args.url)
    85  			if (err != nil) != tt.wantErr {
    86  				t.Errorf("NewNacosClientByURL() error = %v, wantErr %v", err, tt.wantErr)
    87  				return
    88  			}
    89  			if !reflect.DeepEqual(got, tt.want) {
    90  				t.Errorf("NewNacosClientByURL() got = %v, want %v", got, tt.want)
    91  			}
    92  		})
    93  	}
    94  }
    95  
    96  func TestNewNacosClient(t *testing.T) {
    97  	patches := gomonkey.NewPatches()
    98  	patches = patches.ApplyFunc(nacosClient.NewNacosNamingClient, func(name string, share bool, sc []nacosConstant.ServerConfig,
    99  		cc nacosConstant.ClientConfig) (*nacosClient.NacosNamingClient, error) {
   100  		return &nacosClient.NacosNamingClient{}, nil
   101  	})
   102  	defer patches.Reset()
   103  
   104  	tests := []struct {
   105  		name    string
   106  		args    args
   107  		want    *nacosClient.NacosNamingClient
   108  		wantErr bool
   109  	}{
   110  		{
   111  			name: "test",
   112  			args: args{
   113  				rc: &config.RemoteConfig{
   114  					Address:  "test.nacos.io:80/nacos",
   115  					Protocol: "nacos",
   116  					Timeout:  "10s",
   117  					Username: "naocs",
   118  					Password: "nacos",
   119  				},
   120  			},
   121  			want:    &nacosClient.NacosNamingClient{},
   122  			wantErr: false,
   123  		},
   124  	}
   125  	for _, tt := range tests {
   126  		t.Run(tt.name, func(t *testing.T) {
   127  			got, err := NewNacosClient(tt.args.rc)
   128  			if (err != nil) != tt.wantErr {
   129  				t.Errorf("NewNacosClient() error = %v, wantErr %v", err, tt.wantErr)
   130  				return
   131  			}
   132  			if !reflect.DeepEqual(got, tt.want) {
   133  				t.Errorf("NewNacosClient() got = %v, want %v", got, tt.want)
   134  			}
   135  		})
   136  	}
   137  }
   138  
   139  func TestGetNacosConfig(t *testing.T) {
   140  	tests := []struct {
   141  		name    string
   142  		args    args
   143  		want    []nacosConstant.ServerConfig
   144  		want1   nacosConstant.ClientConfig
   145  		wantErr bool
   146  	}{
   147  		{
   148  			name: "test",
   149  			args: args{
   150  				url: getRegURL(),
   151  			},
   152  			want: []nacosConstant.ServerConfig{
   153  				{
   154  					IpAddr: "test.nacos.io",
   155  					Port:   80,
   156  				},
   157  			},
   158  			wantErr: false,
   159  		},
   160  	}
   161  	for _, tt := range tests {
   162  		t.Run(tt.name, func(t *testing.T) {
   163  			got, got1, err := GetNacosConfig(tt.args.url)
   164  			if (err != nil) != tt.wantErr {
   165  				t.Errorf("GetNacosConfig() error = %v, wantErr %v", err, tt.wantErr)
   166  				return
   167  			}
   168  			if !reflect.DeepEqual(got, tt.want) {
   169  				t.Errorf("GetNacosConfig() got = %v, want %v", got, tt.want)
   170  			}
   171  			assert.NotNil(t, got1)
   172  		})
   173  	}
   174  }
   175  
   176  func TestNewNacosConfigClientByUrl(t *testing.T) {
   177  	patches := gomonkey.NewPatches()
   178  	patches = patches.ApplyFunc(nacosClient.NewNacosNamingClient, func(name string, share bool, sc []nacosConstant.ServerConfig,
   179  		cc nacosConstant.ClientConfig) (*nacosClient.NacosNamingClient, error) {
   180  		return &nacosClient.NacosNamingClient{}, nil
   181  	})
   182  	defer patches.Reset()
   183  
   184  	tests := []struct {
   185  		name    string
   186  		args    args
   187  		want    *nacosClient.NacosConfigClient
   188  		wantErr bool
   189  	}{
   190  		{
   191  			name: "test",
   192  			args: args{
   193  				url: getRegURL(),
   194  			},
   195  			wantErr: false,
   196  		},
   197  	}
   198  	for _, tt := range tests {
   199  		t.Run(tt.name, func(t *testing.T) {
   200  			got, err := NewNacosConfigClientByUrl(tt.args.url)
   201  			if (err != nil) != tt.wantErr {
   202  				t.Errorf("NewNacosConfigClientByUrl() error = %v, wantErr %v", err, tt.wantErr)
   203  				return
   204  			}
   205  			assert.NotNil(t, got)
   206  		})
   207  	}
   208  }
   209  
   210  */