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

Improvedigital bidder: Functionality update #1175

Merged
merged 4 commits into from
Apr 27, 2021
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
Original file line number Diff line number Diff line change
@@ -1,15 +1,101 @@
package org.prebid.server.bidder.improvedigital;

import com.iab.openrtb.request.BidRequest;
import com.iab.openrtb.request.Imp;
import com.iab.openrtb.response.BidResponse;
import com.iab.openrtb.response.SeatBid;
import io.vertx.core.http.HttpMethod;
import org.apache.commons.collections4.CollectionUtils;
import org.prebid.server.bidder.Bidder;
import org.prebid.server.bidder.OpenrtbBidder;
import org.prebid.server.bidder.model.BidderBid;
import org.prebid.server.bidder.model.BidderError;
import org.prebid.server.bidder.model.HttpCall;
import org.prebid.server.bidder.model.HttpRequest;
import org.prebid.server.bidder.model.Result;
import org.prebid.server.exception.PreBidException;
import org.prebid.server.json.DecodeException;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.HttpUtil;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* ImproveDigital {@link Bidder} implementation.
*/
public class ImprovedigitalBidder extends OpenrtbBidder<Void> {
public class ImprovedigitalBidder implements Bidder<BidRequest> {

private final String endpointUrl;
private final JacksonMapper mapper;

public ImprovedigitalBidder(String endpointUrl, JacksonMapper mapper) {
super(endpointUrl, RequestCreationStrategy.SINGLE_REQUEST, Void.class, mapper);
this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl));
this.mapper = Objects.requireNonNull(mapper);
}

@Override
public Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest request) {

return Result.withValues(Collections.singletonList(
HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(endpointUrl)
.headers(HttpUtil.headers())
.payload(request)
.body(mapper.encode(request))
.build()));
}

@Override
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(httpCall.getRequest().getPayload(), bidResponse));
} catch (DecodeException | PreBidException e) {
return Result.withError(BidderError.badServerResponse(e.getMessage()));
}
}

private static List<BidderBid> extractBids(BidRequest bidRequest, BidResponse bidResponse) {
if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) {
return Collections.emptyList();
}
if (bidResponse.getSeatbid().size() > 1) {
throw new PreBidException(String.format("Unexpected SeatBid! Must be only one but have: %d",
bidResponse.getSeatbid().size()));
}
return bidsFromResponse(bidRequest, bidResponse);
}

private static List<BidderBid> bidsFromResponse(BidRequest bidRequest, BidResponse bidResponse) {
return bidResponse.getSeatbid().stream()
.filter(Objects::nonNull)
.map(SeatBid::getBid)
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.map(bid -> BidderBid.of(bid, getType(bid.getImpid(), bidRequest.getImp()), bidResponse.getCur()))
.collect(Collectors.toList());
}

private static BidType getType(String impId, List<Imp> imps) {
for (Imp imp : imps) {
if (imp.getId().equals(impId)) {
if (imp.getBanner() != null) {
return BidType.banner;
}
if (imp.getVideo() != null) {
return BidType.video;
}
if (imp.getXNative() != null) {
return BidType.xNative;
}
throw new PreBidException(String.format("Unknown impression type for ID: \"%s\"", impId));
}
}
throw new PreBidException(String.format("Failed to find impression for ID: \"%s\"", impId));
}
}
2 changes: 2 additions & 0 deletions src/main/resources/bidder-config/improvedigital.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ adapters:
app-media-types:
- banner
- video
- native
site-media-types:
- banner
- video
- native
supported-vendors:
vendor-id: 253
usersync:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.prebid.server.bidder.improvedigital;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.iab.openrtb.request.Banner;
import com.iab.openrtb.request.BidRequest;
import com.iab.openrtb.request.Imp;
import com.iab.openrtb.request.Native;
import com.iab.openrtb.request.Video;
import com.iab.openrtb.response.Bid;
import com.iab.openrtb.response.BidResponse;
import com.iab.openrtb.response.SeatBid;
Expand All @@ -20,10 +23,13 @@
import java.util.List;
import java.util.function.Function;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.prebid.server.proto.openrtb.ext.response.BidType.banner;
import static org.prebid.server.proto.openrtb.ext.response.BidType.video;
import static org.prebid.server.proto.openrtb.ext.response.BidType.xNative;

public class ImprovedigitalBidderTest extends VertxTest {

Expand Down Expand Up @@ -57,8 +63,8 @@ public void makeHttpRequestsShouldNotModifyIncomingRequest() {
// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue()).hasSize(1)
.extracting(httpRequest -> mapper.readValue(httpRequest.getBody(), BidRequest.class))
.containsOnly(bidRequest);
.extracting(HttpRequest::getPayload)
.containsExactly(bidRequest);
}

@Test
Expand Down Expand Up @@ -105,7 +111,28 @@ public void makeBidsShouldReturnEmptyListIfBidResponseSeatBidIsNull() throws Jso
}

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

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

// then
assertThat(result.getErrors()).hasSize(1)
.containsExactly(BidderError.badServerResponse("Unexpected SeatBid! Must be only one but have: 2"));
assertThat(result.getValue()).isEmpty();
}

@Test
public void makeBidsShouldReturnErrorIfBannerOrVideoNotPresent() throws JsonProcessingException {
// given
final HttpCall<BidRequest> httpCall = givenHttpCall(
BidRequest.builder()
Expand All @@ -117,10 +144,86 @@ public void makeBidsShouldReturnBannerBidIfBannerIsPresent() throws JsonProcessi
// when
final Result<List<BidderBid>> result = improvedigitalBidder.makeBids(httpCall, null);

// then
assertThat(result.getErrors())
.containsExactly(BidderError.badServerResponse("Unknown impression type for ID: \"123\""));
assertThat(result.getValue()).isEmpty();
}

@Test
public void makeBidsShouldReturnBannerBidIfBannerIsPresent() throws JsonProcessingException {
// given
final HttpCall<BidRequest> httpCall = givenHttpCall(
BidRequest.builder()
.imp(singletonList(Imp.builder().banner(Banner.builder().build()).id("123").build()))
.build(),
mapper.writeValueAsString(
givenBidResponse(bidBuilder -> bidBuilder.impid("123"))));

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

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

@Test
public void makeBidsShouldReturnVideoBidIfVideoIsPresent() throws JsonProcessingException {
// given
final HttpCall<BidRequest> httpCall = givenHttpCall(
BidRequest.builder()
.imp(singletonList(Imp.builder().video(Video.builder().build()).id("123").build()))
.build(),
mapper.writeValueAsString(
givenBidResponse(bidBuilder -> bidBuilder.impid("123"))));

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

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

@Test
public void makeBidsShouldReturnNativeBidIfNativeIsPresent() throws JsonProcessingException {
// given
final HttpCall<BidRequest> httpCall = givenHttpCall(
BidRequest.builder()
.imp(singletonList(Imp.builder().xNative(Native.builder().build()).id("123").build()))
.build(),
mapper.writeValueAsString(
givenBidResponse(bidBuilder -> bidBuilder.impid("123"))));

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

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

@Test
public void makeBidsShouldReturnErrorIfImpNotFoundForId() throws JsonProcessingException {
// given
final HttpCall<BidRequest> httpCall = givenHttpCall(
BidRequest.builder()
.imp(singletonList(Imp.builder().video(Video.builder().build()).id("123").build()))
.build(),
mapper.writeValueAsString(
givenBidResponse(bidBuilder -> bidBuilder.impid("456"))));

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

// then
assertThat(result.getErrors())
.containsExactly(BidderError.badServerResponse("Failed to find impression for ID: \"456\""));
assertThat(result.getValue()).isEmpty();
}

private static BidResponse givenBidResponse(Function<Bid.BidBuilder, Bid.BidBuilder> bidCustomizer) {
Expand Down