@Test public void testSubmitNoAccount() { AccountManager.get(RuntimeEnvironment.application) .removeAccount(account, null, null); UserServices.Callback callback = mock(UserServices.Callback.class); userServices.submit(RuntimeEnvironment.application, "title", "content", true, callback); verify(call, never()).enqueue(any(Callback.class)); verify(callback).onDone(eq(false)); }
@Override public void submit(Context context, String title, String content, boolean isUrl, Callback callback) { Pair<String, String> credentials = AppUtils.getCredentials(context); if (credentials == null) { callback.onDone(false); return; } execute(postSubmitForm(credentials.first, credentials.second)) .flatMap(response -> response.code() != HttpURLConnection.HTTP_MOVED_TEMP ? Observable.just(response) : Observable.error(new IOException())) .flatMap(response -> { try { return Observable.just(new String[]{ response.header(HEADER_SET_COOKIE), response.body().string() }); } catch (IOException e) { return Observable.error(e); } finally { response.close(); } }) .map(array -> { array[1] = getInputValue(array[1], SUBMIT_PARAM_FNID); return array; }) .flatMap(array -> !TextUtils.isEmpty(array[1]) ? Observable.just(array) : Observable.error(new IOException())) .flatMap(array -> execute(postSubmit(title, content, isUrl, array[0], array[1]))) .flatMap(response -> response.code() == HttpURLConnection.HTTP_MOVED_TEMP ? Observable.just(Uri.parse(response.header(HEADER_LOCATION))) : Observable.error(new IOException())) .flatMap(uri -> TextUtils.equals(uri.getPath(), DEFAULT_SUBMIT_REDIRECT) ? Observable.just(true) : Observable.error(buildException(uri))) .observeOn(AndroidSchedulers.mainThread()) .subscribe(callback::onDone, callback::onError); }
UserServicesClient implements UserServices { @Override public void submit(Context context, String title, String content, boolean isUrl, Callback callback) { Pair<String, String> credentials = AppUtils.getCredentials(context); if (credentials == null) { callback.onDone(false); return; } execute(postSubmitForm(credentials.first, credentials.second)) .flatMap(response -> response.code() != HttpURLConnection.HTTP_MOVED_TEMP ? Observable.just(response) : Observable.error(new IOException())) .flatMap(response -> { try { return Observable.just(new String[]{ response.header(HEADER_SET_COOKIE), response.body().string() }); } catch (IOException e) { return Observable.error(e); } finally { response.close(); } }) .map(array -> { array[1] = getInputValue(array[1], SUBMIT_PARAM_FNID); return array; }) .flatMap(array -> !TextUtils.isEmpty(array[1]) ? Observable.just(array) : Observable.error(new IOException())) .flatMap(array -> execute(postSubmit(title, content, isUrl, array[0], array[1]))) .flatMap(response -> response.code() == HttpURLConnection.HTTP_MOVED_TEMP ? Observable.just(Uri.parse(response.header(HEADER_LOCATION))) : Observable.error(new IOException())) .flatMap(uri -> TextUtils.equals(uri.getPath(), DEFAULT_SUBMIT_REDIRECT) ? Observable.just(true) : Observable.error(buildException(uri))) .observeOn(AndroidSchedulers.mainThread()) .subscribe(callback::onDone, callback::onError); } }
UserServicesClient implements UserServices { @Override public void submit(Context context, String title, String content, boolean isUrl, Callback callback) { Pair<String, String> credentials = AppUtils.getCredentials(context); if (credentials == null) { callback.onDone(false); return; } execute(postSubmitForm(credentials.first, credentials.second)) .flatMap(response -> response.code() != HttpURLConnection.HTTP_MOVED_TEMP ? Observable.just(response) : Observable.error(new IOException())) .flatMap(response -> { try { return Observable.just(new String[]{ response.header(HEADER_SET_COOKIE), response.body().string() }); } catch (IOException e) { return Observable.error(e); } finally { response.close(); } }) .map(array -> { array[1] = getInputValue(array[1], SUBMIT_PARAM_FNID); return array; }) .flatMap(array -> !TextUtils.isEmpty(array[1]) ? Observable.just(array) : Observable.error(new IOException())) .flatMap(array -> execute(postSubmit(title, content, isUrl, array[0], array[1]))) .flatMap(response -> response.code() == HttpURLConnection.HTTP_MOVED_TEMP ? Observable.just(Uri.parse(response.header(HEADER_LOCATION))) : Observable.error(new IOException())) .flatMap(uri -> TextUtils.equals(uri.getPath(), DEFAULT_SUBMIT_REDIRECT) ? Observable.just(true) : Observable.error(buildException(uri))) .observeOn(AndroidSchedulers.mainThread()) .subscribe(callback::onDone, callback::onError); } @Inject  UserServicesClient(Call.Factory callFactory, Scheduler ioScheduler); }
UserServicesClient implements UserServices { @Override public void submit(Context context, String title, String content, boolean isUrl, Callback callback) { Pair<String, String> credentials = AppUtils.getCredentials(context); if (credentials == null) { callback.onDone(false); return; } execute(postSubmitForm(credentials.first, credentials.second)) .flatMap(response -> response.code() != HttpURLConnection.HTTP_MOVED_TEMP ? Observable.just(response) : Observable.error(new IOException())) .flatMap(response -> { try { return Observable.just(new String[]{ response.header(HEADER_SET_COOKIE), response.body().string() }); } catch (IOException e) { return Observable.error(e); } finally { response.close(); } }) .map(array -> { array[1] = getInputValue(array[1], SUBMIT_PARAM_FNID); return array; }) .flatMap(array -> !TextUtils.isEmpty(array[1]) ? Observable.just(array) : Observable.error(new IOException())) .flatMap(array -> execute(postSubmit(title, content, isUrl, array[0], array[1]))) .flatMap(response -> response.code() == HttpURLConnection.HTTP_MOVED_TEMP ? Observable.just(Uri.parse(response.header(HEADER_LOCATION))) : Observable.error(new IOException())) .flatMap(uri -> TextUtils.equals(uri.getPath(), DEFAULT_SUBMIT_REDIRECT) ? Observable.just(true) : Observable.error(buildException(uri))) .observeOn(AndroidSchedulers.mainThread()) .subscribe(callback::onDone, callback::onError); } @Inject  UserServicesClient(Call.Factory callFactory, Scheduler ioScheduler); @Override void login(String username, String password, boolean createAccount, Callback callback); @Override boolean voteUp(Context context, String itemId, Callback callback); @Override void reply(Context context, String parentId, String text, Callback callback); @Override void submit(Context context, String title, String content, boolean isUrl, Callback callback); }
UserServicesClient implements UserServices { @Override public void submit(Context context, String title, String content, boolean isUrl, Callback callback) { Pair<String, String> credentials = AppUtils.getCredentials(context); if (credentials == null) { callback.onDone(false); return; } execute(postSubmitForm(credentials.first, credentials.second)) .flatMap(response -> response.code() != HttpURLConnection.HTTP_MOVED_TEMP ? Observable.just(response) : Observable.error(new IOException())) .flatMap(response -> { try { return Observable.just(new String[]{ response.header(HEADER_SET_COOKIE), response.body().string() }); } catch (IOException e) { return Observable.error(e); } finally { response.close(); } }) .map(array -> { array[1] = getInputValue(array[1], SUBMIT_PARAM_FNID); return array; }) .flatMap(array -> !TextUtils.isEmpty(array[1]) ? Observable.just(array) : Observable.error(new IOException())) .flatMap(array -> execute(postSubmit(title, content, isUrl, array[0], array[1]))) .flatMap(response -> response.code() == HttpURLConnection.HTTP_MOVED_TEMP ? Observable.just(Uri.parse(response.header(HEADER_LOCATION))) : Observable.error(new IOException())) .flatMap(uri -> TextUtils.equals(uri.getPath(), DEFAULT_SUBMIT_REDIRECT) ? Observable.just(true) : Observable.error(buildException(uri))) .observeOn(AndroidSchedulers.mainThread()) .subscribe(callback::onDone, callback::onError); } @Inject  UserServicesClient(Call.Factory callFactory, Scheduler ioScheduler); @Override void login(String username, String password, boolean createAccount, Callback callback); @Override boolean voteUp(Context context, String itemId, Callback callback); @Override void reply(Context context, String parentId, String text, Callback callback); @Override void submit(Context context, String title, String content, boolean isUrl, Callback callback);  }