From: ashishj1729 Date: Wed, 24 Sep 2025 11:22:14 +0000 (+0530) Subject: Move Mocks to the separate package "mme_mocks" X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=dd35eeb55b5cbcb114dfb0288f6ca6df2ac266ac;p=aiml-fw%2Fawmf%2Fmodelmgmtservice.git Move Mocks to the separate package "mme_mocks" Moving Mocks of (DbMgrMock, IDBMock) to a separate package "mme_mocks" for better code-readability Issue-id: AIMLFW-261 Change-Id: I3349b5bfc802a26adcb6ce307e711a52680541d3 Signed-off-by: ashishj1729 --- diff --git a/apis_test/mme_mocks/dbMgrMock.go b/apis_test/mme_mocks/dbMgrMock.go new file mode 100644 index 0000000..5309767 --- /dev/null +++ b/apis_test/mme_mocks/dbMgrMock.go @@ -0,0 +1,48 @@ +/* +================================================================================== +Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved. + +Licensed 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 mme_mocks + +import ( + "gerrit.o-ran-sc.org/r/aiml-fw/awmf/modelmgmtservice/core" + "github.com/stretchr/testify/mock" +) + +type DbMgrMock struct { + mock.Mock + core.DBMgr +} + +func (d *DbMgrMock) CreateBucket(bucketName string) (err error) { + args := d.Called(bucketName) + return args.Error(0) +} + +func (d *DbMgrMock) UploadFile(dataBytes []byte, file_name string, bucketName string) error { + args := d.Called() + // If error is passed, return the error + if _, ok := args.Get(0).(error); ok { + return args.Get(0).(error) + } + + return nil +} + +func (d *DbMgrMock) ListBucket(bucketObjPostfix string) ([]core.Bucket, error) { + args := d.Called() + return args.Get(0).([]core.Bucket), args.Error(1) +} diff --git a/apis_test/mme_mocks/iDBMock.go b/apis_test/mme_mocks/iDBMock.go new file mode 100644 index 0000000..618a35c --- /dev/null +++ b/apis_test/mme_mocks/iDBMock.go @@ -0,0 +1,77 @@ +/* +================================================================================== +Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved. + +Licensed 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 mme_mocks + +import ( + "gerrit.o-ran-sc.org/r/aiml-fw/awmf/modelmgmtservice/db" + "gerrit.o-ran-sc.org/r/aiml-fw/awmf/modelmgmtservice/models" + "github.com/stretchr/testify/mock" +) + +type IDBMock struct { + mock.Mock + db.IDB +} + +func (i *IDBMock) Create(modelInfo models.ModelRelatedInformation) error { + args := i.Called(modelInfo) + return args.Error(0) +} + +func (i *IDBMock) GetByID(id string) (*models.ModelRelatedInformation, error) { + return nil, nil +} + +func (i *IDBMock) GetAll() ([]models.ModelRelatedInformation, error) { + args := i.Called() + if _, ok := args.Get(1).(error); !ok { + return args.Get(0).([]models.ModelRelatedInformation), nil + } else { + var emptyModelInfo []models.ModelRelatedInformation + return emptyModelInfo, args.Error(1) + } +} + +func (i *IDBMock) Update(modelInfo models.ModelRelatedInformation) error { + return nil +} + +func (i *IDBMock) Delete(id string) (int64, error) { + return 1, nil +} + +func (i *IDBMock) GetModelInfoByName(modelName string) ([]models.ModelRelatedInformation, error) { + args := i.Called() + if _, ok := args.Get(1).(error); !ok { + return args.Get(0).([]models.ModelRelatedInformation), nil + } else { + var emptyModelInfo []models.ModelRelatedInformation + return emptyModelInfo, args.Error(1) + } +} + +func (i *IDBMock) GetModelInfoByNameAndVer(modelName string, modelVersion string) (*models.ModelRelatedInformation, error) { + args := i.Called() + + if _, ok := args.Get(1).(error); !ok { + return args.Get(0).(*models.ModelRelatedInformation), nil + } else { + var emptyModelInfo *models.ModelRelatedInformation + return emptyModelInfo, args.Error(1) + } +} diff --git a/apis_test/mmes_apis_test.go b/apis_test/mmes_apis_test.go index a098af4..104bc9b 100644 --- a/apis_test/mmes_apis_test.go +++ b/apis_test/mmes_apis_test.go @@ -28,8 +28,7 @@ import ( "testing" "gerrit.o-ran-sc.org/r/aiml-fw/awmf/modelmgmtservice/apis" - "gerrit.o-ran-sc.org/r/aiml-fw/awmf/modelmgmtservice/core" - "gerrit.o-ran-sc.org/r/aiml-fw/awmf/modelmgmtservice/db" + "gerrit.o-ran-sc.org/r/aiml-fw/awmf/modelmgmtservice/apis_test/mme_mocks" "gerrit.o-ran-sc.org/r/aiml-fw/awmf/modelmgmtservice/logging" "gerrit.o-ran-sc.org/r/aiml-fw/awmf/modelmgmtservice/models" "gerrit.o-ran-sc.org/r/aiml-fw/awmf/modelmgmtservice/routers" @@ -87,76 +86,9 @@ var invalidRegisterModelBody2 = `{ } }` -type dbMgrMock struct { - mock.Mock - core.DBMgr -} - -func (d *dbMgrMock) CreateBucket(bucketName string) (err error) { - args := d.Called(bucketName) - return args.Error(0) -} - -func (d *dbMgrMock) UploadFile(dataBytes []byte, file_name string, bucketName string) { -} - -func (d *dbMgrMock) ListBucket(bucketObjPostfix string) ([]core.Bucket, error) { - args := d.Called() - return args.Get(0).([]core.Bucket), args.Error(1) -} - -type iDBMock struct { - mock.Mock - db.IDB -} - -func (i *iDBMock) Create(modelInfo models.ModelRelatedInformation) error { - args := i.Called(modelInfo) - return args.Error(0) -} -func (i *iDBMock) GetByID(id string) (*models.ModelRelatedInformation, error) { - return nil, nil -} -func (i *iDBMock) GetAll() ([]models.ModelRelatedInformation, error) { - args := i.Called() - if _, ok := args.Get(1).(error); !ok { - return args.Get(0).([]models.ModelRelatedInformation), nil - } else { - var emptyModelInfo []models.ModelRelatedInformation - return emptyModelInfo, args.Error(1) - } -} -func (i *iDBMock) Update(modelInfo models.ModelRelatedInformation) error { - return nil -} -func (i *iDBMock) Delete(id string) (int64, error) { - return 1, nil -} - -func (i *iDBMock) GetModelInfoByName(modelName string) ([]models.ModelRelatedInformation, error) { - args := i.Called() - if _, ok := args.Get(1).(error); !ok { - return args.Get(0).([]models.ModelRelatedInformation), nil - } else { - var emptyModelInfo []models.ModelRelatedInformation - return emptyModelInfo, args.Error(1) - } -} - -func (i *iDBMock) GetModelInfoByNameAndVer(modelName string, modelVersion string) (*models.ModelRelatedInformation, error) { - args := i.Called() - - if _, ok := args.Get(1).(error); !ok { - return args.Get(0).(*models.ModelRelatedInformation), nil - } else { - var emptyModelInfo *models.ModelRelatedInformation - return emptyModelInfo, args.Error(1) - } -} - func TestRegisterModel(t *testing.T) { os.Setenv("LOG_FILE_NAME", "testing") - iDBMockInst := new(iDBMock) + iDBMockInst := new(mme_mocks.IDBMock) iDBMockInst.On("Create", mock.Anything).Return(nil) handler := apis.NewMmeApiHandler(nil, iDBMockInst) router := routers.InitRouter(handler) @@ -168,7 +100,7 @@ func TestRegisterModel(t *testing.T) { func TestRegisterModelFailInvalidJson(t *testing.T) { os.Setenv("LOG_FILE_NAME", "testing") - iDBMockInst := new(iDBMock) + iDBMockInst := new(mme_mocks.IDBMock) handler := apis.NewMmeApiHandler(nil, iDBMockInst) router := routers.InitRouter(handler) w := httptest.NewRecorder() @@ -183,7 +115,7 @@ func TestRegisterModelFailInvalidJson(t *testing.T) { func TestRegisterModelFailInvalidRequest(t *testing.T) { os.Setenv("LOG_FILE_NAME", "testing") - iDBMockInst := new(iDBMock) + iDBMockInst := new(mme_mocks.IDBMock) handler := apis.NewMmeApiHandler(nil, iDBMockInst) router := routers.InitRouter(handler) w := httptest.NewRecorder() @@ -198,7 +130,7 @@ func TestRegisterModelFailInvalidRequest(t *testing.T) { func TestRegisterModelFailCreateDuplicateModel(t *testing.T) { os.Setenv("LOG_FILE_NAME", "testing") - iDBMockInst := new(iDBMock) + iDBMockInst := new(mme_mocks.IDBMock) iDBMockInst.On("Create", mock.Anything).Return(&pq.Error{Code: pgerrcode.UniqueViolation}) handler := apis.NewMmeApiHandler(nil, iDBMockInst) router := routers.InitRouter(handler) @@ -214,7 +146,7 @@ func TestRegisterModelFailCreateDuplicateModel(t *testing.T) { func TestRegisterModelFailCreate(t *testing.T) { os.Setenv("LOG_FILE_NAME", "testing") - iDBMockInst := new(iDBMock) + iDBMockInst := new(mme_mocks.IDBMock) iDBMockInst.On("Create", mock.Anything).Return(&pq.Error{Code: pgerrcode.SQLClientUnableToEstablishSQLConnection}) handler := apis.NewMmeApiHandler(nil, iDBMockInst) router := routers.InitRouter(handler) @@ -231,7 +163,7 @@ func TestRegisterModelFailCreate(t *testing.T) { func TestWhenSuccessGetModelInfoList(t *testing.T) { os.Setenv("LOG_FILE_NAME", "testing") - iDBmockInst := new(iDBMock) + iDBmockInst := new(mme_mocks.IDBMock) iDBmockInst.On("GetAll").Return([]models.ModelRelatedInformation{ { Id: "1234", @@ -271,7 +203,7 @@ func TestWhenSuccessGetModelInfoList(t *testing.T) { func TestWhenFailGetModelInfoList(t *testing.T) { os.Setenv("LOG_FILE_NAME", "testing") - iDBmockInst2 := new(iDBMock) + iDBmockInst2 := new(mme_mocks.IDBMock) iDBmockInst2.On("GetAll").Return([]models.ModelRelatedInformation{}, fmt.Errorf("db not available")) handler := apis.NewMmeApiHandler(nil, iDBmockInst2) @@ -293,7 +225,7 @@ func TestWhenFailGetModelInfoList(t *testing.T) { func TestGetModelInfoParamsInvalid(t *testing.T) { os.Setenv("LOG_FILE_NAME", "testing") - iDBMockInst := new(iDBMock) + iDBMockInst := new(mme_mocks.IDBMock) handler := apis.NewMmeApiHandler(nil, iDBMockInst) router := routers.InitRouter(handler) responseRecorder := httptest.NewRecorder() @@ -311,7 +243,7 @@ func TestGetModelInfoParamsInvalid(t *testing.T) { func TestGetModelInfoByNameSuccess(t *testing.T) { os.Setenv("LOG_FILE_NAME", "testing") - iDBMockInst := new(iDBMock) + iDBMockInst := new(mme_mocks.IDBMock) iDBMockInst.On("GetModelInfoByName").Return([]models.ModelRelatedInformation{ { Id: "1234", @@ -351,7 +283,7 @@ func TestGetModelInfoByNameSuccess(t *testing.T) { func TestGetModelInfoByNameFail(t *testing.T) { os.Setenv("LOG_FILE_NAME", "testing") - iDBMockInst := new(iDBMock) + iDBMockInst := new(mme_mocks.IDBMock) iDBMockInst.On("GetModelInfoByName").Return([]models.ModelRelatedInformation{}, fmt.Errorf("db not available")) handler := apis.NewMmeApiHandler(nil, iDBMockInst) @@ -370,7 +302,7 @@ func TestGetModelInfoByNameFail(t *testing.T) { func TestGetModelInfoByNameAndVersionSuccess(t *testing.T) { os.Setenv("LOG_FILE_NAME", "testing") - iDBMockInst := new(iDBMock) + iDBMockInst := new(mme_mocks.IDBMock) modelInfo := models.ModelRelatedInformation{ Id: "1234", @@ -411,7 +343,7 @@ func TestGetModelInfoByNameAndVersionSuccess(t *testing.T) { func TestGetModelInfoByNameAndVersionFail(t *testing.T) { os.Setenv("LOG_FILE_NAME", "testing") - iDBMockInst := new(iDBMock) + iDBMockInst := new(mme_mocks.IDBMock) modelInfo := models.ModelRelatedInformation{} iDBMockInst.On("GetModelInfoByNameAndVer").Return(&modelInfo, fmt.Errorf("db not available"))