CustomEventNativeAdapter { public static void loadNativeAd(@NonNull final Context context, @NonNull final Map<String, Object> localExtras, @NonNull final DownloadResponse downloadResponse, @NonNull final CustomEventNative.CustomEventNativeListener customEventNativeListener) { final String customEventNativeData = downloadResponse.getFirstHeader(ResponseHeader.CUSTOM_EVENT_DATA); final String customEventNativeClassName = downloadResponse.getFirstHeader(ResponseHeader.CUSTOM_EVENT_NAME); final CustomEventNative customEventNative; 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; } Map<String, String> serverExtras = new HashMap<String, String>(); try { serverExtras = Json.jsonStringToMap(customEventNativeData); } catch (Exception e) { MoPubLog.w("Failed to create Map from JSON: " + customEventNativeData, e); } serverExtras.put(RESPONSE_BODY_KEY, HttpResponses.asResponseString(downloadResponse)); customEventNative.loadNativeAd( context, customEventNativeListener, localExtras, serverExtras ); } private  CustomEventNativeAdapter(); static void loadNativeAd(@NonNull final Context context,
            @NonNull final Map<String, Object> localExtras,
            @NonNull final DownloadResponse downloadResponse,
            @NonNull final CustomEventNative.CustomEventNativeListener customEventNativeListener);  }
@Test public void loadNativeAd_withValidInput_shouldCallLoadNativeAdOnTheCustomEvent() throws Exception { CustomEventNativeAdapter.loadNativeAd(context, localExtras, downloadResponse, 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 { testHttpResponseWithHeaders.addHeader(ResponseHeader.CUSTOM_EVENT_NAME.getKey(), "com.mopub.baaad.invalidinvalid123143"); downloadResponse = new DownloadResponse(testHttpResponseWithHeaders); CustomEventNativeAdapter.loadNativeAd(context, localExtras, downloadResponse, 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 { testHttpResponseWithHeaders.addHeader(ResponseHeader.CUSTOM_EVENT_DATA.getKey(), "{ \"bad json"); downloadResponse = new DownloadResponse(testHttpResponseWithHeaders); serverExtras.remove("key"); CustomEventNativeAdapter.loadNativeAd(context, localExtras, downloadResponse, mCustomEventNativeListener); verify(mCustomEventNative).loadNativeAd(context, mCustomEventNativeListener, localExtras, serverExtras); verify(mCustomEventNativeListener, never()).onNativeAdFailed(any(NativeErrorCode.class)); verify(mCustomEventNativeListener, never()).onNativeAdLoaded(any(NativeAdInterface.class)); }