AlexStocks commented on code in PR #1873:
URL: https://github.com/apache/dubbo-go/pull/1873#discussion_r862336810


##########
config_center/nacos/impl_test.go:
##########
@@ -15,85 +15,277 @@
  * limitations under the License.
  */
 
+// nolint
 package nacos
 
 import (
-       "net/url"
+       "reflect"
        "sync"
        "testing"
-       "time"
 )
 
 import (
-       "github.com/stretchr/testify/assert"
+       gxset "github.com/dubbogo/gost/container/set"
+       nacosClient "github.com/dubbogo/gost/database/kv/nacos"
+
+       "github.com/golang/mock/gomock"
+
+       "github.com/nacos-group/nacos-sdk-go/model"
+       "github.com/nacos-group/nacos-sdk-go/vo"
 )
 
 import (
        "dubbo.apache.org/dubbo-go/v3/common"
-       "dubbo.apache.org/dubbo-go/v3/common/constant"
        "dubbo.apache.org/dubbo-go/v3/config_center"
        "dubbo.apache.org/dubbo-go/v3/config_center/parser"
 )
 
-func getNacosConfig(t *testing.T) config_center.DynamicConfiguration {
-       params := url.Values{}
-       params.Set(constant.NacosNotLoadLocalCache, "true")
+// MockIConfigClient is a mock of IConfigClient interface
+type MockIConfigClient struct {
+       ctrl     *gomock.Controller
+       recorder *MockIConfigClientMockRecorder
+}
+
+// MockIConfigClientMockRecorder is the mock recorder for MockIConfigClient
+type MockIConfigClientMockRecorder struct {
+       mock *MockIConfigClient
+}
+
+// NewMockIConfigClient creates a new mock instance
+func NewMockIConfigClient(ctrl *gomock.Controller) *MockIConfigClient {
+       mock := &MockIConfigClient{ctrl: ctrl}
+       mock.recorder = &MockIConfigClientMockRecorder{mock}
+       return mock
+}
+
+// EXPECT returns an object that allows the caller to indicate expected use
+func (m *MockIConfigClient) EXPECT() *MockIConfigClientMockRecorder {
+       return m.recorder
+}
+
+// GetConfig mocks base method
+func (m *MockIConfigClient) GetConfig(param vo.ConfigParam) (string, error) {
+       ret := m.ctrl.Call(m, "GetConfig", param)
+       ret0, _ := ret[0].(string)
+       ret1, _ := ret[1].(error)
+       return ret0, ret1
+}
+
+// GetConfig indicates an expected call of GetConfig
+func (mr *MockIConfigClientMockRecorder) GetConfig(param interface{}) 
*gomock.Call {
+       return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetConfig", 
reflect.TypeOf((*MockIConfigClient)(nil).GetConfig), param)
+}
+
+// PublishConfig mocks base method
+func (m *MockIConfigClient) PublishConfig(param vo.ConfigParam) (bool, error) {
+       ret := m.ctrl.Call(m, "PublishConfig", param)
+       ret0, _ := ret[0].(bool)
+       ret1, _ := ret[1].(error)
+       return ret0, ret1
+}
 
-       params.Set(constant.NacosNamespaceID, "nacos")
-       params.Set(constant.TimeoutKey, "5s")
-       params.Set(constant.ClientNameKey, "nacos-client")
+// PublishConfig indicates an expected call of PublishConfig
+func (mr *MockIConfigClientMockRecorder) PublishConfig(param interface{}) 
*gomock.Call {
+       return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PublishConfig", 
reflect.TypeOf((*MockIConfigClient)(nil).PublishConfig), param)
+}
 
-       registryUrl, err := common.NewURL("registry://console.nacos.io:80", 
common.WithParams(params))
-       assert.Nil(t, err)
-       nacosConfig, err := newNacosDynamicConfiguration(registryUrl)
-       assert.Nil(t, err)
-       return nacosConfig
+// DeleteConfig mocks base method
+func (m *MockIConfigClient) DeleteConfig(param vo.ConfigParam) (bool, error) {
+       ret := m.ctrl.Call(m, "DeleteConfig", param)
+       ret0, _ := ret[0].(bool)
+       ret1, _ := ret[1].(error)
+       return ret0, ret1
 }
 
-func TestPublishConfig(t *testing.T) {
-       nacosConfig := getNacosConfig(t)
-       data := `dubbo.protocol.name=dubbo`
-       err := nacosConfig.PublishConfig("dubbo.properties", "dubbo-go", data)
-       assert.Nil(t, err)
+// DeleteConfig indicates an expected call of DeleteConfig
+func (mr *MockIConfigClientMockRecorder) DeleteConfig(param interface{}) 
*gomock.Call {
+       return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteConfig", 
reflect.TypeOf((*MockIConfigClient)(nil).DeleteConfig), param)
 }
 
-func TestGetConfig(t *testing.T) {
-       nacosConfig := getNacosConfig(t)
-       nacosConfig.SetParser(&parser.DefaultConfigurationParser{})
+// ListenConfig mocks base method
+func (m *MockIConfigClient) ListenConfig(params vo.ConfigParam) error {
+       ret := m.ctrl.Call(m, "ListenConfig", params)
+       ret0, _ := ret[0].(error)
+       return ret0
+}
 
-       config, err := nacosConfig.GetProperties("dubbo.properties", 
config_center.WithGroup("dubbo-go"))
-       assert.NotEmpty(t, config)
-       assert.NoError(t, err)
+// ListenConfig indicates an expected call of ListenConfig
+func (mr *MockIConfigClientMockRecorder) ListenConfig(params interface{}) 
*gomock.Call {
+       return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListenConfig", 
reflect.TypeOf((*MockIConfigClient)(nil).ListenConfig), params)
+}
+
+// CancelListenConfig mocks base method
+func (m *MockIConfigClient) CancelListenConfig(params vo.ConfigParam) error {
+       ret := m.ctrl.Call(m, "CancelListenConfig", params)
+       ret0, _ := ret[0].(error)
+       return ret0
+}
 
-       parse, err := nacosConfig.Parser().Parse(config)
-       assert.NoError(t, err)
-       assert.Equal(t, parse["dubbo.protocol.name"], "dubbo")
+// CancelListenConfig indicates an expected call of CancelListenConfig
+func (mr *MockIConfigClientMockRecorder) CancelListenConfig(params 
interface{}) *gomock.Call {
+       return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, 
"CancelListenConfig", 
reflect.TypeOf((*MockIConfigClient)(nil).CancelListenConfig), params)
 }
 
-func TestGetConfigKeysByGroup(t *testing.T) {
-       nacosConfig := getNacosConfig(t)
-       config, err := nacosConfig.GetConfigKeysByGroup("dubbo-go")
-       assert.NoError(t, err)
-       assert.True(t, config.Contains("dubbo.properties"))
+// SearchConfig mocks base method
+func (m *MockIConfigClient) SearchConfig(param vo.SearchConfigParam) 
(*model.ConfigPage, error) {
+       ret := m.ctrl.Call(m, "SearchConfig", param)
+       ret0, _ := ret[0].(*model.ConfigPage)
+       ret1, _ := ret[1].(error)
+       return ret0, ret1
 }
 
-func TestAddListener(t *testing.T) {
-       nacosConfig := getNacosConfig(t)
-       listener := &mockDataListener{}
-       time.Sleep(time.Second * 2)
-       nacosConfig.AddListener("dubbo.properties", listener)
+// SearchConfig indicates an expected call of SearchConfig
+func (mr *MockIConfigClientMockRecorder) SearchConfig(param interface{}) 
*gomock.Call {
+       return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SearchConfig", 
reflect.TypeOf((*MockIConfigClient)(nil).SearchConfig), param)
 }
 
-func TestRemoveListener(_ *testing.T) {
-       // TODO not supported in current go_nacos_sdk version
+// PublishAggr mocks base method
+func (m *MockIConfigClient) PublishAggr(param vo.ConfigParam) (bool, error) {
+       ret := m.ctrl.Call(m, "PublishAggr", param)
+       ret0, _ := ret[0].(bool)
+       ret1, _ := ret[1].(error)
+       return ret0, ret1
 }
 
-type mockDataListener struct {
-       wg    sync.WaitGroup
-       event string
+// PublishAggr indicates an expected call of PublishAggr
+func (mr *MockIConfigClientMockRecorder) PublishAggr(param interface{}) 
*gomock.Call {
+       return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PublishAggr", 
reflect.TypeOf((*MockIConfigClient)(nil).PublishAggr), param)
 }
 
-func (l *mockDataListener) Process(configType 
*config_center.ConfigChangeEvent) {
-       l.wg.Done()
-       l.event = configType.Key
+func Test_nacosDynamicConfiguration_PublishConfig(t *testing.T) {
+       type fields struct {
+               BaseDynamicConfiguration config_center.BaseDynamicConfiguration
+               url                      *common.URL
+               rootPath                 string
+               wg                       sync.WaitGroup
+               cltLock                  sync.Mutex
+               done                     chan struct{}
+               client                   *nacosClient.NacosConfigClient
+               keyListeners             sync.Map
+               parser                   parser.ConfigurationParser
+       }
+       type args struct {
+               key   string
+               group string
+               value string
+       }
+
+       ctrl := gomock.NewController(t)
+       mnc := NewMockIConfigClient(ctrl)
+       mnc.EXPECT().PublishConfig(gomock.Any()).Return(true, nil)
+       nc := &nacosClient.NacosConfigClient{}
+       nc.SetClient(mnc)
+
+       tests := []struct {
+               name    string
+               fields  fields
+               args    args
+               wantErr bool
+       }{
+               {
+                       name: "test",
+                       fields: fields{
+                               client: nc,
+                       },
+                       args: args{
+                               key:   "dubbo.properties",
+                               group: "dubbogo",
+                               value: "dubbo.protocol.name=dubbo",
+                       },
+                       wantErr: false,
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       n := &nacosDynamicConfiguration{
+                               BaseDynamicConfiguration: 
tt.fields.BaseDynamicConfiguration,
+                               url:                      tt.fields.url,
+                               rootPath:                 tt.fields.rootPath,
+                               wg:                       tt.fields.wg,
+                               cltLock:                  tt.fields.cltLock,
+                               done:                     tt.fields.done,
+                               client:                   tt.fields.client,
+                               keyListeners:             
tt.fields.keyListeners,
+                               parser:                   tt.fields.parser,
+                       }
+                       if err := n.PublishConfig(tt.args.key, tt.args.group, 
tt.args.value); (err != nil) != tt.wantErr {
+                               t.Errorf("PublishConfig() error = %v, wantErr 
%v", err, tt.wantErr)
+                       }
+               })
+       }
+}
+
+func Test_nacosDynamicConfiguration_GetConfigKeysByGroup(t *testing.T) {
+       type fields struct {
+               BaseDynamicConfiguration config_center.BaseDynamicConfiguration
+               url                      *common.URL
+               rootPath                 string
+               wg                       sync.WaitGroup
+               cltLock                  sync.Mutex
+               done                     chan struct{}
+               client                   *nacosClient.NacosConfigClient
+               keyListeners             sync.Map
+               parser                   parser.ConfigurationParser
+       }
+       type args struct {
+               group string
+       }
+
+       cp := &model.ConfigPage{
+               PageItems: []model.ConfigItem{
+                       {
+                               DataId: "dubbogo",
+                       },
+               },
+       }
+       result := gxset.NewSet()
+       result.Add("dubbogo")
+       ctrl := gomock.NewController(t)
+       mnc := NewMockIConfigClient(ctrl)
+       mnc.EXPECT().SearchConfig(gomock.Any()).Return(cp, nil)
+       nc := &nacosClient.NacosConfigClient{}
+       nc.SetClient(mnc)
+
+       tests := []struct {
+               name    string
+               fields  fields
+               args    args
+               want    *gxset.HashSet
+               wantErr bool
+       }{
+               {
+                       name: "test",
+                       fields: fields{
+                               client: nc,
+                       },
+                       args: args{
+                               group: "dubbo",
+                       },
+                       want:    result,
+                       wantErr: false,
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       n := &nacosDynamicConfiguration{
+                               BaseDynamicConfiguration: 
tt.fields.BaseDynamicConfiguration,
+                               url:                      tt.fields.url,
+                               rootPath:                 tt.fields.rootPath,
+                               wg:                       tt.fields.wg,
+                               cltLock:                  tt.fields.cltLock,
+                               done:                     tt.fields.done,
+                               client:                   tt.fields.client,
+                               keyListeners:             
tt.fields.keyListeners,
+                               parser:                   tt.fields.parser,
+                       }

Review Comment:
   n := &nacosDynamicConfiguration{}
   *n = tt.fields



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to