Skip to content

Commit

Permalink
Boldwin: get bid type from bid.mtype
Browse files Browse the repository at this point in the history
  • Loading branch information
VeryExtraordinaryUsername committed Feb 12, 2024
1 parent 53b2200 commit 207fe96
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
31 changes: 14 additions & 17 deletions src/main/java/org/prebid/server/bidder/boldwin/BoldwinBidder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.iab.openrtb.request.BidRequest;
import com.iab.openrtb.request.Imp;
import com.iab.openrtb.response.Bid;
import com.iab.openrtb.response.BidResponse;
import com.iab.openrtb.response.SeatBid;
import org.apache.commons.collections4.CollectionUtils;
Expand Down Expand Up @@ -106,13 +107,13 @@ private HttpRequest<BidRequest> createSingleRequest(Imp imp, BidRequest request)
public Result<List<BidderBid>> makeBids(BidderCall<BidRequest> httpCall, BidRequest bidRequest) {
try {
final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class);
return Result.withValues(extractBids(httpCall.getRequest().getPayload(), bidResponse));
return Result.withValues(extractBids(bidResponse));
} catch (DecodeException | PreBidException e) {
return Result.withError(BidderError.badServerResponse(e.getMessage()));
}
}

private static List<BidderBid> extractBids(BidRequest bidRequest, BidResponse bidResponse) {
private static List<BidderBid> extractBids(BidResponse bidResponse) {
if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) {
return Collections.emptyList();
}
Expand All @@ -123,23 +124,19 @@ private static List<BidderBid> extractBids(BidRequest bidRequest, BidResponse bi
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.filter(Objects::nonNull)
.map(bid -> BidderBid.of(bid, getBidType(bid.getImpid(), bidRequest.getImp()), bidResponse.getCur()))
.map(bid -> BidderBid.of(bid, getBidType(bid), bidResponse.getCur()))
.toList();
}

private static BidType getBidType(String impId, List<Imp> imps) {
for (Imp imp : imps) {
if (imp.getId().equals(impId)) {
if (imp.getBanner() != null) {
return BidType.banner;
} else if (imp.getVideo() != null) {
return BidType.video;
} else if (imp.getXNative() != null) {
return BidType.xNative;
}
}
}

throw new PreBidException("Failed to find impression for ID: '%s'".formatted(impId));
private static BidType getBidType(Bid bid) {
final Integer mType = bid.getMtype() != null ? bid.getMtype() : 999;
return switch (mType) {
case 1 -> BidType.banner;
case 2 -> BidType.video;
case 4 -> BidType.xNative;
default -> throw new PreBidException(
"Unable to fetch mediaType in multi-format: %s".formatted(bid.getImpid())
);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,15 @@ public void makeBidsShouldReturnxNativeBid() throws JsonProcessingException {
.imp(singletonList(Imp.builder().id("123").xNative(Native.builder().build()).build()))
.build(),
mapper.writeValueAsString(
givenBidResponse(bidBuilder -> bidBuilder.impid("123"))));
givenBidResponse(bidBuilder -> bidBuilder.mtype(4).impid("123"))));

// when
final Result<List<BidderBid>> result = target.makeBids(httpCall, null);

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.containsExactly(BidderBid.of(Bid.builder().impid("123").build(), xNative, "USD"));
.containsExactly(BidderBid.of(Bid.builder().impid("123").mtype(4).build(), xNative, "USD"));
}

@Test
Expand All @@ -176,15 +176,15 @@ public void makeBidsShouldReturnBannerBid() throws JsonProcessingException {
.imp(singletonList(Imp.builder().id("123").banner(Banner.builder().build()).build()))
.build(),
mapper.writeValueAsString(
givenBidResponse(bidBuilder -> bidBuilder.impid("123"))));
givenBidResponse(bidBuilder -> bidBuilder.mtype(1).impid("123"))));

// when
final Result<List<BidderBid>> result = target.makeBids(httpCall, null);

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.containsExactly(BidderBid.of(Bid.builder().impid("123").build(), banner, "USD"));
.containsExactly(BidderBid.of(Bid.builder().mtype(1).impid("123").build(), banner, "USD"));
}

@Test
Expand All @@ -195,19 +195,19 @@ public void makeBidsShouldReturnVideoBid() throws JsonProcessingException {
.imp(singletonList(Imp.builder().id("123").video(Video.builder().build()).build()))
.build(),
mapper.writeValueAsString(
givenBidResponse(bidBuilder -> bidBuilder.impid("123"))));
givenBidResponse(bidBuilder -> bidBuilder.mtype(2).impid("123"))));

// when
final Result<List<BidderBid>> result = target.makeBids(httpCall, null);

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.containsExactly(BidderBid.of(Bid.builder().impid("123").build(), video, "USD"));
.containsExactly(BidderBid.of(Bid.builder().mtype(2).impid("123").build(), video, "USD"));
}

@Test
public void makeBidsShouldThrowErrorFailedToFindImpression() throws JsonProcessingException {
public void makeBidsShouldThrowErrorUnableToFetchMediaType() throws JsonProcessingException {
// given
final BidderCall<BidRequest> httpCall = givenHttpCall(
BidRequest.builder()
Expand All @@ -223,7 +223,7 @@ public void makeBidsShouldThrowErrorFailedToFindImpression() throws JsonProcessi
assertThat(result.getValue()).isEmpty();
assertThat(result.getErrors()).hasSize(1)
.allSatisfy(error -> {
assertThat(error.getMessage()).startsWith("Failed to find impression for ID: '123'");
assertThat(error.getMessage()).startsWith("Unable to fetch mediaType in multi-format: 123");
assertThat(error.getType()).isEqualTo(BidderError.Type.bad_server_response);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"type": "video"
},
"origbidcpm": 1.25
}
},
"mtype": 2
}
],
"seat": "boldwin",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"crid": "crid",
"adm": "adm001",
"h": 600,
"w": 800
"w": 800,
"mtype": 2
}
]
}
Expand Down

0 comments on commit 207fe96

Please sign in to comment.