From 9e78c2cb739be78a00a52d83703845d68d8922f6 Mon Sep 17 00:00:00 2001 From: Viktor Kryshtal Date: Fri, 5 Dec 2025 15:13:02 +0200 Subject: [PATCH 1/2] Add msft bidder --- .../prebid/server/bidder/msft/MsftBidder.java | 403 ++++++++ .../bidder/msft/proto/ExtRequestMsft.java | 17 + .../server/bidder/msft/proto/MsftBidExt.java | 19 + .../bidder/msft/proto/MsftBidExtCreative.java | 9 + .../bidder/msft/proto/MsftBidExtVideo.java | 9 + .../bidder/msft/proto/MsftExtImpOutgoing.java | 25 + .../openrtb/ext/request/msft/ExtImpMsft.java | 33 + .../config/bidder/MsftConfiguration.java | 67 ++ src/main/resources/bidder-config/msft.yaml | 118 +++ .../resources/static/bidder-params/msft.json | 68 ++ .../server/bidder/msft/MsftBidderTest.java | 894 ++++++++++++++++++ .../java/org/prebid/server/it/MsftTest.java | 33 + .../msft/test-auction-msft-request.json | 23 + .../msft/test-auction-msft-response.json | 41 + .../openrtb2/msft/test-msft-bid-request.json | 61 ++ .../openrtb2/msft/test-msft-bid-response.json | 21 + .../server/it/test-application.properties | 2 + 17 files changed, 1843 insertions(+) create mode 100644 src/main/java/org/prebid/server/bidder/msft/MsftBidder.java create mode 100644 src/main/java/org/prebid/server/bidder/msft/proto/ExtRequestMsft.java create mode 100644 src/main/java/org/prebid/server/bidder/msft/proto/MsftBidExt.java create mode 100644 src/main/java/org/prebid/server/bidder/msft/proto/MsftBidExtCreative.java create mode 100644 src/main/java/org/prebid/server/bidder/msft/proto/MsftBidExtVideo.java create mode 100644 src/main/java/org/prebid/server/bidder/msft/proto/MsftExtImpOutgoing.java create mode 100644 src/main/java/org/prebid/server/proto/openrtb/ext/request/msft/ExtImpMsft.java create mode 100644 src/main/java/org/prebid/server/spring/config/bidder/MsftConfiguration.java create mode 100644 src/main/resources/bidder-config/msft.yaml create mode 100644 src/main/resources/static/bidder-params/msft.json create mode 100644 src/test/java/org/prebid/server/bidder/msft/MsftBidderTest.java create mode 100644 src/test/java/org/prebid/server/it/MsftTest.java create mode 100644 src/test/resources/org/prebid/server/it/openrtb2/msft/test-auction-msft-request.json create mode 100644 src/test/resources/org/prebid/server/it/openrtb2/msft/test-auction-msft-response.json create mode 100644 src/test/resources/org/prebid/server/it/openrtb2/msft/test-msft-bid-request.json create mode 100644 src/test/resources/org/prebid/server/it/openrtb2/msft/test-msft-bid-response.json diff --git a/src/main/java/org/prebid/server/bidder/msft/MsftBidder.java b/src/main/java/org/prebid/server/bidder/msft/MsftBidder.java new file mode 100644 index 00000000000..7af5ecd3219 --- /dev/null +++ b/src/main/java/org/prebid/server/bidder/msft/MsftBidder.java @@ -0,0 +1,403 @@ +package org.prebid.server.bidder.msft; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.iab.openrtb.request.App; +import com.iab.openrtb.request.Banner; +import com.iab.openrtb.request.BidRequest; +import com.iab.openrtb.request.Format; +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 org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.collections4.ListUtils; +import org.apache.commons.lang3.BooleanUtils; +import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.http.client.utils.URIBuilder; +import org.prebid.server.auction.model.Endpoint; +import org.prebid.server.bidder.Bidder; +import org.prebid.server.bidder.appnexus.SameValueValidator; +import org.prebid.server.bidder.model.BidderBid; +import org.prebid.server.bidder.model.BidderCall; +import org.prebid.server.bidder.model.BidderError; +import org.prebid.server.bidder.model.HttpRequest; +import org.prebid.server.bidder.model.Result; +import org.prebid.server.bidder.msft.proto.MsftBidExt; +import org.prebid.server.bidder.msft.proto.MsftBidExtCreative; +import org.prebid.server.bidder.msft.proto.MsftBidExtVideo; +import org.prebid.server.bidder.msft.proto.MsftExtImpOutgoing; +import org.prebid.server.bidder.msft.proto.ExtRequestMsft; +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.ExtPrebid; +import org.prebid.server.proto.openrtb.ext.request.ExtApp; +import org.prebid.server.proto.openrtb.ext.request.ExtAppPrebid; +import org.prebid.server.proto.openrtb.ext.request.ExtRequest; +import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebid; +import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebidServer; +import org.prebid.server.proto.openrtb.ext.request.ExtRequestTargeting; +import org.prebid.server.proto.openrtb.ext.request.msft.ExtImpMsft; +import org.prebid.server.proto.openrtb.ext.response.BidType; +import org.prebid.server.proto.openrtb.ext.response.ExtBidPrebidVideo; +import org.prebid.server.util.BidderUtil; +import org.prebid.server.util.HttpUtil; +import org.prebid.server.util.ObjectUtil; + +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.stream.Stream; + +public class MsftBidder implements Bidder { + + private static final int MAX_IMPS_PER_REQUEST = 10; + private static final String OPENRTB_VERSION = "2.6"; + + private static final TypeReference> MSFT_EXT_TYPE_REFERENCE = + new TypeReference<>() { + }; + + private final String endpointUrl; + private final int hbSource; + private final int hbSourceVideo; + private final Map iabCategories; + private final JacksonMapper mapper; + + public MsftBidder(String endpointUrl, + int hbSource, + int hbSourceVideo, + Map iabCategories, + JacksonMapper mapper) { + this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl)); + this.hbSource = hbSource; + this.hbSourceVideo = hbSourceVideo; + this.iabCategories = ObjectUtils.defaultIfNull(iabCategories, Collections.emptyMap()); + this.mapper = Objects.requireNonNull(mapper); + } + + @Override + public Result>> makeHttpRequests(BidRequest bidRequest) { + final String defaultDisplayManagerVer = defaultDisplayManagerVer(bidRequest); + final SameValueValidator memberIdValidator = SameValueValidator.create(); + final List errors = new ArrayList<>(); + final List updatedImps = new ArrayList<>(); + + for (Imp imp : bidRequest.getImp()) { + try { + final ExtImpMsft extImp = parseImpExt(imp); + + final Integer memberId = extImp.getMember(); + if (memberId != null && memberIdValidator.isInvalid(memberId)) { + errors.add(BidderError.badInput("Member id mismatch:" + + " all impressions must use the same member id but found two different ids: %s and %s" + .formatted(memberIdValidator.getValue(), memberId))); + return Result.withErrors(errors); + } + + updatedImps.add(updateImp(imp, extImp, defaultDisplayManagerVer)); + } catch (PreBidException e) { + errors.add(BidderError.badInput(e.getMessage())); + } + } + + if (updatedImps.isEmpty()) { + return Result.withErrors(errors); + } + + final String requestUrl; + final BidRequest updatedBidRequest; + try { + requestUrl = makeRequestUrl(memberIdValidator.getValue()); + updatedBidRequest = updateBidRequest(bidRequest); + } catch (PreBidException e) { + errors.add(BidderError.badInput(e.getMessage())); + return Result.withErrors(errors); + } + + return Result.of(splitHttpRequests(updatedBidRequest, updatedImps, requestUrl), errors); + } + + private String defaultDisplayManagerVer(BidRequest request) { + final Optional prebid = Optional.ofNullable(request.getApp()) + .map(App::getExt) + .map(ExtApp::getPrebid); + + final String source = prebid.map(ExtAppPrebid::getSource).orElse(null); + final String version = prebid.map(ExtAppPrebid::getVersion).orElse(null); + + return ObjectUtils.allNotNull(source, version) + ? "%s-%s".formatted(source, version) + : StringUtils.EMPTY; + } + + private ExtImpMsft parseImpExt(Imp imp) { + try { + return mapper.mapper().convertValue(imp.getExt(), MSFT_EXT_TYPE_REFERENCE).getBidder(); + } catch (IllegalArgumentException e) { + throw new PreBidException("Failed to deserialize Microsoft imp extension: " + e.getMessage()); + } + } + + private Imp updateImp(Imp imp, ExtImpMsft extImp, String defaultDisplayManagerVer) { + final Imp.ImpBuilder impBuilder = imp.toBuilder(); + + if (StringUtils.isNotEmpty(extImp.getInvCode())) { + impBuilder.tagid(extImp.getInvCode()); + } + + if (imp.getBanner() != null) { + impBuilder.banner(updateBanner(imp.getBanner(), extImp)); + } + + if (StringUtils.isEmpty(imp.getDisplaymanagerver())) { + impBuilder.displaymanagerver(defaultDisplayManagerVer); + } + + impBuilder.ext(updateImpExt(imp, extImp)); + + return impBuilder.build(); + } + + private Banner updateBanner(Banner banner, ExtImpMsft extImp) { + final Banner.BannerBuilder bannerBuilder = banner.toBuilder(); + + final List bannerFormat = banner.getFormat(); + if (banner.getW() == null && banner.getH() == null && CollectionUtils.isNotEmpty(bannerFormat)) { + final Format firstFormat = bannerFormat.getFirst(); + bannerBuilder.w(firstFormat.getW()); + bannerBuilder.h(firstFormat.getH()); + } + + if (banner.getApi() == null) { + bannerBuilder.api(extImp.getBannerFrameworks()); + } + + return bannerBuilder.build(); + } + + private ObjectNode updateImpExt(Imp imp, ExtImpMsft extImp) { + final ObjectNode updatedImpExt = mapper.mapper().createObjectNode() + .set("appnexus", mapper.mapper().valueToTree( + MsftExtImpOutgoing.builder() + .placementId(extImp.getPlacementId()) + .allowSmallerSizes(extImp.getAllowSmallerSizes()) + .usePmtRule(extImp.getUsePmtRule()) + .keywords(extImp.getKeywords()) + .trafficSourceCode(extImp.getTrafficSourceCode()) + .pubClick(extImp.getPubclick()) + .extInvCode(extImp.getExtInvCode()) + .extImpId(extImp.getExtImpId()) + .build())); + + final String gpid = ObjectUtil.getIfNotNull(imp.getExt().get("gpid"), JsonNode::textValue); + if (StringUtils.isNotEmpty(gpid)) { + updatedImpExt.put("gpid", gpid); + } + + return updatedImpExt; + } + + private String makeRequestUrl(Integer member) { + try { + return member != null + ? new URIBuilder(endpointUrl).addParameter("member_id", member.toString()).build().toString() + : endpointUrl; + } catch (URISyntaxException e) { + throw new PreBidException(e.getMessage()); + } + } + + private BidRequest updateBidRequest(BidRequest bidRequest) { + return bidRequest.toBuilder() + .ext(updateRequestExt(bidRequest.getExt())) + .build(); + } + + private ExtRequest updateRequestExt(ExtRequest requestExt) { + final ExtRequest updatedRequestExt = copyRequestExt(requestExt); + updatedRequestExt.addProperty("appnexus", updateRequestExtMsft(requestExt, parseRequestExt(requestExt))); + return updatedRequestExt; + } + + private ExtRequest copyRequestExt(ExtRequest requestExt) { + if (requestExt == null) { + return ExtRequest.empty(); + } + + final ExtRequest newRequestExt = ExtRequest.of(requestExt.getPrebid()); + mapper.fillExtension(newRequestExt, requestExt); + return newRequestExt; + } + + private ExtRequestMsft parseRequestExt(ExtRequest requestExt) { + return Optional.ofNullable(requestExt) + .map(ext -> ext.getProperty("appnexus")) + .map(this::parseRequestExtMsft) + .orElse(null); + } + + private ExtRequestMsft parseRequestExtMsft(JsonNode extRequestMsftRaw) { + try { + return mapper.mapper().treeToValue(extRequestMsftRaw, ExtRequestMsft.class); + } catch (IllegalArgumentException | JsonProcessingException e) { + throw new PreBidException("Failed to deserialize Microsoft bid request extension: " + e.getMessage()); + } + } + + private JsonNode updateRequestExtMsft(ExtRequest requestExt, ExtRequestMsft requestExtMsft) { + final String endpointUrl = extractEndpointUrl(requestExt); + + final ExtRequestMsft.ExtRequestMsftBuilder updatedRequestExtMsftBuilder = Optional.ofNullable(requestExtMsft) + .map(ExtRequestMsft::toBuilder) + .orElseGet(ExtRequestMsft::builder) + .isAmp(BooleanUtils.toInteger(StringUtils.equals(endpointUrl, Endpoint.openrtb2_amp.value()))) + .hbSource(StringUtils.equals(endpointUrl, Endpoint.openrtb2_video.value()) ? hbSourceVideo : hbSource); + + Optional.ofNullable(requestExt) + .map(ExtRequest::getPrebid) + .map(ExtRequestPrebid::getTargeting) + .map(ExtRequestTargeting::getIncludebrandcategory) + .ifPresent(v -> { + updatedRequestExtMsftBuilder.brandCategoryUniqueness(true); + updatedRequestExtMsftBuilder.includeBrandCategory(true); + }); + + return mapper.mapper().valueToTree(updatedRequestExtMsftBuilder.build()); + } + + private String extractEndpointUrl(ExtRequest requestExt) { + return Optional.ofNullable(requestExt) + .map(ExtRequest::getPrebid) + .map(ExtRequestPrebid::getServer) + .map(ExtRequestPrebidServer::getEndpoint) + .orElse(null); + } + + private List> splitHttpRequests(BidRequest bidRequest, + List imps, + String requestUrl) { + return ListUtils.partition(imps, MAX_IMPS_PER_REQUEST) + .stream() + .map(impsForRequest -> makeHttpRequest(bidRequest, impsForRequest, requestUrl)) + .toList(); + } + + private HttpRequest makeHttpRequest(BidRequest bidRequest, List imps, String requestUrl) { + return BidderUtil.defaultRequest( + bidRequest.toBuilder().imp(imps).build(), + HttpUtil.headers().add(HttpUtil.X_OPENRTB_VERSION_HEADER, OPENRTB_VERSION), + requestUrl, + mapper); + } + + @Override + public Result> makeBids(BidderCall httpCall, BidRequest bidRequest) { + final BidResponse bidResponse; + try { + bidResponse = decodeBodyToBidResponse(httpCall); + } catch (PreBidException e) { + return Result.withError(BidderError.badServerResponse(e.getMessage())); + } + + return bidsFromResponse(bidResponse); + } + + private BidResponse decodeBodyToBidResponse(BidderCall httpCall) { + try { + return mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class); + } catch (DecodeException e) { + throw new PreBidException("Failed to parse response as BidResponse: " + e.getMessage()); + } + } + + private Result> bidsFromResponse(BidResponse bidResponse) { + final List errors = new ArrayList<>(); + final List bidderBids = Stream.ofNullable(bidResponse) + .map(BidResponse::getSeatbid) + .filter(Objects::nonNull) + .flatMap(Collection::stream) + .filter(Objects::nonNull) + .map(SeatBid::getBid) + .filter(Objects::nonNull) + .flatMap(Collection::stream) + .filter(Objects::nonNull) + .map(bid -> makeBid(bid, bidResponse, errors)) + .filter(Objects::nonNull) + .toList(); + + if (bidderBids.isEmpty()) { + errors.add(BidderError.badServerResponse("No valid bids found in response")); + } + + return Result.of(bidderBids, errors); + } + + private BidderBid makeBid(Bid bid, BidResponse bidResponse, List errors) { + try { + final MsftBidExt extBidMsft = Optional.ofNullable(bid.getExt()) + .map(ext -> ext.get("appnexus")) + .map(this::parseExtBidMsft) + .orElseThrow(() -> new PreBidException("Missing Microsoft bid extension")); + + return BidderBid.builder() + .bid(bid.toBuilder().cat(getBidCategories(bid, extBidMsft)).build()) + .type(getBidType(extBidMsft)) + .bidCurrency(bidResponse.getCur()) + .dealPriority(extBidMsft.getDealPriority()) + .videoInfo(makeVideoInfo(extBidMsft)) + .build(); + } catch (PreBidException e) { + errors.add(BidderError.badInput(e.getMessage())); + return null; + } + } + + private MsftBidExt parseExtBidMsft(JsonNode extBidMsftRaw) { + try { + return mapper.mapper().treeToValue(extBidMsftRaw, MsftBidExt.class); + } catch (IllegalArgumentException | JsonProcessingException e) { + throw new PreBidException("Failed to deserialize Microsoft bid extension: " + e.getMessage()); + } + } + + private List getBidCategories(Bid bid, MsftBidExt bidExtMsft) { + return Optional.ofNullable(bidExtMsft.getBrandCategoryId()) + .map(iabCategories::get) + .map(Collections::singletonList) + .orElseGet(() -> { + final List cat = bid.getCat(); + // create empty categories array to force bid to be rejected + return cat != null && cat.size() > 1 ? Collections.emptyList() : cat; + }); + } + + private BidType getBidType(MsftBidExt extBidMsft) { + final int bidAdType = extBidMsft.getBidAdType(); + return switch (bidAdType) { + case 0 -> BidType.banner; + case 1 -> BidType.video; + case 3 -> BidType.xNative; + default -> throw new PreBidException("Unsupported bid ad type: " + bidAdType); + }; + } + + private static ExtBidPrebidVideo makeVideoInfo(MsftBidExt extBidMsft) { + final int duration = Optional.ofNullable(extBidMsft) + .map(MsftBidExt::getCreativeInfo) + .map(MsftBidExtCreative::getVideo) + .map(MsftBidExtVideo::getDuration) + .orElse(0); + + return ExtBidPrebidVideo.of(duration, null); + } +} diff --git a/src/main/java/org/prebid/server/bidder/msft/proto/ExtRequestMsft.java b/src/main/java/org/prebid/server/bidder/msft/proto/ExtRequestMsft.java new file mode 100644 index 00000000000..7e5bc780c6e --- /dev/null +++ b/src/main/java/org/prebid/server/bidder/msft/proto/ExtRequestMsft.java @@ -0,0 +1,17 @@ +package org.prebid.server.bidder.msft.proto; + +import lombok.Builder; +import lombok.Value; + +@Value +@Builder(toBuilder = true) +public class ExtRequestMsft { + + Boolean includeBrandCategory; + + Boolean brandCategoryUniqueness; + + Integer isAmp; + + Integer hbSource; +} diff --git a/src/main/java/org/prebid/server/bidder/msft/proto/MsftBidExt.java b/src/main/java/org/prebid/server/bidder/msft/proto/MsftBidExt.java new file mode 100644 index 00000000000..68a6f3262a0 --- /dev/null +++ b/src/main/java/org/prebid/server/bidder/msft/proto/MsftBidExt.java @@ -0,0 +1,19 @@ +package org.prebid.server.bidder.msft.proto; + +import lombok.Builder; +import lombok.Value; + +@Value +@Builder +public class MsftBidExt { + + int bidAdType; + + Integer brandId; + + Integer brandCategoryId; + + MsftBidExtCreative creativeInfo; + + int dealPriority; +} diff --git a/src/main/java/org/prebid/server/bidder/msft/proto/MsftBidExtCreative.java b/src/main/java/org/prebid/server/bidder/msft/proto/MsftBidExtCreative.java new file mode 100644 index 00000000000..b944fc643dd --- /dev/null +++ b/src/main/java/org/prebid/server/bidder/msft/proto/MsftBidExtCreative.java @@ -0,0 +1,9 @@ +package org.prebid.server.bidder.msft.proto; + +import lombok.Value; + +@Value(staticConstructor = "of") +public class MsftBidExtCreative { + + MsftBidExtVideo video; +} diff --git a/src/main/java/org/prebid/server/bidder/msft/proto/MsftBidExtVideo.java b/src/main/java/org/prebid/server/bidder/msft/proto/MsftBidExtVideo.java new file mode 100644 index 00000000000..991d6d0db65 --- /dev/null +++ b/src/main/java/org/prebid/server/bidder/msft/proto/MsftBidExtVideo.java @@ -0,0 +1,9 @@ +package org.prebid.server.bidder.msft.proto; + +import lombok.Value; + +@Value(staticConstructor = "of") +public class MsftBidExtVideo { + + Integer duration; +} diff --git a/src/main/java/org/prebid/server/bidder/msft/proto/MsftExtImpOutgoing.java b/src/main/java/org/prebid/server/bidder/msft/proto/MsftExtImpOutgoing.java new file mode 100644 index 00000000000..16a987cde44 --- /dev/null +++ b/src/main/java/org/prebid/server/bidder/msft/proto/MsftExtImpOutgoing.java @@ -0,0 +1,25 @@ +package org.prebid.server.bidder.msft.proto; + +import lombok.Builder; +import lombok.Value; + +@Value +@Builder +public class MsftExtImpOutgoing { + + Integer placementId; + + Boolean allowSmallerSizes; + + Boolean usePmtRule; + + String keywords; + + String trafficSourceCode; + + String pubClick; + + String extInvCode; + + String extImpId; +} diff --git a/src/main/java/org/prebid/server/proto/openrtb/ext/request/msft/ExtImpMsft.java b/src/main/java/org/prebid/server/proto/openrtb/ext/request/msft/ExtImpMsft.java new file mode 100644 index 00000000000..146384e16b2 --- /dev/null +++ b/src/main/java/org/prebid/server/proto/openrtb/ext/request/msft/ExtImpMsft.java @@ -0,0 +1,33 @@ +package org.prebid.server.proto.openrtb.ext.request.msft; + +import lombok.Builder; +import lombok.Value; + +import java.util.List; + +@Value +@Builder +public class ExtImpMsft { + + Integer placementId; + + Integer member; + + String invCode; + + Boolean allowSmallerSizes; + + Boolean usePmtRule; + + String keywords; + + String trafficSourceCode; + + String pubclick; + + String extInvCode; + + String extImpId; + + List bannerFrameworks; +} diff --git a/src/main/java/org/prebid/server/spring/config/bidder/MsftConfiguration.java b/src/main/java/org/prebid/server/spring/config/bidder/MsftConfiguration.java new file mode 100644 index 00000000000..574d3eb6eb2 --- /dev/null +++ b/src/main/java/org/prebid/server/spring/config/bidder/MsftConfiguration.java @@ -0,0 +1,67 @@ +package org.prebid.server.spring.config.bidder; + +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; +import org.prebid.server.bidder.BidderDeps; +import org.prebid.server.bidder.msft.MsftBidder; +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.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 org.springframework.validation.annotation.Validated; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import java.util.Map; + +@Configuration +@PropertySource(value = "classpath:/bidder-config/msft.yaml", factory = YamlPropertySourceFactory.class) +public class MsftConfiguration { + + private static final String BIDDER_NAME = "msft"; + + @Bean("msftConfigurationProperties") + @ConfigurationProperties("adapters.msft") + MsftConfigurationProperties configurationProperties() { + return new MsftConfigurationProperties(); + } + + @Bean + BidderDeps msftBidderDeps(MsftConfigurationProperties msftConfigurationProperties, + @NotBlank @Value("${external-url}") String externalUrl, + JacksonMapper mapper) { + + return BidderDepsAssembler.forBidder(BIDDER_NAME) + .withConfig(msftConfigurationProperties) + .usersyncerCreator(UsersyncerCreator.create(externalUrl)) + .bidderCreator(config -> new MsftBidder( + config.getEndpoint(), + msftConfigurationProperties.getHbSource(), + msftConfigurationProperties.getHbSourceVideo(), + msftConfigurationProperties.getIabCategories(), + mapper)) + .assemble(); + } + + @Validated + @Data + @EqualsAndHashCode(callSuper = true) + @NoArgsConstructor + private static class MsftConfigurationProperties extends BidderConfigurationProperties { + + @NotNull + Integer hbSource = 5; + + @NotNull + Integer hbSourceVideo = 6; + + Map iabCategories; + } +} diff --git a/src/main/resources/bidder-config/msft.yaml b/src/main/resources/bidder-config/msft.yaml new file mode 100644 index 00000000000..6d3e93d3fe8 --- /dev/null +++ b/src/main/resources/bidder-config/msft.yaml @@ -0,0 +1,118 @@ +adapters: + msft: + endpoint: http://ib.adnxs.com/openrtb2 + meta-info: + maintainer-email: prebid@microsoft.com + app-media-types: + - banner + - video + - native + site-media-types: + - banner + - video + - native + supported-vendors: + vendor-id: 32 + usersync: + cookie-family-name: adnxs + redirect: + url: https://ib.adnxs.com/getuid?{{redirect_url}} + support-cors: false + uid-macro: '$UID' + platform-id: 5 + iab-categories: + 1: IAB20-3 + 2: IAB18-5 + 3: IAB10-1 + 4: IAB2-3 + 5: IAB19-8 + 6: IAB22-1 + 7: IAB18-1 + 8: IAB12-3 + 9: IAB5-1 + 10: IAB4-5 + 11: IAB13-4 + 12: IAB8-7 + 13: IAB9-7 + 14: IAB7-1 + 15: IAB20-18 + 16: IAB10-7 + 17: IAB19-18 + 18: IAB13-6 + 19: IAB18-4 + 20: IAB1-5 + 21: IAB1-6 + 22: IAB3-4 + 23: IAB19-13 + 24: IAB22-2 + 25: IAB3-9 + 26: IAB17-18 + 27: IAB19-6 + 28: IAB1-7 + 29: IAB9-30 + 30: IAB20-7 + 31: IAB20-17 + 32: IAB7-32 + 33: IAB16-5 + 34: IAB19-34 + 35: IAB11-5 + 36: IAB12-3 + 37: IAB11-4 + 38: IAB12-3 + 39: IAB9-30 + 41: IAB7-44 + 42: IAB7-1 + 43: IAB7-30 + 50: IAB19-30 + 51: IAB17-12 + 52: IAB19-30 + 53: IAB3-1 + 55: IAB13-2 + 56: IAB19-30 + 57: IAB19-30 + 58: IAB7-39 + 59: IAB22-1 + 60: IAB7-39 + 61: IAB21-3 + 62: IAB5-1 + 63: IAB12-3 + 64: IAB20-18 + 65: IAB11-2 + 66: IAB17-18 + 67: IAB9-9 + 68: IAB9-5 + 69: IAB7-44 + 71: IAB22-3 + 73: IAB19-30 + 74: IAB8-5 + 78: IAB22-1 + 85: IAB12-2 + 86: IAB22-3 + 87: IAB11-3 + 112: IAB7-32 + 113: IAB7-32 + 114: IAB7-32 + 115: IAB7-32 + 118: IAB9-5 + 119: IAB9-5 + 120: IAB9-5 + 121: IAB9-5 + 122: IAB9-5 + 123: IAB9-5 + 124: IAB9-5 + 125: IAB9-5 + 126: IAB9-5 + 127: IAB22-1 + 132: IAB1-2 + 133: IAB19-30 + 137: IAB3-9 + 138: IAB19-3 + 140: IAB2-3 + 141: IAB2-1 + 142: IAB2-3 + 143: IAB17-13 + 166: IAB11-4 + 175: IAB3-1 + 176: IAB13-4 + 182: IAB8-9 + 183: IAB3-5 diff --git a/src/main/resources/static/bidder-params/msft.json b/src/main/resources/static/bidder-params/msft.json new file mode 100644 index 00000000000..736bf40ff04 --- /dev/null +++ b/src/main/resources/static/bidder-params/msft.json @@ -0,0 +1,68 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Microsoft Adapter Params", + "description": "A schema which validates params accepted by the Microsoft adapter", + "type": "object", + "properties": { + "placement_id": { + "type": "integer", + "description": "Placement ID from Microsoft Monetize. You may use inv_code with member instead of a placement_id." + }, + "member": { + "type": "integer", + "description": "Member ID from Microsoft Monetize. Must be used with inv_code." + }, + "inv_code": { + "type": "string", + "description": "Inventory code from Microsoft Monetize. Must be used with member." + }, + "allow_smaller_sizes": { + "type": "boolean", + "description": "If true, ads smaller than your ad unit's size array will be allowed to serve." + }, + "use_pmt_rule": { + "type": "boolean", + "description": "If true, Microsoft Monetize will return net price after publisher payment rules are applied." + }, + "keywords": { + "type": "string", + "description": "Comma-delimited key-value pairs (max 100). Must be enabled by a Microsoft Monetize account manager." + }, + "traffic_source_code": { + "type": "string", + "description": "Specifies the third-party source of this impression." + }, + "pubclick": { + "type": "string", + "description": "Specifies a publisher supplied url for third-party click tracking." + }, + "ext_inv_code": { + "type": "string", + "description": "Specifies predefined value passed on the query string that can be used in reporting. The value must be entered into Microsoft Monetize before it is logged.." + }, + "ext_imp_id": { + "type": "string", + "description": "Specifies the unique identifier of an externally generated auction." + }, + "banner_frameworks": { + "type": "array", + "items": { + "type": "integer" + }, + "description": "List of supported API frameworks for banner ads supported by the publisher." + } + }, + "oneOf": [ + { + "required": [ + "placement_id" + ] + }, + { + "required": [ + "inv_code", + "member" + ] + } + ] +} diff --git a/src/test/java/org/prebid/server/bidder/msft/MsftBidderTest.java b/src/test/java/org/prebid/server/bidder/msft/MsftBidderTest.java new file mode 100644 index 00000000000..a232e1b4329 --- /dev/null +++ b/src/test/java/org/prebid/server/bidder/msft/MsftBidderTest.java @@ -0,0 +1,894 @@ +package org.prebid.server.bidder.msft; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.BooleanNode; +import com.fasterxml.jackson.databind.node.IntNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.fasterxml.jackson.databind.node.TextNode; +import com.iab.openrtb.request.App; +import com.iab.openrtb.request.Banner; +import com.iab.openrtb.request.BidRequest; +import com.iab.openrtb.request.Format; +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.netty.handler.codec.http.HttpHeaderValues; +import io.netty.handler.codec.http.HttpResponseStatus; +import io.vertx.core.MultiMap; +import io.vertx.core.http.HttpMethod; +import io.vertx.core.http.impl.headers.HeadersMultiMap; +import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.Test; +import org.prebid.server.VertxTest; +import org.prebid.server.auction.model.Endpoint; +import org.prebid.server.bidder.model.BidderBid; +import org.prebid.server.bidder.model.BidderCall; +import org.prebid.server.bidder.model.BidderError; +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.msft.proto.MsftBidExt; +import org.prebid.server.bidder.msft.proto.MsftBidExtCreative; +import org.prebid.server.bidder.msft.proto.MsftBidExtVideo; +import org.prebid.server.proto.openrtb.ext.ExtIncludeBrandCategory; +import org.prebid.server.proto.openrtb.ext.ExtPrebid; +import org.prebid.server.proto.openrtb.ext.request.ExtApp; +import org.prebid.server.proto.openrtb.ext.request.ExtAppPrebid; +import org.prebid.server.proto.openrtb.ext.request.ExtRequest; +import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebid; +import org.prebid.server.proto.openrtb.ext.request.ExtRequestPrebidServer; +import org.prebid.server.proto.openrtb.ext.request.ExtRequestTargeting; +import org.prebid.server.proto.openrtb.ext.request.msft.ExtImpMsft; +import org.prebid.server.proto.openrtb.ext.response.ExtBidPrebidVideo; +import org.prebid.server.util.HttpUtil; + +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.function.UnaryOperator; + +import static java.util.Collections.singletonList; +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; +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 MsftBidderTest extends VertxTest { + + private static final String ENDPOINT_URL = "https://test.endpoint.com/"; + private static final int DEFAULT_HB_SOURCE = 5; + private static final int DEFAULT_HB_SOURCE_VIDEO = 6; + + private final MsftBidder target = new MsftBidder( + ENDPOINT_URL, + DEFAULT_HB_SOURCE, + DEFAULT_HB_SOURCE_VIDEO, + Map.of(10, "IAB4-5"), + jacksonMapper); + + @Test + public void creationShouldFailOnInvalidEndpointUrl() { + assertThatIllegalArgumentException().isThrownBy(() -> new MsftBidder( + "invalid_url", + DEFAULT_HB_SOURCE, + DEFAULT_HB_SOURCE_VIDEO, + Collections.emptyMap(), + jacksonMapper)); + } + + @Test + public void makeHttpRequestsShouldReturnExpectedRequestUrl() { + // given + final BidRequest bidRequest = givenBidRequest(givenImp()); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getUri) + .containsExactly(ENDPOINT_URL); + } + + @Test + public void makeHttpRequestsShouldAddMemberIdToRequestUrl() { + // given + final BidRequest bidRequest = givenBidRequest(givenImp(givenImpExt(impExt -> impExt.member(17)))); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getUri) + .containsExactly(ENDPOINT_URL + "?member_id=17"); + } + + @Test + public void makeHttpRequestsShouldReturnExpectedRequestMethod() { + // given + final BidRequest bidRequest = givenBidRequest(givenImp()); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getMethod) + .containsExactly(HttpMethod.POST); + } + + @Test + public void makeHttpRequestsShouldReturnExpectedRequestHeaders() { + // given + final BidRequest bidRequest = givenBidRequest(givenImp()); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getHeaders) + .flatExtracting(MultiMap::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(HttpUtil.X_OPENRTB_VERSION_HEADER.toString(), "2.6")); + } + + @Test + public void makeHttpRequestsShouldReturnErrorIfImpExtBidderIsInvalid() { + // given + final BidRequest bidRequest = givenBidRequest(givenImp( + mapper.createObjectNode().put("bidder", "Invalid imp.ext.bidder"))); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getValue()).isEmpty(); + assertThat(result.getErrors()).hasSize(1).allSatisfy( + error -> { + assertThat(error.getType()).isEqualTo(BidderError.Type.bad_input); + assertThat(error.getMessage()).startsWith("Failed to deserialize Microsoft imp extension: "); + }); + } + + @Test + public void makeHttpRequestsShouldReturnErrorIfThereAreImpsWithDuplicateMemberIds() { + // given + final BidRequest bidRequest = givenBidRequest( + givenImp(givenImpExt(impExt -> impExt.member(1))), + givenImp(givenImpExt(impExt -> impExt.member(2)))); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getValue()).isEmpty(); + assertThat(result.getErrors()) + .containsExactly(BidderError.badInput("Member id mismatch: " + + "all impressions must use the same member id but found two different ids: 1 and 2")); + } + + @Test + public void makeHttpRequestsShouldIgnoreImpsWithoutMemberIdsWhenCheckingForDuplicateMemberIds() { + // given + final BidRequest bidRequest = givenBidRequest( + givenImp(givenImpExt()), + givenImp(givenImpExt(impExt -> impExt.member(1))), + givenImp(givenImpExt())); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + } + + @Test + public void makeHttpRequestsShouldReplaceImpTagIdWithInvCodeIfItIsPresent() { + // given + final BidRequest bidRequest = givenBidRequest(givenImp(imp -> imp + .tagid("oldInvCode") + .ext(givenImpExt(impExt -> impExt.invCode("newInvCode"))))); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getPayload) + .flatExtracting(BidRequest::getImp) + .extracting(Imp::getTagid) + .containsExactly("newInvCode"); + } + + @Test + public void makeHttpRequestsShouldNotReplaceImpTagIdWithInvCodeIfItIsAbsent() { + // given + final BidRequest bidRequest = givenBidRequest(givenImp(imp -> imp + .tagid("oldInvCode") + .ext(givenImpExt(impExt -> impExt.invCode(null))))); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getPayload) + .flatExtracting(BidRequest::getImp) + .extracting(Imp::getTagid) + .containsExactly("oldInvCode"); + } + + @Test + public void makeHttpRequestsShouldSetBannerDimensionsToFirstFormatDimensionsIfBannerDimensionsAreAbsent() { + // given + final BidRequest bidRequest = givenBidRequest(givenImp(imp -> imp + .banner(Banner.builder().format(List.of( + Format.builder().w(200).h(300).build(), + Format.builder().w(400).h(600).build())).build()) + .ext(givenImpExt()))); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getPayload) + .flatExtracting(BidRequest::getImp) + .extracting(Imp::getBanner) + .extracting(Banner::getW, Banner::getH) + .containsExactly(tuple(200, 300)); + } + + @Test + public void makeHttpRequestsShouldntReplaceBannerDimensionsWithFirstFormatDimensionsIfBannerDimensionsArePresent() { + // given + final BidRequest bidRequest = givenBidRequest(givenImp(imp -> imp + .banner(Banner.builder() + .w(10) + .h(20) + .format(List.of( + Format.builder().w(200).h(300).build(), + Format.builder().w(400).h(600).build())).build()) + .ext(givenImpExt()))); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getPayload) + .flatExtracting(BidRequest::getImp) + .extracting(Imp::getBanner) + .extracting(Banner::getW, Banner::getH) + .containsExactly(tuple(10, 20)); + } + + @Test + public void makeHttpRequestsShouldSetBannerApiToBannerFrameworksIfBannerApiIsAbsent() { + // given + final BidRequest bidRequest = givenBidRequest(givenImp(imp -> imp + .banner(Banner.builder().build()) + .ext(givenImpExt(impExt -> impExt.bannerFrameworks(List.of(1, 2, 3)))))); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getPayload) + .flatExtracting(BidRequest::getImp) + .extracting(Imp::getBanner) + .extracting(Banner::getApi) + .containsExactly(List.of(1, 2, 3)); + } + + @Test + public void makeHttpRequestsShouldNotReplaceBannerApiWithBannerFrameworksIfBannerApiIsPresent() { + // given + final BidRequest bidRequest = givenBidRequest(givenImp(imp -> imp + .banner(Banner.builder().api(List.of(1, 2, 3)).build()) + .ext(givenImpExt(impExt -> impExt.bannerFrameworks(List.of(4, 5, 6)))))); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getPayload) + .flatExtracting(BidRequest::getImp) + .extracting(Imp::getBanner) + .extracting(Banner::getApi) + .containsExactly(List.of(1, 2, 3)); + } + + @Test + public void makeHttpRequestsShouldSetDisplayManagerVersionIfItIsAbsent() { + // given + final BidRequest bidRequest = givenBidRequest(request -> request.app( + App.builder().ext(ExtApp.of(ExtAppPrebid.of("testSource", "testVersion"), null)).build() + ), givenImp()); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getPayload) + .flatExtracting(BidRequest::getImp) + .extracting(Imp::getDisplaymanagerver) + .containsExactly("testSource-testVersion"); + } + + @Test + public void makeHttpRequestsShouldNotReplaceDisplayManagerVersionIfItIsPresent() { + // given + final BidRequest bidRequest = givenBidRequest(request -> request.app( + App.builder().ext(ExtApp.of(ExtAppPrebid.of("testSource", "testVersion"), null)).build() + ), givenImp(imp -> imp.displaymanagerver("testDisplayManagerVersion").ext(givenImpExt()))); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getPayload) + .flatExtracting(BidRequest::getImp) + .extracting(Imp::getDisplaymanagerver) + .containsExactly("testDisplayManagerVersion"); + } + + @Test + public void makeHttpRequestsShouldReplaceImpExt() { + // given + final BidRequest bidRequest = givenBidRequest(givenImp(givenImpExt(impExt -> impExt + .placementId(17) + .allowSmallerSizes(true) + .usePmtRule(false) + .keywords("testKeywords") + .trafficSourceCode("testTrafficSourceCode") + .pubclick("testPubClick") + .extInvCode("testExtInvCode") + .extImpId("testExtImpId")) + .put("gpid", "testGpid"))); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getPayload) + .flatExtracting(BidRequest::getImp) + .extracting(Imp::getExt) + .containsExactly(mapper.createObjectNode() + .put("gpid", "testGpid") + .set("appnexus", mapper.valueToTree( + Map.of("placement_id", 17, + "allow_smaller_sizes", true, + "use_pmt_rule", false, + "keywords", "testKeywords", + "traffic_source_code", "testTrafficSourceCode", + "pub_click", "testPubClick", + "ext_inv_code", "testExtInvCode", + "ext_imp_id", "testExtImpId") + )) + ); + } + + @Test + public void makeHttpRequestsShouldRemoveGpidFieldFromImpExtIfItIsEmpty() { + // given + final BidRequest bidRequest = givenBidRequest(givenImp(givenImpExt() + .put("gpid", StringUtils.EMPTY))); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getPayload) + .flatExtracting(BidRequest::getImp) + .extracting(Imp::getExt) + .containsExactly(mapper.createObjectNode().set("appnexus", mapper.createObjectNode())); + } + + @Test + public void makeHttpRequestsShouldPreserveOtherBidRequestExtensions() { + // given + final ExtRequest extRequest = ExtRequest.empty(); + final JsonNode anotherExtension = TextNode.valueOf("anotherExtensionValue"); + extRequest.addProperty("anotherExtension", anotherExtension); + final BidRequest bidRequest = givenBidRequest(builder -> builder.ext(extRequest), givenImp()); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(httpRequest -> httpRequest.getPayload().getExt().getProperty("anotherExtension")) + .containsExactly(anotherExtension); + } + + @Test + public void makeHttpRequestsShouldReturnErrorIfMicrosoftBidRequestExtensionIsInvalid() { + // given + final ExtRequest extRequest = ExtRequest.empty(); + extRequest.addProperty("appnexus", TextNode.valueOf("Invalid request.ext")); + final BidRequest bidRequest = givenBidRequest(request -> request.ext(extRequest), givenImp()); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getValue()).hasSize(0); + assertThat(result.getErrors()).hasSize(1).allSatisfy(error -> { + assertThat(error.getType()).isEqualTo(BidderError.Type.bad_input); + assertThat(error.getMessage()).startsWith("Failed to deserialize Microsoft bid request extension: "); + }); + } + + @Test + public void makeHttpRequestsShouldSetBrandCategoryFieldsToTrueIfIncludeBrandCategoryIsPresentInPrebidExtension() { + // given + final ExtRequest extRequest = ExtRequest.of(ExtRequestPrebid.builder() + .targeting(ExtRequestTargeting.builder().includebrandcategory(ExtIncludeBrandCategory.of( + 1, "testPublisher", true, false)).build()).build()); + final BidRequest bidRequest = givenBidRequest(request -> request.ext(extRequest), givenImp()); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(httpRequest -> httpRequest.getPayload().getExt().getProperty("appnexus")) + .extracting(ext -> ext.get("include_brand_category"), ext -> ext.get("brand_category_uniqueness")) + .containsExactly(tuple(BooleanNode.getTrue(), BooleanNode.getTrue())); + } + + @Test + public void makeHttpRequestsShouldPreserveBrandCategoryFieldsIfIncludeBrandCategoryIsAbsentInPrebidExtension() { + // given + final ExtRequest extRequest = ExtRequest.empty(); + extRequest.addProperty("appnexus", mapper.valueToTree(Map.of( + "include_brand_category", false, + "brand_category_uniqueness", false + ))); + final BidRequest bidRequest = givenBidRequest(request -> request.ext(extRequest), givenImp()); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(httpRequest -> httpRequest.getPayload().getExt().getProperty("appnexus")) + .extracting(ext -> ext.get("include_brand_category"), ext -> ext.get("brand_category_uniqueness")) + .containsExactly(tuple(BooleanNode.getFalse(), BooleanNode.getFalse())); + } + + @Test + public void makeHttpRequestsShouldSetIsAmpToOneIfRequestComesFromAmpEndpoint() { + // given + final ExtRequest extRequest = ExtRequest.of(ExtRequestPrebid.builder().server(ExtRequestPrebidServer.of( + "testExternalUrl", 1, "testDatacenter", Endpoint.openrtb2_amp.value() + )).build()); + final BidRequest bidRequest = givenBidRequest(request -> request.ext(extRequest), givenImp()); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(httpRequest -> httpRequest.getPayload().getExt().getProperty("appnexus")) + .extracting(ext -> ext.get("is_amp")) + .containsExactly(IntNode.valueOf(1)); + } + + @Test + public void makeHttpRequestsShouldSetIsAmpToZeroIfRequestDoesNotComeFromAmpEndpoint() { + // given + final ExtRequest extRequest = ExtRequest.of(ExtRequestPrebid.builder().server(ExtRequestPrebidServer.of( + "testExternalUrl", 1, "testDatacenter", Endpoint.openrtb2_video.value() + )).build()); + final BidRequest bidRequest = givenBidRequest(request -> request.ext(extRequest), givenImp()); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(httpRequest -> httpRequest.getPayload().getExt().getProperty("appnexus")) + .extracting(ext -> ext.get("is_amp")) + .containsExactly(IntNode.valueOf(0)); + } + + @Test + public void makeHttpRequestsShouldSetHbSourceToHbSourceVideoValueIfRequestComesFromVideoEndpoint() { + // given + final ExtRequest extRequest = ExtRequest.of(ExtRequestPrebid.builder().server(ExtRequestPrebidServer.of( + "testExternalUrl", 1, "testDatacenter", Endpoint.openrtb2_video.value() + )).build()); + final BidRequest bidRequest = givenBidRequest(request -> request.ext(extRequest), givenImp()); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(httpRequest -> httpRequest.getPayload().getExt().getProperty("appnexus")) + .extracting(ext -> ext.get("hb_source")) + .containsExactly(IntNode.valueOf(6)); + } + + @Test + public void makeHttpRequestsShouldSetHbSourceToDefaultValueIfRequestDoesNotComeFromVideoEndpoint() { + // given + final ExtRequest extRequest = ExtRequest.of(ExtRequestPrebid.builder().server(ExtRequestPrebidServer.of( + "testExternalUrl", 1, "testDatacenter", Endpoint.openrtb2_amp.value() + )).build()); + final BidRequest bidRequest = givenBidRequest(request -> request.ext(extRequest), givenImp()); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(httpRequest -> httpRequest.getPayload().getExt().getProperty("appnexus")) + .extracting(ext -> ext.get("hb_source")) + .containsExactly(IntNode.valueOf(5)); + } + + @Test + public void makeHttpRequestsShouldPartitionImpressions() { + // given + final BidRequest bidRequest = givenBidRequest(Collections.nCopies(30, givenImp()).toArray(new Imp[0])); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getPayload) + .extracting(BidRequest::getImp) + .hasSize(3) + .allSatisfy(imps -> assertThat(imps).hasSize(10)); + } + + @Test + public void makeHttpRequestsShouldReturnErrorAndRequestWithOtherImpressionsIfThereAreImpressionsWithErrors() { + // given + final BidRequest bidRequest = givenBidRequest( + givenImp(imp -> imp.id("imp1").ext(givenImpExt())), + givenImp(imp -> imp.id("imp2") + .ext(mapper.createObjectNode().put("bidder", "Invalid imp.ext.bidder")))); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).hasSize(1); + assertThat(result.getValue()).hasSize(1) + .flatExtracting(HttpRequest::getImpIds) + .containsExactly("imp1"); + } + + @Test + public void makeBidsShouldReturnErrorIfResponseHasInvalidBody() { + // given + final BidderCall httpCall = BidderCall.succeededHttp( + null, + HttpResponse.of(HttpResponseStatus.CREATED.code(), HeadersMultiMap.headers(), "\"Invalid body\""), + null); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getValue()).isEmpty(); + assertThat(result.getErrors()).hasSize(1).allSatisfy(error -> { + assertThat(error.getType()).isEqualTo(BidderError.Type.bad_server_response); + assertThat(error.getMessage()).startsWith("Failed to parse response as BidResponse: "); + }); + } + + @Test + public void makeBidsShouldReturnErrorIfThereAreNoBids() { + // given + final BidderCall httpCall = givenHttpCall(); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getValue()).isEmpty(); + assertThat(result.getErrors()) + .containsExactly(BidderError.badServerResponse("No valid bids found in response")); + } + + @Test + public void makeBidsShouldReturnErrorIfBidIsMissingMicrosoftExtension() { + // given + final BidderCall httpCall = givenHttpCall(Bid.builder().build()); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getValue()).isEmpty(); + assertThat(result.getErrors()).containsExactlyInAnyOrder( + BidderError.badInput("Missing Microsoft bid extension"), + BidderError.badServerResponse("No valid bids found in response")); + } + + @Test + public void makeBidsShouldReturnErrorIfBidHasInvalidMicrosoftExtension() { + // given + final BidderCall httpCall = givenHttpCall( + Bid.builder().ext(mapper.createObjectNode().put("appnexus", "Invalid")).build()); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getValue()).isEmpty(); + assertThat(result.getErrors()).hasSize(2).satisfiesExactlyInAnyOrder( + error -> { + assertThat(error.getType()).isEqualTo(BidderError.Type.bad_input); + assertThat(error.getMessage()).startsWith("Failed to deserialize Microsoft bid extension: "); + }, + error -> assertThat(error) + .isEqualTo(BidderError.badServerResponse("No valid bids found in response")) + ); + } + + @Test + public void makeBidsShouldReturnExpectedBidCategory() { + // given + final BidderCall httpCall = givenHttpCall(givenBid(givenBidExt(ext -> ext.brandCategoryId(10)))); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(BidderBid::getBid) + .flatExtracting(Bid::getCat) + .containsExactly("IAB4-5"); + } + + @Test + public void makeBidsShouldPreserveExistingBidCategoryIfIabCategoryFromExtIsUnknown() { + // given + final BidderCall httpCall = givenHttpCall(givenBid( + bid -> bid.cat(singletonList("testCat")).ext(givenBidExt(ext -> ext.brandCategoryId(-1))))); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(BidderBid::getBid) + .flatExtracting(Bid::getCat) + .containsExactly("testCat"); + } + + @Test + public void makeBidsShouldRemoveExistingBidCategoriesIfThereAreMoreThenOneAndIfIabCategoryFromExtIsUnknown() { + // given + final BidderCall httpCall = givenHttpCall(givenBid( + bid -> bid.cat(List.of("cat1", "cat2")).ext(givenBidExt(ext -> ext.brandCategoryId(-1))))); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(BidderBid::getBid) + .hasSize(1) + .flatExtracting(Bid::getCat) + .isEmpty(); + } + + @Test + public void makeBidsShouldReturnExpectedBidTypes() { + // given + final BidderCall httpCall = givenHttpCall( + givenBid(bid -> bid.id("imp0").ext(givenBidExt(ext -> ext.bidAdType(0)))), + givenBid(bid -> bid.id("imp1").ext(givenBidExt(ext -> ext.bidAdType(1)))), + givenBid(bid -> bid.id("imp3").ext(givenBidExt(ext -> ext.bidAdType(3)))), + givenBid(bid -> bid.id("imp_invalid").ext(givenBidExt(ext -> ext.bidAdType(-1))))); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getValue()) + .extracting(bid -> bid.getBid().getId(), BidderBid::getType) + .containsExactly( + tuple("imp0", banner), + tuple("imp1", video), + tuple("imp3", xNative) + ); + assertThat(result.getErrors()).hasSize(1) + .containsExactly(BidderError.badInput("Unsupported bid ad type: -1")); + } + + @Test + public void makeBidsShouldReturnExpectedCurrency() { + // given + final BidderCall httpCall = givenHttpCall(givenBid(givenBidExt(identity()))); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(BidderBid::getBidCurrency) + .containsExactly("USD"); + } + + @Test + public void makeBidsShouldReturnExpectedDealPriority() { + // given + final BidderCall httpCall = givenHttpCall(givenBid(givenBidExt(ext -> ext.dealPriority(2)))); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getValue()) + .extracting(BidderBid::getDealPriority) + .containsExactly(2); + assertThat(result.getErrors()).isEmpty(); + } + + @Test + public void makeBidsShouldReturnDefaultDealPriority() { + // given + final BidderCall httpCall = givenHttpCall(givenBid(givenBidExt(identity()))); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(BidderBid::getDealPriority) + .containsExactly(0); + } + + @Test + public void makeBidsShouldReturnExpectedVideoDuration() { + // given + final BidderCall httpCall = givenHttpCall(givenBid(givenBidExt(ext -> ext.creativeInfo( + MsftBidExtCreative.of(MsftBidExtVideo.of(17)))))); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(BidderBid::getVideoInfo) + .extracting(ExtBidPrebidVideo::getDuration) + .containsExactly(17); + } + + @Test + public void makeBidsShouldReturnDefaultVideoDuration() { + // given + final BidderCall httpCall = givenHttpCall(givenBid(givenBidExt(identity()))); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(BidderBid::getVideoInfo) + .extracting(ExtBidPrebidVideo::getDuration) + .containsExactly(0); + } + + private static BidRequest givenBidRequest(Imp... imps) { + return givenBidRequest(identity(), imps); + } + + private static BidRequest givenBidRequest(UnaryOperator bidRequestCustomizer, + Imp... imps) { + + return bidRequestCustomizer + .apply(BidRequest.builder() + .id("testBidRequestId") + .imp(List.of(imps))) + .build(); + } + + private static Imp givenImp() { + return givenImp(givenImpExt()); + } + + private static Imp givenImp(ObjectNode impExt) { + return givenImp(imp -> imp.ext(impExt)); + } + + private static Imp givenImp(UnaryOperator impCustomizer) { + return impCustomizer.apply(Imp.builder()).build(); + } + + private static ObjectNode givenImpExt() { + return givenImpExt(identity()); + } + + private static ObjectNode givenImpExt(UnaryOperator extImpCustomizer) { + return mapper.valueToTree(ExtPrebid.of( + null, + extImpCustomizer.apply(ExtImpMsft.builder()).build())); + } + + private static BidderCall givenHttpCall(Bid... bids) { + return BidderCall.succeededHttp( + HttpRequest.builder().payload(null).build(), + HttpResponse.of(HttpResponseStatus.CREATED.code(), null, givenBidResponse(bids)), + null); + } + + private static String givenBidResponse(Bid... bids) { + try { + return mapper.writeValueAsString(BidResponse.builder() + .cur("USD") + .seatbid(singletonList(SeatBid.builder().bid(List.of(bids)).build())) + .build()); + } catch (JsonProcessingException e) { + throw new RuntimeException("Error encoding BidResponse to json: " + e); + } + } + + private static Bid givenBid(ObjectNode bidExt) { + return givenBid(bid -> bid.ext(bidExt)); + } + + private static Bid givenBid(UnaryOperator bidCustomizer) { + return bidCustomizer.apply(Bid.builder()).build(); + } + + private static ObjectNode givenBidExt(UnaryOperator extCustomizer) { + return mapper.valueToTree(Map.of("appnexus", extCustomizer.apply(MsftBidExt.builder()).build())); + } +} diff --git a/src/test/java/org/prebid/server/it/MsftTest.java b/src/test/java/org/prebid/server/it/MsftTest.java new file mode 100644 index 00000000000..d5695213dbb --- /dev/null +++ b/src/test/java/org/prebid/server/it/MsftTest.java @@ -0,0 +1,33 @@ +package org.prebid.server.it; + +import io.restassured.response.Response; +import org.json.JSONException; +import org.junit.jupiter.api.Test; +import org.prebid.server.model.Endpoint; + +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 java.util.Collections.singletonList; + +public class MsftTest extends IntegrationTest { + + @Test + public void openrtb2AuctionShouldRespondWithBidsFromMicrosoft() throws IOException, JSONException { + // given + WIRE_MOCK_RULE.stubFor(post(urlPathEqualTo("/msft-exchange")) + .withRequestBody(equalToJson(jsonFrom("openrtb2/msft/test-msft-bid-request.json"))) + .willReturn(aResponse().withBody(jsonFrom("openrtb2/msft/test-msft-bid-response.json")))); + + // when + final Response response = responseFor("openrtb2/msft/test-auction-msft-request.json", + Endpoint.openrtb2_auction); + + // then + assertJsonEquals("openrtb2/msft/test-auction-msft-response.json", response, + singletonList("msft")); + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/msft/test-auction-msft-request.json b/src/test/resources/org/prebid/server/it/openrtb2/msft/test-auction-msft-request.json new file mode 100644 index 00000000000..f485c752fb8 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/msft/test-auction-msft-request.json @@ -0,0 +1,23 @@ +{ + "id": "bid_request_id", + "imp": [ + { + "id": "imp_id", + "banner": { + "h": 250, + "w": 300, + "pos": 0 + }, + "displaymanagerver": "ver1", + "ext": { + "msft": { + "placement_id": 1 + } + } + } + ], + "tmax": 5000, + "regs": { + "gdpr": 0 + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/msft/test-auction-msft-response.json b/src/test/resources/org/prebid/server/it/openrtb2/msft/test-auction-msft-response.json new file mode 100644 index 00000000000..ced51d3cfeb --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/msft/test-auction-msft-response.json @@ -0,0 +1,41 @@ +{ + "id": "bid_request_id", + "seatbid": [ + { + "bid": [ + { + "id": "1", + "impid": "imp_id", + "price": 2.5, + "adm": "test-ad-content", + "adid": "456", + "cid": "1234", + "crid": "1234", + "exp": 300, + "ext": { + "appnexus": {}, + "prebid": { + "type": "banner", + "meta": { + "adaptercode": "msft" + } + }, + "origbidcpm": 2.5 + } + } + ], + "seat": "msft", + "group": 0 + } + ], + "cur": "USD", + "ext": { + "responsetimemillis": { + "msft": "{{ msft.response_time_ms }}" + }, + "tmaxrequest": 5000, + "prebid": { + "auctiontimestamp": 0 + } + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/msft/test-msft-bid-request.json b/src/test/resources/org/prebid/server/it/openrtb2/msft/test-msft-bid-request.json new file mode 100644 index 00000000000..d89cc5f0941 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/msft/test-msft-bid-request.json @@ -0,0 +1,61 @@ +{ + "id": "bid_request_id", + "imp": [ + { + "id": "imp_id", + "secure": 1, + "banner": { + "h": 250, + "w": 300, + "pos": 0 + }, + "displaymanagerver": "ver1", + "ext": { + "appnexus": { + "placement_id": 1 + } + } + } + ], + "site": { + "domain": "www.example.com", + "page": "http://www.example.com", + "publisher": { + "domain": "example.com" + }, + "ext": { + "amp": 0 + } + }, + "device": { + "ua": "userAgent", + "ip": "193.168.244.1" + }, + "at": 1, + "tmax": "${json-unit.any-number}", + "cur": [ + "USD" + ], + "source": { + "tid": "${json-unit.any-string}" + }, + "regs": { + "ext": { + "gdpr": 0 + } + }, + "ext": { + "prebid": { + "server": { + "externalurl": "http://localhost:8080", + "gvlid": 1, + "datacenter": "local", + "endpoint": "/openrtb2/auction" + } + }, + "appnexus": { + "is_amp": 0, + "hb_source": 5 + } + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/msft/test-msft-bid-response.json b/src/test/resources/org/prebid/server/it/openrtb2/msft/test-msft-bid-response.json new file mode 100644 index 00000000000..e259628864f --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/msft/test-msft-bid-response.json @@ -0,0 +1,21 @@ +{ + "id": "tid", + "seatbid": [ + { + "bid": [ + { + "id": "1", + "impid": "imp_id", + "price": 2.5, + "adm": "test-ad-content", + "adid": "456", + "crid": "1234", + "cid": "1234", + "ext": { + "appnexus": {} + } + } + ] + } + ] +} 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 1a073dcda06..16db8d36333 100644 --- a/src/test/resources/org/prebid/server/it/test-application.properties +++ b/src/test/resources/org/prebid/server/it/test-application.properties @@ -406,6 +406,8 @@ adapters.mobkoi.enabled=true adapters.mobkoi.endpoint=http://localhost:8090/mobkoi-exchange adapters.motorik.enabled=true adapters.motorik.endpoint=http://localhost:8090/motorik-exchange?k={{AccountID}}&name={{SourceId}} +adapters.msft.enabled=true +adapters.msft.endpoint=http://localhost:8090/msft-exchange adapters.nativery.enabled=true adapters.nativery.endpoint=http://localhost:8090/nativery-exchange adapters.nextmillennium.enabled=true From 0f1e635b6290c4de6e5ad3e4ce2f039651810195 Mon Sep 17 00:00:00 2001 From: Viktor Kryshtal Date: Thu, 11 Dec 2025 12:57:27 +0200 Subject: [PATCH 2/2] Fix comments --- .../prebid/server/bidder/msft/MsftBidder.java | 76 +++++++++---------- .../server/bidder/msft/MsftBidderTest.java | 10 +-- 2 files changed, 37 insertions(+), 49 deletions(-) diff --git a/src/main/java/org/prebid/server/bidder/msft/MsftBidder.java b/src/main/java/org/prebid/server/bidder/msft/MsftBidder.java index 7af5ecd3219..f4ee2aa5cf2 100644 --- a/src/main/java/org/prebid/server/bidder/msft/MsftBidder.java +++ b/src/main/java/org/prebid/server/bidder/msft/MsftBidder.java @@ -46,7 +46,6 @@ import org.prebid.server.proto.openrtb.ext.response.ExtBidPrebidVideo; import org.prebid.server.util.BidderUtil; import org.prebid.server.util.HttpUtil; -import org.prebid.server.util.ObjectUtil; import java.net.URISyntaxException; import java.util.ArrayList; @@ -78,6 +77,7 @@ public MsftBidder(String endpointUrl, int hbSourceVideo, Map iabCategories, JacksonMapper mapper) { + this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl)); this.hbSource = hbSource; this.hbSourceVideo = hbSourceVideo; @@ -98,9 +98,7 @@ public Result>> makeHttpRequests(BidRequest bidRequ final Integer memberId = extImp.getMember(); if (memberId != null && memberIdValidator.isInvalid(memberId)) { - errors.add(BidderError.badInput("Member id mismatch:" - + " all impressions must use the same member id but found two different ids: %s and %s" - .formatted(memberIdValidator.getValue(), memberId))); + errors.add(BidderError.badInput("member id mismatch: all impressions must use the same member id")); return Result.withErrors(errors); } @@ -149,57 +147,50 @@ private ExtImpMsft parseImpExt(Imp imp) { } private Imp updateImp(Imp imp, ExtImpMsft extImp, String defaultDisplayManagerVer) { - final Imp.ImpBuilder impBuilder = imp.toBuilder(); - - if (StringUtils.isNotEmpty(extImp.getInvCode())) { - impBuilder.tagid(extImp.getInvCode()); - } - - if (imp.getBanner() != null) { - impBuilder.banner(updateBanner(imp.getBanner(), extImp)); - } - - if (StringUtils.isEmpty(imp.getDisplaymanagerver())) { - impBuilder.displaymanagerver(defaultDisplayManagerVer); - } - - impBuilder.ext(updateImpExt(imp, extImp)); - - return impBuilder.build(); + final String invCode = extImp.getInvCode(); + final Banner banner = imp.getBanner(); + final String displayManagerVer = StringUtils.defaultIfEmpty( + imp.getDisplaymanagerver(), defaultDisplayManagerVer); + + return imp.toBuilder() + .tagid(StringUtils.isNotEmpty(invCode) ? invCode : imp.getTagid()) + .banner(banner != null ? updateBanner(banner, extImp) : null) + .displaymanagerver(displayManagerVer) + .ext(updateImpExt(imp, extImp)) + .build(); } private Banner updateBanner(Banner banner, ExtImpMsft extImp) { - final Banner.BannerBuilder bannerBuilder = banner.toBuilder(); - final List bannerFormat = banner.getFormat(); + + final Banner.BannerBuilder bannerBuilder = banner.toBuilder(); if (banner.getW() == null && banner.getH() == null && CollectionUtils.isNotEmpty(bannerFormat)) { final Format firstFormat = bannerFormat.getFirst(); bannerBuilder.w(firstFormat.getW()); bannerBuilder.h(firstFormat.getH()); } - if (banner.getApi() == null) { - bannerBuilder.api(extImp.getBannerFrameworks()); - } - - return bannerBuilder.build(); + return bannerBuilder + .api(ObjectUtils.defaultIfNull(banner.getApi(), extImp.getBannerFrameworks())) + .build(); } private ObjectNode updateImpExt(Imp imp, ExtImpMsft extImp) { + final MsftExtImpOutgoing impExtOutgoing = MsftExtImpOutgoing.builder() + .placementId(extImp.getPlacementId()) + .allowSmallerSizes(extImp.getAllowSmallerSizes()) + .usePmtRule(extImp.getUsePmtRule()) + .keywords(extImp.getKeywords()) + .trafficSourceCode(extImp.getTrafficSourceCode()) + .pubClick(extImp.getPubclick()) + .extInvCode(extImp.getExtInvCode()) + .extImpId(extImp.getExtImpId()) + .build(); + final String gpid = imp.getExt().at("/gpid").textValue(); + final ObjectNode updatedImpExt = mapper.mapper().createObjectNode() - .set("appnexus", mapper.mapper().valueToTree( - MsftExtImpOutgoing.builder() - .placementId(extImp.getPlacementId()) - .allowSmallerSizes(extImp.getAllowSmallerSizes()) - .usePmtRule(extImp.getUsePmtRule()) - .keywords(extImp.getKeywords()) - .trafficSourceCode(extImp.getTrafficSourceCode()) - .pubClick(extImp.getPubclick()) - .extInvCode(extImp.getExtInvCode()) - .extImpId(extImp.getExtImpId()) - .build())); - - final String gpid = ObjectUtil.getIfNotNull(imp.getExt().get("gpid"), JsonNode::textValue); + .set("appnexus", mapper.mapper().valueToTree(impExtOutgoing)); + if (StringUtils.isNotEmpty(gpid)) { updatedImpExt.put("gpid", gpid); } @@ -250,7 +241,7 @@ private ExtRequestMsft parseRequestExtMsft(JsonNode extRequestMsftRaw) { try { return mapper.mapper().treeToValue(extRequestMsftRaw, ExtRequestMsft.class); } catch (IllegalArgumentException | JsonProcessingException e) { - throw new PreBidException("Failed to deserialize Microsoft bid request extension: " + e.getMessage()); + throw new PreBidException("malformed request ext.appnexus"); } } @@ -286,6 +277,7 @@ private String extractEndpointUrl(ExtRequest requestExt) { private List> splitHttpRequests(BidRequest bidRequest, List imps, String requestUrl) { + return ListUtils.partition(imps, MAX_IMPS_PER_REQUEST) .stream() .map(impsForRequest -> makeHttpRequest(bidRequest, impsForRequest, requestUrl)) diff --git a/src/test/java/org/prebid/server/bidder/msft/MsftBidderTest.java b/src/test/java/org/prebid/server/bidder/msft/MsftBidderTest.java index a232e1b4329..8ea0d154861 100644 --- a/src/test/java/org/prebid/server/bidder/msft/MsftBidderTest.java +++ b/src/test/java/org/prebid/server/bidder/msft/MsftBidderTest.java @@ -178,9 +178,8 @@ public void makeHttpRequestsShouldReturnErrorIfThereAreImpsWithDuplicateMemberId // then assertThat(result.getValue()).isEmpty(); - assertThat(result.getErrors()) - .containsExactly(BidderError.badInput("Member id mismatch: " - + "all impressions must use the same member id but found two different ids: 1 and 2")); + assertThat(result.getErrors()).containsExactly( + BidderError.badInput("member id mismatch: all impressions must use the same member id")); } @Test @@ -447,10 +446,7 @@ public void makeHttpRequestsShouldReturnErrorIfMicrosoftBidRequestExtensionIsInv // then assertThat(result.getValue()).hasSize(0); - assertThat(result.getErrors()).hasSize(1).allSatisfy(error -> { - assertThat(error.getType()).isEqualTo(BidderError.Type.bad_input); - assertThat(error.getMessage()).startsWith("Failed to deserialize Microsoft bid request extension: "); - }); + assertThat(result.getErrors()).containsExactly(BidderError.badInput("malformed request ext.appnexus")); } @Test