Search in sources :

Example 1 with ApiCategoryConfiguration

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

the class RxApiBindingTest method createBindingInFrontOfMockPlugin.

/**
 * To test the binding, we construct a category that has been configured
 * with a mock plugin. The binding delegates to the category.
 * @throws AmplifyException On failure to add plugin or configure category
 */
@Before
public void createBindingInFrontOfMockPlugin() throws AmplifyException {
    // Mock plugin on which we will simulate API responses/failures
    this.delegate = mock(ApiPlugin.class);
    when(delegate.getPluginKey()).thenReturn(RandomString.string());
    // Build a category, add the mock plugin, configure and init the category.
    final ApiCategory apiCategory = new ApiCategory();
    apiCategory.addPlugin(delegate);
    apiCategory.configure(new ApiCategoryConfiguration(), mock(Context.class));
    apiCategory.initialize(mock(Context.class));
    // Provide that category as a backing to our binding.
    this.rxApi = new RxApiBinding(apiCategory);
}
Also used : Context(android.content.Context) ApiPlugin(com.amplifyframework.api.ApiPlugin) ApiCategory(com.amplifyframework.api.ApiCategory) ApiCategoryConfiguration(com.amplifyframework.api.ApiCategoryConfiguration) Before(org.junit.Before)

Example 2 with ApiCategoryConfiguration

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

the class AmplifyConfiguration method configsFromJson.

private static Map<String, CategoryConfiguration> configsFromJson(JSONObject json) throws AmplifyException {
    final List<CategoryConfiguration> possibleConfigs = Arrays.asList(new AnalyticsCategoryConfiguration(), new ApiCategoryConfiguration(), new AuthCategoryConfiguration(), new DataStoreCategoryConfiguration(), new GeoCategoryConfiguration(), new HubCategoryConfiguration(), new LoggingCategoryConfiguration(), new PredictionsCategoryConfiguration(), new StorageCategoryConfiguration());
    final Map<String, CategoryConfiguration> actualConfigs = new HashMap<>();
    try {
        for (CategoryConfiguration possibleConfig : possibleConfigs) {
            String categoryJsonKey = possibleConfig.getCategoryType().getConfigurationKey();
            if (json.has(categoryJsonKey)) {
                possibleConfig.populateFromJSON(json.getJSONObject(categoryJsonKey));
                actualConfigs.put(categoryJsonKey, possibleConfig);
            }
        }
    } catch (JSONException error) {
        throw new AmplifyException("Could not parse amplifyconfiguration.json ", error, "Check any modifications made to the file.");
    }
    return Immutable.of(actualConfigs);
}
Also used : DataStoreCategoryConfiguration(com.amplifyframework.datastore.DataStoreCategoryConfiguration) StorageCategoryConfiguration(com.amplifyframework.storage.StorageCategoryConfiguration) AmplifyException(com.amplifyframework.AmplifyException) PredictionsCategoryConfiguration(com.amplifyframework.predictions.PredictionsCategoryConfiguration) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AuthCategoryConfiguration(com.amplifyframework.auth.AuthCategoryConfiguration) LoggingCategoryConfiguration(com.amplifyframework.logging.LoggingCategoryConfiguration) AnalyticsCategoryConfiguration(com.amplifyframework.analytics.AnalyticsCategoryConfiguration) AuthCategoryConfiguration(com.amplifyframework.auth.AuthCategoryConfiguration) CategoryConfiguration(com.amplifyframework.core.category.CategoryConfiguration) GeoCategoryConfiguration(com.amplifyframework.geo.GeoCategoryConfiguration) DataStoreCategoryConfiguration(com.amplifyframework.datastore.DataStoreCategoryConfiguration) HubCategoryConfiguration(com.amplifyframework.hub.HubCategoryConfiguration) PredictionsCategoryConfiguration(com.amplifyframework.predictions.PredictionsCategoryConfiguration) StorageCategoryConfiguration(com.amplifyframework.storage.StorageCategoryConfiguration) ApiCategoryConfiguration(com.amplifyframework.api.ApiCategoryConfiguration) EmptyCategoryConfiguration(com.amplifyframework.core.category.EmptyCategoryConfiguration) JSONException(org.json.JSONException) GeoCategoryConfiguration(com.amplifyframework.geo.GeoCategoryConfiguration) AnalyticsCategoryConfiguration(com.amplifyframework.analytics.AnalyticsCategoryConfiguration) ApiCategoryConfiguration(com.amplifyframework.api.ApiCategoryConfiguration) HubCategoryConfiguration(com.amplifyframework.hub.HubCategoryConfiguration) LoggingCategoryConfiguration(com.amplifyframework.logging.LoggingCategoryConfiguration)

Example 3 with ApiCategoryConfiguration

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

the class AWSDataStorePluginTest 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));
    // 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;
}
Also used : ModelWithMetadata(com.amplifyframework.datastore.appsync.ModelWithMetadata) Action(com.amplifyframework.core.Action) PaginatedResult(com.amplifyframework.api.graphql.PaginatedResult) GraphQLResponse(com.amplifyframework.api.graphql.GraphQLResponse) RandomString(com.amplifyframework.testutils.random.RandomString) 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 4 with ApiCategoryConfiguration

use of com.amplifyframework.api.ApiCategoryConfiguration 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)

Aggregations

ApiCategoryConfiguration (com.amplifyframework.api.ApiCategoryConfiguration)4 ApiCategory (com.amplifyframework.api.ApiCategory)3 ApiEndpointStatusChangeEvent (com.amplifyframework.api.events.ApiEndpointStatusChangeEvent)2 GraphQLRequest (com.amplifyframework.api.graphql.GraphQLRequest)2 GraphQLResponse (com.amplifyframework.api.graphql.GraphQLResponse)2 PaginatedResult (com.amplifyframework.api.graphql.PaginatedResult)2 Consumer (com.amplifyframework.core.Consumer)2 ModelWithMetadata (com.amplifyframework.datastore.appsync.ModelWithMetadata)2 Context (android.content.Context)1 AmplifyException (com.amplifyframework.AmplifyException)1 AnalyticsCategoryConfiguration (com.amplifyframework.analytics.AnalyticsCategoryConfiguration)1 ApiPlugin (com.amplifyframework.api.ApiPlugin)1 AuthCategoryConfiguration (com.amplifyframework.auth.AuthCategoryConfiguration)1 Action (com.amplifyframework.core.Action)1 CategoryConfiguration (com.amplifyframework.core.category.CategoryConfiguration)1 EmptyCategoryConfiguration (com.amplifyframework.core.category.EmptyCategoryConfiguration)1 DataStoreCategoryConfiguration (com.amplifyframework.datastore.DataStoreCategoryConfiguration)1 GeoCategoryConfiguration (com.amplifyframework.geo.GeoCategoryConfiguration)1 HubCategoryConfiguration (com.amplifyframework.hub.HubCategoryConfiguration)1 LoggingCategoryConfiguration (com.amplifyframework.logging.LoggingCategoryConfiguration)1