Skip to content

Commit

Permalink
Add GET object and responseBody test for jackson
Browse files Browse the repository at this point in the history
  • Loading branch information
amitshekhariitbhu committed May 10, 2017
1 parent bfb75b8 commit 433b37a
Showing 1 changed file with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.androidnetworking.common.ANRequest;
import com.androidnetworking.common.ANResponse;
import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.OkHttpResponseAndParsedRequestListener;
import com.androidnetworking.interfaces.ParsedRequestListener;
import com.jacksonandroidnetworking.model.User;

Expand All @@ -35,8 +36,10 @@

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

import okhttp3.Response;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;

Expand Down Expand Up @@ -263,5 +266,77 @@ public void testSynchronousObjectListGetRequest404() throws InterruptedException

}

public void testResponseBodyAndObjectGet() 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> responseBodySuccess = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);

AndroidNetworking.get(server.url("/").toString())
.setExecutor(Executors.newSingleThreadExecutor())
.build()
.getAsOkHttpResponseAndObject(User.class,
new OkHttpResponseAndParsedRequestListener<User>() {
@Override
public void onResponse(Response okHttpResponse, User user) {
firstNameRef.set(user.firstName);
lastNameRef.set(user.lastName);
responseBodySuccess.set(okHttpResponse.isSuccessful());
latch.countDown();
}

@Override
public void onError(ANError anError) {
assertTrue(false);
}
});

assertTrue(latch.await(2, SECONDS));

assertTrue(responseBodySuccess.get());
assertEquals("Amit", firstNameRef.get());
assertEquals("Shekhar", lastNameRef.get());
}

public void testResponseBodyAndObjectGet404() throws InterruptedException {

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

final AtomicReference<String> errorBodyRef = new AtomicReference<>();
final AtomicReference<Integer> errorCodeRef = new AtomicReference<>();
final AtomicReference<String> errorDetailRef = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);

AndroidNetworking.get(server.url("/").toString())
.setExecutor(Executors.newSingleThreadExecutor())
.build()
.getAsOkHttpResponseAndObject(User.class,
new OkHttpResponseAndParsedRequestListener<User>() {
@Override
public void onResponse(Response okHttpResponse, User user) {
assertTrue(false);
}

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

assertTrue(latch.await(2, SECONDS));

assertEquals(ANConstants.RESPONSE_FROM_SERVER_ERROR, errorDetailRef.get());

assertEquals("data", errorBodyRef.get());

assertEquals(404, errorCodeRef.get().intValue());
}


}

0 comments on commit 433b37a

Please sign in to comment.