This is an automated email from the ASF dual-hosted git repository. liujun pushed a commit to branch refactor-with-go in repository https://gitbox.apache.org/repos/asf/dubbo-admin.git
The following commit(s) were added to refs/heads/refactor-with-go by this push: new ca77afa0 ADD SearchService Test (#1039) ca77afa0 is described below commit ca77afa079d8477d67ec4f1df3ab893bb7cfd467 Author: wudong5 <63356845+wudo...@users.noreply.github.com> AuthorDate: Wed Mar 22 08:53:12 2023 +0800 ADD SearchService Test (#1039) --- pkg/admin/services/provider_service_impl.go | 7 + pkg/admin/services/provider_service_impl_test.go | 271 +++++++++++++++++++++++ pkg/admin/util/sync_utils.go | 7 + pkg/admin/util/sync_utils_test.go | 122 ++++++++++ 4 files changed, 407 insertions(+) diff --git a/pkg/admin/services/provider_service_impl.go b/pkg/admin/services/provider_service_impl.go index 061110b1..ef189b23 100644 --- a/pkg/admin/services/provider_service_impl.go +++ b/pkg/admin/services/provider_service_impl.go @@ -30,6 +30,7 @@ import ( type ProviderServiceImpl struct{} +// FindServices finds all services func (p *ProviderServiceImpl) FindServices() ([]string, error) { var services []string servicesAny, ok := cache.InterfaceRegistryCache.Load(constant.ProvidersCategory) @@ -48,6 +49,7 @@ func (p *ProviderServiceImpl) FindServices() ([]string, error) { return services, nil } +// FindApplications finds all applications func (p *ProviderServiceImpl) FindApplications() ([]string, error) { var ( applications []string @@ -79,6 +81,7 @@ func (p *ProviderServiceImpl) FindApplications() ([]string, error) { return applications, err } +// findAddresses finds all addresses func (p *ProviderServiceImpl) findAddresses() ([]string, error) { var ( addresses []string @@ -110,6 +113,7 @@ func (p *ProviderServiceImpl) findAddresses() ([]string, error) { return addresses, err } +// FindByService finds providers by service name and returns a list of providers func (p *ProviderServiceImpl) FindByService(providerService string) ([]*model.Provider, error) { filter := make(map[string]string) filter[constant.CategoryKey] = constant.ProvidersCategory @@ -121,6 +125,7 @@ func (p *ProviderServiceImpl) FindByService(providerService string) ([]*model.Pr return util.URL2ProviderList(servicesMap), nil } +// FindByApplication finds providers by address and returns a list of providers func (p *ProviderServiceImpl) findByAddress(providerAddress string) ([]*model.Provider, error) { filter := make(map[string]string) filter[constant.CategoryKey] = constant.ProvidersCategory @@ -132,6 +137,7 @@ func (p *ProviderServiceImpl) findByAddress(providerAddress string) ([]*model.Pr return util.URL2ProviderList(servicesMap), nil } +// findByApplication finds providers by application and returns a list of providers func (p *ProviderServiceImpl) findByApplication(providerApplication string) ([]*model.Provider, error) { filter := make(map[string]string) filter[constant.CategoryKey] = constant.ProvidersCategory @@ -143,6 +149,7 @@ func (p *ProviderServiceImpl) findByApplication(providerApplication string) ([]* return util.URL2ProviderList(servicesMap), nil } +// FindService by patterns and filters, patterns support IP, service and application. func (p *ProviderServiceImpl) FindService(pattern string, filter string) ([]*model.Provider, error) { var ( providers []*model.Provider diff --git a/pkg/admin/services/provider_service_impl_test.go b/pkg/admin/services/provider_service_impl_test.go new file mode 100644 index 00000000..454ed1a3 --- /dev/null +++ b/pkg/admin/services/provider_service_impl_test.go @@ -0,0 +1,271 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package services + +import ( + "net/url" + "reflect" + "sync" + "testing" + + "github.com/apache/dubbo-admin/pkg/admin/util" + + "github.com/apache/dubbo-admin/pkg/admin/constant" + + "github.com/apache/dubbo-admin/pkg/admin/cache" + + "dubbo.apache.org/dubbo-go/v3/common" + + "github.com/apache/dubbo-admin/pkg/admin/model" +) + +var testProvider *model.Provider + +func init() { + service := &sync.Map{} + queryParams := url.Values{ + constant.ApplicationKey: {"test"}, + } + testURL, _ := common.NewURL(common.GetLocalIp()+":0", + common.WithProtocol(constant.AdminProtocol), + common.WithParams(queryParams), + common.WithLocation(common.GetLocalIp()+":0"), + ) + service.Store("test", map[string]*common.URL{ + "test": testURL, + }) + cache.InterfaceRegistryCache.Store(constant.ProvidersCategory, service) + testProvider = util.URL2Provider("test", testURL) +} + +func TestProviderServiceImpl_FindServices(t *testing.T) { + tests := []struct { + name string + want []string + wantErr bool + }{ + { + name: "Test", + want: []string{"test"}, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &ProviderServiceImpl{} + got, err := p.FindServices() + if (err != nil) != tt.wantErr { + t.Errorf("FindServices() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("FindServices() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestProviderServiceImpl_FindApplications(t *testing.T) { + tests := []struct { + name string + want []string + wantErr bool + }{ + { + name: "Test", + want: []string{"test"}, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &ProviderServiceImpl{} + got, err := p.FindApplications() + if (err != nil) != tt.wantErr { + t.Errorf("FindApplications() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("FindApplications() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestProviderServiceImpl_findAddresses(t *testing.T) { + tests := []struct { + name string + want []string + wantErr bool + }{ + { + name: "Test", + want: []string{common.GetLocalIp() + ":0"}, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &ProviderServiceImpl{} + got, err := p.findAddresses() + if (err != nil) != tt.wantErr { + t.Errorf("findAddresses() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("findAddresses() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestProviderServiceImpl_FindByService(t *testing.T) { + type args struct { + providerService string + } + tests := []struct { + name string + args args + want []*model.Provider + wantErr bool + }{ + { + name: "Test", + args: args{ + providerService: "test", + }, + want: []*model.Provider{testProvider}, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &ProviderServiceImpl{} + got, err := p.FindByService(tt.args.providerService) + if (err != nil) != tt.wantErr { + t.Errorf("FindByService() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("FindByService() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestProviderServiceImpl_findByAddress(t *testing.T) { + type args struct { + providerAddress string + } + tests := []struct { + name string + args args + want []*model.Provider + wantErr bool + }{ + { + name: "Test", + args: args{ + providerAddress: common.GetLocalIp() + ":0", + }, + want: []*model.Provider{testProvider}, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &ProviderServiceImpl{} + got, err := p.findByAddress(tt.args.providerAddress) + if (err != nil) != tt.wantErr { + t.Errorf("findByAddress() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("findByAddress() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestProviderServiceImpl_findByApplication(t *testing.T) { + type args struct { + providerApplication string + } + tests := []struct { + name string + args args + want []*model.Provider + wantErr bool + }{ + { + name: "Test", + args: args{ + providerApplication: "test", + }, + want: []*model.Provider{testProvider}, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &ProviderServiceImpl{} + got, err := p.findByApplication(tt.args.providerApplication) + if (err != nil) != tt.wantErr { + t.Errorf("findByApplication() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("findByApplication() got = %v, want %v", got, tt.want) + } + }) + } +} + +func TestProviderServiceImpl_FindService(t *testing.T) { + type args struct { + pattern string + filter string + } + tests := []struct { + name string + args args + want []*model.Provider + wantErr bool + }{ + { + name: "Test", + args: args{ + pattern: "ip", + filter: "test", + }, + want: nil, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &ProviderServiceImpl{} + got, err := p.FindService(tt.args.pattern, tt.args.filter) + if (err != nil) != tt.wantErr { + t.Errorf("FindService() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("FindService() got = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/pkg/admin/util/sync_utils.go b/pkg/admin/util/sync_utils.go index 3715e6dd..b4efbc35 100644 --- a/pkg/admin/util/sync_utils.go +++ b/pkg/admin/util/sync_utils.go @@ -32,6 +32,7 @@ const ( IDFilterKey = ".id" ) +// URL2Provider transforms a URL into a Service func URL2Provider(id string, url *common.URL) *model.Provider { if url == nil { return nil @@ -54,6 +55,7 @@ func URL2Provider(id string, url *common.URL) *model.Provider { } } +// URL2ProviderList transforms URLs to a list of providers func URL2ProviderList(servicesMap map[string]*common.URL) []*model.Provider { var providers []*model.Provider if servicesMap == nil { @@ -68,6 +70,7 @@ func URL2ProviderList(servicesMap map[string]*common.URL) []*model.Provider { return providers } +// URL2Consumer transforms a URL to a consumer func URL2Consumer(id string, url *common.URL) *model.Consumer { if url == nil { return nil @@ -83,6 +86,7 @@ func URL2Consumer(id string, url *common.URL) *model.Consumer { } } +// URL2ConsumerList transforms URLs into a list of consumers func URL2ConsumerList(servicesMap map[string]*common.URL) []*model.Consumer { var consumers []*model.Consumer if servicesMap == nil { @@ -97,6 +101,7 @@ func URL2ConsumerList(servicesMap map[string]*common.URL) []*model.Consumer { return consumers } +// FilterFromCategory get URLs from cache by filter func FilterFromCategory(filter map[string]string) (map[string]*common.URL, error) { c, ok := filter[constant.CategoryKey] if !ok { @@ -114,6 +119,7 @@ func FilterFromCategory(filter map[string]string) (map[string]*common.URL, error return filterFromService(servicesMap, filter) } +// filterFromService get URLs from service by filter func filterFromService(servicesMap *sync.Map, filter map[string]string) (map[string]*common.URL, error) { ret := make(map[string]*common.URL) var err error @@ -143,6 +149,7 @@ func filterFromService(servicesMap *sync.Map, filter map[string]string) (map[str return ret, err } +// filterFromURLs filter URLs func filterFromURLs(from, to map[string]*common.URL, filter map[string]string) { if from == nil || to == nil { return diff --git a/pkg/admin/util/sync_utils_test.go b/pkg/admin/util/sync_utils_test.go new file mode 100644 index 00000000..a1f2f2de --- /dev/null +++ b/pkg/admin/util/sync_utils_test.go @@ -0,0 +1,122 @@ +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package util + +import ( + "reflect" + "sync" + "testing" + + "github.com/apache/dubbo-admin/pkg/admin/constant" + + "dubbo.apache.org/dubbo-go/v3/common" +) + +func TestFilterFromCategory(t *testing.T) { + type args struct { + filter map[string]string + } + tests := []struct { + name string + args args + want map[string]*common.URL + wantErr bool + }{ + { + name: "RightTest", + args: args{ + filter: map[string]string{ + constant.CategoryKey: constant.ProvidersCategory, + }, + }, + want: map[string]*common.URL{}, + wantErr: false, + }, + { + name: "WrongTest", + args: args{ + filter: map[string]string{}, + }, + want: map[string]*common.URL{}, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + _, err := FilterFromCategory(tt.args.filter) + if (err != nil) != tt.wantErr { + t.Errorf("FilterFromCategory() error = %v, wantErr %v", err, tt.wantErr) + return + } + }) + } +} + +func Test_filterFromService(t *testing.T) { + type args struct { + servicesMap *sync.Map + filter map[string]string + } + tests := []struct { + name string + args args + want map[string]*common.URL + wantErr bool + }{ + { + name: "RightTest", + args: args{ + servicesMap: &sync.Map{}, + filter: map[string]string{ + ServiceFilterKey: "test", + }, + }, + want: map[string]*common.URL{ + "test": {}, + }, + wantErr: false, + }, + { + name: "WrongTest", + args: args{ + servicesMap: &sync.Map{}, + filter: map[string]string{ + ServiceFilterKey: "test", + }, + }, + want: nil, + wantErr: true, + }, + } + tests[0].args.servicesMap.Store("test", map[string]*common.URL{ + "test": {}, + }) + tests[1].args.servicesMap.Store("test", map[string]string{ + "test": "string", + }) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := filterFromService(tt.args.servicesMap, tt.args.filter) + if (err != nil) != tt.wantErr { + t.Errorf("filterFromService() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("filterFromService() got = %v, want %v", got, tt.want) + } + }) + } +}