-
Notifications
You must be signed in to change notification settings - Fork 108
/
AWSDataStorePluginTest.java
620 lines (553 loc) · 29.6 KB
/
AWSDataStorePluginTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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.amplifyframework.datastore;
import android.content.Context;
import androidx.annotation.NonNull;
import com.amplifyframework.AmplifyException;
import com.amplifyframework.api.ApiCategory;
import com.amplifyframework.api.ApiCategoryConfiguration;
import com.amplifyframework.api.ApiException;
import com.amplifyframework.api.ApiPlugin;
import com.amplifyframework.api.events.ApiChannelEventName;
import com.amplifyframework.api.events.ApiEndpointStatusChangeEvent;
import com.amplifyframework.api.graphql.GraphQLOperation;
import com.amplifyframework.api.graphql.GraphQLRequest;
import com.amplifyframework.api.graphql.GraphQLResponse;
import com.amplifyframework.api.graphql.PaginatedResult;
import com.amplifyframework.api.graphql.SubscriptionType;
import com.amplifyframework.core.Action;
import com.amplifyframework.core.Amplify;
import com.amplifyframework.core.Consumer;
import com.amplifyframework.core.InitializationStatus;
import com.amplifyframework.core.category.CategoryType;
import com.amplifyframework.core.model.ModelProvider;
import com.amplifyframework.core.model.temporal.Temporal;
import com.amplifyframework.datastore.appsync.ModelMetadata;
import com.amplifyframework.datastore.appsync.ModelWithMetadata;
import com.amplifyframework.datastore.model.SimpleModelProvider;
import com.amplifyframework.hub.HubChannel;
import com.amplifyframework.hub.HubEvent;
import com.amplifyframework.logging.Logger;
import com.amplifyframework.testmodels.personcar.AmplifyCliGeneratedModelProvider;
import com.amplifyframework.testmodels.personcar.Person;
import com.amplifyframework.testutils.HubAccumulator;
import com.amplifyframework.testutils.random.RandomString;
import com.amplifyframework.testutils.sync.SynchronousDataStore;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatcher;
import org.robolectric.RobolectricTestRunner;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import io.reactivex.rxjava3.core.Observable;
import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockingDetails;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@SuppressWarnings("SameParameterValue")
@RunWith(RobolectricTestRunner.class)
public final class AWSDataStorePluginTest {
private static final Logger LOG = Amplify.Logging.forNamespace("amplify:datastore:test");
private static final long TIMEOUT_MS = TimeUnit.SECONDS.toMillis(1);
private static final String MOCK_API_PLUGIN_NAME = "MockApiPlugin";
private Context context;
private ModelProvider modelProvider;
private AtomicInteger subscriptionStartedCounter;
private AtomicInteger subscriptionCancelledCounter;
private int modelCount;
/**
* Sets up the test. The {@link SimpleModelProvider} is spy'd, so that
* we can check if the SyncProcessor queries it. If it does, that means
* the SyncProcessor is running. Otherwise, either the SyncProcessor is *not*
* running, or it is running but not functioning as we expect it to.
*/
@Before
public void setup() {
this.context = getApplicationContext();
modelProvider = spy(AmplifyCliGeneratedModelProvider.singletonInstance());
subscriptionCancelledCounter = new AtomicInteger();
subscriptionStartedCounter = new AtomicInteger();
modelCount = modelProvider.modelNames().size();
}
/**
* Configuring and initializing the plugin succeeds without freezing or crashing the calling thread. Basic. 🙄
* @throws AmplifyException On failure to configure or initialize plugin.
*/
@Test
public void configureAndInitialize() throws AmplifyException {
//Configure DataStore with an empty config (All defaults)
ApiCategory emptyApiCategory = spy(ApiCategory.class);
AWSDataStorePlugin standAloneDataStorePlugin = AWSDataStorePlugin.builder()
.modelProvider(modelProvider)
.apiCategory(emptyApiCategory)
.build();
standAloneDataStorePlugin.configure(new JSONObject(), context);
standAloneDataStorePlugin.initialize(context);
}
/**
* Starting the plugin in local mode (no API plugin) works without freezing or crashing the calling thread.
* @throws AmplifyException Not expected; on failure to configure of initialize plugin.
*/
@Test
public void startInLocalMode() throws AmplifyException {
// Configure DataStore with an empty config (All defaults)
HubAccumulator dataStoreReadyObserver =
HubAccumulator.create(HubChannel.DATASTORE, DataStoreChannelEventName.READY, 1)
.start();
ApiCategory emptyApiCategory = spy(ApiCategory.class);
AWSDataStorePlugin standAloneDataStorePlugin = AWSDataStorePlugin.builder()
.modelProvider(modelProvider)
.apiCategory(emptyApiCategory)
.build();
SynchronousDataStore synchronousDataStore = SynchronousDataStore.delegatingTo(standAloneDataStorePlugin);
standAloneDataStorePlugin.configure(new JSONObject(), context);
standAloneDataStorePlugin.initialize(context);
// Trick the DataStore since it's not getting initialized as part of the Amplify.initialize call chain
Amplify.Hub.publish(HubChannel.DATASTORE, HubEvent.create(InitializationStatus.SUCCEEDED));
synchronousDataStore.start();
dataStoreReadyObserver.await();
assertSyncProcessorNotStarted(emptyApiCategory);
Person person1 = createPerson("Test", "Dummy I");
synchronousDataStore.save(person1);
assertNotNull(person1.getId());
Person person1FromDb = synchronousDataStore.get(Person.class, person1.getPrimaryKeyString());
assertEquals(person1, person1FromDb);
}
/**
* Starting the plugin when in API sync mode succeeds without freezing or crashing the calling thread.
* @throws JSONException on failure to arrange plugin config
* @throws AmplifyException on failure to arrange API plugin via Amplify facade
*/
@Test
public void startInApiMode() throws JSONException, AmplifyException {
HubAccumulator dataStoreReadyObserver =
HubAccumulator.create(HubChannel.DATASTORE, DataStoreChannelEventName.READY, 1)
.start();
HubAccumulator subscriptionsEstablishedObserver =
HubAccumulator.create(HubChannel.DATASTORE, DataStoreChannelEventName.SUBSCRIPTIONS_ESTABLISHED, 1)
.start();
HubAccumulator networkStatusObserver =
HubAccumulator.create(HubChannel.DATASTORE, DataStoreChannelEventName.NETWORK_STATUS, 1)
.start();
ApiCategory mockApiCategory = mockApiCategoryWithGraphQlApi();
JSONObject dataStorePluginJson = new JSONObject()
.put("syncIntervalInMinutes", 60);
AWSDataStorePlugin awsDataStorePlugin = AWSDataStorePlugin.builder()
.modelProvider(modelProvider)
.apiCategory(mockApiCategory)
.build();
SynchronousDataStore synchronousDataStore = SynchronousDataStore.delegatingTo(awsDataStorePlugin);
awsDataStorePlugin.configure(dataStorePluginJson, context);
awsDataStorePlugin.initialize(context);
// Trick the DataStore since it's not getting initialized as part of the Amplify.initialize call chain
Amplify.Hub.publish(HubChannel.DATASTORE, HubEvent.create(InitializationStatus.SUCCEEDED));
synchronousDataStore.start();
dataStoreReadyObserver.await();
subscriptionsEstablishedObserver.await();
networkStatusObserver.await();
assertRemoteSubscriptionsStarted();
}
/**
* Verify that when the clear method is called, the following happens
* - All remote synchronization processes are stopped
* - The database is deleted.
* - On the next interaction with the DataStore, the synchronization processes are restarted.
* @throws JSONException on failure to arrange plugin config
* @throws AmplifyException on failure to arrange API plugin via Amplify facade
*/
@Test
public void clearStopsSyncAndDeletesDatabase() throws AmplifyException, JSONException {
ApiCategory mockApiCategory = mockApiCategoryWithGraphQlApi();
ApiPlugin<?> mockApiPlugin = mockApiCategory.getPlugin(MOCK_API_PLUGIN_NAME);
JSONObject dataStorePluginJson = new JSONObject()
.put("syncIntervalInMinutes", 60);
AWSDataStorePlugin awsDataStorePlugin = AWSDataStorePlugin.builder()
.modelProvider(modelProvider)
.apiCategory(mockApiCategory)
.build();
SynchronousDataStore synchronousDataStore = SynchronousDataStore.delegatingTo(awsDataStorePlugin);
awsDataStorePlugin.configure(dataStorePluginJson, context);
awsDataStorePlugin.initialize(context);
// Trick the DataStore since it's not getting initialized as part of the Amplify.initialize call chain
Amplify.Hub.publish(HubChannel.DATASTORE, HubEvent.create(InitializationStatus.SUCCEEDED));
// Setup objects
Person person1 = createPerson("Test", "Dummy I");
Person person2 = createPerson("Test", "Dummy II");
// Mock responses for person 1
doAnswer(invocation -> {
int indexOfResponseConsumer = 1;
Consumer<GraphQLResponse<ModelWithMetadata<Person>>> onResponse =
invocation.getArgument(indexOfResponseConsumer);
ModelMetadata modelMetadata = new ModelMetadata(person1.getId(), false, 1, Temporal.Timestamp.now());
ModelWithMetadata<Person> modelWithMetadata = new ModelWithMetadata<>(person1, modelMetadata);
onResponse.accept(new GraphQLResponse<>(modelWithMetadata, Collections.emptyList()));
return mock(GraphQLOperation.class);
}).when(mockApiPlugin).mutate(any(), any(), any());
HubAccumulator apiInteractionObserver =
HubAccumulator.create(HubChannel.DATASTORE, DataStoreChannelEventName.OUTBOX_MUTATION_PROCESSED, 1)
.start();
// Save person 1
synchronousDataStore.save(person1);
Person result1 = synchronousDataStore.get(Person.class, person1.getPrimaryKeyString());
assertEquals(person1, result1);
apiInteractionObserver.await(15, TimeUnit.SECONDS);
verify(mockApiCategory).mutate(argThat(getMatcherFor(person1)), any(), any());
// Mock responses for person 2
doAnswer(invocation -> {
int indexOfResponseConsumer = 1;
Consumer<GraphQLResponse<ModelWithMetadata<Person>>> onResponse =
invocation.getArgument(indexOfResponseConsumer);
ModelMetadata modelMetadata = new ModelMetadata(person2.getId(), false, 1,
Temporal.Timestamp.now());
ModelWithMetadata<Person> modelWithMetadata = new ModelWithMetadata<>(person2, modelMetadata);
onResponse.accept(new GraphQLResponse<>(modelWithMetadata, Collections.emptyList()));
return mock(GraphQLOperation.class);
}).when(mockApiPlugin).mutate(any(), any(), any());
// Do the thing!
synchronousDataStore.clear();
assertRemoteSubscriptionsCancelled();
apiInteractionObserver =
HubAccumulator.create(HubChannel.DATASTORE, DataStoreChannelEventName.OUTBOX_MUTATION_PROCESSED, 1)
.start();
HubAccumulator orchestratorInitObserver =
HubAccumulator.create(HubChannel.DATASTORE, DataStoreChannelEventName.READY, 1)
.start();
// Interact with the DataStore after the clear
synchronousDataStore.save(person2);
// Verify person 2 was published to the cloud
apiInteractionObserver.await();
// Verify the orchestrator started back up and subscriptions are active.
orchestratorInitObserver.await();
assertRemoteSubscriptionsStarted();
Person result2 = synchronousDataStore.get(Person.class, person2.getPrimaryKeyString());
assertEquals(person2, result2);
verify(mockApiCategory, atLeastOnce())
.mutate(argThat(getMatcherFor(person2)), any(), any());
}
/**
* Verify that when the stop method is called, the following happens
* - All remote synchronization processes are stopped
* - On the next interaction with the DataStore, the synchronization processes are restarted.
* @throws JSONException on failure to arrange plugin config
* @throws AmplifyException on failure to arrange API plugin via Amplify facade
*/
@Test
public void stopStopsSyncUntilNextInteraction() throws AmplifyException, JSONException {
ApiCategory mockApiCategory = mockApiCategoryWithGraphQlApi();
ApiPlugin<?> mockApiPlugin = mockApiCategory.getPlugin(MOCK_API_PLUGIN_NAME);
JSONObject dataStorePluginJson = new JSONObject()
.put("syncIntervalInMinutes", 60);
AWSDataStorePlugin awsDataStorePlugin = AWSDataStorePlugin.builder()
.modelProvider(modelProvider)
.apiCategory(mockApiCategory)
.build();
SynchronousDataStore synchronousDataStore = SynchronousDataStore.delegatingTo(awsDataStorePlugin);
awsDataStorePlugin.configure(dataStorePluginJson, context);
awsDataStorePlugin.initialize(context);
// Trick the DataStore since it's not getting initialized as part of the Amplify.initialize call chain
Amplify.Hub.publish(HubChannel.DATASTORE, HubEvent.create(InitializationStatus.SUCCEEDED));
// Setup objects
Person person1 = createPerson("Test", "Dummy I");
Person person2 = createPerson("Test", "Dummy II");
// Mock responses for person 1
doAnswer(invocation -> {
int indexOfResponseConsumer = 1;
Consumer<GraphQLResponse<ModelWithMetadata<Person>>> onResponse =
invocation.getArgument(indexOfResponseConsumer);
ModelMetadata modelMetadata = new ModelMetadata(person1.getPrimaryKeyString(), false, 1,
Temporal.Timestamp.now());
ModelWithMetadata<Person> modelWithMetadata = new ModelWithMetadata<>(person1, modelMetadata);
onResponse.accept(new GraphQLResponse<>(modelWithMetadata, Collections.emptyList()));
return mock(GraphQLOperation.class);
}).when(mockApiPlugin).mutate(any(), any(), any());
HubAccumulator apiInteractionObserver =
HubAccumulator.create(HubChannel.DATASTORE, DataStoreChannelEventName.OUTBOX_MUTATION_PROCESSED, 1)
.start();
// Save person 1
synchronousDataStore.save(person1);
Person result1 = synchronousDataStore.get(Person.class, person1.getPrimaryKeyString());
assertEquals(person1, result1);
apiInteractionObserver.await();
verify(mockApiCategory).mutate(argThat(getMatcherFor(person1)), any(), any());
// Mock responses for person 2
doAnswer(invocation -> {
int indexOfResponseConsumer = 1;
Consumer<GraphQLResponse<ModelWithMetadata<Person>>> onResponse =
invocation.getArgument(indexOfResponseConsumer);
ModelMetadata modelMetadata = new ModelMetadata(person2.getPrimaryKeyString(), false, 1,
Temporal.Timestamp.now());
ModelWithMetadata<Person> modelWithMetadata = new ModelWithMetadata<>(person2, modelMetadata);
onResponse.accept(new GraphQLResponse<>(modelWithMetadata, Collections.emptyList()));
return mock(GraphQLOperation.class);
}).when(mockApiPlugin).mutate(any(), any(), any());
// Do the thing!
synchronousDataStore.stop();
assertRemoteSubscriptionsCancelled();
apiInteractionObserver =
HubAccumulator.create(HubChannel.DATASTORE, DataStoreChannelEventName.OUTBOX_MUTATION_PROCESSED, 1)
.start();
HubAccumulator orchestratorInitObserver =
HubAccumulator.create(HubChannel.DATASTORE, DataStoreChannelEventName.READY, 1)
.start();
// Interact with the DataStore after the stop
synchronousDataStore.save(person2);
// Verify person 2 was published to the cloud
apiInteractionObserver.await();
// Verify the orchestrator started back up and subscriptions are active.
orchestratorInitObserver.await();
assertRemoteSubscriptionsStarted();
// Verify person 1 and 2 are in the DataStore
List<Person> results = synchronousDataStore.list(Person.class);
assertEquals(Arrays.asList(person1, person2), results);
verify(mockApiCategory, atLeastOnce())
.mutate(argThat(getMatcherFor(person2)), any(), any());
}
private void assertRemoteSubscriptionsCancelled() {
// Check that we've had active subscriptions
assertTrue(subscriptionStartedCounter.get() > 0);
// And the number of started and cancelled are the same
assertEquals(subscriptionStartedCounter.get(), subscriptionCancelledCounter.get());
}
private void assertRemoteSubscriptionsStarted() {
// For each model, there should be 3 subscriptions setup.
// If subscriptions are active, the active counters should be:
// activeCount - cancelledCount = modelCount * 3
// The difference between active and cancelled should always be at most modelCount * 3
final int diffTypesOfSubscriptions = SubscriptionType.values().length;
assertEquals(
modelCount * diffTypesOfSubscriptions,
subscriptionStartedCounter.get() - subscriptionCancelledCounter.get()
);
}
/**
* Check that the SyncProcessor did not start by asserting that the
* only interaction with the API category was triggered by the getPlugins
* method.
* @param mockApi Mock or spy of the ApiCategory being used for the test.
*/
private void assertSyncProcessorNotStarted(ApiCategory mockApi) {
Long callsToApiCategory = Observable.fromIterable(mockingDetails(mockApi).getInvocations())
.filter(invocation -> !"getPlugins".equals(invocation.getMethod().getName()))
.count()
.blockingGet();
assertEquals(Long.valueOf(0), callsToApiCategory);
}
@SuppressWarnings("unchecked")
private ApiCategory mockApiCategoryWithGraphQlApi() throws AmplifyException {
ApiCategory mockApiCategory = spy(ApiCategory.class);
ApiPlugin<?> mockApiPlugin = mock(ApiPlugin.class);
when(mockApiPlugin.getPluginKey()).thenReturn(MOCK_API_PLUGIN_NAME);
when(mockApiPlugin.getCategoryType()).thenReturn(CategoryType.API);
ApiEndpointStatusChangeEvent eventData =
new ApiEndpointStatusChangeEvent(ApiEndpointStatusChangeEvent.ApiEndpointStatus.REACHABLE,
ApiEndpointStatusChangeEvent.ApiEndpointStatus.UNKOWN);
HubEvent<ApiEndpointStatusChangeEvent> hubEvent =
HubEvent.create(ApiChannelEventName.API_ENDPOINT_STATUS_CHANGED, eventData);
// Make believe that queries return response immediately
doAnswer(invocation -> {
// Mock the API emitting an ApiEndpointStatusChangeEvent event.
Amplify.Hub.publish(HubChannel.API, hubEvent);
int indexOfResponseConsumer = 1;
Consumer<GraphQLResponse<PaginatedResult<ModelWithMetadata<Person>>>> onResponse =
invocation.getArgument(indexOfResponseConsumer);
PaginatedResult<ModelWithMetadata<Person>> data = new PaginatedResult<>(Collections.emptyList(), null);
onResponse.accept(new GraphQLResponse<>(data, Collections.emptyList()));
return null;
}).when(mockApiPlugin).query(any(GraphQLRequest.class), any(Consumer.class), any(Consumer.class));
// Make believe that subscriptions return response immediately
doAnswer(invocation -> {
int indexOfStartConsumer = 1;
Consumer<String> onStart = invocation.getArgument(indexOfStartConsumer);
GraphQLOperation<?> mockOperation = mock(GraphQLOperation.class);
doAnswer(opAnswer -> {
this.subscriptionCancelledCounter.incrementAndGet();
return null;
}).when(mockOperation).cancel();
this.subscriptionStartedCounter.incrementAndGet();
// Trigger the subscription start event.
onStart.accept(RandomString.string());
return mockOperation;
}).when(mockApiPlugin).subscribe(
any(GraphQLRequest.class),
any(Consumer.class),
any(Consumer.class),
any(Consumer.class),
any(Action.class)
);
mockApiCategory.addPlugin(mockApiPlugin);
mockApiCategory.configure(new ApiCategoryConfiguration(), getApplicationContext());
mockApiCategory.initialize(getApplicationContext());
return mockApiCategory;
}
/**
* Almost the same as mockApiCategoryWithGraphQlApi, but it calls the onError callback instead.
*
* @return A mock version of the API Category.
* @throws AmplifyException Throw if an error happens when adding the plugin.
*/
@SuppressWarnings("unchecked")
private static ApiCategory mockApiPluginWithExceptions() throws AmplifyException {
ApiCategory mockApiCategory = spy(ApiCategory.class);
ApiPlugin<?> mockApiPlugin = mock(ApiPlugin.class);
when(mockApiPlugin.getPluginKey()).thenReturn(MOCK_API_PLUGIN_NAME);
when(mockApiPlugin.getCategoryType()).thenReturn(CategoryType.API);
doAnswer(invocation -> {
int indexOfErrorConsumer = 2;
Consumer<ApiException> onError = invocation.getArgument(indexOfErrorConsumer);
onError.accept(new ApiException("Fake exception thrown from the API.query method", "Just retry"));
return null;
}).when(mockApiPlugin).query(any(GraphQLRequest.class), any(Consumer.class), any(Consumer.class));
doAnswer(invocation -> {
int indexOfErrorConsumer = 2;
Consumer<ApiException> onError = invocation.getArgument(indexOfErrorConsumer);
onError.accept(new ApiException("Fake exception thrown from the API.mutate method", "Just retry"));
return null;
}).when(mockApiPlugin).mutate(any(GraphQLRequest.class), any(Consumer.class), any(Consumer.class));
doAnswer(invocation -> {
int indexOfErrorConsumer = 3;
Consumer<ApiException> onError = invocation.getArgument(indexOfErrorConsumer);
ApiException apiException =
new ApiException("Fake exception thrown from the API.subscribe method", "Just retry");
onError.accept(apiException);
return null;
}).when(mockApiPlugin).subscribe(
any(GraphQLRequest.class),
any(Consumer.class),
any(Consumer.class),
any(Consumer.class),
any(Action.class)
);
mockApiCategory.addPlugin(mockApiPlugin);
return mockApiCategory;
}
/**
* Verify that the observe api returns itemChanged which matches the predicate.
*
* @throws JSONException on failure to arrange plugin config.
* @throws AmplifyException on failure to arrange API plugin via Amplify facade.
* @throws InterruptedException If interrupted while test observer awaits terminal result.
*/
@Test
public void observeWithMatchingPredicate() throws InterruptedException, AmplifyException, JSONException {
AWSDataStorePlugin awsDataStorePlugin = AWSDataStorePlugin.builder()
.modelProvider(modelProvider)
.build();
JSONObject dataStorePluginJson = new JSONObject()
.put("syncIntervalInMinutes", 60);
awsDataStorePlugin.configure(dataStorePluginJson, context);
awsDataStorePlugin.initialize(context);
Amplify.Hub.publish(HubChannel.DATASTORE, HubEvent.create(InitializationStatus.SUCCEEDED));
SynchronousDataStore synchronousDataStore = SynchronousDataStore.delegatingTo(awsDataStorePlugin);
final CountDownLatch latch = new CountDownLatch(1);
Person expectedResult = createPerson("Test", "Dummy I");
final Person[] actualResult = {null};
Consumer<DataStoreItemChange<Person>> onObserveResult = spy(new Consumer<DataStoreItemChange<Person>>() {
@Override
public void accept(@NonNull DataStoreItemChange<Person> value) {
latch.countDown();
actualResult[0] = value.item();
}
});
awsDataStorePlugin.observe(Person.class,
Person.FIRST_NAME.eq("Test"),
value -> { },
onObserveResult,
error -> {
LOG.error("Error: " + error);
},
() -> { }
);
synchronousDataStore.save(expectedResult);
latch.await(TIMEOUT_MS, TimeUnit.SECONDS);
verify(onObserveResult).accept(any());
assertEquals(actualResult[0], expectedResult);
}
/**
* Verify that the observe api is not invoke when the item changed does not match the predicate.
*
* @throws JSONException on failure to arrange plugin config.
* @throws AmplifyException on failure to arrange API plugin via Amplify facade.
* @throws InterruptedException If interrupted while test observer awaits terminal result.
*/
@Test
public void observeWithoutMatchingPredicate() throws InterruptedException, AmplifyException, JSONException {
AWSDataStorePlugin awsDataStorePlugin = AWSDataStorePlugin.builder()
.modelProvider(modelProvider)
.build();
JSONObject dataStorePluginJson = new JSONObject()
.put("syncIntervalInMinutes", 60);
awsDataStorePlugin.configure(dataStorePluginJson, context);
awsDataStorePlugin.initialize(context);
Amplify.Hub.publish(HubChannel.DATASTORE, HubEvent.create(InitializationStatus.SUCCEEDED));
SynchronousDataStore synchronousDataStore = SynchronousDataStore.delegatingTo(awsDataStorePlugin);
Person expectedResult = createPerson("Test", "Dummy I");
final Person[] actualResult = {null};
Consumer<DataStoreItemChange<Person>> onObserveResult = spy(new Consumer<DataStoreItemChange<Person>>() {
@Override
public void accept(@NonNull DataStoreItemChange<Person> value) {
actualResult[0] = value.item();
}
});
awsDataStorePlugin.observe(Person.class,
Person.FIRST_NAME.eq("NO MATCH"),
value -> { },
onObserveResult,
error -> {
LOG.error("Error: " + error);
},
() -> { }
);
synchronousDataStore.save(expectedResult);
Thread.sleep(1000L);
verify(onObserveResult, never()).accept(any());
}
private Person createPerson(String firstName, String lastName) {
return Person.builder()
.firstName(firstName)
.lastName(lastName)
.age(41)
.build();
}
private static ArgumentMatcher<GraphQLRequest<ModelWithMetadata<Person>>> getMatcherFor(Person person) {
return graphQLRequest -> {
try {
JSONObject payload = new JSONObject(graphQLRequest.getContent());
String modelIdInRequest = payload.getJSONObject("variables").getJSONObject("input").getString("id");
return person.getId().equals(modelIdInRequest);
} catch (JSONException exception) {
fail("Invalid GraphQLRequest payload." + exception.getMessage());
}
return false;
};
}
}