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: Make OpenRTB battr logic more strict #3538

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

public class RequestUpdater {

Expand Down Expand Up @@ -99,36 +98,42 @@ private static List<Integer> extractBattr(Map<MediaType, Map<String, List<Intege
}

private static Banner updateBanner(Banner banner, List<Integer> btype, List<Integer> battr) {
final List<Integer> existingBtype = banner != null ? banner.getBtype() : null;
final List<Integer> existingBattr = banner != null ? banner.getBattr() : null;
if (banner == null) {
return null;
}

final List<Integer> existingBtype = banner.getBtype();
final List<Integer> existingBattr = banner.getBattr();

return CollectionUtils.isEmpty(existingBtype) || CollectionUtils.isEmpty(existingBattr)
? Optional.ofNullable(banner)
.map(Banner::toBuilder)
.orElseGet(Banner::builder)
? banner.toBuilder()
.btype(CollectionUtils.isNotEmpty(existingBtype) ? existingBtype : btype)
.battr(CollectionUtils.isNotEmpty(existingBattr) ? existingBattr : battr)
.build()
: banner;
}

private static Video updateVideo(Video video, List<Integer> battr) {
final List<Integer> existingBattr = video != null ? video.getBattr() : null;
if (video == null) {
return null;
}

final List<Integer> existingBattr = video.getBattr();
return CollectionUtils.isEmpty(existingBattr)
? Optional.ofNullable(video)
.map(Video::toBuilder)
.orElseGet(Video::builder)
? video.toBuilder()
.battr(battr)
.build()
: video;
}

private static Audio updateAudio(Audio audio, List<Integer> battr) {
final List<Integer> existingBattr = audio != null ? audio.getBattr() : null;
if (audio == null) {
return null;
}

final List<Integer> existingBattr = audio.getBattr();
return CollectionUtils.isEmpty(existingBattr)
? Optional.ofNullable(audio)
.map(Audio::toBuilder)
.orElseGet(Audio::builder)
? audio.toBuilder()
.battr(battr)
.build()
: audio;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,12 @@ MediaType.VIDEO, singletonMap("impId1", asList(3, 4)),
MediaType.AUDIO, singletonMap("impId1", asList(5, 6))))
.build());
final BidRequest request = BidRequest.builder()
.imp(singletonList(Imp.builder().id("impId1").build()))
.imp(singletonList(Imp.builder()
.id("impId1")
.banner(Banner.builder().build())
.video(Video.builder().build())
.audio(Audio.builder().build())
.build()))
.build();

// when and then
Expand All @@ -373,4 +378,115 @@ MediaType.AUDIO, singletonMap("impId1", asList(5, 6))))
.build()))
.build());
}

@Test
public void shouldNotUpdateMissingBanner() {
// given
final RequestUpdater updater = RequestUpdater.create(
BlockedAttributes.builder()
.badv(asList("domain1.com", "domain2.com"))
.bcat(asList("cat1", "cat2"))
.bapp(asList("app1", "app2"))
.btype(singletonMap("impId1", asList(1, 2)))
.battr(Map.of(
MediaType.BANNER, singletonMap("impId1", asList(1, 2)),
MediaType.VIDEO, singletonMap("impId1", asList(3, 4)),
MediaType.AUDIO, singletonMap("impId1", asList(5, 6))))
.build());
final BidRequest request = BidRequest.builder()
.imp(singletonList(Imp.builder()
.id("impId1")
.video(Video.builder().build())
.audio(Audio.builder().build())
.build()))
.build();

// when and then
assertThat(updater.update(request)).isEqualTo(BidRequest.builder()
.badv(asList("domain1.com", "domain2.com"))
.bcat(asList("cat1", "cat2"))
.bapp(asList("app1", "app2"))
.imp(singletonList(Imp.builder()
.id("impId1")
.video(Video.builder().battr(asList(3, 4)).build())
.audio(Audio.builder().battr(asList(5, 6)).build())
.build()))
.build());
}

@Test
public void shouldNotUpdateMissingVideo() {
// given
final RequestUpdater updater = RequestUpdater.create(
BlockedAttributes.builder()
.badv(asList("domain1.com", "domain2.com"))
.bcat(asList("cat1", "cat2"))
.bapp(asList("app1", "app2"))
.btype(singletonMap("impId1", asList(1, 2)))
.battr(Map.of(
MediaType.BANNER, singletonMap("impId1", asList(1, 2)),
MediaType.VIDEO, singletonMap("impId1", asList(3, 4)),
MediaType.AUDIO, singletonMap("impId1", asList(5, 6))))
.build());
final BidRequest request = BidRequest.builder()
.imp(singletonList(Imp.builder()
.id("impId1")
.banner(Banner.builder().build())
.audio(Audio.builder().build())
.build()))
.build();

// when and then
assertThat(updater.update(request)).isEqualTo(BidRequest.builder()
.badv(asList("domain1.com", "domain2.com"))
.bcat(asList("cat1", "cat2"))
.bapp(asList("app1", "app2"))
.imp(singletonList(Imp.builder()
.id("impId1")
.banner(Banner.builder()
.btype(asList(1, 2))
.battr(asList(1, 2))
.build())
.audio(Audio.builder().battr(asList(5, 6)).build())
.build()))
.build());
}

@Test
public void shouldNotUpdateMissingAudio() {
// given
final RequestUpdater updater = RequestUpdater.create(
BlockedAttributes.builder()
.badv(asList("domain1.com", "domain2.com"))
.bcat(asList("cat1", "cat2"))
.bapp(asList("app1", "app2"))
.btype(singletonMap("impId1", asList(1, 2)))
.battr(Map.of(
MediaType.BANNER, singletonMap("impId1", asList(1, 2)),
MediaType.VIDEO, singletonMap("impId1", asList(3, 4)),
MediaType.AUDIO, singletonMap("impId1", asList(5, 6))))
.build());
final BidRequest request = BidRequest.builder()
.imp(singletonList(Imp.builder()
.id("impId1")
.banner(Banner.builder().build())
.video(Video.builder().build())
.build()))
.build();

// when and then
assertThat(updater.update(request)).isEqualTo(BidRequest.builder()
.badv(asList("domain1.com", "domain2.com"))
.bcat(asList("cat1", "cat2"))
.bapp(asList("app1", "app2"))
.imp(singletonList(Imp.builder()
.id("impId1")
.banner(Banner.builder()
.btype(asList(1, 2))
.battr(asList(1, 2))
.build())
.video(Video.builder().battr(asList(3, 4)).build())
.build()))
.build());
}
}
Loading