Skip to content

Commit

Permalink
ApolloParseException is thrown in case response json data field is nu…
Browse files Browse the repository at this point in the history
…ll (#471)
  • Loading branch information
sav007 authored May 2, 2017
1 parent 7af4794 commit acad7bb
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.apollographql.android.impl.httpcache.AllFilms;
import com.apollographql.android.impl.httpcache.AllPlanets;
import com.apollographql.android.impl.httpcache.type.CustomType;
import com.apollographql.android.impl.normalizer.HeroName;
import com.apollographql.apollo.api.Error;
import com.apollographql.apollo.api.Response;
import com.apollographql.apollo.exception.ApolloException;
Expand Down Expand Up @@ -76,7 +77,7 @@ public class IntegrationTest {
}

@SuppressWarnings("ConstantConditions") @Test public void allPlanetQuery() throws Exception {
server.enqueue(mockResponse("/HttpCacheTestAllPlanets.json"));
server.enqueue(mockResponse("HttpCacheTestAllPlanets.json"));

Response<AllPlanets.Data> body = apolloClient.newCall(new AllPlanets()).execute();
assertThat(body.hasErrors()).isFalse();
Expand Down Expand Up @@ -141,7 +142,7 @@ public class IntegrationTest {
}

@Test public void errorResponse() throws Exception {
server.enqueue(mockResponse("/HttpCacheTestError.json"));
server.enqueue(mockResponse("HttpCacheTestError.json"));
Response<AllPlanets.Data> body = apolloClient.newCall(new AllPlanets()).execute();
assertThat(body.hasErrors()).isTrue();
//noinspection ConstantConditions
Expand All @@ -151,7 +152,7 @@ public class IntegrationTest {
}

@Test public void allFilmsWithDate() throws Exception {
server.enqueue(mockResponse("/HttpCacheTestAllFilms.json"));
server.enqueue(mockResponse("HttpCacheTestAllFilms.json"));

Response<AllFilms.Data> body = apolloClient.newCall(new AllFilms()).execute();
assertThat(body.hasErrors()).isFalse();
Expand All @@ -173,7 +174,7 @@ public class IntegrationTest {
}

@Test public void allPlanetQueryAsync() throws Exception {
server.enqueue(mockResponse("/HttpCacheTestAllPlanets.json"));
server.enqueue(mockResponse("HttpCacheTestAllPlanets.json"));
final NamedCountDownLatch latch = new NamedCountDownLatch("latch", 1);
apolloClient.newCall(new AllPlanets()).enqueue(new ApolloCall.Callback<AllPlanets.Data>() {
@Override public void onResponse(@Nonnull Response<AllPlanets.Data> response) {
Expand All @@ -190,7 +191,27 @@ public class IntegrationTest {
latch.awaitOrThrowWithTimeout(TIME_OUT_SECONDS, TimeUnit.SECONDS);
}

@Test public void dataEmpty() throws Exception {
MockResponse mockResponse = mockResponse("ResponseDataEmpty.json");
server.enqueue(mockResponse);

ApolloCall<HeroName.Data> call = apolloClient.newCall(new HeroName());
Response<HeroName.Data> body = call.execute();
assertThat(body.data()).isNotNull();
assertThat(body.hasErrors()).isFalse();
}

@Test public void dataNull() throws Exception {
MockResponse mockResponse = mockResponse("ResponseDataNull.json");
server.enqueue(mockResponse);

ApolloCall<HeroName.Data> call = apolloClient.newCall(new HeroName());
Response<HeroName.Data> body = call.execute();
assertThat(body.data()).isNull();
assertThat(body.hasErrors()).isFalse();
}

private MockResponse mockResponse(String fileName) throws IOException {
return new MockResponse().setChunkedBody(Utils.readFileToString(getClass(), fileName), 32);
return new MockResponse().setChunkedBody(Utils.readFileToString(getClass(), "/" + fileName), 32);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,6 @@ private MockResponse mockResponse(String fileName) throws IOException, ApolloExc
assertThat(heroRecord.field("name")).isEqualTo("R2-D2");
}

@Test public void testHeroNameNullable() throws IOException, ApolloException {
MockResponse mockResponse = mockResponse("HeroNameNullHeroResponse.json");
server.enqueue(mockResponse);

ApolloCall<HeroName.Data> call = apolloClient.newCall(new HeroName());
Response<HeroName.Data> body = call.execute();
assertThat(body.hasErrors()).isFalse();
}

@Test
public void testHeroNameWithVariable() throws IOException, ApolloException {
MockResponse mockResponse = mockResponse("EpisodeHeroNameResponse.json");
Expand Down
3 changes: 3 additions & 0 deletions apollo-integration/src/test/resources/ResponseDataNull.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"data": null
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Response<W> parse(ResponseBody responseBody,
String name = responseStreamReader.nextName();
if ("data".equals(name)) {
//noinspection unchecked
data = (D) responseStreamReader.nextObject(false, new ResponseJsonStreamReader.ObjectReader<Object>() {
data = (D) responseStreamReader.nextObject(true, new ResponseJsonStreamReader.ObjectReader<Object>() {
@Override public Object read(ResponseJsonStreamReader reader) throws IOException {
Map<String, Object> buffer = reader.buffer();
RealResponseReader<Map<String, Object>> realResponseReader = new RealResponseReader<>(operation, buffer,
Expand Down

0 comments on commit acad7bb

Please sign in to comment.