From 355d1b1bf412f8cb9190be0873749722425ae8d4 Mon Sep 17 00:00:00 2001 From: antonbabak Date: Tue, 20 Aug 2024 11:10:08 +0200 Subject: [PATCH] MeloZen Bidder --- .../server/bidder/melozen/MeloZenBidder.java | 211 +++++++++ .../ext/request/melozen/MeloZenImpExt.java | 12 + .../config/bidder/MeloZenConfiguration.java | 43 ++ src/main/resources/bidder-config/melozen.yaml | 19 + .../static/bidder-params/melozen.json | 16 + .../bidder/melozen/MeloZenBidderTest.java | 424 ++++++++++++++++++ .../org/prebid/server/it/MeloZenTest.java | 37 ++ .../melozen/test-auction-melozen-request.json | 25 ++ .../test-auction-melozen-response.json | 35 ++ .../melozen/test-melozen-bid-request.json | 58 +++ .../melozen/test-melozen-bid-response.json | 22 + .../server/it/test-application.properties | 2 + 12 files changed, 904 insertions(+) create mode 100644 src/main/java/org/prebid/server/bidder/melozen/MeloZenBidder.java create mode 100644 src/main/java/org/prebid/server/proto/openrtb/ext/request/melozen/MeloZenImpExt.java create mode 100644 src/main/java/org/prebid/server/spring/config/bidder/MeloZenConfiguration.java create mode 100644 src/main/resources/bidder-config/melozen.yaml create mode 100644 src/main/resources/static/bidder-params/melozen.json create mode 100644 src/test/java/org/prebid/server/bidder/melozen/MeloZenBidderTest.java create mode 100644 src/test/java/org/prebid/server/it/MeloZenTest.java create mode 100644 src/test/resources/org/prebid/server/it/openrtb2/melozen/test-auction-melozen-request.json create mode 100644 src/test/resources/org/prebid/server/it/openrtb2/melozen/test-auction-melozen-response.json create mode 100644 src/test/resources/org/prebid/server/it/openrtb2/melozen/test-melozen-bid-request.json create mode 100644 src/test/resources/org/prebid/server/it/openrtb2/melozen/test-melozen-bid-response.json diff --git a/src/main/java/org/prebid/server/bidder/melozen/MeloZenBidder.java b/src/main/java/org/prebid/server/bidder/melozen/MeloZenBidder.java new file mode 100644 index 00000000000..226a9a60149 --- /dev/null +++ b/src/main/java/org/prebid/server/bidder/melozen/MeloZenBidder.java @@ -0,0 +1,211 @@ +package org.prebid.server.bidder.melozen; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.node.ObjectNode; +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.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.lang3.StringUtils; +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.Price; +import org.prebid.server.bidder.model.Result; +import org.prebid.server.currency.CurrencyConversionService; +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.melozen.MeloZenImpExt; +import org.prebid.server.proto.openrtb.ext.response.BidType; +import org.prebid.server.proto.openrtb.ext.response.ExtBidPrebid; +import org.prebid.server.util.BidderUtil; +import org.prebid.server.util.HttpUtil; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +public class MeloZenBidder implements Bidder { + + private static final TypeReference> TYPE_REFERENCE = new TypeReference<>() { + }; + + private static final String PUBLISHER_ID_MACRO = "{{PublisherID}}"; + private static final String BIDDER_CURRENCY = "USD"; + private static final String EXT_PREBID = "prebid"; + + private final CurrencyConversionService currencyConversionService; + private final String endpointUrl; + private final JacksonMapper mapper; + + public MeloZenBidder(CurrencyConversionService currencyConversionService, + String endpoint, + JacksonMapper mapper) { + + this.currencyConversionService = Objects.requireNonNull(currencyConversionService); + this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpoint)); + this.mapper = Objects.requireNonNull(mapper); + } + + @Override + public Result>> makeHttpRequests(BidRequest request) { + final List> requests = new ArrayList<>(); + final List errors = new ArrayList<>(); + + for (Imp imp : request.getImp()) { + try { + final MeloZenImpExt impExt = parseImpExt(imp); + final String url = resolveEndpoint(impExt); + final Imp modifiedImp = modifyImp(request, imp); + splitImpByMediaType(modifiedImp).forEach(splitImp -> + requests.add(BidderUtil.defaultRequest(modifyRequest(request, splitImp), url, mapper))); + } catch (PreBidException e) { + errors.add(BidderError.badInput(e.getMessage())); + } + } + + return Result.of(requests, errors); + } + + private MeloZenImpExt parseImpExt(Imp imp) { + try { + return mapper.mapper().convertValue(imp.getExt(), TYPE_REFERENCE).getBidder(); + } catch (IllegalArgumentException e) { + throw new PreBidException(e.getMessage()); + } + } + + private Imp modifyImp(BidRequest bidRequest, Imp imp) { + final Price resolvedFloor = resolveBidFloor(bidRequest, imp); + return imp.toBuilder() + .bidfloor(resolvedFloor.getValue()) + .bidfloorcur(resolvedFloor.getCurrency()) + .build(); + } + + private Price resolveBidFloor(BidRequest bidRequest, Imp imp) { + final BigDecimal bidFloor = imp.getBidfloor(); + final String bidFloorCurrency = imp.getBidfloorcur(); + + if (BidderUtil.isValidPrice(bidFloor) + && StringUtils.isNotBlank(bidFloorCurrency) + && !StringUtils.equalsIgnoreCase(bidFloorCurrency, BIDDER_CURRENCY)) { + + final BigDecimal convertedFloor = currencyConversionService.convertCurrency( + bidFloor, + bidRequest, + bidFloorCurrency, + BIDDER_CURRENCY); + + return Price.of(BIDDER_CURRENCY, convertedFloor); + } + + return Price.of(bidFloorCurrency, bidFloor); + } + + private String resolveEndpoint(MeloZenImpExt impExt) { + return endpointUrl + .replace(PUBLISHER_ID_MACRO, HttpUtil.encodeUrl(StringUtils.defaultString(impExt.getPubId()))); + } + + private List splitImpByMediaType(Imp imp) { + final Banner banner = imp.getBanner(); + final Video video = imp.getVideo(); + final Native xNative = imp.getXNative(); + + if (ObjectUtils.allNull(banner, video, xNative)) { + throw new PreBidException("Invalid MediaType. MeloZen only supports Banner, Video and Native."); + } + + final List imps = new ArrayList<>(); + + if (banner != null) { + imps.add(imp.toBuilder().video(null).xNative(null).build()); + } + + if (video != null) { + imps.add(imp.toBuilder().banner(null).xNative(null).build()); + } + + if (xNative != null) { + imps.add(imp.toBuilder().banner(null).video(null).build()); + } + + return imps; + } + + private BidRequest modifyRequest(BidRequest request, Imp imp) { + return request.toBuilder() + .imp(Collections.singletonList(imp)) + .build(); + } + + @Override + public final Result> makeBids(BidderCall httpCall, BidRequest bidRequest) { + try { + final List errors = new ArrayList<>(); + final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class); + return Result.of(extractBids(bidResponse, errors), errors); + } catch (DecodeException | PreBidException e) { + return Result.withError(BidderError.badServerResponse(e.getMessage())); + } + } + + private List extractBids(BidResponse bidResponse, List errors) { + if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) { + return Collections.emptyList(); + } + + return bidResponse.getSeatbid().stream() + .filter(Objects::nonNull) + .map(SeatBid::getBid) + .filter(Objects::nonNull) + .flatMap(Collection::stream) + .filter(Objects::nonNull) + .map(bid -> toBidderBid(bid, bidResponse.getCur(), errors)) + .filter(Objects::nonNull) + .toList(); + } + + private BidderBid toBidderBid(Bid bid, String currency, List errors) { + try { + return BidderBid.of(bid, getBidType(bid), currency); + } catch (PreBidException e) { + errors.add(BidderError.badServerResponse(e.getMessage())); + return null; + } + } + + private BidType getBidType(Bid bid) { + return Optional.ofNullable(bid.getExt()) + .map(ext -> ext.get(EXT_PREBID)) + .map(ObjectNode.class::cast) + .map(this::parseExtBidPrebid) + .map(ExtBidPrebid::getType) + .orElseThrow(() -> new PreBidException( + "Failed to parse bid mediatype for impression \"%s\"".formatted(bid.getImpid()))); + } + + private ExtBidPrebid parseExtBidPrebid(ObjectNode prebid) { + try { + return mapper.mapper().treeToValue(prebid, ExtBidPrebid.class); + } catch (JsonProcessingException e) { + return null; + } + } +} diff --git a/src/main/java/org/prebid/server/proto/openrtb/ext/request/melozen/MeloZenImpExt.java b/src/main/java/org/prebid/server/proto/openrtb/ext/request/melozen/MeloZenImpExt.java new file mode 100644 index 00000000000..ae7b45e8c86 --- /dev/null +++ b/src/main/java/org/prebid/server/proto/openrtb/ext/request/melozen/MeloZenImpExt.java @@ -0,0 +1,12 @@ +package org.prebid.server.proto.openrtb.ext.request.melozen; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Value; + +@Value(staticConstructor = "of") +public class MeloZenImpExt { + + @JsonProperty("pubId") + String pubId; + +} diff --git a/src/main/java/org/prebid/server/spring/config/bidder/MeloZenConfiguration.java b/src/main/java/org/prebid/server/spring/config/bidder/MeloZenConfiguration.java new file mode 100644 index 00000000000..797ec11730c --- /dev/null +++ b/src/main/java/org/prebid/server/spring/config/bidder/MeloZenConfiguration.java @@ -0,0 +1,43 @@ +package org.prebid.server.spring.config.bidder; + +import org.prebid.server.bidder.BidderDeps; +import org.prebid.server.bidder.melozen.MeloZenBidder; +import org.prebid.server.currency.CurrencyConversionService; +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/melozen.yaml", factory = YamlPropertySourceFactory.class) +public class MeloZenConfiguration { + + private static final String BIDDER_NAME = "melozen"; + + @Bean("melozenConfigurationProperties") + @ConfigurationProperties("adapters.melozen") + BidderConfigurationProperties configurationProperties() { + return new BidderConfigurationProperties(); + } + + @Bean + BidderDeps melozenBidderDeps(BidderConfigurationProperties melozenConfigurationProperties, + CurrencyConversionService currencyConversionService, + @NotBlank @Value("${external-url}") String externalUrl, + JacksonMapper mapper) { + + return BidderDepsAssembler.forBidder(BIDDER_NAME) + .withConfig(melozenConfigurationProperties) + .usersyncerCreator(UsersyncerCreator.create(externalUrl)) + .bidderCreator(config -> new MeloZenBidder(currencyConversionService, config.getEndpoint(), mapper)) + .assemble(); + } +} diff --git a/src/main/resources/bidder-config/melozen.yaml b/src/main/resources/bidder-config/melozen.yaml new file mode 100644 index 00000000000..8626fd4eabe --- /dev/null +++ b/src/main/resources/bidder-config/melozen.yaml @@ -0,0 +1,19 @@ +adapters: + melozen: + endpoint: https://prebid.melozen.com/rtb/v2/bid?publisher_id={{PublisherID}} + endpoint-compression: gzip + modifying-vast-xml-allowed: true + geoscope: + - global + meta-info: + maintainer-email: DSP@melodong.com + 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/melozen.json b/src/main/resources/static/bidder-params/melozen.json new file mode 100644 index 00000000000..eebd391944b --- /dev/null +++ b/src/main/resources/static/bidder-params/melozen.json @@ -0,0 +1,16 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "MeloZen Adapter Params", + "description": "A schema which validates params accepted by the MeloZen adapter", + "type": "object", + "properties": { + "pubId": { + "type": "string", + "minLength": 1, + "description": "The unique identifier for the publisher." + } + }, + "required": [ + "pubId" + ] +} diff --git a/src/test/java/org/prebid/server/bidder/melozen/MeloZenBidderTest.java b/src/test/java/org/prebid/server/bidder/melozen/MeloZenBidderTest.java new file mode 100644 index 00000000000..a62b8f0ab49 --- /dev/null +++ b/src/test/java/org/prebid/server/bidder/melozen/MeloZenBidderTest.java @@ -0,0 +1,424 @@ +package org.prebid.server.bidder.melozen; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.node.ObjectNode; +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.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +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.currency.CurrencyConversionService; +import org.prebid.server.proto.openrtb.ext.ExtPrebid; +import org.prebid.server.proto.openrtb.ext.request.melozen.MeloZenImpExt; + +import java.math.BigDecimal; +import java.util.Arrays; +import java.util.List; +import java.util.function.UnaryOperator; + +import static java.util.Arrays.asList; +import static java.util.Collections.singleton; +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.assertj.core.api.Assertions.tuple; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.verifyNoInteractions; +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; +import static org.prebid.server.util.HttpUtil.ACCEPT_HEADER; +import static org.prebid.server.util.HttpUtil.APPLICATION_JSON_CONTENT_TYPE; +import static org.prebid.server.util.HttpUtil.CONTENT_TYPE_HEADER; +import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON_VALUE; + +@ExtendWith(MockitoExtension.class) +class MeloZenBidderTest extends VertxTest { + + private static final String ENDPOINT_URL = "https://test-url.com/{{PublisherID}}"; + + @Mock + private CurrencyConversionService currencyConversionService; + + private MeloZenBidder target; + + @BeforeEach + public void before() { + target = new MeloZenBidder(currencyConversionService, ENDPOINT_URL, jacksonMapper); + } + + @Test + public void creationShouldFailOnInvalidEndpointUrl() { + // when and then + assertThatIllegalArgumentException().isThrownBy(() -> + new MeloZenBidder(currencyConversionService, "invalid_url", jacksonMapper)); + } + + @Test + public void makeHttpRequestsShouldReturnErrorIfImpExtCouldNotBeParsed() { + // given + final BidRequest bidRequest = givenBidRequest( + imp -> imp.ext(mapper.valueToTree(ExtPrebid.of(null, mapper.createArrayNode())))); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getValue()).isEmpty(); + assertThat(result.getErrors()).hasSize(1).allSatisfy(bidderError -> { + assertThat(bidderError.getType()).isEqualTo(BidderError.Type.bad_input); + assertThat(bidderError.getMessage()).startsWith("Cannot deserialize value"); + }); + } + + @Test + public void makeHttpRequestsShouldMakeOneRequestPerImp() { + // given + final BidRequest bidRequest = givenBidRequest(imp -> imp.id("givenImp1"), imp -> imp.id("givenImp2")); + + //when + final Result>> result = target.makeHttpRequests(bidRequest); + + //then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).hasSize(2) + .extracting(HttpRequest::getPayload) + .flatExtracting(BidRequest::getImp) + .containsExactlyInAnyOrderElementsOf(bidRequest.getImp()); + } + + @Test + public void makeHttpRequestsShouldMakeOneRequestPerImpPerMediaType() { + // given + final BidRequest bidRequest = givenBidRequest( + imp -> imp.id("givenImp1") + .banner(Banner.builder().build()) + .video(Video.builder().build()) + .xNative(Native.builder().build()), + imp -> imp.id("givenImp2") + .banner(null) + .xNative(null) + .video(Video.builder().build())); + + //when + final Result>> result = target.makeHttpRequests(bidRequest); + + //then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).hasSize(4) + .extracting(HttpRequest::getPayload) + .flatExtracting(BidRequest::getImp) + .containsExactlyInAnyOrder( + givenImp(imp -> imp.id("givenImp1").banner(Banner.builder().build()).video(null).xNative(null)), + givenImp(imp -> imp.id("givenImp1").banner(null).video(Video.builder().build()).xNative(null)), + givenImp(imp -> imp.id("givenImp1").banner(null).video(null).xNative(Native.builder().build())), + givenImp(imp -> imp.id("givenImp2").banner(null).video(Video.builder().build()).xNative(null))); + } + + @Test + public void makeHttpRequestsShouldReturnErrorWhenImpDoesNotHaveBannerVideoOrNative() { + // given + final BidRequest bidRequest = givenBidRequest(imp -> imp.id("impid").banner(null).video(null).xNative(null)); + + //when + final Result>> result = target.makeHttpRequests(bidRequest); + + //then + assertThat(result.getValue()).isEmpty(); + assertThat(result.getErrors()).containsOnly( + BidderError.badInput("Invalid MediaType. MeloZen only supports Banner, Video and Native.")); + } + + @Test + public void makeHttpRequestsShouldHaveImpIds() { + // given + final BidRequest bidRequest = givenBidRequest(imp -> imp.id("givenImp1"), imp -> imp.id("givenImp2")); + + //when + final Result>> result = target.makeHttpRequests(bidRequest); + + //then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).hasSize(2) + .extracting(HttpRequest::getImpIds) + .containsExactlyInAnyOrder(singleton("givenImp1"), singleton("givenImp2")); + } + + @Test + public void makeHttpRequestsShouldReturnExpectedHeaders() { + // given + final BidRequest bidRequest = givenBidRequest(identity()); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getValue()).hasSize(1).first() + .extracting(HttpRequest::getHeaders) + .satisfies(headers -> assertThat(headers.get(CONTENT_TYPE_HEADER)) + .isEqualTo(APPLICATION_JSON_CONTENT_TYPE)) + .satisfies(headers -> assertThat(headers.get(ACCEPT_HEADER)) + .isEqualTo(APPLICATION_JSON_VALUE)); + assertThat(result.getErrors()).isEmpty(); + } + + @Test + public void shouldMakeOneRequestWhenOneImpIsValidAndAnotherAreInvalid() { + // given + final BidRequest bidRequest = givenBidRequest( + imp -> imp.id("impId1").ext(mapper.valueToTree(ExtPrebid.of(null, mapper.createArrayNode()))), + imp -> imp.id("impId2").banner(null).video(null).xNative(null), + imp -> imp.id("impId3")); + + //when + final Result>> result = target.makeHttpRequests(bidRequest); + + //then + assertThat(result.getValue()).hasSize(1) + .extracting(HttpRequest::getPayload) + .flatExtracting(BidRequest::getImp) + .extracting(Imp::getId) + .containsExactly("impId3"); + } + + @Test + public void makeHttpRequestsShouldUseCorrectUri() { + // given + final BidRequest bidRequest = givenBidRequest(identity()); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getUri) + .containsExactly("https://test-url.com/publisherId"); + } + + @Test + public void makeHttpRequestsShouldNotConvertBidfloorWhenBidfloorHasUSDCurrency() { + // given + final BidRequest bidRequest = givenBidRequest(imp -> imp.bidfloor(BigDecimal.TEN).bidfloorcur("USD")); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getPayload) + .flatExtracting(BidRequest::getImp) + .extracting(Imp::getBidfloor, Imp::getBidfloorcur) + .containsOnly(tuple(BigDecimal.TEN, "USD")); + + verifyNoInteractions(currencyConversionService); + } + + @Test + public void makeHttpRequestsShouldNotConvertBidfloorAndAssignUSDCurrencyWhenBidfloorIsEmpty() { + // given + final BidRequest bidRequest = givenBidRequest(imp -> imp.bidfloor(null).bidfloorcur("EUR")); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getPayload) + .flatExtracting(BidRequest::getImp) + .extracting(Imp::getBidfloor, Imp::getBidfloorcur) + .containsOnly(tuple(null, "EUR")); + + verifyNoInteractions(currencyConversionService); + } + + @Test + public void makeHttpRequestsShouldNotConvertBidfloorWhenBidfloorHasEmptyCurrency() { + // given + final BidRequest bidRequest = givenBidRequest(imp -> imp.bidfloor(BigDecimal.TEN).bidfloorcur(null)); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getPayload) + .flatExtracting(BidRequest::getImp) + .extracting(Imp::getBidfloor, Imp::getBidfloorcur) + .containsOnly(tuple(BigDecimal.TEN, null)); + + verifyNoInteractions(currencyConversionService); + } + + @Test + public void makeHttpRequestsShouldConvertBidfloorToUSDWhenBidfloorHasAnotherCurrency() { + // given + final BidRequest bidRequest = givenBidRequest(imp -> imp.bidfloor(BigDecimal.TEN).bidfloorcur("EUR")); + + given(currencyConversionService.convertCurrency(BigDecimal.TEN, bidRequest, "EUR", "USD")) + .willReturn(BigDecimal.ONE); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .extracting(HttpRequest::getPayload) + .flatExtracting(BidRequest::getImp) + .extracting(Imp::getBidfloor, Imp::getBidfloorcur) + .containsOnly(tuple(BigDecimal.ONE, "USD")); + } + + @Test + public void makeBidsShouldReturnErrorWhenResponseCanNotBeParsed() { + // given + final BidderCall httpCall = givenHttpCall("invalid"); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getValue()).isEmpty(); + assertThat(result.getErrors()).hasSize(1).allSatisfy(error -> { + assertThat(error.getType()).isEqualTo(BidderError.Type.bad_server_response); + assertThat(error.getMessage()).startsWith("Failed to decode"); + }); + } + + @Test + public void makeBidsShouldReturnErrorWhenResponseDoesNotHaveSeatBid() throws JsonProcessingException { + // given + final BidderCall httpCall = givenHttpCall(givenBidResponse()); + + // when + final Result> actual = target.makeBids(httpCall, null); + + // then + assertThat(actual.getValue()).isEmpty(); + assertThat(actual.getErrors()).isEmpty(); + } + + @Test + public void makeBidsShouldReturnBannerBidBasedOnBidExt() throws JsonProcessingException { + // given + final ObjectNode prebidNode = mapper.createObjectNode().put("type", "banner"); + final ObjectNode bidExtNode = mapper.createObjectNode().set("prebid", prebidNode); + final Bid bannerBid = Bid.builder().impid("1").ext(bidExtNode).build(); + + final BidderCall httpCall = givenHttpCall(givenBidResponse(bannerBid)); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).containsExactly(BidderBid.of(bannerBid, banner, null)); + + } + + @Test + public void makeBidsShouldReturnVideoBidBasedOnBidExt() throws JsonProcessingException { + // given + final ObjectNode prebidNode = mapper.createObjectNode().put("type", "video"); + final ObjectNode bidExtNode = mapper.createObjectNode().set("prebid", prebidNode); + final Bid videoBid = Bid.builder().impid("2").ext(bidExtNode).build(); + + final BidderCall httpCall = givenHttpCall(givenBidResponse(videoBid)); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).containsExactly(BidderBid.of(videoBid, video, null)); + } + + @Test + public void makeBidsShouldReturnNativeBidOnBidExt() throws JsonProcessingException { + // given + final ObjectNode prebidNode = mapper.createObjectNode().put("type", "native"); + final ObjectNode bidExtNode = mapper.createObjectNode().set("prebid", prebidNode); + final Bid nativeBid = Bid.builder().impid("3").ext(bidExtNode).build(); + + final BidderCall httpCall = givenHttpCall(givenBidResponse(nativeBid)); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).containsExactly(BidderBid.of(nativeBid, xNative, null)); + } + + @Test + public void makeBidsShouldReturnErrorWhenBidExtCanNotResolveType() throws JsonProcessingException { + // given + final ObjectNode prebidNode = mapper.createObjectNode().put("type", "unknown"); + final ObjectNode bidExtNode = mapper.createObjectNode().set("prebid", prebidNode); + final Bid bid = Bid.builder().impid("3").ext(bidExtNode).build(); + + final BidderCall httpCall = givenHttpCall(givenBidResponse(bid)); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()) + .containsOnly(BidderError.badServerResponse("Failed to parse bid mediatype for impression \"3\"")); + assertThat(result.getValue()).isEmpty(); + } + + private static BidRequest givenBidRequest(UnaryOperator... impCustomizers) { + return BidRequest.builder() + .imp(Arrays.stream(impCustomizers).map(MeloZenBidderTest::givenImp).toList()) + .build(); + } + + private static Imp givenImp(UnaryOperator impCustomizer) { + return impCustomizer.apply(Imp.builder() + .id("impId") + .banner(Banner.builder().build()) + .bidfloor(BigDecimal.TEN) + .bidfloorcur("USD") + .ext(mapper.valueToTree(ExtPrebid.of(null, MeloZenImpExt.of("publisherId"))))) + .build(); + } + + private static String givenBidResponse(Bid... bids) throws JsonProcessingException { + return mapper.writeValueAsString(BidResponse.builder() + .seatbid(singletonList(SeatBid.builder().bid(asList(bids)).build())) + .build()); + } + + private static Bid givenBid(UnaryOperator bidCustomizer) { + return bidCustomizer.apply(Bid.builder()).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/MeloZenTest.java b/src/test/java/org/prebid/server/it/MeloZenTest.java new file mode 100644 index 00000000000..185aa894a2f --- /dev/null +++ b/src/test/java/org/prebid/server/it/MeloZenTest.java @@ -0,0 +1,37 @@ +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 java.util.List; + +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; + +public class MeloZenTest extends IntegrationTest { + + @Test + public void openrtb2AuctionShouldRespondWithBidsFromMelozen() throws IOException, JSONException { + // given + WIRE_MOCK_RULE.stubFor(post(urlPathEqualTo("/melozen-exchange")) + .withQueryParam("pubId", equalTo("publisherId")) + .withRequestBody(equalToJson(jsonFrom("openrtb2/melozen/test-melozen-bid-request.json"))) + .willReturn(aResponse().withBody(jsonFrom("openrtb2/melozen/test-melozen-bid-response.json")))); + + // when + final Response response = responseFor( + "openrtb2/melozen/test-auction-melozen-request.json", + Endpoint.openrtb2_auction + ); + + // then + assertJsonEquals("openrtb2/melozen/test-auction-melozen-response.json", response, List.of("melozen")); + } + +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/melozen/test-auction-melozen-request.json b/src/test/resources/org/prebid/server/it/openrtb2/melozen/test-auction-melozen-request.json new file mode 100644 index 00000000000..16518953d21 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/melozen/test-auction-melozen-request.json @@ -0,0 +1,25 @@ +{ + "id": "request_id", + "imp": [ + { + "id": "imp_id", + "banner": { + "w": 320, + "h": 250 + }, + "bidfloor": 10, + "bidfloorcur": "USD", + "ext": { + "melozen": { + "pubId": "publisherId" + } + } + } + ], + "tmax": 5000, + "regs": { + "ext": { + "gdpr": 0 + } + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/melozen/test-auction-melozen-response.json b/src/test/resources/org/prebid/server/it/openrtb2/melozen/test-auction-melozen-response.json new file mode 100644 index 00000000000..42c6696f9bb --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/melozen/test-auction-melozen-response.json @@ -0,0 +1,35 @@ +{ + "id": "request_id", + "seatbid": [ + { + "bid": [ + { + "id": "bid_id", + "impid": "imp_id", + "price": 0.01, + "adid": "2068416", + "cid": "8048", + "crid": "24080", + "ext": { + "prebid": { + "type": "banner" + }, + "origbidcpm": 0.01 + } + } + ], + "seat": "melozen", + "group": 0 + } + ], + "cur": "USD", + "ext": { + "responsetimemillis": { + "melozen": "{{ melozen.response_time_ms }}" + }, + "prebid": { + "auctiontimestamp": 0 + }, + "tmaxrequest": 5000 + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/melozen/test-melozen-bid-request.json b/src/test/resources/org/prebid/server/it/openrtb2/melozen/test-melozen-bid-request.json new file mode 100644 index 00000000000..c7a91804b4f --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/melozen/test-melozen-bid-request.json @@ -0,0 +1,58 @@ +{ + "id": "request_id", + "imp": [ + { + "id": "imp_id", + "secure": 1, + "banner": { + "w": 320, + "h": 250 + }, + "bidfloor": 10, + "bidfloorcur": "USD", + "ext": { + "tid": "${json-unit.any-string}", + "bidder": { + "pubId": "publisherId" + } + } + } + ], + "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/melozen/test-melozen-bid-response.json b/src/test/resources/org/prebid/server/it/openrtb2/melozen/test-melozen-bid-response.json new file mode 100644 index 00000000000..93d9130a19b --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/melozen/test-melozen-bid-response.json @@ -0,0 +1,22 @@ +{ + "id": "tid", + "seatbid": [ + { + "bid": [ + { + "crid": "24080", + "adid": "2068416", + "price": 0.01, + "id": "bid_id", + "impid": "imp_id", + "cid": "8048", + "ext": { + "prebid": { + "type": "banner" + } + } + } + ] + } + ] +} 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 9c32c0638d0..96cff05e2da 100644 --- a/src/test/resources/org/prebid/server/it/test-application.properties +++ b/src/test/resources/org/prebid/server/it/test-application.properties @@ -291,6 +291,8 @@ adapters.mediago.enabled=true adapters.mediago.endpoint=http://localhost:8090/mediago-exchange?token={{AccountID}}®ion={{Host}} adapters.medianet.enabled=true adapters.medianet.endpoint=http://localhost:8090/medianet-exchange +adapters.melozen.enabled=true +adapters.melozen.endpoint=http://localhost:8090/melozen-exchange?pubId={{PublisherID}} adapters.mgid.enabled=true adapters.mgid.endpoint=http://localhost:8090/mgid-exchange/ adapters.mgidX.enabled=true