use of com.amplifyframework.core.AmplifyConfiguration in project amplify-android by aws-amplify.
the class UserAgentConfigurationTest method setUpOnce.
/**
* Since Amplify can only be configured once at the time of writing this test,
* call {@link Amplify#configure(AmplifyConfiguration, Context)} once during
* this test suite.
* @throws AmplifyException if Amplify fails to configure.
*/
@BeforeClass
public static void setUpOnce() throws AmplifyException {
Context context = getApplicationContext();
AmplifyConfiguration config = AmplifyConfiguration.builder(context, R.raw.amplifyconfiguration).addPlatform(UserAgent.Platform.FLUTTER, BuildConfig.VERSION_NAME).build();
Amplify.configure(config, context);
}
use of com.amplifyframework.core.AmplifyConfiguration in project amplify-android by aws-amplify.
the class MultiAuthSyncEngineInstrumentationTest method configure.
/**
* Method used to configure each scenario.
* @param modelType The model type.
* @param signInToCognito Does the test scenario require the user to be logged in with user pools.
* @param signInWithOidc Does the test scenario require the user to be logged in with an OIDC provider.
* @param expectedAuthType The auth type that should succeed for the test.
* @throws AmplifyException No expected.
* @throws IOException Not expected.
*/
private void configure(Class<? extends Model> modelType, boolean signInToCognito, boolean signInWithOidc, AuthorizationType expectedAuthType) throws AmplifyException, IOException {
Amplify.addPlugin(new AndroidLoggingPlugin(LogLevel.VERBOSE));
String tag = modelType.getSimpleName();
MultiAuthTestModelProvider modelProvider = MultiAuthTestModelProvider.getInstance(Collections.singletonList(modelType));
SchemaRegistry schemaRegistry = SchemaRegistry.instance();
ModelSchema modelSchema = ModelSchema.fromModelClass(modelType);
schemaRegistry.register(modelType.getSimpleName(), modelSchema);
StrictMode.enable();
Context context = getApplicationContext();
@RawRes int configResourceId = Resources.getRawResourceId(context, "amplifyconfiguration");
AmplifyConfiguration amplifyConfiguration = AmplifyConfiguration.fromConfigFile(context, configResourceId);
readCredsFromConfig(context);
// Setup an auth plugin
CategoryConfiguration authCategoryConfiguration = amplifyConfiguration.forCategoryType(CategoryType.AUTH);
// Turn off persistence so the mobile client's state for one test does not interfere with the others.
try {
authCategoryConfiguration.getPluginConfig("awsCognitoAuthPlugin").getJSONObject("Auth").getJSONObject("Default").put("Persistence", false);
} catch (JSONException exception) {
exception.printStackTrace();
fail();
return;
}
AuthCategory authCategory = new AuthCategory();
AWSCognitoAuthPlugin authPlugin = new AWSCognitoAuthPlugin();
authCategory.addPlugin(authPlugin);
authCategory.configure(authCategoryConfiguration, context);
auth = SynchronousAuth.delegatingTo(authCategory);
if (signInToCognito) {
Log.v(tag, "Test requires signIn.");
AuthSignInResult authSignInResult = auth.signIn(cognitoUser, cognitoPassword);
if (!authSignInResult.isSignInComplete()) {
fail("Unable to complete initial sign-in");
}
}
if (signInWithOidc) {
oidcLogin();
if (token.get() == null) {
fail("Unable to autenticate with OIDC provider");
}
}
// Setup an API
DefaultCognitoUserPoolsAuthProvider cognitoProvider = new DefaultCognitoUserPoolsAuthProvider(authPlugin.getEscapeHatch());
CategoryConfiguration apiCategoryConfiguration = amplifyConfiguration.forCategoryType(CategoryType.API);
ApiAuthProviders apiAuthProviders = ApiAuthProviders.builder().cognitoUserPoolsAuthProvider(cognitoProvider).awsCredentialsProvider(authPlugin.getEscapeHatch()).oidcAuthProvider(token::get).build();
ApiCategory apiCategory = new ApiCategory();
requestInterceptor = new HttpRequestInterceptor(expectedAuthType);
apiCategory.addPlugin(AWSApiPlugin.builder().configureClient("DataStoreIntegTestsApi", okHttpClientBuilder -> okHttpClientBuilder.addInterceptor(requestInterceptor)).apiAuthProviders(apiAuthProviders).build());
apiCategory.configure(apiCategoryConfiguration, context);
api = SynchronousApi.delegatingTo(apiCategory);
// Setup DataStore
DataStoreConfiguration dsConfig = DataStoreConfiguration.builder().errorHandler(exception -> Log.e(tag, "DataStore error handler received an error.", exception)).syncExpression(modelSchema.getName(), () -> Where.id("FAKE_ID").getQueryPredicate()).build();
CategoryConfiguration dataStoreCategoryConfiguration = AmplifyConfiguration.fromConfigFile(context, configResourceId).forCategoryType(CategoryType.DATASTORE);
String databaseName = "IntegTest" + modelType.getSimpleName() + ".db";
SQLiteStorageAdapter sqLiteStorageAdapter = TestStorageAdapter.create(schemaRegistry, modelProvider, databaseName);
AWSDataStorePlugin awsDataStorePlugin = AWSDataStorePlugin.builder().storageAdapter(sqLiteStorageAdapter).modelProvider(modelProvider).apiCategory(apiCategory).authModeStrategy(AuthModeStrategyType.MULTIAUTH).schemaRegistry(schemaRegistry).dataStoreConfiguration(dsConfig).build();
DataStoreCategory dataStoreCategory = new DataStoreCategory();
dataStoreCategory.addPlugin(awsDataStorePlugin);
dataStoreCategory.configure(dataStoreCategoryConfiguration, context);
dataStoreCategory.initialize(context);
dataStore = SynchronousDataStore.delegatingTo(dataStoreCategory);
}
use of com.amplifyframework.core.AmplifyConfiguration in project amplify-android by aws-amplify.
the class RxAmplifyTest method canAddPluginsAndConfigure.
/**
* Calling {@link RxAmplify#addPlugin(Plugin)} and {@link RxAmplify#configure(AmplifyConfiguration, Context)}
* will pass config JSON down into the plugin via its {@link Plugin#configure(JSONObject, Context)}
* method.
* @throws AmplifyException Not exected; possible from RxAmplilfy's addPlugin(), configure().
* @throws JSONException Not expected; on failure to arrange test JSON inputs.
*/
@SuppressWarnings("unchecked")
@Test
public void canAddPluginsAndConfigure() throws AmplifyException, JSONException {
// Setup a mock plugin, add it to Amplify.
CategoryType categoryType = CategoryType.STORAGE;
String pluginKey = RandomString.string();
Plugin<Void> one = mock(Plugin.class);
when(one.getPluginKey()).thenReturn(pluginKey);
when(one.getCategoryType()).thenReturn(categoryType);
RxAmplify.addPlugin(one);
// Configure Amplify, with a config to match the plugin above.
Map<String, CategoryConfiguration> categoryConfigs = new HashMap<>();
String categoryName = categoryType.getConfigurationKey();
CategoryConfiguration categoryConfig = new DataStoreCategoryConfiguration();
JSONObject pluginJson = new JSONObject().put("someKey", "someVal");
categoryConfig.populateFromJSON(new JSONObject().put("plugins", new JSONObject().put(pluginKey, pluginJson)));
categoryConfigs.put(categoryName, categoryConfig);
AmplifyConfiguration config = new AmplifyConfiguration(categoryConfigs);
Context mockContext = mock(Context.class);
when(mockContext.getApplicationContext()).thenReturn(mockContext);
when(mockContext.getApplicationInfo()).thenReturn(new ApplicationInfo());
RxAmplify.configure(config, mockContext);
// Validate that the plugin gets configured with the provided JSON
ArgumentCaptor<JSONObject> configJsonCapture = ArgumentCaptor.forClass(JSONObject.class);
verify(one).configure(configJsonCapture.capture(), any(Context.class));
assertEquals(pluginJson, configJsonCapture.getValue());
}
Aggregations