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

Telaria: add pod support #1763

Merged
merged 7 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 23 additions & 18 deletions src/main/java/org/prebid/server/bidder/telaria/TelariaBidder.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class TelariaBidder implements Bidder<BidRequest> {

Expand Down Expand Up @@ -64,14 +63,13 @@ public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest bidRequ
String seatCode = null;
final BidRequest.BidRequestBuilder requestBuilder = bidRequest.toBuilder();
SerhiiNahornyi marked this conversation as resolved.
Show resolved Hide resolved
ExtImpTelaria extImp = null;
for (Imp imp : bidRequest.getImp()) {
try {
extImp = parseImpExt(imp);
seatCode = extImp.getSeatCode();
validImps.add(updateImp(imp, extImp, publisherId));
} catch (PreBidException e) {
return Result.withError(BidderError.badInput(e.getMessage()));
}
final Imp imp = bidRequest.getImp().get(0);
try {
marki1an marked this conversation as resolved.
Show resolved Hide resolved
extImp = parseImpExt(imp);
seatCode = extImp.getSeatCode();
validImps.add(updateImp(imp, extImp, publisherId));
} catch (PreBidException e) {
return Result.withError(BidderError.badInput(e.getMessage()));
}

if (extImp != null && extImp.getExtra() != null) {
Expand Down Expand Up @@ -169,29 +167,36 @@ private static MultiMap headers(BidRequest bidRequest) {
public Result<List<BidderBid>> makeBids(HttpCall<BidRequest> httpCall, BidRequest bidRequest) {
try {
final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class);
return Result.withValues(extractBids(bidResponse));
return Result.withValues(extractBids(bidResponse, bidRequest));
} catch (PreBidException | DecodeException e) {
return Result.withError(BidderError.badServerResponse(e.getMessage()));
}
}

private List<BidderBid> extractBids(BidResponse bidResponse) {
private List<BidderBid> extractBids(BidResponse bidResponse, BidRequest bidRequest) {
if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) {
return Collections.emptyList();
}
return bidsFromResponse(bidResponse);
return bidsFromResponse(bidResponse, bidRequest);
}

private static List<BidderBid> bidsFromResponse(BidResponse bidResponse) {
private static List<BidderBid> bidsFromResponse(BidResponse bidResponse, BidRequest bidRequest) {
final SeatBid firstSeatBid = bidResponse.getSeatbid().get(0);
final List<Bid> bids = firstSeatBid.getBid();
final List<BidderBid> bidderBids = new ArrayList<>();
final List<Imp> imps = bidRequest.getImp();

if (CollectionUtils.isEmpty(bids)) {
if (CollectionUtils.isEmpty(bids) || CollectionUtils.isEmpty(imps)) {
return Collections.emptyList();
}
marki1an marked this conversation as resolved.
Show resolved Hide resolved
return bids.stream()
.filter(Objects::nonNull)
.map(bid -> BidderBid.of(bid, BidType.video, bidResponse.getCur()))
.collect(Collectors.toList());

for (int i = 0; i < bids.size(); i++) {
final Bid bid = bids.get(i);
if (bid != null && i < imps.size()) {
final Bid modifyBid = bid.toBuilder().impid(imps.get(i).getId()).build();
bidderBids.add(BidderBid.of(modifyBid, BidType.video, bidResponse.getCur()));
}
}
return bidderBids;
marki1an marked this conversation as resolved.
Show resolved Hide resolved
}
}
124 changes: 73 additions & 51 deletions src/test/java/org/prebid/server/bidder/telaria/TelariaBidderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.UnaryOperator;

import static java.util.Collections.singletonList;
import static java.util.function.Function.identity;
import static java.util.function.UnaryOperator.identity;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.tuple;
Expand Down Expand Up @@ -225,38 +225,6 @@ public void makeHttpRequestsShouldSetSitePublisherIdIfSiteIsPresent() {
.containsExactly("seatCode");
}

@Test
public void makeBidsShouldReturnEmptyBidderBidsFromSecondSeatBid() throws JsonProcessingException {
// given
final SeatBid firstSeatBId = SeatBid.builder()
.bid(singletonList(Bid.builder()
.impid("123")
.build()))
.build();

final SeatBid secondSeatBid = SeatBid.builder()
.bid(singletonList(Bid.builder()
.impid("456")
.build()))
.build();

final HttpCall<BidRequest> httpCall = givenHttpCall(
BidRequest.builder()
.imp(singletonList(Imp.builder().id("123").banner(Banner.builder().build()).build()))
.build(),
mapper.writeValueAsString(BidResponse.builder()
.seatbid(Arrays.asList(firstSeatBId, secondSeatBid))
.build()));

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

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

@Test
public void makeHttpRequestsShouldReturnResultWithHttpRequestsContainingExpectedHeaders() {
// given
Expand Down Expand Up @@ -287,6 +255,40 @@ public void makeHttpRequestsShouldReturnResultWithHttpRequestsContainingExpected
tuple("DNT", "0"));
}

@Test
public void makeBidsShouldReturnEmptyBidderBidsFromSecondSeatBid() throws JsonProcessingException {
// given
final SeatBid firstSeatBId = SeatBid.builder()
.bid(singletonList(Bid.builder()
.impid("123")
.build()))
.build();

final SeatBid secondSeatBid = SeatBid.builder()
.bid(singletonList(Bid.builder()
.impid("456")
.build()))
.build();

final BidRequest bidRequest = BidRequest.builder()
.imp(singletonList(Imp.builder().id("123").banner(Banner.builder().build()).build()))
.build();

final HttpCall<BidRequest> httpCall = givenHttpCall(
bidRequest,
mapper.writeValueAsString(BidResponse.builder()
.seatbid(Arrays.asList(firstSeatBId, secondSeatBid))
.build()));

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

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

@Test
public void makeBidsShouldReturnErrorIfResponseBodyCouldNotBeParsed() {
// given
Expand Down Expand Up @@ -319,14 +321,32 @@ public void makeBidsShouldReturnEmptyListIfBidResponseSeatBidIsNull() throws Jso
}

@Test
public void makeBidsShouldReturnErrorIfNoBidsFromSeatArePresent() throws JsonProcessingException {
public void makeBidsShouldReturnEmptyListIfNoBidsFromSeatArePresent() throws JsonProcessingException {
// given
final HttpCall<BidRequest> httpCall = givenHttpCall(null,
final BidRequest bidRequest = BidRequest.builder().build();
final HttpCall<BidRequest> httpCall = givenHttpCall(bidRequest,
mapper.writeValueAsString(BidResponse.builder()
.seatbid(singletonList(SeatBid.builder().build())).build()));

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

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue()).isEmpty();
}

@Test
public void makeBidsShouldReturnEmptyListIfNoImpsFromBidRequestArePresent() throws JsonProcessingException {
// given
final BidRequest bidRequest = BidRequest.builder().build();
final HttpCall<BidRequest> httpCall = givenHttpCall(bidRequest,
mapper.writeValueAsString(BidResponse.builder()
.seatbid(singletonList(SeatBid.builder()
.bid(singletonList(Bid.builder().build())).build())).build()));

// when
final Result<List<BidderBid>> result = telariaBidder.makeBids(httpCall, bidRequest);
marki1an marked this conversation as resolved.
Show resolved Hide resolved

// then
assertThat(result.getErrors()).isEmpty();
Expand All @@ -336,15 +356,17 @@ public void makeBidsShouldReturnErrorIfNoBidsFromSeatArePresent() throws JsonPro
@Test
public void makeBidsShouldReturnVideoBidIfVideoIsPresentInRequestImp() throws JsonProcessingException {
// given
final BidRequest bidRequest = BidRequest.builder()
.imp(singletonList(Imp.builder().id("123").video(Video.builder().build()).build()))
.build();

final HttpCall<BidRequest> httpCall = givenHttpCall(
BidRequest.builder()
.imp(singletonList(Imp.builder().id("123").video(Video.builder().build()).build()))
.build(),
bidRequest,
mapper.writeValueAsString(
givenBidResponse(bidBuilder -> bidBuilder.impid("123"))));

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

// then
assertThat(result.getErrors()).isEmpty();
Expand All @@ -353,28 +375,28 @@ public void makeBidsShouldReturnVideoBidIfVideoIsPresentInRequestImp() throws Js
}

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

return bidRequestCustomizer.apply(BidRequest.builder()
.imp(singletonList(givenImp(impCustomizer))))
.imp(singletonList(givenImp(impCustomizer))))
.build();
}

private static BidRequest givenBidRequest(Function<Imp.ImpBuilder, Imp.ImpBuilder> impCustomizer) {
private static BidRequest givenBidRequest(UnaryOperator<Imp.ImpBuilder> impCustomizer) {
return givenBidRequest(identity(), impCustomizer);
}

private static Imp givenImp(Function<Imp.ImpBuilder, Imp.ImpBuilder> impCustomizer) {
private static Imp givenImp(UnaryOperator<Imp.ImpBuilder> impCustomizer) {
return impCustomizer.apply(Imp.builder()
.id("123")
.video(Video.builder().build())
.ext(mapper.valueToTree(ExtPrebid.of(null,
ExtImpTelaria.of("adCode", "seatCode", null)))))
.id("123")
.video(Video.builder().build())
.ext(mapper.valueToTree(ExtPrebid.of(null,
ExtImpTelaria.of("adCode", "seatCode", null)))))
.build();
}

private static BidResponse givenBidResponse(Function<Bid.BidBuilder, Bid.BidBuilder> bidCustomizer) {
private static BidResponse givenBidResponse(UnaryOperator<Bid.BidBuilder> bidCustomizer) {
return BidResponse.builder()
.cur("USD")
.seatbid(singletonList(SeatBid.builder().bid(singletonList(bidCustomizer.apply(Bid.builder()).build()))
Expand Down