diff --git a/src/main/java/org/prebid/server/bidder/grid/GridBidder.java b/src/main/java/org/prebid/server/bidder/grid/GridBidder.java index e17048049d7..7512593d145 100644 --- a/src/main/java/org/prebid/server/bidder/grid/GridBidder.java +++ b/src/main/java/org/prebid/server/bidder/grid/GridBidder.java @@ -2,8 +2,12 @@ import com.iab.openrtb.request.Imp; import com.iab.openrtb.response.Bid; +import org.apache.commons.lang3.StringUtils; import org.prebid.server.bidder.OpenrtbBidder; import org.prebid.server.bidder.grid.model.ExtImpGrid; +import org.prebid.server.bidder.grid.model.GridExtImp; +import org.prebid.server.bidder.grid.model.GridExtImpData; +import org.prebid.server.bidder.grid.model.GridExtImpDataAdServer; import org.prebid.server.exception.PreBidException; import org.prebid.server.json.JacksonMapper; import org.prebid.server.proto.openrtb.ext.response.BidType; @@ -21,6 +25,26 @@ protected Imp modifyImp(Imp imp, ExtImpGrid impExt) { if (impExt.getUid() == null || impExt.getUid() == 0) { throw new PreBidException("uid is empty"); } + + final GridExtImp gridExtImp; + try { + gridExtImp = mapper.mapper().convertValue(imp.getExt(), GridExtImp.class); + } catch (Exception e) { + throw new PreBidException(e.getMessage()); + } + + final GridExtImpData extImpData = gridExtImp != null ? gridExtImp.getData() : null; + final GridExtImpDataAdServer adServer = extImpData != null ? extImpData.getAdServer() : null; + final String adSlot = adServer != null ? adServer.getAdSlot() : null; + if (StringUtils.isNotEmpty(adSlot)) { + + final GridExtImp modifiedGridExtImp = gridExtImp.toBuilder() + .gpid(adSlot) + .build(); + + return imp.toBuilder().ext(mapper.mapper().valueToTree(modifiedGridExtImp)).build(); + } + return imp; } diff --git a/src/main/java/org/prebid/server/bidder/grid/model/GridExtImp.java b/src/main/java/org/prebid/server/bidder/grid/model/GridExtImp.java new file mode 100644 index 00000000000..72fe1667058 --- /dev/null +++ b/src/main/java/org/prebid/server/bidder/grid/model/GridExtImp.java @@ -0,0 +1,19 @@ +package org.prebid.server.bidder.grid.model; + +import com.fasterxml.jackson.databind.JsonNode; +import lombok.Builder; +import lombok.Value; +import org.prebid.server.proto.openrtb.ext.request.ExtImpPrebid; + +@Builder(toBuilder = true) +@Value +public class GridExtImp { + + ExtImpPrebid prebid; + + JsonNode bidder; + + GridExtImpData data; + + String gpid; +} diff --git a/src/main/java/org/prebid/server/bidder/grid/model/GridExtImpData.java b/src/main/java/org/prebid/server/bidder/grid/model/GridExtImpData.java new file mode 100644 index 00000000000..43d4e35bb91 --- /dev/null +++ b/src/main/java/org/prebid/server/bidder/grid/model/GridExtImpData.java @@ -0,0 +1,16 @@ +package org.prebid.server.bidder.grid.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Value; + +@AllArgsConstructor(staticName = "of") +@Value +public class GridExtImpData { + + @JsonProperty("pbadslot") + String pbAdSlot; + + @JsonProperty("adserver") + GridExtImpDataAdServer adServer; +} diff --git a/src/main/java/org/prebid/server/bidder/grid/model/GridExtImpDataAdServer.java b/src/main/java/org/prebid/server/bidder/grid/model/GridExtImpDataAdServer.java new file mode 100644 index 00000000000..a84828ba1c5 --- /dev/null +++ b/src/main/java/org/prebid/server/bidder/grid/model/GridExtImpDataAdServer.java @@ -0,0 +1,15 @@ +package org.prebid.server.bidder.grid.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Value; + +@AllArgsConstructor(staticName = "of") +@Value +public class GridExtImpDataAdServer { + + String name; + + @JsonProperty("adslot") + String adSlot; +} diff --git a/src/test/java/org/prebid/server/bidder/grid/GridBidderTest.java b/src/test/java/org/prebid/server/bidder/grid/GridBidderTest.java index b775ae42e36..cbfa1d50094 100644 --- a/src/test/java/org/prebid/server/bidder/grid/GridBidderTest.java +++ b/src/test/java/org/prebid/server/bidder/grid/GridBidderTest.java @@ -12,6 +12,9 @@ import org.junit.Test; import org.prebid.server.VertxTest; import org.prebid.server.bidder.grid.model.ExtImpGrid; +import org.prebid.server.bidder.grid.model.GridExtImp; +import org.prebid.server.bidder.grid.model.GridExtImpData; +import org.prebid.server.bidder.grid.model.GridExtImpDataAdServer; import org.prebid.server.bidder.model.BidderBid; import org.prebid.server.bidder.model.BidderError; import org.prebid.server.bidder.model.HttpCall; @@ -231,6 +234,23 @@ public void makeBidsShouldReturnErrorIfImpIdsFromBidAndRequestWereNotMatchedAndI .containsOnly(BidderError.badServerResponse("Failed to find impression for ID: 123")); } + @Test + public void modifyImpShouldChangeImpExt() { + // given + final GridExtImp gridExtImp = GridExtImp.builder() + .data(GridExtImpData.of(null, GridExtImpDataAdServer.of("name", "adslot"))) + .build(); + + final Imp imp = Imp.builder().ext(mapper.valueToTree(gridExtImp)).build(); + + // when + final Imp modifiedImp = gridBidder.modifyImp(imp, ExtImpGrid.of(1)); + + // then + assertThat(mapper.convertValue(modifiedImp.getExt(), GridExtImp.class).getGpid()) + .isEqualTo(gridExtImp.getData().getAdServer().getAdSlot()); + } + private static BidResponse givenBidResponse(UnaryOperator bidResponseCustomizer, UnaryOperator bidCustomizer) { return bidResponseCustomizer.apply(BidResponse.builder() diff --git a/src/test/resources/org/prebid/server/it/openrtb2/grid/test-auction-grid-request.json b/src/test/resources/org/prebid/server/it/openrtb2/grid/test-auction-grid-request.json index 6ea2e260623..7f190baf03f 100644 --- a/src/test/resources/org/prebid/server/it/openrtb2/grid/test-auction-grid-request.json +++ b/src/test/resources/org/prebid/server/it/openrtb2/grid/test-auction-grid-request.json @@ -14,6 +14,12 @@ "ext": { "grid": { "uid": 1 + }, + "data": { + "adserver": { + "name": "some_name", + "adslot": "some_slot" + } } } } diff --git a/src/test/resources/org/prebid/server/it/openrtb2/grid/test-grid-bid-request-1.json b/src/test/resources/org/prebid/server/it/openrtb2/grid/test-grid-bid-request-1.json index c0c1a403cad..816d7a24b98 100644 --- a/src/test/resources/org/prebid/server/it/openrtb2/grid/test-grid-bid-request-1.json +++ b/src/test/resources/org/prebid/server/it/openrtb2/grid/test-grid-bid-request-1.json @@ -14,7 +14,14 @@ "ext": { "bidder": { "uid": 1 - } + }, + "data": { + "adserver": { + "name": "some_name", + "adslot": "some_slot" + } + }, + "gpid" : "some_slot" } } ],