Skip to content

Commit

Permalink
Yieldmo: Add gpid support (#1452)
Browse files Browse the repository at this point in the history
  • Loading branch information
And1sS authored Sep 24, 2021
1 parent 86b7132 commit 20d03fa
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.prebid.server.bidder.yieldmo;

import com.fasterxml.jackson.databind.JsonNode;
import com.iab.openrtb.request.Imp;
import com.iab.openrtb.response.Bid;
import org.apache.commons.lang3.StringUtils;
import org.prebid.server.bidder.Bidder;
import org.prebid.server.bidder.OpenrtbBidder;
import org.prebid.server.bidder.yieldmo.proto.YieldmoImpExt;
Expand All @@ -25,8 +27,13 @@ public YieldmoBidder(String endpointUrl, JacksonMapper mapper) {
protected Imp modifyImp(Imp imp, ExtImpYieldmo impExt) throws PreBidException {
final Imp.ImpBuilder modifiedImp = imp.toBuilder();

final JsonNode pbadslotNode = imp.getExt().at("/data/pbadslot");
final String pbadslot = !pbadslotNode.isMissingNode()
? StringUtils.defaultIfEmpty(pbadslotNode.asText(), null)
: null;

try {
modifiedImp.ext(mapper.mapper().valueToTree(YieldmoImpExt.of(impExt.getPlacementId())));
modifiedImp.ext(mapper.mapper().valueToTree(YieldmoImpExt.of(impExt.getPlacementId(), pbadslot)));
} catch (IllegalArgumentException e) {
throw new PreBidException(e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
public class YieldmoImpExt {

String placementId;

String pbadslot;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.prebid.server.bidder.yieldmo;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.iab.openrtb.request.Banner;
import com.iab.openrtb.request.BidRequest;
import com.iab.openrtb.request.Imp;
Expand Down Expand Up @@ -75,14 +77,70 @@ public void makeHttpRequestsShouldReturnExtPlacementFromYieldmoPlacement() {
final Result<List<HttpRequest<BidRequest>>> result = yieldmoBidder.makeHttpRequests(bidRequest);

// then
assertThat(result.getErrors()).hasSize(0);
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.extracting(httpRequest -> mapper.readValue(httpRequest.getBody(), BidRequest.class))
.flatExtracting(BidRequest::getImp)
.extracting(Imp::getExt)
.containsExactly(mapper.valueToTree(YieldmoImpExt.of(PLACEMENT_VALUE, null)));
}

@Test
public void makeHttpRequestsShouldAddPbadslotToImpExtIfPresentAndNotEmpty() {
// given
final ObjectNode impExt = mapper.valueToTree(ExtPrebid.of(null, ExtImpYieldmo.of(null)));
impExt.set("data", mapper.createObjectNode().set("pbadslot", TextNode.valueOf("pbadslot")));

final BidRequest bidRequest = givenBidRequest(impBuilder -> impBuilder.ext(impExt));

// when
final Result<List<HttpRequest<BidRequest>>> result = yieldmoBidder.makeHttpRequests(bidRequest);

final YieldmoImpExt expectedExt = YieldmoImpExt.of(PLACEMENT_VALUE);
assertThat(result.getValue()).hasSize(1)
// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.extracting(httpRequest -> mapper.readValue(httpRequest.getBody(), BidRequest.class))
.flatExtracting(BidRequest::getImp)
.extracting(Imp::getExt)
.containsOnly(mapper.valueToTree(expectedExt));
.containsExactly(mapper.valueToTree(YieldmoImpExt.of(null, "pbadslot")));
}

@Test
public void makeHttpRequestsShouldNotAddPbadslotToImpExtIfEmpty() {
// given
final ObjectNode impExt = mapper.valueToTree(ExtPrebid.of(null, ExtImpYieldmo.of(null)));
impExt.set("data", mapper.createObjectNode().set("pbadslot", TextNode.valueOf("")));

final BidRequest bidRequest = givenBidRequest(impBuilder -> impBuilder.ext(impExt));

// when
final Result<List<HttpRequest<BidRequest>>> result = yieldmoBidder.makeHttpRequests(bidRequest);

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.extracting(httpRequest -> mapper.readValue(httpRequest.getBody(), BidRequest.class))
.flatExtracting(BidRequest::getImp)
.extracting(Imp::getExt)
.containsExactly(mapper.valueToTree(YieldmoImpExt.of(null, null)));
}

@Test
public void makeHttpRequestsShouldNotAddPbadslotToImpExtIfNull() {
// given
final BidRequest bidRequest = givenBidRequest(impBuilder ->
impBuilder.ext(mapper.valueToTree(ExtPrebid.of(null, ExtImpYieldmo.of(null)))));

// when
final Result<List<HttpRequest<BidRequest>>> result = yieldmoBidder.makeHttpRequests(bidRequest);

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.extracting(httpRequest -> mapper.readValue(httpRequest.getBody(), BidRequest.class))
.flatExtracting(BidRequest::getImp)
.extracting(Imp::getExt)
.containsExactly(mapper.valueToTree(YieldmoImpExt.of(null, null)));
}

@Test
Expand All @@ -94,10 +152,10 @@ public void makeHttpRequestShouldReturnCorrectHeaders() {
final Result<List<HttpRequest<BidRequest>>> result = yieldmoBidder.makeHttpRequests(bidRequest);

// then
assertThat(result.getErrors()).hasSize(0);
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue()).flatExtracting(httpRequest -> httpRequest.getHeaders().entries())
.extracting(Map.Entry::getKey, Map.Entry::getValue)
.containsOnly(
.containsExactly(
tuple(HttpUtil.CONTENT_TYPE_HEADER.toString(), HttpUtil.APPLICATION_JSON_CONTENT_TYPE),
tuple(HttpUtil.ACCEPT_HEADER.toString(), HttpHeaderValues.APPLICATION_JSON.toString()));
}
Expand Down Expand Up @@ -161,7 +219,7 @@ public void makeBidsShouldReturnVideoBidByDefault() throws JsonProcessingExcepti
// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.containsOnly(BidderBid.of(Bid.builder().impid("123").build(), video, "USD"));
.containsExactly(BidderBid.of(Bid.builder().impid("123").build(), video, "USD"));
}

@Test
Expand All @@ -180,7 +238,7 @@ public void makeBidsShouldReturnBannerBidIfBannerIsPresentInRequestImp() throws
// 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(), banner, "USD"));
}

@Test
Expand All @@ -199,15 +257,15 @@ public void makeBidsShouldReturnVideoBidIfVideoIsPresentInRequestImp() throws Js
// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.containsOnly(BidderBid.of(Bid.builder().impid("123").build(), video, "USD"));
.containsExactly(BidderBid.of(Bid.builder().impid("123").build(), video, "USD"));
}

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

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

Expand All @@ -217,9 +275,9 @@ private static BidRequest givenBidRequest(Function<Imp.ImpBuilder, Imp.ImpBuilde

private static Imp givenImp(Function<Imp.ImpBuilder, Imp.ImpBuilder> impCustomizer) {
return impCustomizer.apply(Imp.builder()
.id("123")
.banner(Banner.builder().build())
.ext(mapper.valueToTree(ExtPrebid.of(null, ExtImpYieldmo.of(PLACEMENT_VALUE)))))
.id("123")
.banner(Banner.builder().build())
.ext(mapper.valueToTree(ExtPrebid.of(null, ExtImpYieldmo.of(PLACEMENT_VALUE)))))
.build();
}

Expand Down

0 comments on commit 20d03fa

Please sign in to comment.