diff --git a/modules/pubmatic/openwrap/adapters/bidders.go b/modules/pubmatic/openwrap/adapters/bidders.go index 678dc601ec8..d8feb482c81 100644 --- a/modules/pubmatic/openwrap/adapters/bidders.go +++ b/modules/pubmatic/openwrap/adapters/bidders.go @@ -730,3 +730,22 @@ func builderAidem(params BidderParameters) (json.RawMessage, error) { return jsonStr.Bytes(), nil } + +func builderCompass(params BidderParameters) (json.RawMessage, error) { + jsonStr := bytes.Buffer{} + jsonStr.WriteByte('{') + oneOf := []string{"placementId", "endpointId"} + for _, param := range oneOf { + if key, ok := getString(params.FieldMap[param]); ok { + fmt.Fprintf(&jsonStr, `"%s":"%s"`, param, key) + break + } + } + // len=0 (no mandatory params present) + if jsonStr.Len() == 1 { + return nil, fmt.Errorf(errMandatoryParameterMissingFormat, params.AdapterName, oneOf) + } + + jsonStr.WriteByte('}') + return jsonStr.Bytes(), nil +} diff --git a/modules/pubmatic/openwrap/adapters/bidders_test.go b/modules/pubmatic/openwrap/adapters/bidders_test.go index 08c01d55a5c..52783889c68 100644 --- a/modules/pubmatic/openwrap/adapters/bidders_test.go +++ b/modules/pubmatic/openwrap/adapters/bidders_test.go @@ -3119,3 +3119,50 @@ func Test_builderAidem(t *testing.T) { }) } } + +func TestBuilderCompass(t *testing.T) { + type args struct { + params BidderParameters + } + tests := []struct { + name string + args args + want json.RawMessage + wantErr bool + }{ + { + name: "Valid Scenerio (oneOf placementId or endpointId) is present-placementId", + args: args{params: BidderParameters{FieldMap: JSONObject{"placementId": "dbdsfh"}}}, + want: json.RawMessage(`{"placementId": "dbdsfh"}`), + wantErr: false, + }, + { + name: "Valid Scenerio (oneOf placementId or endpointId) is present-endpointId", + args: args{params: BidderParameters{FieldMap: JSONObject{"endpointId": "dbdsfh"}}}, + want: json.RawMessage(`{"endpointId": "dbdsfh"}`), + wantErr: false, + }, + { + name: "Valid Scenerio (oneOf placementId or endpointId), Both are present", + args: args{params: BidderParameters{FieldMap: JSONObject{"placementId": "sdhks", "endpointId": "sdjksd"}}}, + want: json.RawMessage(`{"placementId": "sdhks"}`), + wantErr: false, + }, + { + name: "Invalid Scenerio (None Of placementId or endpointId) is present", + args: args{params: BidderParameters{FieldMap: JSONObject{}}}, + want: json.RawMessage(``), + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := builderCompass(tt.args.params) + if (err != nil) != tt.wantErr { + t.Errorf("builderCompass() error = %v, wantErr %v", err, tt.wantErr) + return + } + AssertJSON(t, tt.want, got) + }) + } +} diff --git a/modules/pubmatic/openwrap/adapters/builder.go b/modules/pubmatic/openwrap/adapters/builder.go index 23ce21b68b7..c2996005017 100644 --- a/modules/pubmatic/openwrap/adapters/builder.go +++ b/modules/pubmatic/openwrap/adapters/builder.go @@ -59,6 +59,7 @@ func initBidderBuilderFactory() { string(openrtb_ext.BidderKargo): builderKargo, string(openrtb_ext.BidderPGAMSsp): builderPGAMSSP, string(openrtb_ext.BidderAidem): builderAidem, + string(openrtb_ext.BidderCompass): builderCompass, } } diff --git a/modules/pubmatic/openwrap/adapters/tests/s2s_bidders.json b/modules/pubmatic/openwrap/adapters/tests/s2s_bidders.json index 12cefbbf095..9a0d55a1bbe 100644 --- a/modules/pubmatic/openwrap/adapters/tests/s2s_bidders.json +++ b/modules/pubmatic/openwrap/adapters/tests/s2s_bidders.json @@ -136,5 +136,20 @@ "siteId":"abcdefg" } } + }, + { + "name": "compass_json_OneOf - placementId/endpointId", + "args" : { + "adapterName": "compass", + "requestJSON": { + "placementId": "123456", + "endpointId": "abcdefg" + } + }, + "want": { + "expectedJSON": { + "placementId": "123456" + } + } } ] \ No newline at end of file