Skip to content

Commit

Permalink
Adhese: Add payload to request (#1571)
Browse files Browse the repository at this point in the history
  • Loading branch information
SerhiiNahornyi authored Nov 9, 2021
1 parent 5cf53f0 commit d059abf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 39 deletions.
12 changes: 7 additions & 5 deletions src/main/java/org/prebid/server/bidder/adhese/AdheseBidder.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import java.util.Objects;
import java.util.TreeMap;

public class AdheseBidder implements Bidder<Void> {
public class AdheseBidder implements Bidder<AdheseRequestBody> {

private static final TypeReference<ExtPrebid<?, ExtImpAdhese>> ADHESE_EXT_TYPE_REFERENCE =
new TypeReference<ExtPrebid<?, ExtImpAdhese>>() {
Expand All @@ -66,7 +66,7 @@ public AdheseBidder(String endpointUrl, JacksonMapper mapper) {
}

@Override
public Result<List<HttpRequest<Void>>> makeHttpRequests(BidRequest request) {
public Result<List<HttpRequest<AdheseRequestBody>>> makeHttpRequests(BidRequest request) {
if (CollectionUtils.isEmpty(request.getImp())) {
return Result.withError(BidderError.badInput("No impression in the bid request"));
}
Expand All @@ -79,12 +79,14 @@ public Result<List<HttpRequest<Void>>> makeHttpRequests(BidRequest request) {
}

final String uri = getUrl(extImpAdhese);
final AdheseRequestBody body = buildBody(request, extImpAdhese);

return Result.of(Collections.singletonList(
HttpRequest.<Void>builder()
HttpRequest.<AdheseRequestBody>builder()
.method(HttpMethod.POST)
.uri(uri)
.body(mapper.encodeToBytes(buildBody(request, extImpAdhese)))
.body(mapper.encodeToBytes(body))
.payload(body)
.headers(replaceHeaders(request.getDevice()))
.build()),
Collections.emptyList());
Expand Down Expand Up @@ -167,7 +169,7 @@ private static Map<String, List<String>> getIfaParameter(Device device) {
}

@Override
public Result<List<BidderBid>> makeBids(HttpCall<Void> httpCall, BidRequest bidRequest) {
public Result<List<BidderBid>> makeBids(HttpCall<AdheseRequestBody> httpCall, BidRequest bidRequest) {
final HttpResponse httpResponse = httpCall.getResponse();

final JsonNode bodyNode;
Expand Down
68 changes: 34 additions & 34 deletions src/test/java/org/prebid/server/bidder/adhese/AdheseBidderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
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;
Expand Down Expand Up @@ -72,7 +71,7 @@ public void makeHttpRequestsShouldReturnErrorIfImpressionListSizeIsZero() {
.build();

// when
final Result<List<HttpRequest<Void>>> result = adheseBidder.makeHttpRequests(bidRequest);
final Result<List<HttpRequest<AdheseRequestBody>>> result = adheseBidder.makeHttpRequests(bidRequest);

// then
assertThat(result.getErrors()).hasSize(1)
Expand All @@ -89,7 +88,7 @@ public void makeHttpRequestsShouldReturnErrorWhenImpExtCouldNotBeParsed() {
.build();

// when
final Result<List<HttpRequest<Void>>> result = adheseBidder.makeHttpRequests(bidRequest);
final Result<List<HttpRequest<AdheseRequestBody>>> result = adheseBidder.makeHttpRequests(bidRequest);

// then
assertThat(result.getErrors()).hasSize(1);
Expand All @@ -101,10 +100,10 @@ public void makeHttpRequestsShouldReturnErrorWhenImpExtCouldNotBeParsed() {
public void makeHttpRequestsShouldModifyIncomingRequestAndSetExpectedHttpRequestUri()
throws JsonProcessingException {
// given
Map<String, List<String>> targets = new HashMap<>();
targets.put("ci", asList("gent", "brussels"));
targets.put("ag", singletonList("55"));
targets.put("tl", singletonList("all"));
final Map<String, List<String>> targets = Map.of(
"ci", asList("gent", "brussels"),
"ag", singletonList("55"),
"tl", singletonList("all"));
final BidRequest bidRequest = BidRequest.builder()
.user(User.builder()
.ext(ExtUser.builder().consent("dummy").build())
Expand All @@ -120,28 +119,29 @@ public void makeHttpRequestsShouldModifyIncomingRequestAndSetExpectedHttpRequest
.build();

// when
final Result<List<HttpRequest<Void>>> result = adheseBidder.makeHttpRequests(bidRequest);
final Result<List<HttpRequest<AdheseRequestBody>>> result = adheseBidder.makeHttpRequests(bidRequest);

// then
final TreeMap<String, List<String>> expectedParameters = new TreeMap<>();
expectedParameters.put("ag", singletonList("55"));
expectedParameters.put("ci", List.of("gent", "brussels"));
expectedParameters.put("tl", singletonList("all"));
expectedParameters.put("xt", singletonList("dummy"));
expectedParameters.put("xz", singletonList("dum-my"));

assertThat(result.getValue())
.extracting(HttpRequest::getUri)
.containsOnly("https://ads-demo.adhese.com/json");
assertThat(result.getValue())
.extracting(HttpRequest::getBody)
.containsOnly(jacksonMapper.mapper().writeValueAsBytes(
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()));
AdheseRequestBody.builder()
.slots(Collections.singletonList(
AdheseRequestBody.Slot.builder()
.slotname("_adhese_prebid_demo_-leaderboard")
.build()))
.parameters(expectedParameters)
.build()));
}

@Test
Expand All @@ -157,7 +157,7 @@ public void makeHttpRequestsShouldModifyIncomingRequestWithIfaParameter() throws
.build();

// when
final Result<List<HttpRequest<Void>>> result = adheseBidder.makeHttpRequests(bidRequest);
final Result<List<HttpRequest<AdheseRequestBody>>> result = adheseBidder.makeHttpRequests(bidRequest);

// then
assertThat(result.getValue())
Expand Down Expand Up @@ -190,7 +190,7 @@ public void makeHttpRequestsShouldModifyIncomingRequestWithRefererParameter() th
.build();

// when
final Result<List<HttpRequest<Void>>> result = adheseBidder.makeHttpRequests(bidRequest);
final Result<List<HttpRequest<AdheseRequestBody>>> result = adheseBidder.makeHttpRequests(bidRequest);

// then
assertThat(result.getValue())
Expand Down Expand Up @@ -227,7 +227,7 @@ public void makeHttpRequestsShouldNotModifyIncomingRequestIfTargetsNotPresent()
.build();

// when
final Result<List<HttpRequest<Void>>> result = adheseBidder.makeHttpRequests(bidRequest);
final Result<List<HttpRequest<AdheseRequestBody>>> result = adheseBidder.makeHttpRequests(bidRequest);

// then
assertThat(result.getValue())
Expand All @@ -250,7 +250,7 @@ public void makeHttpRequestsShouldNotModifyIncomingRequestIfTargetsNotPresent()
@Test
public void makeBidsShouldReturnErrorIfResponseBodyCouldNotBeParsed() {
// given
final HttpCall<Void> httpCall = givenHttpCall("invalid");
final HttpCall<AdheseRequestBody> httpCall = givenHttpCall("invalid");

// when
final Result<List<BidderBid>> result = adheseBidder.makeBids(httpCall, null);
Expand All @@ -265,7 +265,7 @@ public void makeBidsShouldReturnErrorIfResponseBodyCouldNotBeParsed() {
@Test
public void makeBidsShouldReturnErrorIfResponseBodyIsUnexpected() {
// given
final HttpCall<Void> httpCall = givenHttpCall("{}");
final HttpCall<AdheseRequestBody> httpCall = givenHttpCall("{}");

// when
final Result<List<BidderBid>> result = adheseBidder.makeBids(httpCall, null);
Expand All @@ -280,7 +280,7 @@ public void makeBidsShouldReturnErrorIfResponseBodyIsUnexpected() {
@Test
public void makeBidsShouldReturnEmptyResultWhenResponseIsEmptyArray() {
// given
final HttpCall<Void> httpCall = givenHttpCall("[]");
final HttpCall<AdheseRequestBody> httpCall = givenHttpCall("[]");

// when
final Result<List<BidderBid>> result = adheseBidder.makeBids(httpCall, null);
Expand All @@ -293,7 +293,7 @@ public void makeBidsShouldReturnEmptyResultWhenResponseIsEmptyArray() {
@Test
public void makeBidsShouldReturnEmptyResultWhenResponseIsArrayWithEmptyObject() {
// given
final HttpCall<Void> httpCall = givenHttpCall("[{}]");
final HttpCall<AdheseRequestBody> httpCall = givenHttpCall("[{}]");

// when
final Result<List<BidderBid>> result = adheseBidder.makeBids(httpCall, null);
Expand Down Expand Up @@ -331,7 +331,7 @@ public void makeBidsShouldReturnErrorIfSeatbidIsEmpty() throws JsonProcessingExc
final JsonNode mergedResponse = JsonMergePatch.fromJson(adheseOriginDataNode).apply(mergedResponseBid);

final ArrayNode body = mapper.createArrayNode().add(mergedResponse);
final HttpCall<Void> httpCall = givenHttpCall(mapper.writeValueAsString(body));
final HttpCall<AdheseRequestBody> httpCall = givenHttpCall(mapper.writeValueAsString(body));

// when
final Result<List<BidderBid>> result = adheseBidder.makeBids(httpCall, null);
Expand Down Expand Up @@ -375,7 +375,7 @@ public void makeBidsShouldReturnCorrectBidderBid() throws JsonProcessingExceptio
final JsonNode mergedResponse = JsonMergePatch.fromJson(adheseOriginDataNode).apply(mergedResponseBid);

final ArrayNode body = mapper.createArrayNode().add(mergedResponse);
final HttpCall<Void> httpCall = givenHttpCall(mapper.writeValueAsString(body));
final HttpCall<AdheseRequestBody> httpCall = givenHttpCall(mapper.writeValueAsString(body));

// when
final Result<List<BidderBid>> result = adheseBidder.makeBids(httpCall, bidRequest);
Expand Down Expand Up @@ -434,7 +434,7 @@ public void makeBidsShouldReturnCorrectBidIfAdheseBidContainsVastTag() throws Js
final JsonNode mergedResponse = JsonMergePatch.fromJson(adheseOriginDataNode).apply(mergedResponseBid);

final ArrayNode body = mapper.createArrayNode().add(mergedResponse);
final HttpCall<Void> httpCall = givenHttpCall(mapper.writeValueAsString(body));
final HttpCall<AdheseRequestBody> httpCall = givenHttpCall(mapper.writeValueAsString(body));

// when
final Result<List<BidderBid>> result = adheseBidder.makeBids(httpCall, bidRequest);
Expand Down Expand Up @@ -492,7 +492,7 @@ public void makeBidsShouldReturnCorrectBidIfAdheseResponseExtIsNotEqualJs() thro
final JsonNode mergedResponse = JsonMergePatch.fromJson(adheseOriginDataNode).apply(mergedResponseBid);

final ArrayNode body = mapper.createArrayNode().add(mergedResponse);
final HttpCall<Void> httpCall = givenHttpCall(mapper.writeValueAsString(body));
final HttpCall<AdheseRequestBody> httpCall = givenHttpCall(mapper.writeValueAsString(body));

// when
final Result<List<BidderBid>> result = adheseBidder.makeBids(httpCall, bidRequest);
Expand All @@ -518,9 +518,9 @@ public void makeBidsShouldReturnCorrectBidIfAdheseResponseExtIsNotEqualJs() thro
assertThat(result.getValue()).doesNotContainNull().hasSize(1).first().isEqualTo(expected);
}

private static HttpCall<Void> givenHttpCall(String responseBody) {
private static HttpCall<AdheseRequestBody> givenHttpCall(String responseBody) {
return HttpCall.success(
HttpRequest.<Void>builder().build(),
HttpRequest.<AdheseRequestBody>builder().build(),
HttpResponse.of(200, null, responseBody), null);
}
}

0 comments on commit d059abf

Please sign in to comment.