Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix POST requests with Adhese adapter #1124

Merged
merged 17 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 32 additions & 37 deletions src/main/java/org/prebid/server/bidder/adhese/AdheseBidder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.prebid.server.bidder.Bidder;
import org.prebid.server.bidder.adhese.model.AdheseBid;
import org.prebid.server.bidder.adhese.model.AdheseOriginData;
import org.prebid.server.bidder.adhese.model.AdheseRequestBody;
import org.prebid.server.bidder.adhese.model.AdheseResponseExt;
import org.prebid.server.bidder.adhese.model.Cpm;
import org.prebid.server.bidder.adhese.model.CpmValues;
Expand All @@ -44,7 +45,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
* Adhese {@link Bidder} implementation.
Expand All @@ -56,9 +56,9 @@ public class AdheseBidder implements Bidder<Void> {
};

private static final String ORIGIN_BID = "JERLICIA";
private static final String GDPR_QUERY_PARAMETER = "/xt";
private static final String REFERER_QUERY_PARAMETER = "/xf";
private static final String IFA_QUERY_PARAMETER = "/xz";
private static final String GDPR_QUERY_PARAMETER = "xt";
private static final String REFERER_QUERY_PARAMETER = "xf";
private static final String IFA_QUERY_PARAMETER = "xz";

private final String endpointUrl;
private final JacksonMapper mapper;
Expand All @@ -81,12 +81,13 @@ public Result<List<HttpRequest<Void>>> makeHttpRequests(BidRequest request) {
return Result.withError(BidderError.badInput(e.getMessage()));
}

final String uri = buildUrl(request, extImpAdhese);
final String uri = getUrl(extImpAdhese);

return Result.of(Collections.singletonList(
HttpRequest.<Void>builder()
.method(HttpMethod.POST)
.uri(uri)
.body(mapper.encode(buildBody(request, extImpAdhese)))
.headers(replaceHeaders(request.getDevice()))
.build()),
Collections.emptyList());
Expand All @@ -109,36 +110,35 @@ private ExtImpAdhese parseImpExt(Imp imp) {
}
}

private String buildUrl(BidRequest request, ExtImpAdhese extImpAdhese) {
return String.format("%s%s%s%s%s%s",
getUrl(extImpAdhese),
getSlotParameter(extImpAdhese),
getTargetParameters(extImpAdhese),
getGdprParameter(request.getUser()),
getRefererParameter(request.getSite()),
getIfaParameter(request.getDevice()));
private AdheseRequestBody buildBody(BidRequest request, ExtImpAdhese extImpAdhese) {
final Map<String, List<String>> parameterMap = new TreeMap<>();
SerhiiNahornyi marked this conversation as resolved.
Show resolved Hide resolved
parameterMap.putAll(getTargetParameters(extImpAdhese));
parameterMap.putAll(getGdprParameter(request.getUser()));
parameterMap.putAll(getRefererParameter(request.getSite()));
parameterMap.putAll(getIfaParameter(request.getDevice()));

return AdheseRequestBody.builder()
.slots(Collections.singletonList(
AdheseRequestBody.Slot.builder()
.slotname(getSlotParameter(extImpAdhese))
.build()))
.parameters(parameterMap)
.build();
}

private String getUrl(ExtImpAdhese extImpAdhese) {
return endpointUrl.replace("{{AccountId}}", extImpAdhese.getAccount());
}

private static String getSlotParameter(ExtImpAdhese extImpAdhese) {
return String.format("/sl%s-%s",
return String.format("%s-%s",
HttpUtil.encodeUrl(extImpAdhese.getLocation()),
HttpUtil.encodeUrl(extImpAdhese.getFormat()));
}

private String getTargetParameters(ExtImpAdhese extImpAdhese) {
private Map<String, List<String>> getTargetParameters(ExtImpAdhese extImpAdhese) {
final JsonNode targets = extImpAdhese.getTargets();
if (targets == null || targets.isNull()) {
return "";
}

final Map<String, List<String>> targetParameters = parseTargetParametersAndSort(targets);
return targetParameters.entrySet().stream()
.map(entry -> createPartOrUrl(entry.getKey(), entry.getValue()))
.collect(Collectors.joining());
return targets == null || targets.isEmpty() ? Collections.emptyMap() : parseTargetParametersAndSort(targets);
}

private Map<String, List<String>> parseTargetParametersAndSort(JsonNode targets) {
Expand All @@ -147,31 +147,26 @@ private Map<String, List<String>> parseTargetParametersAndSort(JsonNode targets)
}));
}

private static String createPartOrUrl(String key, List<String> values) {
final String formattedValues = String.join(";", values);
return String.format("/%s%s", HttpUtil.encodeUrl(key), formattedValues);
}

private static String getGdprParameter(User user) {
private static Map<String, List<String>> getGdprParameter(User user) {
final ExtUser extUser = user != null ? user.getExt() : null;
final String consent = extUser != null ? extUser.getConsent() : null;
return StringUtils.isNotBlank(consent)
? String.format("%s%s", GDPR_QUERY_PARAMETER, consent)
: "";
? Collections.singletonMap(GDPR_QUERY_PARAMETER, Collections.singletonList(consent))
: Collections.emptyMap();
}

private static String getRefererParameter(Site site) {
private static Map<String, List<String>> getRefererParameter(Site site) {
final String page = site != null ? site.getPage() : null;
return StringUtils.isNotBlank(page)
? String.format("%s%s", REFERER_QUERY_PARAMETER, HttpUtil.encodeUrl(page))
: "";
? Collections.singletonMap(REFERER_QUERY_PARAMETER, Collections.singletonList(page))
: Collections.emptyMap();
}

private static String getIfaParameter(Device device) {
private static Map<String, List<String>> getIfaParameter(Device device) {
final String ifa = device != null ? device.getIfa() : null;
return StringUtils.isNotBlank(ifa)
? String.format("%s%s", IFA_QUERY_PARAMETER, HttpUtil.encodeUrl(ifa))
: "";
? Collections.singletonMap(IFA_QUERY_PARAMETER, Collections.singletonList(ifa))
: Collections.emptyMap();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.prebid.server.bidder.adhese.model;

import lombok.Builder;
import lombok.Value;

import java.util.List;
import java.util.Map;

@Builder
@Value
public class AdheseRequestBody {
SerhiiNahornyi marked this conversation as resolved.
Show resolved Hide resolved

List<Slot> slots;
SerhiiNahornyi marked this conversation as resolved.
Show resolved Hide resolved
Map<String, List<String>> parameters;

@Builder
@Value
public static class Slot {

String slotname;
}
}
88 changes: 75 additions & 13 deletions src/test/java/org/prebid/server/bidder/adhese/AdheseBidderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.prebid.server.VertxTest;
import org.prebid.server.bidder.adhese.model.AdheseBid;
import org.prebid.server.bidder.adhese.model.AdheseOriginData;
import org.prebid.server.bidder.adhese.model.AdheseRequestBody;
import org.prebid.server.bidder.adhese.model.AdheseResponseExt;
import org.prebid.server.bidder.adhese.model.Cpm;
import org.prebid.server.bidder.adhese.model.CpmValues;
Expand All @@ -33,9 +34,12 @@
import org.prebid.server.proto.openrtb.ext.response.BidType;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
Expand Down Expand Up @@ -94,7 +98,8 @@ public void makeHttpRequestsShouldReturnErrorWhenImpExtCouldNotBeParsed() {
}

@Test
public void makeHttpRequestsShouldModifyIncomingRequestAndSetExpectedHttpRequestUri() {
public void makeHttpRequestsShouldModifyIncomingRequestAndSetExpectedHttpRequestUri()
throws JsonProcessingException {
// given
Map<String, List<String>> targets = new HashMap<>();
targets.put("ci", asList("gent", "brussels"));
Expand All @@ -105,8 +110,11 @@ public void makeHttpRequestsShouldModifyIncomingRequestAndSetExpectedHttpRequest
.ext(ExtUser.builder().consent("dummy").build())
.build())
.imp(singletonList(Imp.builder()
.ext(mapper.valueToTree(ExtPrebid.of(null, ExtImpAdhese.of("demo",
"_adhese_prebid_demo_", "leaderboard", mapper.convertValue(targets, JsonNode.class)))))
.ext(mapper.valueToTree(ExtPrebid.of(null, ExtImpAdhese.of(
"demo",
"_adhese_prebid_demo_",
"leaderboard",
mapper.convertValue(targets, JsonNode.class)))))
.build()))
.device(Device.builder().ifa("dum-my").build())
.build();
Expand All @@ -117,12 +125,27 @@ public void makeHttpRequestsShouldModifyIncomingRequestAndSetExpectedHttpRequest
// then
assertThat(result.getValue())
.extracting(HttpRequest::getUri)
.containsOnly("https://ads-demo.adhese.com/json/sl_adhese_prebid_demo_-leaderboard/ag55/cigent;brussels"
+ "/tlall/xtdummy/xzdum-my");
.containsOnly("https://ads-demo.adhese.com/json");
assertThat(result.getValue())
.extracting(HttpRequest::getBody)
.containsOnly(jacksonMapper.mapper().writeValueAsString(
AdheseRequestBody
.builder()
.slots(Collections.singletonList(
AdheseRequestBody.Slot.builder()
.slotname("_adhese_prebid_demo_-leaderboard")
.build()))
.parameters(new TreeMap<String, List<String>>() {{
put("ag", Arrays.asList("55"));
put("ci", Arrays.asList("gent", "brussels"));
put("tl", Arrays.asList("all"));
put("xt", Arrays.asList("dummy"));
put("xz", Arrays.asList("dum-my")); }})
.build()));
}

@Test
public void makeHttpRequestsShouldModifyIncomingRequestWithIfaParameter() {
public void makeHttpRequestsShouldModifyIncomingRequestWithIfaParameter() throws JsonProcessingException {
// given
final BidRequest bidRequest = BidRequest.builder()
.device(Device.builder().ifa("ifaValue").build())
Expand All @@ -139,11 +162,23 @@ public void makeHttpRequestsShouldModifyIncomingRequestWithIfaParameter() {
// then
assertThat(result.getValue())
.extracting(HttpRequest::getUri)
.containsOnly("https://ads-demo.adhese.com/json/sl_adhese_prebid_demo_-leaderboard/xzifaValue");
.containsOnly("https://ads-demo.adhese.com/json");
assertThat(result.getValue())
.extracting(HttpRequest::getBody)
.containsOnly(jacksonMapper.mapper().writeValueAsString(
AdheseRequestBody
.builder()
.slots(Collections.singletonList(
AdheseRequestBody.Slot.builder()
.slotname("_adhese_prebid_demo_-leaderboard")
.build()))
.parameters(new TreeMap<>(Collections.singletonMap(
"xz", Arrays.asList("ifaValue"))))
.build()));
}

@Test
public void makeHttpRequestsShouldModifyIncomingRequestWithRefererParameter() {
public void makeHttpRequestsShouldModifyIncomingRequestWithRefererParameter() throws JsonProcessingException {
// given
final BidRequest bidRequest = BidRequest.builder()
.site(Site.builder().page("pageValue").build())
Expand All @@ -160,19 +195,34 @@ public void makeHttpRequestsShouldModifyIncomingRequestWithRefererParameter() {
// then
assertThat(result.getValue())
.extracting(HttpRequest::getUri)
.containsOnly("https://ads-demo.adhese.com/json/sl_adhese_prebid_demo_-leaderboard/xfpageValue");
.containsOnly("https://ads-demo.adhese.com/json");
assertThat(result.getValue())
.extracting(HttpRequest::getBody)
.containsOnly(jacksonMapper.mapper().writeValueAsString(
AdheseRequestBody
.builder()
.slots(Collections.singletonList(
AdheseRequestBody.Slot.builder()
.slotname("_adhese_prebid_demo_-leaderboard")
.build()))
.parameters(new TreeMap<>(Collections.singletonMap(
"xf", Arrays.asList("pageValue"))))
.build()));
}

@Test
public void makeHttpRequestsShouldNotModifyIncomingRequestIfTargetsNotPresent() {
public void makeHttpRequestsShouldNotModifyIncomingRequestIfTargetsNotPresent() throws JsonProcessingException {
// given
final BidRequest bidRequest = BidRequest.builder()
.user(User.builder()
.ext(ExtUser.builder().consent("dummy").build())
.build())
.imp(singletonList(Imp.builder()
.ext(mapper.valueToTree(ExtPrebid.of(null, ExtImpAdhese.of("demo",
"_adhese_prebid_demo_", "leaderboard", mapper.convertValue(null, JsonNode.class)))))
.ext(mapper.valueToTree(ExtPrebid.of(null, ExtImpAdhese.of(
"demo",
"_adhese_prebid_demo_",
"leaderboard",
mapper.convertValue(null, JsonNode.class)))))
.build()))
.build();

Expand All @@ -182,7 +232,19 @@ public void makeHttpRequestsShouldNotModifyIncomingRequestIfTargetsNotPresent()
// then
assertThat(result.getValue())
.extracting(HttpRequest::getUri)
.containsOnly("https://ads-demo.adhese.com/json/sl_adhese_prebid_demo_-leaderboard/xtdummy");
.containsOnly("https://ads-demo.adhese.com/json");
assertThat(result.getValue())
.extracting(HttpRequest::getBody)
.containsOnly(jacksonMapper.mapper().writeValueAsString(
AdheseRequestBody
.builder()
.slots(Collections.singletonList(
AdheseRequestBody.Slot.builder()
.slotname("_adhese_prebid_demo_-leaderboard")
.build()))
.parameters(new TreeMap<>(Collections.singletonMap(
"xt", Arrays.asList("dummy"))))
.build()));
}

@Test
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/org/prebid/server/it/AdheseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ public class AdheseTest extends IntegrationTest {
@Test
public void openrtb2AuctionShouldRespondWithBidsFromAdhese() throws IOException, JSONException {

WIRE_MOCK_RULE.stubFor(WireMock.post(WireMock.urlPathEqualTo("/adhese-exchange/sl_adhese_prebid_demo_-"
+ "leaderboard/ag55/cigent;brussels/tlall/xtconsentValue/xfhttp%3A%2F%2Fwww.example.com/xzifaId"))
WIRE_MOCK_RULE.stubFor(WireMock.post(WireMock.urlPathEqualTo("/adhese-exchange"))
.withHeader("Accept", WireMock.equalTo("application/json"))
.withHeader("Content-Type", WireMock.equalTo("application/json;charset=UTF-8"))
.withRequestBody(WireMock.absent())
.withRequestBody(WireMock.equalToJson("{\"slots\":[{\"slotname\":\"_adhese_prebid_demo_-leaderboard\"}],\"parameters\":{\"ag\":[\"55\"],\"ci\":[\"gent\",\"brussels\"],\"tl\":[\"all\"],\"xt\":[\"consentValue\"],\"xf\":[\"http://www.example.com\"],\"xz\":[\"ifaId\"]}}"))
Copy link
Collaborator

@SerhiiNahornyi SerhiiNahornyi Feb 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we create json file with this body?
.withRequestBody(WireMock.equalToJson(jsonFrom("openrtb2/adhese/adheese-request-body.json")))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sander-adhese Agree with the above?

.willReturn(WireMock.aResponse().withBody(jsonFrom("openrtb2/adhese/test-adhese-bid-response.json"))));

// pre-bid cache
Expand Down