Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Adapter: AAX #1819

Merged
merged 3 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions src/main/java/org/prebid/server/bidder/aax/AaxBidder.java
Original file line number Diff line number Diff line change
@@ -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<BidRequest> {

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<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest bidRequest) {
return Result.withValue(HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.headers(HttpUtil.headers())
.uri(endpointUrl)
.body(mapper.encodeToBytes(bidRequest))
.payload(bidRequest)
.build());
}

@Override
public final Result<List<BidderBid>> makeBids(HttpCall<BidRequest> 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<BidderBid> 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<Imp> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
21 changes: 21 additions & 0 deletions src/main/resources/bidder-config/aax.yaml
Original file line number Diff line number Diff line change
@@ -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
And1sS marked this conversation as resolved.
Show resolved Hide resolved
- 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=<vsid>
cookie-family-name: aax
type: redirect
support-cors: false
20 changes: 20 additions & 0 deletions src/main/resources/static/bidder-params/aax.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
145 changes: 145 additions & 0 deletions src/test/java/org/prebid/server/bidder/aax/AaxBidderTest.java
Original file line number Diff line number Diff line change
@@ -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<List<HttpRequest<BidRequest>>> 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<BidRequest> httpCall = sampleHttpCall(givenBidRequest(), "invalid response");

// when
final Result<List<BidderBid>> 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<BidRequest> httpCall;
httpCall = sampleHttpCall(givenBidRequest(), mapper.writeValueAsString(null));

// when
final Result<List<BidderBid>> result = aaxBidder.makeBids(httpCall, null);

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue()).isEmpty();
}

@Test
public void makeBidsShouldReturnEmptyListIfBidResponseSeatBidIsNull() throws JsonProcessingException {
// given
final HttpCall<BidRequest> httpCall;
httpCall = sampleHttpCall(null, mapper.writeValueAsString(BidResponse.builder().build()));

// when
final Result<List<BidderBid>> result = aaxBidder.makeBids(httpCall, null);

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue()).isEmpty();
}

@Test
public void makeBidsShouldReturnBannerBidIfBannerIsPresent() throws JsonProcessingException {
// given
final HttpCall<BidRequest> httpCall = sampleHttpCall(
givenBidRequest(),
mapper.writeValueAsString(sampleBidResponse(bidBuilder -> bidBuilder.impid("123"))));

// when
final Result<List<BidderBid>> 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<Bid.BidBuilder,
Bid.BidBuilder> bidCustomizer) {
return BidResponse.builder()
.cur("USD")
.seatbid(singletonList(SeatBid.builder()
.bid(singletonList(bidCustomizer.apply(Bid.builder()).build()))
.build()))
.build();
}

private static HttpCall<BidRequest> sampleHttpCall(BidRequest bidRequest, String body) {
return HttpCall.success(
HttpRequest.<BidRequest>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();
}
}
36 changes: 36 additions & 0 deletions src/test/java/org/prebid/server/it/AaxTest.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Loading