From 6c123aa1f29365aef94f6c8bc5c0dc3d835aebe9 Mon Sep 17 00:00:00 2001 From: Serhii Nahornyi Date: Wed, 7 Jul 2021 13:49:46 +0300 Subject: [PATCH] Sovrn: Change Imp.bidfloor if invalid (#1331) --- .../server/bidder/sovrn/SovrnBidder.java | 11 ++- .../server/bidder/sovrn/SovrnBidderTest.java | 69 +++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/prebid/server/bidder/sovrn/SovrnBidder.java b/src/main/java/org/prebid/server/bidder/sovrn/SovrnBidder.java index 646d7f6dac6..c138de011fa 100644 --- a/src/main/java/org/prebid/server/bidder/sovrn/SovrnBidder.java +++ b/src/main/java/org/prebid/server/bidder/sovrn/SovrnBidder.java @@ -28,6 +28,7 @@ import org.prebid.server.proto.openrtb.ext.response.BidType; import org.prebid.server.util.HttpUtil; +import java.math.BigDecimal; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -103,7 +104,7 @@ private Imp makeImp(Imp imp) { final ExtImpSovrn sovrnExt = parseExtImpSovrn(imp); return imp.toBuilder() - .bidfloor(sovrnExt.getBidfloor()) + .bidfloor(resolveBidFloor(imp.getBidfloor(), sovrnExt.getBidfloor())) .tagid(ObjectUtils.defaultIfNull(sovrnExt.getTagid(), sovrnExt.getLegacyTagId())) .build(); } @@ -120,6 +121,14 @@ private ExtImpSovrn parseExtImpSovrn(Imp imp) { } } + private static BigDecimal resolveBidFloor(BigDecimal impBidFloor, BigDecimal extBidFloor) { + return !isValidBidFloor(impBidFloor) && isValidBidFloor(extBidFloor) ? extBidFloor : impBidFloor; + } + + private static boolean isValidBidFloor(BigDecimal bidFloor) { + return bidFloor != null && bidFloor.compareTo(BigDecimal.ZERO) > 0; + } + private static MultiMap headers(BidRequest bidRequest) { final MultiMap headers = HttpUtil.headers(); diff --git a/src/test/java/org/prebid/server/bidder/sovrn/SovrnBidderTest.java b/src/test/java/org/prebid/server/bidder/sovrn/SovrnBidderTest.java index d8e16048d4b..2372ab92972 100644 --- a/src/test/java/org/prebid/server/bidder/sovrn/SovrnBidderTest.java +++ b/src/test/java/org/prebid/server/bidder/sovrn/SovrnBidderTest.java @@ -175,6 +175,75 @@ public void makeHttpRequestsShouldReturnResultWithHttpRequestContainingExpectedF .build()); } + @Test + public void makeHttpRequestsShouldSetBidFloorFromExtIfImpBidFloorIsZero() { + // given + final BidRequest bidRequest = BidRequest.builder() + .imp(Collections.singletonList( + Imp.builder().id("impId") + .bidfloor(BigDecimal.ZERO) + .ext(mapper.valueToTree(ExtPrebid.of(null, ExtImpSovrn.of(null, null, BigDecimal.TEN)))) + .build())) + .build(); + + // when + final Result>> result = sovrnBidder.makeHttpRequests(bidRequest); + + // then + assertThat(result.getValue()).hasSize(1) + .extracting(HttpRequest::getBody) + .extracting(body -> mapper.readValue(body, BidRequest.class)) + .flatExtracting(BidRequest::getImp) + .extracting(Imp::getBidfloor) + .containsExactly(BigDecimal.TEN); + } + + @Test + public void makeHttpRequestsShouldSetBidFloorFromExtIfImpBidFloorIsMissed() { + // given + final BidRequest bidRequest = BidRequest.builder() + .imp(Collections.singletonList( + Imp.builder().id("impId") + .bidfloor(null) + .ext(mapper.valueToTree(ExtPrebid.of(null, ExtImpSovrn.of(null, null, BigDecimal.TEN)))) + .build())) + .build(); + + // when + final Result>> result = sovrnBidder.makeHttpRequests(bidRequest); + + // then + assertThat(result.getValue()).hasSize(1) + .extracting(HttpRequest::getBody) + .extracting(body -> mapper.readValue(body, BidRequest.class)) + .flatExtracting(BidRequest::getImp) + .extracting(Imp::getBidfloor) + .containsExactly(BigDecimal.TEN); + } + + @Test + public void makeHttpRequestsShouldNotSetBidFloorFromExtIfImpBidFloorIsValid() { + // given + final BidRequest bidRequest = BidRequest.builder() + .imp(Collections.singletonList( + Imp.builder().id("impId") + .bidfloor(BigDecimal.ONE) + .ext(mapper.valueToTree(ExtPrebid.of(null, ExtImpSovrn.of(null, null, BigDecimal.TEN)))) + .build())) + .build(); + + // when + final Result>> result = sovrnBidder.makeHttpRequests(bidRequest); + + // then + assertThat(result.getValue()).hasSize(1) + .extracting(HttpRequest::getBody) + .extracting(body -> mapper.readValue(body, BidRequest.class)) + .flatExtracting(BidRequest::getImp) + .extracting(Imp::getBidfloor) + .containsExactly(BigDecimal.ONE); + } + @Test public void makeHttpRequestsShouldReturnResultWithHttpRequestsContainingExpectedHeaders() { // given