From 87a2333b826304b4bf6f605aae392ad14e60144e Mon Sep 17 00:00:00 2001 From: Kalidas Date: Thu, 9 Nov 2023 09:35:06 +0000 Subject: [PATCH] Updated the review comments --- adapters/alkimi/alkimi.go | 47 ++++++++------- adapters/alkimi/alkimi_test.go | 103 ++++++++++++++++++++++++++++++++- 2 files changed, 125 insertions(+), 25 deletions(-) diff --git a/adapters/alkimi/alkimi.go b/adapters/alkimi/alkimi.go index 0f20b57c96d..d298977791f 100644 --- a/adapters/alkimi/alkimi.go +++ b/adapters/alkimi/alkimi.go @@ -123,7 +123,7 @@ func buildBidderRequest(adapter *adapter, encoded []byte) *adapters.RequestData func (adapter *adapter) MakeBids(request *openrtb2.BidRequest, externalRequest *adapters.RequestData, response *adapters.ResponseData) (*adapters.BidderResponse, []error) { var errs []error - if request == nil || response == nil || adapters.IsResponseStatusCodeNoContent(response) { + if adapters.IsResponseStatusCodeNoContent(response) { return nil, nil } @@ -138,35 +138,34 @@ func (adapter *adapter) MakeBids(request *openrtb2.BidRequest, externalRequest * } bidCount := len(bidResp.SeatBid) - if bidCount > 0 { - bidResponse := adapters.NewBidderResponseWithBidsCapacity(bidCount) - for _, seatBid := range bidResp.SeatBid { - for _, bid := range seatBid.Bid { - copyBid := bid - resolveMacros(©Bid) - impId := copyBid.ImpID - imp := request.Imp - bidType, err := getMediaTypeForImp(impId, imp) - if err != nil { - errs = append(errs, err) - } - bidderBid := &adapters.TypedBid{ - Bid: ©Bid, - BidType: bidType, - Seat: "alkimi", - } - bidResponse.Bids = append(bidResponse.Bids, bidderBid) + if bidCount == 0 { + return nil, []error{&errortypes.BadServerResponse{ + Message: "Empty SeatBid array", + }} + } + + bidResponse := adapters.NewBidderResponseWithBidsCapacity(bidCount) + for _, seatBid := range bidResp.SeatBid { + for _, bid := range seatBid.Bid { + copyBid := bid + resolveMacros(©Bid) + impId := copyBid.ImpID + imp := request.Imp + bidType, err := getMediaTypeForImp(impId, imp) + if err != nil { + errs = append(errs, err) } + bidderBid := &adapters.TypedBid{ + Bid: ©Bid, + BidType: bidType, + } + bidResponse.Bids = append(bidResponse.Bids, bidderBid) } - return bidResponse, errs } - return nil, nil + return bidResponse, errs } func resolveMacros(bid *openrtb2.Bid) { - if bid == nil { - return - } strPrice := strconv.FormatFloat(bid.Price, 'f', -1, 64) bid.NURL = strings.Replace(bid.NURL, price_macro, strPrice, -1) bid.AdM = strings.Replace(bid.AdM, price_macro, strPrice, -1) diff --git a/adapters/alkimi/alkimi_test.go b/adapters/alkimi/alkimi_test.go index 7b2ded463dc..5177b316251 100644 --- a/adapters/alkimi/alkimi_test.go +++ b/adapters/alkimi/alkimi_test.go @@ -116,11 +116,41 @@ func TestMakeBidsShouldReturnErrorIfResponseBodyCouldNotBeParsed(t *testing.T) { assert.Equal(t, "adm:1", bids.Bids[0].Bid.AdM) } +func TestMakeBidsShouldReturnErrorIfResponseBodyContainsIncorrectImp(t *testing.T) { + // given + bidder, _ := buildBidder() + bid := openrtb2.Bid{ + ImpID: "impId-1-incorrect", + AdM: "adm:${AUCTION_PRICE}", + NURL: "nurl:${AUCTION_PRICE}", + Price: 1, + } + sb := openrtb2.SeatBid{Bid: []openrtb2.Bid{bid}} + resp := openrtb2.BidResponse{SeatBid: []openrtb2.SeatBid{sb}} + respJson, jsonErr := json.Marshal(resp) + request := openrtb2.BidRequest{ + Imp: append(make([]openrtb2.Imp, 1), openrtb2.Imp{ID: "impId-1", Banner: &openrtb2.Banner{}}), + } + // when + bids, errs := bidder.MakeBids(&request, nil, &adapters.ResponseData{ + StatusCode: 200, + Body: respJson, + }) + // then + if jsonErr != nil { + t.Fatalf("Failed to serialize test bid %v: %v", bid, jsonErr) + } + assert.Len(t, bids.Bids, 1) + assert.Len(t, errs, 1) +} + func TestMakeBidsShouldReturnEmptyListIfBidResponseIsNull(t *testing.T) { // given bidder, _ := buildBidder() // when - bids, errs := bidder.MakeBids(&openrtb2.BidRequest{}, nil, nil) + bids, errs := bidder.MakeBids(&openrtb2.BidRequest{}, nil, &adapters.ResponseData{ + StatusCode: 204, + }) // then if len(errs) > 0 { t.Fatalf("Failed to make bids: %v", errs) @@ -128,6 +158,18 @@ func TestMakeBidsShouldReturnEmptyListIfBidResponseIsNull(t *testing.T) { assert.Nil(t, bids) } +func TestMakeBidsShouldReturnEmptyListIfBidResponseIsError(t *testing.T) { + // given + bidder, _ := buildBidder() + // when + bids, errs := bidder.MakeBids(&openrtb2.BidRequest{}, nil, &adapters.ResponseData{ + StatusCode: 500, + }) + // then + assert.Len(t, errs, 1) + assert.Nil(t, bids) +} + func TestMakeBidsShouldReturnBidWithResolvedMacros(t *testing.T) { // given bidder, _ := buildBidder() @@ -161,6 +203,65 @@ func TestMakeBidsShouldReturnBidWithResolvedMacros(t *testing.T) { assert.Equal(t, "adm:1", bids.Bids[0].Bid.AdM) } +func TestMakeBidsShouldReturnBidForAllTypes(t *testing.T) { + // given + bidder, _ := buildBidder() + bid := openrtb2.Bid{ + ImpID: "impId-1", + AdM: "adm:${AUCTION_PRICE}", + NURL: "nurl:${AUCTION_PRICE}", + Price: 1, + } + seatBid := openrtb2.SeatBid{Bid: []openrtb2.Bid{bid}} + resp := openrtb2.BidResponse{SeatBid: []openrtb2.SeatBid{seatBid}} + respJson, jsonErr := json.Marshal(resp) + + request := openrtb2.BidRequest{ + Imp: append(make([]openrtb2.Imp, 1), openrtb2.Imp{ID: "impId-1", Video: &openrtb2.Video{}}), + } + // when + bids, errs := bidder.MakeBids(&request, nil, &adapters.ResponseData{ + StatusCode: 200, + Body: respJson, + }) + // then + if jsonErr != nil { + t.Fatalf("Failed to serialize test bid %v: %v", bid, jsonErr) + } + if len(errs) > 0 { + t.Fatalf("Failed to make bids: %v", errs) + } + assert.Len(t, bids.Bids, 1) + + request = openrtb2.BidRequest{ + Imp: append(make([]openrtb2.Imp, 1), openrtb2.Imp{ID: "impId-1", Audio: &openrtb2.Audio{}}), + } + // when + bids, errs = bidder.MakeBids(&request, nil, &adapters.ResponseData{ + StatusCode: 200, + Body: respJson, + }) + // then + if len(errs) > 0 { + t.Fatalf("Failed to make bids: %v", errs) + } + assert.Len(t, bids.Bids, 1) + + request = openrtb2.BidRequest{ + Imp: append(make([]openrtb2.Imp, 1), openrtb2.Imp{ID: "impId-1", Native: &openrtb2.Native{}}), + } + // when + bids, errs = bidder.MakeBids(&request, nil, &adapters.ResponseData{ + StatusCode: 200, + Body: respJson, + }) + // then + if len(errs) > 0 { + t.Fatalf("Failed to make bids: %v", errs) + } + assert.Len(t, bids.Bids, 1) +} + func buildBidder() (adapters.Bidder, error) { return Builder( openrtb_ext.BidderAlkimi,