Skip to content

Commit

Permalink
Adding unit tests and code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Pubmatic-Dhruv-Sonone committed Jun 12, 2024
1 parent d29f544 commit c302e97
Show file tree
Hide file tree
Showing 10 changed files with 625 additions and 320 deletions.
4 changes: 2 additions & 2 deletions adapters/ortbbidder/bidderparams/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ func (bcfg *BidderConfig) SetRequestParams(bidderName string, requestParams map[
}

// SetRequestParams sets the bidder specific requestParams
func (bcfg *BidderConfig) SetResponseParams(bidderName string, requestParams map[string]BidderParamMapper) {
func (bcfg *BidderConfig) SetResponseParams(bidderName string, responseParams map[string]BidderParamMapper) {
if _, found := bcfg.bidderConfigMap[bidderName]; !found {
bcfg.bidderConfigMap[bidderName] = &config{}
}
bcfg.bidderConfigMap[bidderName].requestParams = requestParams
bcfg.bidderConfigMap[bidderName].responseParams = responseParams
}

// GetRequestParams returns bidder specific requestParams
Expand Down
5 changes: 3 additions & 2 deletions adapters/ortbbidder/bidderparams/parser.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package bidderparams

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/prebid/prebid-server/v2/util/jsonutil"
)

const (
Expand Down Expand Up @@ -64,7 +65,7 @@ func readFile(dirPath, file string) (map[string]any, error) {
return nil, err
}
var contentMap map[string]any
err = json.Unmarshal(content, &contentMap)
err = jsonutil.UnmarshalValid(content, &contentMap)
return contentMap, err
}

Expand Down
102 changes: 84 additions & 18 deletions adapters/ortbbidder/bidderparams/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func TestPrepareRequestParams(t *testing.T) {
requestParams, err := prepareParams(tt.args.bidderName, tt.args.requestParamCfg)
assert.Equalf(t, tt.want.err, err, "updateBidderParamsMapper returned unexpected error")
assert.Equalf(t, tt.want.requestParams, requestParams, "updateBidderParamsMapper returned unexpected mapper")

})
}
}
Expand All @@ -174,7 +175,7 @@ func TestLoadBidderConfig(t *testing.T) {
name: "read_directory_fail",
want: want{
biddersConfigMap: nil,
err: "error:[open invalid-path: no such file or directory] dirPath:[invalid-path]",
err: "error handling request params: error:[open invalid-request-param-path: no such file or directory] dirPath:[invalid-request-param-path]",
},
setup: func() (string, string, error) {
return "invalid-request-param-path", "invalid-response-param-path", nil
Expand All @@ -184,36 +185,75 @@ func TestLoadBidderConfig(t *testing.T) {
name: "found_file_without_.json_extension",
want: want{
biddersConfigMap: nil,
err: "error:[invalid_json_file_name] filename:[example.txt]",
err: "error handling request params: error:[invalid_json_file_name] filename:[example.txt]",
},
setup: func() (string, string, error) {
dirPath := t.TempDir()
err := os.WriteFile(dirPath+"/example.txt", []byte("anything"), 0644)
return dirPath, "", err
},
},
{
name: "response params - read_directory_fail",
want: want{
biddersConfigMap: nil,
err: "error handling response params: error:[open invalid-path: no such file or directory] dirPath:[invalid-path]",
},
setup: func() (string, string, error) {
dirPath := t.TempDir()
err := os.WriteFile(dirPath+"/example.json", []byte("anything"), 0644)
return dirPath, "invalid-path", err
},
},
{
name: "response params - found_file_without_.json_extension",
want: want{
biddersConfigMap: nil,
err: "error handling response params: error:[invalid_json_file_name] filename:[example.txt]",
},
setup: func() (string, string, error) {
requestDirPath := t.TempDir()
err := os.WriteFile(requestDirPath+"/example.json", []byte("anything"), 0644)
if err != nil {
return "", "", err
}
responseDirPath := t.TempDir()
err = os.WriteFile(responseDirPath+"/example.txt", []byte("anything"), 0644)
return requestDirPath, responseDirPath, err
},
},
{
name: "oRTB_bidder_not_found",
want: want{
biddersConfigMap: &BidderConfig{bidderConfigMap: make(map[string]*config)},
err: "",
},
setup: func() (string, string, error) {
dirPath := t.TempDir()
err := os.WriteFile(dirPath+"/example.json", []byte("anything"), 0644)
return dirPath, "", err
requestDirPath := t.TempDir()
err := os.WriteFile(requestDirPath+"/example.json", []byte("anything"), 0644)
if err != nil {
return "", "", err
}
responseDirPath := t.TempDir()
err = os.WriteFile(responseDirPath+"/example.json", []byte("anything"), 0644)
return requestDirPath, responseDirPath, err
},
},
{
name: "oRTB_bidder_found_but_invalid_json_present",
want: want{
biddersConfigMap: nil,
err: "error:[fail_to_read_file]",
err: "error handling request params: error:[fail_to_read_file]",
},
setup: func() (string, string, error) {
dirPath := t.TempDir()
err := os.WriteFile(dirPath+"/owortb_test.json", []byte("anything"), 0644)
return dirPath, "", err
requestDirPath := t.TempDir()
err := os.WriteFile(requestDirPath+"/owortb_test.json", []byte("anything"), 0644)
if err != nil {
return "", "", err
}
responseDirPath := t.TempDir()
err = os.WriteFile(responseDirPath+"/owortb_test.json", []byte("anything"), 0644)
return requestDirPath, responseDirPath, err
},
},
{
Expand All @@ -227,9 +267,14 @@ func TestLoadBidderConfig(t *testing.T) {
err: "",
},
setup: func() (string, string, error) {
dirPath := t.TempDir()
err := os.WriteFile(dirPath+"/owortb_test.json", []byte("{}"), 0644)
return dirPath, "", err
requestDirPath := t.TempDir()
err := os.WriteFile(requestDirPath+"/owortb_test.json", []byte("{}"), 0644)
if err != nil {
return "", "", err
}
responseDirPath := t.TempDir()
err = os.WriteFile(responseDirPath+"/owortb_test.json", []byte("{}"), 0644)
return requestDirPath, responseDirPath, err
},
},
{
Expand All @@ -239,9 +284,14 @@ func TestLoadBidderConfig(t *testing.T) {
err: "error:[invalid_json_file_content_malformed_properties] bidderName:[owortb_test]",
},
setup: func() (string, string, error) {
dirPath := t.TempDir()
err := os.WriteFile(dirPath+"/owortb_test.json", []byte(`{"properties":"invalid-properties"}`), 0644)
return dirPath, "", err
requestDirPath := t.TempDir()
err := os.WriteFile(requestDirPath+"/owortb_test.json", []byte(`{"properties":"invalid-properties"}`), 0644)
if err != nil {
return "", "", err
}
responseDirPath := t.TempDir()
err = os.WriteFile(responseDirPath+"/owortb_test.json", []byte(`{"properties":"invalid-properties"}`), 0644)
return requestDirPath, responseDirPath, err
},
},
{
Expand All @@ -254,13 +304,16 @@ func TestLoadBidderConfig(t *testing.T) {
"adunitid": {Location: "app.adunit.id"},
"slotname": {Location: "ext.slotname"},
},
responseParams: map[string]BidderParamMapper{
"mtype": {Location: "seatbid.#.bid.#.ext.mtype"},
},
},
}},
err: "",
},
setup: func() (string, string, error) {
dirPath := t.TempDir()
err := os.WriteFile(dirPath+"/owortb_test.json", []byte(`
requestDirPath := t.TempDir()
err := os.WriteFile(requestDirPath+"/owortb_test.json", []byte(`
{
"title":"ortb bidder",
"properties": {
Expand All @@ -275,7 +328,20 @@ func TestLoadBidderConfig(t *testing.T) {
}
}
`), 0644)
return dirPath, "", err
if err != nil {
return "", "", err
}
responseDirPath := t.TempDir()
err = os.WriteFile(responseDirPath+"/owortb_test.json", []byte(`{
"title":"ortb bidder",
"properties": {
"mtype": {
"type": "string",
"location": "seatbid.#.bid.#.ext.mtype"
}
}
}`), 0644)
return requestDirPath, responseDirPath, err
},
},
}
Expand Down
13 changes: 0 additions & 13 deletions adapters/ortbbidder/error.go

This file was deleted.

6 changes: 6 additions & 0 deletions adapters/ortbbidder/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ func newBadInputError(message string, args ...any) error {
Message: fmt.Sprintf(message, args...),
}
}

func newBadServerResponseError(message string, args ...any) error {
return &errortypes.BadServerResponse{
Message: fmt.Sprintf(message, args...),
}
}
Loading

0 comments on commit c302e97

Please sign in to comment.