Skip to content

Commit

Permalink
Make OpenRTB battr logic more strict
Browse files Browse the repository at this point in the history
  • Loading branch information
Net-burst committed Nov 5, 2024
1 parent 98e8065 commit a9ccd6d
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 15 deletions.
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());
}
}

0 comments on commit a9ccd6d

Please sign in to comment.