Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bidadjustments fix #1243

Merged
merged 2 commits into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions src/main/java/org/prebid/server/auction/ExchangeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -1094,14 +1094,12 @@ private BidderBid updateBidderBidWithBidPriceChanges(BidderBid bidderBid,
final ObjectNode bidExt = bid.getExt();
final ObjectNode updatedBidExt = bidExt != null ? bidExt : mapper.mapper().createObjectNode();

updateExtWithOrigPriceValues(updatedBidExt, price, bidCurrency);

if (adjustedPrice.compareTo(price) != 0) {
bid.setPrice(adjustedPrice);
addPropertyToNode(updatedBidExt, ORIGINAL_BID_CPM, new DecimalNode(price));
}
// add origbidcur if conversion occurred
if (priceInAdServerCurrency.compareTo(price) != 0 && StringUtils.isNotBlank(bidCurrency)) {
addPropertyToNode(updatedBidExt, ORIGINAL_BID_CURRENCY, new TextNode(bidCurrency));
}

if (!updatedBidExt.isEmpty()) {
bid.setExt(updatedBidExt);
}
Expand All @@ -1112,6 +1110,7 @@ private BidderBid updateBidderBidWithBidPriceChanges(BidderBid bidderBid,
private static BidAdjustmentMediaType resolveBidAdjustmentMediaType(String bidImpId,
List<Imp> imps,
BidType bidType) {

switch (bidType) {
case banner:
return BidAdjustmentMediaType.banner;
Expand All @@ -1131,12 +1130,17 @@ private static BidAdjustmentMediaType resolveBidAdjustmentVideoMediaType(String
.filter(imp -> imp.getId().equals(bidImpId))
.map(Imp::getVideo)
.filter(Objects::nonNull)
.findFirst().orElse(null);
if (bidImpVideo == null || Objects.equals(bidImpVideo.getPlacement(), 1)) {
return BidAdjustmentMediaType.video;
.findFirst()
.orElse(null);

if (bidImpVideo == null) {
return null;
}

return BidAdjustmentMediaType.video_outstream;
final Integer placement = bidImpVideo.getPlacement();
return placement == null || Objects.equals(placement, 1)
? BidAdjustmentMediaType.video
: BidAdjustmentMediaType.video_outstream;
}

private static BigDecimal bidAdjustmentForBidder(String bidder,
Expand Down Expand Up @@ -1175,7 +1179,14 @@ private static BigDecimal adjustPrice(BigDecimal priceAdjustmentFactor, BigDecim
: price;
}

private void addPropertyToNode(ObjectNode node, String propertyName, JsonNode propertyValue) {
private static void updateExtWithOrigPriceValues(ObjectNode updatedBidExt, BigDecimal price, String bidCurrency) {
addPropertyToNode(updatedBidExt, ORIGINAL_BID_CPM, new DecimalNode(price));
if (StringUtils.isNotBlank(bidCurrency)) {
addPropertyToNode(updatedBidExt, ORIGINAL_BID_CURRENCY, new TextNode(bidCurrency));
}
}

private static void addPropertyToNode(ObjectNode node, String propertyName, JsonNode propertyValue) {
node.set(propertyName, propertyValue);
}

Expand Down
68 changes: 68 additions & 0 deletions src/test/java/org/prebid/server/auction/ExchangeServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.iab.openrtb.request.Regs;
import com.iab.openrtb.request.Site;
import com.iab.openrtb.request.User;
import com.iab.openrtb.request.Video;
import com.iab.openrtb.response.Bid;
import com.iab.openrtb.response.BidResponse;
import com.iab.openrtb.response.SeatBid;
Expand Down Expand Up @@ -137,6 +138,7 @@
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.prebid.server.proto.openrtb.ext.response.BidType.banner;
import static org.prebid.server.proto.openrtb.ext.response.BidType.video;

public class ExchangeServiceTest extends VertxTest {

Expand Down Expand Up @@ -2562,6 +2564,72 @@ public void shouldReturnBidsWithAdjustedPricesWhenAdjustmentFactorPresent() {
.containsExactly(BigDecimal.valueOf(4.936));
}

@Test
public void shouldReturnBidsWithAdjustedPricesWithVideoInstreamMediaTypeIfVideoPlacementEqualsOne() {
// given
final Bidder<?> bidder = mock(Bidder.class);
givenBidder("bidder", bidder, givenSeatBid(singletonList(
BidderBid.of(Bid.builder().impid("123").price(BigDecimal.valueOf(2)).build(), video, null))));

final ExtRequestBidadjustmentfactors givenAdjustments = ExtRequestBidadjustmentfactors.builder()
.mediatypes(new EnumMap<>(Collections.singletonMap(BidAdjustmentMediaType.video,
Collections.singletonMap("bidder", BigDecimal.valueOf(3.456)))))
.build();

final BidRequest bidRequest = givenBidRequest(singletonList(givenImp(singletonMap("bidder", 2), impBuilder ->
impBuilder.id("123").video(Video.builder().placement(1).build()))),
builder -> builder.ext(ExtRequest.of(ExtRequestPrebid.builder()
.aliases(emptyMap())
.bidadjustmentfactors(givenAdjustments)
.auctiontimestamp(1000L)
.build())));

// when
exchangeService.holdAuction(givenRequestContext(bidRequest)).result();

// then
final List<BidderResponse> capturedBidResponses = captureBidResponses();
assertThat(capturedBidResponses)
.extracting(BidderResponse::getSeatBid)
.flatExtracting(BidderSeatBid::getBids)
.extracting(BidderBid::getBid)
.extracting(Bid::getPrice)
.containsExactly(BigDecimal.valueOf(6.912));
}

@Test
public void shouldReturnBidsWithAdjustedPricesWithVideoInstreamMediaTypeIfVideoPlacementIsMissing() {
// given
final Bidder<?> bidder = mock(Bidder.class);
givenBidder("bidder", bidder, givenSeatBid(singletonList(
BidderBid.of(Bid.builder().impid("123").price(BigDecimal.valueOf(2)).build(), video, null))));

final ExtRequestBidadjustmentfactors givenAdjustments = ExtRequestBidadjustmentfactors.builder()
.mediatypes(new EnumMap<>(Collections.singletonMap(BidAdjustmentMediaType.video,
Collections.singletonMap("bidder", BigDecimal.valueOf(3.456)))))
.build();

final BidRequest bidRequest = givenBidRequest(singletonList(givenImp(singletonMap("bidder", 2), impBuilder ->
impBuilder.id("123").video(Video.builder().build()))),
builder -> builder.ext(ExtRequest.of(ExtRequestPrebid.builder()
.aliases(emptyMap())
.bidadjustmentfactors(givenAdjustments)
.auctiontimestamp(1000L)
.build())));

// when
exchangeService.holdAuction(givenRequestContext(bidRequest)).result();

// then
final List<BidderResponse> capturedBidResponses = captureBidResponses();
assertThat(capturedBidResponses)
.extracting(BidderResponse::getSeatBid)
.flatExtracting(BidderSeatBid::getBids)
.extracting(BidderBid::getBid)
.extracting(Bid::getPrice)
.containsExactly(BigDecimal.valueOf(6.912));
}

@Test
public void shouldReturnBidsWithAdjustedPricesWhenAdjustmentMediaFactorPresent() {
// given
Expand Down
8 changes: 7 additions & 1 deletion src/test/java/org/prebid/server/it/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private static String cacheResponseFromRequestJson(
final List<CacheObject> responseCacheObjects = new ArrayList<>();
for (PutObject putItem : puts) {
final String id = putItem.getType().equals("json")
? putItem.getValue().get("id").textValue() + "@" + putItem.getValue().get("price")
? putItem.getValue().get("id").textValue() + "@" + resolvePriceForJsonMediaType(putItem)
: putItem.getValue().textValue();

final String uuid = jsonNodeMatcher.get(id).textValue();
Expand All @@ -154,6 +154,12 @@ private static String cacheResponseFromRequestJson(
}
}

private static String resolvePriceForJsonMediaType(PutObject putItem) {
final JsonNode extObject = putItem.getValue().get("ext");
final JsonNode origBidCpm = extObject != null ? extObject.get("origbidcpm") : null;
return origBidCpm != null ? origBidCpm.toString() : putItem.getValue().get("price").toString();
}

/**
* Cache debug fields "requestbody" and "responsebody" are escaped JSON strings.
* This comparator allows to compare them with actual values as usual JSON objects.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
]
}
]
}
},
"origbidcpm": 12.09
}
}
},
Expand All @@ -47,9 +48,11 @@
"bidder_id": 2,
"bid_ad_type": 0,
"ranking_price": 0.0
}
},
"origbidcpm": 5.5,
"origbidcur": "USD"
}
}
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"hb_size_rubicon": "120x600",
"hb_bidder_rubicon": "rubicon"
}
}
},
"origbidcpm": 4.26
}
},
{
Expand Down Expand Up @@ -64,7 +65,8 @@
1
]
}
}
},
"origbidcpm": 3
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"cacheId": "3c0769d8-0dd9-465c-8bf3-f570605ba698"
}
}
}
},
"origbidcpm": 0.01
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
"price": 0.01,
"id": "testid",
"impid": "testimpid",
"cid": "8048"
"cid": "8048",
"ext": {
"origbidcpm": 0.01
}
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
}
},
"origbidcpm": 0.5,
"origbidcur" : "USD"
"origbidcur": "USD"
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"crid": "crid12",
"w": 400,
"h": 300,
"ext" : {
"origbidcpm" : 0.5,
"origbidcur" : "USD"
"ext": {
"origbidcpm": 0.5,
"origbidcur": "USD"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
"cacheId": "f0ab9105-cb21-4e59-b433-70f5ad6671cb"
}
}
}
},
"origbidcpm": 46.6,
"origbidcur": "USD"
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
"crid": "Dummy_supership.jp",
"dealid": "test-deal-id",
"w": 300,
"h": 250
"h": 250,
"ext": {
"origbidcpm": 46.6,
"origbidcur": "USD"
}
}
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
"cacheId": "a5d3a873-d06e-4f2f-8556-120e05d62b28"
}
}
}
},
"origbidcpm": 1.00,
"origbidcur": "USD"
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
"w": 728,
"h": 90,
"ext": {
"adType": "leaderboard"
"adType": "leaderboard",
"origbidcpm": 1,
"origbidcur": "USD"
}
}
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"cacheId": "71615e3d-8a18-4099-a807-3952199b532a"
}
}
}
},
"origbidcpm": 2.25
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
"tag-example.com"
],
"cid": "1001",
"crid": "2002"
"crid": "2002",
"ext": {
"origbidcpm": 2.25
}
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"cacheId": "a3558327-7696-4606-a5e9-43413bd7faea"
}
}
}
},
"origbidcpm": 2.25
}
},
{
Expand Down Expand Up @@ -87,7 +88,8 @@
"cacheId": "aedec380-879a-4029-8e7a-5a51676c887c"
}
}
}
},
"origbidcpm": 0.5
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
"crid": "crid022",
"cat": [
"IAB2"
]
],
"ext": {
"origbidcpm": 2.25
}
}
},
{
Expand All @@ -35,7 +38,10 @@
"IAB2"
],
"w": 300,
"h": 250
"h": 250,
"ext": {
"origbidcpm": 0.5
}
}
},
{
Expand All @@ -44,4 +50,4 @@
"expiry": 120
}
]
}
}
Loading