CustomEventNativeAdapter { public static void loadNativeAd(@NonNull final Context context, @NonNull final Map<String, Object> localExtras, @NonNull final AdResponse adResponse, @NonNull final CustomEventNative.CustomEventNativeListener customEventNativeListener) { final CustomEventNative customEventNative; String customEventNativeClassName = adResponse.getCustomEventClassName(); try { customEventNative = CustomEventNativeFactory.create(customEventNativeClassName); } catch (Exception e) { MoPubLog.w("Failed to load Custom Event Native class: " + customEventNativeClassName); customEventNativeListener.onNativeAdFailed(NativeErrorCode.NATIVE_ADAPTER_NOT_FOUND); return; } if (adResponse.hasJson()) { localExtras.put(DataKeys.JSON_BODY_KEY, adResponse.getJsonBody()); } customEventNative.loadNativeAd( context, customEventNativeListener, localExtras, adResponse.getServerExtras() ); } private  CustomEventNativeAdapter(); static void loadNativeAd(@NonNull final Context context,
            @NonNull final Map<String, Object> localExtras,
            @NonNull final AdResponse adResponse,
            @NonNull final CustomEventNative.CustomEventNativeListener customEventNativeListener);  }
@Test public void loadNativeAd_withValidInput_shouldCallLoadNativeAdOnTheCustomEvent() throws Exception { CustomEventNativeAdapter.loadNativeAd(context, localExtras, testAdResponse, mCustomEventNativeListener); verify(mCustomEventNative).loadNativeAd(context, mCustomEventNativeListener, localExtras, serverExtras); verify(mCustomEventNativeListener, never()).onNativeAdFailed(any(NativeErrorCode.class)); verify(mCustomEventNativeListener, never()).onNativeAdLoaded(any(NativeAdInterface.class)); }
@Test public void loadNativeAd_withInvalidClassName_shouldNotifyListenerOfOnNativeAdFailedAndReturn() throws Exception { testAdResponse = testAdResponse.toBuilder() .setCustomEventClassName("com.mopub.baaad.invalidinvalid123143") .build(); CustomEventNativeAdapter.loadNativeAd(context, localExtras, testAdResponse, mCustomEventNativeListener); verify(mCustomEventNativeListener).onNativeAdFailed(NativeErrorCode.NATIVE_ADAPTER_NOT_FOUND); verify(mCustomEventNativeListener, never()).onNativeAdLoaded(any(NativeAdInterface.class)); verify(mCustomEventNative, never()).loadNativeAd(context, mCustomEventNativeListener, localExtras, serverExtras); }
@Test public void loadNativeAd_withInvalidCustomEventNativeData_shouldNotAddToServerExtras() throws Exception { testAdResponse = testAdResponse.toBuilder() .setServerExtras(null) .build(); CustomEventNativeAdapter.loadNativeAd(context, localExtras, testAdResponse, mCustomEventNativeListener); verify(mCustomEventNative).loadNativeAd(eq(context), eq(mCustomEventNativeListener), eq(localExtras), eq(new HashMap<String, String>())); verify(mCustomEventNativeListener, never()).onNativeAdFailed(any(NativeErrorCode.class)); verify(mCustomEventNativeListener, never()).onNativeAdLoaded(any(NativeAdInterface.class)); }