dubbo.apache.org/dubbo-go/v3@v3.1.1/config_center/nacos/impl_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  // nolint
    19  package nacos
    20  
    21  import (
    22  	"reflect"
    23  	"testing"
    24  )
    25  
    26  import (
    27  	gxset "github.com/dubbogo/gost/container/set"
    28  	nacosClient "github.com/dubbogo/gost/database/kv/nacos"
    29  
    30  	"github.com/golang/mock/gomock"
    31  
    32  	"github.com/nacos-group/nacos-sdk-go/v2/model"
    33  	"github.com/nacos-group/nacos-sdk-go/v2/vo"
    34  )
    35  
    36  import (
    37  	"dubbo.apache.org/dubbo-go/v3/common"
    38  	"dubbo.apache.org/dubbo-go/v3/config_center"
    39  	"dubbo.apache.org/dubbo-go/v3/config_center/parser"
    40  )
    41  
    42  // MockIConfigClient is a mock of IConfigClient interface
    43  type MockIConfigClient struct {
    44  	ctrl     *gomock.Controller
    45  	recorder *MockIConfigClientMockRecorder
    46  }
    47  
    48  // MockIConfigClientMockRecorder is the mock recorder for MockIConfigClient
    49  type MockIConfigClientMockRecorder struct {
    50  	mock *MockIConfigClient
    51  }
    52  
    53  // NewMockIConfigClient creates a new mock instance
    54  func NewMockIConfigClient(ctrl *gomock.Controller) *MockIConfigClient {
    55  	mock := &MockIConfigClient{ctrl: ctrl}
    56  	mock.recorder = &MockIConfigClientMockRecorder{mock}
    57  	return mock
    58  }
    59  
    60  // EXPECT returns an object that allows the caller to indicate expected use
    61  func (m *MockIConfigClient) EXPECT() *MockIConfigClientMockRecorder {
    62  	return m.recorder
    63  }
    64  
    65  // GetConfig mocks base method
    66  func (m *MockIConfigClient) GetConfig(param vo.ConfigParam) (string, error) {
    67  	ret := m.ctrl.Call(m, "GetConfig", param)
    68  	ret0, _ := ret[0].(string)
    69  	ret1, _ := ret[1].(error)
    70  	return ret0, ret1
    71  }
    72  
    73  // GetConfig indicates an expected call of GetConfig
    74  func (mr *MockIConfigClientMockRecorder) GetConfig(param interface{}) *gomock.Call {
    75  	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetConfig", reflect.TypeOf((*MockIConfigClient)(nil).GetConfig), param)
    76  }
    77  
    78  // PublishConfig mocks base method
    79  func (m *MockIConfigClient) PublishConfig(param vo.ConfigParam) (bool, error) {
    80  	ret := m.ctrl.Call(m, "PublishConfig", param)
    81  	ret0, _ := ret[0].(bool)
    82  	ret1, _ := ret[1].(error)
    83  	return ret0, ret1
    84  }
    85  
    86  // PublishConfig indicates an expected call of PublishConfig
    87  func (mr *MockIConfigClientMockRecorder) PublishConfig(param interface{}) *gomock.Call {
    88  	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PublishConfig", reflect.TypeOf((*MockIConfigClient)(nil).PublishConfig), param)
    89  }
    90  
    91  // DeleteConfig mocks base method
    92  func (m *MockIConfigClient) DeleteConfig(param vo.ConfigParam) (bool, error) {
    93  	ret := m.ctrl.Call(m, "DeleteConfig", param)
    94  	ret0, _ := ret[0].(bool)
    95  	ret1, _ := ret[1].(error)
    96  	return ret0, ret1
    97  }
    98  
    99  // CloseClient client nacos client
   100  func (mr *MockIConfigClient) CloseClient() {
   101  
   102  }
   103  
   104  // DeleteConfig indicates an expected call of DeleteConfig
   105  func (mr *MockIConfigClientMockRecorder) DeleteConfig(param interface{}) *gomock.Call {
   106  	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteConfig", reflect.TypeOf((*MockIConfigClient)(nil).DeleteConfig), param)
   107  }
   108  
   109  // ListenConfig mocks base method
   110  func (m *MockIConfigClient) ListenConfig(params vo.ConfigParam) error {
   111  	ret := m.ctrl.Call(m, "ListenConfig", params)
   112  	ret0, _ := ret[0].(error)
   113  	return ret0
   114  }
   115  
   116  // ListenConfig indicates an expected call of ListenConfig
   117  func (mr *MockIConfigClientMockRecorder) ListenConfig(params interface{}) *gomock.Call {
   118  	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListenConfig", reflect.TypeOf((*MockIConfigClient)(nil).ListenConfig), params)
   119  }
   120  
   121  // CancelListenConfig mocks base method
   122  func (m *MockIConfigClient) CancelListenConfig(params vo.ConfigParam) error {
   123  	ret := m.ctrl.Call(m, "CancelListenConfig", params)
   124  	ret0, _ := ret[0].(error)
   125  	return ret0
   126  }
   127  
   128  // CancelListenConfig indicates an expected call of CancelListenConfig
   129  func (mr *MockIConfigClientMockRecorder) CancelListenConfig(params interface{}) *gomock.Call {
   130  	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CancelListenConfig", reflect.TypeOf((*MockIConfigClient)(nil).CancelListenConfig), params)
   131  }
   132  
   133  // SearchConfig mocks base method
   134  func (m *MockIConfigClient) SearchConfig(param vo.SearchConfigParam) (*model.ConfigPage, error) {
   135  	ret := m.ctrl.Call(m, "SearchConfig", param)
   136  	ret0, _ := ret[0].(*model.ConfigPage)
   137  	ret1, _ := ret[1].(error)
   138  	return ret0, ret1
   139  }
   140  
   141  // SearchConfig indicates an expected call of SearchConfig
   142  func (mr *MockIConfigClientMockRecorder) SearchConfig(param interface{}) *gomock.Call {
   143  	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SearchConfig", reflect.TypeOf((*MockIConfigClient)(nil).SearchConfig), param)
   144  }
   145  
   146  // PublishAggr mocks base method
   147  func (m *MockIConfigClient) PublishAggr(param vo.ConfigParam) (bool, error) {
   148  	ret := m.ctrl.Call(m, "PublishAggr", param)
   149  	ret0, _ := ret[0].(bool)
   150  	ret1, _ := ret[1].(error)
   151  	return ret0, ret1
   152  }
   153  
   154  // PublishAggr indicates an expected call of PublishAggr
   155  func (mr *MockIConfigClientMockRecorder) PublishAggr(param interface{}) *gomock.Call {
   156  	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PublishAggr", reflect.TypeOf((*MockIConfigClient)(nil).PublishAggr), param)
   157  }
   158  
   159  type fields struct {
   160  	BaseDynamicConfiguration config_center.BaseDynamicConfiguration
   161  	url                      *common.URL
   162  	rootPath                 string
   163  	done                     chan struct{}
   164  	client                   *nacosClient.NacosConfigClient
   165  	parser                   parser.ConfigurationParser
   166  }
   167  type args struct {
   168  	key   string
   169  	group string
   170  	value string
   171  }
   172  
   173  func newnNacosDynamicConfiguration(f *fields) *nacosDynamicConfiguration {
   174  	return &nacosDynamicConfiguration{
   175  		BaseDynamicConfiguration: f.BaseDynamicConfiguration,
   176  		url:                      f.url,
   177  		rootPath:                 f.rootPath,
   178  		done:                     f.done,
   179  		client:                   f.client,
   180  		parser:                   f.parser,
   181  	}
   182  }
   183  
   184  func Test_nacosDynamicConfiguration_PublishConfig(t *testing.T) {
   185  	ctrl := gomock.NewController(t)
   186  	mnc := NewMockIConfigClient(ctrl)
   187  	mnc.EXPECT().PublishConfig(gomock.Any()).Return(true, nil)
   188  	nc := &nacosClient.NacosConfigClient{}
   189  	nc.SetClient(mnc)
   190  
   191  	tests := []struct {
   192  		name    string
   193  		fields  fields
   194  		args    args
   195  		wantErr bool
   196  	}{
   197  		{
   198  			name: "test",
   199  			fields: fields{
   200  				client: nc,
   201  			},
   202  			args: args{
   203  				key:   "dubbo.properties",
   204  				group: "dubbogo",
   205  				value: "dubbo.protocol.name=dubbo",
   206  			},
   207  			wantErr: false,
   208  		},
   209  	}
   210  	for _, tt := range tests {
   211  		t.Run(tt.name, func(t *testing.T) {
   212  			n := newnNacosDynamicConfiguration(&tt.fields)
   213  			if err := n.PublishConfig(tt.args.key, tt.args.group, tt.args.value); (err != nil) != tt.wantErr {
   214  				t.Errorf("PublishConfig() error = %v, wantErr %v", err, tt.wantErr)
   215  			}
   216  		})
   217  	}
   218  }
   219  
   220  func Test_nacosDynamicConfiguration_GetConfigKeysByGroup(t *testing.T) {
   221  	cp := &model.ConfigPage{
   222  		PageItems: []model.ConfigItem{
   223  			{
   224  				DataId: "dubbogo",
   225  			},
   226  		},
   227  	}
   228  	result := gxset.NewSet()
   229  	result.Add("dubbogo")
   230  	ctrl := gomock.NewController(t)
   231  	mnc := NewMockIConfigClient(ctrl)
   232  	mnc.EXPECT().SearchConfig(gomock.Any()).Return(cp, nil)
   233  	nc := &nacosClient.NacosConfigClient{}
   234  	nc.SetClient(mnc)
   235  
   236  	tests := []struct {
   237  		name    string
   238  		fields  fields
   239  		args    args
   240  		want    *gxset.HashSet
   241  		wantErr bool
   242  	}{
   243  		{
   244  			name: "test",
   245  			fields: fields{
   246  				client: nc,
   247  			},
   248  			args: args{
   249  				group: "dubbo",
   250  			},
   251  			want:    result,
   252  			wantErr: false,
   253  		},
   254  	}
   255  	for _, tt := range tests {
   256  		t.Run(tt.name, func(t *testing.T) {
   257  			n := newnNacosDynamicConfiguration(&tt.fields)
   258  			got, err := n.GetConfigKeysByGroup(tt.args.group)
   259  			if (err != nil) != tt.wantErr {
   260  				t.Errorf("GetConfigKeysByGroup() error = %v, wantErr %v", err, tt.wantErr)
   261  				return
   262  			}
   263  			if !reflect.DeepEqual(got, tt.want) {
   264  				t.Errorf("GetConfigKeysByGroup() got = %v, want %v", got, tt.want)
   265  			}
   266  		})
   267  	}
   268  }