diff --git a/internal/app/service.go b/internal/app/service.go index 59c1ede01..fd72f7488 100644 --- a/internal/app/service.go +++ b/internal/app/service.go @@ -323,6 +323,20 @@ func (svc *Service) ApplicationSettings() map[string]string { return svc.config.ApplicationSettings } +// GetAppSettingStrings returns the string for the specified App Setting. +func (svc *Service) GetAppSetting(setting string) (string, error) { + if svc.config.ApplicationSettings == nil { + return "", fmt.Errorf("%s setting not found: ApplicationSettings section is missing", setting) + } + + settingValue, ok := svc.config.ApplicationSettings[setting] + if !ok { + return "", fmt.Errorf("%s setting not found in ApplicationSettings section", setting) + } + + return settingValue, nil +} + // GetAppSettingStrings returns the strings slice for the specified App Setting. func (svc *Service) GetAppSettingStrings(setting string) ([]string, error) { if svc.config.ApplicationSettings == nil { @@ -331,7 +345,7 @@ func (svc *Service) GetAppSettingStrings(setting string) ([]string, error) { settingValue, ok := svc.config.ApplicationSettings[setting] if !ok { - return nil, fmt.Errorf("%s setting not found in ApplicationSettings", setting) + return nil, fmt.Errorf("%s setting not found in ApplicationSettings section", setting) } valueStrings := util.DeleteEmptyAndTrim(strings.FieldsFunc(settingValue, util.SplitComma)) diff --git a/internal/app/service_test.go b/internal/app/service_test.go index e1ad9d310..b3b38f974 100644 --- a/internal/app/service_test.go +++ b/internal/app/service_test.go @@ -195,6 +195,33 @@ func TestApplicationSettingsNil(t *testing.T) { require.Nil(t, appSettings, "returned application settings expected to be nil") } +func TestGetAppSetting(t *testing.T) { + goodSettingName := "ExportUrl" + expectedGoodValue := "http:/somewhere.com" + badSettingName := "DeviceName" + + svc := Service{ + config: &common.ConfigurationStruct{ + ApplicationSettings: map[string]string{ + goodSettingName: expectedGoodValue, + }, + }, + } + + actual, err := svc.GetAppSetting(goodSettingName) + require.NoError(t, err) + assert.EqualValues(t, expectedGoodValue, actual, "actual application setting values not as expected") + + _, err = svc.GetAppSetting(badSettingName) + require.Error(t, err) + assert.EqualError(t, err, fmt.Sprintf("%s setting not found in ApplicationSettings section", badSettingName)) + + svc.config.ApplicationSettings = nil + _, err = svc.GetAppSetting(goodSettingName) + require.Error(t, err) + assert.EqualError(t, err, fmt.Sprintf("%s setting not found: ApplicationSettings section is missing", goodSettingName)) +} + func TestGetAppSettingStrings(t *testing.T) { setting := "DeviceNames" expected := []string{"dev1", "dev2"} diff --git a/pkg/interfaces/mocks/AppFunctionContext.go b/pkg/interfaces/mocks/AppFunctionContext.go index 646c33e70..8a9eaae1b 100644 --- a/pkg/interfaces/mocks/AppFunctionContext.go +++ b/pkg/interfaces/mocks/AppFunctionContext.go @@ -181,6 +181,22 @@ func (_m *AppFunctionContext) ResponseContentType() string { return r0 } +// ResponseData provides a mock function with given fields: +func (_m *AppFunctionContext) ResponseData() []byte { + ret := _m.Called() + + var r0 []byte + if rf, ok := ret.Get(0).(func() []byte); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]byte) + } + } + + return r0 +} + // SecretsLastUpdated provides a mock function with given fields: func (_m *AppFunctionContext) SecretsLastUpdated() time.Time { ret := _m.Called() diff --git a/pkg/interfaces/mocks/ApplicationService.go b/pkg/interfaces/mocks/ApplicationService.go index 62c654dc2..e83db142f 100644 --- a/pkg/interfaces/mocks/ApplicationService.go +++ b/pkg/interfaces/mocks/ApplicationService.go @@ -109,6 +109,27 @@ func (_m *ApplicationService) EventClient() coredata.EventClient { return r0 } +// GetAppSetting provides a mock function with given fields: setting +func (_m *ApplicationService) GetAppSetting(setting string) (string, error) { + ret := _m.Called(setting) + + var r0 string + if rf, ok := ret.Get(0).(func(string) string); ok { + r0 = rf(setting) + } else { + r0 = ret.Get(0).(string) + } + + var r1 error + if rf, ok := ret.Get(1).(func(string) error); ok { + r1 = rf(setting) + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + // GetAppSettingStrings provides a mock function with given fields: setting func (_m *ApplicationService) GetAppSettingStrings(setting string) ([]string, error) { ret := _m.Called(setting) diff --git a/pkg/interfaces/service.go b/pkg/interfaces/service.go index 891ba66d3..8967847d4 100644 --- a/pkg/interfaces/service.go +++ b/pkg/interfaces/service.go @@ -60,9 +60,13 @@ type ApplicationService interface { AddRoute(route string, handler func(http.ResponseWriter, *http.Request), methods ...string) error // ApplicationSettings returns the key/value map of custom settings ApplicationSettings() map[string]string + // GetAppSetting is a convenience function return a setting from the ApplicationSetting + // section of the service configuration. + // An error is returned if the specified setting is not found. + GetAppSetting(setting string) (string, error) // GetAppSettingStrings is a convenience function that parses the value for the specified custom // application setting as a comma separated list. It returns the list of strings. - // An error is returned if the specified setting is no found. + // An error is returned if the specified setting is not found. GetAppSettingStrings(setting string) ([]string, error) // SetFunctionsPipeline set the functions pipeline with the specified list of Application Functions. // Note that the functions are executed in the order provided in the list.