Search in sources :

Example 1 with User

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

the class Rx2GetObjectApiTest method testObjectListGetRequest404.

public void testObjectListGetRequest404() 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.get(server.url("/").toString()).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 2 with User

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

the class Rx2GetObjectApiTest method testObjectListSingleGetRequest.

public void testObjectListSingleGetRequest() 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 CountDownLatch latch = new CountDownLatch(1);
    Rx2AndroidNetworking.get(server.url("/").toString()).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) {
            firstNameRef.set(users.get(0).firstName);
            lastNameRef.set(users.get(0).lastName);
            latch.countDown();
        }

        @Override
        public void onError(@NonNull Throwable throwable) {
            assertTrue(false);
        }
    });
    assertTrue(latch.await(2, SECONDS));
    assertTrue(isSubscribedRef.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 3 with User

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

the class Rx2GetObjectApiTest method testObjectSingleGetRequest.

public void testObjectSingleGetRequest() 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 CountDownLatch latch = new CountDownLatch(1);
    Rx2AndroidNetworking.get(server.url("/").toString()).build().getObjectSingle(User.class).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new SingleObserver<User>() {

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

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

        @Override
        public void onError(@NonNull Throwable throwable) {
            assertTrue(false);
        }
    });
    assertTrue(latch.await(2, SECONDS));
    assertTrue(isSubscribedRef.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 4 with User

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

the class Rx2PostObjectApiTest method testObjectPostRequest.

public void testObjectPostRequest() 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.post(server.url("/").toString()).addBodyParameter("fistName", "Amit").addBodyParameter("lastName", "Shekhar").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 5 with User

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

the class Rx2PostObjectApiTest method testObjectListPostRequest.

public void testObjectListPostRequest() 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.post(server.url("/").toString()).addBodyParameter("fistName", "Amit").addBodyParameter("lastName", "Shekhar").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)

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