diff --git a/src/main/java/org/prebid/server/bidder/adagio/AdagioBidder.java b/src/main/java/org/prebid/server/bidder/adagio/AdagioBidder.java new file mode 100644 index 00000000000..460d6ac2e4e --- /dev/null +++ b/src/main/java/org/prebid/server/bidder/adagio/AdagioBidder.java @@ -0,0 +1,101 @@ +package org.prebid.server.bidder.adagio; + +import com.iab.openrtb.request.BidRequest; +import com.iab.openrtb.response.Bid; +import com.iab.openrtb.response.BidResponse; +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.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.exception.PreBidException; +import org.prebid.server.json.DecodeException; +import org.prebid.server.json.JacksonMapper; +import org.prebid.server.proto.openrtb.ext.response.BidType; +import org.prebid.server.proto.openrtb.ext.response.ExtBidPrebid; +import org.prebid.server.util.BidderUtil; +import org.prebid.server.util.HttpUtil; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Objects; + +public class AdagioBidder implements Bidder { + + private final String endpointUrl; + private final JacksonMapper mapper; + + public AdagioBidder(String endpointUrl, JacksonMapper mapper) { + this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl)); + this.mapper = Objects.requireNonNull(mapper); + } + + @Override + public Result>> makeHttpRequests(BidRequest request) { + return Result.withValue(BidderUtil.defaultRequest(request, endpointUrl, mapper)); + } + + @Override + public Result> makeBids(BidderCall httpCall, BidRequest bidRequest) { + final List errors = new ArrayList<>(); + try { + final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class); + return Result.of(extractBids(bidResponse, errors), errors); + } catch (DecodeException e) { + return Result.withError(BidderError.badServerResponse(e.getMessage())); + } + } + + private List extractBids(BidResponse bidResponse, List errors) { + if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) { + errors.add(BidderError.badServerResponse("empty seatbid array")); + return Collections.emptyList(); + } + + return bidResponse.getSeatbid().stream() + .filter(Objects::nonNull) + .flatMap(seatBid -> seatBid.getBid().stream() + .filter(Objects::nonNull) + .map(bid -> makeBid(bid, bidResponse.getCur(), seatBid.getSeat(), errors))) + .filter(Objects::nonNull) + .toList(); + } + + private BidderBid makeBid(Bid bid, String currency, String seat, List errors) { + try { + final ExtBidPrebid extBidPrebid = parseBidExt(bid); + return BidderBid.builder() + .bid(bid) + .type(getBidType(bid)) + .bidCurrency(currency) + .videoInfo(extBidPrebid != null ? extBidPrebid.getVideo() : null) + .seat(seat) + .build(); + + } catch (PreBidException e) { + errors.add(BidderError.badServerResponse(e.getMessage())); + return null; + } + } + + private ExtBidPrebid parseBidExt(Bid bid) { + try { + return mapper.mapper().convertValue(bid.getExt(), ExtBidPrebid.class); + } catch (IllegalArgumentException e) { + throw new PreBidException("bid.ext can not be parsed"); + } + } + + private static BidType getBidType(Bid bid) { + return switch (bid.getMtype()) { + case 1 -> BidType.banner; + case 2 -> BidType.video; + case 4 -> BidType.xNative; + case null, default -> throw new PreBidException( + "Could not define media type for impression: " + bid.getImpid()); + }; + } +} diff --git a/src/main/java/org/prebid/server/proto/openrtb/ext/request/adagio/ExtImpAdagio.java b/src/main/java/org/prebid/server/proto/openrtb/ext/request/adagio/ExtImpAdagio.java new file mode 100644 index 00000000000..37db89821b5 --- /dev/null +++ b/src/main/java/org/prebid/server/proto/openrtb/ext/request/adagio/ExtImpAdagio.java @@ -0,0 +1,17 @@ +package org.prebid.server.proto.openrtb.ext.request.adagio; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Value; + +@Value(staticConstructor = "of") +public class ExtImpAdagio { + + @JsonProperty("organizationId") + String organizationId; + + String placement; + + String pagetype; + + String category; +} diff --git a/src/main/java/org/prebid/server/spring/config/bidder/AdagioConfiguration.java b/src/main/java/org/prebid/server/spring/config/bidder/AdagioConfiguration.java new file mode 100644 index 00000000000..cd4544370a8 --- /dev/null +++ b/src/main/java/org/prebid/server/spring/config/bidder/AdagioConfiguration.java @@ -0,0 +1,41 @@ +package org.prebid.server.spring.config.bidder; + +import org.prebid.server.bidder.BidderDeps; +import org.prebid.server.bidder.adagio.AdagioBidder; +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 jakarta.validation.constraints.NotBlank; + +@Configuration +@PropertySource(value = "classpath:/bidder-config/adagio.yaml", factory = YamlPropertySourceFactory.class) +public class AdagioConfiguration { + + private static final String BIDDER_NAME = "adagio"; + + @Bean("adagioConfigurationProperties") + @ConfigurationProperties("adapters.adagio") + BidderConfigurationProperties configurationProperties() { + return new BidderConfigurationProperties(); + } + + @Bean + BidderDeps adagioBidderDeps(BidderConfigurationProperties adagioConfigurationProperties, + @NotBlank @Value("${external-url}") String externalUrl, + JacksonMapper mapper) { + + return BidderDepsAssembler.forBidder(BIDDER_NAME) + .withConfig(adagioConfigurationProperties) + .usersyncerCreator(UsersyncerCreator.create(externalUrl)) + .bidderCreator(config -> new AdagioBidder(config.getEndpoint(), mapper)) + .assemble(); + } +} diff --git a/src/main/resources/bidder-config/adagio.yaml b/src/main/resources/bidder-config/adagio.yaml new file mode 100644 index 00000000000..c252b39c5af --- /dev/null +++ b/src/main/resources/bidder-config/adagio.yaml @@ -0,0 +1,22 @@ +adapters: + adagio: + # Please deploy this config in each of your datacenters with the appropriate regional subdomain. + # Replace the `REGION` by one of the value below: + # - For AMER: las => (https://mp-las.4dex.io/pbserver) + # - For EMEA: ams => (https://mp-ams.4dex.io/pbserver) + # - For APAC: tyo => (https://mp-tyo.4dex.io/pbserver) + endpoint: https://mp-REGION.4dex.io/pbserver + ortb-version: "2.6" + endpoint-compression: gzip + meta-info: + maintainer-email: dev@adagio.io + app-media-types: + - banner + - video + - native + site-media-types: + - banner + - video + - native + supported-vendors: + vendor-id: 617 diff --git a/src/main/resources/static/bidder-params/adagio.json b/src/main/resources/static/bidder-params/adagio.json new file mode 100644 index 00000000000..bacb62125ca --- /dev/null +++ b/src/main/resources/static/bidder-params/adagio.json @@ -0,0 +1,31 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Adagio Adapter Params", + "description": "A schema which validates params accepted by the Adagio adapter", + "type": "object", + "properties": { + "organizationId": { + "type": "string", + "description": "Id of the Organization. Handed out by Adagio." + }, + "placement": { + "type": "string", + "description": "Refers to the placement of an adunit in a page. Must not contain any information about the type of device.", + "maxLength": 30 + }, + "pagetype": { + "type": "string", + "description": "Describes what kind of content will be present in the page.", + "maxLength": 30 + }, + "category": { + "type": "string", + "description": "Category of the content displayed in the page.", + "maxLength": 30 + } + }, + "required": [ + "organizationId", + "placement" + ] +} diff --git a/src/test/java/org/prebid/server/bidder/adagio/AdagioBidderTest.java b/src/test/java/org/prebid/server/bidder/adagio/AdagioBidderTest.java new file mode 100644 index 00000000000..e4b7440f6cd --- /dev/null +++ b/src/test/java/org/prebid/server/bidder/adagio/AdagioBidderTest.java @@ -0,0 +1,245 @@ +package org.prebid.server.bidder.adagio; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.iab.openrtb.request.BidRequest; +import com.iab.openrtb.request.Imp; +import com.iab.openrtb.response.Bid; +import com.iab.openrtb.response.BidResponse; +import com.iab.openrtb.response.SeatBid; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import org.prebid.server.VertxTest; +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.proto.openrtb.ext.response.BidType; +import org.prebid.server.proto.openrtb.ext.response.ExtBidPrebid; +import org.prebid.server.proto.openrtb.ext.response.ExtBidPrebidVideo; + +import java.math.BigDecimal; +import java.util.List; +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.bidder.model.BidderError.badServerResponse; +import static org.prebid.server.util.HttpUtil.ACCEPT_HEADER; +import static org.prebid.server.util.HttpUtil.APPLICATION_JSON_CONTENT_TYPE; +import static org.prebid.server.util.HttpUtil.CONTENT_TYPE_HEADER; +import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON_VALUE; + +@ExtendWith(MockitoExtension.class) +public class AdagioBidderTest extends VertxTest { + + private static final String ENDPOINT_URL = "https://test.endpoint.com"; + + private final AdagioBidder target = new AdagioBidder(ENDPOINT_URL, jacksonMapper); + + @Test + public void creationShouldFailOnInvalidEndpointUrl() { + assertThatIllegalArgumentException().isThrownBy(() -> new AdagioBidder("invalid_url", jacksonMapper)); + } + + @Test + public void makeHttpRequestsShouldCreateExpectedUrl() { + // given + final BidRequest bidRequest = givenBidRequest(identity()); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).hasSize(1) + .extracting(HttpRequest::getUri) + .containsExactly("https://test.endpoint.com"); + } + + @Test + public void makeHttpRequestsShouldReturnExpectedHeaders() { + // given + final BidRequest bidRequest = givenBidRequest(identity()); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getValue()).hasSize(1).first() + .extracting(HttpRequest::getHeaders) + .satisfies(headers -> assertThat(headers.get(CONTENT_TYPE_HEADER)) + .isEqualTo(APPLICATION_JSON_CONTENT_TYPE)) + .satisfies(headers -> assertThat(headers.get(ACCEPT_HEADER)) + .isEqualTo(APPLICATION_JSON_VALUE)); + assertThat(result.getErrors()).isEmpty(); + } + + @Test + public void makeHttpRequestsShouldReturnExpectedBody() { + // given + final BidRequest bidRequest = givenBidRequest(imp -> imp.id("imp")); + + // when + final Result>> results = target.makeHttpRequests(bidRequest); + + // then + assertThat(results.getValue()).hasSize(1) + .extracting(HttpRequest::getBody, HttpRequest::getPayload) + .containsExactly(tuple(jacksonMapper.encodeToBytes(bidRequest), bidRequest)); + assertThat(results.getErrors()).isEmpty(); + } + + @Test + public void makeBidsShouldReturnErrorWhenResponseBodyCouldNotBeParsed() { + // given + final BidderCall httpCall = givenHttpCall("invalid_json"); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).hasSize(1) + .allSatisfy(error -> { + assertThat(error.getType()).isEqualTo(BidderError.Type.bad_server_response); + assertThat(error.getMessage()).startsWith("Failed to decode: Unrecognized token 'invalid_json'"); + }); + assertThat(result.getValue()).isEmpty(); + } + + @Test + public void makeBidsShouldReturnErrorWhenBidResponseOrSeatBidAreNull() throws JsonProcessingException { + // given + final BidderCall invalidHttpCall = givenHttpCall(mapper.writeValueAsString(null)); + + // when + final Result> result = target.makeBids(invalidHttpCall, null); + + // then + assertThat(result.getErrors()).hasSize(1).containsExactly(badServerResponse("empty seatbid array")); + assertThat(result.getValue()).isEmpty(); + } + + @Test + public void makeBidsShouldReturnBannerBidWhenMtypeIs1() throws JsonProcessingException { + // given + final Bid bannerBid = givenBid(1); + final BidderCall httpCall = givenHttpCall(givenBidResponse(bannerBid)); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).hasSize(1) + .containsExactly(BidderBid.of(bannerBid, BidType.banner, "adagio", "USD")); + } + + @Test + public void makeBidsShouldReturnVideoBidWhenMtypeIs2() throws JsonProcessingException { + // given + final Bid videoBid = givenBid(2, ext -> ext.video(ExtBidPrebidVideo.of(10, "cat"))); + final BidderCall httpCall = givenHttpCall(givenBidResponse(videoBid)); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).hasSize(1) + .containsExactly(BidderBid.builder() + .type(BidType.video) + .seat("adagio") + .bidCurrency("USD") + .bid(videoBid) + .videoInfo(ExtBidPrebidVideo.of(10, "cat")) + .build()); + } + + @Test + public void makeBidsShouldReturnNativeBidWhenMtypeIs4() throws JsonProcessingException { + // given + final Bid nativeBid = givenBid(4); + final BidderCall httpCall = givenHttpCall(givenBidResponse(nativeBid)); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).hasSize(1) + .containsExactly(BidderBid.of(nativeBid, BidType.xNative, "adagio", "USD")); + } + + @Test + public void makeBidsShouldReturnErrorWhenMtypeIsMissing() throws JsonProcessingException { + // given + final Bid bidWithMissingMtype = givenBid(null); + final BidderCall httpCall = givenHttpCall(givenBidResponse(bidWithMissingMtype)); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getValue()).isEmpty(); + assertThat(result.getErrors()).hasSize(1) + .containsExactly(BidderError.badServerResponse("Could not define media type for impression: impId")); + } + + @Test + public void makeBidsShouldReturnErrorWhenMtypeIsUnsupported() throws JsonProcessingException { + // given + final Bid bidWithUnsupportedMtype = givenBid(3); + final BidderCall httpCall = givenHttpCall(givenBidResponse(bidWithUnsupportedMtype)); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getValue()).isEmpty(); + assertThat(result.getErrors()).hasSize(1) + .containsExactly(BidderError.badServerResponse("Could not define media type for impression: impId")); + } + + private static BidRequest givenBidRequest(UnaryOperator impCustomizer) { + return BidRequest.builder().imp(singletonList(givenImp(impCustomizer))).build(); + } + + private static Imp givenImp(UnaryOperator impCustomizer) { + return impCustomizer.apply(Imp.builder().id("impId")).build(); + } + + private static Bid givenBid(Integer mtype) { + return givenBid(mtype, identity()); + } + + private static Bid givenBid(Integer mtype, UnaryOperator extCustomizer) { + final ExtBidPrebid extBidPrebid = extCustomizer.apply(ExtBidPrebid.builder()).build(); + return Bid.builder() + .id("bidId") + .impid("impId") + .price(BigDecimal.ONE) + .mtype(mtype) + .ext(mapper.valueToTree(extBidPrebid)) + .build(); + } + + private static String givenBidResponse(Bid... bids) throws JsonProcessingException { + return mapper.writeValueAsString(BidResponse.builder() + .cur("USD") + .seatbid(singletonList(SeatBid.builder().seat("adagio").bid(List.of(bids)).build())) + .build()); + } + + private static BidderCall givenHttpCall(String body) { + return BidderCall.succeededHttp( + HttpRequest.builder().build(), + HttpResponse.of(200, null, body), + null); + } +} diff --git a/src/test/java/org/prebid/server/it/AdagioTest.java b/src/test/java/org/prebid/server/it/AdagioTest.java new file mode 100644 index 00000000000..01279362d4f --- /dev/null +++ b/src/test/java/org/prebid/server/it/AdagioTest.java @@ -0,0 +1,34 @@ +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 java.util.List; + +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; + +public class AdagioTest extends IntegrationTest { + + @Test + public void openrtb2AuctionShouldRespondWithBidsFromAdagio() throws IOException, JSONException { + // given + WIRE_MOCK_RULE.stubFor(post(urlPathEqualTo("/adagio-exchange")) + .withRequestBody(equalToJson(jsonFrom("openrtb2/adagio/test-adagio-bid-request.json"))) + .willReturn(aResponse().withBody(jsonFrom("openrtb2/adagio/test-adagio-bid-response.json")))); + + // when + final Response response = responseFor( + "openrtb2/adagio/test-auction-adagio-request.json", + Endpoint.openrtb2_auction); + + // then + assertJsonEquals("openrtb2/adagio/test-auction-adagio-response.json", response, List.of("adagio")); + } + +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/adagio/test-adagio-bid-request.json b/src/test/resources/org/prebid/server/it/openrtb2/adagio/test-adagio-bid-request.json new file mode 100644 index 00000000000..607314934f3 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/adagio/test-adagio-bid-request.json @@ -0,0 +1,55 @@ +{ + "id": "request_id", + "imp": [ + { + "id": "imp_id", + "secure": 1, + "banner": { + "w": 300, + "h": 250 + }, + "ext": { + "tid": "${json-unit.any-string}", + "bidder": { + "organizationId" : "organizationId", + "placement": "placement" + } + } + } + ], + "source": { + "tid": "${json-unit.any-string}" + }, + "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" + ], + "regs": { + "gdpr": 0 + }, + "ext": { + "prebid": { + "server": { + "externalurl": "http://localhost:8080", + "gvlid": 1, + "datacenter": "local", + "endpoint": "/openrtb2/auction" + } + } + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/adagio/test-adagio-bid-response.json b/src/test/resources/org/prebid/server/it/openrtb2/adagio/test-adagio-bid-response.json new file mode 100644 index 00000000000..940bae9975c --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/adagio/test-adagio-bid-response.json @@ -0,0 +1,21 @@ +{ + "id": "request_id", + "seatbid": [ + { + "bid": [ + { + "id": "bid_id", + "impid": "imp_id", + "price": 3.33, + "adid": "adid001", + "crid": "crid001", + "cid": "cid001", + "adm": "adm001", + "mtype": 2, + "h": 250, + "w": 300 + } + ] + } + ] +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/adagio/test-auction-adagio-request.json b/src/test/resources/org/prebid/server/it/openrtb2/adagio/test-auction-adagio-request.json new file mode 100644 index 00000000000..3a07c1ece20 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/adagio/test-auction-adagio-request.json @@ -0,0 +1,22 @@ +{ + "id": "request_id", + "imp": [ + { + "id": "imp_id", + "banner": { + "w": 300, + "h": 250 + }, + "ext": { + "adagio": { + "organizationId" : "organizationId", + "placement": "placement" + } + } + } + ], + "tmax": 5000, + "regs": { + "gdpr": 0 + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/adagio/test-auction-adagio-response.json b/src/test/resources/org/prebid/server/it/openrtb2/adagio/test-auction-adagio-response.json new file mode 100644 index 00000000000..7361ed22f23 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/adagio/test-auction-adagio-response.json @@ -0,0 +1,43 @@ +{ + "id": "request_id", + "seatbid": [ + { + "bid": [ + { + "id": "bid_id", + "impid": "imp_id", + "exp": 1500, + "price": 3.33, + "adm": "adm001", + "adid": "adid001", + "cid": "cid001", + "crid": "crid001", + "mtype": 2, + "w": 300, + "h": 250, + "ext": { + "origbidcpm": 3.33, + "prebid": { + "type": "video", + "meta": { + "adaptercode": "adagio" + } + } + } + } + ], + "seat": "adagio", + "group": 0 + } + ], + "cur": "USD", + "ext": { + "responsetimemillis": { + "adagio": "{{ adagio.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 717b0c09445..c232162d982 100644 --- a/src/test/resources/org/prebid/server/it/test-application.properties +++ b/src/test/resources/org/prebid/server/it/test-application.properties @@ -20,6 +20,8 @@ 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.adagio.enabled=true +adapters.adagio.endpoint=http://localhost:8090/adagio-exchange adapters.adelement.enabled=true adapters.adelement.endpoint=http://localhost:8090/adelement-exchange adapters.adf.enabled=true