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

IX: Add multi-format ad unit support #2078

Merged
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
34 changes: 31 additions & 3 deletions src/main/java/org/prebid/server/bidder/ix/IxBidder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.iab.openrtb.request.Banner;
import com.iab.openrtb.request.BidRequest;
Expand Down Expand Up @@ -41,6 +42,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;

public class IxBidder implements Bidder<BidRequest> {
Expand Down Expand Up @@ -217,7 +219,7 @@ private List<BidderBid> bidsFromResponse(BidResponse bidResponse, BidRequest bid
private BidderBid toBidderBid(Bid bid, BidRequest bidRequest, BidResponse bidResponse, List<BidderError> errors) {
final BidType bidType;
try {
bidType = getBidType(bid.getImpid(), bidRequest.getImp());
bidType = getBidType(bid, bidRequest.getImp());
} catch (PreBidException e) {
errors.add(BidderError.badServerResponse(e.getMessage()));
return null;
Expand Down Expand Up @@ -290,8 +292,34 @@ private static Response mergeNativeImpTrackers(Response response, List<EventTrac
.build();
}

private static BidType getBidType(String impId, List<Imp> imps) {
for (Imp imp : imps) {
private static BidType getBidType(Bid bid, List<Imp> imps) {
return getBidTypeFromMtype(bid.getMtype())
.or(() -> getBidTypeFromExtPrebidType(bid.getExt()))
.orElseGet(() -> getBidTypeFromImp(imps, bid.getImpid()));
}

private static Optional<BidType> getBidTypeFromMtype(Integer mType) {
final BidType bidType = mType != null ? switch (mType) {
case 1 -> BidType.banner;
case 2 -> BidType.video;
case 3 -> BidType.audio;
case 4 -> BidType.xNative;
default -> null;
} : null;

return Optional.ofNullable(bidType);
}

private static Optional<BidType> getBidTypeFromExtPrebidType(ObjectNode bidExt) {
return Optional.ofNullable(bidExt)
.map(ext -> ext.get("prebid"))
.map(prebid -> prebid.get("type"))
.map(JsonNode::asText)
.map(BidType::fromString);
}

private static BidType getBidTypeFromImp(List<Imp> imps, String impId) {
for (Imp imp: imps) {
if (imp.getId().equals(impId)) {
if (imp.getBanner() != null) {
return BidType.banner;
Expand Down
141 changes: 141 additions & 0 deletions src/test/java/org/prebid/server/bidder/ix/IxBidderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,147 @@ public void makeBidsShouldReturnValidAdmIfNativeIsPresentInImpAndAdm12() throws
.containsExactly(mapper.writeValueAsString(expectedNativeResponse));
}

@Test
public void makeBidsShouldReturnBannerBidIfMTypeIsOne() throws JsonProcessingException {
// given
final Banner banner = Banner.builder().w(300).h(200).build();
final Video video = Video.builder().build();
final BidderCall<BidRequest> httpCall = givenHttpCall(
BidRequest.builder()
.imp(singletonList(Imp.builder()
.id("123")
.banner(banner)
.video(video).build()))
.build(),
mapper.writeValueAsString(
givenBidResponse(
bidBuilder -> bidBuilder
.impid("123")
.mtype(1))));

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

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.extracting(BidderBid::getType)
.containsExactly(BidType.banner);
}

@Test
public void makeBidsShouldReturnVideoBidIfMTypeIsTwo() throws JsonProcessingException {
// given
final Banner banner = Banner.builder().w(300).h(200).build();
final Video video = Video.builder().build();
final BidderCall<BidRequest> httpCall = givenHttpCall(
BidRequest.builder()
.imp(singletonList(Imp.builder()
.id("123")
.banner(banner)
.video(video).build()))
.build(),
mapper.writeValueAsString(
givenBidResponse(
bidBuilder -> bidBuilder
.impid("123")
.mtype(2))));

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

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.extracting(BidderBid::getType)
.containsExactly(BidType.video);
}

@Test
public void makeBidsShouldReturnAudioBidIfMTypeIsThree() throws JsonProcessingException {
// given
final Banner banner = Banner.builder().w(300).h(200).build();
final Video video = Video.builder().build();
final BidderCall<BidRequest> httpCall = givenHttpCall(
BidRequest.builder()
.imp(singletonList(Imp.builder()
.id("123")
.banner(banner)
.video(video).build()))
.build(),
mapper.writeValueAsString(
givenBidResponse(
bidBuilder -> bidBuilder
.impid("123")
.mtype(3))));

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

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.extracting(BidderBid::getType)
.containsExactly(BidType.audio);
}

@Test
public void makeBidsShouldReturnNativeBidIfMTypeIsFour() throws JsonProcessingException {
// given
final Banner banner = Banner.builder().w(300).h(200).build();
final Video video = Video.builder().build();
final BidderCall<BidRequest> httpCall = givenHttpCall(
BidRequest.builder()
.imp(singletonList(Imp.builder()
.id("123")
.banner(banner)
.video(video).build()))
.build(),
mapper.writeValueAsString(
givenBidResponse(
bidBuilder -> bidBuilder
.impid("123")
.mtype(4))));

// when
final Result<List<BidderBid>> result = ixBidder.makeBids(httpCall, null);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We prefer to have empty line betweengiven when then blocks

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also pls check for similar occurences


// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.extracting(BidderBid::getType)
.containsExactly(BidType.xNative);
}

@Test
public void makeBidsShouldReturnCorrectTypeExtPrebidTypeInResponse() throws JsonProcessingException {
// given
final Banner banner = Banner.builder().w(300).h(200).build();
final Video video = Video.builder().build();
final BidderCall<BidRequest> httpCall = givenHttpCall(
BidRequest.builder()
.imp(singletonList(Imp.builder()
.id("123")
.banner(banner)
.video(video).build()))
.build(),
mapper.writeValueAsString(
givenBidResponse(
bidBuilder -> bidBuilder
.impid("123")
.ext(mapper.createObjectNode()
.set("prebid", mapper.createObjectNode().put("type", "video"))))));

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

// then
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls make 768 line empty

assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.extracting(BidderBid::getType)
.containsExactly(BidType.video);
}

private static BidRequest givenBidRequest(
Function<BidRequest.BidRequestBuilder, BidRequest.BidRequestBuilder> bidRequestCustomizer,
Function<Imp.ImpBuilder, Imp.ImpBuilder> impCustomizer) {
Expand Down