Search in sources :

Example 16 with ApiCategory

use of com.amplifyframework.api.ApiCategory in project amplify-android by aws-amplify.

the class ConflictResolverIntegrationTest method mockApiCategoryWithGraphQlApi.

@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));
    mockApiCategory.addPlugin(mockApiPlugin);
    mockApiCategory.configure(new ApiCategoryConfiguration(), getApplicationContext());
    mockApiCategory.initialize(getApplicationContext());
    return mockApiCategory;
}
Also used : ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) GraphQLRequest(com.amplifyframework.api.graphql.GraphQLRequest) Consumer(com.amplifyframework.core.Consumer) ApiEndpointStatusChangeEvent(com.amplifyframework.api.events.ApiEndpointStatusChangeEvent) ApiCategory(com.amplifyframework.api.ApiCategory) ApiCategoryConfiguration(com.amplifyframework.api.ApiCategoryConfiguration)

Example 17 with ApiCategory

use of com.amplifyframework.api.ApiCategory in project amplify-android by aws-amplify.

the class AppSyncClientInstrumentationTest method onceBeforeTests.

/**
 * Configure Amplify for API tests, if it has not been configured, yet.
 * @throws AmplifyException From Amplify configuration
 */
@BeforeClass
public static void onceBeforeTests() throws AmplifyException {
    Context context = getApplicationContext();
    @RawRes int resourceId = Resources.getRawResourceId(context, "amplifyconfiguration");
    ApiCategory asyncDelegate = new ApiCategory();
    asyncDelegate.addPlugin(new AWSApiPlugin());
    asyncDelegate.configure(AmplifyConfiguration.fromConfigFile(context, resourceId).forCategoryType(CategoryType.API), context);
    asyncDelegate.initialize(context);
    api = AppSyncClient.via(asyncDelegate);
}
Also used : Context(android.content.Context) ApplicationProvider.getApplicationContext(androidx.test.core.app.ApplicationProvider.getApplicationContext) AWSApiPlugin(com.amplifyframework.api.aws.AWSApiPlugin) RawRes(androidx.annotation.RawRes) ApiCategory(com.amplifyframework.api.ApiCategory) BeforeClass(org.junit.BeforeClass)

Example 18 with ApiCategory

use of com.amplifyframework.api.ApiCategory in project amplify-android by aws-amplify.

the class BasicCloudSyncInstrumentationTest method setup.

/**
 * Once, before any/all tests in this class, setup miscellaneous dependencies,
 * including synchronous API, AppSync, and DataStore interfaces. The API and AppSync instances
 * are used to arrange/validate data. The DataStore interface will delegate to an
 * {@link AWSDataStorePlugin}, which is the thing we're actually testing.
 * @throws AmplifyException On failure to read config, setup API or DataStore categories
 */
@BeforeClass
public static void setup() throws AmplifyException {
    Amplify.addPlugin(new AndroidLoggingPlugin(LogLevel.VERBOSE));
    StrictMode.enable();
    Context context = getApplicationContext();
    @RawRes int configResourceId = Resources.getRawResourceId(context, "amplifyconfiguration");
    // Setup an API
    CategoryConfiguration apiCategoryConfiguration = AmplifyConfiguration.fromConfigFile(context, configResourceId).forCategoryType(CategoryType.API);
    ApiCategory apiCategory = new ApiCategory();
    apiCategory.addPlugin(new AWSApiPlugin());
    apiCategory.configure(apiCategoryConfiguration, context);
    // To arrange and verify state, we need to access the supporting AppSync API
    api = SynchronousApi.delegatingTo(apiCategory);
    appSync = SynchronousAppSync.using(AppSyncClient.via(apiCategory));
    long tenMinutesAgo = new Date().getTime() - TimeUnit.MINUTES.toMillis(10);
    Temporal.DateTime tenMinutesAgoDateTime = new Temporal.DateTime(new Date(tenMinutesAgo), 0);
    DataStoreCategory dataStoreCategory = DataStoreCategoryConfigurator.begin().api(apiCategory).clearDatabase(true).context(context).modelProvider(AmplifyModelProvider.getInstance()).resourceId(configResourceId).timeout(TIMEOUT_SECONDS, TimeUnit.SECONDS).dataStoreConfiguration(DataStoreConfiguration.builder().syncExpression(BlogOwner.class, () -> BlogOwner.CREATED_AT.gt(tenMinutesAgoDateTime)).syncExpression(Blog.class, () -> Blog.CREATED_AT.gt(tenMinutesAgoDateTime)).syncExpression(Post.class, () -> Post.CREATED_AT.gt(tenMinutesAgoDateTime)).syncExpression(Comment.class, () -> Comment.CREATED_AT.gt(tenMinutesAgoDateTime)).syncExpression(Author.class, () -> Author.CREATED_AT.gt(tenMinutesAgoDateTime)).syncExpression(PostAuthorJoin.class, () -> PostAuthorJoin.CREATED_AT.gt(tenMinutesAgoDateTime)).build()).finish();
    dataStore = SynchronousDataStore.delegatingTo(dataStoreCategory);
}
Also used : ApplicationProvider.getApplicationContext(androidx.test.core.app.ApplicationProvider.getApplicationContext) Context(android.content.Context) Comment(com.amplifyframework.testmodels.commentsblog.Comment) RawRes(androidx.annotation.RawRes) CategoryConfiguration(com.amplifyframework.core.category.CategoryConfiguration) PostAuthorJoin(com.amplifyframework.testmodels.commentsblog.PostAuthorJoin) AndroidLoggingPlugin(com.amplifyframework.logging.AndroidLoggingPlugin) Date(java.util.Date) AWSApiPlugin(com.amplifyframework.api.aws.AWSApiPlugin) Temporal(com.amplifyframework.core.model.temporal.Temporal) ApiCategory(com.amplifyframework.api.ApiCategory) Blog(com.amplifyframework.testmodels.commentsblog.Blog) BeforeClass(org.junit.BeforeClass)

Example 19 with ApiCategory

use of com.amplifyframework.api.ApiCategory in project amplify-android by aws-amplify.

the class CodeGenerationInstrumentationTest method onceBeforeTests.

/**
 * Configure Amplify for API tests, if it has not been configured, yet.
 * @throws AmplifyException From Amplify configuration
 */
@BeforeClass
public static void onceBeforeTests() throws AmplifyException {
    ApiCategory asyncDelegate = TestApiCategory.fromConfiguration(R.raw.amplifyconfiguration);
    api = SynchronousApi.delegatingTo(asyncDelegate);
}
Also used : ApiCategory(com.amplifyframework.api.ApiCategory) BeforeClass(org.junit.BeforeClass)

Example 20 with ApiCategory

use of com.amplifyframework.api.ApiCategory in project amplify-android by aws-amplify.

the class RestApiInstrumentationTest method setUp.

/**
 * Configure the Amplify framework and auth.
 * @throws AmplifyException if configuration fails
 * @throws SynchronousMobileClient.MobileClientException If AWS Mobile Client initialization fails
 */
@Before
public void setUp() throws AmplifyException, SynchronousMobileClient.MobileClientException {
    ApiCategory asyncDelegate = TestApiCategory.fromConfiguration(R.raw.amplifyconfiguration);
    api = SynchronousApi.delegatingTo(asyncDelegate);
    SynchronousMobileClient mobileClient = SynchronousMobileClient.instance();
    mobileClient.initialize();
}
Also used : ApiCategory(com.amplifyframework.api.ApiCategory) SynchronousMobileClient(com.amplifyframework.testutils.sync.SynchronousMobileClient) Before(org.junit.Before)

Aggregations

ApiCategory (com.amplifyframework.api.ApiCategory)21 JSONObject (org.json.JSONObject)9 Test (org.junit.Test)9 SynchronousDataStore (com.amplifyframework.testutils.sync.SynchronousDataStore)8 Context (android.content.Context)7 ApplicationProvider.getApplicationContext (androidx.test.core.app.ApplicationProvider.getApplicationContext)6 Person (com.amplifyframework.testmodels.personcar.Person)6 Before (org.junit.Before)6 RawRes (androidx.annotation.RawRes)5 AWSApiPlugin (com.amplifyframework.api.aws.AWSApiPlugin)5 GraphQLResponse (com.amplifyframework.api.graphql.GraphQLResponse)5 CategoryConfiguration (com.amplifyframework.core.category.CategoryConfiguration)5 ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)5 HubAccumulator (com.amplifyframework.testutils.HubAccumulator)5 ApiCategoryConfiguration (com.amplifyframework.api.ApiCategoryConfiguration)4 GraphQLRequest (com.amplifyframework.api.graphql.GraphQLRequest)4 Consumer (com.amplifyframework.core.Consumer)4 AndroidLoggingPlugin (com.amplifyframework.logging.AndroidLoggingPlugin)4 ApiEndpointStatusChangeEvent (com.amplifyframework.api.events.ApiEndpointStatusChangeEvent)3 PaginatedResult (com.amplifyframework.api.graphql.PaginatedResult)3