diff --git a/src/main/java/org/prebid/server/bidder/pangle/PangleBidder.java b/src/main/java/org/prebid/server/bidder/pangle/PangleBidder.java new file mode 100644 index 00000000000..1c30a714ba6 --- /dev/null +++ b/src/main/java/org/prebid/server/bidder/pangle/PangleBidder.java @@ -0,0 +1,207 @@ +package org.prebid.server.bidder.pangle; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.iab.openrtb.request.BidRequest; +import com.iab.openrtb.request.Imp; +import com.iab.openrtb.response.Bid; +import com.iab.openrtb.response.BidResponse; +import com.iab.openrtb.response.SeatBid; +import io.vertx.core.MultiMap; +import io.vertx.core.http.HttpMethod; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.prebid.server.bidder.Bidder; +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.bidder.pangle.model.BidExt; +import org.prebid.server.bidder.pangle.model.PangleBidExt; +import org.prebid.server.bidder.pangle.model.WrappedImpExtBidder; +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.request.ExtImpPrebid; +import org.prebid.server.proto.openrtb.ext.response.BidType; +import org.prebid.server.util.HttpUtil; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + +/** + * Pangle {@link Bidder} implementation. + */ +public class PangleBidder implements Bidder { + + private final String endpointUrl; + private final JacksonMapper mapper; + + public PangleBidder(String endpointUrl, JacksonMapper mapper) { + this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl)); + this.mapper = Objects.requireNonNull(mapper); + } + + @Override + public Result>> makeHttpRequests(BidRequest request) { + final List> requests = new ArrayList<>(); + final List errors = new ArrayList<>(); + + for (Imp imp : request.getImp()) { + try { + final WrappedImpExtBidder extBidder = parseImpExt(imp); + final Integer adType = resolveAdType(imp, extBidder); + final Imp modifiedImp = modifyImp(imp, adType, extBidder); + + requests.add(createRequest(request, modifiedImp, extBidder.getBidder().getToken())); + } catch (PreBidException e) { + errors.add(BidderError.badInput(e.getMessage())); + } + } + + return Result.of(requests, errors); + } + + private WrappedImpExtBidder parseImpExt(Imp imp) { + try { + return mapper.mapper().convertValue(imp.getExt(), WrappedImpExtBidder.class); + } catch (IllegalArgumentException e) { + throw new PreBidException(String.format("failed unmarshalling imp ext (err)%s", e.getMessage())); + } + } + + private static int resolveAdType(Imp imp, WrappedImpExtBidder extBidder) { + if (imp.getVideo() != null) { + final ExtImpPrebid extPrebid = extBidder != null ? extBidder.getPrebid() : null; + final Integer isRewardedInventory = extPrebid != null ? extPrebid.getIsRewardedInventory() : null; + if (Objects.equals(isRewardedInventory, 1)) { + return 7; + } + + if (Objects.equals(imp.getInstl(), 1)) { + return 8; + } + } + + if (imp.getBanner() != null) { + if (Objects.equals(imp.getInstl(), 1)) { + return 2; + } else { + return 1; + } + } + if (imp.getXNative() != null && StringUtils.isNotBlank(imp.getXNative().getRequest())) { + return 5; + } + + throw new PreBidException("not a supported adtype"); + } + + private Imp modifyImp(Imp imp, Integer adType, WrappedImpExtBidder extBidder) { + final WrappedImpExtBidder updatedImpExt = extBidder.toBuilder().adType(adType).build(); + return imp.toBuilder() + .ext(mapper.mapper().convertValue(updatedImpExt, ObjectNode.class)) + .build(); + } + + private HttpRequest createRequest(BidRequest request, Imp imp, String token) { + final BidRequest outgoingRequest = request.toBuilder().imp(Collections.singletonList(imp)).build(); + + return HttpRequest.builder() + .method(HttpMethod.POST) + .uri(endpointUrl) + .headers(makeHeaders(token)) + .payload(outgoingRequest) + .body(mapper.encode(outgoingRequest)) + .build(); + } + + private static MultiMap makeHeaders(String token) { + return HttpUtil.headers() + .add("TOKEN", token); + } + + @Override + public final Result> makeBids(HttpCall httpCall, BidRequest bidRequest) { + try { + final List errors = new ArrayList<>(); + final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class); + return Result.of(extractBids(bidResponse, errors), errors); + } catch (DecodeException e) { + return Result.withError(BidderError.badServerResponse(e.getMessage())); + } + } + + private List extractBids(BidResponse bidResponse, List errors) { + if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) { + return Collections.emptyList(); + } + return bidsFromResponse(bidResponse, errors); + } + + private List bidsFromResponse(BidResponse bidResponse, List errors) { + return bidResponse.getSeatbid().stream() + .filter(Objects::nonNull) + .map(SeatBid::getBid) + .flatMap(Collection::stream) + .map(bid -> createBid(bid, bidResponse.getCur(), errors)) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + } + + private BidderBid createBid(Bid bid, String currency, List errors) { + final Integer adType; + try { + adType = getAdTypeFromBidExt(bid); + } catch (PreBidException e) { + errors.add(BidderError.badServerResponse(e.getMessage())); + return null; + } + final BidType bidType; + switch (adType) { + case 1: + bidType = BidType.banner; + break; + case 2: + bidType = BidType.banner; + break; + case 5: + bidType = BidType.xNative; + break; + case 7: + bidType = BidType.video; + break; + case 8: + bidType = BidType.video; + break; + default: + errors.add(BidderError.badServerResponse("unrecognized adtype in response")); + return null; + } + + return BidderBid.of(bid, bidType, currency); + } + + private Integer getAdTypeFromBidExt(Bid bid) { + if (bid == null) { + throw new PreBidException("the bid request object is not present"); + } + final PangleBidExt bidExt; + try { + bidExt = mapper.mapper().convertValue(bid.getExt(), PangleBidExt.class); + } catch (IllegalArgumentException e) { + throw new PreBidException("invalid bid ext"); + } + + final BidExt pangleBidExt = bidExt != null ? bidExt.getPangle() : null; + final Integer adType = pangleBidExt != null ? pangleBidExt.getAdType() : null; + if (adType == null) { + throw new PreBidException("missing pangleExt/adtype in bid ext"); + } + return adType; + } +} diff --git a/src/main/java/org/prebid/server/bidder/pangle/model/BidExt.java b/src/main/java/org/prebid/server/bidder/pangle/model/BidExt.java new file mode 100644 index 00000000000..ded899b2091 --- /dev/null +++ b/src/main/java/org/prebid/server/bidder/pangle/model/BidExt.java @@ -0,0 +1,15 @@ +package org.prebid.server.bidder.pangle.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Value; + +@AllArgsConstructor(staticName = "of") +@Builder(toBuilder = true) +@Value +public class BidExt { + + @JsonProperty("adtype") + Integer adType; +} diff --git a/src/main/java/org/prebid/server/bidder/pangle/model/PangleBidExt.java b/src/main/java/org/prebid/server/bidder/pangle/model/PangleBidExt.java new file mode 100644 index 00000000000..c85dd19b087 --- /dev/null +++ b/src/main/java/org/prebid/server/bidder/pangle/model/PangleBidExt.java @@ -0,0 +1,13 @@ +package org.prebid.server.bidder.pangle.model; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Value; + +@AllArgsConstructor(staticName = "of") +@Builder(toBuilder = true) +@Value +public class PangleBidExt { + + BidExt pangle; +} diff --git a/src/main/java/org/prebid/server/bidder/pangle/model/WrappedImpExtBidder.java b/src/main/java/org/prebid/server/bidder/pangle/model/WrappedImpExtBidder.java new file mode 100644 index 00000000000..833b88c9bb6 --- /dev/null +++ b/src/main/java/org/prebid/server/bidder/pangle/model/WrappedImpExtBidder.java @@ -0,0 +1,21 @@ +package org.prebid.server.bidder.pangle.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Value; +import org.prebid.server.proto.openrtb.ext.request.ExtImpPrebid; +import org.prebid.server.proto.openrtb.ext.request.pangle.ExtImpPangle; + +@AllArgsConstructor(staticName = "of") +@Builder(toBuilder = true) +@Value +public class WrappedImpExtBidder { + + ExtImpPrebid prebid; + + ExtImpPangle bidder; + + @JsonProperty("adtype") + Integer adType; +} diff --git a/src/main/java/org/prebid/server/proto/openrtb/ext/request/pangle/ExtImpPangle.java b/src/main/java/org/prebid/server/proto/openrtb/ext/request/pangle/ExtImpPangle.java new file mode 100644 index 00000000000..fcf02381406 --- /dev/null +++ b/src/main/java/org/prebid/server/proto/openrtb/ext/request/pangle/ExtImpPangle.java @@ -0,0 +1,11 @@ +package org.prebid.server.proto.openrtb.ext.request.pangle; + +import lombok.AllArgsConstructor; +import lombok.Value; + +@AllArgsConstructor(staticName = "of") +@Value +public class ExtImpPangle { + + String token; +} diff --git a/src/main/java/org/prebid/server/spring/config/bidder/PangleConfiguration.java b/src/main/java/org/prebid/server/spring/config/bidder/PangleConfiguration.java new file mode 100644 index 00000000000..26c04626faf --- /dev/null +++ b/src/main/java/org/prebid/server/spring/config/bidder/PangleConfiguration.java @@ -0,0 +1,51 @@ +package org.prebid.server.spring.config.bidder; + +import org.prebid.server.bidder.BidderDeps; +import org.prebid.server.bidder.pangle.PangleBidder; +import org.prebid.server.json.JacksonMapper; +import org.prebid.server.spring.config.bidder.model.BidderConfigurationProperties; +import org.prebid.server.spring.config.bidder.util.BidderDepsAssembler; +import org.prebid.server.spring.config.bidder.util.UsersyncerCreator; +import org.prebid.server.spring.env.YamlPropertySourceFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; + +import javax.validation.constraints.NotBlank; + +@Configuration +@PropertySource(value = "classpath:/bidder-config/pangle.yaml", factory = YamlPropertySourceFactory.class) +public class PangleConfiguration { + + private static final String BIDDER_NAME = "pangle"; + + @Value("${external-url}") + @NotBlank + private String externalUrl; + + @Autowired + private JacksonMapper mapper; + + @Autowired + @Qualifier("pangleConfigurationProperties") + private BidderConfigurationProperties configProperties; + + @Bean("pangleConfigurationProperties") + @ConfigurationProperties("adapters.pangle") + BidderConfigurationProperties configurationProperties() { + return new BidderConfigurationProperties(); + } + + @Bean + BidderDeps pangleBidderDeps() { + return BidderDepsAssembler.forBidder(BIDDER_NAME) + .withConfig(configProperties) + .usersyncerCreator(UsersyncerCreator.create(externalUrl)) + .bidderCreator(config -> new PangleBidder(config.getEndpoint(), mapper)) + .assemble(); + } +} diff --git a/src/main/resources/bidder-config/pangle.yaml b/src/main/resources/bidder-config/pangle.yaml new file mode 100644 index 00000000000..2447d2edbb7 --- /dev/null +++ b/src/main/resources/bidder-config/pangle.yaml @@ -0,0 +1,24 @@ +adapters: + pangle: + enabled: false + endpoint: https:// + pbs-enforces-gdpr: true + pbs-enforces-ccpa: true + modifying-vast-xml-allowed: true + deprecated-names: + aliases: {} + meta-info: + maintainer-email: pangle_dsp@bytedance.com + app-media-types: + - banner + - video + - native + site-media-types: + supported-vendors: + vendor-id: 0 + usersync: + url: + redirect-url: + cookie-family-name: pangle + type: redirect + support-cors: false diff --git a/src/main/resources/static/bidder-params/pangle.json b/src/main/resources/static/bidder-params/pangle.json new file mode 100644 index 00000000000..74085cb5e65 --- /dev/null +++ b/src/main/resources/static/bidder-params/pangle.json @@ -0,0 +1,16 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Pangle Adapter Params", + "description": "A schema which validates params accepted by the Pangle adapter", + "type": "object", + "properties": { + "token": { + "type": "string", + "description": "Access Token", + "pattern": ".+" + } + }, + "required": [ + "token" + ] +} diff --git a/src/test/java/org/prebid/server/bidder/pangle/PangleBidderTest.java b/src/test/java/org/prebid/server/bidder/pangle/PangleBidderTest.java new file mode 100644 index 00000000000..cbe95a016e5 --- /dev/null +++ b/src/test/java/org/prebid/server/bidder/pangle/PangleBidderTest.java @@ -0,0 +1,508 @@ +package org.prebid.server.bidder.pangle; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.iab.openrtb.request.Audio; +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; +import io.netty.handler.codec.http.HttpHeaderValues; +import org.junit.Before; +import org.junit.Test; +import org.prebid.server.VertxTest; +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.HttpResponse; +import org.prebid.server.bidder.model.Result; +import org.prebid.server.bidder.pangle.model.BidExt; +import org.prebid.server.bidder.pangle.model.PangleBidExt; +import org.prebid.server.bidder.pangle.model.WrappedImpExtBidder; +import org.prebid.server.proto.openrtb.ext.ExtPrebid; +import org.prebid.server.proto.openrtb.ext.request.ExtImpPrebid; +import org.prebid.server.proto.openrtb.ext.request.pangle.ExtImpPangle; +import org.prebid.server.util.HttpUtil; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.function.Function; + +import static java.util.Collections.singletonList; +import static java.util.function.Function.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; +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 PangleBidderTest extends VertxTest { + + public static final String ENDPOINT_URL = "https://test.endpoint.com"; + + private PangleBidder pangleBidder; + + @Before + public void setUp() { + pangleBidder = new PangleBidder(ENDPOINT_URL, jacksonMapper); + } + + @Test + public void creationShouldFailOnInvalidEndpointUrl() { + assertThatIllegalArgumentException().isThrownBy(() -> new PangleBidder("invalid_url", jacksonMapper)); + } + + @Test + public void makeHttpRequestsShouldMakeOneRequestPerImp() { + // given + final BidRequest bidRequest = givenBidRequest( + identity(), + requestBuilder -> requestBuilder.imp(Arrays.asList( + givenImp(identity()), + givenImp(identity())))); + + // when + final Result>> result = pangleBidder.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).hasSize(2) + .extracting(HttpRequest::getPayload) + .flatExtracting(BidRequest::getImp) + .hasSize(2); + } + + @Test + public void makeHttpRequestsShouldUpdateImpExtWithAdTypeSevenIfVideoIsPresentAndIsRewardedIsOne() { + // given + final ExtImpPrebid extPrebid = ExtImpPrebid.builder().isRewardedInventory(1).build(); + final BidRequest bidRequest = BidRequest.builder() + .imp(singletonList(Imp.builder() + .video(Video.builder().build()) + .ext(mapper.valueToTree(WrappedImpExtBidder.of(extPrebid, ExtImpPangle.of("token"), null))) + .build())) + .build(); + + // when + final Result>> result = pangleBidder.makeHttpRequests(bidRequest); + + // then + final ObjectNode expectedExt = mapper + .valueToTree(WrappedImpExtBidder.of(extPrebid, ExtImpPangle.of("token"), 7)); + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getPayload) + .flatExtracting(BidRequest::getImp) + .extracting(Imp::getExt) + .containsExactly(expectedExt); + } + + @Test + public void makeHttpRequestsShouldUpdateImpExtWithAdTypeEightIfVideoIsPresentAndInstlIsOne() { + // given + final BidRequest bidRequest = BidRequest.builder() + .imp(singletonList(Imp.builder() + .instl(1) + .video(Video.builder().build()) + .ext(mapper.valueToTree(WrappedImpExtBidder.of(null, ExtImpPangle.of("token"), null))) + .build())) + .build(); + + // when + final Result>> result = pangleBidder.makeHttpRequests(bidRequest); + + // then + final ObjectNode expectedExt = mapper + .valueToTree(WrappedImpExtBidder.of(null, ExtImpPangle.of("token"), 8)); + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getPayload) + .flatExtracting(BidRequest::getImp) + .extracting(Imp::getExt) + .containsExactly(expectedExt); + } + + @Test + public void makeHttpRequestsShouldUpdateImpExtWithAdTypeTwoIfBannerIsPresentAndInstlIsOne() { + // given + final BidRequest bidRequest = BidRequest.builder() + .imp(singletonList(Imp.builder() + .instl(1) + .banner(Banner.builder().build()) + .ext(mapper.valueToTree(WrappedImpExtBidder.of(null, ExtImpPangle.of("token"), null))) + .build())) + .build(); + + // when + final Result>> result = pangleBidder.makeHttpRequests(bidRequest); + + // then + final ObjectNode expectedExt = mapper + .valueToTree(WrappedImpExtBidder.of(null, ExtImpPangle.of("token"), 2)); + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getPayload) + .flatExtracting(BidRequest::getImp) + .extracting(Imp::getExt) + .containsExactly(expectedExt); + } + + @Test + public void makeHttpRequestsShouldUpdateImpExtWithAdTypeOneIfBannerIsPresent() { + // given + final BidRequest bidRequest = BidRequest.builder() + .imp(singletonList(Imp.builder() + .banner(Banner.builder().build()) + .ext(mapper.valueToTree(WrappedImpExtBidder.of(null, ExtImpPangle.of("token"), null))) + .build())) + .build(); + + // when + final Result>> result = pangleBidder.makeHttpRequests(bidRequest); + + // then + final ObjectNode expectedExt = mapper + .valueToTree(WrappedImpExtBidder.of(null, ExtImpPangle.of("token"), 1)); + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getPayload) + .flatExtracting(BidRequest::getImp) + .extracting(Imp::getExt) + .containsExactly(expectedExt); + } + + @Test + public void makeHttpRequestsShouldUpdateImpExtWithAdTypeFiveIfNativeRequestIsPresent() { + // given + final BidRequest bidRequest = BidRequest.builder() + .imp(singletonList(Imp.builder() + .xNative(Native.builder().request("someRequest").build()) + .ext(mapper.valueToTree(WrappedImpExtBidder.of(null, ExtImpPangle.of("token"), null))) + .build())) + .build(); + + // when + final Result>> result = pangleBidder.makeHttpRequests(bidRequest); + + // then + final ObjectNode expectedExt = mapper + .valueToTree(WrappedImpExtBidder.of(null, ExtImpPangle.of("token"), 5)); + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getPayload) + .flatExtracting(BidRequest::getImp) + .extracting(Imp::getExt) + .containsExactly(expectedExt); + } + + @Test + public void makeHttpRequestsShouldReturnErrorForNotSupportedAdType() { + // given + final BidRequest bidRequest = BidRequest.builder() + .imp(singletonList(Imp.builder() + .audio(Audio.builder().build()) + .ext(mapper.valueToTree(WrappedImpExtBidder.of(null, ExtImpPangle.of("token"), null))) + .build())) + .build(); + + // when + final Result>> result = pangleBidder.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).containsExactly(BidderError.badInput("not a supported adtype")); + assertThat(result.getValue()).isEmpty(); + } + + @Test + public void makeHttpRequestsShouldCorrectlyAddHeaders() { + // given + final BidRequest bidRequest = givenBidRequest(identity()); + + // when + final Result>> result = pangleBidder.makeHttpRequests(bidRequest); + + // then + assertThat(result.getValue()) + .flatExtracting(res -> res.getHeaders().entries()) + .extracting(Map.Entry::getKey, Map.Entry::getValue) + .containsExactlyInAnyOrder( + tuple(HttpUtil.CONTENT_TYPE_HEADER.toString(), HttpUtil.APPLICATION_JSON_CONTENT_TYPE), + tuple(HttpUtil.ACCEPT_HEADER.toString(), HttpHeaderValues.APPLICATION_JSON.toString()), + tuple("TOKEN", "token")); + } + + @Test + public void makeBidsShouldReturnEmptyListIfBidResponseIsNull() throws JsonProcessingException { + // given + final HttpCall httpCall = givenHttpCall(null, mapper.writeValueAsString(null)); + + // when + final Result> result = pangleBidder.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).isEmpty(); + } + + @Test + public void makeBidsShouldReturnEmptyListIfBidResponseSeatBidIsNull() throws JsonProcessingException { + // given + final HttpCall httpCall = givenHttpCall(null, + mapper.writeValueAsString(BidResponse.builder().build())); + + // when + final Result> result = pangleBidder.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).isEmpty(); + } + + @Test + public void makeBidsShouldReturnErrorIfResponseBodyCouldNotBeParsed() { + // given + final HttpCall httpCall = givenHttpCall(null, "invalid"); + + // when + final Result> result = pangleBidder.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).hasSize(1) + .allSatisfy(error -> { + assertThat(error.getType()).isEqualTo(BidderError.Type.bad_server_response); + assertThat(error.getMessage()).startsWith("Failed to decode: Unrecognized token"); + }); + assertThat(result.getValue()).isEmpty(); + } + + @Test + public void makeBidsShouldReturnErrorIfBidIsNull() throws JsonProcessingException { + // given + final HttpCall httpCall = givenHttpCall( + givenBidRequest(identity()), + mapper.writeValueAsString(BidResponse.builder() + .seatbid(singletonList(SeatBid.builder() + .bid(singletonList(null)) + .build())) + .build())); + + // when + final Result> result = pangleBidder.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()) + .containsExactly( + BidderError.badServerResponse("the bid request object is not present")); + } + + @Test + public void makeBidsShouldReturnErrorIfBidExtIsNull() throws JsonProcessingException { + // given + final HttpCall httpCall = givenHttpCall( + givenBidRequest(identity()), + mapper.writeValueAsString(givenBidResponse(identity()))); + + // when + final Result> result = pangleBidder.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()) + .containsExactly( + BidderError.badServerResponse("missing pangleExt/adtype in bid ext")); + } + + @Test + public void makeBidsShouldReturnErrorIfBidExtAdTypeIsNull() throws JsonProcessingException { + // given + final HttpCall httpCall = givenHttpCall( + givenBidRequest(identity()), + mapper.writeValueAsString( + givenBidResponse(bid -> bid.ext(mapper.valueToTree(PangleBidExt.of(BidExt.of(null))))))); + + // when + final Result> result = pangleBidder.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()) + .containsExactly( + BidderError.badServerResponse("missing pangleExt/adtype in bid ext")); + } + + @Test + public void makeBidsShouldReturnBannerBidIfAdTypeIsOne() throws JsonProcessingException { + // given + final HttpCall httpCall = givenHttpCall( + givenBidRequest(identity()), + mapper.writeValueAsString(givenBidResponse(bid -> + bid.ext(mapper.valueToTree(PangleBidExt.of(BidExt.of(1))))))); + + // when + final Result> result = pangleBidder.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .containsExactly(BidderBid.of(Bid.builder() + .ext(mapper.valueToTree(PangleBidExt.of(BidExt.of(1)))) + .build(), banner, "USD")); + } + + @Test + public void makeBidsShouldReturnBannerBidIfAdTypeIsTwo() throws JsonProcessingException { + // given + final HttpCall httpCall = givenHttpCall( + givenBidRequest(identity()), + mapper.writeValueAsString(givenBidResponse(bid -> + bid.ext(mapper.valueToTree(PangleBidExt.of(BidExt.of(2))))))); + + // when + final Result> result = pangleBidder.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .containsExactly(BidderBid.of(Bid.builder() + .ext(mapper.valueToTree(PangleBidExt.of(BidExt.of(2)))) + .build(), banner, "USD")); + } + + @Test + public void makeBidsShouldReturnNativeBidIfAdTypeIsFive() throws JsonProcessingException { + // given + final HttpCall httpCall = givenHttpCall( + givenBidRequest(identity()), + mapper.writeValueAsString(givenBidResponse(bid -> + bid.ext(mapper.valueToTree(PangleBidExt.of(BidExt.of(5))))))); + + // when + final Result> result = pangleBidder.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .containsExactly(BidderBid.of(Bid.builder() + .ext(mapper.valueToTree(PangleBidExt.of(BidExt.of(5)))) + .build(), xNative, "USD")); + } + + @Test + public void makeBidsShouldReturnVide0BidIfAdTypeIsSeven() throws JsonProcessingException { + // given + final HttpCall httpCall = givenHttpCall( + givenBidRequest(identity()), + mapper.writeValueAsString(givenBidResponse(bid -> + bid.ext(mapper.valueToTree(PangleBidExt.of(BidExt.of(7))))))); + + // when + final Result> result = pangleBidder.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .containsExactly(BidderBid.of(Bid.builder() + .ext(mapper.valueToTree(PangleBidExt.of(BidExt.of(7)))) + .build(), video, "USD")); + } + + @Test + public void makeBidsShouldReturnVide0BidIfAdTypeIsEight() throws JsonProcessingException { + // given + final HttpCall httpCall = givenHttpCall( + givenBidRequest(identity()), + mapper.writeValueAsString(givenBidResponse(bid -> + bid.ext(mapper.valueToTree(PangleBidExt.of(BidExt.of(8))))))); + + // when + final Result> result = pangleBidder.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .containsExactly(BidderBid.of(Bid.builder() + .ext(mapper.valueToTree(PangleBidExt.of(BidExt.of(8)))) + .build(), video, "USD")); + } + + @Test + public void makeBidsShouldReturnErrorForUnrecognizedAdType() throws JsonProcessingException { + // given + final HttpCall httpCall = givenHttpCall( + givenBidRequest(identity()), + mapper.writeValueAsString(givenBidResponse(bid -> + bid.ext(mapper.valueToTree(PangleBidExt.of(BidExt.of(777))))))); + + // when + final Result> result = pangleBidder.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()) + .containsExactly( + BidderError.badServerResponse("unrecognized adtype in response")); + } + + @Test + public void makeBidsShouldReturnErrorsAndResult() throws JsonProcessingException { + // given + final HttpCall httpCall = givenHttpCall( + givenBidRequest(identity()), + mapper.writeValueAsString(BidResponse.builder() + .cur("USD") + .seatbid(singletonList(SeatBid.builder() + .bid(Arrays.asList(Bid.builder().build(), + Bid.builder() + .ext(mapper.valueToTree(PangleBidExt.of(BidExt.of(8)))) + .build())) + .build())) + .build())); + + // when + final Result> result = pangleBidder.makeBids(httpCall, null); + + // then + assertThat(result.getValue()).hasSize(1); + assertThat(result.getErrors()).hasSize(1); + } + + private static BidRequest givenBidRequest( + Function impCustomizer, + Function requestCustomizer) { + return requestCustomizer.apply(BidRequest.builder() + .imp(singletonList(givenImp(impCustomizer)))) + .build(); + } + + private static BidRequest givenBidRequest( + Function impCustomizer) { + return givenBidRequest(impCustomizer, identity()); + } + + private static Imp givenImp(Function impCustomizer) { + return impCustomizer.apply(Imp.builder() + .id("123")) + .banner(Banner.builder().build()) + .ext(mapper.valueToTree(ExtPrebid.of(null, ExtImpPangle.of("token")))) + .build(); + } + + private static BidResponse givenBidResponse(Function bidCustomizer) { + return BidResponse.builder() + .cur("USD") + .seatbid(singletonList(SeatBid.builder() + .bid(singletonList(bidCustomizer.apply(Bid.builder()).build())) + .build())) + .build(); + } + + private static HttpCall givenHttpCall(BidRequest bidRequest, String body) { + return HttpCall.success(HttpRequest.builder().payload(bidRequest).build(), + HttpResponse.of(200, null, body), null); + } +} + diff --git a/src/test/java/org/prebid/server/it/PangleTest.java b/src/test/java/org/prebid/server/it/PangleTest.java new file mode 100644 index 00000000000..f9bc8acd738 --- /dev/null +++ b/src/test/java/org/prebid/server/it/PangleTest.java @@ -0,0 +1,57 @@ +package org.prebid.server.it; + +import io.restassured.response.Response; +import org.json.JSONException; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.skyscreamer.jsonassert.JSONAssert; +import org.skyscreamer.jsonassert.JSONCompareMode; +import org.springframework.test.context.junit4.SpringRunner; + +import java.io.IOException; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson; +import static com.github.tomakehurst.wiremock.client.WireMock.post; +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; +import static io.restassured.RestAssured.given; +import static java.util.Collections.singletonList; + +@RunWith(SpringRunner.class) +public class PangleTest extends IntegrationTest { + + @Test + public void openrtb2AuctionShouldRespondWithBidsFromPangle() throws IOException, JSONException { + // given + // Pangle bid response for imp 001 + WIRE_MOCK_RULE.stubFor(post(urlPathEqualTo("/pangle-exchange")) + .withRequestBody(equalToJson(jsonFrom("openrtb2/pangle/test-pangle-bid-request-1.json"))) + .willReturn(aResponse().withBody(jsonFrom("openrtb2/pangle/test-pangle-bid-response-1.json")))); + + // pre-bid cache + WIRE_MOCK_RULE.stubFor(post(urlPathEqualTo("/cache")) + .withRequestBody(equalToBidCacheRequest( + jsonFrom("openrtb2/pangle/test-cache-pangle-request.json"))) + .willReturn(aResponse() + .withTransformers("cache-response-transformer") + .withTransformerParameter("matcherName", "openrtb2/pangle/test-cache-matcher-pangle.json"))); + + // when + final Response response = given(SPEC) + .header("Referer", "http://www.example.com") + .header("X-Forwarded-For", "193.168.244.1") + .header("User-Agent", "userAgent") + .header("Origin", "http://www.example.com") + // this uids cookie value stands for {"uids":{"pangle":"PG-UID"}} + .cookie("uids", "eyJ1aWRzIjp7InBhbmdsZSI6IlBHLVVJRCJ9fQ==") + .body(jsonFrom("openrtb2/pangle/test-auction-pangle-request.json")) + .post("/openrtb2/auction"); + + // then + final String expectedAuctionResponse = openrtbAuctionResponseFrom( + "openrtb2/pangle/test-auction-pangle-response.json", + response, singletonList("pangle")); + + JSONAssert.assertEquals(expectedAuctionResponse, response.asString(), JSONCompareMode.NON_EXTENSIBLE); + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/pangle/test-auction-pangle-request.json b/src/test/resources/org/prebid/server/it/openrtb2/pangle/test-auction-pangle-request.json new file mode 100644 index 00000000000..78aad361c2b --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/pangle/test-auction-pangle-request.json @@ -0,0 +1,77 @@ +{ + "id": "tid", + "imp": [ + { + "id": "impId001", + "banner": { + "format": [ + { + "w": 300, + "h": 250 + } + ] + }, + "ext": { + "prebid": { + "bidder": { + "pangle": { + "token": ".token" + } + } + } + } + } + ], + "device": { + "pxratio": 4.2, + "dnt": 2, + "language": "en", + "ifa": "ifaId" + }, + "site": { + "publisher": { + "id": "publisherId" + } + }, + "at": 1, + "tmax": 5000, + "cur": [ + "USD" + ], + "source": { + "fd": 1, + "tid": "tid" + }, + "user": { + "ext": { + "consent": "consentValue" + } + }, + "regs": { + "ext": { + "gdpr": 0 + } + }, + "ext": { + "prebid": { + "targeting": { + "pricegranularity": { + "precision": 2, + "ranges": [ + { + "max": 20, + "increment": 0.1 + } + ] + } + }, + "cache": { + "bids": {}, + "vastxml": { + "ttlseconds": 120 + } + }, + "auctiontimestamp": 1000 + } + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/pangle/test-auction-pangle-response.json b/src/test/resources/org/prebid/server/it/openrtb2/pangle/test-auction-pangle-response.json new file mode 100644 index 00000000000..79a23244932 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/pangle/test-auction-pangle-response.json @@ -0,0 +1,62 @@ +{ + "id": "tid", + "seatbid": [ + { + "bid": [ + { + "id": "bid001", + "impid": "impId001", + "price": 1.25, + "adm": "adm001", + "crid": "crid001", + "w": 300, + "h": 250, + "ext": { + "prebid": { + "type": "banner", + "targeting": { + "hb_pb": "1.20", + "hb_cache_id_pangle": "9092799c-93b0-4e11-a232-2c0151d5d275", + "hb_cache_path_pangle": "{{ cache.path }}", + "hb_cache_path": "{{ cache.path }}", + "hb_pb_pangle": "1.20", + "hb_size": "300x250", + "hb_bidder_pangle": "pangle", + "hb_size_pangle": "300x250", + "hb_bidder": "pangle", + "hb_cache_id": "9092799c-93b0-4e11-a232-2c0151d5d275", + "hb_cache_host": "{{ cache.host }}", + "hb_cache_host_pangle": "{{ cache.host }}" + }, + "cache": { + "bids": { + "url": "{{ cache.resource_url }}9092799c-93b0-4e11-a232-2c0151d5d275", + "cacheId": "9092799c-93b0-4e11-a232-2c0151d5d275" + } + } + }, + "bidder": { + "pangle": { + "adtype": 1 + } + }, + "origbidcpm" : 1.25 + } + } + ], + "seat": "pangle", + "group": 0 + } + ], + "cur": "USD", + "ext": { + "responsetimemillis": { + "pangle": "{{ pangle.response_time_ms }}", + "cache": "{{ cache.response_time_ms }}" + }, + "prebid": { + "auctiontimestamp": 1000 + }, + "tmaxrequest": 5000 + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/pangle/test-cache-matcher-pangle.json b/src/test/resources/org/prebid/server/it/openrtb2/pangle/test-cache-matcher-pangle.json new file mode 100644 index 00000000000..e438661be9f --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/pangle/test-cache-matcher-pangle.json @@ -0,0 +1,5 @@ +{ + "bid001@1.25": "9092799c-93b0-4e11-a232-2c0151d5d275", + "bid01@2.25": "83cdc325-c816-4d2e-bf2c-9213a70671dd", + "adm002": "99dc3357-34ac-4819-9f68-0820039a542f" +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/pangle/test-cache-pangle-request.json b/src/test/resources/org/prebid/server/it/openrtb2/pangle/test-cache-pangle-request.json new file mode 100644 index 00000000000..952d0e304a7 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/pangle/test-cache-pangle-request.json @@ -0,0 +1,22 @@ +{ + "puts": [ + { + "type": "json", + "value": { + "id": "bid001", + "impid": "impId001", + "price": 1.25, + "adm": "adm001", + "crid": "crid001", + "w": 300, + "h": 250, + "ext": { + "origbidcpm" : 1.25, + "pangle": { + "adtype": 1 + } + } + } + } + ] +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/pangle/test-pangle-bid-request-1.json b/src/test/resources/org/prebid/server/it/openrtb2/pangle/test-pangle-bid-request-1.json new file mode 100644 index 00000000000..84c73982dee --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/pangle/test-pangle-bid-request-1.json @@ -0,0 +1,87 @@ +{ + "id": "tid", + "imp": [ + { + "id": "impId001", + "banner": { + "format": [ + { + "w": 300, + "h": 250 + } + ] + }, + "ext": { + "bidder": { + "token": ".token" + }, + "adtype" : 1 + } + } + ], + "site": { + "domain": "example.com", + "page": "http://www.example.com", + "publisher": { + "id": "publisherId" + }, + "ext": { + "amp": 0 + } + }, + "device": { + "ua": "userAgent", + "dnt": 2, + "ip": "193.168.244.1", + "pxratio": 4.2, + "language": "en", + "ifa": "ifaId" + }, + "user": { + "buyeruid": "PG-UID", + "ext": { + "consent": "consentValue" + } + }, + "at": 1, + "tmax": 5000, + "cur": [ + "USD" + ], + "source": { + "fd": 1, + "tid": "tid" + }, + "regs": { + "ext": { + "gdpr": 0 + } + }, + "ext": { + "prebid": { + "targeting": { + "pricegranularity": { + "precision": 2, + "ranges": [ + { + "max": 20, + "increment": 0.1 + } + ] + }, + "includewinners": true, + "includebidderkeys": true + }, + "cache": { + "bids": {}, + "vastxml": { + "ttlseconds": 120 + } + }, + "auctiontimestamp": 1000, + "channel": { + "name": "web" + } + } + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/pangle/test-pangle-bid-response-1.json b/src/test/resources/org/prebid/server/it/openrtb2/pangle/test-pangle-bid-response-1.json new file mode 100644 index 00000000000..caa03582698 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/pangle/test-pangle-bid-response-1.json @@ -0,0 +1,24 @@ +{ + "id": "tid", + "seatbid": [ + { + "bid": [ + { + "id": "bid001", + "impid": "impId001", + "price": 1.25, + "crid": "crid001", + "adm": "adm001", + "h": 250, + "w": 300, + "ext": { + "pangle": { + "adtype": 1 + } + } + } + ] + } + ], + "bidid": "bid001" +} diff --git a/src/test/resources/org/prebid/server/it/test-application.properties b/src/test/resources/org/prebid/server/it/test-application.properties index 90ca93f5f79..bd127905db6 100644 --- a/src/test/resources/org/prebid/server/it/test-application.properties +++ b/src/test/resources/org/prebid/server/it/test-application.properties @@ -241,6 +241,10 @@ adapters.orbidder.enabled=true adapters.orbidder.endpoint=http://localhost:8090/orbidder-exchange adapters.orbidder.pbs-enforces-gdpr=true adapters.orbidder.usersync.url=//orbidder-usersync +adapters.pangle.enabled=true +adapters.pangle.endpoint=http://localhost:8090/pangle-exchange +adapters.pangle.pbs-enforces-gdpr=true +adapters.pangle.usersync.url=//pangle-usersync adapters.pubmatic.enabled=true adapters.pubmatic.endpoint=http://localhost:8090/pubmatic-exchange adapters.pubmatic.pbs-enforces-gdpr=true