diff --git a/src/main/java/org/prebid/server/bidder/aax/AaxBidder.java b/src/main/java/org/prebid/server/bidder/aax/AaxBidder.java new file mode 100644 index 00000000000..3fd232428b2 --- /dev/null +++ b/src/main/java/org/prebid/server/bidder/aax/AaxBidder.java @@ -0,0 +1,90 @@ +package org.prebid.server.bidder.aax; + +import com.iab.openrtb.request.BidRequest; +import com.iab.openrtb.request.Imp; +import com.iab.openrtb.response.BidResponse; +import com.iab.openrtb.response.SeatBid; +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.json.DecodeException; +import org.prebid.server.json.JacksonMapper; +import org.prebid.server.proto.openrtb.ext.response.BidType; +import org.prebid.server.util.HttpUtil; + +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + +public class AaxBidder implements Bidder { + + private final String endpointUrl; + private final JacksonMapper mapper; + + public AaxBidder(String endpointUrl, JacksonMapper mapper) { + this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl)); + this.mapper = Objects.requireNonNull(mapper); + } + + @Override + public Result>> makeHttpRequests(BidRequest bidRequest) { + return Result.withValue(HttpRequest.builder() + .method(HttpMethod.POST) + .headers(HttpUtil.headers()) + .uri(endpointUrl) + .body(mapper.encodeToBytes(bidRequest)) + .payload(bidRequest) + .build()); + } + + @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 e) { + return Result.withError(BidderError.badServerResponse(e.getMessage())); + } + } + + private static List extractBids(BidRequest bidRequest, BidResponse bidResponse) { + if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) { + return Collections.emptyList(); + } + + final String currency = bidResponse.getCur(); + return bidResponse.getSeatbid().stream() + .filter(Objects::nonNull) + .map(SeatBid::getBid) + .filter(Objects::nonNull) + .flatMap(Collection::stream) + .filter(Objects::nonNull) + .map(bid -> BidderBid.of(bid, resolveBidType(bid.getImpid(), bidRequest.getImp()), currency)) + .collect(Collectors.toList()); + } + + private static BidType resolveBidType(String impId, List imps) { + for (Imp imp : imps) { + if (Objects.equals(impId, imp.getId())) { + if (imp.getBanner() != null) { + return BidType.banner; + } else if (imp.getVideo() != null) { + return BidType.video; + } else if (imp.getXNative() != null) { + return BidType.xNative; + } else if (imp.getAudio() != null) { + return BidType.audio; + } + } + } + + return BidType.banner; + } +} diff --git a/src/main/java/org/prebid/server/spring/config/bidder/AaxConfiguration.java b/src/main/java/org/prebid/server/spring/config/bidder/AaxConfiguration.java new file mode 100644 index 00000000000..615c581adcc --- /dev/null +++ b/src/main/java/org/prebid/server/spring/config/bidder/AaxConfiguration.java @@ -0,0 +1,47 @@ +package org.prebid.server.spring.config.bidder; + +import org.prebid.server.bidder.BidderDeps; +import org.prebid.server.bidder.aax.AaxBidder; +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.prebid.server.util.HttpUtil; +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/aax.yaml", factory = YamlPropertySourceFactory.class) +public class AaxConfiguration { + + private static final String BIDDER_NAME = "aax"; + private static final String EXTERNAL_URL_MACRO = "{{PREBID_SERVER_ENDPOINT}}"; + + @Bean("aaxConfigurationProperties") + @ConfigurationProperties("adapters.aax") + BidderConfigurationProperties configurationProperties() { + return new BidderConfigurationProperties(); + } + + @Bean + BidderDeps aaxBidderDeps(BidderConfigurationProperties aaxConfigurationProperties, + @NotBlank @Value("${external-url}") String externalUrl, + JacksonMapper mapper) { + + return BidderDepsAssembler.forBidder(BIDDER_NAME) + .withConfig(aaxConfigurationProperties) + .usersyncerCreator(UsersyncerCreator.create(externalUrl)) + .bidderCreator(config -> new AaxBidder(resolveEndpoint(config.getEndpoint(), externalUrl), mapper)) + .assemble(); + } + + private String resolveEndpoint(String configEndpoint, String externalUrl) { + return configEndpoint.replace(EXTERNAL_URL_MACRO, HttpUtil.encodeUrl(externalUrl)); + } +} diff --git a/src/main/resources/bidder-config/aax.yaml b/src/main/resources/bidder-config/aax.yaml new file mode 100644 index 00000000000..e6bda296d94 --- /dev/null +++ b/src/main/resources/bidder-config/aax.yaml @@ -0,0 +1,21 @@ +adapters: + aax: + endpoint: https://prebid.aaxads.com/rtb/pb/aax-prebid?src={{PREBID_SERVER_ENDPOINT}} + meta-info: + maintainer-email: product@aax.media + app-media-types: + - banner + - video + - native + site-media-types: + - banner + - video + - native + supported-vendors: + vendor-id: 720 + usersync: + url: https://c.aaxads.com/aacxc.php?fv=1&wbsh=psa&ryvlg=setstatuscode&redirect= + redirect-url: /setuid?bidder=aax&gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&us_privacy={{us_privacy}}&uid= + cookie-family-name: aax + type: redirect + support-cors: false diff --git a/src/main/resources/static/bidder-params/aax.json b/src/main/resources/static/bidder-params/aax.json new file mode 100644 index 00000000000..83cdfc59406 --- /dev/null +++ b/src/main/resources/static/bidder-params/aax.json @@ -0,0 +1,20 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Aax Adapter Params", + "description": "A schema which validates params accepted by the Aax adapter", + "type": "object", + "properties": { + "cid": { + "type": "string", + "description": "The customer id provided by AAX." + }, + "crid": { + "type": "string", + "description": "The placement id provided by AAX." + } + }, + "required": [ + "cid", + "crid" + ] +} diff --git a/src/test/java/org/prebid/server/bidder/aax/AaxBidderTest.java b/src/test/java/org/prebid/server/bidder/aax/AaxBidderTest.java new file mode 100644 index 00000000000..2ec81cc06c9 --- /dev/null +++ b/src/test/java/org/prebid/server/bidder/aax/AaxBidderTest.java @@ -0,0 +1,145 @@ +package org.prebid.server.bidder.aax; + +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.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 java.util.List; +import java.util.function.Function; + +import static java.util.Collections.singletonList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.prebid.server.proto.openrtb.ext.response.BidType.banner; + +public class AaxBidderTest extends VertxTest { + + private static final String ENDPOINT_URL = "https://test.aax.net?src=external.prebidserver.com"; + + private AaxBidder aaxBidder; + + @Before + public void setup() { + aaxBidder = new AaxBidder(ENDPOINT_URL, jacksonMapper); + } + + @Test + public void creationShouldFailOnInvalidEndpointUrl() { + assertThatIllegalArgumentException() + .isThrownBy(() -> new AaxBidder("invalid_url", jacksonMapper)); + } + + @Test + public void makeHttpRequestsShouldNotModifyIncomingRequest() { + // given + final BidRequest bidRequest = givenBidRequest(); + + // when + final Result>> result; + result = aaxBidder.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).hasSize(1) + .extracting(httpRequest -> mapper.readValue(httpRequest.getBody(), BidRequest.class)) + .containsExactly(bidRequest); + } + + @Test + public void makeBidsShouldReturnErrorIfResponseBodyCouldNotBeParsed() { + // given + final HttpCall httpCall = sampleHttpCall(givenBidRequest(), "invalid response"); + + // when + final Result> result = aaxBidder.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).hasSize(1) + .allMatch(error -> error.getType() == BidderError.Type.bad_server_response + && error.getMessage().startsWith("Failed to decode: Unrecognized token")); + } + + @Test + public void makeBidsShouldReturnEmptyListIfBidResponseIsNull() throws JsonProcessingException { + // given + final HttpCall httpCall; + httpCall = sampleHttpCall(givenBidRequest(), mapper.writeValueAsString(null)); + + // when + final Result> result = aaxBidder.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).isEmpty(); + } + + @Test + public void makeBidsShouldReturnEmptyListIfBidResponseSeatBidIsNull() throws JsonProcessingException { + // given + final HttpCall httpCall; + httpCall = sampleHttpCall(null, mapper.writeValueAsString(BidResponse.builder().build())); + + // when + final Result> result = aaxBidder.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).isEmpty(); + } + + @Test + public void makeBidsShouldReturnBannerBidIfBannerIsPresent() throws JsonProcessingException { + // given + final HttpCall httpCall = sampleHttpCall( + givenBidRequest(), + mapper.writeValueAsString(sampleBidResponse(bidBuilder -> bidBuilder.impid("123")))); + + // when + final Result> result = aaxBidder.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .containsExactly(BidderBid.of(Bid.builder().impid("123").build(), banner, "USD")); + } + + private static BidResponse sampleBidResponse(Function bidCustomizer) { + return BidResponse.builder() + .cur("USD") + .seatbid(singletonList(SeatBid.builder() + .bid(singletonList(bidCustomizer.apply(Bid.builder()).build())) + .build())) + .build(); + } + + private static HttpCall sampleHttpCall(BidRequest bidRequest, String body) { + return HttpCall.success( + HttpRequest.builder().payload(bidRequest).build(), + HttpResponse.of(200, null, body), + null); + } + + private static BidRequest givenBidRequest() { + return BidRequest.builder() + .id("request_id") + .imp(singletonList(Imp.builder() + .id("imp_id") + .ext(mapper.valueToTree(ExtPrebid.of(null, mapper.createObjectNode()))) + .build())) + .build(); + } +} diff --git a/src/test/java/org/prebid/server/it/AaxTest.java b/src/test/java/org/prebid/server/it/AaxTest.java new file mode 100644 index 00000000000..01f4061995f --- /dev/null +++ b/src/test/java/org/prebid/server/it/AaxTest.java @@ -0,0 +1,36 @@ +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 AaxTest extends IntegrationTest { + + @Test + public void openrtb2AuctionShouldRespondWithBidsFromTheAax() throws IOException, JSONException { + // given + WIRE_MOCK_RULE.stubFor(post(urlPathEqualTo("/aax-exchange")) + .withRequestBody(equalToJson(jsonFrom("openrtb2/aax/test-aax-bid-request.json"))) + .willReturn(aResponse().withBody(jsonFrom("openrtb2/aax/test-aax-bid-response.json")))); + + // when + final Response response = responseFor("openrtb2/aax/test-auction-aax-request.json", + Endpoint.openrtb2_auction); + + // then + assertJsonEquals("openrtb2/aax/test-auction-aax-response.json", response, + singletonList("aax")); + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/aax/test-aax-bid-request.json b/src/test/resources/org/prebid/server/it/openrtb2/aax/test-aax-bid-request.json new file mode 100644 index 00000000000..ba09e5f9846 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/aax/test-aax-bid-request.json @@ -0,0 +1,42 @@ +{ + "id": "test-request-id", + "imp": [ + { + "id": "test-imp-id", + "banner": { + "w": 300, + "h": 250 + }, + "ext": { + "bidder": { + "cid": "AAXCID", + "crid": "12345678" + } + } + } + ], + "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/aax/test-aax-bid-response.json b/src/test/resources/org/prebid/server/it/openrtb2/aax/test-aax-bid-response.json new file mode 100644 index 00000000000..4b1ecda6ece --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/aax/test-aax-bid-response.json @@ -0,0 +1,22 @@ + { + "id": "tid", + "seatbid": [ + { + "seat": "aax", + "bid": [ + { + "id": "randomid", + "impid": "test-imp-id", + "price": 0.500000, + "adid": "12345678", + "adm": "some-test-ad", + "cid": "987", + "crid": "12345678", + "h": 250, + "w": 300 + } + ] + } + ], + "bidid": "bid01" +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/aax/test-auction-aax-request.json b/src/test/resources/org/prebid/server/it/openrtb2/aax/test-auction-aax-request.json new file mode 100644 index 00000000000..7e76123f01b --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/aax/test-auction-aax-request.json @@ -0,0 +1,24 @@ +{ + "id": "test-request-id", + "imp": [ + { + "id": "test-imp-id", + "banner": { + "w": 300, + "h": 250 + }, + "ext": { + "aax": { + "cid": "AAXCID", + "crid": "12345678" + } + } + } + ], + "tmax": 5000, + "regs": { + "ext": { + "gdpr": 0 + } + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/aax/test-auction-aax-response.json b/src/test/resources/org/prebid/server/it/openrtb2/aax/test-auction-aax-response.json new file mode 100644 index 00000000000..510834b5e2e --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/aax/test-auction-aax-response.json @@ -0,0 +1,38 @@ +{ + "id": "test-request-id", + "seatbid": [ + { + "bid": [ + { + "id": "randomid", + "impid": "test-imp-id", + "price": 0.5, + "adm": "some-test-ad", + "adid": "12345678", + "cid": "987", + "crid": "12345678", + "w": 300, + "h": 250, + "ext": { + "prebid": { + "type": "banner" + }, + "origbidcpm": 0.5 + } + } + ], + "seat": "aax", + "group": 0 + } + ], + "cur": "USD", + "ext": { + "responsetimemillis": { + "aax": "{{ aax.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 d9f05a99a54..825bcc910e3 100644 --- a/src/test/resources/org/prebid/server/it/test-application.properties +++ b/src/test/resources/org/prebid/server/it/test-application.properties @@ -288,6 +288,8 @@ adapters.yieldone.enabled=true adapters.yieldone.endpoint=http://localhost:8090/yieldone-exchange adapters.zeroclickfraud.enabled=true adapters.zeroclickfraud.endpoint=http://{{Host}}/zeroclickfraud-exchange?sid={{SourceId}} +adapters.aax.enabled=true +adapters.aax.endpoint=http://localhost:8090/aax-exchange http-client.circuit-breaker.enabled=true http-client.circuit-breaker.idle-expire-hours=24 http-client.circuit-breaker.opening-threshold=1