dubbo.apache.org/dubbo-go/v3@v3.1.1/metadata/report/nacos/report_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 import ( 21 "encoding/json" 22 "reflect" 23 "strconv" 24 "testing" 25 ) 26 27 import ( 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/common/constant" 39 "dubbo.apache.org/dubbo-go/v3/metadata/identifier" 40 ) 41 42 func newServiceMetadataIdentifier() *identifier.ServiceMetadataIdentifier { 43 return &identifier.ServiceMetadataIdentifier{ 44 Protocol: "nacos", 45 Revision: "a", 46 BaseMetadataIdentifier: identifier.BaseMetadataIdentifier{ 47 ServiceInterface: "com.test.MyTest", 48 Version: "1.0.0", 49 Group: "test_group", 50 Side: "service", 51 }, 52 } 53 } 54 55 func newMetadataIdentifier(side string) *identifier.MetadataIdentifier { 56 return &identifier.MetadataIdentifier{ 57 Application: "test", 58 BaseMetadataIdentifier: identifier.BaseMetadataIdentifier{ 59 ServiceInterface: "com.test.MyTest", 60 Version: "1.0.0", 61 Group: "test_group", 62 Side: side, 63 }, 64 } 65 } 66 67 // MockIConfigClient is a mock of IConfigClient interface 68 type MockIConfigClient struct { 69 ctrl *gomock.Controller 70 recorder *MockIConfigClientMockRecorder 71 } 72 73 // MockIConfigClientMockRecorder is the mock recorder for MockIConfigClient 74 type MockIConfigClientMockRecorder struct { 75 mock *MockIConfigClient 76 } 77 78 // NewMockIConfigClient creates a new mock instance 79 func NewMockIConfigClient(ctrl *gomock.Controller) *MockIConfigClient { 80 mock := &MockIConfigClient{ctrl: ctrl} 81 mock.recorder = &MockIConfigClientMockRecorder{mock} 82 return mock 83 } 84 85 // EXPECT returns an object that allows the caller to indicate expected use 86 func (m *MockIConfigClient) EXPECT() *MockIConfigClientMockRecorder { 87 return m.recorder 88 } 89 90 // GetConfig mocks base method 91 func (m *MockIConfigClient) GetConfig(param vo.ConfigParam) (string, error) { 92 ret := m.ctrl.Call(m, "GetConfig", param) 93 ret0, _ := ret[0].(string) 94 ret1, _ := ret[1].(error) 95 return ret0, ret1 96 } 97 98 // GetConfig indicates an expected call of GetConfig 99 func (mr *MockIConfigClientMockRecorder) GetConfig(param interface{}) *gomock.Call { 100 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetConfig", reflect.TypeOf((*MockIConfigClient)(nil).GetConfig), param) 101 } 102 103 // PublishConfig mocks base method 104 func (m *MockIConfigClient) PublishConfig(param vo.ConfigParam) (bool, error) { 105 ret := m.ctrl.Call(m, "PublishConfig", param) 106 ret0, _ := ret[0].(bool) 107 ret1, _ := ret[1].(error) 108 return ret0, ret1 109 } 110 111 // PublishConfig indicates an expected call of PublishConfig 112 func (mr *MockIConfigClientMockRecorder) PublishConfig(param interface{}) *gomock.Call { 113 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PublishConfig", reflect.TypeOf((*MockIConfigClient)(nil).PublishConfig), param) 114 } 115 116 // DeleteConfig mocks base method 117 func (m *MockIConfigClient) DeleteConfig(param vo.ConfigParam) (bool, error) { 118 ret := m.ctrl.Call(m, "DeleteConfig", param) 119 ret0, _ := ret[0].(bool) 120 ret1, _ := ret[1].(error) 121 return ret0, ret1 122 } 123 124 // DeleteConfig indicates an expected call of DeleteConfig 125 func (mr *MockIConfigClientMockRecorder) DeleteConfig(param interface{}) *gomock.Call { 126 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteConfig", reflect.TypeOf((*MockIConfigClient)(nil).DeleteConfig), param) 127 } 128 129 // ListenConfig mocks base method 130 func (m *MockIConfigClient) ListenConfig(params vo.ConfigParam) error { 131 ret := m.ctrl.Call(m, "ListenConfig", params) 132 ret0, _ := ret[0].(error) 133 return ret0 134 } 135 136 // CloseClient close nacos client 137 func (m *MockIConfigClient) CloseClient() { 138 } 139 140 // ListenConfig indicates an expected call of ListenConfig 141 func (mr *MockIConfigClientMockRecorder) ListenConfig(params interface{}) *gomock.Call { 142 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListenConfig", reflect.TypeOf((*MockIConfigClient)(nil).ListenConfig), params) 143 } 144 145 // CancelListenConfig mocks base method 146 func (m *MockIConfigClient) CancelListenConfig(params vo.ConfigParam) error { 147 ret := m.ctrl.Call(m, "CancelListenConfig", params) 148 ret0, _ := ret[0].(error) 149 return ret0 150 } 151 152 // CancelListenConfig indicates an expected call of CancelListenConfig 153 func (mr *MockIConfigClientMockRecorder) CancelListenConfig(params interface{}) *gomock.Call { 154 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CancelListenConfig", reflect.TypeOf((*MockIConfigClient)(nil).CancelListenConfig), params) 155 } 156 157 // SearchConfig mocks base method 158 func (m *MockIConfigClient) SearchConfig(param vo.SearchConfigParam) (*model.ConfigPage, error) { 159 ret := m.ctrl.Call(m, "SearchConfig", param) 160 ret0, _ := ret[0].(*model.ConfigPage) 161 ret1, _ := ret[1].(error) 162 return ret0, ret1 163 } 164 165 // SearchConfig indicates an expected call of SearchConfig 166 func (mr *MockIConfigClientMockRecorder) SearchConfig(param interface{}) *gomock.Call { 167 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SearchConfig", reflect.TypeOf((*MockIConfigClient)(nil).SearchConfig), param) 168 } 169 170 // PublishAggr mocks base method 171 func (m *MockIConfigClient) PublishAggr(param vo.ConfigParam) (bool, error) { 172 ret := m.ctrl.Call(m, "PublishAggr", param) 173 ret0, _ := ret[0].(bool) 174 ret1, _ := ret[1].(error) 175 return ret0, ret1 176 } 177 178 // PublishAggr indicates an expected call of PublishAggr 179 func (mr *MockIConfigClientMockRecorder) PublishAggr(param interface{}) *gomock.Call { 180 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PublishAggr", reflect.TypeOf((*MockIConfigClient)(nil).PublishAggr), param) 181 } 182 183 type fields struct { 184 client *nacosClient.NacosConfigClient 185 } 186 type args struct { 187 subscriberMetadataIdentifier *identifier.SubscriberMetadataIdentifier 188 info *common.MetadataInfo 189 providerIdentifier *identifier.MetadataIdentifier 190 serviceDefinitions string 191 consumerMetadataIdentifier *identifier.MetadataIdentifier 192 serviceParameterString string 193 url *common.URL 194 serviceMetadataIdentifier *identifier.ServiceMetadataIdentifier 195 urls string 196 key string 197 group string 198 value string 199 } 200 201 func newNacosMetadataReport(f fields) *nacosMetadataReport { 202 return &nacosMetadataReport{ 203 client: f.client, 204 } 205 } 206 207 func Test_nacosMetadataReport_GetAppMetadata(t *testing.T) { 208 mi := common.MetadataInfo{ 209 App: "GetAppMetadata", 210 } 211 data, _ := json.Marshal(mi) 212 213 ctrl := gomock.NewController(t) 214 mnc := NewMockIConfigClient(ctrl) 215 mnc.EXPECT().GetConfig(gomock.Any()).MaxTimes(2).Return(string(data), nil) 216 nc := &nacosClient.NacosConfigClient{} 217 nc.SetClient(mnc) 218 219 tests := []struct { 220 name string 221 fields fields 222 args args 223 want *common.MetadataInfo 224 wantErr bool 225 }{ 226 { 227 name: "test", 228 fields: fields{ 229 client: nc, 230 }, 231 args: args{ 232 subscriberMetadataIdentifier: &identifier.SubscriberMetadataIdentifier{}, 233 }, 234 want: &mi, 235 wantErr: false, 236 }, 237 } 238 for _, tt := range tests { 239 t.Run(tt.name, func(t *testing.T) { 240 n := newNacosMetadataReport(tt.fields) 241 got, err := n.GetAppMetadata(tt.args.subscriberMetadataIdentifier) 242 if (err != nil) != tt.wantErr { 243 t.Errorf("GetAppMetadata() error = %v, wantErr %v", err, tt.wantErr) 244 return 245 } 246 if !reflect.DeepEqual(got, tt.want) { 247 t.Errorf("GetAppMetadata() got = %v, want %v", got, tt.want) 248 } 249 }) 250 } 251 } 252 253 func Test_nacosMetadataReport_PublishAppMetadata(t *testing.T) { 254 ctrl := gomock.NewController(t) 255 mnc := NewMockIConfigClient(ctrl) 256 mnc.EXPECT().PublishConfig(gomock.Any()).Times(2).Return(true, nil) 257 nc := &nacosClient.NacosConfigClient{} 258 nc.SetClient(mnc) 259 260 tests := []struct { 261 name string 262 fields fields 263 args args 264 wantErr bool 265 }{ 266 { 267 name: "test", 268 fields: fields{ 269 client: nc, 270 }, 271 args: args{ 272 subscriberMetadataIdentifier: &identifier.SubscriberMetadataIdentifier{}, 273 info: &common.MetadataInfo{ 274 App: "PublishAppMetadata", 275 }, 276 }, 277 wantErr: false, 278 }, 279 } 280 for _, tt := range tests { 281 t.Run(tt.name, func(t *testing.T) { 282 n := newNacosMetadataReport(tt.fields) 283 if err := n.PublishAppMetadata(tt.args.subscriberMetadataIdentifier, tt.args.info); (err != nil) != tt.wantErr { 284 t.Errorf("PublishAppMetadata() error = %v, wantErr %v", err, tt.wantErr) 285 } 286 }) 287 } 288 } 289 290 func Test_nacosMetadataReport_StoreProviderMetadata(t *testing.T) { 291 ctrl := gomock.NewController(t) 292 mnc := NewMockIConfigClient(ctrl) 293 mnc.EXPECT().PublishConfig(gomock.Any()).Return(true, nil) 294 nc := &nacosClient.NacosConfigClient{} 295 nc.SetClient(mnc) 296 297 tests := []struct { 298 name string 299 fields fields 300 args args 301 wantErr bool 302 }{ 303 { 304 name: "test", 305 fields: fields{ 306 client: nc, 307 }, 308 args: args{ 309 providerIdentifier: newMetadataIdentifier("provider"), 310 serviceDefinitions: "provider", 311 }, 312 wantErr: false, 313 }, 314 } 315 for _, tt := range tests { 316 t.Run(tt.name, func(t *testing.T) { 317 n := newNacosMetadataReport(tt.fields) 318 if err := n.StoreProviderMetadata(tt.args.providerIdentifier, tt.args.serviceDefinitions); (err != nil) != tt.wantErr { 319 t.Errorf("StoreProviderMetadata() error = %v, wantErr %v", err, tt.wantErr) 320 } 321 }) 322 } 323 } 324 325 func Test_nacosMetadataReport_StoreConsumerMetadata(t *testing.T) { 326 ctrl := gomock.NewController(t) 327 mnc := NewMockIConfigClient(ctrl) 328 mnc.EXPECT().PublishConfig(gomock.Any()).Return(true, nil) 329 nc := &nacosClient.NacosConfigClient{} 330 nc.SetClient(mnc) 331 332 tests := []struct { 333 name string 334 fields fields 335 args args 336 wantErr bool 337 }{ 338 { 339 name: "test", 340 fields: fields{ 341 client: nc, 342 }, 343 args: args{ 344 consumerMetadataIdentifier: newMetadataIdentifier("conusmer"), 345 serviceParameterString: "conusmer", 346 }, 347 wantErr: false, 348 }, 349 } 350 for _, tt := range tests { 351 t.Run(tt.name, func(t *testing.T) { 352 n := newNacosMetadataReport(tt.fields) 353 if err := n.StoreConsumerMetadata(tt.args.consumerMetadataIdentifier, tt.args.serviceParameterString); (err != nil) != tt.wantErr { 354 t.Errorf("StoreConsumerMetadata() error = %v, wantErr %v", err, tt.wantErr) 355 } 356 }) 357 } 358 } 359 360 func Test_nacosMetadataReport_SaveServiceMetadata(t *testing.T) { 361 ctrl := gomock.NewController(t) 362 mnc := NewMockIConfigClient(ctrl) 363 mnc.EXPECT().PublishConfig(gomock.Any()).Return(true, nil) 364 nc := &nacosClient.NacosConfigClient{} 365 nc.SetClient(mnc) 366 367 serviceURL, _ := common.NewURL("registry://test.nacos.io:80", 368 common.WithParamsValue(constant.RegistryRoleKey, strconv.Itoa(common.PROVIDER)), 369 common.WithParamsValue(constant.ClientNameKey, "nacos-client")) 370 371 tests := []struct { 372 name string 373 fields fields 374 args args 375 wantErr bool 376 }{ 377 { 378 name: "test", 379 fields: fields{ 380 client: nc, 381 }, 382 args: args{ 383 serviceMetadataIdentifier: newServiceMetadataIdentifier(), 384 url: serviceURL, 385 }, 386 wantErr: false, 387 }, 388 } 389 for _, tt := range tests { 390 t.Run(tt.name, func(t *testing.T) { 391 n := newNacosMetadataReport(tt.fields) 392 if err := n.SaveServiceMetadata(tt.args.serviceMetadataIdentifier, tt.args.url); (err != nil) != tt.wantErr { 393 t.Errorf("SaveServiceMetadata() error = %v, wantErr %v", err, tt.wantErr) 394 } 395 }) 396 } 397 } 398 399 func Test_nacosMetadataReport_RemoveServiceMetadata(t *testing.T) { 400 ctrl := gomock.NewController(t) 401 mnc := NewMockIConfigClient(ctrl) 402 mnc.EXPECT().DeleteConfig(gomock.Any()).Return(true, nil) 403 nc := &nacosClient.NacosConfigClient{} 404 nc.SetClient(mnc) 405 406 tests := []struct { 407 name string 408 fields fields 409 args args 410 wantErr bool 411 }{ 412 { 413 name: "test", 414 fields: fields{ 415 client: nc, 416 }, 417 args: args{ 418 serviceMetadataIdentifier: newServiceMetadataIdentifier(), 419 }, 420 wantErr: false, 421 }, 422 } 423 for _, tt := range tests { 424 t.Run(tt.name, func(t *testing.T) { 425 n := newNacosMetadataReport(tt.fields) 426 if err := n.RemoveServiceMetadata(tt.args.serviceMetadataIdentifier); (err != nil) != tt.wantErr { 427 t.Errorf("RemoveServiceMetadata() error = %v, wantErr %v", err, tt.wantErr) 428 } 429 }) 430 } 431 } 432 433 func Test_nacosMetadataReport_SaveSubscribedData(t *testing.T) { 434 ctrl := gomock.NewController(t) 435 mnc := NewMockIConfigClient(ctrl) 436 mnc.EXPECT().PublishConfig(gomock.Any()).Return(true, nil) 437 nc := &nacosClient.NacosConfigClient{} 438 nc.SetClient(mnc) 439 440 tests := []struct { 441 name string 442 fields fields 443 args args 444 wantErr bool 445 }{ 446 { 447 name: "test", 448 fields: fields{ 449 client: nc, 450 }, 451 args: args{ 452 subscriberMetadataIdentifier: &identifier.SubscriberMetadataIdentifier{}, 453 urls: "urls", 454 }, 455 wantErr: false, 456 }, 457 } 458 for _, tt := range tests { 459 t.Run(tt.name, func(t *testing.T) { 460 n := newNacosMetadataReport(tt.fields) 461 if err := n.SaveSubscribedData(tt.args.subscriberMetadataIdentifier, tt.args.urls); (err != nil) != tt.wantErr { 462 t.Errorf("SaveSubscribedData() error = %v, wantErr %v", err, tt.wantErr) 463 } 464 }) 465 } 466 } 467 468 func Test_nacosMetadataReport_RegisterServiceAppMapping(t *testing.T) { 469 ctrl := gomock.NewController(t) 470 mnc := NewMockIConfigClient(ctrl) 471 mnc.EXPECT().GetConfig(gomock.Any()).Return("oldValue", nil) 472 nc := &nacosClient.NacosConfigClient{} 473 nc.SetClient(mnc) 474 475 tests := []struct { 476 name string 477 fields fields 478 args args 479 wantErr bool 480 }{ 481 { 482 name: "test", 483 fields: fields{ 484 client: nc, 485 }, 486 args: args{ 487 key: "test", 488 group: "test", 489 value: "oldValue", 490 }, 491 wantErr: false, 492 }, 493 } 494 for _, tt := range tests { 495 t.Run(tt.name, func(t *testing.T) { 496 n := newNacosMetadataReport(tt.fields) 497 if err := n.RegisterServiceAppMapping(tt.args.key, tt.args.group, tt.args.value); (err != nil) != tt.wantErr { 498 t.Errorf("RegisterServiceAppMapping() error = %v, wantErr %v", err, tt.wantErr) 499 } 500 }) 501 } 502 }