2020-09-22 18:18:31 +08:00
package handler
import (
"testing"
"github.com/go-playground/assert/v2"
"github.com/golang/mock/gomock"
"github.com/goodrain/rainbond/api/model"
"github.com/goodrain/rainbond/db"
daomock "github.com/goodrain/rainbond/db/dao"
dbmodel "github.com/goodrain/rainbond/db/model"
2020-09-23 11:08:01 +08:00
"github.com/pkg/errors"
2020-09-22 18:18:31 +08:00
)
2020-09-23 11:08:01 +08:00
func TestAddAppConfigGroup ( t * testing . T ) {
tests := [ ] struct {
name string
appID string
request * model . ApplicationConfigGroup
mockFunc func ( manager * db . MockManager , ctrl * gomock . Controller )
wanterr bool
} {
{
name : "add config group success" ,
appID : "appID1" ,
request : & model . ApplicationConfigGroup {
ConfigGroupName : "configName1" ,
DeployType : "env" ,
ServiceIDs : [ ] string { "sid1" } ,
ConfigItems : [ ] model . ConfigItem {
{ ItemKey : "key1" , ItemValue : "value1" } ,
{ ItemKey : "key2" , ItemValue : "value2" } ,
} ,
} ,
mockFunc : func ( manager * db . MockManager , ctrl * gomock . Controller ) {
2020-09-23 15:57:19 +08:00
serviceResult := [ ] * dbmodel . TenantServices {
{ ServiceID : "sid1" , ServiceAlias : "sid1_name" } ,
2020-09-23 11:08:01 +08:00
}
config := & dbmodel . ApplicationConfigGroup {
AppID : "appID1" ,
ConfigGroupName : "configName1" ,
DeployType : "env" ,
}
2020-09-23 14:48:53 +08:00
tenantServiceDao := daomock . NewMockTenantServiceDao ( ctrl )
2020-09-23 15:57:19 +08:00
tenantServiceDao . EXPECT ( ) . GetServicesByServiceIDs ( gomock . Any ( ) ) . Return ( serviceResult , nil )
2020-09-23 14:48:53 +08:00
manager . EXPECT ( ) . TenantServiceDao ( ) . Return ( tenantServiceDao )
2020-09-23 15:30:31 +08:00
serviceConfigGroupDao := daomock . NewMockAppConfigGroupServiceDao ( ctrl )
2020-09-23 11:08:01 +08:00
serviceConfigGroupDao . EXPECT ( ) . AddModel ( gomock . Any ( ) ) . Return ( nil ) . AnyTimes ( )
2020-09-23 15:30:31 +08:00
manager . EXPECT ( ) . AppConfigGroupServiceDao ( ) . Return ( serviceConfigGroupDao ) . AnyTimes ( )
2020-09-22 18:18:31 +08:00
2020-09-23 15:30:31 +08:00
configItemDao := daomock . NewMockAppConfigGroupItemDao ( ctrl )
2020-09-23 11:08:01 +08:00
configItemDao . EXPECT ( ) . AddModel ( gomock . Any ( ) ) . Return ( nil ) . AnyTimes ( )
2020-09-23 15:30:31 +08:00
manager . EXPECT ( ) . AppConfigGroupItemDao ( ) . Return ( configItemDao ) . AnyTimes ( )
2020-09-22 18:18:31 +08:00
2020-09-23 15:30:31 +08:00
applicationConfigDao := daomock . NewMockAppConfigGroupDao ( ctrl )
2020-09-23 11:08:01 +08:00
applicationConfigDao . EXPECT ( ) . AddModel ( gomock . Any ( ) ) . Return ( nil )
2020-09-23 21:41:13 +08:00
applicationConfigDao . EXPECT ( ) . GetConfigGroupByID ( gomock . Any ( ) , gomock . Any ( ) ) . Return ( config , nil )
2020-09-23 15:30:31 +08:00
manager . EXPECT ( ) . AppConfigGroupDao ( ) . Return ( applicationConfigDao ) . AnyTimes ( )
2020-09-23 11:08:01 +08:00
} ,
wanterr : false ,
} ,
{
name : "add config group service failed" ,
appID : "appID1" ,
request : & model . ApplicationConfigGroup {
ConfigGroupName : "configName1" ,
DeployType : "env" ,
ServiceIDs : [ ] string { "sid1" } ,
ConfigItems : [ ] model . ConfigItem {
{ ItemKey : "key1" , ItemValue : "value1" } ,
{ ItemKey : "key2" , ItemValue : "value2" } ,
} ,
} ,
mockFunc : func ( manager * db . MockManager , ctrl * gomock . Controller ) {
2020-09-23 15:57:19 +08:00
serviceResult := [ ] * dbmodel . TenantServices {
{ ServiceID : "sid1" , ServiceAlias : "sid1_name" } ,
2020-09-23 14:48:53 +08:00
}
tenantServiceDao := daomock . NewMockTenantServiceDao ( ctrl )
2020-09-23 15:57:19 +08:00
tenantServiceDao . EXPECT ( ) . GetServicesByServiceIDs ( gomock . Any ( ) ) . Return ( serviceResult , nil )
2020-09-23 14:48:53 +08:00
manager . EXPECT ( ) . TenantServiceDao ( ) . Return ( tenantServiceDao )
2020-09-23 15:30:31 +08:00
serviceConfigGroupDao := daomock . NewMockAppConfigGroupServiceDao ( ctrl )
2020-09-23 11:08:01 +08:00
serviceConfigGroupDao . EXPECT ( ) . AddModel ( gomock . Any ( ) ) . Return ( errors . New ( "add service config failed" ) ) . AnyTimes ( )
2020-09-23 15:30:31 +08:00
manager . EXPECT ( ) . AppConfigGroupServiceDao ( ) . Return ( serviceConfigGroupDao ) . AnyTimes ( )
2020-09-23 11:08:01 +08:00
} ,
wanterr : true ,
} ,
{
name : "add config item failed" ,
appID : "appID1" ,
request : & model . ApplicationConfigGroup {
ConfigGroupName : "configName1" ,
DeployType : "env" ,
ServiceIDs : [ ] string { "sid1" } ,
ConfigItems : [ ] model . ConfigItem {
{ ItemKey : "key1" , ItemValue : "value1" } ,
{ ItemKey : "key2" , ItemValue : "value2" } ,
} ,
} ,
mockFunc : func ( manager * db . MockManager , ctrl * gomock . Controller ) {
2020-09-23 15:57:19 +08:00
serviceResult := [ ] * dbmodel . TenantServices {
{ ServiceID : "sid1" , ServiceAlias : "sid1_name" } ,
2020-09-23 14:48:53 +08:00
}
tenantServiceDao := daomock . NewMockTenantServiceDao ( ctrl )
2020-09-23 15:57:19 +08:00
tenantServiceDao . EXPECT ( ) . GetServicesByServiceIDs ( gomock . Any ( ) ) . Return ( serviceResult , nil )
2020-09-23 14:48:53 +08:00
manager . EXPECT ( ) . TenantServiceDao ( ) . Return ( tenantServiceDao )
2020-09-23 15:30:31 +08:00
serviceConfigGroupDao := daomock . NewMockAppConfigGroupServiceDao ( ctrl )
2020-09-23 11:08:01 +08:00
serviceConfigGroupDao . EXPECT ( ) . AddModel ( gomock . Any ( ) ) . Return ( nil ) . AnyTimes ( )
2020-09-23 15:30:31 +08:00
manager . EXPECT ( ) . AppConfigGroupServiceDao ( ) . Return ( serviceConfigGroupDao ) . AnyTimes ( )
2020-09-22 18:18:31 +08:00
2020-09-23 15:30:31 +08:00
configItemDao := daomock . NewMockAppConfigGroupItemDao ( ctrl )
2020-09-23 11:08:01 +08:00
configItemDao . EXPECT ( ) . AddModel ( gomock . Any ( ) ) . Return ( errors . New ( "add config item failed" ) ) . AnyTimes ( )
2020-09-23 15:30:31 +08:00
manager . EXPECT ( ) . AppConfigGroupItemDao ( ) . Return ( configItemDao ) . AnyTimes ( )
2020-09-23 11:08:01 +08:00
} ,
wanterr : true ,
} ,
{
name : "add application config group failed" ,
appID : "appID1" ,
request : & model . ApplicationConfigGroup {
ConfigGroupName : "configName1" ,
DeployType : "env" ,
ServiceIDs : [ ] string { "sid1" } ,
ConfigItems : [ ] model . ConfigItem {
{ ItemKey : "key1" , ItemValue : "value1" } ,
{ ItemKey : "key2" , ItemValue : "value2" } ,
} ,
} ,
mockFunc : func ( manager * db . MockManager , ctrl * gomock . Controller ) {
2020-09-23 15:57:19 +08:00
serviceResult := [ ] * dbmodel . TenantServices {
{ ServiceID : "sid1" , ServiceAlias : "sid1_name" } ,
2020-09-23 14:48:53 +08:00
}
tenantServiceDao := daomock . NewMockTenantServiceDao ( ctrl )
2020-09-23 15:57:19 +08:00
tenantServiceDao . EXPECT ( ) . GetServicesByServiceIDs ( gomock . Any ( ) ) . Return ( serviceResult , nil )
2020-09-23 14:48:53 +08:00
manager . EXPECT ( ) . TenantServiceDao ( ) . Return ( tenantServiceDao )
2020-09-23 15:30:31 +08:00
serviceConfigGroupDao := daomock . NewMockAppConfigGroupServiceDao ( ctrl )
2020-09-23 11:08:01 +08:00
serviceConfigGroupDao . EXPECT ( ) . AddModel ( gomock . Any ( ) ) . Return ( nil ) . AnyTimes ( )
2020-09-23 15:30:31 +08:00
manager . EXPECT ( ) . AppConfigGroupServiceDao ( ) . Return ( serviceConfigGroupDao ) . AnyTimes ( )
2020-09-22 18:18:31 +08:00
2020-09-23 15:30:31 +08:00
configItemDao := daomock . NewMockAppConfigGroupItemDao ( ctrl )
2020-09-23 11:08:01 +08:00
configItemDao . EXPECT ( ) . AddModel ( gomock . Any ( ) ) . Return ( nil ) . AnyTimes ( )
2020-09-23 15:30:31 +08:00
manager . EXPECT ( ) . AppConfigGroupItemDao ( ) . Return ( configItemDao ) . AnyTimes ( )
2020-09-22 18:18:31 +08:00
2020-09-23 15:30:31 +08:00
applicationConfigDao := daomock . NewMockAppConfigGroupDao ( ctrl )
2020-09-23 11:08:01 +08:00
applicationConfigDao . EXPECT ( ) . AddModel ( gomock . Any ( ) ) . Return ( errors . New ( "add application config group failed" ) )
2020-09-23 15:30:31 +08:00
manager . EXPECT ( ) . AppConfigGroupDao ( ) . Return ( applicationConfigDao ) . AnyTimes ( )
2020-09-23 11:08:01 +08:00
} ,
wanterr : true ,
} ,
}
for i := range tests {
tc := tests [ i ]
t . Run ( tc . name , func ( t * testing . T ) {
ctrl := gomock . NewController ( t )
defer ctrl . Finish ( )
2020-09-22 18:18:31 +08:00
2020-09-23 11:08:01 +08:00
manager := db . NewMockManager ( ctrl )
db . SetTestManager ( manager )
tc . mockFunc ( manager , ctrl )
2020-09-22 18:18:31 +08:00
2020-09-28 16:43:27 +08:00
appAction := ApplicationAction { }
2020-09-23 11:08:01 +08:00
resp , err := appAction . AddConfigGroup ( tc . appID , tc . request )
if ( err != nil ) != tc . wanterr {
t . Errorf ( "Unexpected error = %v, wantErr %v" , err , tc . wanterr )
return
}
if resp != nil {
assert . Equal ( t , resp . AppID , tc . appID )
assert . Equal ( t , resp . DeployType , tc . request . DeployType )
}
} )
2020-09-22 18:18:31 +08:00
}
2020-09-23 11:08:01 +08:00
2020-09-22 18:18:31 +08:00
}
2020-09-24 14:30:34 +08:00
func TestListConfigGroups ( t * testing . T ) {
tests := [ ] struct {
name string
appID string
request * model . ApplicationConfigGroup
mockFunc func ( manager * db . MockManager , ctrl * gomock . Controller )
wanterr bool
} {
{
name : "list config group success" ,
appID : "appID1" ,
request : & model . ApplicationConfigGroup {
ConfigGroupName : "configName1" ,
DeployType : "env" ,
ServiceIDs : [ ] string { "sid1" } ,
ConfigItems : [ ] model . ConfigItem {
{ ItemKey : "key1" , ItemValue : "value1" } ,
{ ItemKey : "key2" , ItemValue : "value2" } ,
} ,
} ,
mockFunc : func ( manager * db . MockManager , ctrl * gomock . Controller ) {
2020-11-13 09:42:17 +08:00
configGroupsServiceResult := [ ] * dbmodel . ConfigGroupService {
2020-09-24 14:30:34 +08:00
{ ServiceID : "sid1" , ServiceAlias : "sid1_name" } ,
}
configGroupsResult := [ ] * dbmodel . ApplicationConfigGroup {
{ AppID : "appID1" , ConfigGroupName : "configName1" , DeployType : "env" } ,
}
2020-10-08 16:10:03 +08:00
configGroupItemResult := [ ] * dbmodel . ConfigGroupItem {
2020-09-24 14:30:34 +08:00
{ ItemKey : "itemKey1" , ItemValue : "itemValue1" } ,
}
applicationConfigDao := daomock . NewMockAppConfigGroupDao ( ctrl )
applicationConfigDao . EXPECT ( ) . GetConfigGroupsByAppID ( gomock . Any ( ) , gomock . Any ( ) , gomock . Any ( ) ) . Return ( configGroupsResult , int64 ( 1 ) , nil )
manager . EXPECT ( ) . AppConfigGroupDao ( ) . Return ( applicationConfigDao ) . AnyTimes ( )
serviceConfigGroupDao := daomock . NewMockAppConfigGroupServiceDao ( ctrl )
serviceConfigGroupDao . EXPECT ( ) . GetConfigGroupServicesByID ( gomock . Any ( ) , gomock . Any ( ) ) . Return ( configGroupsServiceResult , nil ) . AnyTimes ( )
manager . EXPECT ( ) . AppConfigGroupServiceDao ( ) . Return ( serviceConfigGroupDao ) . AnyTimes ( )
configItemDao := daomock . NewMockAppConfigGroupItemDao ( ctrl )
configItemDao . EXPECT ( ) . GetConfigGroupItemsByID ( gomock . Any ( ) , gomock . Any ( ) ) . Return ( configGroupItemResult , nil ) . AnyTimes ( )
manager . EXPECT ( ) . AppConfigGroupItemDao ( ) . Return ( configItemDao ) . AnyTimes ( )
} ,
wanterr : false ,
} ,
{
name : "list config group failed because get config group error" ,
appID : "appID1" ,
mockFunc : func ( manager * db . MockManager , ctrl * gomock . Controller ) {
applicationConfigDao := daomock . NewMockAppConfigGroupDao ( ctrl )
applicationConfigDao . EXPECT ( ) . GetConfigGroupsByAppID ( gomock . Any ( ) , gomock . Any ( ) , gomock . Any ( ) ) . Return ( nil , int64 ( 0 ) , errors . New ( "get config group error" ) )
manager . EXPECT ( ) . AppConfigGroupDao ( ) . Return ( applicationConfigDao ) . AnyTimes ( )
} ,
wanterr : true ,
} ,
{
name : "list config group failed because get config group service error" ,
appID : "appID1" ,
mockFunc : func ( manager * db . MockManager , ctrl * gomock . Controller ) {
configGroupsResult := [ ] * dbmodel . ApplicationConfigGroup {
{ AppID : "appID1" , ConfigGroupName : "configName1" , DeployType : "env" } ,
}
applicationConfigDao := daomock . NewMockAppConfigGroupDao ( ctrl )
applicationConfigDao . EXPECT ( ) . GetConfigGroupsByAppID ( gomock . Any ( ) , gomock . Any ( ) , gomock . Any ( ) ) . Return ( configGroupsResult , int64 ( 1 ) , nil )
manager . EXPECT ( ) . AppConfigGroupDao ( ) . Return ( applicationConfigDao ) . AnyTimes ( )
serviceConfigGroupDao := daomock . NewMockAppConfigGroupServiceDao ( ctrl )
serviceConfigGroupDao . EXPECT ( ) . GetConfigGroupServicesByID ( gomock . Any ( ) , gomock . Any ( ) ) . Return ( nil , errors . New ( "get config group service error" ) ) . AnyTimes ( )
manager . EXPECT ( ) . AppConfigGroupServiceDao ( ) . Return ( serviceConfigGroupDao ) . AnyTimes ( )
} ,
wanterr : true ,
} ,
{
name : "list config group failed because get config group item error" ,
appID : "appID1" ,
mockFunc : func ( manager * db . MockManager , ctrl * gomock . Controller ) {
2020-11-13 09:42:17 +08:00
configGroupsServiceResult := [ ] * dbmodel . ConfigGroupService {
2020-09-24 14:30:34 +08:00
{ ServiceID : "sid1" , ServiceAlias : "sid1_name" } ,
}
configGroupsResult := [ ] * dbmodel . ApplicationConfigGroup {
{ AppID : "appID1" , ConfigGroupName : "configName1" , DeployType : "env" } ,
}
applicationConfigDao := daomock . NewMockAppConfigGroupDao ( ctrl )
applicationConfigDao . EXPECT ( ) . GetConfigGroupsByAppID ( gomock . Any ( ) , gomock . Any ( ) , gomock . Any ( ) ) . Return ( configGroupsResult , int64 ( 1 ) , nil )
manager . EXPECT ( ) . AppConfigGroupDao ( ) . Return ( applicationConfigDao ) . AnyTimes ( )
serviceConfigGroupDao := daomock . NewMockAppConfigGroupServiceDao ( ctrl )
serviceConfigGroupDao . EXPECT ( ) . GetConfigGroupServicesByID ( gomock . Any ( ) , gomock . Any ( ) ) . Return ( configGroupsServiceResult , nil ) . AnyTimes ( )
manager . EXPECT ( ) . AppConfigGroupServiceDao ( ) . Return ( serviceConfigGroupDao ) . AnyTimes ( )
configItemDao := daomock . NewMockAppConfigGroupItemDao ( ctrl )
configItemDao . EXPECT ( ) . GetConfigGroupItemsByID ( gomock . Any ( ) , gomock . Any ( ) ) . Return ( nil , errors . New ( "get config group item error" ) ) . AnyTimes ( )
manager . EXPECT ( ) . AppConfigGroupItemDao ( ) . Return ( configItemDao ) . AnyTimes ( )
} ,
wanterr : true ,
} ,
}
for i := range tests {
tc := tests [ i ]
t . Run ( tc . name , func ( t * testing . T ) {
ctrl := gomock . NewController ( t )
defer ctrl . Finish ( )
manager := db . NewMockManager ( ctrl )
db . SetTestManager ( manager )
tc . mockFunc ( manager , ctrl )
2021-02-05 15:54:55 +08:00
appAction := NewApplicationHandler ( nil , nil )
2020-09-24 14:30:34 +08:00
resp , err := appAction . ListConfigGroups ( tc . appID , 1 , 10 )
if ( err != nil ) != tc . wanterr {
t . Errorf ( "Unexpected error = %v, wantErr %v" , err , tc . wanterr )
return
}
if resp != nil {
for _ , r := range resp . ConfigGroup {
assert . Equal ( t , r . AppID , tc . appID )
assert . Equal ( t , r . ConfigGroupName , tc . request . ConfigGroupName )
assert . Equal ( t , r . DeployType , tc . request . DeployType )
got := false
for _ , s := range r . Services {
if s . ServiceID == tc . request . ServiceIDs [ 0 ] {
got = true
}
}
if got != true {
t . Errorf ( "get config group service error,serviceID not exists" )
}
}
}
} )
}
}