From 010d24ab09adfbabf5d62bf01c2375dec2dba819 Mon Sep 17 00:00:00 2001 From: Valentin Petrovych Date: Wed, 11 Oct 2023 15:38:53 +0200 Subject: [PATCH] refactor: caching #699 --- .../main/java/org/prebid/mobile/AdUnit.java | 10 +++++++++- .../org/prebid/mobile/api/data/BidInfo.java | 18 +++++------------- .../api/original/OnCompleteListenerImpl.java | 12 +++++++++++- .../mobile/api/original/PrebidAdUnit.java | 2 +- .../original/OnCompleteListenerImplTest.java | 7 +++++++ 5 files changed, 33 insertions(+), 16 deletions(-) diff --git a/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/AdUnit.java b/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/AdUnit.java index d19d937df..db56d9269 100644 --- a/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/AdUnit.java +++ b/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/AdUnit.java @@ -200,7 +200,15 @@ public void fetchDemand(OnFetchDemandResult listener) { allowNullableAdObject = true; fetchDemand(null, resultCode -> { - BidInfo bidInfo = BidInfo.create(resultCode, bidResponse, configuration, adObject); + BidInfo bidInfo = BidInfo.create(resultCode, bidResponse); + + boolean isNativeAndSuccessful = configuration.getNativeConfiguration() != null && resultCode == ResultCode.SUCCESS && bidResponse != null; + if (isNativeAndSuccessful) { + String cacheId = CacheManager.save(bidResponse.getWinningBidJson()); + Util.saveCacheId(cacheId, adObject); + bidInfo.setNativeCacheId(cacheId); + } + listener.onComplete(bidInfo); }); } diff --git a/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/api/data/BidInfo.java b/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/api/data/BidInfo.java index 13df335e5..f7f050514 100644 --- a/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/api/data/BidInfo.java +++ b/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/api/data/BidInfo.java @@ -3,10 +3,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import org.prebid.mobile.CacheManager; import org.prebid.mobile.ResultCode; -import org.prebid.mobile.Util; -import org.prebid.mobile.configuration.AdUnitConfiguration; import org.prebid.mobile.rendering.bidding.data.bid.Bid; import org.prebid.mobile.rendering.bidding.data.bid.BidResponse; @@ -54,6 +51,10 @@ public String getNativeCacheId() { return nativeCacheId; } + public void setNativeCacheId(@Nullable String nativeCacheId) { + this.nativeCacheId = nativeCacheId; + } + @Nullable public Integer getExp() { return exp; @@ -68,9 +69,7 @@ public Map getEvents() { @NonNull public static BidInfo create( @NonNull ResultCode resultCode, - @Nullable BidResponse bidResponse, - @Nullable AdUnitConfiguration configuration, - @Nullable Object adObject + @Nullable BidResponse bidResponse ) { BidInfo bidInfo = new BidInfo(resultCode); if (bidResponse == null) { @@ -86,13 +85,6 @@ public static BidInfo create( bidInfo.events = winningBid.getEvents(); } - boolean isNative = configuration != null && configuration.getNativeConfiguration() != null; - if (isNative && bidInfo.resultCode == ResultCode.SUCCESS) { - String cacheId = CacheManager.save(bidResponse.getWinningBidJson()); - Util.saveCacheId(cacheId, adObject); - bidInfo.nativeCacheId = cacheId; - } - return bidInfo; } diff --git a/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/api/original/OnCompleteListenerImpl.java b/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/api/original/OnCompleteListenerImpl.java index 5df60a06e..b55e02115 100644 --- a/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/api/original/OnCompleteListenerImpl.java +++ b/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/api/original/OnCompleteListenerImpl.java @@ -3,9 +3,11 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import org.prebid.mobile.CacheManager; import org.prebid.mobile.OnCompleteListener; import org.prebid.mobile.OnCompleteListener2; import org.prebid.mobile.ResultCode; +import org.prebid.mobile.Util; import org.prebid.mobile.api.data.BidInfo; import java.util.Map; @@ -44,7 +46,15 @@ public void onComplete(ResultCode resultCode, @Nullable Map unmo private void notifyListener(ResultCode resultCode) { - BidInfo bidInfo = BidInfo.create(resultCode, adUnit.getBidResponse(), adUnit.getConfiguration(), adObject); + BidInfo bidInfo = BidInfo.create(resultCode, adUnit.getBidResponse()); + + boolean isNativeAndSuccessful = adUnit.getConfiguration().getNativeConfiguration() != null && resultCode == ResultCode.SUCCESS && adUnit.getBidResponse() != null; + if (isNativeAndSuccessful) { + String cacheId = CacheManager.save(adUnit.getBidResponse().getWinningBidJson()); + Util.saveCacheId(cacheId, adObject); + bidInfo.setNativeCacheId(cacheId); + } + listener.onComplete(bidInfo); } diff --git a/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/api/original/PrebidAdUnit.java b/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/api/original/PrebidAdUnit.java index 040e3d155..eca59eeb9 100644 --- a/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/api/original/PrebidAdUnit.java +++ b/PrebidMobile/PrebidMobile-core/src/main/java/org/prebid/mobile/api/original/PrebidAdUnit.java @@ -75,7 +75,7 @@ private void baseFetchDemand( } if (request == null || requestDoesNotHaveAnyConfiguration(request)) { - userListener.onComplete(BidInfo.create(ResultCode.INVALID_PREBID_REQUEST_OBJECT, null, null, null)); + userListener.onComplete(BidInfo.create(ResultCode.INVALID_PREBID_REQUEST_OBJECT, null)); return; } diff --git a/PrebidMobile/PrebidMobile-core/src/test/java/org/prebid/mobile/api/original/OnCompleteListenerImplTest.java b/PrebidMobile/PrebidMobile-core/src/test/java/org/prebid/mobile/api/original/OnCompleteListenerImplTest.java index 747abc222..3f2f2b5e5 100644 --- a/PrebidMobile/PrebidMobile-core/src/test/java/org/prebid/mobile/api/original/OnCompleteListenerImplTest.java +++ b/PrebidMobile/PrebidMobile-core/src/test/java/org/prebid/mobile/api/original/OnCompleteListenerImplTest.java @@ -16,6 +16,8 @@ import org.prebid.mobile.NativeParameters; import org.prebid.mobile.ResultCode; import org.prebid.mobile.api.data.BidInfo; +import org.prebid.mobile.configuration.AdUnitConfiguration; +import org.prebid.mobile.configuration.NativeAdUnitConfiguration; import org.prebid.mobile.rendering.bidding.data.bid.Bid; import org.prebid.mobile.rendering.bidding.data.bid.BidResponse; @@ -54,6 +56,7 @@ public void emptyBidResponse() { ); when(mockAdUnit.getBidResponse()).thenReturn(null); + when(mockAdUnit.getConfiguration()).thenReturn(mock(AdUnitConfiguration.class)); subject.onComplete(ResultCode.NO_BIDS); @@ -76,6 +79,7 @@ public void fullBidResponse() { null, mockListener ); + when(mockAdUnit.getConfiguration()).thenReturn(mock(AdUnitConfiguration.class)); HashMap keywords = new HashMap<>(); keywords.put("key1", "value1"); @@ -115,6 +119,9 @@ public void fullBidResponseWithNative() { Bid mockBid = mock(Bid.class); when(mockBid.getEvents()).thenReturn(events); + AdUnitConfiguration mockConfiguration = mock(AdUnitConfiguration.class); + when(mockConfiguration.getNativeConfiguration()).thenReturn(mock(NativeAdUnitConfiguration.class)); + when(mockAdUnit.getConfiguration()).thenReturn(mockConfiguration); when(mockPrebidRequest.getNativeParameters()).thenReturn(mock(NativeParameters.class)); when(mockBidResponse.getTargeting()).thenReturn(keywords); when(mockAdUnit.getBidResponse()).thenReturn(mockBidResponse);