/*
 *
 *  *    Copyright (C) 2016 Amit Shekhar
 *  *    Copyright (C) 2011 Android Open Source Project
 *  *
 *  *    Licensed under the Apache License, Version 2.0 (the "License");
 *  *    you may not use this file except in compliance with the License.
 *  *    You may obtain a copy of the License at
 *  *
 *  *        http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *    Unless required by applicable law or agreed to in writing, software
 *  *    distributed under the License is distributed on an "AS IS" BASIS,
 *  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *    See the License for the specific language governing permissions and
 *  *    limitations under the License.
 *
 */

package com.rx2androidnetworking;

import android.app.Application;
import android.test.ApplicationTestCase;

import org.junit.Rule;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;

import static java.util.concurrent.TimeUnit.SECONDS;

/**
 * Created by amitshekhar on 22/04/17.
 */

public class Rx2GetStringApiTest extends ApplicationTestCase<Application> {

    @Rule
    public final MockWebServer server = new MockWebServer();

    public Rx2GetStringApiTest() {
        super(Application.class);
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        createApplication();
    }

    public void testStringGetRequest() throws InterruptedException {

        server.enqueue(new MockResponse().setBody("data"));

        final AtomicReference<String> responseRef = 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()
                .getStringObservable()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        isSubscribedRef.set(true);
                    }

                    @Override
                    public void onNext(String response) {
                        responseRef.set(response);
                        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("data", responseRef.get());
    }

}

@@ -830,24 +830,118 @@
 nTestCase;%0A%0A
+import com.androidnetworking.common.ANConstants;%0Aimport com.androidnetworking.error.ANError;%0A%0A
 import org.j
@@ -3390,10 +3390,2008 @@
 %0A    %7D%0A%0A
+    public void testStringGetRequest404() throws InterruptedException %7B%0A%0A        server.enqueue(new MockResponse().setResponseCode(404).setBody(%22data%22));%0A%0A        final AtomicReference%3CString%3E errorDetailRef = new AtomicReference%3C%3E();%0A        final AtomicReference%3CString%3E errorBodyRef = new AtomicReference%3C%3E();%0A        final AtomicReference%3CInteger%3E errorCodeRef = new AtomicReference%3C%3E();%0A        final AtomicReference%3CBoolean%3E isSubscribedRef = new AtomicReference%3C%3E();%0A        final CountDownLatch latch = new CountDownLatch(1);%0A%0A        Rx2AndroidNetworking.get(server.url(%22/%22).toString())%0A                .build()%0A                .getStringObservable()%0A                .subscribeOn(Schedulers.io())%0A                .observeOn(AndroidSchedulers.mainThread())%0A                .subscribe(new Observer%3CString%3E() %7B%0A                    @Override%0A                    public void onSubscribe(Disposable d) %7B%0A                        isSubscribedRef.set(true);%0A                    %7D%0A%0A                    @Override%0A                    public void onNext(String response) %7B%0A                        assertTrue(false);%0A                    %7D%0A%0A                    @Override%0A                    public void onError(Throwable e) %7B%0A                        ANError anError = (ANError) e;%0A                        errorBodyRef.set(anError.getErrorBody());%0A                        errorDetailRef.set(anError.getErrorDetail());%0A                        errorCodeRef.set(anError.getErrorCode());%0A                        latch.countDown();%0A                    %7D%0A%0A                    @Override%0A                    public void onComplete() %7B%0A                        assertTrue(false);%0A                    %7D%0A                %7D);%0A%0A        assertTrue(latch.await(2, SECONDS));%0A%0A        assertTrue(isSubscribedRef.get());%0A%0A        assertEquals(ANConstants.RESPONSE_FROM_SERVER_ERROR, errorDetailRef.get());%0A%0A        assertEquals(%22data%22, errorBodyRef.get());%0A%0A        assertEquals(404, errorCodeRef.get().intValue());%0A%0A    %7D%0A%0A
 %7D%0A

Add GET 404 String RxJava Test