This is an automated email from the ASF dual-hosted git repository.
albumenj 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 95ac6ae2 test: add unit tests for route service (#1058)
95ac6ae2 is described below
commit 95ac6ae23f703ac419b66796c2e1481fdd645531
Author: Yixiang Zhao <[email protected]>
AuthorDate: Tue Mar 28 15:11:48 2023 +0800
test: add unit tests for route service (#1058)
* test: add unit tests for tag_route service
Signed-off-by: Yixiang Zhao <[email protected]>
* test: add unit tests for condition_route service
Signed-off-by: Yixiang Zhao <[email protected]>
* test: add unit test for getRoutePath
Signed-off-by: Yixiang Zhao <[email protected]>
---------
Signed-off-by: Yixiang Zhao <[email protected]>
---
pkg/admin/services/route_service_impl_test.go | 649 ++++++++++++++++++++++++++
1 file changed, 649 insertions(+)
diff --git a/pkg/admin/services/route_service_impl_test.go
b/pkg/admin/services/route_service_impl_test.go
new file mode 100644
index 00000000..811609b4
--- /dev/null
+++ b/pkg/admin/services/route_service_impl_test.go
@@ -0,0 +1,649 @@
+// 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 (
+ "reflect"
+ "testing"
+
+ "github.com/apache/dubbo-admin/pkg/admin/config"
+ "github.com/apache/dubbo-admin/pkg/admin/constant"
+ "github.com/apache/dubbo-admin/pkg/admin/model"
+ "github.com/apache/dubbo-admin/pkg/admin/util"
+ "github.com/golang/mock/gomock"
+)
+
+func TestRouteServiceImpl_CreateTagRoute(t *testing.T) {
+ ctrl := gomock.NewController(t)
+ mockGovernanceConfig := config.NewMockGovernanceConfig(ctrl)
+ mockGovernanceConfig.EXPECT().SetConfig(gomock.Any(),
gomock.Any()).Return(nil)
+
+ type args struct {
+ tagRoute *model.TagRouteDto
+ }
+ tests := []struct {
+ name string
+ s RouteService
+ args args
+ wantErr bool
+ }{
+ {
+ name: "test create tag route",
+ s: &RouteServiceImpl{
+ GovernanceConfig: mockGovernanceConfig,
+ },
+ args: args{
+ tagRoute: &model.TagRouteDto{
+ Base: model.Base{
+ Application: "",
+ Service: "testService",
+ ServiceVersion: "testVersion",
+ ServiceGroup: "testGroup",
+ },
+ Enabled: true,
+ ConfigVersion: "v3.0",
+ Force: true,
+ Tags: []model.Tag{
+ {
+ Name: "gray",
+ Match:
[]model.ParamMatch{
+ {
+ Key:
"env",
+ Value:
model.StringMatch{
+
Exact: "gray",
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ wantErr: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if err := tt.s.CreateTagRoute(*tt.args.tagRoute); (err
!= nil) != tt.wantErr {
+ t.Errorf("RouteServiceImpl.CreateTagRoute()
error = %v, wantErr %v", err, tt.wantErr)
+ }
+ })
+ }
+}
+
+func TestRouteServiceImpl_UpdateTagRoute(t *testing.T) {
+ ctrl := gomock.NewController(t)
+ mockGovernanceConfig := config.NewMockGovernanceConfig(ctrl)
+ mockGovernanceConfig.EXPECT().SetConfig(gomock.Any(),
gomock.Any()).Return(nil)
+
mockGovernanceConfig.EXPECT().GetConfig(getRoutePath(util.BuildServiceKey(model.Base{
+ Application: "",
+ Service: "testService",
+ ServiceVersion: "testVersion",
+ ServiceGroup: "testGroup",
+ }),
constant.TagRoute)).Return(`{"enabled":true,"force":true,"key":"testService:testVersion:testGroup","tags":[{"name":"gray","match":[{"key":"env","value":{"exact":"gray"}}]}]}`,
nil)
+
+ type args struct {
+ tagRoute *model.TagRouteDto
+ }
+ tests := []struct {
+ name string
+ s RouteService
+ args args
+ wantErr bool
+ }{
+ {
+ name: "test update tag route",
+ s: &RouteServiceImpl{
+ GovernanceConfig: mockGovernanceConfig,
+ },
+ args: args{
+ tagRoute: &model.TagRouteDto{
+ Base: model.Base{
+ Application: "",
+ Service: "testService",
+ ServiceVersion: "testVersion",
+ ServiceGroup: "testGroup",
+ },
+ Enabled: true,
+ ConfigVersion: "v3.0",
+ Force: false,
+ Tags: []model.Tag{
+ {
+ Name: "gray",
+ Match:
[]model.ParamMatch{
+ {
+ Key:
"env",
+ Value:
model.StringMatch{
+
Exact: "gray",
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ wantErr: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if err := tt.s.UpdateTagRoute(*tt.args.tagRoute); (err
!= nil) != tt.wantErr {
+ t.Errorf("RouteServiceImpl.UpdateTagRoute()
error = %v, wantErr %v", err, tt.wantErr)
+ }
+ })
+ }
+}
+
+func TestRouteServiceImpl_DeleteTagRoute(t *testing.T) {
+ ctrl := gomock.NewController(t)
+ mockGovernanceConfig := config.NewMockGovernanceConfig(ctrl)
+
mockGovernanceConfig.EXPECT().DeleteConfig(getRoutePath(util.BuildServiceKey(model.Base{
+ Application: "",
+ Service: "testService",
+ ServiceVersion: "testVersion",
+ ServiceGroup: "testGroup",
+ }), constant.TagRoute)).Return(nil)
+
+ type args struct {
+ key string
+ }
+ tests := []struct {
+ name string
+ s RouteService
+ args args
+ wantErr bool
+ }{
+ {
+ name: "test delete tag route",
+ s: &RouteServiceImpl{
+ GovernanceConfig: mockGovernanceConfig,
+ },
+ args: args{
+ key: "testService:testVersion:testGroup",
+ },
+ wantErr: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if err := tt.s.DeleteTagRoute(tt.args.key); (err !=
nil) != tt.wantErr {
+ t.Errorf("RouteServiceImpl.DeleteTagRoute()
error = %v, wantErr %v", err, tt.wantErr)
+ }
+ })
+ }
+}
+
+func TestRouteServiceImpl_FindTagRoute(t *testing.T) {
+ ctrl := gomock.NewController(t)
+ mockGovernanceConfig := config.NewMockGovernanceConfig(ctrl)
+
mockGovernanceConfig.EXPECT().GetConfig(getRoutePath(util.BuildServiceKey(model.Base{
+ Application: "",
+ Service: "testService",
+ ServiceVersion: "testVersion",
+ ServiceGroup: "testGroup",
+ }),
constant.TagRoute)).Return(`{"enabled":true,"force":true,"key":"testService:testVersion:testGroup","tags":[{"name":"gray","match":[{"key":"env","value":{"exact":"gray"}}]}]}`,
nil)
+
+ type args struct {
+ key string
+ }
+ tests := []struct {
+ name string
+ s RouteService
+ args args
+ want model.TagRouteDto
+ wantErr bool
+ }{
+ {
+ name: "test find tag route",
+ s: &RouteServiceImpl{
+ GovernanceConfig: mockGovernanceConfig,
+ },
+ args: args{
+ key: "testService:testVersion:testGroup",
+ },
+ want: model.TagRouteDto{
+ Base: model.Base{
+ Application:
"testService:testVersion:testGroup",
+ },
+ Enabled: true,
+ Force: true,
+ Tags: []model.Tag{
+ {
+ Name: "gray",
+ Match: []model.ParamMatch{
+ {
+ Key: "env",
+ Value:
model.StringMatch{
+ Exact:
"gray",
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got, err := tt.s.FindTagRoute(tt.args.key)
+ if (err != nil) != tt.wantErr {
+ t.Errorf("RouteServiceImpl.FindTagRoute() error
= %v, wantErr %v", err, tt.wantErr)
+ return
+ }
+ if !reflect.DeepEqual(got, tt.want) {
+ t.Errorf("RouteServiceImpl.FindTagRoute() =
%+v, want %+v", got, tt.want)
+ }
+ })
+ }
+}
+
+func TestRouteServiceImpl_EnableTagRoute(t *testing.T) {
+ ctrl := gomock.NewController(t)
+ mockGovernanceConfig := config.NewMockGovernanceConfig(ctrl)
+ mockGovernanceConfig.EXPECT().SetConfig(gomock.Any(),
gomock.Any()).Return(nil)
+
mockGovernanceConfig.EXPECT().GetConfig(getRoutePath(util.BuildServiceKey(model.Base{
+ Application: "",
+ Service: "testService",
+ ServiceVersion: "testVersion",
+ ServiceGroup: "testGroup",
+ }),
constant.TagRoute)).Return(`{"enabled":true,"force":true,"key":"testService:testVersion:testGroup","tags":[{"name":"gray","match":[{"key":"env","value":{"exact":"gray"}}]}]}`,
nil)
+
+ type args struct {
+ key string
+ }
+ tests := []struct {
+ name string
+ s RouteService
+ args args
+ wantErr bool
+ }{
+ {
+ name: "test enable tag route",
+ s: &RouteServiceImpl{
+ GovernanceConfig: mockGovernanceConfig,
+ },
+ args: args{
+ key: "testService:testVersion:testGroup",
+ },
+ wantErr: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if err := tt.s.EnableTagRoute(tt.args.key); (err !=
nil) != tt.wantErr {
+ t.Errorf("RouteServiceImpl.EnableTagRoute()
error = %v, wantErr %v", err, tt.wantErr)
+ }
+ })
+ }
+}
+
+func TestRouteServiceImpl_DisableTagRoute(t *testing.T) {
+ ctrl := gomock.NewController(t)
+ mockGovernanceConfig := config.NewMockGovernanceConfig(ctrl)
+ mockGovernanceConfig.EXPECT().SetConfig(gomock.Any(),
gomock.Any()).Return(nil)
+
mockGovernanceConfig.EXPECT().GetConfig(getRoutePath(util.BuildServiceKey(model.Base{
+ Application: "",
+ Service: "testService",
+ ServiceVersion: "testVersion",
+ ServiceGroup: "testGroup",
+ }),
constant.TagRoute)).Return(`{"enabled":false,"force":true,"key":"testService:testVersion:testGroup","tags":[{"name":"gray","match":[{"key":"env","value":{"exact":"gray"}}]}]}`,
nil)
+
+ type args struct {
+ key string
+ }
+ tests := []struct {
+ name string
+ s RouteService
+ args args
+ wantErr bool
+ }{
+ {
+ name: "test disable tag route",
+ s: &RouteServiceImpl{
+ GovernanceConfig: mockGovernanceConfig,
+ },
+ args: args{
+ key: "testService:testVersion:testGroup",
+ },
+ wantErr: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if err := tt.s.DisableTagRoute(tt.args.key); (err !=
nil) != tt.wantErr {
+ t.Errorf("RouteServiceImpl.DisableTagRoute()
error = %v, wantErr %v", err, tt.wantErr)
+ }
+ })
+ }
+}
+
+func TestRouteServiceImpl_CreateConditionRoute(t *testing.T) {
+ ctrl := gomock.NewController(t)
+ mockGovernanceConfig := config.NewMockGovernanceConfig(ctrl)
+ mockGovernanceConfig.EXPECT().SetConfig(gomock.Any(),
gomock.Any()).Return(nil)
+
mockGovernanceConfig.EXPECT().GetConfig(getRoutePath(util.BuildServiceKey(model.Base{
+ Application: "",
+ Service: "testService",
+ ServiceVersion: "testVersion",
+ ServiceGroup: "testGroup",
+ }), constant.ConditionRoute)).Return("", nil)
+
+ type args struct {
+ route *model.ConditionRouteDto
+ }
+ tests := []struct {
+ name string
+ s RouteService
+ args args
+ wantErr bool
+ }{
+ {
+ name: "test create condition route",
+ s: &RouteServiceImpl{
+ GovernanceConfig: mockGovernanceConfig,
+ },
+ args: args{
+ route: &model.ConditionRouteDto{
+ Base: model.Base{
+ Application: "",
+ Service: "testService",
+ ServiceGroup: "testGroup",
+ ServiceVersion: "testVersion",
+ },
+ Enabled: true,
+ Force: true,
+ Runtime: true,
+ Conditions: []string{"method=getComment
=> region=Hangzhou"},
+ },
+ },
+ wantErr: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if err := tt.s.CreateConditionRoute(*tt.args.route);
(err != nil) != tt.wantErr {
+
t.Errorf("RouteServiceImpl.CreateConditionRoute() error = %v, wantErr %v", err,
tt.wantErr)
+ }
+ })
+ }
+}
+
+func TestRouteServiceImpl_UpdateConditionRoute(t *testing.T) {
+ ctrl := gomock.NewController(t)
+ mockGovernanceConfig := config.NewMockGovernanceConfig(ctrl)
+ mockGovernanceConfig.EXPECT().SetConfig(gomock.Any(),
gomock.Any()).Return(nil)
+
mockGovernanceConfig.EXPECT().GetConfig(getRoutePath(util.BuildServiceKey(model.Base{
+ Application: "",
+ Service: "testService",
+ ServiceVersion: "testVersion",
+ ServiceGroup: "testGroup",
+ }),
constant.ConditionRoute)).Return(`{"enabled":true,"force":false,"runtime":true,"key":"testService:testVersion:testGroup","conditions":["method=getComment
=> region=Hangzhou"]}`, nil)
+
+ type args struct {
+ route model.ConditionRouteDto
+ }
+ tests := []struct {
+ name string
+ s RouteService
+ args args
+ wantErr bool
+ }{
+ {
+ name: "test update condition route",
+ s: &RouteServiceImpl{
+ GovernanceConfig: mockGovernanceConfig,
+ },
+ args: args{
+ route: model.ConditionRouteDto{
+ Base: model.Base{
+ Application: "",
+ Service: "testService",
+ ServiceGroup: "testGroup",
+ ServiceVersion: "testVersion",
+ },
+ Enabled: true,
+ Force: true,
+ Runtime: true,
+ Conditions: []string{"method=getComment
=> region=Hangzhou"},
+ },
+ },
+ wantErr: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if err := tt.s.UpdateConditionRoute(tt.args.route);
(err != nil) != tt.wantErr {
+
t.Errorf("RouteServiceImpl.UpdateConditionRoute() error = %v, wantErr %v", err,
tt.wantErr)
+ }
+ })
+ }
+}
+
+func TestRouteServiceImpl_DeleteConditionRoute(t *testing.T) {
+ ctrl := gomock.NewController(t)
+ mockGovernanceConfig := config.NewMockGovernanceConfig(ctrl)
+ mockGovernanceConfig.EXPECT().DeleteConfig(gomock.Any()).Return(nil)
+
+ type args struct {
+ key string
+ }
+ tests := []struct {
+ name string
+ s RouteService
+ args args
+ wantErr bool
+ }{
+ {
+ name: "test delete condition route",
+ s: &RouteServiceImpl{
+ GovernanceConfig: mockGovernanceConfig,
+ },
+ args: args{
+ key: "testService:testVersion:testGroup",
+ },
+ wantErr: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if err := tt.s.DeleteConditionRoute(tt.args.key); (err
!= nil) != tt.wantErr {
+
t.Errorf("RouteServiceImpl.DeleteConditionRoute() error = %v, wantErr %v", err,
tt.wantErr)
+ }
+ })
+ }
+}
+
+func TestRouteServiceImpl_FindConditionRouteById(t *testing.T) {
+ ctrl := gomock.NewController(t)
+ mockGovernanceConfig := config.NewMockGovernanceConfig(ctrl)
+
mockGovernanceConfig.EXPECT().GetConfig(getRoutePath(util.BuildServiceKey(model.Base{
+ Application: "",
+ Service: "testService",
+ ServiceVersion: "testVersion",
+ ServiceGroup: "testGroup",
+ }),
constant.ConditionRoute)).Return(`{"enabled":true,"force":true,"runtime":true,"key":"testService:testVersion:testGroup","conditions":["method=getComment
=> region=Hangzhou"]}`, nil)
+
+ type args struct {
+ key string
+ }
+ tests := []struct {
+ name string
+ s RouteService
+ args args
+ want model.ConditionRouteDto
+ wantErr bool
+ }{
+ {
+ name: "test find condition route by id",
+ s: &RouteServiceImpl{
+ GovernanceConfig: mockGovernanceConfig,
+ },
+ args: args{
+ key: "testService:testVersion:testGroup",
+ },
+ want: model.ConditionRouteDto{
+ Base: model.Base{
+ ID:
"testService:testVersion:testGroup",
+ Service:
"testService:testVersion:testGroup",
+ ServiceGroup: "testGroup",
+ ServiceVersion: "testVersion",
+ },
+ Enabled: true,
+ Force: true,
+ Runtime: true,
+ Conditions: []string{"method=getComment =>
region=Hangzhou"},
+ },
+ wantErr: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got, err := tt.s.FindConditionRouteById(tt.args.key)
+ if (err != nil) != tt.wantErr {
+
t.Errorf("RouteServiceImpl.FindConditionRouteById() error = %v, wantErr %v",
err, tt.wantErr)
+ return
+ }
+ if !reflect.DeepEqual(got, tt.want) {
+
t.Errorf("RouteServiceImpl.FindConditionRouteById() = %v\n, want %v", got,
tt.want)
+ }
+ })
+ }
+}
+
+func TestRouteServiceImpl_EnableConditionRoute(t *testing.T) {
+ ctrl := gomock.NewController(t)
+ mockGovernanceConfig := config.NewMockGovernanceConfig(ctrl)
+ mockGovernanceConfig.EXPECT().SetConfig(gomock.Any(),
gomock.Any()).Return(nil)
+
mockGovernanceConfig.EXPECT().GetConfig(getRoutePath(util.BuildServiceKey(model.Base{
+ Application: "",
+ Service: "testService",
+ ServiceVersion: "testVersion",
+ ServiceGroup: "testGroup",
+ }),
constant.ConditionRoute)).Return(`{"enabled":false,"force":true,"runtime":true,"key":"testService:testVersion:testGroup","conditions":["method=getComment
=> region=Hangzhou"]}`, nil)
+
+ type args struct {
+ key string
+ }
+ tests := []struct {
+ name string
+ s RouteService
+ args args
+ wantErr bool
+ }{
+ {
+ name: "test enable condition route",
+ s: &RouteServiceImpl{
+ GovernanceConfig: mockGovernanceConfig,
+ },
+ args: args{
+ key: "testService:testVersion:testGroup",
+ },
+ wantErr: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if err := tt.s.EnableConditionRoute(tt.args.key); (err
!= nil) != tt.wantErr {
+
t.Errorf("RouteServiceImpl.EnableConditionRoute() error = %v, wantErr %v", err,
tt.wantErr)
+ }
+ })
+ }
+}
+
+func TestRouteServiceImpl_DisableConditionRoute(t *testing.T) {
+ ctrl := gomock.NewController(t)
+ mockGovernanceConfig := config.NewMockGovernanceConfig(ctrl)
+ mockGovernanceConfig.EXPECT().SetConfig(gomock.Any(),
gomock.Any()).Return(nil)
+
mockGovernanceConfig.EXPECT().GetConfig(getRoutePath(util.BuildServiceKey(model.Base{
+ Application: "",
+ Service: "testService",
+ ServiceVersion: "testVersion",
+ ServiceGroup: "testGroup",
+ }),
constant.ConditionRoute)).Return(`{"enabled":true,"force":true,"runtime":true,"key":"testService:testVersion:testGroup","conditions":["method=getComment
=> region=Hangzhou"]}`, nil)
+
+ type args struct {
+ key string
+ }
+ tests := []struct {
+ name string
+ s RouteService
+ args args
+ wantErr bool
+ }{
+ {
+ name: "test disable condition route",
+ s: &RouteServiceImpl{
+ GovernanceConfig: mockGovernanceConfig,
+ },
+ args: args{
+ key: "testService:testVersion:testGroup",
+ },
+ wantErr: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if err := tt.s.DisableConditionRoute(tt.args.key); (err
!= nil) != tt.wantErr {
+
t.Errorf("RouteServiceImpl.DisableConditionRoute() error = %v, wantErr %v",
err, tt.wantErr)
+ }
+ })
+ }
+}
+
+func TestRouteServiceImpl_getRoutePath(t *testing.T) {
+ type args struct {
+ key string
+ routeType string
+ }
+ tests := []struct {
+ name string
+ s *RouteServiceImpl
+ args args
+ want string
+ }{
+ {
+ name: "test get route path",
+ s: &RouteServiceImpl{
+ GovernanceConfig: nil,
+ },
+ args: args{
+ key: "testService:testVersion:testGroup",
+ routeType: constant.TagRoute,
+ },
+ want: "testService:testVersion:testGroup" +
constant.TagRuleSuffix,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ if got := getRoutePath(tt.args.key, tt.args.routeType);
got != tt.want {
+ t.Errorf("RouteServiceImpl.getRoutePath() = %v,
want %v", got, tt.want)
+ }
+ })
+ }
+}