retrofit2.mock.NetworkBehavior Java Examples
The following examples show how to use
retrofit2.mock.NetworkBehavior.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CallAdapter.java From Mockery with Apache License 2.0 | 6 votes |
<T> T adapt(Method method, NetworkBehavior networkBehavior, Call<?> call) { try { Class<?> behaviorCallClass = Class.forName("retrofit2.mock.BehaviorCall"); Constructor<?> constructor = behaviorCallClass.getDeclaredConstructor(NetworkBehavior.class, ExecutorService.class, Call.class); constructor.setAccessible(true); Call<?> behaviorCall = (Call<?>) constructor.newInstance(networkBehavior, Executors.newCachedThreadPool(), call); return (T) retrofit .callAdapter(method.getGenericReturnType(), method.getAnnotations()) .adapt(behaviorCall); } catch (Exception e) { throw new RuntimeException(e); } }
Example #2
Source File: ApiModule.java From AndroidBlueprints with Apache License 2.0 | 6 votes |
@Provides @Singleton MockRetrofit provideRetrofit(GsonConverterFactory gson, OkHttpClient okHttpClient) { NetworkBehavior behavior = NetworkBehavior.create(); behavior.setDelay(1, TimeUnit.SECONDS); behavior.setVariancePercent(40); behavior.setErrorPercent(2); Retrofit retrofit = new Retrofit.Builder() .baseUrl(BuildConfig.API_URL) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addCallAdapterFactory(LiveDataCallAdapterFactory.create()) .client(okHttpClient) .build(); return new MockRetrofit.Builder(retrofit) .networkBehavior(behavior) .build(); }
Example #3
Source File: RetrofitInterceptor.java From Mockery with Apache License 2.0 | 5 votes |
private NetworkBehavior networkBehaviour(Metadata<Retrofit> metadata) { Retrofit retrofit = metadata.getAnnotation(); NetworkBehavior networkBehavior = NetworkBehavior.create(); networkBehavior.setDelay(retrofit.delay(), TimeUnit.MILLISECONDS); networkBehavior.setVariancePercent(retrofit.variancePercentage()); networkBehavior.setFailurePercent(retrofit.failurePercent()); return networkBehavior; }
Example #4
Source File: DebugDataModule.java From u2020-mvp with Apache License 2.0 | 5 votes |
@Provides @ApplicationScope Picasso providePicasso(OkHttpClient client, NetworkBehavior behavior, @IsMockMode boolean isMockMode, Application app) { Picasso.Builder builder = new Picasso.Builder(app).downloader(new OkHttp3Downloader(client)); if (isMockMode) { builder.addRequestHandler(new MockRequestHandler(behavior, app.getAssets())); } builder.listener((picasso, uri, exception) -> Timber.e(exception, "Error while loading image %s", uri)); return builder.build(); }
Example #5
Source File: DebugApiModule.java From u2020-mvp with Apache License 2.0 | 5 votes |
@Provides @ApplicationScope MockRetrofit provideMockRetrofit(Retrofit retrofit, NetworkBehavior behavior) { return new MockRetrofit.Builder(retrofit) .networkBehavior(behavior) .build(); }
Example #6
Source File: DebugApiModule.java From u2020-mvp with Apache License 2.0 | 5 votes |
@Provides @ApplicationScope NetworkBehavior provideBehavior(@NetworkDelay Preference<Long> networkDelay, @NetworkFailurePercent Preference<Integer> networkFailurePercent, @NetworkVariancePercent Preference<Integer> networkVariancePercent) { NetworkBehavior behavior = NetworkBehavior.create(); behavior.setDelay(networkDelay.get(), MILLISECONDS); behavior.setFailurePercent(networkFailurePercent.get()); behavior.setVariancePercent(networkVariancePercent.get()); return behavior; }
Example #7
Source File: QuoteOfTheDayMockAdapterTest.java From android-retrofit-test-examples with MIT License | 5 votes |
@Override public void setUp() throws Exception { super.setUp(); retrofit = new Retrofit.Builder().baseUrl("http://test.com") .client(new OkHttpClient()) .addConverterFactory(JacksonConverterFactory.create()) .build(); NetworkBehavior behavior = NetworkBehavior.create(); mockRetrofit = new MockRetrofit.Builder(retrofit) .networkBehavior(behavior) .build(); }
Example #8
Source File: Helpers.java From quill with MIT License | 5 votes |
private static NetworkBehavior getNetworkBehavior(int delayMsec, int delayVariance, int failurePercent, int errorPercent) { NetworkBehavior networkBehavior = NetworkBehavior.create(); networkBehavior.setDelay(delayMsec, TimeUnit.MILLISECONDS); networkBehavior.setVariancePercent(delayVariance); // "failure" means network layer failure networkBehavior.setFailurePercent(failurePercent); // "error" means HTTP error networkBehavior.setErrorPercent(errorPercent); return networkBehavior; }
Example #9
Source File: DebugApiModule.java From u2020 with Apache License 2.0 | 5 votes |
@Provides @Singleton NetworkBehavior provideBehavior(@NetworkDelay Preference<Long> networkDelay, @NetworkVariancePercent Preference<Integer> networkVariancePercent, @NetworkFailurePercent Preference<Integer> networkFailurePercent, @NetworkErrorPercent Preference<Integer> networkErrorPercent, Preference<NetworkErrorCode> networkErrorCode) { NetworkBehavior behavior = NetworkBehavior.create(); behavior.setDelay(networkDelay.get(), MILLISECONDS); behavior.setVariancePercent(networkVariancePercent.get()); behavior.setFailurePercent(networkFailurePercent.get()); behavior.setErrorPercent(networkErrorPercent.get()); behavior.setErrorFactory( () -> Response.error(networkErrorCode.get().code, ResponseBody.create(null, new byte[0]))); return behavior; }
Example #10
Source File: DebugDataModule.java From u2020 with Apache License 2.0 | 5 votes |
@Provides @Singleton Picasso providePicasso(OkHttpClient client, NetworkBehavior behavior, @IsMockMode boolean isMockMode, Application app) { Picasso.Builder builder = new Picasso.Builder(app).downloader(new OkHttp3Downloader(client)); if (isMockMode) { builder.addRequestHandler(new MockRequestHandler(behavior, app.getAssets())); } builder.listener((picasso, uri, exception) -> { Timber.e(exception, "Error while loading image %s", uri); }); return builder.build(); }
Example #11
Source File: LoginOrchestratorTest.java From quill with MIT License | 5 votes |
private static LoginOrchestrator makeOrchestrator(CredentialSource credSource, CredentialSink credSink, boolean useGhostAuth, NetworkBehavior networkBehavior) { BlogUrlValidator blogUrlValidator = blogUrl -> Observable.just("http://" + blogUrl); HACKListener hackListener = mock(HACKListener.class); final MockApiProviderFactory apiProviderFactory = new MockApiProviderFactory( useGhostAuth, networkBehavior); return new LoginOrchestrator(blogUrlValidator, apiProviderFactory, credSource, credSink, hackListener); }
Example #12
Source File: LoginOrchestratorTest.java From quill with MIT License | 5 votes |
@Test public void ghostAuth_networkFailure() { NetworkBehavior failingNetworkBehavior = Helpers.getFailingNetworkBehaviour(); LoginOrchestrator orchestrator = makeOrchestrator(credSource, credSink, true, failingNetworkBehavior); when(credSource.getGhostAuthCode(any())).thenReturn(Observable.just("auth-code")); orchestrator.listen(listener); orchestrator.start(BLOG_URL_WITHOUT_PROTOCOL); verify(listener).onNetworkError(any(), argThat(sameInstance(failingNetworkBehavior.failureException()))); }
Example #13
Source File: RetrofitInterceptor.java From Mockery with Apache License 2.0 | 5 votes |
@Override public Call onIllegalMock(AssertionError assertionError, Metadata<Retrofit> metadata) { checkReturnMethodTypeIsCall(metadata); String errorMessage = assertionError.getMessage() != null ? assertionError.getMessage() : ""; String safeErrorMessage = adaptErrorResponse(errorMessage, metadata); ResponseBody responseBody = ResponseBody .create(MediaType.parse("application/json"), safeErrorMessage); NetworkBehavior networkBehavior = networkBehaviour(metadata); return callAdapter.adapt(metadata.getMethod(), networkBehavior, Calls.response(Response.error(404, responseBody))); }
Example #14
Source File: RetrofitInterceptor.java From Mockery with Apache License 2.0 | 5 votes |
@Override public Call onLegalMock(Object mock, Metadata<Retrofit> metadata) { checkReturnMethodTypeIsCall(metadata); checkTypeMockIsNotCall(metadata, mock); NetworkBehavior networkBehavior = networkBehaviour(metadata); return callAdapter.adapt(metadata.getMethod(), networkBehavior, Calls.response(mock)); }
Example #15
Source File: Rx2RetrofitInterceptor.java From Mockery with Apache License 2.0 | 5 votes |
private NetworkBehavior networkBehaviour(Metadata<Rx2Retrofit> metadata) { Rx2Retrofit retrofit = metadata.getAnnotation(); NetworkBehavior networkBehavior = NetworkBehavior.create(); networkBehavior.setDelay(retrofit.delay(), TimeUnit.MILLISECONDS); networkBehavior.setVariancePercent(retrofit.variancePercentage()); networkBehavior.setFailurePercent(retrofit.failurePercent()); return networkBehavior; }
Example #16
Source File: Rx2RetrofitInterceptor.java From Mockery with Apache License 2.0 | 5 votes |
@Override public Object onIllegalMock(final AssertionError assertionError, final Metadata<Rx2Retrofit> metadata) { checkReturnMethodTypeIsSingleOrCompletable(metadata); final String errorMessage = assertionError.getMessage() != null ? assertionError.getMessage() : ""; final String adaptedErrorMessage = adaptErrorResponse(errorMessage, metadata); NetworkBehavior networkBehavior = networkBehaviour(metadata); final ResponseBody responseBody = ResponseBody .create(MediaType.parse("application/json"), adaptedErrorMessage); return callAdapter.adapt(metadata.getMethod(), networkBehavior, Calls.response(Response.error(404, responseBody))); }
Example #17
Source File: Rx2RetrofitInterceptor.java From Mockery with Apache License 2.0 | 5 votes |
@Override public Object onLegalMock(final Object mock, final Metadata<Rx2Retrofit> metadata) { checkReturnMethodTypeIsSingleOrCompletable(metadata); checkTypeMockIsNotSingleNeitherResponse(metadata, mock); NetworkBehavior networkBehavior = networkBehaviour(metadata); return callAdapter.adapt(metadata.getMethod(), networkBehavior, Calls.response(mock)); }
Example #18
Source File: RxRetrofitInterceptor.java From Mockery with Apache License 2.0 | 5 votes |
private NetworkBehavior networkBehaviour(Metadata<RxRetrofit> metadata) { RxRetrofit retrofit = metadata.getAnnotation(); NetworkBehavior networkBehavior = NetworkBehavior.create(); networkBehavior.setDelay(retrofit.delay(), TimeUnit.MILLISECONDS); networkBehavior.setVariancePercent(retrofit.variancePercentage()); networkBehavior.setFailurePercent(retrofit.failurePercent()); return networkBehavior; }
Example #19
Source File: RxRetrofitInterceptor.java From Mockery with Apache License 2.0 | 5 votes |
@Override public Observable onIllegalMock(final AssertionError assertionError, final Metadata<RxRetrofit> metadata) { checkReturnMethodTypeIsObservable(metadata); final String errorMessage = assertionError.getMessage() != null ? assertionError.getMessage() : ""; final String adaptedErrorMessage = adaptErrorResponse(errorMessage, metadata); NetworkBehavior networkBehavior = networkBehaviour(metadata); final ResponseBody responseBody = ResponseBody .create(MediaType.parse("application/json"), adaptedErrorMessage); return callAdapter.adapt(metadata.getMethod(), networkBehavior, Calls.response(Response.error(404, responseBody))); }
Example #20
Source File: RxRetrofitInterceptor.java From Mockery with Apache License 2.0 | 5 votes |
@Override public Observable onLegalMock(final Object mock, final Metadata<RxRetrofit> metadata) { checkReturnMethodTypeIsObservable(metadata); checkTypeMockIsNotObservableNeitherResponse(metadata, mock); NetworkBehavior networkBehavior = networkBehaviour(metadata); return callAdapter.adapt(metadata.getMethod(), networkBehavior, Calls.response(mock)); }
Example #21
Source File: LyftButtonCallManagerTest.java From lyft-android-sdk with Apache License 2.0 | 5 votes |
@Before public void setup() { Retrofit retrofit = new Retrofit.Builder() .baseUrl(LyftApi.API_ROOT) .build(); NetworkBehavior behavior = NetworkBehavior.create(); behavior.setDelay(1, TimeUnit.MILLISECONDS); behavior.setVariancePercent(0); behavior.setFailurePercent(0); MockRetrofit mockRetrofit = new MockRetrofit.Builder(retrofit) .networkBehavior(behavior) .build(); BehaviorDelegate<LyftApi> lyftApidelegate = mockRetrofit.create(LyftApi.class); lyftPublicApi = spy(new MockLyftApi(lyftApidelegate)); BehaviorDelegate<GoogleApi> googleApiDelegate = mockRetrofit.create(GoogleApi.class); googleApi = spy(new MockGoogleApi(googleApiDelegate)); HashMap<String, GoogleLatLng> addressToLatLngMap = new HashMap<>(); addressToLatLngMap.put(PICKUP_ADDR, new GoogleLatLng(PICKUP_LAT, PICKUP_LNG)); addressToLatLngMap.put(DROPOFF_ADDR, new GoogleLatLng(DROPOFF_LAT, DROPOFF_LNG)); googleApi.setAddressToLatLngMap(addressToLatLngMap); mockCallback = mock(LyftButton.ResultLoadedCallback.class); callManager = new LyftButtonCallManager("clientId", lyftPublicApi, googleApi, new MockExecutorService()); }
Example #22
Source File: MockRequestHandler.java From u2020 with Apache License 2.0 | 4 votes |
public MockRequestHandler(NetworkBehavior behavior, AssetManager assetManager) { this.behavior = behavior; this.assetManager = assetManager; }
Example #23
Source File: DebugApiModule.java From u2020 with Apache License 2.0 | 4 votes |
@Provides @Singleton MockRetrofit provideMockRetrofit(Retrofit retrofit, NetworkBehavior behavior) { return new MockRetrofit.Builder(retrofit) .networkBehavior(behavior) .build(); }
Example #24
Source File: MockRequestHandler.java From u2020-mvp with Apache License 2.0 | 4 votes |
public MockRequestHandler(NetworkBehavior behavior, AssetManager assetManager) { this.behavior = behavior; this.assetManager = assetManager; }
Example #25
Source File: Helpers.java From quill with MIT License | 4 votes |
public static NetworkBehavior getFailingNetworkBehaviour() { return getNetworkBehavior(0, 0, 100, 0); }
Example #26
Source File: Helpers.java From quill with MIT License | 4 votes |
public static NetworkBehavior getIdealNetworkBehavior() { return getNetworkBehavior(0, 0, 0, 0); }
Example #27
Source File: Helpers.java From quill with MIT License | 4 votes |
@NonNull public static MockRetrofit getMockRetrofit(Retrofit retrofit, NetworkBehavior networkBehavior) { return new MockRetrofit.Builder(retrofit) .networkBehavior(networkBehavior) .build(); }
Example #28
Source File: LoginOrchestratorTest.java From quill with MIT License | 4 votes |
public MockApiProviderFactory(boolean useGhostAuth, NetworkBehavior networkBehavior) { mUseGhostAuth = useGhostAuth; mNetworkBehavior = networkBehavior; }