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 Bidder: Adf #1288

Merged
merged 6 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
46 changes: 46 additions & 0 deletions src/main/java/org/prebid/server/bidder/adf/AdfBidder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.prebid.server.bidder.adf;

import com.iab.openrtb.request.Imp;
import com.iab.openrtb.response.Bid;
import org.prebid.server.bidder.Bidder;
import org.prebid.server.bidder.OpenrtbBidder;
import org.prebid.server.exception.PreBidException;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.proto.openrtb.ext.request.adf.ExtImpAdf;
import org.prebid.server.proto.openrtb.ext.response.BidType;

import java.util.List;

/**
* Adf {@link Bidder} implementation.
*/
public class AdfBidder extends OpenrtbBidder<ExtImpAdf> {
SerhiiNahornyi marked this conversation as resolved.
Show resolved Hide resolved

public AdfBidder(String endpointUrl, JacksonMapper mapper) {
super(endpointUrl, RequestCreationStrategy.SINGLE_REQUEST, ExtImpAdf.class, mapper);
}

@Override
protected Imp modifyImp(Imp imp, ExtImpAdf impExt) {
return imp.toBuilder()
.tagid(impExt.getMid())
.build();
}

@Override
protected BidType getBidType(Bid bid, List<Imp> imps) {
final String impId = bid.getImpid();
for (Imp imp : imps) {
if (imp.getId().equals(impId)) {
if (imp.getBanner() != null) {
return BidType.banner;
} else if (imp.getVideo() != null) {
return BidType.video;
} else if (imp.getXNative() != null) {
return BidType.xNative;
}
}
}
throw new PreBidException(String.format("Failed to find impression %s", impId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.prebid.server.proto.openrtb.ext.request.adf;

import lombok.AllArgsConstructor;
import lombok.Value;

/**
* Defines the contract for bidrequest.imp[i].ext.adf
*/
@AllArgsConstructor(staticName = "of")
@Value
public class ExtImpAdf {

String mid;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.prebid.server.spring.config.bidder;

import org.prebid.server.bidder.BidderDeps;
import org.prebid.server.bidder.adf.AdfBidder;
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/adf.yaml", factory = YamlPropertySourceFactory.class)
public class AdfConfiguration {

private static final String BIDDER_NAME = "adf";

@Value("${external-url}")
@NotBlank
private String externalUrl;

@Autowired
private JacksonMapper mapper;

@Autowired
@Qualifier("adfConfigurationProperties")
private BidderConfigurationProperties configProperties;

@Bean("adfConfigurationProperties")
@ConfigurationProperties("adapters.adf")
BidderConfigurationProperties configurationProperties() {
return new BidderConfigurationProperties();
}

@Bean
BidderDeps adfBidderDeps() {

return BidderDepsAssembler.forBidder(BIDDER_NAME)
.withConfig(configProperties)
.usersyncerCreator(UsersyncerCreator.create(externalUrl))
.bidderCreator(config -> new AdfBidder(config.getEndpoint(), mapper))
.assemble();
}
}
27 changes: 27 additions & 0 deletions src/main/resources/bidder-config/adf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
adapters:
adf:
enabled: false
endpoint: https://adx.adform.net/adx/openrtb
pbs-enforces-gdpr: true
pbs-enforces-ccpa: true
modifying-vast-xml-allowed: true
deprecated-names:
aliases: {}
meta-info:
maintainer-email: scope.sspp@adform.com
app-media-types:
- banner
- native
- video
site-media-types:
- banner
- native
- video
supported-vendors:
vendor-id: 50
usersync:
url: https://cm.adform.net/cookie?redirect_url=
redirect-url: /setuid?bidder=adf&gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&us_privacy={{us_privacy}}&uid=$UID
cookie-family-name: adf
type: redirect
support-cors: false
19 changes: 19 additions & 0 deletions src/main/resources/static/bidder-params/adf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Adf Adapter Params",
"description": "A schema which validates params accepted by the adf adapter",
"type": "object",
"properties": {
"mid": {
"type": [
"integer",
"string"
],
"pattern": "^\\d+$",
"description": "An ID which identifies the placement selling the impression"
}
},
"required": [
"mid"
]
}
194 changes: 194 additions & 0 deletions src/test/java/org/prebid/server/bidder/adf/AdfBidderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package org.prebid.server.bidder.adf;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.iab.openrtb.request.Banner;
import com.iab.openrtb.request.BidRequest;
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 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.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.adf.ExtImpAdf;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

import static java.util.Collections.singletonList;
import static java.util.function.Function.identity;
import static org.assertj.core.api.Assertions.assertThat;
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 AdfBidderTest extends VertxTest {

private static final String ENDPOINT_URL = "https://test.endpoint.com/";

private AdfBidder adfBidder;

@Before
public void setup() {
adfBidder = new AdfBidder(ENDPOINT_URL, jacksonMapper);
}

@Test
public void makeHttpRequestsShouldReturnExpectedBidRequest() {
// given
final BidRequest bidRequest = givenBidRequest(identity());

// when
final Result<List<HttpRequest<BidRequest>>> result = adfBidder.makeHttpRequests(bidRequest);

// then
final BidRequest expectedRequest = bidRequest.toBuilder()
.imp(singletonList(bidRequest.getImp().get(0).toBuilder().tagid("12345").build()))
.build();
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue()).hasSize(1)
.extracting(httpRequest -> mapper.readValue(httpRequest.getBody(), BidRequest.class))
.containsExactly(expectedRequest);
}

@Test
public void makeHttpRequestsShouldMakeOneRequestForAllImps() {
// given
final BidRequest bidRequest = givenBidRequest(
identity(),
requestBuilder -> requestBuilder.imp(Arrays.asList(
givenImp(identity()),
Imp.builder()
.ext(mapper.valueToTree(ExtPrebid.of(null, ExtImpAdf.of("1"))))
.build())));

// when
final Result<List<HttpRequest<BidRequest>>> result = adfBidder.makeHttpRequests(bidRequest);

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue()).hasSize(1)
.extracting(httpRequest -> mapper.readValue(httpRequest.getBody(), BidRequest.class))
.flatExtracting(BidRequest::getImp).hasSize(2)
.extracting(Imp::getTagid)
.containsExactly("12345", "1");
}

@Test
public void makeBidsShouldReturnBannerBid() throws JsonProcessingException {
// given
final HttpCall<BidRequest> httpCall = givenHttpCall(
BidRequest.builder().imp(singletonList(Imp.builder().id("123").banner(Banner.builder()
.w(1)
.h(1)
.build()).build())).build(),
mapper.writeValueAsString(givenBidResponse(bidBuilder -> bidBuilder.impid("123"))));

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

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.containsExactly(BidderBid.of(Bid.builder().impid("123").build(), banner, "USD"));
}

@Test
public void makeBidsShouldReturnVideoBid() throws JsonProcessingException {
// given
final HttpCall<BidRequest> httpCall = givenHttpCall(
BidRequest.builder().imp(singletonList(Imp.builder().id("123").video(Video.builder()
.w(1)
.h(1)
.build()).build())).build(),
mapper.writeValueAsString(givenBidResponse(bidBuilder -> bidBuilder.impid("123"))));

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

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.containsExactly(BidderBid.of(Bid.builder().impid("123").build(), video, "USD"));
}

@Test
public void makeBidsShouldReturnNativeBid() throws JsonProcessingException {
// given
final HttpCall<BidRequest> httpCall = givenHttpCall(
BidRequest.builder().imp(singletonList(Imp.builder().id("123")
.xNative(new Native())
.build())).build(),
mapper.writeValueAsString(givenBidResponse(bidBuilder -> bidBuilder.impid("123"))));

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

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.containsExactly(BidderBid.of(Bid.builder().impid("123").build(), xNative, "USD"));
}

@Test
public void makeBidsShouldThrowErrorNoImp() throws JsonProcessingException {
// given
final HttpCall<BidRequest> httpCall = givenHttpCall(
BidRequest.builder().imp(singletonList(Imp.builder().id("123")
.build())).build(),
mapper.writeValueAsString(givenBidResponse(bidBuilder -> bidBuilder.impid("123"))));

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

// then
assertThat(result.getErrors()).hasSize(1);
}

private static BidRequest givenBidRequest(
Function<Imp.ImpBuilder, Imp.ImpBuilder> impCustomizer,
Function<BidRequest.BidRequestBuilder, BidRequest.BidRequestBuilder> requestCustomizer) {
return requestCustomizer.apply(BidRequest.builder()
.imp(singletonList(givenImp(impCustomizer))))
.build();
}

private static BidRequest givenBidRequest(
Function<Imp.ImpBuilder, Imp.ImpBuilder> impCustomizer) {
return givenBidRequest(impCustomizer, identity());
}

private static Imp givenImp(Function<Imp.ImpBuilder, Imp.ImpBuilder> impCustomizer) {
return impCustomizer.apply(Imp.builder()
.id("123"))
.banner(Banner.builder().build())
.video(Video.builder().build())
.ext(mapper.valueToTree(ExtPrebid.of(null, ExtImpAdf.of("12345"))))
.build();
}

private static HttpCall<BidRequest> givenHttpCall(BidRequest bidRequest, String body) {
return HttpCall.success(HttpRequest.<BidRequest>builder().payload(bidRequest).build(),
HttpResponse.of(200, null, body), null);
}

private static BidResponse givenBidResponse(
Function<Bid.BidBuilder, Bid.BidBuilder> bidCustomizer) {
return BidResponse.builder()
.cur("USD")
.seatbid(singletonList(SeatBid.builder()
.bid(singletonList(bidCustomizer.apply(Bid.builder()).build()))
.build()))
.build();
}

}
Loading