Skip to content

Commit

Permalink
Oms: Add adapter (#2888)
Browse files Browse the repository at this point in the history
  • Loading branch information
SerhiiNahornyi authored Jan 10, 2024
1 parent 8095eda commit 5f4ddb8
Show file tree
Hide file tree
Showing 11 changed files with 471 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/main/java/org/prebid/server/bidder/oms/OmsBidder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.prebid.server.bidder.oms;

import com.iab.openrtb.request.BidRequest;
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.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.json.DecodeException;
import org.prebid.server.json.JacksonMapper;
import org.prebid.server.proto.openrtb.ext.response.BidType;
import org.prebid.server.util.BidderUtil;
import org.prebid.server.util.HttpUtil;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

public class OmsBidder implements Bidder<BidRequest> {

private final String endpointUrl;
private final JacksonMapper mapper;

public OmsBidder(String endpointUrl, JacksonMapper mapper) {
this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl));
this.mapper = Objects.requireNonNull(mapper);
}

@Override
public final Result<List<HttpRequest<BidRequest>>> makeHttpRequests(BidRequest bidRequest) {
return Result.withValue(
HttpRequest.<BidRequest>builder()
.method(HttpMethod.POST)
.uri(endpointUrl)
.headers(HttpUtil.headers())
.body(mapper.encodeToBytes(bidRequest))
.impIds(BidderUtil.impIds(bidRequest))
.payload(bidRequest)
.build());
}

@Override
public final Result<List<BidderBid>> makeBids(BidderCall<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();
}
return bidsFromResponse(bidRequest, bidResponse);
}

private static List<BidderBid> bidsFromResponse(BidRequest bidRequest, BidResponse bidResponse) {
return bidResponse.getSeatbid().stream()
.filter(Objects::nonNull)
.map(SeatBid::getBid)
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.map(bid -> BidderBid.of(bid, BidType.banner, bidResponse.getCur()))
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.prebid.server.spring.config.bidder;

import org.prebid.server.bidder.BidderDeps;
import org.prebid.server.bidder.oms.OmsBidder;
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 javax.validation.constraints.NotBlank;

@Configuration
@PropertySource(value = "classpath:/bidder-config/oms.yaml", factory = YamlPropertySourceFactory.class)
public class OmsBidderConfiguration {

private static final String BIDDER_NAME = "oms";

@Bean("omsConfigurationProperties")
@ConfigurationProperties("adapters.oms")
BidderConfigurationProperties configurationProperties() {
return new BidderConfigurationProperties();
}

@Bean
BidderDeps omsBidderDeps(BidderConfigurationProperties omsConfigurationProperties,
@NotBlank @Value("${external-url}") String externalUrl,
JacksonMapper mapper) {

return BidderDepsAssembler.forBidder(BIDDER_NAME)
.withConfig(omsConfigurationProperties)
.usersyncerCreator(UsersyncerCreator.create(externalUrl))
.bidderCreator(config -> new OmsBidder(config.getEndpoint(), mapper))
.assemble();
}
}

11 changes: 11 additions & 0 deletions src/main/resources/bidder-config/oms.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
adapters:
oms:
endpoint: http://rt.marphezis.com/pbs
meta-info:
maintainer-email: prebid@onlinemediasolutions.com
app-media-types:
- banner
site-media-types:
- banner
supported-vendors:
vendor-id: 0
16 changes: 16 additions & 0 deletions src/main/resources/static/bidder-params/oms.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Online Media Solutions Adapter Params",
"description": "A schema which validates params accepted by the OMS adapter",
"type": "object",
"properties": {
"pid": {
"type": "string",
"description": "An id used to identify OMS publisher.",
"minLength": 5
}
},
"required": [
"pid"
]
}
161 changes: 161 additions & 0 deletions src/test/java/org/prebid/server/bidder/oms/OmsBidderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package org.prebid.server.bidder.oms;

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.response.Bid;
import com.iab.openrtb.response.BidResponse;
import com.iab.openrtb.response.SeatBid;
import org.junit.Test;
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 java.util.List;
import java.util.function.Function;
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.prebid.server.proto.openrtb.ext.response.BidType.banner;

public class OmsBidderTest extends VertxTest {

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

private final OmsBidder target = new OmsBidder(ENDPOINT_URL, jacksonMapper);

@Test
public void creationShouldFailOnInvalidEndpointUrl() {
assertThatIllegalArgumentException().isThrownBy(() -> new OmsBidder("invalid_url", jacksonMapper));
}

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

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

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue()).hasSize(1)
.extracting(HttpRequest::getUri)
.containsExactly("https://randomurl.com");
}

@Test
public void makeBidsShouldReturnErrorIfResponseBodyCouldNotBeParsed() {
// given
final BidderCall<BidRequest> httpCall = givenHttpCall(null, "invalid");

// when
final Result<List<BidderBid>> 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");
});
assertThat(result.getValue()).isEmpty();
}

@Test
public void makeBidsShouldReturnEmptyListIfBidResponseIsNull() throws JsonProcessingException {
// given
final BidderCall<BidRequest> httpCall = givenHttpCall(null, mapper.writeValueAsString(null));

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

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

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

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

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

@Test
public void makeBidsShouldReturnBannerBid() throws JsonProcessingException {
// given
final BidderCall<BidRequest> httpCall = givenHttpCall(
givenBidRequest(impBuilder -> impBuilder.banner(Banner.builder().build())),
mapper.writeValueAsString(givenBidResponse(impBuilder -> impBuilder.impid("123"))));

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

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.containsExactly(BidderBid.of(givenBid(), banner, null));
}

@Test
public void makeBidsShouldReturnBannerBidIfBannerAndVideoAndAudioAndNativeIsAbsentInRequestImp()
throws JsonProcessingException {
// given
final BidderCall<BidRequest> httpCall = givenHttpCall(
givenBidRequest(identity()),
mapper.writeValueAsString(givenBidResponse(impBuilder -> impBuilder.impid("123"))));

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

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.containsExactly(BidderBid.of(givenBid(), banner, null));
}

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

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

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

private static Bid givenBid() {
return Bid.builder().impid("123").build();
}

private static BidderCall<BidRequest> givenHttpCall(BidRequest bidRequest, String body) {
return BidderCall.succeededHttp(
HttpRequest.<BidRequest>builder().payload(bidRequest).build(),
HttpResponse.of(200, null, body),
null);
}
}
38 changes: 38 additions & 0 deletions src/test/java/org/prebid/server/it/OmsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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 OmsTest extends IntegrationTest {

@Test
public void openrtb2AuctionShouldRespondWithBidsFromTheOmsBidder() throws IOException, JSONException {
// given
WIRE_MOCK_RULE.stubFor(post(urlPathEqualTo("/oms-exchange"))
.withRequestBody(equalToJson(
jsonFrom("openrtb2/oms/test-oms-bid-request.json")))
.willReturn(aResponse().withBody(
jsonFrom("openrtb2/oms/test-oms-bid-response.json"))));

// when
final Response response = responseFor("openrtb2/oms/test-auction-oms-request.json",
Endpoint.openrtb2_auction);

// then
assertJsonEquals("openrtb2/oms/test-auction-oms-response.json", response,
singletonList("oms"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"id": "request_id",
"imp": [
{
"id": "imp_id",
"banner": {
"w": 300,
"h": 250
},
"ext": {
"oms": {
"pid": "exampleProperty"
}
}
}
],
"tmax": 5000,
"regs": {
"ext": {
"gdpr": 0
}
}
}
Loading

0 comments on commit 5f4ddb8

Please sign in to comment.