diff --git a/src/main/java/org/prebid/server/bidder/pgamssp/PgamSspBidder.java b/src/main/java/org/prebid/server/bidder/pgamssp/PgamSspBidder.java new file mode 100644 index 00000000000..ce0a5161120 --- /dev/null +++ b/src/main/java/org/prebid/server/bidder/pgamssp/PgamSspBidder.java @@ -0,0 +1,134 @@ +package org.prebid.server.bidder.pgamssp; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.node.ObjectNode; +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 io.vertx.core.http.HttpMethod; +import org.apache.commons.collections4.CollectionUtils; +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.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.pgamssp.PgamSspImpExt; +import org.prebid.server.proto.openrtb.ext.response.BidType; +import org.prebid.server.util.HttpUtil; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +public class PgamSspBidder implements Bidder { + + private static final TypeReference> PGAMSSP_EXT_TYPE_REFERENCE = new TypeReference<>() { + }; + private static final String PUBLISHER_IMP_EXT_TYPE = "publisher"; + private static final String NETWORK_IMP_EXT_TYPE = "network"; + + private final String endpointUrl; + private final JacksonMapper mapper; + + public PgamSspBidder(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<>(); + + for (Imp imp : request.getImp()) { + try { + final PgamSspImpExt impExt = parseImpExt(imp); + final BidRequest modifiedBidRequest = makeRequest(request, imp, impExt); + httpRequests.add(makeHttpRequest(modifiedBidRequest, imp.getId())); + } catch (PreBidException e) { + return Result.withError(BidderError.badInput(e.getMessage())); + } + } + + return Result.withValues(httpRequests); + } + + private PgamSspImpExt parseImpExt(Imp imp) throws PreBidException { + try { + return mapper.mapper().convertValue(imp.getExt(), PGAMSSP_EXT_TYPE_REFERENCE).getBidder(); + } catch (IllegalArgumentException e) { + throw new PreBidException("Invalid ext. Imp.Id: " + imp.getId()); + } + } + + private BidRequest makeRequest(BidRequest request, Imp imp, PgamSspImpExt impExt) { + final boolean hasPlacement = StringUtils.isNotBlank(impExt.getPlacementId()); + final boolean hasEndpoint = !hasPlacement && StringUtils.isNotBlank(impExt.getEndpointId()); + final PgamSspImpExt modifiedImpExt = PgamSspImpExt.builder() + .placementId(hasPlacement ? impExt.getPlacementId() : null) + .endpointId(hasEndpoint ? impExt.getEndpointId() : null) + .type(hasPlacement ? PUBLISHER_IMP_EXT_TYPE : hasEndpoint ? NETWORK_IMP_EXT_TYPE : null) + .build(); + + final ObjectNode ext = mapper.mapper().valueToTree(ExtPrebid.of(null, modifiedImpExt)); + return request.toBuilder().imp(Collections.singletonList(imp.toBuilder().ext(ext).build())).build(); + } + + private HttpRequest makeHttpRequest(BidRequest bidRequest, String impId) { + return HttpRequest.builder() + .method(HttpMethod.POST) + .headers(HttpUtil.headers()) + .impIds(Collections.singleton(impId)) + .uri(endpointUrl) + .body(mapper.encodeToBytes(bidRequest)) + .payload(bidRequest) + .build(); + } + + @Override + public Result> makeBids(BidderCall httpCall, BidRequest bidRequest) { + try { + final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class); + return Result.withValues(extractBids(bidResponse)); + } catch (DecodeException e) { + return Result.withError(BidderError.badServerResponse("Bad Server Response")); + } catch (PreBidException e) { + return Result.withError(BidderError.badServerResponse(e.getMessage())); + } + } + + private List extractBids(BidResponse bidResponse) { + if (bidResponse == null || CollectionUtils.isEmpty(bidResponse.getSeatbid())) { + throw new PreBidException("Empty SeatBid array"); + } + + return bidResponse.getSeatbid().stream() + .flatMap(seatBid -> Optional.ofNullable(seatBid.getBid()).orElse(List.of()).stream()) + .map(bid -> BidderBid.of(bid, getBidMediaType(bid), bidResponse.getCur())) + .toList(); + } + + private static BidType getBidMediaType(Bid bid) { + final Integer markupType = bid.getMtype(); + if (markupType == null) { + throw new PreBidException("Missing MType for bid: " + bid.getId()); + } + + return switch (markupType) { + case 1 -> BidType.banner; + case 2 -> BidType.video; + case 3 -> BidType.audio; + case 4 -> BidType.xNative; + default -> throw new PreBidException( + "Unable to fetch mediaType " + bid.getMtype() + " in multi-format: " + bid.getImpid()); + }; + } +} diff --git a/src/main/java/org/prebid/server/proto/openrtb/ext/request/pgamssp/PgamSspImpExt.java b/src/main/java/org/prebid/server/proto/openrtb/ext/request/pgamssp/PgamSspImpExt.java new file mode 100644 index 00000000000..468ee01fcc7 --- /dev/null +++ b/src/main/java/org/prebid/server/proto/openrtb/ext/request/pgamssp/PgamSspImpExt.java @@ -0,0 +1,19 @@ +package org.prebid.server.proto.openrtb.ext.request.pgamssp; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Builder; +import lombok.Value; + +@Value +@Builder +public class PgamSspImpExt { + + String type; + + @JsonProperty("placementId") + String placementId; + + @JsonProperty("endpointId") + String endpointId; + +} diff --git a/src/main/java/org/prebid/server/spring/config/bidder/PgamSspConfiguration.java b/src/main/java/org/prebid/server/spring/config/bidder/PgamSspConfiguration.java new file mode 100644 index 00000000000..80304cbc659 --- /dev/null +++ b/src/main/java/org/prebid/server/spring/config/bidder/PgamSspConfiguration.java @@ -0,0 +1,41 @@ +package org.prebid.server.spring.config.bidder; + +import org.prebid.server.bidder.BidderDeps; +import org.prebid.server.bidder.pgamssp.PgamSspBidder; +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/pgamssp.yaml", factory = YamlPropertySourceFactory.class) +public class PgamSspConfiguration { + + private static final String BIDDER_NAME = "pgamssp"; + + @Bean("pgamsspConfigurationProperties") + @ConfigurationProperties("adapters.pgamssp") + BidderConfigurationProperties configurationProperties() { + return new BidderConfigurationProperties(); + } + + @Bean + BidderDeps pgamsspBidderDeps(BidderConfigurationProperties pgamsspConfigurationProperties, + @NotBlank @Value("${external-url}") String externalUrl, + JacksonMapper mapper) { + + return BidderDepsAssembler.forBidder(BIDDER_NAME) + .withConfig(pgamsspConfigurationProperties) + .usersyncerCreator(UsersyncerCreator.create(externalUrl)) + .bidderCreator(config -> new PgamSspBidder(config.getEndpoint(), mapper)) + .assemble(); + } +} diff --git a/src/main/resources/bidder-config/pgamssp.yaml b/src/main/resources/bidder-config/pgamssp.yaml new file mode 100644 index 00000000000..e95261073f3 --- /dev/null +++ b/src/main/resources/bidder-config/pgamssp.yaml @@ -0,0 +1,21 @@ +adapters: + pgamssp: + endpoint: http://us-east.pgammedia.com/pserver + meta-info: + maintainer-email: info@pgammedia.com + app-media-types: + - banner + - video + - native + site-media-types: + - banner + - video + - native + supported-vendors: + vendor-id: 0 + usersync: + cookie-family-name: pgamssp + redirect: + url: https://cs.pgammedia.com/pserver?gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&us_privacy={{us_privacy}}&r={{redirect_url}} + support-cors: false + uid-macro: '[UID]' diff --git a/src/main/resources/static/bidder-params/pgamssp.json b/src/main/resources/static/bidder-params/pgamssp.json new file mode 100644 index 00000000000..f8204494c8e --- /dev/null +++ b/src/main/resources/static/bidder-params/pgamssp.json @@ -0,0 +1,23 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "PgamSSP Adapter Params", + "description": "A schema which validates params accepted by the PgamSSP adapter", + "type": "object", + + "properties": { + "placementId": { + "type": "string", + "minLength": 1, + "description": "Placement ID" + }, + "endpointId": { + "type": "string", + "minLength": 1, + "description": "Endpoint ID" + } + }, + "oneOf": [ + { "required": ["placementId"] }, + { "required": ["endpointId"] } + ] +} diff --git a/src/test/java/org/prebid/server/bidder/pgamssp/PgamSspBidderTest.java b/src/test/java/org/prebid/server/bidder/pgamssp/PgamSspBidderTest.java new file mode 100644 index 00000000000..21d0b6d110f --- /dev/null +++ b/src/test/java/org/prebid/server/bidder/pgamssp/PgamSspBidderTest.java @@ -0,0 +1,353 @@ +package org.prebid.server.bidder.pgamssp; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.node.ObjectNode; +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 io.vertx.core.http.HttpMethod; +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.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.pgamssp.PgamSspImpExt; + +import java.util.Arrays; +import java.util.List; +import java.util.Set; +import java.util.function.UnaryOperator; + +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.prebid.server.bidder.model.BidderError.badInput; +import static org.prebid.server.bidder.model.BidderError.badServerResponse; +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; + +public class PgamSspBidderTest extends VertxTest { + + private static final String ENDPOINT_URL = "http://test-url.com"; + + private final PgamSspBidder target = new PgamSspBidder(ENDPOINT_URL, jacksonMapper); + + @Test + public void creationShouldFailOnInvalidEndpointUrl() { + assertThatIllegalArgumentException().isThrownBy(() -> new PgamSspBidder("invalid_url", jacksonMapper)); + } + + @Test + public void makeHttpRequestsShouldReturnErrorWhenRequestHasInvalidImpression() { + // given + final ObjectNode invalidExt = mapper.valueToTree(ExtPrebid.of(null, mapper.createArrayNode())); + final BidRequest bidRequest = givenBidRequest(impBuilder -> impBuilder.ext(invalidExt)); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).hasSize(1).first().isEqualTo(badInput("Invalid ext. Imp.Id: imp_id")); + } + + @Test + public void makeHttpRequestsShouldReturnPublisherTypeWhenRequestHasPlacementId() { + // given + final ObjectNode impExt = givenImpExt(extBuilder -> extBuilder.placementId("placementId")); + final BidRequest bidRequest = givenBidRequest(impBuilder -> impBuilder.ext(impExt)); + + // when + final Result>> results = target.makeHttpRequests(bidRequest); + + // then + final ObjectNode expectedImpExt = givenImpExt( + extBuilder -> extBuilder.placementId("placementId").type("publisher")); + final BidRequest expectedBidRequest = givenBidRequest(impBuilder -> impBuilder.ext(expectedImpExt)); + assertThat(results.getValue()).hasSize(1).first() + .satisfies(request -> assertThat(request.getBody()) + .isEqualTo(jacksonMapper.encodeToBytes(expectedBidRequest))) + .satisfies(request -> assertThat(request.getPayload()) + .isEqualTo(expectedBidRequest)); + assertThat(results.getErrors()).isEmpty(); + } + + @Test + public void makeHttpRequestsShouldReturnNetworkTypeWhenRequestHasEndpointId() { + // given + final ObjectNode impExt = givenImpExt(extBuilder -> extBuilder.endpointId("endpointId")); + final BidRequest bidRequest = givenBidRequest(impBuilder -> impBuilder.ext(impExt)); + + // when + final Result>> results = target.makeHttpRequests(bidRequest); + + // then + final ObjectNode expectedImpExt = givenImpExt( + extBuilder -> extBuilder.endpointId("endpointId").type("network")); + final BidRequest expectedBidRequest = givenBidRequest(impBuilder -> impBuilder.ext(expectedImpExt)); + assertThat(results.getValue()).hasSize(1).first() + .satisfies(request -> assertThat(request.getPayload()) + .isEqualTo(expectedBidRequest)) + .satisfies(request -> assertThat(request.getBody()) + .isEqualTo(jacksonMapper.encodeToBytes(expectedBidRequest))); + assertThat(results.getErrors()).isEmpty(); + } + + @Test + public void makeHttpRequestsShouldReturnPublisherTypeWhenRequestHasPlacementIdAndEndpointId() { + // given + final ObjectNode impExt = givenImpExt( + extBuilder -> extBuilder.placementId("placementId").endpointId("endpointId")); + final BidRequest bidRequest = givenBidRequest(impBuilder -> impBuilder.ext(impExt)); + + // when + final Result>> results = target.makeHttpRequests(bidRequest); + + // then + final ObjectNode expectedImpExt = givenImpExt( + extBuilder -> extBuilder.placementId("placementId").type("publisher")); + final BidRequest expectedBidRequest = givenBidRequest(impBuilder -> impBuilder.ext(expectedImpExt)); + assertThat(results.getValue()).hasSize(1).first() + .satisfies(request -> assertThat(request.getPayload()) + .isEqualTo(expectedBidRequest)) + .satisfies(request -> assertThat(request.getBody()) + .isEqualTo(jacksonMapper.encodeToBytes(expectedBidRequest))); + assertThat(results.getErrors()).isEmpty(); + } + + @Test + public void makeHttpRequestsShouldNotReturnTypeWhenRequestDoesNotHaveNeitherPlacementIdNorEndpointId() { + // given + final ObjectNode impExt = givenImpExt(extBuilder -> extBuilder.placementId(null).endpointId(null)); + final BidRequest bidRequest = givenBidRequest(impBuilder -> impBuilder.ext(impExt)); + + // when + final Result>> results = target.makeHttpRequests(bidRequest); + + // then + final ObjectNode expectedImpExt = givenImpExt(UnaryOperator.identity()); + final BidRequest expectedBidRequest = givenBidRequest(impBuilder -> impBuilder.ext(expectedImpExt)); + assertThat(results.getValue()).hasSize(1).first() + .satisfies(request -> assertThat(request.getPayload()) + .isEqualTo(expectedBidRequest)) + .satisfies(request -> assertThat(request.getBody()) + .isEqualTo(jacksonMapper.encodeToBytes(expectedBidRequest))); + assertThat(results.getErrors()).isEmpty(); + } + + @Test + public void makeHttpRequestsShouldReturn4RequestsWhenRequestHas4ValidImps() { + // given + final ObjectNode impExt1 = givenImpExt( + extBuilder -> extBuilder.placementId(null).endpointId(null)); + final ObjectNode impExt2 = givenImpExt( + extBuilder -> extBuilder.placementId("placementId").endpointId("endpointId")); + final ObjectNode impExt3 = givenImpExt( + extBuilder -> extBuilder.placementId("placementId").endpointId(null)); + final ObjectNode impExt4 = givenImpExt( + extBuilder -> extBuilder.placementId(null).endpointId("endpointId")); + final BidRequest bidRequest = givenBidRequest( + impBuilder -> impBuilder.id("impid1").ext(impExt1), + impBuilder -> impBuilder.id("impid2").ext(impExt2), + impBuilder -> impBuilder.id("impid3").ext(impExt3), + impBuilder -> impBuilder.id("impid4").ext(impExt4)); + + // when + final Result>> results = target.makeHttpRequests(bidRequest); + + // then + final ObjectNode expectedImpExt1 = givenImpExt( + extBuilder -> extBuilder.placementId(null).endpointId(null).type(null)); + final ObjectNode expectedImpExt2 = givenImpExt( + extBuilder -> extBuilder.placementId("placementId").type("publisher")); + final ObjectNode expectedImpExt3 = givenImpExt( + extBuilder -> extBuilder.placementId("placementId").type("publisher")); + final ObjectNode expectedImpExt4 = givenImpExt( + extBuilder -> extBuilder.endpointId("endpointId").type("network")); + final BidRequest expectedBidRequest1 = givenBidRequest( + impBuilder -> impBuilder.id("impid1").ext(expectedImpExt1)); + final BidRequest expectedBidRequest2 = givenBidRequest( + impBuilder -> impBuilder.id("impid2").ext(expectedImpExt2)); + final BidRequest expectedBidRequest3 = givenBidRequest( + impBuilder -> impBuilder.id("impid3").ext(expectedImpExt3)); + final BidRequest expectedBidRequest4 = givenBidRequest( + impBuilder -> impBuilder.id("impid4").ext(expectedImpExt4)); + assertThat(results.getValue()).hasSize(4) + .extracting(HttpRequest::getPayload, HttpRequest::getBody, HttpRequest::getImpIds) + .containsExactly( + tuple(expectedBidRequest1, jacksonMapper.encodeToBytes(expectedBidRequest1), Set.of("impid1")), + tuple(expectedBidRequest2, jacksonMapper.encodeToBytes(expectedBidRequest2), Set.of("impid2")), + tuple(expectedBidRequest3, jacksonMapper.encodeToBytes(expectedBidRequest3), Set.of("impid3")), + tuple(expectedBidRequest4, jacksonMapper.encodeToBytes(expectedBidRequest4), Set.of("impid4"))); + assertThat(results.getErrors()).isEmpty(); + } + + @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 makeHttpRequestsShouldReturnExpectedMethodAndUri() { + // given + final BidRequest bidRequest = givenBidRequest(identity()); + + // when + final Result>> result = target.makeHttpRequests(bidRequest); + + // then + assertThat(result.getValue()).hasSize(1).first() + .extracting(HttpRequest::getMethod, HttpRequest::getUri) + .containsExactly(HttpMethod.POST, "http://test-url.com"); + assertThat(result.getErrors()).isEmpty(); + } + + @Test + public void makeBidsShouldReturnErrorWhenResponseCanNotBeParsed() { + // given + final BidderCall httpCall = givenHttpCall(null, "invalid"); + + // when + final Result> actual = target.makeBids(httpCall, null); + + // then + assertThat(actual.getValue()).isEmpty(); + assertThat(actual.getErrors()).containsExactly(badServerResponse("Bad Server Response")); + } + + @Test + public void makeBidsShouldReturnErrorWhenResponseDoesNotHaveSeatBid() throws JsonProcessingException { + // given + final BidderCall httpCall = givenHttpCall(null, givenBidResponse()); + + // when + final Result> actual = target.makeBids(httpCall, null); + + // then + assertThat(actual.getValue()).isEmpty(); + assertThat(actual.getErrors()).containsExactly(badServerResponse("Empty SeatBid array")); + } + + @Test + public void makeBidsShouldReturnAllThreeBidsTypesSuccessfully() 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 nativeBid = Bid.builder().impid("3").mtype(4).build(); + + final BidderCall httpCall = givenHttpCall( + givenBidRequest(identity()), + givenBidResponse(bannerBid, videoBid, nativeBid)); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).containsExactly( + BidderBid.of(bannerBid, banner, null), + BidderBid.of(videoBid, video, null), + BidderBid.of(nativeBid, xNative, null)); + } + + @Test + public void makeBidsShouldReturnBannerBidSuccessfully() throws JsonProcessingException { + // given + final Bid bannerBid = Bid.builder().impid("1").mtype(1).build(); + + final BidderCall httpCall = givenHttpCall(givenBidRequest(identity()), 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 makeBidsShouldReturnVideoBidSuccessfully() throws JsonProcessingException { + // given + final Bid videoBid = Bid.builder().impid("2").mtype(2).build(); + + final BidderCall httpCall = givenHttpCall(givenBidRequest(identity()), 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 makeBidsShouldReturnNativeBidSuccessfully() throws JsonProcessingException { + // given + final Bid nativeBid = Bid.builder().impid("3").mtype(4).build(); + + final BidderCall httpCall = givenHttpCall(givenBidRequest(identity()), givenBidResponse(nativeBid)); + + // when + final Result> result = target.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).containsExactly(BidderBid.of(nativeBid, xNative, null)); + } + + private static BidRequest givenBidRequest(UnaryOperator... impCustomizers) { + return BidRequest.builder() + .imp(Arrays.stream(impCustomizers).map(PgamSspBidderTest::givenImp).toList()) + .build(); + } + + private static Imp givenImp(UnaryOperator impCustomizer) { + return impCustomizer.apply( + Imp.builder() + .id("imp_id") + .ext(givenImpExt(extImpBuilder -> extImpBuilder.placementId("placementId")))) + .build(); + } + + private static ObjectNode givenImpExt(UnaryOperator impExtBuilder) { + return mapper.valueToTree(ExtPrebid.of(null, impExtBuilder.apply(PgamSspImpExt.builder()).build())); + } + + private static String givenBidResponse(Bid... bids) throws JsonProcessingException { + return mapper.writeValueAsString(BidResponse.builder() + .seatbid(bids.length == 0 ? List.of() : List.of(SeatBid.builder().bid(List.of(bids)).build())) + .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/PgamSspTest.java b/src/test/java/org/prebid/server/it/PgamSspTest.java new file mode 100644 index 00000000000..36fab068ad3 --- /dev/null +++ b/src/test/java/org/prebid/server/it/PgamSspTest.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 java.util.List; + +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; + +@RunWith(SpringRunner.class) +public class PgamSspTest extends IntegrationTest { + + @Test + public void openrtb2AuctionShouldRespondWithBidsFromPgamSsp() throws IOException, JSONException { + // given + WIRE_MOCK_RULE.stubFor(post(urlPathEqualTo("/pgamssp-exchange")) + .withRequestBody(equalToJson(jsonFrom("openrtb2/pgamssp/test-pgamssp-bid-request.json"))) + .willReturn(aResponse().withBody(jsonFrom("openrtb2/pgamssp/test-pgamssp-bid-response.json")))); + + // when + final Response response = responseFor( + "openrtb2/pgamssp/test-auction-pgamssp-request.json", + Endpoint.openrtb2_auction + ); + + // then + assertJsonEquals("openrtb2/pgamssp/test-auction-pgamssp-response.json", response, List.of("pgamssp")); + } + +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/pgamssp/test-auction-pgamssp-request.json b/src/test/resources/org/prebid/server/it/openrtb2/pgamssp/test-auction-pgamssp-request.json new file mode 100644 index 00000000000..479b3f1c3c6 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/pgamssp/test-auction-pgamssp-request.json @@ -0,0 +1,23 @@ +{ + "id": "request_id", + "imp": [ + { + "id": "imp_id", + "banner": { + "w": 320, + "h": 250 + }, + "ext": { + "pgamssp": { + "placementId": "placementid" + } + } + } + ], + "tmax": 5000, + "regs": { + "ext": { + "gdpr": 0 + } + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/pgamssp/test-auction-pgamssp-response.json b/src/test/resources/org/prebid/server/it/openrtb2/pgamssp/test-auction-pgamssp-response.json new file mode 100644 index 00000000000..f2c7121be84 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/pgamssp/test-auction-pgamssp-response.json @@ -0,0 +1,36 @@ +{ + "id": "request_id", + "seatbid": [ + { + "bid": [ + { + "id": "bid_id", + "impid": "imp_id", + "price": 0.01, + "adid": "2068416", + "cid": "8048", + "crid": "24080", + "mtype": 1, + "ext": { + "prebid": { + "type": "banner" + }, + "origbidcpm": 0.01 + } + } + ], + "seat": "pgamssp", + "group": 0 + } + ], + "cur": "USD", + "ext": { + "responsetimemillis": { + "pgamssp": "{{ pgamssp.response_time_ms }}" + }, + "prebid": { + "auctiontimestamp": 0 + }, + "tmaxrequest": 5000 + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/pgamssp/test-pgamssp-bid-request.json b/src/test/resources/org/prebid/server/it/openrtb2/pgamssp/test-pgamssp-bid-request.json new file mode 100644 index 00000000000..2a1de62b354 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/pgamssp/test-pgamssp-bid-request.json @@ -0,0 +1,56 @@ +{ + "id": "request_id", + "imp": [ + { + "id": "imp_id", + "secure": 1, + "banner": { + "w": 320, + "h": 250 + }, + "ext" : { + "bidder" : { + "type" : "publisher", + "placementId" : "placementid" + } + } + } + ], + "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/pgamssp/test-pgamssp-bid-response.json b/src/test/resources/org/prebid/server/it/openrtb2/pgamssp/test-pgamssp-bid-response.json new file mode 100644 index 00000000000..47d4f8718ea --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/pgamssp/test-pgamssp-bid-response.json @@ -0,0 +1,19 @@ +{ + "id": "tid", + "seatbid": [ + { + "bid": [ + { + "crid": "24080", + "adid": "2068416", + "price": 0.01, + "id": "bid_id", + "impid": "imp_id", + "cid": "8048", + "mtype": 1 + } + ], + "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 2ca4d48981f..b9cd0f2bc46 100644 --- a/src/test/resources/org/prebid/server/it/test-application.properties +++ b/src/test/resources/org/prebid/server/it/test-application.properties @@ -267,6 +267,8 @@ adapters.outbrain.enabled=true adapters.outbrain.endpoint=http://localhost:8090/outbrain-exchange adapters.pangle.enabled=true adapters.pangle.endpoint=http://localhost:8090/pangle-exchange +adapters.pgamssp.enabled=true +adapters.pgamssp.endpoint=http://localhost:8090/pgamssp-exchange adapters.preciso.enabled=true adapters.preciso.endpoint=http://localhost:8090/preciso-exchange adapters.pubmatic.enabled=true