From 574653c94a1d62257f89548b6e009cccdc559816 Mon Sep 17 00:00:00 2001 From: apavlyuk Date: Tue, 1 Sep 2020 12:10:00 +0300 Subject: [PATCH 1/3] Add LogicadBidder and tests --- .../server/bidder/logicad/LogicadBidder.java | 154 ++++++++++++ .../ext/request/logicad/ExtImpLogicad.java | 14 ++ .../config/bidder/LogicadConfiguration.java | 56 +++++ src/main/resources/bidder-config/logicad.yaml | 23 ++ .../static/bidder-params/logicad.json | 13 + .../bidder/logicad/LogicadBidderTest.java | 234 ++++++++++++++++++ 6 files changed, 494 insertions(+) create mode 100644 src/main/java/org/prebid/server/bidder/logicad/LogicadBidder.java create mode 100644 src/main/java/org/prebid/server/proto/openrtb/ext/request/logicad/ExtImpLogicad.java create mode 100644 src/main/java/org/prebid/server/spring/config/bidder/LogicadConfiguration.java create mode 100644 src/main/resources/bidder-config/logicad.yaml create mode 100644 src/main/resources/static/bidder-params/logicad.json create mode 100644 src/test/java/org/prebid/server/bidder/logicad/LogicadBidderTest.java diff --git a/src/main/java/org/prebid/server/bidder/logicad/LogicadBidder.java b/src/main/java/org/prebid/server/bidder/logicad/LogicadBidder.java new file mode 100644 index 00000000000..29b90876fe9 --- /dev/null +++ b/src/main/java/org/prebid/server/bidder/logicad/LogicadBidder.java @@ -0,0 +1,154 @@ +package org.prebid.server.bidder.logicad; + +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.BidResponse; +import com.iab.openrtb.response.SeatBid; +import io.vertx.core.http.HttpMethod; +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.BidderError; +import org.prebid.server.bidder.model.HttpCall; +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.logicad.ExtImpLogicad; +import org.prebid.server.proto.openrtb.ext.response.BidType; +import org.prebid.server.util.HttpUtil; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; + +public class LogicadBidder implements Bidder { + + private static final TypeReference> LOGICAD_EXT_TYPE_REFERENCE = + new TypeReference>() { + }; + + private static final String DEFAULT_BID_CURRENCY = "USD"; + + private final String endpointUrl; + private final JacksonMapper mapper; + + public LogicadBidder(String endpointUrl, JacksonMapper mapper) { + this.endpointUrl = HttpUtil.validateUrl(Objects.requireNonNull(endpointUrl)); + this.mapper = Objects.requireNonNull(mapper); + } + + @Override + public Result>> makeHttpRequests(BidRequest request) { + final List errors = new ArrayList<>(); + List> httpRequests = new ArrayList<>(); + try { + final Map> impToExtImp = getImpToExtImp(request, errors); + httpRequests.addAll(buildAdapterRequests(request, impToExtImp)); + } catch (PreBidException e) { + return Result.of(Collections.emptyList(), errors); + } + + return Result.of(httpRequests, errors); + } + + private Map> getImpToExtImp(BidRequest request, List errors) { + final Map> extToListOfUpdatedImp = new HashMap<>(); + for (Imp imp : request.getImp()) { + try { + final ExtImpLogicad extImpLogicad = parseAndValidateImpExt(imp); + final Imp updatedImp = updateImp(imp, extImpLogicad.getTid()); + + extToListOfUpdatedImp.putIfAbsent(extImpLogicad, new ArrayList<>()); + extToListOfUpdatedImp.get(extImpLogicad).add(updatedImp); + } catch (PreBidException e) { + errors.add(BidderError.badInput(e.getMessage())); + } + } + + if (extToListOfUpdatedImp.isEmpty()) { + throw new PreBidException("No appropriate impressions"); + } + + return extToListOfUpdatedImp; + } + + private ExtImpLogicad parseAndValidateImpExt(Imp imp) { + final ExtImpLogicad bidder; + try { + bidder = mapper.mapper().convertValue(imp.getExt(), LOGICAD_EXT_TYPE_REFERENCE).getBidder(); + } catch (IllegalArgumentException e) { + throw new PreBidException(e.getMessage(), e); + } + + if (StringUtils.isBlank(bidder.getTid())) { + throw new PreBidException("No tid value provided"); + } + + return bidder; + } + + private static Imp updateImp(Imp imp, String tid) { + return imp.toBuilder().tagid(tid).ext(null).build(); + } + + private List> buildAdapterRequests(BidRequest bidRequest, + Map> impExtToListOfImps) { + final List> httpRequests = new ArrayList<>(); + + for (Map.Entry> impExtAndListOfImps : impExtToListOfImps.entrySet()) { + final BidRequest updatedBidRequest = BidRequest.builder().imp(impExtAndListOfImps.getValue()).build(); + final String body = mapper.encode(updatedBidRequest); + final HttpRequest createdBidRequest = HttpRequest.builder() + .method(HttpMethod.POST) + .uri(endpointUrl) + .body(body) + .headers(HttpUtil.headers()) + .payload(bidRequest) + .build(); + + httpRequests.add(createdBidRequest); + } + + return httpRequests; + } + + @Override + public Result> makeBids(HttpCall httpCall, BidRequest bidRequest) { + try { + final BidResponse bidResponse = mapper.decodeValue(httpCall.getResponse().getBody(), BidResponse.class); + return Result.of(extractBids(bidResponse), Collections.emptyList()); + } catch (DecodeException | PreBidException e) { + return Result.emptyWithError(BidderError.badServerResponse(e.getMessage())); + } + } + + private static List extractBids(BidResponse bidResponse) { + if (bidResponse == null || bidResponse.getSeatbid() == null) { + return Collections.emptyList(); + } + return bidsFromResponse(bidResponse); + } + + private static List bidsFromResponse(BidResponse bidResponse) { + return bidResponse.getSeatbid().stream() + .map(SeatBid::getBid) + .flatMap(Collection::stream) + .map(bid -> BidderBid.of(bid, BidType.banner, DEFAULT_BID_CURRENCY)) + .collect(Collectors.toList()); + } + + @Override + public Map extractTargeting(ObjectNode ext) { + return Collections.emptyMap(); + } +} diff --git a/src/main/java/org/prebid/server/proto/openrtb/ext/request/logicad/ExtImpLogicad.java b/src/main/java/org/prebid/server/proto/openrtb/ext/request/logicad/ExtImpLogicad.java new file mode 100644 index 00000000000..9ebc3cb2238 --- /dev/null +++ b/src/main/java/org/prebid/server/proto/openrtb/ext/request/logicad/ExtImpLogicad.java @@ -0,0 +1,14 @@ +package org.prebid.server.proto.openrtb.ext.request.logicad; + +import lombok.AllArgsConstructor; +import lombok.Value; + +/** + * Defines the contract for bidRequest.imp[i].ext.logicad + */ +@AllArgsConstructor(staticName = "of") +@Value +public class ExtImpLogicad { + + String tid; +} diff --git a/src/main/java/org/prebid/server/spring/config/bidder/LogicadConfiguration.java b/src/main/java/org/prebid/server/spring/config/bidder/LogicadConfiguration.java new file mode 100644 index 00000000000..4c4958d9161 --- /dev/null +++ b/src/main/java/org/prebid/server/spring/config/bidder/LogicadConfiguration.java @@ -0,0 +1,56 @@ +package org.prebid.server.spring.config.bidder; + +import org.prebid.server.bidder.BidderDeps; +import org.prebid.server.bidder.logicad.LogicadBidder; +import org.prebid.server.json.JacksonMapper; +import org.prebid.server.spring.config.bidder.model.BidderConfigurationProperties; +import org.prebid.server.spring.config.bidder.model.UsersyncConfigurationProperties; +import org.prebid.server.spring.config.bidder.util.BidderDepsAssembler; +import org.prebid.server.spring.config.bidder.util.BidderInfoCreator; +import org.prebid.server.spring.config.bidder.util.UsersyncerCreator; +import org.prebid.server.spring.env.YamlPropertySourceFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +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/logicad.yaml", factory = YamlPropertySourceFactory.class) +public class LogicadConfiguration { + + private static final String BIDDER_NAME = "logicad"; + + @Value("${external-url}") + @NotBlank + private String externalUrl; + + @Autowired + private JacksonMapper mapper; + + @Autowired + @Qualifier("admixerConfigurationProperties") + private BidderConfigurationProperties configProperties; + + @Bean("logicadConfigurationProperties") + @ConfigurationProperties("adapters.logicad") + BidderConfigurationProperties configurationProperties() { + return new BidderConfigurationProperties(); + } + + @Bean + BidderDeps logicadBidderDeps() { + final UsersyncConfigurationProperties usersync = configProperties.getUsersync(); + + return BidderDepsAssembler.forBidder(BIDDER_NAME) + .withConfig(configProperties) + .bidderInfo(BidderInfoCreator.create(configProperties)) + .usersyncerCreator(UsersyncerCreator.create(usersync, externalUrl)) + .bidderCreator(() -> new LogicadBidder(configProperties.getEndpoint(), mapper)) + .assemble(); + } +} diff --git a/src/main/resources/bidder-config/logicad.yaml b/src/main/resources/bidder-config/logicad.yaml new file mode 100644 index 00000000000..813d79eefe0 --- /dev/null +++ b/src/main/resources/bidder-config/logicad.yaml @@ -0,0 +1,23 @@ +adapters: + logicad: + enabled: false + endpoint: https://pbs.ladsp.com/adrequest/prebidserver + pbs-enforces-gdpr: true + pbs-enforces-ccpa: true + modifying-vast-xml-allowed: true + deprecated-names: + aliases: + meta-info: + maintainer-email: prebid@so-netmedia.jp + app-media-types: + - banner + site-media-types: + - banner + supported-vendors: + vendor-id: 14 + usersync: + url: https://cr-p31.ladsp.jp/cookiesender/31?r=true&gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&ru= + redirect-url: /setuid?bidder=logicad&gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&uid=$UID + cookie-family-name: logicad + type: redirect + support-cors: false diff --git a/src/main/resources/static/bidder-params/logicad.json b/src/main/resources/static/bidder-params/logicad.json new file mode 100644 index 00000000000..2a892f91266 --- /dev/null +++ b/src/main/resources/static/bidder-params/logicad.json @@ -0,0 +1,13 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "title": "Logicad Adapter Params", + "description": "A schema which validates params accepted by the Logicad adapter", + "type": "object", + "properties": { + "tid": { + "type": "string", + "description": "Logicad for Publishers placement ID" + } + }, + "required": ["tid"] +} diff --git a/src/test/java/org/prebid/server/bidder/logicad/LogicadBidderTest.java b/src/test/java/org/prebid/server/bidder/logicad/LogicadBidderTest.java new file mode 100644 index 00000000000..d51bcc3e86e --- /dev/null +++ b/src/test/java/org/prebid/server/bidder/logicad/LogicadBidderTest.java @@ -0,0 +1,234 @@ +package org.prebid.server.bidder.logicad; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.iab.openrtb.request.Banner; +import com.iab.openrtb.request.BidRequest; +import com.iab.openrtb.request.Imp; +import com.iab.openrtb.response.Bid; +import com.iab.openrtb.response.BidResponse; +import com.iab.openrtb.response.SeatBid; +import org.assertj.core.api.Assertions; +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.BidderError; +import org.prebid.server.bidder.model.HttpCall; +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.logicad.ExtImpLogicad; + +import java.util.List; +import java.util.function.Function; + +import static java.util.Collections.emptyMap; +import static java.util.Collections.singletonList; +import static java.util.function.Function.identity; +import static org.assertj.core.api.Assertions.assertThat; +import static org.prebid.server.proto.openrtb.ext.response.BidType.banner; + +public class LogicadBidderTest extends VertxTest { + + private static final String ENDPOINT_URL = "https://test.endpoint.com/"; + + private LogicadBidder logicadBidder; + + @Before + public void setUp() { + logicadBidder = new LogicadBidder(ENDPOINT_URL, jacksonMapper); + } + + @Test + public void creationShouldFailOnInvalidEndpointUrl() { + Assertions.assertThatIllegalArgumentException().isThrownBy(() -> + new LogicadBidder("invalid_url", jacksonMapper)); + } + + @Test + public void makeHttpRequestsShouldReturnErrorWhenImpExtCouldNotBeParsed() { + // given + final BidRequest bidRequest = BidRequest.builder() + .imp(singletonList(Imp.builder() + .ext(mapper.valueToTree(ExtPrebid.of(null, mapper.createArrayNode()))) + .build())) + .build(); + + // when + final Result>> result = logicadBidder.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).hasSize(1); + assertThat(result.getErrors().get(0).getMessage()).startsWith("Cannot deserialize instance"); + assertThat(result.getValue()).isEmpty(); + } + + @Test + public void makeHttpRequestsShouldReturnErrorWhenExtTidIsNull() { + // given + final BidRequest bidRequest = givenBidRequest(identity(), ExtImpLogicad.of(null)); + + // when + final Result>> result = logicadBidder.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).hasSize(1) + .containsOnly(BidderError.badInput("No tid value provided")); + assertThat(result.getValue()).isEmpty(); + } + + @Test + public void makeHttpRequestsShouldReturnErrorWhenExtPubIdIsBlank() { + // given + final BidRequest bidRequest = givenBidRequest(identity(), ExtImpLogicad.of(" ")); + + // when + final Result>> result = logicadBidder.makeHttpRequests(bidRequest); + + // then + assertThat(result.getErrors()).hasSize(1) + .containsOnly(BidderError.badInput("No tid value provided")); + assertThat(result.getValue()).isEmpty(); + } + + @Test + public void makeHttpRequestShouldSetTagidFromExtTid() { + // given + final BidRequest bidRequest = givenBidRequest( + impBuilder -> impBuilder + .tagid("was") + .banner(Banner.builder().build()), + ExtImpLogicad.of("tid")); + + // when + final Result>> result = logicadBidder.makeHttpRequests(bidRequest); + + // then + final Imp expectedImp = Imp.builder() + .banner(Banner.builder().build()) + .tagid("tid") + .build(); + + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).hasSize(1) + .extracting(httpRequest -> mapper.readValue(httpRequest.getBody(), BidRequest.class)) + .flatExtracting(BidRequest::getImp) + .containsOnly(expectedImp); + } + + @Test + public void makeBidsShouldReturnErrorWhenResponseBodyCouldNotBeParsed() { + // given + final HttpCall httpCall = givenHttpCall(null, "invalid"); + + // when + final Result> result = logicadBidder.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).hasSize(1); + assertThat(result.getErrors().get(0).getMessage()).startsWith("Failed to decode: Unrecognized token"); + assertThat(result.getErrors().get(0).getType()).isEqualTo(BidderError.Type.bad_server_response); + assertThat(result.getValue()).isEmpty(); + } + + @Test + public void makeBidsShouldReturnEmptyListWhenBidResponseIsNull() throws JsonProcessingException { + // given + final HttpCall httpCall = givenHttpCall(null, + mapper.writeValueAsString(null)); + + // when + final Result> result = logicadBidder.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).isEmpty(); + } + + @Test + public void makeBidsShouldReturnEmptyListWhenBidResponseSeatBidIsNull() throws JsonProcessingException { + // given + final HttpCall httpCall = givenHttpCall(null, + mapper.writeValueAsString(BidResponse.builder().build())); + + // when + final Result> result = logicadBidder.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()).isEmpty(); + } + + @Test + public void makeBidsShouldReturnBannerBid() throws JsonProcessingException { + // given + final HttpCall httpCall = givenHttpCall( + BidRequest.builder() + .imp(singletonList(Imp.builder().id("123").build())) + .build(), + mapper.writeValueAsString( + givenBidResponse(bidBuilder -> bidBuilder.impid("123")))); + + // when + final Result> result = logicadBidder.makeBids(httpCall, null); + + // then + assertThat(result.getErrors()).isEmpty(); + assertThat(result.getValue()) + .containsOnly(BidderBid.of(Bid.builder().impid("123").build(), banner, "USD")); + } + + @Test + public void extractTargetingShouldReturnEmptyMap() { + assertThat(logicadBidder.extractTargeting(mapper.createObjectNode())).isEqualTo(emptyMap()); + } + + private static BidRequest givenBidRequest( + Function bidRequestCustomizer, + Function impCustomizer, + ExtImpLogicad extImpLogicad) { + + return bidRequestCustomizer.apply(BidRequest.builder() + .imp(singletonList(givenImp(impCustomizer, extImpLogicad)))) + .build(); + } + + private static BidRequest givenBidRequest(Function impCustomizer) { + return givenBidRequest(identity(), impCustomizer, ExtImpLogicad.of("tid")); + } + + private static BidRequest givenBidRequest(Function impCustomizer, + ExtImpLogicad extImpLogicad) { + return givenBidRequest(identity(), impCustomizer, extImpLogicad); + } + + private static Imp givenImp(Function impCustomizer, + ExtImpLogicad extImpLogicad) { + return impCustomizer.apply(Imp.builder() + .ext(mapper.valueToTree( + ExtPrebid.of(null, extImpLogicad)))) + .build(); + } + + private static Imp givenImp(Function impCustomizer) { + return impCustomizer.apply(Imp.builder() + .ext(mapper.valueToTree(ExtPrebid.of(null, ExtImpLogicad.of("tid"))))) + .build(); + } + + private static BidResponse givenBidResponse(Function bidCustomizer) { + return BidResponse.builder() + .seatbid(singletonList(SeatBid.builder() + .bid(singletonList(bidCustomizer.apply(Bid.builder()).build())) + .build())) + .build(); + } + + private static HttpCall givenHttpCall(BidRequest bidRequest, String body) { + return HttpCall.success( + HttpRequest.builder().payload(bidRequest).build(), + HttpResponse.of(200, null, body), + null); + } +} From ad6a48880d1490f3d6af9f61d1f48397567301a6 Mon Sep 17 00:00:00 2001 From: apavlyuk Date: Tue, 1 Sep 2020 12:46:06 +0300 Subject: [PATCH 2/3] Add integration test --- .../server/bidder/logicad/LogicadBidder.java | 2 +- .../config/bidder/LogicadConfiguration.java | 2 +- .../org/prebid/server/it/LogicadTest.java | 60 +++++++++++++ .../logicad/test-auction-logicad-request.json | 85 ++++++++++++++++++ .../test-auction-logicad-response.json | 53 +++++++++++ .../logicad/test-cache-logicad-request.json | 15 ++++ .../logicad/test-cache-logicad-response.json | 7 ++ .../logicad/test-logicad-bid-request.json | 90 +++++++++++++++++++ .../logicad/test-logicad-bid-response.json | 18 ++++ .../server/it/test-application.properties | 4 + 10 files changed, 334 insertions(+), 2 deletions(-) create mode 100644 src/test/java/org/prebid/server/it/LogicadTest.java create mode 100644 src/test/resources/org/prebid/server/it/openrtb2/logicad/test-auction-logicad-request.json create mode 100644 src/test/resources/org/prebid/server/it/openrtb2/logicad/test-auction-logicad-response.json create mode 100644 src/test/resources/org/prebid/server/it/openrtb2/logicad/test-cache-logicad-request.json create mode 100644 src/test/resources/org/prebid/server/it/openrtb2/logicad/test-cache-logicad-response.json create mode 100644 src/test/resources/org/prebid/server/it/openrtb2/logicad/test-logicad-bid-request.json create mode 100644 src/test/resources/org/prebid/server/it/openrtb2/logicad/test-logicad-bid-response.json diff --git a/src/main/java/org/prebid/server/bidder/logicad/LogicadBidder.java b/src/main/java/org/prebid/server/bidder/logicad/LogicadBidder.java index 29b90876fe9..4a30be4c4bb 100644 --- a/src/main/java/org/prebid/server/bidder/logicad/LogicadBidder.java +++ b/src/main/java/org/prebid/server/bidder/logicad/LogicadBidder.java @@ -106,7 +106,7 @@ private List> buildAdapterRequests(BidRequest bidRequest final List> httpRequests = new ArrayList<>(); for (Map.Entry> impExtAndListOfImps : impExtToListOfImps.entrySet()) { - final BidRequest updatedBidRequest = BidRequest.builder().imp(impExtAndListOfImps.getValue()).build(); + final BidRequest updatedBidRequest = bidRequest.toBuilder().imp(impExtAndListOfImps.getValue()).build(); final String body = mapper.encode(updatedBidRequest); final HttpRequest createdBidRequest = HttpRequest.builder() .method(HttpMethod.POST) diff --git a/src/main/java/org/prebid/server/spring/config/bidder/LogicadConfiguration.java b/src/main/java/org/prebid/server/spring/config/bidder/LogicadConfiguration.java index 4c4958d9161..adaf6cfe4d4 100644 --- a/src/main/java/org/prebid/server/spring/config/bidder/LogicadConfiguration.java +++ b/src/main/java/org/prebid/server/spring/config/bidder/LogicadConfiguration.java @@ -33,7 +33,7 @@ public class LogicadConfiguration { private JacksonMapper mapper; @Autowired - @Qualifier("admixerConfigurationProperties") + @Qualifier("logicadConfigurationProperties") private BidderConfigurationProperties configProperties; @Bean("logicadConfigurationProperties") diff --git a/src/test/java/org/prebid/server/it/LogicadTest.java b/src/test/java/org/prebid/server/it/LogicadTest.java new file mode 100644 index 00000000000..f1ae373aa22 --- /dev/null +++ b/src/test/java/org/prebid/server/it/LogicadTest.java @@ -0,0 +1,60 @@ +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.skyscreamer.jsonassert.JSONAssert; +import org.skyscreamer.jsonassert.JSONCompareMode; +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.equalToIgnoreCase; +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 io.restassured.RestAssured.given; +import static java.util.Collections.singletonList; + +@RunWith(SpringRunner.class) +public class LogicadTest extends IntegrationTest { + + @Test + public void openrtb2AuctionShouldRespondWithBidsFromLogicad() throws IOException, JSONException { + // given + // Logicad bid response for imp + WIRE_MOCK_RULE.stubFor(post(urlPathEqualTo("/logicad-exchange")) + .withHeader("Accept", equalTo("application/json")) + .withHeader("Content-Type", equalToIgnoreCase("application/json;charset=UTF-8")) + .withRequestBody(equalToJson(jsonFrom("openrtb2/logicad/test-logicad-bid-request.json"))) + .willReturn(aResponse().withBody( + jsonFrom("openrtb2/logicad/test-logicad-bid-response.json")))); + + // pre-bid cache + WIRE_MOCK_RULE.stubFor(post(urlPathEqualTo("/cache")) + .withRequestBody(equalToJson(jsonFrom("openrtb2/logicad/test-cache-logicad-request.json"))) + .willReturn(aResponse().withBody( + jsonFrom("openrtb2/logicad/test-cache-logicad-response.json")))); + + // when + final Response response = given(SPEC) + .header("Referer", "http://www.example.com") + .header("X-Forwarded-For", "193.168.244.1") + .header("User-Agent", "userAgent") + .header("Origin", "http://www.example.com") + // this uids cookie value stands for {"uids":{"logicad":"LC-UID"}} + .cookie("uids", "eyJ1aWRzIjp7ImxvZ2ljYWQiOiJMQy1VSUQifX0=") + .body(jsonFrom("openrtb2/logicad/test-auction-logicad-request.json")) + .post("/openrtb2/auction"); + + // then + final String expectedAuctionResponse = openrtbAuctionResponseFrom( + "openrtb2/logicad/test-auction-logicad-response.json", + response, singletonList("logicad")); + + JSONAssert.assertEquals(expectedAuctionResponse, response.asString(), JSONCompareMode.NON_EXTENSIBLE); + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/logicad/test-auction-logicad-request.json b/src/test/resources/org/prebid/server/it/openrtb2/logicad/test-auction-logicad-request.json new file mode 100644 index 00000000000..af7b053eeae --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/logicad/test-auction-logicad-request.json @@ -0,0 +1,85 @@ +{ + "id": "tid", + "imp": [ + { + "id": "testimpid", + "banner": { + "format": [ + { + "w": 320, + "h": 250 + }, + { + "w": 320, + "h": 300 + } + ], + "w": 320, + "h": 250 + }, + "ext": { + "logicad": { + "tid": "testtid" + } + }, + "tagid": "impId021" + } + ], + "device": { + "pxratio": 4.2, + "dnt": 2, + "language": "en", + "ifa": "ifaId" + }, + "site": { + "publisher": { + "id": "publisherId" + } + }, + "at": 1, + "tmax": 5000, + "cur": [ + "USD" + ], + "source": { + "fd": 1, + "tid": "tid" + }, + "ext": { + "prebid": { + "targeting": { + "pricegranularity": { + "precision": 2, + "ranges": [ + { + "max": 20, + "increment": 0.1 + } + ] + } + }, + "cache": { + "bids": {}, + "vastxml": { + "ttlseconds": 120 + } + }, + "auctiontimestamp": 1000 + } + }, + "user": { + "ext": { + "consent": "consentValue", + "digitrust": { + "id": "id", + "keyv": 123, + "pref": 0 + } + } + }, + "regs": { + "ext": { + "gdpr": 0 + } + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/logicad/test-auction-logicad-response.json b/src/test/resources/org/prebid/server/it/openrtb2/logicad/test-auction-logicad-response.json new file mode 100644 index 00000000000..6b5285ded93 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/logicad/test-auction-logicad-response.json @@ -0,0 +1,53 @@ +{ + "id": "tid", + "seatbid": [ + { + "bid": [ + { + "id": "testid", + "impid": "testimpid", + "price": 0.01, + "adid": "2068416", + "cid": "8048", + "crid": "24080", + "ext": { + "prebid": { + "type": "banner", + "targeting": { + "hb_pb": "0.00", + "hb_cache_id_logicad": "3c0769d8-0dd9-465c-8bf3-f570605ba698", + "hb_bidder_logicad": "logicad", + "hb_bidder": "logicad", + "hb_cache_id": "3c0769d8-0dd9-465c-8bf3-f570605ba698", + "hb_pb_logicad": "0.00", + "hb_cache_host": "{{ cache.host }}", + "hb_cache_host_logicad": "{{ cache.host }}", + "hb_cache_path": "/cache", + "hb_cache_path_logicad": "/cache" + }, + "cache": { + "bids": { + "url": "{{ cache.resource_url }}3c0769d8-0dd9-465c-8bf3-f570605ba698", + "cacheId": "3c0769d8-0dd9-465c-8bf3-f570605ba698" + } + } + } + } + } + ], + "seat": "logicad", + "group": 0 + } + ], + "cur": "USD", + "ext": { + "responsetimemillis": { + "logicad": "{{ logicad.response_time_ms }}", + "cache": "{{ cache.response_time_ms }}" + }, + "prebid": { + "auctiontimestamp": 1000 + }, + "tmaxrequest": 5000 + } +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/logicad/test-cache-logicad-request.json b/src/test/resources/org/prebid/server/it/openrtb2/logicad/test-cache-logicad-request.json new file mode 100644 index 00000000000..ca8e3ab2f6a --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/logicad/test-cache-logicad-request.json @@ -0,0 +1,15 @@ +{ + "puts": [ + { + "type": "json", + "value": { + "crid": "24080", + "adid": "2068416", + "price": 0.01, + "id": "testid", + "impid": "testimpid", + "cid": "8048" + } + } + ] +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/logicad/test-cache-logicad-response.json b/src/test/resources/org/prebid/server/it/openrtb2/logicad/test-cache-logicad-response.json new file mode 100644 index 00000000000..c0100536be1 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/logicad/test-cache-logicad-response.json @@ -0,0 +1,7 @@ +{ + "responses": [ + { + "uuid": "3c0769d8-0dd9-465c-8bf3-f570605ba698" + } + ] +} diff --git a/src/test/resources/org/prebid/server/it/openrtb2/logicad/test-logicad-bid-request.json b/src/test/resources/org/prebid/server/it/openrtb2/logicad/test-logicad-bid-request.json new file mode 100644 index 00000000000..599b2df0da7 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/logicad/test-logicad-bid-request.json @@ -0,0 +1,90 @@ +{ + "id": "tid", + "imp": [ + { + "id": "testimpid", + "banner": { + "format": [ + { + "w": 320, + "h": 250 + }, + { + "w": 320, + "h": 300 + } + ], + "w": 320, + "h": 250 + }, + "tagid": "testtid" + } + ], + "site": { + "domain": "example.com", + "page": "http://www.example.com", + "publisher": { + "id": "publisherId" + }, + "ext": { + "amp": 0 + } + }, + "device": { + "ua": "userAgent", + "dnt": 2, + "ip": "193.168.244.1", + "pxratio": 4.2, + "language": "en", + "ifa": "ifaId" + }, + "user": { + "buyeruid": "LC-UID", + "ext": { + "consent": "consentValue", + "digitrust": { + "id": "id", + "keyv": 123, + "pref": 0 + } + } + }, + "at": 1, + "tmax": 5000, + "cur": [ + "USD" + ], + "source": { + "fd": 1, + "tid": "tid" + }, + "regs": { + "ext": { + "gdpr": 0 + } + }, + "ext": { + "prebid": { + "targeting": { + "pricegranularity": { + "precision": 2, + "ranges": [ + { + "max": 20, + "increment": 0.1 + } + ] + }, + "includewinners": true, + "includebidderkeys": true + }, + "cache": { + "bids": {}, + "vastxml": { + "ttlseconds": 120 + } + }, + "auctiontimestamp": 1000 + } + } +} \ No newline at end of file diff --git a/src/test/resources/org/prebid/server/it/openrtb2/logicad/test-logicad-bid-response.json b/src/test/resources/org/prebid/server/it/openrtb2/logicad/test-logicad-bid-response.json new file mode 100644 index 00000000000..ca4e6ee1db4 --- /dev/null +++ b/src/test/resources/org/prebid/server/it/openrtb2/logicad/test-logicad-bid-response.json @@ -0,0 +1,18 @@ +{ + "id": "tid", + "seatbid": [ + { + "bid": [ + { + "crid": "24080", + "adid": "2068416", + "price": 0.01, + "id": "testid", + "impid": "testimpid", + "cid": "8048" + } + ], + "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 13c679079f7..d8e18b6e3e8 100644 --- a/src/test/resources/org/prebid/server/it/test-application.properties +++ b/src/test/resources/org/prebid/server/it/test-application.properties @@ -130,6 +130,10 @@ adapters.lockerdome.enabled=true adapters.lockerdome.endpoint=http://localhost:8090/lockerdome-exchange adapters.lockerdome.pbs-enforces-gdpr=true adapters.lockerdome.usersync.url=//lockerdome-usersync +adapters.logicad.enabled=true +adapters.logicad.endpoint=http://localhost:8090/logicad-exchange +adapters.logicad.pbs-enforces-gdpr=true +adapters.logicad.usersync.url=//logicad-usersync adapters.marsmedia.enabled=true adapters.marsmedia.endpoint=http://localhost:8090/marsmedia-exchange adapters.marsmedia.pbs-enforces-gdpr=true From fbc01f9b6d63209f4bf2252cf5433268a51f2868 Mon Sep 17 00:00:00 2001 From: rpanchyk Date: Tue, 29 Sep 2020 16:27:10 +0300 Subject: [PATCH 3/3] Clean code --- .../server/bidder/logicad/LogicadBidder.java | 38 ++++++++++--------- src/main/resources/bidder-config/logicad.yaml | 2 +- .../bidder/logicad/LogicadBidderTest.java | 13 +------ 3 files changed, 23 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/prebid/server/bidder/logicad/LogicadBidder.java b/src/main/java/org/prebid/server/bidder/logicad/LogicadBidder.java index 4a30be4c4bb..e53b13bec1f 100644 --- a/src/main/java/org/prebid/server/bidder/logicad/LogicadBidder.java +++ b/src/main/java/org/prebid/server/bidder/logicad/LogicadBidder.java @@ -31,14 +31,15 @@ import java.util.Objects; import java.util.stream.Collectors; +/** + * Logicad {@link Bidder} implementation. + */ public class LogicadBidder implements Bidder { private static final TypeReference> LOGICAD_EXT_TYPE_REFERENCE = new TypeReference>() { }; - private static final String DEFAULT_BID_CURRENCY = "USD"; - private final String endpointUrl; private final JacksonMapper mapper; @@ -50,9 +51,9 @@ public LogicadBidder(String endpointUrl, JacksonMapper mapper) { @Override public Result>> makeHttpRequests(BidRequest request) { final List errors = new ArrayList<>(); - List> httpRequests = new ArrayList<>(); + final List> httpRequests = new ArrayList<>(); try { - final Map> impToExtImp = getImpToExtImp(request, errors); + final Map> impToExtImp = getExtImpToImps(request, errors); httpRequests.addAll(buildAdapterRequests(request, impToExtImp)); } catch (PreBidException e) { return Result.of(Collections.emptyList(), errors); @@ -61,40 +62,40 @@ public Result>> makeHttpRequests(BidRequest request return Result.of(httpRequests, errors); } - private Map> getImpToExtImp(BidRequest request, List errors) { - final Map> extToListOfUpdatedImp = new HashMap<>(); + private Map> getExtImpToImps(BidRequest request, List errors) { + final Map> result = new HashMap<>(); for (Imp imp : request.getImp()) { try { final ExtImpLogicad extImpLogicad = parseAndValidateImpExt(imp); final Imp updatedImp = updateImp(imp, extImpLogicad.getTid()); - extToListOfUpdatedImp.putIfAbsent(extImpLogicad, new ArrayList<>()); - extToListOfUpdatedImp.get(extImpLogicad).add(updatedImp); + result.putIfAbsent(extImpLogicad, new ArrayList<>()); + result.get(extImpLogicad).add(updatedImp); } catch (PreBidException e) { errors.add(BidderError.badInput(e.getMessage())); } } - if (extToListOfUpdatedImp.isEmpty()) { + if (result.isEmpty()) { throw new PreBidException("No appropriate impressions"); } - return extToListOfUpdatedImp; + return result; } private ExtImpLogicad parseAndValidateImpExt(Imp imp) { - final ExtImpLogicad bidder; + final ExtImpLogicad extImp; try { - bidder = mapper.mapper().convertValue(imp.getExt(), LOGICAD_EXT_TYPE_REFERENCE).getBidder(); + extImp = mapper.mapper().convertValue(imp.getExt(), LOGICAD_EXT_TYPE_REFERENCE).getBidder(); } catch (IllegalArgumentException e) { throw new PreBidException(e.getMessage(), e); } - if (StringUtils.isBlank(bidder.getTid())) { + if (StringUtils.isBlank(extImp.getTid())) { throw new PreBidException("No tid value provided"); } - return bidder; + return extImp; } private static Imp updateImp(Imp imp, String tid) { @@ -102,12 +103,13 @@ private static Imp updateImp(Imp imp, String tid) { } private List> buildAdapterRequests(BidRequest bidRequest, - Map> impExtToListOfImps) { + Map> extImpToImps) { final List> httpRequests = new ArrayList<>(); - for (Map.Entry> impExtAndListOfImps : impExtToListOfImps.entrySet()) { - final BidRequest updatedBidRequest = bidRequest.toBuilder().imp(impExtAndListOfImps.getValue()).build(); + for (Map.Entry> entry : extImpToImps.entrySet()) { + final BidRequest updatedBidRequest = bidRequest.toBuilder().imp(entry.getValue()).build(); final String body = mapper.encode(updatedBidRequest); + final HttpRequest createdBidRequest = HttpRequest.builder() .method(HttpMethod.POST) .uri(endpointUrl) @@ -143,7 +145,7 @@ private static List bidsFromResponse(BidResponse bidResponse) { return bidResponse.getSeatbid().stream() .map(SeatBid::getBid) .flatMap(Collection::stream) - .map(bid -> BidderBid.of(bid, BidType.banner, DEFAULT_BID_CURRENCY)) + .map(bid -> BidderBid.of(bid, BidType.banner, bidResponse.getCur())) .collect(Collectors.toList()); } diff --git a/src/main/resources/bidder-config/logicad.yaml b/src/main/resources/bidder-config/logicad.yaml index 813d79eefe0..9761a40a805 100644 --- a/src/main/resources/bidder-config/logicad.yaml +++ b/src/main/resources/bidder-config/logicad.yaml @@ -17,7 +17,7 @@ adapters: vendor-id: 14 usersync: url: https://cr-p31.ladsp.jp/cookiesender/31?r=true&gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&ru= - redirect-url: /setuid?bidder=logicad&gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&uid=$UID + redirect-url: /setuid?bidder=logicad&gdpr={{gdpr}}&gdpr_consent={{gdpr_consent}}&us_privacy={{us_privacy}}&uid=$UID cookie-family-name: logicad type: redirect support-cors: false diff --git a/src/test/java/org/prebid/server/bidder/logicad/LogicadBidderTest.java b/src/test/java/org/prebid/server/bidder/logicad/LogicadBidderTest.java index d51bcc3e86e..ec096c5a80a 100644 --- a/src/test/java/org/prebid/server/bidder/logicad/LogicadBidderTest.java +++ b/src/test/java/org/prebid/server/bidder/logicad/LogicadBidderTest.java @@ -176,7 +176,7 @@ public void makeBidsShouldReturnBannerBid() throws JsonProcessingException { // then assertThat(result.getErrors()).isEmpty(); assertThat(result.getValue()) - .containsOnly(BidderBid.of(Bid.builder().impid("123").build(), banner, "USD")); + .containsOnly(BidderBid.of(Bid.builder().impid("123").build(), banner, "EUR")); } @Test @@ -194,10 +194,6 @@ private static BidRequest givenBidRequest( .build(); } - private static BidRequest givenBidRequest(Function impCustomizer) { - return givenBidRequest(identity(), impCustomizer, ExtImpLogicad.of("tid")); - } - private static BidRequest givenBidRequest(Function impCustomizer, ExtImpLogicad extImpLogicad) { return givenBidRequest(identity(), impCustomizer, extImpLogicad); @@ -211,17 +207,12 @@ private static Imp givenImp(Function impCustomiz .build(); } - private static Imp givenImp(Function impCustomizer) { - return impCustomizer.apply(Imp.builder() - .ext(mapper.valueToTree(ExtPrebid.of(null, ExtImpLogicad.of("tid"))))) - .build(); - } - private static BidResponse givenBidResponse(Function bidCustomizer) { return BidResponse.builder() .seatbid(singletonList(SeatBid.builder() .bid(singletonList(bidCustomizer.apply(Bid.builder()).build())) .build())) + .cur("EUR") .build(); }