Skip to content

Commit

Permalink
Beachfront update (#1177)
Browse files Browse the repository at this point in the history
  • Loading branch information
SerhiiNahornyi authored and nickluck8 committed Aug 9, 2021
1 parent cbde713 commit 1857515
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public class BeachfrontBidder implements Bidder<Void> {
private static final String NURL_VIDEO_TYPE = "nurl";
private static final String ADM_VIDEO_TYPE = "adm";
private static final String BEACHFRONT_NAME = "BF_PREBID_S2S";
private static final String BEACHFRONT_VERSION = "0.9.1";
private static final String BEACHFRONT_VERSION = "0.9.2";
private static final String NURL_VIDEO_ENDPOINT_SUFFIX = "&prebidserver";
private static final String TEST_IP = "192.168.255.255";
private static final String FAKE_IP = "255.255.255.255";

private static final BigDecimal MIN_BID_FLOOR = BigDecimal.valueOf(0.01);
private static final int DEFAULT_VIDEO_WIDTH = 300;
Expand Down Expand Up @@ -236,7 +236,7 @@ private static String getAppId(ExtImpBeachfront extImpBeachfront, boolean isBann
}

private static BigDecimal checkBidFloor(BigDecimal bidFloor) {
return bidFloor.compareTo(MIN_BID_FLOOR) > 0 ? bidFloor : BigDecimal.ZERO;
return bidFloor != null && bidFloor.compareTo(MIN_BID_FLOOR) > 0 ? bidFloor : BigDecimal.ZERO;
}

/**
Expand Down Expand Up @@ -273,7 +273,7 @@ private static User makeUser(User user) {
*/
private static void populateDeviceFields(BeachfrontBannerRequest.BeachfrontBannerRequestBuilder builder,
Device device) {
builder.ip(getIP(device.getIp()));
builder.ip(device.getIp());
builder.deviceModel(device.getModel());
builder.deviceOs(device.getOs());
if (device.getDnt() != null) {
Expand All @@ -284,10 +284,6 @@ private static void populateDeviceFields(BeachfrontBannerRequest.BeachfrontBanne
}
}

private static String getIP(String ip) {
return StringUtils.isBlank(ip) || ip.equals("::1") || ip.equals("127.0.0.1") ? TEST_IP : ip;
}

private static int getSecure(String page) {
return StringUtils.contains(page, "https") ? 1 : 0;
}
Expand All @@ -308,15 +304,19 @@ private List<BeachfrontVideoRequest> getVideoRequests(BidRequest bidRequest, Lis
}

final String videoResponseType = extImpBeachfront.getVideoResponseType();
final String responseType = StringUtils.isBlank(videoResponseType) ? ADM_VIDEO_TYPE : videoResponseType;
final BeachfrontVideoRequest.BeachfrontVideoRequestBuilder requestBuilder = BeachfrontVideoRequest.builder()
.appId(appId)
.videoResponseType(responseType);
.appId(appId);
final String responseType;

if (responseType.equals(NURL_VIDEO_TYPE)) {
if (videoResponseType != null && videoResponseType.equals(NURL_VIDEO_TYPE)) {
requestBuilder.isPrebid(true);
responseType = NURL_VIDEO_TYPE;
} else {
responseType = ADM_VIDEO_TYPE;
}

requestBuilder.videoResponseType(responseType);

final BidRequest.BidRequestBuilder bidRequestBuilder = bidRequest.toBuilder();
int secure = 0;
final Site site = bidRequest.getSite();
Expand All @@ -326,29 +326,30 @@ private List<BeachfrontVideoRequest> getVideoRequests(BidRequest bidRequest, Lis
secure = getSecure(site.getPage());
}

final App app = bidRequest.getApp();
if (app != null && StringUtils.isBlank(app.getDomain()) && StringUtils.isNotBlank(app.getBundle())) {
final String trimmedBundle = StringUtils.removeStart(app.getBundle(), "_");
final String[] split = StringUtils.removeEnd(trimmedBundle, "_").split("\\.");

if (split.length > 1) {
bidRequestBuilder.app(app.toBuilder().domain(String.format("%s.%s", split[1], split[0])).build());
}
}

final Device device = bidRequest.getDevice();
if (device != null) {
final Device.DeviceBuilder deviceBuilder = device.toBuilder();
final Integer devicetype = device.getDevicetype();
if (devicetype == null || devicetype == 0) {
deviceBuilder.devicetype(bidRequest.getSite() != null ? 2 : 1);
}
if (StringUtils.isNotBlank(device.getIp())) {
deviceBuilder.ip(getIP(device.getIp()));
if (StringUtils.isBlank(device.getIp()) && responseType.equals(ADM_VIDEO_TYPE)) {
deviceBuilder.ip(FAKE_IP);
}

bidRequestBuilder.device(deviceBuilder.build());
}

final App app = bidRequest.getApp();
if (app != null && StringUtils.isBlank(app.getDomain()) && StringUtils.isNotBlank(app.getBundle())) {
final String trimmedBundle = StringUtils.removeStart(app.getBundle(), "_");
final String[] split = StringUtils.removeEnd(trimmedBundle, "_").split("\\.");

if (split.length > 1) {
bidRequestBuilder.app(app.toBuilder().domain(String.format("%s.%s", split[1], split[0])).build());
}
}

final Imp.ImpBuilder impBuilder = imp.toBuilder()
.banner(null)
.ext(null)
Expand Down Expand Up @@ -421,7 +422,8 @@ private List<BeachfrontResponseSlot> makeBeachfrontResponseSlots(String response
responseBody,
mapper.mapper().getTypeFactory().constructCollectionType(List.class, BeachfrontResponseSlot.class));
} catch (IOException ex) {
throw new PreBidException(ex.getMessage());
throw new PreBidException("server response failed to unmarshal "
+ "as valid rtb. Run with request.debug = 1 for more info");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,8 @@ public void makeHttpRequestsShouldReturnExpectedBannerRequest() {
.deviceOs("nokia")
.isMobile(1)
.user(User.builder().id("userId").buyeruid("buid").build())
.adapterVersion("0.9.1")
.adapterVersion("0.9.2")
.adapterName("BF_PREBID_S2S")
.ip("192.168.255.255")
.requestId("153")
.real204(true)
.build());
Expand All @@ -281,7 +280,7 @@ public void makeHttpRequestsShouldReturnExpectedAdmAndNurlVideoRequests() {
// given
final BidRequest bidRequest = BidRequest.builder()
.app(App.builder().bundle("prefix_test1.test2.test3_suffix").build())
.device(Device.builder().ip("127.0.0.1").build())
.device(Device.builder().build())
.imp(asList(
givenImp(impBuilder -> impBuilder.ext(mapper.valueToTree(ExtPrebid.of(null,
mapper.valueToTree(ExtImpBeachfront.of("appId2", null, BigDecimal.TEN, "nurl")))))),
Expand All @@ -294,10 +293,6 @@ public void makeHttpRequestsShouldReturnExpectedAdmAndNurlVideoRequests() {
// then
assertThat(result.getErrors()).isEmpty();

final BidRequest.BidRequestBuilder expectedRequestBuilder = BidRequest.builder()
.device(Device.builder().ip("192.168.255.255").devicetype(1).build())
.cur(singletonList("USD"));

final Imp.ImpBuilder expectedImpBuilder = Imp.builder()
.video(Video.builder().w(300).h(250).build())
.secure(0);
Expand All @@ -308,25 +303,50 @@ public void makeHttpRequestsShouldReturnExpectedAdmAndNurlVideoRequests() {
.isPrebid(true)
.appId("appId2")
.videoResponseType("nurl")
.request(expectedRequestBuilder
.request(BidRequest.builder()
.device(Device.builder().devicetype(1).build())
.app(App.builder().bundle("prefix_test1.test2.test3_suffix")
.domain("test2.prefix_test1").build())
.imp(singletonList(expectedImpBuilder.id("123")
.bidfloor(BigDecimal.TEN).build()))
.cur(singletonList("USD"))
.build())
.build(),
BeachfrontVideoRequest.builder()
.appId("appId")
.videoResponseType("adm")
.request(expectedRequestBuilder
.request(BidRequest.builder()
.device(Device.builder().ip("255.255.255.255").devicetype(1).build())
.app(App.builder().bundle("prefix_test1.test2.test3_suffix")
.domain("test2.prefix_test1").build())
.imp(singletonList(expectedImpBuilder.id("234")
.bidfloor(BigDecimal.ONE).build()))
.cur(singletonList("USD"))
.build())
.build());
}

@Test
public void makeHttpRequestsShouldCreateAdmRequestForEveryUnknownResponseType() {
// given
final BidRequest bidRequest = BidRequest.builder()
.imp(singletonList(
givenImp(impBuilder -> impBuilder.ext(mapper.valueToTree(ExtPrebid.of(null,
mapper.valueToTree(ExtImpBeachfront.of("appId2", null, null, "unknownType"))))))
))
.build();

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

// then
assertThat(result.getErrors()).isEmpty();
assertThat(result.getValue())
.extracting(httpRequest -> mapper.readValue(httpRequest.getBody(), BeachfrontVideoRequest.class))
.extracting(BeachfrontVideoRequest::getVideoResponseType)
.containsExactly("adm");
}

@Test
public void makeBidsShouldReturnEmptyResultWhenResponseBodyHasEmptyArray() {
// given
Expand All @@ -351,7 +371,9 @@ public void makeBidsShouldReturnErrorWhenResponseBodyIsInvalid() {
// then
assertThat(result.getValue()).isEmpty();
assertThat(result.getErrors()).hasSize(1);
assertThat(result.getErrors().get(0).getMessage()).startsWith("Unrecognized token");
assertThat(result.getErrors().get(0).getMessage())
.isEqualTo("server response failed to unmarshal as valid rtb. "
+ "Run with request.debug = 1 for more info");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"buyeruid": "BF-UID"
},
"adapterName": "BF_PREBID_S2S",
"adapterVersion": "0.9.1",
"adapterVersion": "0.9.2",
"ip": "193.168.244.1",
"requestId": "tid",
"real204": true
}
}

0 comments on commit 1857515

Please sign in to comment.