Search in sources :

Example 11 with User

use of com.rx2androidnetworking.model.User in project Fast-Android-Networking by amitshekhariitbhu.

the class Rx2MultipartObjectApiTest method testObjectListMultipartRequest404.

public void testObjectListMultipartRequest404() throws InterruptedException {
    server.enqueue(new MockResponse().setResponseCode(404).setBody("data"));
    final AtomicReference<String> errorDetailRef = new AtomicReference<>();
    final AtomicReference<String> errorBodyRef = new AtomicReference<>();
    final AtomicReference<Integer> errorCodeRef = new AtomicReference<>();
    final AtomicReference<Boolean> isSubscribedRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    Rx2AndroidNetworking.upload(server.url("/").toString()).addMultipartParameter("key", "value").build().getObjectListObservable(User.class).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<List<User>>() {

        @Override
        public void onSubscribe(Disposable d) {
            isSubscribedRef.set(true);
        }

        @Override
        public void onNext(List<User> userList) {
            assertTrue(false);
        }

        @Override
        public void onError(Throwable e) {
            ANError anError = (ANError) e;
            errorBodyRef.set(anError.getErrorBody());
            errorDetailRef.set(anError.getErrorDetail());
            errorCodeRef.set(anError.getErrorCode());
            latch.countDown();
        }

        @Override
        public void onComplete() {
            assertTrue(false);
        }
    });
    assertTrue(latch.await(2, SECONDS));
    assertTrue(isSubscribedRef.get());
    assertEquals(ANConstants.RESPONSE_FROM_SERVER_ERROR, errorDetailRef.get());
    assertEquals("data", errorBodyRef.get());
    assertEquals(404, errorCodeRef.get().intValue());
}
Also used : Disposable(io.reactivex.disposables.Disposable) MockResponse(okhttp3.mockwebserver.MockResponse) User(com.rx2androidnetworking.model.User) AtomicReference(java.util.concurrent.atomic.AtomicReference) ANError(com.androidnetworking.error.ANError) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List)

Example 12 with User

use of com.rx2androidnetworking.model.User in project Fast-Android-Networking by amitshekhariitbhu.

the class Rx2MultipartObjectApiTest method testObjectListSingleMultipartRequest404.

public void testObjectListSingleMultipartRequest404() throws InterruptedException {
    server.enqueue(new MockResponse().setResponseCode(404).setBody("data"));
    final AtomicReference<String> errorDetailRef = new AtomicReference<>();
    final AtomicReference<String> errorBodyRef = new AtomicReference<>();
    final AtomicReference<Integer> errorCodeRef = new AtomicReference<>();
    final AtomicReference<Boolean> isSubscribedRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    Rx2AndroidNetworking.upload(server.url("/").toString()).addMultipartParameter("key", "value").build().getObjectListSingle(User.class).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new SingleObserver<List<User>>() {

        @Override
        public void onSubscribe(@NonNull Disposable disposable) {
            isSubscribedRef.set(true);
        }

        @Override
        public void onSuccess(@NonNull List<User> users) {
            assertTrue(false);
        }

        @Override
        public void onError(@NonNull Throwable e) {
            ANError anError = (ANError) e;
            errorBodyRef.set(anError.getErrorBody());
            errorDetailRef.set(anError.getErrorDetail());
            errorCodeRef.set(anError.getErrorCode());
            latch.countDown();
        }
    });
    assertTrue(latch.await(2, SECONDS));
    assertTrue(isSubscribedRef.get());
    assertEquals(ANConstants.RESPONSE_FROM_SERVER_ERROR, errorDetailRef.get());
    assertEquals("data", errorBodyRef.get());
    assertEquals(404, errorCodeRef.get().intValue());
}
Also used : Disposable(io.reactivex.disposables.Disposable) MockResponse(okhttp3.mockwebserver.MockResponse) User(com.rx2androidnetworking.model.User) AtomicReference(java.util.concurrent.atomic.AtomicReference) ANError(com.androidnetworking.error.ANError) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List)

Example 13 with User

use of com.rx2androidnetworking.model.User in project Fast-Android-Networking by amitshekhariitbhu.

the class Rx2MultipartObjectApiTest method testObjectListMultipartRequest.

public void testObjectListMultipartRequest() throws InterruptedException {
    server.enqueue(new MockResponse().setBody("[{\"firstName\":\"Amit\", \"lastName\":\"Shekhar\"}]"));
    final AtomicReference<String> firstNameRef = new AtomicReference<>();
    final AtomicReference<String> lastNameRef = new AtomicReference<>();
    final AtomicReference<Boolean> isSubscribedRef = new AtomicReference<>();
    final AtomicReference<Boolean> isCompletedRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(2);
    Rx2AndroidNetworking.upload(server.url("/").toString()).addMultipartParameter("key", "value").build().getObjectListObservable(User.class).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<List<User>>() {

        @Override
        public void onSubscribe(Disposable d) {
            isSubscribedRef.set(true);
        }

        @Override
        public void onNext(List<User> userList) {
            firstNameRef.set(userList.get(0).firstName);
            lastNameRef.set(userList.get(0).lastName);
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            assertTrue(false);
        }

        @Override
        public void onComplete() {
            isCompletedRef.set(true);
            latch.countDown();
        }
    });
    assertTrue(latch.await(2, SECONDS));
    assertTrue(isSubscribedRef.get());
    assertTrue(isCompletedRef.get());
    assertEquals("Amit", firstNameRef.get());
    assertEquals("Shekhar", lastNameRef.get());
}
Also used : Disposable(io.reactivex.disposables.Disposable) MockResponse(okhttp3.mockwebserver.MockResponse) User(com.rx2androidnetworking.model.User) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List)

Example 14 with User

use of com.rx2androidnetworking.model.User in project Fast-Android-Networking by amitshekhariitbhu.

the class Rx2MultipartObjectApiTest method testObjectMultipartRequest.

public void testObjectMultipartRequest() throws InterruptedException {
    server.enqueue(new MockResponse().setBody("{\"firstName\":\"Amit\", \"lastName\":\"Shekhar\"}"));
    final AtomicReference<String> firstNameRef = new AtomicReference<>();
    final AtomicReference<String> lastNameRef = new AtomicReference<>();
    final AtomicReference<Boolean> isSubscribedRef = new AtomicReference<>();
    final AtomicReference<Boolean> isCompletedRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(2);
    Rx2AndroidNetworking.upload(server.url("/").toString()).addMultipartParameter("key", "value").build().getObjectObservable(User.class).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<User>() {

        @Override
        public void onSubscribe(Disposable d) {
            isSubscribedRef.set(true);
        }

        @Override
        public void onNext(User user) {
            firstNameRef.set(user.firstName);
            lastNameRef.set(user.lastName);
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            assertTrue(false);
        }

        @Override
        public void onComplete() {
            isCompletedRef.set(true);
            latch.countDown();
        }
    });
    assertTrue(latch.await(2, SECONDS));
    assertTrue(isSubscribedRef.get());
    assertTrue(isCompletedRef.get());
    assertEquals("Amit", firstNameRef.get());
    assertEquals("Shekhar", lastNameRef.get());
}
Also used : Disposable(io.reactivex.disposables.Disposable) MockResponse(okhttp3.mockwebserver.MockResponse) User(com.rx2androidnetworking.model.User) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 15 with User

use of com.rx2androidnetworking.model.User in project Fast-Android-Networking by amitshekhariitbhu.

the class Rx2GetObjectApiTest method testObjectGetRequest.

public void testObjectGetRequest() throws InterruptedException {
    server.enqueue(new MockResponse().setBody("{\"firstName\":\"Amit\", \"lastName\":\"Shekhar\"}"));
    final AtomicReference<String> firstNameRef = new AtomicReference<>();
    final AtomicReference<String> lastNameRef = new AtomicReference<>();
    final AtomicReference<Boolean> isSubscribedRef = new AtomicReference<>();
    final AtomicReference<Boolean> isCompletedRef = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(2);
    Rx2AndroidNetworking.get(server.url("/").toString()).build().getObjectObservable(User.class).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<User>() {

        @Override
        public void onSubscribe(Disposable d) {
            isSubscribedRef.set(true);
        }

        @Override
        public void onNext(User user) {
            firstNameRef.set(user.firstName);
            lastNameRef.set(user.lastName);
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            assertTrue(false);
        }

        @Override
        public void onComplete() {
            isCompletedRef.set(true);
            latch.countDown();
        }
    });
    assertTrue(latch.await(2, SECONDS));
    assertTrue(isSubscribedRef.get());
    assertTrue(isCompletedRef.get());
    assertEquals("Amit", firstNameRef.get());
    assertEquals("Shekhar", lastNameRef.get());
}
Also used : Disposable(io.reactivex.disposables.Disposable) MockResponse(okhttp3.mockwebserver.MockResponse) User(com.rx2androidnetworking.model.User) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

User (com.rx2androidnetworking.model.User)24 Disposable (io.reactivex.disposables.Disposable)24 CountDownLatch (java.util.concurrent.CountDownLatch)24 AtomicReference (java.util.concurrent.atomic.AtomicReference)24 MockResponse (okhttp3.mockwebserver.MockResponse)24 ANError (com.androidnetworking.error.ANError)12 List (java.util.List)12