From 486bab61e21fb3c9dd09bee45bfee2a81d22dd93 Mon Sep 17 00:00:00 2001 From: Alex Maltsev Date: Tue, 14 Sep 2021 18:30:21 +0300 Subject: [PATCH 1/7] Added Aceex bidder --- .../server/bidder/aceex/AceexBidder.java | 134 ++++++++++++++++++ .../ext/request/aceex/ExtImpAceex.java | 13 ++ 2 files changed, 147 insertions(+) create mode 100644 src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java create mode 100644 src/main/java/org/prebid/server/proto/openrtb/ext/request/aceex/ExtImpAceex.java diff --git a/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java b/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java new file mode 100644 index 00000000000..af7749ec840 --- /dev/null +++ b/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java @@ -0,0 +1,134 @@ +package org.prebid.server.bidder.aceex; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.iab.openrtb.request.BidRequest; +import com.iab.openrtb.request.Device; +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.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.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.aceex.ExtImpAceex; +import org.prebid.server.proto.openrtb.ext.response.BidType; +import org.prebid.server.util.HttpUtil; +import org.prebid.server.util.ObjectUtils; + +import java.util.Collection; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + +public class AceexBidder implements Bidder { + + private static final TypeReference> ACEEX_EXT_TYPE_REFERENCE = + new TypeReference>() { + }; + private static final String ACCOUNT_ID_MACRO = "{{AccountId}}"; + private static final String X_OPENRTB_VERSION = "2.5"; + + private final String endpointUrl; + private final JacksonMapper mapper; + + public AceexBidder(String endpointUrl, JacksonMapper mapper) { + this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl)); + this.mapper = Objects.requireNonNull(mapper); + } + + @Override + public Result>> makeHttpRequests(BidRequest request) { + final Imp firstImp = request.getImp().get(0); + try { + final ExtImpAceex extImpAceex = mapper.mapper().convertValue( + firstImp.getExt(), ACEEX_EXT_TYPE_REFERENCE).getBidder(); + return Result.withValue(makeHttpRequest(request, extImpAceex.getAccountId())); + } catch (DecodeException e) { + return Result.withError(BidderError.badInput("Ext.bidder not provided")); + } + } + + private HttpRequest makeHttpRequest(BidRequest request, String accountId) { + return HttpRequest.builder() + .method(HttpMethod.POST) + .uri(resolveEndpoint(accountId)) + .headers(constructHeaders(request)) + .body(mapper.encode(request)) + .payload(request) + .build(); + } + + private static MultiMap constructHeaders(BidRequest bidRequest) { + final Device device = bidRequest.getDevice(); + MultiMap headers = HttpUtil.headers(); + + headers.set(HttpUtil.X_OPENRTB_VERSION_HEADER, X_OPENRTB_VERSION); + HttpUtil.addHeaderIfValueIsNotEmpty(headers, HttpUtil.USER_AGENT_HEADER, + ObjectUtils.getIfNotNull(device, Device::getUa)); + HttpUtil.addHeaderIfValueIsNotEmpty(headers, HttpUtil.X_FORWARDED_FOR_HEADER, + ObjectUtils.getIfNotNull(device, Device::getIpv6)); + HttpUtil.addHeaderIfValueIsNotEmpty(headers, HttpUtil.X_FORWARDED_FOR_HEADER, + ObjectUtils.getIfNotNull(device, Device::getIp)); + + return headers; + } + + private String resolveEndpoint(String accountId) { + return endpointUrl.replace(ACCOUNT_ID_MACRO, HttpUtil.encodeUrl(accountId)); + } + + @Override + public final Result> makeBids(HttpCall httpCall, BidRequest bidRequest) { + try { + final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class); + return Result.withValues(extractBids(httpCall.getRequest().getPayload(), bidResponse)); + } catch (DecodeException | PreBidException e) { + return Result.withError(BidderError.badServerResponse("Bad Server Response")); + } + } + + private static List extractBids(BidRequest bidRequest, BidResponse bidResponse) { + final List seatBids = ObjectUtils.getIfNotNull(bidResponse, BidResponse::getSeatbid); + final SeatBid firstSeatBid = CollectionUtils.isNotEmpty(seatBids) ? seatBids.get(0) : null; + if (firstSeatBid == null) { + throw new PreBidException("Empty SeatBid array"); + } + + final Collection bids = CollectionUtils.emptyIfNull(firstSeatBid.getBid()); + return bidsFromSeatBid(bids, bidRequest, bidResponse); + } + + private static List bidsFromSeatBid(Collection bids, + BidRequest bidRequest, + BidResponse bidResponse) { + return bids.stream() + .filter(Objects::nonNull) + .map(bid -> BidderBid.of(bid, getBidMediaType(bid.getImpid(), bidRequest.getImp()), + bidResponse.getCur())) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + } + + private static BidType getBidMediaType(String impId, List imps) { + for (Imp imp : imps) { + if (imp.getId().equals(impId)) { + if (imp.getVideo() != null) { + return BidType.video; + } else if (imp.getXNative() != null) { + return BidType.xNative; + } + } + } + return BidType.banner; + } +} diff --git a/src/main/java/org/prebid/server/proto/openrtb/ext/request/aceex/ExtImpAceex.java b/src/main/java/org/prebid/server/proto/openrtb/ext/request/aceex/ExtImpAceex.java new file mode 100644 index 00000000000..88e75d7b736 --- /dev/null +++ b/src/main/java/org/prebid/server/proto/openrtb/ext/request/aceex/ExtImpAceex.java @@ -0,0 +1,13 @@ +package org.prebid.server.proto.openrtb.ext.request.aceex; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Value; + +@Value +@AllArgsConstructor(staticName = "of") +public class ExtImpAceex { + + @JsonProperty("accountId") + String accountId; +} From c1d2a1d3149916a8f2a7a220583716fc0d8178ed Mon Sep 17 00:00:00 2001 From: Alex Maltsev Date: Tue, 14 Sep 2021 21:09:05 +0300 Subject: [PATCH 2/7] Added configuration --- .../server/bidder/aceex/AceexBidder.java | 5 +- .../config/bidder/AceexConfiguration.java | 51 +++++++++++++++++++ src/main/resources/bidder-config/aceex.yaml | 27 ++++++++++ .../resources/static/bidder-params/aceex.json | 14 +++++ 4 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/prebid/server/spring/config/bidder/AceexConfiguration.java create mode 100644 src/main/resources/bidder-config/aceex.yaml create mode 100644 src/main/resources/static/bidder-params/aceex.json diff --git a/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java b/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java index af7749ec840..dc84ff3dfed 100644 --- a/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java +++ b/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java @@ -10,6 +10,7 @@ import io.vertx.core.MultiMap; import io.vertx.core.http.HttpMethod; import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.collections4.ListUtils; import org.prebid.server.bidder.Bidder; import org.prebid.server.bidder.model.BidderBid; import org.prebid.server.bidder.model.BidderError; @@ -104,11 +105,11 @@ private static List extractBids(BidRequest bidRequest, BidResponse bi throw new PreBidException("Empty SeatBid array"); } - final Collection bids = CollectionUtils.emptyIfNull(firstSeatBid.getBid()); + final List bids = ListUtils.emptyIfNull(firstSeatBid.getBid()); return bidsFromSeatBid(bids, bidRequest, bidResponse); } - private static List bidsFromSeatBid(Collection bids, + private static List bidsFromSeatBid(List bids, BidRequest bidRequest, BidResponse bidResponse) { return bids.stream() diff --git a/src/main/java/org/prebid/server/spring/config/bidder/AceexConfiguration.java b/src/main/java/org/prebid/server/spring/config/bidder/AceexConfiguration.java new file mode 100644 index 00000000000..3314d6dbe7b --- /dev/null +++ b/src/main/java/org/prebid/server/spring/config/bidder/AceexConfiguration.java @@ -0,0 +1,51 @@ +package org.prebid.server.spring.config.bidder; + +import org.prebid.server.bidder.BidderDeps; +import org.prebid.server.bidder.aceex.AceexBidder; +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/aceex.yaml", factory = YamlPropertySourceFactory.class) +public class AceexConfiguration { + + private static final String BIDDER_NAME = "aceex"; + + @Value("${external-url}") + @NotBlank + private String externalUrl; + + @Autowired + private JacksonMapper mapper; + + @Autowired + @Qualifier("aceexConfigurationProperties") + private BidderConfigurationProperties configProperties; + + @Bean("aceexConfigurationProperties") + @ConfigurationProperties("adapters.aceex") + BidderConfigurationProperties configurationProperties() { + return new BidderConfigurationProperties(); + } + + @Bean + BidderDeps aceexBidderDeps() { + return BidderDepsAssembler.forBidder(BIDDER_NAME) + .withConfig(configProperties) + .usersyncerCreator(UsersyncerCreator.create(externalUrl)) + .bidderCreator(config -> new AceexBidder(config.getEndpoint(), mapper)) + .assemble(); + } +} diff --git a/src/main/resources/bidder-config/aceex.yaml b/src/main/resources/bidder-config/aceex.yaml new file mode 100644 index 00000000000..9fef0e464db --- /dev/null +++ b/src/main/resources/bidder-config/aceex.yaml @@ -0,0 +1,27 @@ +adapters: + aceex: + enabled: false + endpoint: http://bl-us.aceex.io/?uqhash={{AccountId}} + pbs-enforces-gdpr: true + pbs-enforces-ccpa: true + modifying-vast-xml-allowed: true + deprecated-names: + aliases: {} + meta-info: + maintainer-email: tech@aceex.io + app-media-types: + - banner + - video + - native + site-media-types: + - banner + - video + - native + supported-vendors: + vendor-id: 0 + usersync: + url: + redirect-url: + cookie-family-name: aceex + type: redirect + support-cors: false diff --git a/src/main/resources/static/bidder-params/aceex.json b/src/main/resources/static/bidder-params/aceex.json new file mode 100644 index 00000000000..9fb97d41136 --- /dev/null +++ b/src/main/resources/static/bidder-params/aceex.json @@ -0,0 +1,14 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Aceex Adapter Params", + "description": "A schema which validates params accepted by the Aceex adapter", + "type": "object", + "properties": { + "accountid": { + "type": "string", + "description": "Account id", + "minLength": 1 + } + }, + "required": ["accountid"] +} From 7dfe62fcbdc00ebfa5c9ffa7e02acbda500d9169 Mon Sep 17 00:00:00 2001 From: Alex Maltsev Date: Tue, 14 Sep 2021 21:23:25 +0300 Subject: [PATCH 3/7] Added integration test --- .../server/bidder/aceex/AceexBidder.java | 1 - .../ext/request/aceex/ExtImpAceex.java | 2 +- .../java/org/prebid/server/it/AceexTest.java | 35 ++++++++++++++++ .../aceex/test-aceex-bid-request.json | 41 +++++++++++++++++++ .../aceex/test-aceex-bid-response.json | 18 ++++++++ .../aceex/test-auction-aceex-request.json | 23 +++++++++++ .../aceex/test-auction-aceex-response.json | 35 ++++++++++++++++ .../server/it/test-application.properties | 2 + 8 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 src/test/java/org/prebid/server/it/AceexTest.java create mode 100644 src/test/resources/org/prebid/server/it/openrtb2/aceex/test-aceex-bid-request.json create mode 100644 src/test/resources/org/prebid/server/it/openrtb2/aceex/test-aceex-bid-response.json create mode 100644 src/test/resources/org/prebid/server/it/openrtb2/aceex/test-auction-aceex-request.json create mode 100644 src/test/resources/org/prebid/server/it/openrtb2/aceex/test-auction-aceex-response.json diff --git a/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java b/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java index dc84ff3dfed..6aaca927a5f 100644 --- a/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java +++ b/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java @@ -26,7 +26,6 @@ import org.prebid.server.util.HttpUtil; import org.prebid.server.util.ObjectUtils; -import java.util.Collection; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; diff --git a/src/main/java/org/prebid/server/proto/openrtb/ext/request/aceex/ExtImpAceex.java b/src/main/java/org/prebid/server/proto/openrtb/ext/request/aceex/ExtImpAceex.java index 88e75d7b736..fb26ec0a898 100644 --- a/src/main/java/org/prebid/server/proto/openrtb/ext/request/aceex/ExtImpAceex.java +++ b/src/main/java/org/prebid/server/proto/openrtb/ext/request/aceex/ExtImpAceex.java @@ -8,6 +8,6 @@ @AllArgsConstructor(staticName = "of") public class ExtImpAceex { - @JsonProperty("accountId") + @JsonProperty("accountid") String accountId; } diff --git a/src/test/java/org/prebid/server/it/AceexTest.java b/src/test/java/org/prebid/server/it/AceexTest.java new file mode 100644 index 00000000000..67cc8573b9d --- /dev/null +++ b/src/test/java/org/prebid/server/it/AceexTest.java @@ -0,0 +1,35 @@ +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.prebid.server.model.Endpoint; +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 java.util.Collections.singletonList; + +@RunWith(SpringRunner.class) +public class AceexTest extends IntegrationTest { + + @Test + public void openrtb2AuctionShouldRespondWithBidsFromAceex() throws IOException, JSONException { + // given + WIRE_MOCK_RULE.stubFor(post(urlPathEqualTo("/aceex-exchange")) + .withRequestBody(equalToJson(jsonFrom("openrtb2/aceex/test-aceex-bid-request.json"))) + .willReturn(aResponse().withBody(jsonFrom("openrtb2/aceex/test-aceex-bid-response.json")))); + + // when + final Response response = responseFor("openrtb2/aceex/test-auction-aceex-request.json", + Endpoint.openrtb2_auction); + + // then + assertJsonEquals("openrtb2/aceex/test-auction-aceex-response.json", response, singletonList("aceex")); + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/aceex/test-aceex-bid-request.json b/src/test/resources/org/prebid/server/it/openrtb2/aceex/test-aceex-bid-request.json new file mode 100644 index 00000000000..e6654826b68 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/aceex/test-aceex-bid-request.json @@ -0,0 +1,41 @@ +{ + "id": "request_id", + "imp": [ + { + "id": "imp_id", + "banner": { + "w": 320, + "h": 250 + }, + "ext": { + "bidder": { + "accountid": "accountid" + } + } + } + ], + "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": 5000, + "cur": [ + "USD" + ], + "regs": { + "ext": { + "gdpr": 0 + } + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/aceex/test-aceex-bid-response.json b/src/test/resources/org/prebid/server/it/openrtb2/aceex/test-aceex-bid-response.json new file mode 100644 index 00000000000..da5c7fc51cd --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/aceex/test-aceex-bid-response.json @@ -0,0 +1,18 @@ +{ + "id": "tid", + "seatbid": [ + { + "bid": [ + { + "crid": "24080", + "adid": "2068416", + "price": 0.01, + "id": "bid_id", + "impid": "imp_id", + "cid": "8048" + } + ], + "type": "banner" + } + ] +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/aceex/test-auction-aceex-request.json b/src/test/resources/org/prebid/server/it/openrtb2/aceex/test-auction-aceex-request.json new file mode 100644 index 00000000000..3ee06d504e8 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/aceex/test-auction-aceex-request.json @@ -0,0 +1,23 @@ +{ + "id": "request_id", + "imp": [ + { + "id": "imp_id", + "banner": { + "w": 320, + "h": 250 + }, + "ext": { + "aceex": { + "accountid": "accountid" + } + } + } + ], + "tmax": 5000, + "regs": { + "ext": { + "gdpr": 0 + } + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/aceex/test-auction-aceex-response.json b/src/test/resources/org/prebid/server/it/openrtb2/aceex/test-auction-aceex-response.json new file mode 100644 index 00000000000..f80400fe5d1 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/aceex/test-auction-aceex-response.json @@ -0,0 +1,35 @@ +{ + "id": "request_id", + "seatbid": [ + { + "bid": [ + { + "id": "bid_id", + "impid": "imp_id", + "price": 0.01, + "adid": "2068416", + "cid": "8048", + "crid": "24080", + "ext": { + "prebid": { + "type": "banner" + }, + "origbidcpm": 0.01 + } + } + ], + "seat": "aceex", + "group": 0 + } + ], + "cur": "USD", + "ext": { + "responsetimemillis": { + "aceex": "{{ aceex.response_time_ms }}" + }, + "prebid": { + "auctiontimestamp": 0 + }, + "tmaxrequest": 5000 + } +} 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 2a363f1ebe8..e7f4bf59732 100644 --- a/src/test/resources/org/prebid/server/it/test-application.properties +++ b/src/test/resources/org/prebid/server/it/test-application.properties @@ -1,3 +1,5 @@ +adapters.aceex.enabled=true +adapters.aceex.endpoint=http://localhost:8090/aceex-exchange adapters.acuityads.enabled=true adapters.acuityads.endpoint=http://localhost:8090/acuityads-exchange adapters.adf.enabled=true From 41e47c138ebccdf6e5f7c842ed1553c7b0adc571 Mon Sep 17 00:00:00 2001 From: Alex Maltsev Date: Tue, 14 Sep 2021 21:59:49 +0300 Subject: [PATCH 4/7] Added unit tests --- .../server/bidder/aceex/AceexBidder.java | 4 +- .../java/org/prebid/server/util/HttpUtil.java | 2 +- .../server/bidder/aceex/AceexBidderTest.java | 237 ++++++++++++++++++ 3 files changed, 241 insertions(+), 2 deletions(-) create mode 100644 src/test/java/org/prebid/server/bidder/aceex/AceexBidderTest.java diff --git a/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java b/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java index 6aaca927a5f..6d25bdad529 100644 --- a/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java +++ b/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java @@ -92,8 +92,10 @@ public final Result> makeBids(HttpCall httpCall, Bid try { final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class); return Result.withValues(extractBids(httpCall.getRequest().getPayload(), bidResponse)); - } catch (DecodeException | PreBidException e) { + } catch (DecodeException e) { return Result.withError(BidderError.badServerResponse("Bad Server Response")); + } catch (PreBidException e) { + return Result.withError(BidderError.badServerResponse(e.getMessage())); } } diff --git a/src/main/java/org/prebid/server/util/HttpUtil.java b/src/main/java/org/prebid/server/util/HttpUtil.java index 613ab6d47e7..fea60988e19 100644 --- a/src/main/java/org/prebid/server/util/HttpUtil.java +++ b/src/main/java/org/prebid/server/util/HttpUtil.java @@ -129,7 +129,7 @@ public static MultiMap headers() { */ public static void addHeaderIfValueIsNotEmpty(MultiMap headers, CharSequence headerName, CharSequence headerValue) { if (StringUtils.isNotEmpty(headerValue)) { - headers.add(headerName, headerValue); + headers.set(headerName, headerValue); } } diff --git a/src/test/java/org/prebid/server/bidder/aceex/AceexBidderTest.java b/src/test/java/org/prebid/server/bidder/aceex/AceexBidderTest.java new file mode 100644 index 00000000000..b0e351cb0ef --- /dev/null +++ b/src/test/java/org/prebid/server/bidder/aceex/AceexBidderTest.java @@ -0,0 +1,237 @@ +package org.prebid.server.bidder.aceex; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.iab.openrtb.request.BidRequest; +import com.iab.openrtb.request.Device; +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 io.vertx.core.MultiMap; +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.proto.openrtb.ext.ExtPrebid; +import org.prebid.server.proto.openrtb.ext.request.aceex.ExtImpAceex; +import org.prebid.server.util.HttpUtil; + +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +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.codehaus.groovy.runtime.InvokerHelper.asList; +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 AceexBidderTest extends VertxTest { + + private static final String ENDPOINT_URL = "https://test-url.com/?param={{AccountId}}"; + + private AceexBidder aceexBidder; + + @Before + public void setUp() { + aceexBidder = new AceexBidder(ENDPOINT_URL, jacksonMapper); + } + + @Test + public void creationShouldFailOnInvalidEndpointUrl() { + assertThatIllegalArgumentException().isThrownBy(() -> new AceexBidder("invalid_url", jacksonMapper)); + } + + @Test + public void makeHttpRequestsShouldCreateCorrectURL() { + // given + final BidRequest bidRequest = givenBidRequest(identity()); + + // when + final Result>> result = aceexBidder.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getUri) + .containsExactly("https://test-url.com/?param=accountId"); + } + + @Test + public void makeHttpRequestsShouldCorrectlyAddHeaders() { + // given + final BidRequest bidRequest = givenBidRequest(identity()); + + // when + final Result>> result = aceexBidder.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.USER_AGENT_HEADER.toString(), "ua"), + tuple(HttpUtil.X_FORWARDED_FOR_HEADER.toString(), "ip"), + tuple(HttpUtil.X_OPENRTB_VERSION_HEADER.toString(), "2.5")); + } + + @Test + public void makeHttpRequestsShouldOverrideForwardedForHeaderWithIpIfIpAndIpv6ArePresentInRequestDevice() { + // given + final BidRequest bidRequest = givenBidRequest( + bidRequestBuilder -> bidRequestBuilder.device(Device.builder().ip("ip").ipv6("ipv6").build()), + singletonList(identity())); + + // when + final Result>> result = aceexBidder.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getHeaders) + .flatExtracting(MultiMap::entries) + .extracting(Map.Entry::getKey, Map.Entry::getValue) + .filteredOn(tuple -> tuple.toList().get(0).equals(HttpUtil.X_FORWARDED_FOR_HEADER.toString())) + .containsExactly(tuple(HttpUtil.X_FORWARDED_FOR_HEADER.toString(), "ip")); + } + + @Test + public void makeBidsShouldReturnErrorIfResponseBodyCouldNotBeParsed() { + // given + final HttpCall httpCall = givenHttpCall(null, "invalid"); + + // when + final Result> result = aceexBidder.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("Bad Server Response"); + }); + } + + @Test + public void makeBidsShouldThrowErrorIfBidResponseSeatBidIsNull() throws JsonProcessingException { + // given + final HttpCall httpCall = givenHttpCall(null, + mapper.writeValueAsString(BidResponse.builder().build())); + + // when + final Result> result = aceexBidder.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("Empty SeatBid array"); + }); + } + + @Test + public void makeBidsShouldReturnNativeBidIfNativeIsPresentInRequestImp() throws JsonProcessingException { + // given + final HttpCall httpCall = givenHttpCall( + givenBidRequest(impBuilder -> impBuilder.xNative(Native.builder().build())), + mapper.writeValueAsString(givenBidResponse(bidBuilder -> bidBuilder.impid("123")))); + + // when + final Result> result = aceexBidder.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .containsExactly(BidderBid.of(Bid.builder().impid("123").build(), xNative, null)); + } + + @Test + public void makeBidsShouldReturnVideoBidIfVideoIsPresentAndNativeIsAbsentInRequestImp() + throws JsonProcessingException { + // given + final HttpCall httpCall = givenHttpCall( + givenBidRequest(impBuilder -> impBuilder.video(Video.builder().build())), + mapper.writeValueAsString(givenBidResponse(bidBuilder -> bidBuilder.impid("123")))); + + // when + final Result> result = aceexBidder.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .containsExactly(BidderBid.of(Bid.builder().impid("123").build(), video, null)); + } + + @Test + public void makeBidsShouldReturnBannerBidIfNativeAndVideoAreAbsentInRequestImp() + throws JsonProcessingException { + // given + final HttpCall httpCall = givenHttpCall( + givenBidRequest(impBuilder -> impBuilder.xNative(null).video(null)), + mapper.writeValueAsString(givenBidResponse(bidBuilder -> bidBuilder.impid("123")))); + + // when + final Result> result = aceexBidder.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .containsExactly(BidderBid.of(Bid.builder().impid("123").build(), banner, null)); + } + + private static BidRequest givenBidRequest( + Function bidRequestCustomizer, + List> impCustomizers) { + + return bidRequestCustomizer.apply(BidRequest.builder() + .device(Device.builder().ua("ua").ip("ip").ipv6("ipv6").build()) + .imp(impCustomizers.stream() + .map(AceexBidderTest::givenImp) + .collect(Collectors.toList()))) + .build(); + } + + private static BidRequest givenBidRequest(Function... impCustomizers) { + return givenBidRequest(identity(), asList(impCustomizers)); + } + + private static Imp givenImp(Function impCustomizer) { + return impCustomizer.apply(Imp.builder() + .id("123") + .ext(mapper.valueToTree(ExtPrebid.of(null, + ExtImpAceex.of("accountId"))))) + .build(); + } + + private static BidResponse givenBidResponse(Function bidCustomizer) { + return BidResponse.builder() + .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); + } +} From 075c9ba2335569800ac2378922314ef23c5d79e3 Mon Sep 17 00:00:00 2001 From: Alex Maltsev Date: Tue, 21 Sep 2021 16:05:14 +0300 Subject: [PATCH 5/7] Resolved conversation --- .../server/bidder/aceex/AceexBidder.java | 35 +++++++----------- .../java/org/prebid/server/util/HttpUtil.java | 2 +- .../server/bidder/aceex/AceexBidderTest.java | 36 +++++++++---------- 3 files changed, 29 insertions(+), 44 deletions(-) diff --git a/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java b/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java index 6d25bdad529..f1bb5d926ba 100644 --- a/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java +++ b/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java @@ -4,13 +4,11 @@ import com.iab.openrtb.request.BidRequest; import com.iab.openrtb.request.Device; 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.collections4.ListUtils; import org.prebid.server.bidder.Bidder; import org.prebid.server.bidder.model.BidderBid; import org.prebid.server.bidder.model.BidderError; @@ -49,23 +47,25 @@ public AceexBidder(String endpointUrl, JacksonMapper mapper) { @Override public Result>> makeHttpRequests(BidRequest request) { final Imp firstImp = request.getImp().get(0); + final ExtImpAceex extImpAceex; + try { - final ExtImpAceex extImpAceex = mapper.mapper().convertValue( - firstImp.getExt(), ACEEX_EXT_TYPE_REFERENCE).getBidder(); - return Result.withValue(makeHttpRequest(request, extImpAceex.getAccountId())); - } catch (DecodeException e) { + extImpAceex = mapper.mapper().convertValue(firstImp.getExt(), ACEEX_EXT_TYPE_REFERENCE).getBidder(); + } catch (IllegalArgumentException e) { return Result.withError(BidderError.badInput("Ext.bidder not provided")); } - } - private HttpRequest makeHttpRequest(BidRequest request, String accountId) { - return HttpRequest.builder() + return Result.withValue(HttpRequest.builder() .method(HttpMethod.POST) - .uri(resolveEndpoint(accountId)) + .uri(resolveEndpoint(extImpAceex.getAccountId())) .headers(constructHeaders(request)) .body(mapper.encode(request)) .payload(request) - .build(); + .build()); + } + + private String resolveEndpoint(String accountId) { + return endpointUrl.replace(ACCOUNT_ID_MACRO, HttpUtil.encodeUrl(accountId)); } private static MultiMap constructHeaders(BidRequest bidRequest) { @@ -83,10 +83,6 @@ private static MultiMap constructHeaders(BidRequest bidRequest) { return headers; } - private String resolveEndpoint(String accountId) { - return endpointUrl.replace(ACCOUNT_ID_MACRO, HttpUtil.encodeUrl(accountId)); - } - @Override public final Result> makeBids(HttpCall httpCall, BidRequest bidRequest) { try { @@ -106,14 +102,7 @@ private static List extractBids(BidRequest bidRequest, BidResponse bi throw new PreBidException("Empty SeatBid array"); } - final List bids = ListUtils.emptyIfNull(firstSeatBid.getBid()); - return bidsFromSeatBid(bids, bidRequest, bidResponse); - } - - private static List bidsFromSeatBid(List bids, - BidRequest bidRequest, - BidResponse bidResponse) { - return bids.stream() + return CollectionUtils.emptyIfNull(firstSeatBid.getBid()).stream() .filter(Objects::nonNull) .map(bid -> BidderBid.of(bid, getBidMediaType(bid.getImpid(), bidRequest.getImp()), bidResponse.getCur())) diff --git a/src/main/java/org/prebid/server/util/HttpUtil.java b/src/main/java/org/prebid/server/util/HttpUtil.java index fea60988e19..613ab6d47e7 100644 --- a/src/main/java/org/prebid/server/util/HttpUtil.java +++ b/src/main/java/org/prebid/server/util/HttpUtil.java @@ -129,7 +129,7 @@ public static MultiMap headers() { */ public static void addHeaderIfValueIsNotEmpty(MultiMap headers, CharSequence headerName, CharSequence headerValue) { if (StringUtils.isNotEmpty(headerValue)) { - headers.set(headerName, headerValue); + headers.add(headerName, headerValue); } } diff --git a/src/test/java/org/prebid/server/bidder/aceex/AceexBidderTest.java b/src/test/java/org/prebid/server/bidder/aceex/AceexBidderTest.java index b0e351cb0ef..594f1cdb57a 100644 --- a/src/test/java/org/prebid/server/bidder/aceex/AceexBidderTest.java +++ b/src/test/java/org/prebid/server/bidder/aceex/AceexBidderTest.java @@ -55,6 +55,21 @@ public void creationShouldFailOnInvalidEndpointUrl() { assertThatIllegalArgumentException().isThrownBy(() -> new AceexBidder("invalid_url", jacksonMapper)); } + @Test + public void makeHttpRequestsShouldReturnErrorsOfNotValidImps() { + // given + final BidRequest bidRequest = givenBidRequest( + impBuilder -> impBuilder + .ext(mapper.valueToTree(ExtPrebid.of(null, mapper.createArrayNode())))); + // when + final Result>> result = aceexBidder.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).hasSize(1); + assertThat(result.getErrors()) + .containsExactly(BidderError.badInput("Ext.bidder not provided")); + } + @Test public void makeHttpRequestsShouldCreateCorrectURL() { // given @@ -89,29 +104,10 @@ public void makeHttpRequestsShouldCorrectlyAddHeaders() { tuple(HttpUtil.ACCEPT_HEADER.toString(), HttpHeaderValues.APPLICATION_JSON.toString()), tuple(HttpUtil.USER_AGENT_HEADER.toString(), "ua"), tuple(HttpUtil.X_FORWARDED_FOR_HEADER.toString(), "ip"), + tuple(HttpUtil.X_FORWARDED_FOR_HEADER.toString(), "ipv6"), tuple(HttpUtil.X_OPENRTB_VERSION_HEADER.toString(), "2.5")); } - @Test - public void makeHttpRequestsShouldOverrideForwardedForHeaderWithIpIfIpAndIpv6ArePresentInRequestDevice() { - // given - final BidRequest bidRequest = givenBidRequest( - bidRequestBuilder -> bidRequestBuilder.device(Device.builder().ip("ip").ipv6("ipv6").build()), - singletonList(identity())); - - // when - final Result>> result = aceexBidder.makeHttpRequests(bidRequest); - - // then - assertThat(result.getErrors()).isEmpty(); - assertThat(result.getValue()) - .extracting(HttpRequest::getHeaders) - .flatExtracting(MultiMap::entries) - .extracting(Map.Entry::getKey, Map.Entry::getValue) - .filteredOn(tuple -> tuple.toList().get(0).equals(HttpUtil.X_FORWARDED_FOR_HEADER.toString())) - .containsExactly(tuple(HttpUtil.X_FORWARDED_FOR_HEADER.toString(), "ip")); - } - @Test public void makeBidsShouldReturnErrorIfResponseBodyCouldNotBeParsed() { // given From be2ed03964eda5078ef7a003f00656479ebda1f4 Mon Sep 17 00:00:00 2001 From: Alex Maltsev Date: Tue, 21 Sep 2021 16:12:45 +0300 Subject: [PATCH 6/7] Fixed errors after merge of master --- .../org/prebid/server/bidder/aceex/AceexBidder.java | 10 +++++----- .../prebid/server/bidder/aceex/AceexBidderTest.java | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java b/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java index f1bb5d926ba..21701dd79dd 100644 --- a/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java +++ b/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java @@ -22,7 +22,7 @@ import org.prebid.server.proto.openrtb.ext.request.aceex.ExtImpAceex; import org.prebid.server.proto.openrtb.ext.response.BidType; import org.prebid.server.util.HttpUtil; -import org.prebid.server.util.ObjectUtils; +import org.prebid.server.util.ObjectUtil; import java.util.List; import java.util.Objects; @@ -74,11 +74,11 @@ private static MultiMap constructHeaders(BidRequest bidRequest) { headers.set(HttpUtil.X_OPENRTB_VERSION_HEADER, X_OPENRTB_VERSION); HttpUtil.addHeaderIfValueIsNotEmpty(headers, HttpUtil.USER_AGENT_HEADER, - ObjectUtils.getIfNotNull(device, Device::getUa)); + ObjectUtil.getIfNotNull(device, Device::getUa)); HttpUtil.addHeaderIfValueIsNotEmpty(headers, HttpUtil.X_FORWARDED_FOR_HEADER, - ObjectUtils.getIfNotNull(device, Device::getIpv6)); + ObjectUtil.getIfNotNull(device, Device::getIpv6)); HttpUtil.addHeaderIfValueIsNotEmpty(headers, HttpUtil.X_FORWARDED_FOR_HEADER, - ObjectUtils.getIfNotNull(device, Device::getIp)); + ObjectUtil.getIfNotNull(device, Device::getIp)); return headers; } @@ -96,7 +96,7 @@ public final Result> makeBids(HttpCall httpCall, Bid } private static List extractBids(BidRequest bidRequest, BidResponse bidResponse) { - final List seatBids = ObjectUtils.getIfNotNull(bidResponse, BidResponse::getSeatbid); + final List seatBids = ObjectUtil.getIfNotNull(bidResponse, BidResponse::getSeatbid); final SeatBid firstSeatBid = CollectionUtils.isNotEmpty(seatBids) ? seatBids.get(0) : null; if (firstSeatBid == null) { throw new PreBidException("Empty SeatBid array"); diff --git a/src/test/java/org/prebid/server/bidder/aceex/AceexBidderTest.java b/src/test/java/org/prebid/server/bidder/aceex/AceexBidderTest.java index 594f1cdb57a..8b31987e8f0 100644 --- a/src/test/java/org/prebid/server/bidder/aceex/AceexBidderTest.java +++ b/src/test/java/org/prebid/server/bidder/aceex/AceexBidderTest.java @@ -29,12 +29,12 @@ import java.util.function.Function; import java.util.stream.Collectors; +import static java.util.Arrays.asList; 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.codehaus.groovy.runtime.InvokerHelper.asList; 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; From dbc8da5e669f0b1654f19e92a15af5fad359c3ad Mon Sep 17 00:00:00 2001 From: Alex Maltsev Date: Mon, 27 Sep 2021 12:26:47 +0300 Subject: [PATCH 7/7] Resolved conversation --- .../prebid/server/bidder/aceex/AceexBidder.java | 1 - src/main/resources/bidder-config/aceex.yaml | 6 ------ .../server/bidder/aceex/AceexBidderTest.java | 15 ++++----------- 3 files changed, 4 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java b/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java index 21701dd79dd..9d35c0e54a4 100644 --- a/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java +++ b/src/main/java/org/prebid/server/bidder/aceex/AceexBidder.java @@ -106,7 +106,6 @@ private static List extractBids(BidRequest bidRequest, BidResponse bi .filter(Objects::nonNull) .map(bid -> BidderBid.of(bid, getBidMediaType(bid.getImpid(), bidRequest.getImp()), bidResponse.getCur())) - .filter(Objects::nonNull) .collect(Collectors.toList()); } diff --git a/src/main/resources/bidder-config/aceex.yaml b/src/main/resources/bidder-config/aceex.yaml index 9fef0e464db..d16691ed283 100644 --- a/src/main/resources/bidder-config/aceex.yaml +++ b/src/main/resources/bidder-config/aceex.yaml @@ -1,12 +1,6 @@ adapters: aceex: - enabled: false endpoint: http://bl-us.aceex.io/?uqhash={{AccountId}} - pbs-enforces-gdpr: true - pbs-enforces-ccpa: true - modifying-vast-xml-allowed: true - deprecated-names: - aliases: {} meta-info: maintainer-email: tech@aceex.io app-media-types: diff --git a/src/test/java/org/prebid/server/bidder/aceex/AceexBidderTest.java b/src/test/java/org/prebid/server/bidder/aceex/AceexBidderTest.java index 8b31987e8f0..072e91876e3 100644 --- a/src/test/java/org/prebid/server/bidder/aceex/AceexBidderTest.java +++ b/src/test/java/org/prebid/server/bidder/aceex/AceexBidderTest.java @@ -65,7 +65,6 @@ public void makeHttpRequestsShouldReturnErrorsOfNotValidImps() { final Result>> result = aceexBidder.makeHttpRequests(bidRequest); // then - assertThat(result.getErrors()).hasSize(1); assertThat(result.getErrors()) .containsExactly(BidderError.badInput("Ext.bidder not provided")); } @@ -118,11 +117,8 @@ public void makeBidsShouldReturnErrorIfResponseBodyCouldNotBeParsed() { // 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("Bad Server Response"); - }); + assertThat(result.getErrors()) + .containsExactly(BidderError.badServerResponse("Bad Server Response")); } @Test @@ -136,11 +132,8 @@ public void makeBidsShouldThrowErrorIfBidResponseSeatBidIsNull() throws JsonProc // 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("Empty SeatBid array"); - }); + assertThat(result.getErrors()) + .containsExactly(BidderError.badServerResponse("Empty SeatBid array")); } @Test