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

Use video.plcmt to Resolve Media Type #3572

Merged
merged 3 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ private static ImpMediaType resolveBidAdjustmentVideoMediaType(String bidImpId,
.orElse(null);

if (bidImpVideo == null) {
return null;
return ImpMediaType.video_outstream;
}

final Integer placement = bidImpVideo.getPlacement();
return placement == null || Objects.equals(placement, 1)
? ImpMediaType.video
final Integer plcmt = bidImpVideo.getPlcmt();

return Objects.equals(placement, 1) || Objects.equals(plcmt, 1)
? ImpMediaType.video_instream
: ImpMediaType.video_outstream;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,35 +82,33 @@ public AuctionParticipation enrichWithAdjustedBids(AuctionParticipation auctionP
return auctionParticipation.with(updatedBidderResponse);
}

private static ExtRequestBidAdjustmentFactors extBidAdjustmentFactors(BidRequest bidRequest) {
final ExtRequestPrebid prebid = PbsUtil.extRequestPrebid(bidRequest);
return prebid != null ? prebid.getBidadjustmentfactors() : null;
}

private static BigDecimal adjustPrice(BigDecimal priceAdjustmentFactor, BigDecimal price) {
return priceAdjustmentFactor != null && priceAdjustmentFactor.compareTo(BigDecimal.ONE) != 0
? price.multiply(priceAdjustmentFactor)
: price;
}

private BidderBid applyBidAdjustments(BidderBid bidderBid,
BidRequest bidRequest,
String bidder,
BidAdjustments bidAdjustments,
List<BidderError> errors) {
try {
final Price originalPrice = getOriginalPrice(bidderBid);

final ImpMediaType mediaType = ImpMediaTypeResolver.resolve(
bidderBid.getBid().getImpid(),
bidRequest.getImp(),
bidderBid.getType());

final Price priceWithFactorsApplied = applyBidAdjustmentFactors(
originalPrice,
bidderBid,
bidder,
bidRequest);
bidRequest,
mediaType);

final Price priceWithAdjustmentsApplied = applyBidAdjustmentRules(
priceWithFactorsApplied,
bidderBid,
bidder,
bidRequest,
bidAdjustments);
bidAdjustments,
mediaType,
bidderBid.getBid().getDealid());

return updateBid(originalPrice, priceWithAdjustmentsApplied, bidderBid, bidRequest);
} catch (PreBidException e) {
errors.add(BidderError.generic(e.getMessage()));
Expand Down Expand Up @@ -154,51 +152,54 @@ private Price getOriginalPrice(BidderBid bidderBid) {
return Price.of(StringUtils.stripToNull(bidCurrency), price);
}

private Price applyBidAdjustmentFactors(Price bidPrice, BidderBid bidderBid, String bidder, BidRequest bidRequest) {
private Price applyBidAdjustmentFactors(Price bidPrice,
String bidder,
BidRequest bidRequest,
ImpMediaType mediaType) {

final String bidCurrency = bidPrice.getCurrency();
final BigDecimal price = bidPrice.getValue();

final BigDecimal priceAdjustmentFactor = bidAdjustmentForBidder(bidder, bidRequest, bidderBid);
final BigDecimal priceAdjustmentFactor = bidAdjustmentForBidder(bidder, bidRequest, mediaType);
final BigDecimal adjustedPrice = adjustPrice(priceAdjustmentFactor, price);

return Price.of(bidCurrency, adjustedPrice.compareTo(price) != 0 ? adjustedPrice : price);
}

private BigDecimal bidAdjustmentForBidder(String bidder, BidRequest bidRequest, BidderBid bidderBid) {
private BigDecimal bidAdjustmentForBidder(String bidder, BidRequest bidRequest, ImpMediaType mediaType) {
final ExtRequestBidAdjustmentFactors adjustmentFactors = extBidAdjustmentFactors(bidRequest);
if (adjustmentFactors == null) {
return null;
}

final ImpMediaType mediaType = ImpMediaTypeResolver.resolve(
bidderBid.getBid().getImpid(),
bidRequest.getImp(),
bidderBid.getType());
final ImpMediaType targetMediaType = mediaType == ImpMediaType.video_instream ? ImpMediaType.video : mediaType;
return bidAdjustmentFactorResolver.resolve(targetMediaType, adjustmentFactors, bidder);
}

private static ExtRequestBidAdjustmentFactors extBidAdjustmentFactors(BidRequest bidRequest) {
final ExtRequestPrebid prebid = PbsUtil.extRequestPrebid(bidRequest);
return prebid != null ? prebid.getBidadjustmentfactors() : null;
}

return bidAdjustmentFactorResolver.resolve(mediaType, adjustmentFactors, bidder);
private static BigDecimal adjustPrice(BigDecimal priceAdjustmentFactor, BigDecimal price) {
return priceAdjustmentFactor != null && priceAdjustmentFactor.compareTo(BigDecimal.ONE) != 0
? price.multiply(priceAdjustmentFactor)
: price;
}

private Price applyBidAdjustmentRules(Price bidPrice,
BidderBid bidderBid,
String bidder,
BidRequest bidRequest,
BidAdjustments bidAdjustments) {

final Bid bid = bidderBid.getBid();
final String bidCurrency = bidPrice.getCurrency();
final BigDecimal price = bidPrice.getValue();

final ImpMediaType mediaType = ImpMediaTypeResolver.resolve(
bid.getImpid(),
bidRequest.getImp(),
bidderBid.getType());
BidAdjustments bidAdjustments,
ImpMediaType mediaType,
String dealId) {

return bidAdjustmentsResolver.resolve(
Price.of(bidCurrency, price),
bidPrice,
bidRequest,
bidAdjustments,
mediaType == null || mediaType == ImpMediaType.video ? ImpMediaType.video_instream : mediaType,
mediaType,
bidder,
bid.getDealid());
dealId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.prebid.server.VertxTest;
import org.prebid.server.auction.adjustment.BidAdjustmentFactorResolver;
import org.prebid.server.auction.model.AuctionParticipation;
Expand Down Expand Up @@ -47,7 +49,6 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mock.Strictness.LENIENT;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
Expand All @@ -57,13 +58,14 @@
import static org.prebid.server.proto.openrtb.ext.response.BidType.xNative;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class BidAdjustmentsProcessorTest extends VertxTest {

@Mock(strictness = LENIENT)
@Mock
private CurrencyConversionService currencyService;
@Mock(strictness = LENIENT)
@Mock
private BidAdjustmentFactorResolver bidAdjustmentFactorResolver;
@Mock(strictness = LENIENT)
@Mock
private BidAdjustmentsResolver bidAdjustmentsResolver;

private BidAdjustmentsProcessor target;
Expand Down Expand Up @@ -469,7 +471,7 @@ public void shouldReturnBidsWithAdjustedPricesWithVideoInstreamMediaTypeIfVideoP
}

@Test
public void shouldReturnBidsWithAdjustedPricesWithVideoInstreamMediaTypeIfVideoPlacementIsMissing() {
public void shouldReturnBidsWithAdjustedPricesWithVideoInstreamMediaTypeIfVideoPlcmtEqualsOne() {
// given
final BidderResponse bidderResponse = BidderResponse.of(
"bidder",
Expand All @@ -491,6 +493,58 @@ public void shouldReturnBidsWithAdjustedPricesWithVideoInstreamMediaTypeIfVideoP
given(bidAdjustmentFactorResolver.resolve(ImpMediaType.video, givenAdjustments, "bidder"))
.willReturn(BigDecimal.valueOf(3.456));

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

final AuctionParticipation auctionParticipation = givenAuctionParticipation(bidderResponse, bidRequest);

// when
final AuctionParticipation result = target.enrichWithAdjustedBids(
auctionParticipation, bidRequest, givenBidAdjustments());

// then
assertThat(result.getBidderResponse().getSeatBid().getBids())
.extracting(BidderBid::getBid)
.extracting(Bid::getPrice)
.containsExactly(BigDecimal.valueOf(6.912));

verify(bidAdjustmentsResolver).resolve(
eq(Price.of("USD", BigDecimal.valueOf(6.912))),
eq(bidRequest),
eq(givenBidAdjustments()),
eq(ImpMediaType.video_instream),
eq("bidder"),
eq("dealId"));
}

@Test
public void shouldReturnBidsWithAdjustedPricesWithVideoOutstreamMediaTypeIfVideoPlacementAndPlcmtIsMissing() {
// given
final BidderResponse bidderResponse = BidderResponse.of(
"bidder",
BidderSeatBid.builder()
.bids(List.of(
givenBidderBid(Bid.builder()
.impid("123")
.price(BigDecimal.valueOf(2))
.dealid("dealId")
.build(),
"USD", video)))
.build(),
1);

final ExtRequestBidAdjustmentFactors givenAdjustments = ExtRequestBidAdjustmentFactors.builder()
.mediatypes(new EnumMap<>(singletonMap(ImpMediaType.video,
singletonMap("bidder", BigDecimal.valueOf(3.456)))))
.build();
given(bidAdjustmentFactorResolver.resolve(ImpMediaType.video_outstream, givenAdjustments, "bidder"))
.willReturn(BigDecimal.valueOf(3.456));

final BidRequest bidRequest = givenBidRequest(singletonList(givenImp(singletonMap("bidder", 2), impBuilder ->
impBuilder.id("123").video(Video.builder().build()))),
builder -> builder.ext(ExtRequest.of(ExtRequestPrebid.builder()
Expand All @@ -514,13 +568,13 @@ public void shouldReturnBidsWithAdjustedPricesWithVideoInstreamMediaTypeIfVideoP
eq(Price.of("USD", BigDecimal.valueOf(6.912))),
eq(bidRequest),
eq(givenBidAdjustments()),
eq(ImpMediaType.video_instream),
eq(ImpMediaType.video_outstream),
eq("bidder"),
eq("dealId"));
}

@Test
public void shouldReturnBidAdjustmentMediaTypeNullIfImpIdNotEqualBidImpId() {
public void shouldReturnBidAdjustmentMediaTypeVideoOutstreamIfImpIdNotEqualBidImpId() {
// given
final BidderResponse bidderResponse = BidderResponse.of(
"bidder",
Expand Down Expand Up @@ -560,11 +614,12 @@ public void shouldReturnBidAdjustmentMediaTypeNullIfImpIdNotEqualBidImpId() {
.extracting(Bid::getPrice)
.containsExactly(BigDecimal.valueOf(2));

verify(bidAdjustmentFactorResolver).resolve(ImpMediaType.video_outstream, givenAdjustments, "bidder");
verify(bidAdjustmentsResolver).resolve(
eq(Price.of("USD", BigDecimal.valueOf(2))),
eq(bidRequest),
eq(givenBidAdjustments()),
eq(ImpMediaType.video_instream),
eq(ImpMediaType.video_outstream),
eq("bidder"),
eq("dealId"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"mimes"
],
"w": 300,
"h": 250
"h": 250,
"placement": 1
},
"ext": {
"prebid": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"imp" : [ {
"id": "impId001",
"video": {
"placement": 1,
"mimes": [
"mimes"
],
Expand Down
Loading