Search in sources :

Example 26 with ResponseField

use of com.apollographql.apollo.api.ResponseField in project apollo-android by apollographql.

the class CacheKeyForFieldTest method testFieldWithNonPrimitiveValue.

@Test
public void testFieldWithNonPrimitiveValue() {
    // noinspection unchecked
    ResponseField field = ResponseField.forString("hero", "hero", new UnmodifiableMapBuilder<String, Object>(1).put("episode", Episode.JEDI).build(), false, Collections.<ResponseField.Condition>emptyList());
    Operation.Variables variables = new Operation.Variables() {

        @Nonnull
        @Override
        public Map<String, Object> valueMap() {
            return super.valueMap();
        }
    };
    assertThat(field.cacheKey(variables)).isEqualTo("hero({\"episode\":\"JEDI\"})");
}
Also used : ResponseField(com.apollographql.apollo.api.ResponseField) Operation(com.apollographql.apollo.api.Operation) Test(org.junit.Test)

Example 27 with ResponseField

use of com.apollographql.apollo.api.ResponseField in project apollo-android by apollographql.

the class CacheKeyForFieldTest method testFieldWithArgument.

@Test
public void testFieldWithArgument() {
    // noinspection unchecked
    Map<String, Object> arguments = new UnmodifiableMapBuilder<String, Object>(1).put("episode", "JEDI").build();
    ResponseField field = createResponseField("hero", "hero", arguments);
    Operation.Variables variables = new Operation.Variables() {

        @Nonnull
        @Override
        public Map<String, Object> valueMap() {
            return super.valueMap();
        }
    };
    assertThat(field.cacheKey(variables)).isEqualTo("hero({\"episode\":\"JEDI\"})");
}
Also used : ResponseField(com.apollographql.apollo.api.ResponseField) Operation(com.apollographql.apollo.api.Operation) UnmodifiableMapBuilder(com.apollographql.apollo.api.internal.UnmodifiableMapBuilder) Test(org.junit.Test)

Example 28 with ResponseField

use of com.apollographql.apollo.api.ResponseField in project apollo-android by apollographql.

the class ResponseReaderTest method readObject.

@Test
public void readObject() throws Exception {
    final Object responseObject1 = new Object();
    final Object responseObject2 = new Object();
    ResponseField successField = ResponseField.forObject("successFieldResponseName", "successFieldName", null, false, NO_CONDITIONS);
    ResponseField classCastExceptionField = ResponseField.forObject("classCastExceptionFieldResponseName", "classCastExceptionFieldName", null, false, NO_CONDITIONS);
    Map<String, Object> recordSet = new HashMap<>();
    recordSet.put("successFieldResponseName", responseObject1);
    recordSet.put("successFieldName", responseObject2);
    recordSet.put("classCastExceptionFieldResponseName", "anything");
    RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet);
    assertThat(responseReader.readObject(successField, new ResponseReader.ObjectReader<Object>() {

        @Override
        public Object read(ResponseReader reader) {
            return responseObject1;
        }
    })).isEqualTo(responseObject1);
    try {
        responseReader.readObject(classCastExceptionField, new ResponseReader.ObjectReader<Object>() {

            @Override
            public Object read(ResponseReader reader) {
                return reader.readString(ResponseField.forString("anything", "anything", null, true, NO_CONDITIONS));
            }
        });
        fail("expected ClassCastException");
    } catch (ClassCastException expected) {
    // expected
    }
}
Also used : ResponseField(com.apollographql.apollo.api.ResponseField) HashMap(java.util.HashMap) ResponseReader(com.apollographql.apollo.api.ResponseReader) RealResponseReader(com.apollographql.apollo.internal.response.RealResponseReader) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 29 with ResponseField

use of com.apollographql.apollo.api.ResponseField in project apollo-android by apollographql.

the class ResponseReaderTest method readString.

@Test
public void readString() throws Exception {
    ResponseField successField = ResponseField.forString("successFieldResponseName", "successFieldName", null, false, NO_CONDITIONS);
    ResponseField classCastExceptionField = ResponseField.forString("classCastExceptionFieldResponseName", "classCastExceptionFieldName", null, false, NO_CONDITIONS);
    Map<String, Object> recordSet = new HashMap<>();
    recordSet.put("successFieldResponseName", "response1");
    recordSet.put("successFieldName", "response2");
    recordSet.put("classCastExceptionFieldResponseName", 1);
    RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet);
    assertThat(responseReader.readString(successField)).isEqualTo("response1");
    try {
        responseReader.readString(classCastExceptionField);
        fail("expected ClassCastException");
    } catch (ClassCastException expected) {
    // expected
    }
}
Also used : ResponseField(com.apollographql.apollo.api.ResponseField) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 30 with ResponseField

use of com.apollographql.apollo.api.ResponseField in project apollo-android by apollographql.

the class ResponseReaderTest method readConditional.

@Test
public void readConditional() throws Exception {
    final Object responseObject1 = new Object();
    final Object responseObject2 = new Object();
    ResponseField successField = ResponseField.forFragment("successFieldResponseName", "successFieldName", Collections.<String>emptyList());
    ResponseField classCastExceptionField = ResponseField.forFragment("classCastExceptionFieldResponseName", "classCastExceptionFieldName", Collections.<String>emptyList());
    Map<String, Object> recordSet = new HashMap<>();
    recordSet.put("successFieldResponseName", "responseObject1");
    recordSet.put("successFieldName", "responseObject2");
    recordSet.put("classCastExceptionFieldResponseName", 1);
    RealResponseReader<Map<String, Object>> responseReader = responseReader(recordSet);
    assertThat(responseReader.readConditional(successField, new ResponseReader.ConditionalTypeReader<Object>() {

        @Override
        public Object read(String conditionalType, ResponseReader reader) {
            if (conditionalType.equals("responseObject1")) {
                return responseObject1;
            } else {
                return responseObject2;
            }
        }
    })).isEqualTo(responseObject1);
    try {
        responseReader.readConditional(classCastExceptionField, new ResponseReader.ConditionalTypeReader<Object>() {

            @Override
            public Object read(String conditionalType, ResponseReader reader) {
                return null;
            }
        });
        fail("expected ClassCastException");
    } catch (ClassCastException expected) {
    // expected
    }
}
Also used : ResponseField(com.apollographql.apollo.api.ResponseField) HashMap(java.util.HashMap) ResponseReader(com.apollographql.apollo.api.ResponseReader) RealResponseReader(com.apollographql.apollo.internal.response.RealResponseReader) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

ResponseField (com.apollographql.apollo.api.ResponseField)34 Test (org.junit.Test)33 HashMap (java.util.HashMap)25 Map (java.util.Map)23 ResponseReader (com.apollographql.apollo.api.ResponseReader)13 RealResponseReader (com.apollographql.apollo.internal.response.RealResponseReader)13 Operation (com.apollographql.apollo.api.Operation)11 UnmodifiableMapBuilder (com.apollographql.apollo.api.internal.UnmodifiableMapBuilder)6 Arrays.asList (java.util.Arrays.asList)3 List (java.util.List)3 CacheKeyResolver (com.apollographql.apollo.cache.normalized.CacheKeyResolver)1 NormalizedCacheFactory (com.apollographql.apollo.cache.normalized.NormalizedCacheFactory)1 LruNormalizedCacheFactory (com.apollographql.apollo.cache.normalized.lru.LruNormalizedCacheFactory)1 ApolloSqlHelper (com.apollographql.apollo.cache.normalized.sql.ApolloSqlHelper)1 SqlNormalizedCacheFactory (com.apollographql.apollo.cache.normalized.sql.SqlNormalizedCacheFactory)1 WebSocketSubscriptionTransport (com.apollographql.apollo.subscription.WebSocketSubscriptionTransport)1 Nonnull (javax.annotation.Nonnull)1 OkHttpClient (okhttp3.OkHttpClient)1