diff --git a/src/main/java/org/prebid/server/bidder/zentotem/ZentotemBidder.java b/src/main/java/org/prebid/server/bidder/zentotem/ZentotemBidder.java new file mode 100644 index 00000000000..c665b06650c --- /dev/null +++ b/src/main/java/org/prebid/server/bidder/zentotem/ZentotemBidder.java @@ -0,0 +1,107 @@ +package org.prebid.server.bidder.zentotem; + +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.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.response.BidType; +import org.prebid.server.util.BidderUtil; +import org.prebid.server.util.HttpUtil; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; + +public class ZentotemBidder implements Bidder { + + private final String endpointUrl; + private final JacksonMapper mapper; + + public ZentotemBidder(String endpointUrl, JacksonMapper mapper) { + this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl)); + this.mapper = Objects.requireNonNull(mapper); + } + + @Override + public Result>> makeHttpRequests(BidRequest request) { + final List> httpRequests = new ArrayList<>(); + final List errors = new ArrayList<>(); + + for (Imp imp : request.getImp()) { + try { + final BidRequest outgoingRequest = request.toBuilder() + .imp(Collections.singletonList(imp)) + .build(); + httpRequests.add(BidderUtil.defaultRequest(outgoingRequest, endpointUrl, mapper)); + } catch (PreBidException e) { + errors.add(BidderError.badInput(e.getMessage())); + } + } + + return Result.of(httpRequests, errors); + } + + @Override + public Result> makeBids(BidderCall httpCall, BidRequest bidRequest) { + try { + final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class); + final List errors = new ArrayList<>(); + return Result.of(extractBids(bidResponse, errors), errors); + } catch (DecodeException | PreBidException e) { + return Result.withError(BidderError.badServerResponse(e.getMessage())); + } + } + + private static List extractBids(BidResponse bidResponse, List errors) { + if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) { + return Collections.emptyList(); + } + return bidsFromResponse(bidResponse, errors); + } + + private static List bidsFromResponse(BidResponse bidResponse, List errors) { + return bidResponse.getSeatbid().stream() + .filter(Objects::nonNull) + .map(SeatBid::getBid) + .filter(Objects::nonNull) + .flatMap(Collection::stream) + .filter(Objects::nonNull) + .map(bid -> makeBidderBid(bid, bidResponse.getCur(), errors)) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + } + + private static BidderBid makeBidderBid(Bid bid, String currency, List errors) { + final BidType bidType = getBidType(bid, errors); + return bidType != null + ? BidderBid.of(bid, bidType, currency) + : null; + } + + private static BidType getBidType(Bid bid, List errors) { + return switch (bid.getMtype()) { + case 1 -> BidType.banner; + case 2 -> BidType.video; + case 4 -> BidType.xNative; + case null, default -> { + errors.add(BidderError.badServerResponse( + "could not define media type for impression: " + bid.getImpid())); + yield null; + } + }; + } +} diff --git a/src/main/java/org/prebid/server/spring/config/bidder/ZentotemConfiguration.java b/src/main/java/org/prebid/server/spring/config/bidder/ZentotemConfiguration.java new file mode 100644 index 00000000000..1578640e0ad --- /dev/null +++ b/src/main/java/org/prebid/server/spring/config/bidder/ZentotemConfiguration.java @@ -0,0 +1,41 @@ +package org.prebid.server.spring.config.bidder; + +import org.prebid.server.bidder.BidderDeps; +import org.prebid.server.bidder.zentotem.ZentotemBidder; +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 jakarta.validation.constraints.NotBlank; + +@Configuration +@PropertySource(value = "classpath:/bidder-config/zentotem.yaml", factory = YamlPropertySourceFactory.class) +public class ZentotemConfiguration { + + private static final String BIDDER_NAME = "zentotem"; + + @Bean("zentotemConfigurationProperties") + @ConfigurationProperties("adapters.zentotem") + BidderConfigurationProperties configurationProperties() { + return new BidderConfigurationProperties(); + } + + @Bean + BidderDeps zentotemBidderDeps(BidderConfigurationProperties zentotemConfigurationProperties, + @NotBlank @Value("${external-url}") String externalUrl, + JacksonMapper mapper) { + + return BidderDepsAssembler.forBidder(BIDDER_NAME) + .withConfig(zentotemConfigurationProperties) + .usersyncerCreator(UsersyncerCreator.create(externalUrl)) + .bidderCreator(config -> new ZentotemBidder(config.getEndpoint(), mapper)) + .assemble(); + } +} diff --git a/src/main/resources/bidder-config/zentotem.yaml b/src/main/resources/bidder-config/zentotem.yaml new file mode 100644 index 00000000000..0d6b0fe8121 --- /dev/null +++ b/src/main/resources/bidder-config/zentotem.yaml @@ -0,0 +1,17 @@ +adapters: + zentotem: + endpoint: https://rtb.zentotem.net/bid?sspuid=cqlnvfk00bhs0b6rci6g + endpoint-compression: gzip + modifying-vast-xml-allowed: true + meta-info: + maintainer-email: support@zentotem.net + app-media-types: + - banner + - video + - native + site-media-types: + - banner + - video + - native + supported-vendors: + vendor-id: 0 diff --git a/src/main/resources/static/bidder-params/zentotem.json b/src/main/resources/static/bidder-params/zentotem.json new file mode 100644 index 00000000000..de59fc47b70 --- /dev/null +++ b/src/main/resources/static/bidder-params/zentotem.json @@ -0,0 +1,7 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Zentotem Adapter Params", + "description": "A schema which validates params accepted by the Zentotem adapter", + "type": "object", + "properties": {} +} diff --git a/src/test/java/org/prebid/server/bidder/zentotem/ZentotemBidderTest.java b/src/test/java/org/prebid/server/bidder/zentotem/ZentotemBidderTest.java new file mode 100644 index 00000000000..b17b44699c1 --- /dev/null +++ b/src/test/java/org/prebid/server/bidder/zentotem/ZentotemBidderTest.java @@ -0,0 +1,197 @@ +package org.prebid.server.bidder.zentotem; + +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.jupiter.api.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.response.BidType; + +import java.math.BigDecimal; +import java.util.Arrays; +import java.util.List; +import java.util.Set; +import java.util.function.UnaryOperator; + +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.bidder.model.BidderError.badServerResponse; + +public class ZentotemBidderTest extends VertxTest { + + private static final String ENDPOINT_URL = "https://test.endpoint.com"; + + private final ZentotemBidder target = new ZentotemBidder(ENDPOINT_URL, jacksonMapper); + + @Test + public void creationShouldFailOnInvalidEndpointUrl() { + assertThatIllegalArgumentException().isThrownBy(() -> new ZentotemBidder("invalid_url", jacksonMapper)); + } + + @Test + public void makeHttpRequestsShouldCreateSeparateRequestForEachImp() { + // given + final BidRequest bidRequest = givenBidRequest( + imp -> imp.id("imp1"), + imp -> imp.id("imp2")); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).hasSize(2) + .extracting(HttpRequest::getImpIds) + .containsExactly(Set.of("imp1"), Set.of("imp2")); + } + + @Test + public void makeHttpRequestsShouldSetCorrectUriAndBody() { + // given + final BidRequest bidRequest = givenBidRequest(imp -> imp.id("imp1")); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + final BidRequest expectedRequest = bidRequest.toBuilder() + .imp(singletonList(bidRequest.getImp().getFirst())) + .build(); + + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).hasSize(1).first() + .satisfies(httpRequest -> { + assertThat(httpRequest.getUri()).isEqualTo(ENDPOINT_URL); + assertThat(httpRequest.getPayload()).isEqualTo(expectedRequest); + assertThat(httpRequest.getBody()).isEqualTo(jacksonMapper.encodeToBytes(expectedRequest)); + }); + } + + @Test + public void makeBidsShouldReturnErrorWhenResponseBodyCouldNotBeParsed() { + // given + final BidderCall httpCall = givenHttpCall("invalid_json"); + + // when + final Result> 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 'invalid_json'"); + }); + assertThat(result.getValue()).isEmpty(); + } + + @Test + public void makeBidsShouldReturnEmptyListWhenBidResponseOrSeatBidAreNull() throws JsonProcessingException { + // given + final BidResponse bidResponseWithNullSeatBid = BidResponse.builder().seatbid(null).build(); + final BidderCall httpCallWithNullSeatBid = + givenHttpCall(mapper.writeValueAsString(bidResponseWithNullSeatBid)); + + // when + final Result> nullSeatBidResult = target.makeBids(httpCallWithNullSeatBid, null); + + // then + assertThat(nullSeatBidResult.getErrors()).isEmpty(); + assertThat(nullSeatBidResult.getValue()).isEmpty(); + } + + @Test + public void makeBidsShouldReturnBannerBid() throws JsonProcessingException { + // given + final Bid bannerBid = givenBid(1); + final BidderCall httpCall = givenHttpCall(givenBidResponse(bannerBid)); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).containsOnly(BidderBid.of(bannerBid, BidType.banner, "USD")); + } + + @Test + public void makeBidsShouldReturnVideoBid() throws JsonProcessingException { + // given + final Bid videoBid = givenBid(2); + final BidderCall httpCall = givenHttpCall(givenBidResponse(videoBid)); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).containsOnly(BidderBid.of(videoBid, BidType.video, "USD")); + } + + @Test + public void makeBidsShouldReturnNativeBid() throws JsonProcessingException { + // given + final Bid nativeBid = givenBid(4); + final BidderCall httpCall = givenHttpCall(givenBidResponse(nativeBid)); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).containsOnly(BidderBid.of(nativeBid, BidType.xNative, "USD")); + } + + @Test + public void makeBidsShouldReturnErrorIfMtypeIsUnsupported() throws JsonProcessingException { + // given + final Bid bidWithUnsupportedMtype = givenBid(3); + final BidderCall httpCall = givenHttpCall(givenBidResponse(bidWithUnsupportedMtype)); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getValue()).isEmpty(); + assertThat(result.getErrors()).hasSize(1) + .containsExactly(badServerResponse("could not define media type for impression: impId")); + } + + private static BidRequest givenBidRequest(UnaryOperator... impCustomizers) { + final List imps = Arrays.stream(impCustomizers) + .map(ZentotemBidderTest::givenImp) + .toList(); + return BidRequest.builder().imp(imps).build(); + } + + private static Imp givenImp(UnaryOperator impCustomizer) { + return impCustomizer.apply(Imp.builder().id("impId")).build(); + } + + private static Bid givenBid(Integer mtype) { + return Bid.builder().id("bidId").impid("impId").price(BigDecimal.ONE).mtype(mtype).build(); + } + + private static String givenBidResponse(Bid... bids) throws JsonProcessingException { + return mapper.writeValueAsString(BidResponse.builder() + .cur("USD") + .seatbid(singletonList(SeatBid.builder().bid(List.of(bids)).build())) + .build()); + } + + private static BidderCall givenHttpCall(String body) { + return BidderCall.succeededHttp( + HttpRequest.builder().build(), + HttpResponse.of(200, null, body), + null); + } +} diff --git a/src/test/java/org/prebid/server/it/ZentotemTest.java b/src/test/java/org/prebid/server/it/ZentotemTest.java new file mode 100644 index 00000000000..6012e7cfe4a --- /dev/null +++ b/src/test/java/org/prebid/server/it/ZentotemTest.java @@ -0,0 +1,32 @@ +package org.prebid.server.it; + +import io.restassured.response.Response; +import org.json.JSONException; +import org.junit.jupiter.api.Test; +import org.prebid.server.model.Endpoint; + +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; + +public class ZentotemTest extends IntegrationTest { + + @Test + public void openrtb2AuctionShouldRespondWithBidsFromZentotem() throws IOException, JSONException { + // given + WIRE_MOCK_RULE.stubFor(post(urlPathEqualTo("/zentotem-exchange")) + .withRequestBody(equalToJson(jsonFrom("openrtb2/zentotem/test-zentotem-bid-request.json"))) + .willReturn(aResponse().withBody(jsonFrom("openrtb2/zentotem/test-zentotem-bid-response.json")))); + + // when + final Response response = responseFor("openrtb2/zentotem/test-auction-zentotem-request.json", + Endpoint.openrtb2_auction); + + // then + assertJsonEquals("openrtb2/zentotem/test-auction-zentotem-response.json", response, singletonList("zentotem")); + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/zentotem/test-auction-zentotem-request.json b/src/test/resources/org/prebid/server/it/openrtb2/zentotem/test-auction-zentotem-request.json new file mode 100644 index 00000000000..ce0353e4eba --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/zentotem/test-auction-zentotem-request.json @@ -0,0 +1,23 @@ +{ + "id": "request_id", + "imp": [ + { + "id": "imp_id", + "banner": { + "w": 300, + "h": 250 + }, + "ext": { + "zentotem": { + "property": "value" + } + } + } + ], + "tmax": 5000, + "regs": { + "ext": { + "gdpr": 0 + } + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/zentotem/test-auction-zentotem-response.json b/src/test/resources/org/prebid/server/it/openrtb2/zentotem/test-auction-zentotem-response.json new file mode 100644 index 00000000000..6aa0fcfc2c2 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/zentotem/test-auction-zentotem-response.json @@ -0,0 +1,43 @@ +{ + "id": "request_id", + "seatbid": [ + { + "bid": [ + { + "id": "bid_id", + "impid": "imp_id", + "exp": 300, + "price": 3.33, + "adm": "adm001", + "adid": "adid", + "cid": "cid", + "crid": "crid", + "mtype": 1, + "w": 300, + "h": 250, + "ext": { + "prebid": { + "type": "banner", + "meta": { + "adaptercode": "zentotem" + } + }, + "origbidcpm": 3.33 + } + } + ], + "seat": "zentotem", + "group": 0 + } + ], + "cur": "USD", + "ext": { + "responsetimemillis": { + "zentotem": "{{ zentotem.response_time_ms }}" + }, + "prebid": { + "auctiontimestamp": 0 + }, + "tmaxrequest": 5000 + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/zentotem/test-zentotem-bid-request.json b/src/test/resources/org/prebid/server/it/openrtb2/zentotem/test-zentotem-bid-request.json new file mode 100644 index 00000000000..1d41be4e061 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/zentotem/test-zentotem-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": { + "property": "value" + } + } + } + ], + "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/zentotem/test-zentotem-bid-response.json b/src/test/resources/org/prebid/server/it/openrtb2/zentotem/test-zentotem-bid-response.json new file mode 100644 index 00000000000..ae69236c864 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/zentotem/test-zentotem-bid-response.json @@ -0,0 +1,21 @@ +{ + "id": "request_id", + "seatbid": [ + { + "bid": [ + { + "id": "bid_id", + "impid": "imp_id", + "price": 3.33, + "adid": "adid", + "crid": "crid", + "cid": "cid", + "adm": "adm001", + "mtype": 1, + "h": 250, + "w": 300 + } + ] + } + ] +} 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 747b9a2fc6a..40c3a52cae2 100644 --- a/src/test/resources/org/prebid/server/it/test-application.properties +++ b/src/test/resources/org/prebid/server/it/test-application.properties @@ -603,6 +603,8 @@ adapters.yieldmo.enabled=true adapters.yieldmo.endpoint=http://localhost:8090/yieldmo-exchange adapters.yieldone.enabled=true adapters.yieldone.endpoint=http://localhost:8090/yieldone-exchange +adapters.zentotem.enabled=true +adapters.zentotem.endpoint=http://localhost:8090/zentotem-exchange adapters.zeroclickfraud.enabled=true adapters.zeroclickfraud.endpoint=http://{{Host}}/zeroclickfraud-exchange?sid={{SourceId}} adapters.aax.enabled=true