Skip to content

Commit

Permalink
Fix issues in integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tasomaniac committed Apr 8, 2020
1 parent 44abbde commit 09d0cad
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 60 deletions.
Expand Up @@ -127,15 +127,15 @@ public void testQueryWatcherUpdated_Store_write() throws IOException, Interrupte
assertThat(heroNameList.get(0)).isEqualTo("R2-D2");

// Someone writes to the store directly
Set<String> changedKeys = apolloClient.apolloStore().writeTransaction(new Transaction<WriteableStore, Set<String>>() {
Set<String> changedKeys = apolloClient.getApolloStore().writeTransaction(new Transaction<WriteableStore, Set<String>>() {
@Nullable @Override public Set<String> execute(WriteableStore cache) {
Record record = Record.builder("2001")
.addField("name", "Artoo")
.build();
return cache.merge(Collections.singletonList(record), CacheHeaders.NONE);
}
});
apolloClient.apolloStore().publish(changedKeys);
apolloClient.getApolloStore().publish(changedKeys);

assertThat(heroNameList.get(1)).isEqualTo("Artoo");

Expand Down
Expand Up @@ -4,12 +4,12 @@
import com.apollographql.apollo.api.ResponseField;
import com.apollographql.apollo.cache.normalized.CacheKey;
import com.apollographql.apollo.cache.normalized.CacheKeyResolver;
import org.jetbrains.annotations.NotNull;

import java.util.Map;

import org.jetbrains.annotations.NotNull;

public class IdFieldCacheKeyResolver extends CacheKeyResolver {

@NotNull @Override
public CacheKey fromFieldRecordSet(@NotNull ResponseField field, @NotNull Map<String, Object> recordSet) {
Object id = recordSet.get("id");
Expand All @@ -34,7 +34,7 @@ private CacheKey formatCacheKey(String id) {
if (id == null || id.isEmpty()) {
return CacheKey.NO_KEY;
} else {
return CacheKey.from(id);
return new CacheKey(id);
}
}
}
Expand Up @@ -404,7 +404,7 @@ public boolean test(Response<HeroTypeDependentAliasedFieldQuery.Data> response)
}
);

HeroWithFriendsFragment heroWithFriendsFragment = apolloClient.apolloStore().read(
HeroWithFriendsFragment heroWithFriendsFragment = apolloClient.getApolloStore().read(
new HeroWithFriendsFragment.Mapper(), CacheKey.from("2001"), Operation.EMPTY_VARIABLES).execute();

assertThat(heroWithFriendsFragment.id()).isEqualTo("2001");
Expand All @@ -417,17 +417,17 @@ public boolean test(Response<HeroTypeDependentAliasedFieldQuery.Data> response)
assertThat(heroWithFriendsFragment.friends().get(2).fragments().humanWithIdFragment().id()).isEqualTo("1003");
assertThat(heroWithFriendsFragment.friends().get(2).fragments().humanWithIdFragment().name()).isEqualTo("Leia Organa");

HumanWithIdFragment fragment = apolloClient.apolloStore().read(new HumanWithIdFragment.Mapper(),
HumanWithIdFragment fragment = apolloClient.getApolloStore().read(new HumanWithIdFragment.Mapper(),
CacheKey.from("1000"), Operation.EMPTY_VARIABLES).execute();
assertThat(fragment.id()).isEqualTo("1000");
assertThat(fragment.name()).isEqualTo("Luke Skywalker");

fragment = apolloClient.apolloStore().read(new HumanWithIdFragment.Mapper(), CacheKey.from("1002"),
fragment = apolloClient.getApolloStore().read(new HumanWithIdFragment.Mapper(), CacheKey.from("1002"),
Operation.EMPTY_VARIABLES).execute();
assertThat(fragment.id()).isEqualTo("1002");
assertThat(fragment.name()).isEqualTo("Han Solo");

fragment = apolloClient.apolloStore().read(new HumanWithIdFragment.Mapper(), CacheKey.from("1003"),
fragment = apolloClient.getApolloStore().read(new HumanWithIdFragment.Mapper(), CacheKey.from("1003"),
Operation.EMPTY_VARIABLES).execute();
assertThat(fragment.id()).isEqualTo("1003");
assertThat(fragment.name()).isEqualTo("Leia Organa");
Expand Down Expand Up @@ -510,7 +510,7 @@ public boolean test(Response<HeroTypeDependentAliasedFieldQuery.Data> response)
);

// test remove root query object
assertThat(apolloClient.apolloStore().remove(CacheKey.from("2001")).execute()).isTrue();
assertThat(apolloClient.getApolloStore().remove(CacheKey.from("2001")).execute()).isTrue();

Utils.INSTANCE.assertResponse(
apolloClient.query(new HeroAndFriendsNamesWithIDsQuery(Input.fromNullable(Episode.NEWHOPE)))
Expand Down Expand Up @@ -546,7 +546,7 @@ public boolean test(Response<HeroTypeDependentAliasedFieldQuery.Data> response)
);

// test remove object from the list
assertThat(apolloClient.apolloStore().remove(CacheKey.from("1002")).execute()).isTrue();
assertThat(apolloClient.getApolloStore().remove(CacheKey.from("1002")).execute()).isTrue();

Utils.INSTANCE.assertResponse(
apolloClient.query(new HeroAndFriendsNamesWithIDsQuery(Input.fromNullable(Episode.NEWHOPE)))
Expand Down Expand Up @@ -630,7 +630,7 @@ public boolean test(Response<HeroTypeDependentAliasedFieldQuery.Data> response)
}
);

assertThat(apolloClient.apolloStore().remove(asList(CacheKey.from("1002"), CacheKey.from("1000")))
assertThat(apolloClient.getApolloStore().remove(asList(CacheKey.from("1002"), CacheKey.from("1000")))
.execute()).isEqualTo(2);

Utils.INSTANCE.assertResponse(
Expand Down Expand Up @@ -797,7 +797,7 @@ public boolean test(Response<HeroTypeDependentAliasedFieldQuery.Data> response)
}
);

Map<Class, Map<String, Record>> dump = apolloClient.apolloStore().normalizedCache().dump();
Map<Class, Map<String, Record>> dump = apolloClient.getApolloStore().normalizedCache().dump();
assertThat(NormalizedCache.prettifyDump(dump)).isEqualTo("OptimisticNormalizedCache {}\n" +
"LruNormalizedCache {\n" +
" \"1002\" : {\n" +
Expand Down Expand Up @@ -883,7 +883,7 @@ public boolean test(Response<HeroTypeDependentAliasedFieldQuery.Data> response)
);

// test remove root query object
assertThat(apolloClient.apolloStore().remove(CacheKey.from("2001"), true).execute()).isTrue();
assertThat(apolloClient.getApolloStore().remove(CacheKey.from("2001"), true).execute()).isTrue();

Utils.INSTANCE.assertResponse(
apolloClient.query(new HeroAndFriendsNamesWithIDsQuery(Input.fromNullable(Episode.NEWHOPE)))
Expand Down Expand Up @@ -929,7 +929,7 @@ public boolean test(Response<HeroTypeDependentAliasedFieldQuery.Data> response)
}
);

assertThat(NormalizedCache.prettifyDump(apolloClient.apolloStore().normalizedCache().dump())).isEqualTo("" +
assertThat(NormalizedCache.prettifyDump(apolloClient.getApolloStore().normalizedCache().dump())).isEqualTo("" +
"OptimisticNormalizedCache {}\n" +
"LruNormalizedCache {\n" +
" \"QUERY_ROOT\" : {\n" +
Expand Down
Expand Up @@ -16,7 +16,12 @@
import com.apollographql.apollo.integration.normalizer.type.ColorInput;
import com.apollographql.apollo.integration.normalizer.type.Episode;
import com.apollographql.apollo.integration.normalizer.type.ReviewInput;

import io.reactivex.functions.Predicate;
import okhttp3.Dispatcher;
import okhttp3.OkHttpClient;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.jetbrains.annotations.NotNull;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -27,14 +32,6 @@
import java.util.List;
import java.util.UUID;

import org.jetbrains.annotations.NotNull;

import io.reactivex.functions.Predicate;
import okhttp3.Dispatcher;
import okhttp3.OkHttpClient;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;

import static com.apollographql.apollo.fetcher.ApolloResponseFetchers.CACHE_ONLY;
import static com.google.common.truth.Truth.assertThat;

Expand Down Expand Up @@ -88,7 +85,7 @@ private MockResponse mockResponse(String fileName) throws IOException {
)
)
));
apolloClient.apolloStore().writeOptimisticUpdatesAndPublish(query, data, mutationId).execute();
apolloClient.getApolloStore().writeOptimisticUpdatesAndPublish(query, data, mutationId).execute();

Utils.INSTANCE.assertResponse(
apolloClient.query(query).responseFetcher(CACHE_ONLY),
Expand All @@ -103,7 +100,7 @@ private MockResponse mockResponse(String fileName) throws IOException {
}
);

apolloClient.apolloStore().rollbackOptimisticUpdates(mutationId).execute();
apolloClient.getApolloStore().rollbackOptimisticUpdates(mutationId).execute();

Utils.INSTANCE.assertResponse(
apolloClient.query(query).responseFetcher(CACHE_ONLY),
Expand Down Expand Up @@ -157,7 +154,7 @@ private MockResponse mockResponse(String fileName) throws IOException {
)
)
);
apolloClient.apolloStore().writeOptimisticUpdatesAndPublish(query1, data1, mutationId1).execute();
apolloClient.getApolloStore().writeOptimisticUpdatesAndPublish(query1, data1, mutationId1).execute();

// check if query1 see optimistic updates
Utils.INSTANCE.assertResponse(
Expand Down Expand Up @@ -192,7 +189,7 @@ private MockResponse mockResponse(String fileName) throws IOException {
"1000",
"Beast"
));
apolloClient.apolloStore().writeOptimisticUpdatesAndPublish(query2, data2, mutationId2).execute();
apolloClient.getApolloStore().writeOptimisticUpdatesAndPublish(query2, data2, mutationId2).execute();

// check if query1 see the latest optimistic updates
Utils.INSTANCE.assertResponse(
Expand Down Expand Up @@ -224,7 +221,7 @@ private MockResponse mockResponse(String fileName) throws IOException {
);

// rollback query1 optimistic updates
apolloClient.apolloStore().rollbackOptimisticUpdates(mutationId1).execute();
apolloClient.getApolloStore().rollbackOptimisticUpdates(mutationId1).execute();

// check if query1 see the latest optimistic updates
Utils.INSTANCE.assertResponse(
Expand Down Expand Up @@ -258,7 +255,7 @@ private MockResponse mockResponse(String fileName) throws IOException {
);

// rollback query2 optimistic updates
apolloClient.apolloStore().rollbackOptimisticUpdates(mutationId2).execute();
apolloClient.getApolloStore().rollbackOptimisticUpdates(mutationId2).execute();

// check if query2 see the latest optimistic updates
Utils.INSTANCE.assertResponse(
Expand Down Expand Up @@ -286,7 +283,7 @@ private MockResponse mockResponse(String fileName) throws IOException {
);

UUID mutationId = UUID.randomUUID();
apolloClient.apolloStore().writeOptimisticUpdates(
apolloClient.getApolloStore().writeOptimisticUpdates(
new HeroNameQuery(),
new HeroNameQuery.Data(new HeroNameQuery.Hero("Droid", "R22-D22")),
mutationId
Expand All @@ -304,7 +301,7 @@ private MockResponse mockResponse(String fileName) throws IOException {
}
);

apolloClient.apolloStore().rollbackOptimisticUpdates(mutationId).execute();
apolloClient.getApolloStore().rollbackOptimisticUpdates(mutationId).execute();

Utils.INSTANCE.assertResponse(
apolloClient.query(new HeroNameWithEnumsQuery()).responseFetcher(CACHE_ONLY),
Expand Down Expand Up @@ -441,14 +438,14 @@ private MockResponse mockResponse(String fileName) throws IOException {
)
)
);
apolloClient.apolloStore().writeOptimisticUpdatesAndPublish(query1, data1, mutationId1).execute();
apolloClient.getApolloStore().writeOptimisticUpdatesAndPublish(query1, data1, mutationId1).execute();

HeroNameWithIdQuery.Data data2 = new HeroNameWithIdQuery.Data(new HeroNameWithIdQuery.Hero(
"Human",
"1000",
"Spiderman"
));
apolloClient.apolloStore().writeOptimisticUpdatesAndPublish(query2, data2, mutationId2).execute();
apolloClient.getApolloStore().writeOptimisticUpdatesAndPublish(query2, data2, mutationId2).execute();

// check if query1 see optimistic updates
Utils.INSTANCE.assertResponse(
Expand Down Expand Up @@ -480,7 +477,7 @@ private MockResponse mockResponse(String fileName) throws IOException {
);

// rollback query2 optimistic updates
apolloClient.apolloStore().rollbackOptimisticUpdates(mutationId2).execute();
apolloClient.getApolloStore().rollbackOptimisticUpdates(mutationId2).execute();

// check if query1 see the latest optimistic updates
Utils.INSTANCE.assertResponse(
Expand Down Expand Up @@ -512,7 +509,7 @@ private MockResponse mockResponse(String fileName) throws IOException {
);

// rollback query1 optimistic updates
apolloClient.apolloStore().rollbackOptimisticUpdates(mutationId1).execute();
apolloClient.getApolloStore().rollbackOptimisticUpdates(mutationId1).execute();

// check if query1 see the latest non-optimistic updates
Utils.INSTANCE.assertResponse(
Expand Down
Expand Up @@ -20,7 +20,10 @@
import com.apollographql.apollo.integration.normalizer.HeroParentTypeDependentFieldQuery;
import com.apollographql.apollo.integration.normalizer.HeroTypeDependentAliasedFieldQuery;
import com.apollographql.apollo.integration.normalizer.SameHeroTwiceQuery;

import io.reactivex.functions.Predicate;
import okhttp3.Dispatcher;
import okhttp3.OkHttpClient;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -30,11 +33,6 @@
import java.util.Collections;
import java.util.List;

import io.reactivex.functions.Predicate;
import okhttp3.Dispatcher;
import okhttp3.OkHttpClient;
import okhttp3.mockwebserver.MockWebServer;

import static com.apollographql.apollo.integration.normalizer.type.Episode.EMPIRE;
import static com.apollographql.apollo.integration.normalizer.type.Episode.JEDI;
import static com.google.common.truth.Truth.assertThat;
Expand All @@ -59,7 +57,7 @@ public class ResponseNormalizationTest {
.normalizedCache(new LruNormalizedCacheFactory(EvictionPolicy.NO_EVICTION), new IdFieldCacheKeyResolver())
.dispatcher(Utils.INSTANCE.immediateExecutor())
.build();
normalizedCache = apolloClient.apolloStore().normalizedCache();
normalizedCache = apolloClient.getApolloStore().normalizedCache();
}

@Test public void testHeroName() throws Exception {
Expand Down
Expand Up @@ -98,7 +98,7 @@ public void customType() throws Exception {
DATE_TIME_FORMAT.parse("1985-04-16"),
Collections.<Date>emptyList()
);
apolloClient.apolloStore().write(query, new EpisodeHeroWithDatesQuery.Data(hero)).execute();
apolloClient.getApolloStore().write(query, new EpisodeHeroWithDatesQuery.Data(hero)).execute();

assertCachedQueryResponse(
query,
Expand All @@ -122,7 +122,7 @@ public void customType() throws Exception {
DATE_TIME_FORMAT.parse("2017-05-16")
)
);
apolloClient.apolloStore().write(query, new EpisodeHeroWithDatesQuery.Data(hero)).execute();
apolloClient.getApolloStore().write(query, new EpisodeHeroWithDatesQuery.Data(hero)).execute();

assertCachedQueryResponse(
query,
Expand Down Expand Up @@ -168,7 +168,7 @@ public void enums() throws Exception {
Episode.JEDI,
Collections.<Episode>emptyList()
);
apolloClient.apolloStore().write(query, new HeroNameWithEnumsQuery.Data(hero)).execute();
apolloClient.getApolloStore().write(query, new HeroNameWithEnumsQuery.Data(hero)).execute();

assertCachedQueryResponse(
query,
Expand All @@ -189,7 +189,7 @@ public void enums() throws Exception {
Episode.JEDI,
asList(Episode.EMPIRE)
);
apolloClient.apolloStore().write(query, new HeroNameWithEnumsQuery.Data(hero)).execute();
apolloClient.getApolloStore().write(query, new HeroNameWithEnumsQuery.Data(hero)).execute();

assertCachedQueryResponse(
query,
Expand Down Expand Up @@ -240,7 +240,7 @@ public void objects() throws Exception {
"R222-D222",
null
);
apolloClient.apolloStore().write(query, new HeroAndFriendsNamesWithIDsQuery.Data(hero)).execute();
apolloClient.getApolloStore().write(query, new HeroAndFriendsNamesWithIDsQuery.Data(hero)).execute();

assertCachedQueryResponse(
query,
Expand All @@ -266,7 +266,7 @@ public void objects() throws Exception {
"R222-D222",
singletonList(friend)
);
apolloClient.apolloStore().write(query, new HeroAndFriendsNamesWithIDsQuery.Data(hero)).execute();
apolloClient.getApolloStore().write(query, new HeroAndFriendsNamesWithIDsQuery.Data(hero)).execute();

assertCachedQueryResponse(
query,
Expand Down Expand Up @@ -346,7 +346,7 @@ public void operation_with_fragments() throws Exception {
)
)
);
apolloClient.apolloStore().write(query, new HeroAndFriendsWithFragmentsQuery.Data(hero)).execute();
apolloClient.getApolloStore().write(query, new HeroAndFriendsWithFragmentsQuery.Data(hero)).execute();

assertCachedQueryResponse(
query,
Expand Down Expand Up @@ -422,7 +422,7 @@ public boolean test(Response<EpisodeHeroWithInlineFragmentQuery.Data> response)
)
)
);
apolloClient.apolloStore().write(query, new EpisodeHeroWithInlineFragmentQuery.Data(hero)).execute();
apolloClient.getApolloStore().write(query, new EpisodeHeroWithInlineFragmentQuery.Data(hero)).execute();

assertCachedQueryResponse(
query,
Expand Down Expand Up @@ -477,7 +477,7 @@ public void fragments() throws Exception {
}
);

apolloClient.apolloStore().write(
apolloClient.getApolloStore().write(
new HeroWithFriendsFragment(
"Droid",
"2001",
Expand Down Expand Up @@ -507,7 +507,7 @@ public void fragments() throws Exception {
), CacheKey.from("2001"), query.variables()
).execute();

apolloClient.apolloStore().write(
apolloClient.getApolloStore().write(
new HumanWithIdFragment(
"Human",
"1002",
Expand Down Expand Up @@ -561,7 +561,7 @@ public void fragments() throws Exception {
"SuperRocket",
asList(asList(900d, 800d), asList(700d, 600d))
);
apolloClient.apolloStore().write(query, new StarshipByIdQuery.Data(starship)).execute();
apolloClient.getApolloStore().write(query, new StarshipByIdQuery.Data(starship)).execute();

assertCachedQueryResponse(
query,
Expand Down

0 comments on commit 09d0cad

Please sign in to comment.