From 935260e402929a18d598fbe5f194227b0df0ef30 Mon Sep 17 00:00:00 2001 From: ian-12-NYCU Date: Thu, 27 Jun 2024 09:19:28 +0000 Subject: [PATCH] fix: testing file error --- go.mod | 1 + go.sum | 1 + internal/sbi/api_sanity_test.go | 44 +++++++-- internal/sbi/processor/processor.go | 3 +- internal/sbi/server_mock.go | 139 ++++++++++++++++++++++++++++ 5 files changed, 176 insertions(+), 12 deletions(-) create mode 100644 internal/sbi/server_mock.go diff --git a/go.mod b/go.mod index be2803e..735cbfc 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/free5gc/openapi v1.0.8 github.com/free5gc/util v1.0.6 github.com/gin-gonic/gin v1.9.1 + github.com/golang/mock v1.4.4 github.com/google/uuid v1.3.0 github.com/mitchellh/mapstructure v1.4.3 github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index 6b40f4a..c9d0310 100644 --- a/go.sum +++ b/go.sum @@ -98,6 +98,7 @@ github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFU github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= +github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc= github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= diff --git a/internal/sbi/api_sanity_test.go b/internal/sbi/api_sanity_test.go index 2d3df55..a670538 100644 --- a/internal/sbi/api_sanity_test.go +++ b/internal/sbi/api_sanity_test.go @@ -9,11 +9,13 @@ import ( "testing" "github.com/gin-gonic/gin" + "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" "github.com/free5gc/openapi/models" db "github.com/free5gc/udr/internal/database" "github.com/free5gc/udr/internal/logger" + "github.com/free5gc/udr/internal/sbi/processor" "github.com/free5gc/udr/pkg/factory" util_logger "github.com/free5gc/util/logger" "github.com/free5gc/util/mongoapi" @@ -24,10 +26,32 @@ type testdata struct { supi string } -func setupHttpServer() *gin.Engine { +func setupHttpServer(t *testing.T) *gin.Engine { router := util_logger.NewGinWithLogrus(logger.GinLog) dataRepositoryGroup := router.Group(factory.UdrDrResUriPrefix) - var s Server + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + udr := NewMockUDR(ctrl) + factory.UdrConfig = &factory.Config{ + Configuration: &factory.Configuration{ + DbConnectorType: "mongodb", + Mongodb: &factory.Mongodb{}, + Sbi: &factory.Sbi{ + BindingIPv4: "127.0.0.1", + Port: 8000, + }, + }, + } + udr.EXPECT(). + Config(). + Return(factory.UdrConfig). + AnyTimes() + + processor := processor.NewProcessor(udr) + udr.EXPECT().Processor().Return(processor).AnyTimes() + + s := NewServer(udr, "") dataRepositoryRoutes := s.getDataRepositoryRoutes() AddService(dataRepositoryGroup, dataRepositoryRoutes) return router @@ -45,7 +69,7 @@ func setupMongoDB(t *testing.T) { } func getUri(t *testing.T, baseUri, extUri string) *httptest.ResponseRecorder { - server := setupHttpServer() + server := setupHttpServer(t) reqUri := baseUri + extUri req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, reqUri, nil) require.Nil(t, err) @@ -84,7 +108,7 @@ func getInfluData(supi string) *models.TrafficInfluData { func postPutInfluData(t *testing.T, method string, baseUri, extUri string, influData *models.TrafficInfluData) ( *httptest.ResponseRecorder, []byte, ) { - server := setupHttpServer() + server := setupHttpServer(t) reqUri := baseUri + extUri bjson, err := json.Marshal(influData) require.Nil(t, err) @@ -109,7 +133,7 @@ func putInfluData(t *testing.T, baseUri, extUri string, influData *models.Traffi } func delUri(t *testing.T, baseUri, extUri string) *httptest.ResponseRecorder { - server := setupHttpServer() + server := setupHttpServer(t) reqUri := baseUri + extUri req, err := http.NewRequestWithContext(context.Background(), http.MethodDelete, reqUri, nil) require.Nil(t, err) @@ -119,7 +143,7 @@ func delUri(t *testing.T, baseUri, extUri string) *httptest.ResponseRecorder { } func TestUDR_Root(t *testing.T) { - server := setupHttpServer() + server := setupHttpServer(t) reqUri := factory.UdrDrResUriPrefix + "/" req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, reqUri, nil) @@ -138,7 +162,7 @@ func TestUDR_GetSubs2Notify_GetBeforeCreateingOne(t *testing.T) { t.Skip("skipping testing in short mode") } - server := setupHttpServer() + server := setupHttpServer(t) reqUri := factory.UdrDrResUriPrefix + "/application-data/influenceData/subs-to-notify?dnn=internet" req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, reqUri, nil) @@ -157,7 +181,7 @@ func TestUDR_GetSubs2Notify_CreateThenGet(t *testing.T) { t.Skip("skipping testing in short mode") } - server := setupHttpServer() + server := setupHttpServer(t) baseUri := factory.UdrDrResUriPrefix + "/application-data/influenceData/subs-to-notify" reqUri := baseUri @@ -233,7 +257,7 @@ func TestUDR_InfluData_GetBeforeCreateing(t *testing.T) { t.Skip("skipping testing in short mode") } - server := setupHttpServer() + server := setupHttpServer(t) reqUri := factory.UdrDrResUriPrefix + "/application-data/influenceData" req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, reqUri, nil) @@ -255,7 +279,7 @@ func TestUDR_InfluData_CreateThenGet(t *testing.T) { // PUT, PATCH, DELETE setupMongoDB(t) - server := setupHttpServer() + server := setupHttpServer(t) baseUri := factory.UdrDrResUriPrefix + "/application-data/influenceData" td1 := testdata{"/influenceId0001", "imsi-208930000000001"} td2 := testdata{"/influenceId0002", "imsi-208930000000002"} diff --git a/internal/sbi/processor/processor.go b/internal/sbi/processor/processor.go index 97a44f2..b2babb1 100644 --- a/internal/sbi/processor/processor.go +++ b/internal/sbi/processor/processor.go @@ -3,7 +3,6 @@ package processor import ( "github.com/free5gc/udr/internal/database" "github.com/free5gc/udr/pkg/app" - "github.com/free5gc/udr/pkg/factory" ) type Processor struct { @@ -14,6 +13,6 @@ type Processor struct { func NewProcessor(udr app.App) *Processor { return &Processor{ App: udr, - DbConnector: database.NewDbConnector(factory.UdrConfig.Configuration.DbConnectorType), + DbConnector: database.NewDbConnector(udr.Config().Configuration.DbConnectorType), } } diff --git a/internal/sbi/server_mock.go b/internal/sbi/server_mock.go new file mode 100644 index 0000000..d9a8032 --- /dev/null +++ b/internal/sbi/server_mock.go @@ -0,0 +1,139 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: server.go + +// Package sbi is a generated GoMock package. +package sbi + +import ( + reflect "reflect" + + context "github.com/free5gc/udr/internal/context" + processor "github.com/free5gc/udr/internal/sbi/processor" + factory "github.com/free5gc/udr/pkg/factory" + gomock "github.com/golang/mock/gomock" +) + +// MockUDR is a mock of UDR interface. +type MockUDR struct { + ctrl *gomock.Controller + recorder *MockUDRMockRecorder +} + +// MockUDRMockRecorder is the mock recorder for MockUDR. +type MockUDRMockRecorder struct { + mock *MockUDR +} + +// NewMockUDR creates a new mock instance. +func NewMockUDR(ctrl *gomock.Controller) *MockUDR { + mock := &MockUDR{ctrl: ctrl} + mock.recorder = &MockUDRMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockUDR) EXPECT() *MockUDRMockRecorder { + return m.recorder +} + +// Config mocks base method. +func (m *MockUDR) Config() *factory.Config { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Config") + ret0, _ := ret[0].(*factory.Config) + return ret0 +} + +// Config indicates an expected call of Config. +func (mr *MockUDRMockRecorder) Config() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Config", reflect.TypeOf((*MockUDR)(nil).Config)) +} + +// Context mocks base method. +func (m *MockUDR) Context() *context.UDRContext { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Context") + ret0, _ := ret[0].(*context.UDRContext) + return ret0 +} + +// Context indicates an expected call of Context. +func (mr *MockUDRMockRecorder) Context() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Context", reflect.TypeOf((*MockUDR)(nil).Context)) +} + +// Processor mocks base method. +func (m *MockUDR) Processor() *processor.Processor { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Processor") + ret0, _ := ret[0].(*processor.Processor) + return ret0 +} + +// Processor indicates an expected call of Processor. +func (mr *MockUDRMockRecorder) Processor() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Processor", reflect.TypeOf((*MockUDR)(nil).Processor)) +} + +// SetLogEnable mocks base method. +func (m *MockUDR) SetLogEnable(enable bool) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetLogEnable", enable) +} + +// SetLogEnable indicates an expected call of SetLogEnable. +func (mr *MockUDRMockRecorder) SetLogEnable(enable interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetLogEnable", reflect.TypeOf((*MockUDR)(nil).SetLogEnable), enable) +} + +// SetLogLevel mocks base method. +func (m *MockUDR) SetLogLevel(level string) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetLogLevel", level) +} + +// SetLogLevel indicates an expected call of SetLogLevel. +func (mr *MockUDRMockRecorder) SetLogLevel(level interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetLogLevel", reflect.TypeOf((*MockUDR)(nil).SetLogLevel), level) +} + +// SetReportCaller mocks base method. +func (m *MockUDR) SetReportCaller(reportCaller bool) { + m.ctrl.T.Helper() + m.ctrl.Call(m, "SetReportCaller", reportCaller) +} + +// SetReportCaller indicates an expected call of SetReportCaller. +func (mr *MockUDRMockRecorder) SetReportCaller(reportCaller interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetReportCaller", reflect.TypeOf((*MockUDR)(nil).SetReportCaller), reportCaller) +} + +// Start mocks base method. +func (m *MockUDR) Start() { + m.ctrl.T.Helper() + m.ctrl.Call(m, "Start") +} + +// Start indicates an expected call of Start. +func (mr *MockUDRMockRecorder) Start() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Start", reflect.TypeOf((*MockUDR)(nil).Start)) +} + +// Terminate mocks base method. +func (m *MockUDR) Terminate() { + m.ctrl.T.Helper() + m.ctrl.Call(m, "Terminate") +} + +// Terminate indicates an expected call of Terminate. +func (mr *MockUDRMockRecorder) Terminate() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Terminate", reflect.TypeOf((*MockUDR)(nil).Terminate)) +}