diff --git a/src/main/java/org/prebid/server/bidder/smrtconnect/SmrtconnectBidder.java b/src/main/java/org/prebid/server/bidder/smrtconnect/SmrtconnectBidder.java new file mode 100644 index 00000000000..9e7e8f56369 --- /dev/null +++ b/src/main/java/org/prebid/server/bidder/smrtconnect/SmrtconnectBidder.java @@ -0,0 +1,110 @@ +package org.prebid.server.bidder.smrtconnect; + +import com.fasterxml.jackson.core.type.TypeReference; +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.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.ExtPrebid; +import org.prebid.server.proto.openrtb.ext.request.smrtconnect.ExtImpSmrtconnect; +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 SmrtconnectBidder implements Bidder { + + private static final String SUPPLY_ID_MACRO = "{{SupplyId}}"; + private static final TypeReference> SMRTCONNECT_EXT_TYPE_REFERENCE = + new TypeReference<>() { + }; + private final String endpointUrl; + private final JacksonMapper mapper; + + public SmrtconnectBidder(String endpointUrl, JacksonMapper mapper) { + this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl)); + this.mapper = Objects.requireNonNull(mapper); + } + + @Override + public final Result>> makeHttpRequests(BidRequest bidRequest) { + final Imp firstImp = bidRequest.getImp().get(0); + final ExtImpSmrtconnect extImpSmrtconnect; + + try { + extImpSmrtconnect = mapper.mapper().convertValue(firstImp.getExt(), SMRTCONNECT_EXT_TYPE_REFERENCE) + .getBidder(); + } catch (IllegalArgumentException e) { + return Result.withError(BidderError.badInput("Ext.bidder not provided")); + } + + return Result.withValue( + HttpRequest.builder() + .method(HttpMethod.POST) + .uri(resolveEndpoint(extImpSmrtconnect.getSupplyId())) + .headers(HttpUtil.headers()) + .body(mapper.encodeToBytes(bidRequest)) + .impIds(BidderUtil.impIds(bidRequest)) + .payload(bidRequest) + .build()); + } + + private String resolveEndpoint(String supplyId) { + return endpointUrl.replace(SUPPLY_ID_MACRO, HttpUtil.encodeUrl(supplyId)); + } + + @Override + public Result> makeBids(BidderCall 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("Bad Server Response")); + } catch (PreBidException 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(); + } + return bidsFromResponse(bidRequest, bidResponse); + } + + private static List 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, getBidType(bid.getMtype()), bidResponse.getCur())) + .toList(); + } + + private static BidType getBidType(Integer mType) { + return switch (mType) { + case 1 -> BidType.banner; + case 2 -> BidType.video; + case 3 -> BidType.audio; + case 4 -> BidType.xNative; + + default -> throw new PreBidException("Unsupported mType " + mType); + }; + } +} diff --git a/src/main/java/org/prebid/server/proto/openrtb/ext/request/smrtconnect/ExtImpSmrtconnect.java b/src/main/java/org/prebid/server/proto/openrtb/ext/request/smrtconnect/ExtImpSmrtconnect.java new file mode 100644 index 00000000000..7dda6c6fa7b --- /dev/null +++ b/src/main/java/org/prebid/server/proto/openrtb/ext/request/smrtconnect/ExtImpSmrtconnect.java @@ -0,0 +1,9 @@ +package org.prebid.server.proto.openrtb.ext.request.smrtconnect; + +import lombok.Value; + +@Value(staticConstructor = "of") +public class ExtImpSmrtconnect { + + String supplyId; +} diff --git a/src/main/java/org/prebid/server/spring/config/bidder/SmrtconnectConfiguration.java b/src/main/java/org/prebid/server/spring/config/bidder/SmrtconnectConfiguration.java new file mode 100644 index 00000000000..24eb4d44996 --- /dev/null +++ b/src/main/java/org/prebid/server/spring/config/bidder/SmrtconnectConfiguration.java @@ -0,0 +1,41 @@ +package org.prebid.server.spring.config.bidder; + +import org.prebid.server.bidder.BidderDeps; +import org.prebid.server.bidder.smrtconnect.SmrtconnectBidder; +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/smrtconnect.yaml", factory = YamlPropertySourceFactory.class) +public class SmrtconnectConfiguration { + + private static final String BIDDER_NAME = "smrtconnect"; + + @Bean("smrtconnectConfigurationProperties") + @ConfigurationProperties("adapters.smrtconnect") + BidderConfigurationProperties configurationProperties() { + return new BidderConfigurationProperties(); + } + + @Bean + BidderDeps smrtconnectBidderDeps(BidderConfigurationProperties smrtconnectConfigurationProperties, + @NotBlank @Value("${external-url}") String externalUrl, + JacksonMapper mapper) { + + return BidderDepsAssembler.forBidder(BIDDER_NAME) + .withConfig(smrtconnectConfigurationProperties) + .usersyncerCreator(UsersyncerCreator.create(externalUrl)) + .bidderCreator(config -> new SmrtconnectBidder(config.getEndpoint(), mapper)) + .assemble(); + } +} diff --git a/src/main/resources/bidder-config/smrtconnect.yaml b/src/main/resources/bidder-config/smrtconnect.yaml new file mode 100644 index 00000000000..c9ee3a640ef --- /dev/null +++ b/src/main/resources/bidder-config/smrtconnect.yaml @@ -0,0 +1,21 @@ +adapters: + smrtconnect: + endpoint: https://amp.smrtconnect.com/openrtb2/auction?supply_id={{SupplyId}} + # This bidder does not operate globally. Please consider setting "disabled: true" in European datacenters. + geoscope: + - "!EEA" + endpoint-compression: gzip + meta-info: + maintainer-email: prebid@smrtconnect.com + app-media-types: + - banner + - native + - video + - audio + site-media-types: + - banner + - native + - video + - audio + supported-vendors: + vendor-id: 0 diff --git a/src/main/resources/static/bidder-params/smrtconnect.json b/src/main/resources/static/bidder-params/smrtconnect.json new file mode 100644 index 00000000000..74229a46133 --- /dev/null +++ b/src/main/resources/static/bidder-params/smrtconnect.json @@ -0,0 +1,14 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Smrtconnect Params", + "description": "A schema which validates params accepted by the Smrtconnect", + "type": "object", + "properties": { + "supply_id": { + "type": "string", + "description": "Supply id", + "minLength": 1 + } + }, + "required": ["supply_id"] +} diff --git a/src/test/java/org/prebid/server/bidder/smrtconnect/SmrtconnectBidderTest.java b/src/test/java/org/prebid/server/bidder/smrtconnect/SmrtconnectBidderTest.java new file mode 100644 index 00000000000..35e8ed061d3 --- /dev/null +++ b/src/test/java/org/prebid/server/bidder/smrtconnect/SmrtconnectBidderTest.java @@ -0,0 +1,156 @@ +package org.prebid.server.bidder.smrtconnect; + +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.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.ExtPrebid; +import org.prebid.server.proto.openrtb.ext.request.smrtconnect.ExtImpSmrtconnect; + +import java.util.Collections; +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.prebid.server.proto.openrtb.ext.response.BidType.audio; +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 SmrtconnectBidderTest extends VertxTest { + + private static final String ENDPOINT_URL = "https://test-url.com/?param={{SupplyId}}"; + + private SmrtconnectBidder smrtconnectBidder; + + @Before + public void setUp() { + smrtconnectBidder = new SmrtconnectBidder(ENDPOINT_URL, jacksonMapper); + } + + @Test + public void creationShouldFailOnInvalidEndpointUrl() { + assertThatIllegalArgumentException().isThrownBy(() -> new SmrtconnectBidder("invalid_url", jacksonMapper)); + } + + @Test + public void makeHttpRequestsShouldReturnErrorsOfNotValidImps() { + // given + final BidRequest bidRequest = givenBidRequest( + impBuilder -> impBuilder + .ext(mapper.valueToTree(ExtPrebid.of(null, mapper.createArrayNode())))); + // when + final Result>> result = smrtconnectBidder.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()) + .containsExactly(BidderError.badInput("Ext.bidder not provided")); + } + + @Test + public void makeHttpRequestsShouldCreateCorrectURL() { + // given + final BidRequest bidRequest = givenBidRequest(identity()); + + // when + final Result>> result = smrtconnectBidder.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getUri) + .containsExactly("https://test-url.com/?param=1"); + } + + @Test + public void makeBidsShouldReturnErrorIfResponseBodyCouldNotBeParsed() { + // given + final BidderCall httpCall = givenHttpCall(null, "invalid"); + + // when + final Result> result = smrtconnectBidder.makeBids(httpCall, null); + + // then + assertThat(result.getValue()).isEmpty(); + assertThat(result.getErrors()) + .containsExactly(BidderError.badServerResponse("Bad Server Response")); + } + + @Test + public void makeBidsShouldReturnAllFourBidTypesSuccessfully() throws JsonProcessingException { + // given + final Bid bannerBid = Bid.builder().impid("1").mtype(1).build(); + final Bid videoBid = Bid.builder().impid("2").mtype(2).build(); + final Bid audioBid = Bid.builder().impid("3").mtype(3).build(); + final Bid nativeBid = Bid.builder().impid("4").mtype(4).build(); + + final BidderCall httpCall = givenHttpCall( + givenBidRequest( + impBuilder -> impBuilder + .ext(mapper.valueToTree(ExtPrebid.of(null, mapper.createArrayNode())))), + givenBidResponse(bannerBid, videoBid, audioBid, nativeBid)); + + // when + final Result> result = smrtconnectBidder.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).containsExactly( + BidderBid.of(bannerBid, banner, "USD"), + BidderBid.of(videoBid, video, "USD"), + BidderBid.of(audioBid, audio, "USD"), + BidderBid.of(nativeBid, xNative, "USD")); + } + + private static BidRequest givenBidRequest(UnaryOperator impCustomizer) { + return givenBidRequest(UnaryOperator.identity(), impCustomizer); + } + + private static BidRequest givenBidRequest( + UnaryOperator bidRequestCustomizer, + UnaryOperator impCustomizer) { + + return bidRequestCustomizer.apply(BidRequest.builder() + .imp(singletonList(givenImp(impCustomizer)))) + .build(); + } + + private static String givenBidResponse(Bid... bids) throws JsonProcessingException { + return mapper.writeValueAsString( + BidResponse.builder() + .cur("USD") + .seatbid(bids.length == 0 + ? Collections.emptyList() + : List.of(SeatBid.builder().bid(List.of(bids)).build())) + .build()); + } + + private static Imp givenImp(UnaryOperator impCustomizer) { + return impCustomizer.apply(Imp.builder() + .id("123") + .ext(mapper.valueToTree(ExtPrebid.of(null, + ExtImpSmrtconnect.of("1"))))) + .build(); + } + + private static BidderCall givenHttpCall(BidRequest bidRequest, String body) { + return BidderCall.succeededHttp( + HttpRequest.builder().payload(bidRequest).build(), + HttpResponse.of(200, null, body), + null); + } +} diff --git a/src/test/java/org/prebid/server/it/SmrtconnectTest.java b/src/test/java/org/prebid/server/it/SmrtconnectTest.java new file mode 100644 index 00000000000..92e8c238e34 --- /dev/null +++ b/src/test/java/org/prebid/server/it/SmrtconnectTest.java @@ -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.equalTo; +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 SmrtconnectTest extends IntegrationTest { + + @Test + public void openrtb2AuctionShouldRespondWithBidsFromTheSmrtconnect() throws IOException, JSONException { + // given + WIRE_MOCK_RULE.stubFor(post(urlPathEqualTo("/smrtconnect-exchange")) + .withQueryParam("supply_id", equalTo("1")) + .withRequestBody(equalToJson(jsonFrom("openrtb2/smrtconnect/test-smrtconnect-bid-request.json"))) + .willReturn(aResponse().withBody(jsonFrom("openrtb2/smrtconnect/test-smrtconnect-bid-response.json")))); + + // when + final Response response = responseFor("openrtb2/smrtconnect/test-auction-smrtconnect-request.json", + Endpoint.openrtb2_auction); + + // then + assertJsonEquals("openrtb2/smrtconnect/test-auction-smrtconnect-response.json", response, + singletonList("smrtconnect")); + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/smrtconnect/test-auction-smrtconnect-request.json b/src/test/resources/org/prebid/server/it/openrtb2/smrtconnect/test-auction-smrtconnect-request.json new file mode 100644 index 00000000000..77e4bcbe174 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/smrtconnect/test-auction-smrtconnect-request.json @@ -0,0 +1,23 @@ +{ + "id": "request_id", + "imp": [ + { + "id": "imp_id", + "banner": { + "w": 300, + "h": 250 + }, + "ext": { + "smrtconnect": { + "supply_id": "1" + } + } + } + ], + "tmax": 5000, + "regs": { + "ext": { + "gdpr": 0 + } + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/smrtconnect/test-auction-smrtconnect-response.json b/src/test/resources/org/prebid/server/it/openrtb2/smrtconnect/test-auction-smrtconnect-response.json new file mode 100644 index 00000000000..90d63b63f15 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/smrtconnect/test-auction-smrtconnect-response.json @@ -0,0 +1,39 @@ +{ + "id": "request_id", + "seatbid": [ + { + "bid": [ + { + "id": "bid_id", + "impid": "imp_id", + "price": 0.5, + "adm": "some-test-ad", + "adid": "12345678", + "cid": "987", + "crid": "12345678", + "w": 300, + "h": 250, + "mtype": 1, + "ext": { + "prebid": { + "type": "banner" + }, + "origbidcpm": 0.5 + } + } + ], + "seat": "smrtconnect", + "group": 0 + } + ], + "cur": "USD", + "ext": { + "responsetimemillis": { + "smrtconnect": "{{ smrtconnect.response_time_ms }}" + }, + "prebid": { + "auctiontimestamp": 0 + }, + "tmaxrequest": 5000 + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/smrtconnect/test-smrtconnect-bid-request.json b/src/test/resources/org/prebid/server/it/openrtb2/smrtconnect/test-smrtconnect-bid-request.json new file mode 100644 index 00000000000..9bec1f0c932 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/smrtconnect/test-smrtconnect-bid-request.json @@ -0,0 +1,56 @@ +{ + "id": "request_id", + "imp": [ + { + "id": "imp_id", + "secure": 1, + "banner": { + "w": 300, + "h": 250 + }, + "ext": { + "tid": "${json-unit.any-string}", + "bidder": { + "supply_id": "1" + } + } + } + ], + "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": { + "ext": { + "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/smrtconnect/test-smrtconnect-bid-response.json b/src/test/resources/org/prebid/server/it/openrtb2/smrtconnect/test-smrtconnect-bid-response.json new file mode 100644 index 00000000000..b68c3c92bba --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/smrtconnect/test-smrtconnect-bid-response.json @@ -0,0 +1,23 @@ +{ + "id": "request_id", + "seatbid": [ + { + "seat": "smrtconnect", + "bid": [ + { + "id": "bid_id", + "impid": "imp_id", + "price": 0.500000, + "adid": "12345678", + "adm": "some-test-ad", + "cid": "987", + "crid": "12345678", + "h": 250, + "w": 300, + "mtype": 1 + } + ] + } + ], + "bidid": "bid_id" +} 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 725834c80e8..93200a51b51 100644 --- a/src/test/resources/org/prebid/server/it/test-application.properties +++ b/src/test/resources/org/prebid/server/it/test-application.properties @@ -351,6 +351,8 @@ adapters.smartyads.enabled=true adapters.smartyads.endpoint=http://localhost:8090/smartyads-exchange adapters.smilewanted.enabled=true adapters.smilewanted.endpoint=http://localhost:8090/smilewanted-exchange +adapters.smrtconnect.enabled=true +adapters.smrtconnect.endpoint=http://localhost:8090/smrtconnect-exchange?supply_id={{SupplyId}} adapters.sonobi.enabled=true adapters.sonobi.endpoint=http://localhost:8090/sonobi-exchange adapters.sovrn.enabled=true