From bb85e65d3273bdf4393137699cecd2847688ab71 Mon Sep 17 00:00:00 2001 From: Nilesh Chate <97721111+pm-nilesh-chate@users.noreply.github.com> Date: Wed, 11 Oct 2023 18:33:27 +0530 Subject: [PATCH] DCOPS-1343: Handle legacy invalid profiles (#582) --- .../openwrap/database/mysql/adunit_config.go | 6 ++++ .../database/mysql/adunit_config_test.go | 30 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/modules/pubmatic/openwrap/database/mysql/adunit_config.go b/modules/pubmatic/openwrap/database/mysql/adunit_config.go index 7d3ff03034b..204cf02327b 100644 --- a/modules/pubmatic/openwrap/database/mysql/adunit_config.go +++ b/modules/pubmatic/openwrap/database/mysql/adunit_config.go @@ -40,6 +40,12 @@ func (db *mySqlDB) GetAdunitConfig(profileID, displayVersion int) (*adunitconfig adunitConfig.ConfigPattern = models.MACRO_AD_UNIT_ID } + // safe check for old legacy profiles + // new profiles cannot be created as UI-API has config object validation + if adunitConfig.Config == nil { + adunitConfig.Config = make(map[string]*adunitconfig.AdConfig) + } + if _, ok := adunitConfig.Config["default"]; !ok { adunitConfig.Config["default"] = &adunitconfig.AdConfig{} } diff --git a/modules/pubmatic/openwrap/database/mysql/adunit_config_test.go b/modules/pubmatic/openwrap/database/mysql/adunit_config_test.go index 29396c1cb1a..57faca742af 100644 --- a/modules/pubmatic/openwrap/database/mysql/adunit_config_test.go +++ b/modules/pubmatic/openwrap/database/mysql/adunit_config_test.go @@ -186,6 +186,36 @@ func Test_mySqlDB_GetAdunitConfig(t *testing.T) { return db }, }, + { + name: "config key not present", + fields: fields{ + cfg: config.Database{ + Queries: config.Queries{ + GetAdunitConfigQuery: "^SELECT (.+) FROM wrapper_media_config (.+)", + }, + }, + }, + args: args{ + profileID: 5890, + displayVersion: 1, + }, + want: &adunitconfig.AdUnitConfig{ + ConfigPattern: "_DIV_", + Config: map[string]*adunitconfig.AdConfig{ + "default": {}, + }, + }, + wantErr: false, + setup: func() *sql.DB { + db, mock, err := sqlmock.New() + if err != nil { + t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) + } + rows := sqlmock.NewRows([]string{"adunitConfig"}).AddRow(`{"configPattern": "_DIV_"}`) + mock.ExpectQuery(regexp.QuoteMeta("^SELECT (.+) FROM wrapper_media_config (.+)")).WillReturnRows(rows) + return db + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {