-
Couldn't load subscription status.
- Fork 298
Add the entry point for Remote Config #478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
src/main/java/com/google/firebase/remoteconfig/FirebaseRemoteConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| /* | ||
| * Copyright 2020 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License 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.google.firebase.remoteconfig; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkNotNull; | ||
|
|
||
| import com.google.api.core.ApiFuture; | ||
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.google.firebase.FirebaseApp; | ||
| import com.google.firebase.ImplFirebaseTrampolines; | ||
| import com.google.firebase.internal.CallableOperation; | ||
| import com.google.firebase.internal.FirebaseService; | ||
|
|
||
| /** | ||
| * This class is the entry point for all server-side Firebase Remote Config actions. | ||
| * | ||
| * <p>You can get an instance of {@link FirebaseRemoteConfig} via {@link #getInstance(FirebaseApp)}, | ||
| * and then use it to manage Remote Config templates. | ||
| */ | ||
| public final class FirebaseRemoteConfig { | ||
|
|
||
| private static final String SERVICE_ID = FirebaseRemoteConfig.class.getName(); | ||
| private final FirebaseApp app; | ||
| private final FirebaseRemoteConfigClient remoteConfigClient; | ||
|
|
||
| @VisibleForTesting | ||
| FirebaseRemoteConfig(FirebaseApp app, FirebaseRemoteConfigClient client) { | ||
| this.app = checkNotNull(app); | ||
| this.remoteConfigClient = checkNotNull(client); | ||
| } | ||
|
|
||
| private FirebaseRemoteConfig(FirebaseApp app) { | ||
| this(app, FirebaseRemoteConfigClientImpl.fromApp(app)); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the {@link FirebaseRemoteConfig} instance for the default {@link FirebaseApp}. | ||
| * | ||
| * @return The {@link FirebaseRemoteConfig} instance for the default {@link FirebaseApp}. | ||
| */ | ||
| public static FirebaseRemoteConfig getInstance() { | ||
| return getInstance(FirebaseApp.getInstance()); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the {@link FirebaseRemoteConfig} instance for the specified {@link FirebaseApp}. | ||
| * | ||
| * @return The {@link FirebaseRemoteConfig} instance for the specified {@link FirebaseApp}. | ||
| */ | ||
| public static synchronized FirebaseRemoteConfig getInstance(FirebaseApp app) { | ||
| FirebaseRemoteConfigService service = ImplFirebaseTrampolines.getService(app, SERVICE_ID, | ||
| FirebaseRemoteConfigService.class); | ||
| if (service == null) { | ||
| service = ImplFirebaseTrampolines.addService(app, new FirebaseRemoteConfigService(app)); | ||
| } | ||
| return service.getInstance(); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the current active version of the Remote Config template. | ||
| * | ||
| * @return A {@link RemoteConfigTemplate}. | ||
| * @throws FirebaseRemoteConfigException If an error occurs while getting the template. | ||
| */ | ||
| public RemoteConfigTemplate getTemplate() throws FirebaseRemoteConfigException { | ||
| return getTemplateOp().call(); | ||
| } | ||
|
|
||
| /** | ||
| * Similar to {@link #getTemplate()} but performs the operation asynchronously. | ||
| * | ||
| * @return An {@code ApiFuture} that completes with a {@link RemoteConfigTemplate} when | ||
| * the template is available. | ||
| */ | ||
| public ApiFuture<RemoteConfigTemplate> getTemplateAsync() { | ||
| return getTemplateOp().callAsync(app); | ||
| } | ||
|
|
||
| private CallableOperation<RemoteConfigTemplate, FirebaseRemoteConfigException> getTemplateOp() { | ||
| final FirebaseRemoteConfigClient remoteConfigClient = getRemoteConfigClient(); | ||
| return new CallableOperation<RemoteConfigTemplate, FirebaseRemoteConfigException>() { | ||
| @Override | ||
| protected RemoteConfigTemplate execute() throws FirebaseRemoteConfigException { | ||
| return remoteConfigClient.getTemplate(); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| FirebaseRemoteConfigClient getRemoteConfigClient() { | ||
| return remoteConfigClient; | ||
| } | ||
|
|
||
| private static class FirebaseRemoteConfigService extends FirebaseService<FirebaseRemoteConfig> { | ||
|
|
||
| FirebaseRemoteConfigService(FirebaseApp app) { | ||
| super(SERVICE_ID, new FirebaseRemoteConfig(app)); | ||
| } | ||
|
|
||
| @Override | ||
| public void destroy() { | ||
| // NOTE: We don't explicitly tear down anything here, but public methods of | ||
| // FirebaseRemoteConfig will now fail because calls to getOptions() and getToken() | ||
| // will hit FirebaseApp, which will throw once the app is deleted. | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| /* | ||
| * Copyright 2020 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License 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.google.firebase.remoteconfig; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertSame; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.junit.Assert.fail; | ||
|
|
||
| import com.google.firebase.ErrorCode; | ||
| import com.google.firebase.FirebaseApp; | ||
| import com.google.firebase.FirebaseOptions; | ||
| import com.google.firebase.TestOnlyImplFirebaseTrampolines; | ||
| import com.google.firebase.auth.MockGoogleCredentials; | ||
| import java.util.concurrent.ExecutionException; | ||
| import org.junit.After; | ||
| import org.junit.Test; | ||
|
|
||
| public class FirebaseRemoteConfigTest { | ||
|
|
||
| private static final FirebaseOptions TEST_OPTIONS = FirebaseOptions.builder() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add space before |
||
| .setCredentials(new MockGoogleCredentials("test-token")) | ||
| .setProjectId("test-project") | ||
| .build(); | ||
| private static final FirebaseRemoteConfigException TEST_EXCEPTION = | ||
| new FirebaseRemoteConfigException(ErrorCode.INTERNAL, "Test error message"); | ||
|
|
||
| @After | ||
| public void tearDown() { | ||
| TestOnlyImplFirebaseTrampolines.clearInstancesForTest(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetInstance() { | ||
| FirebaseApp.initializeApp(TEST_OPTIONS); | ||
|
|
||
| FirebaseRemoteConfig remoteConfig = FirebaseRemoteConfig.getInstance(); | ||
|
|
||
| assertSame(remoteConfig, FirebaseRemoteConfig.getInstance()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetInstanceByApp() { | ||
| FirebaseApp app = FirebaseApp.initializeApp(TEST_OPTIONS, "custom-app"); | ||
|
|
||
| FirebaseRemoteConfig remoteConfig = FirebaseRemoteConfig.getInstance(app); | ||
|
|
||
| assertSame(remoteConfig, FirebaseRemoteConfig.getInstance(app)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDefaultRemoteConfigClient() { | ||
| FirebaseApp app = FirebaseApp.initializeApp(TEST_OPTIONS, "custom-app"); | ||
| FirebaseRemoteConfig remoteConfig = FirebaseRemoteConfig.getInstance(app); | ||
|
|
||
| FirebaseRemoteConfigClient client = remoteConfig.getRemoteConfigClient(); | ||
|
|
||
| assertTrue(client instanceof FirebaseRemoteConfigClientImpl); | ||
| assertSame(client, remoteConfig.getRemoteConfigClient()); | ||
| String expectedUrl = "https://firebaseremoteconfig.googleapis.com/v1/projects/test-project/remoteConfig"; | ||
| assertEquals(expectedUrl, ((FirebaseRemoteConfigClientImpl) client).getRemoteConfigUrl()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAppDelete() { | ||
| FirebaseApp app = FirebaseApp.initializeApp(TEST_OPTIONS, "custom-app"); | ||
| FirebaseRemoteConfig remoteConfig = FirebaseRemoteConfig.getInstance(app); | ||
| assertNotNull(remoteConfig); | ||
|
|
||
| app.delete(); | ||
|
|
||
| try { | ||
| FirebaseRemoteConfig.getInstance(app); | ||
| fail("No error thrown when getting remote config instance after deleting app"); | ||
| } catch (IllegalStateException expected) { | ||
| // expected | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoteConfigClientWithoutProjectId() { | ||
| FirebaseOptions options = FirebaseOptions.builder() | ||
| .setCredentials(new MockGoogleCredentials("test-token")) | ||
| .build(); | ||
| FirebaseApp.initializeApp(options); | ||
|
|
||
| try { | ||
| FirebaseRemoteConfig.getInstance(); | ||
| fail("No error thrown for missing project ID"); | ||
| } catch (IllegalArgumentException expected) { | ||
| String message = "Project ID is required to access Remote Config service. Use a service " | ||
| + "account credential or set the project ID explicitly via FirebaseOptions. " | ||
| + "Alternatively you can also set the project ID via the GOOGLE_CLOUD_PROJECT " | ||
| + "environment variable."; | ||
| assertEquals(message, expected.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private static final String TEST_ETAG = "etag-123456789012-1"; | ||
|
|
||
| @Test | ||
| public void testGetTemplate() throws FirebaseRemoteConfigException { | ||
| MockRemoteConfigClient client = MockRemoteConfigClient.fromTemplate( | ||
| new RemoteConfigTemplate().setETag(TEST_ETAG)); | ||
| FirebaseRemoteConfig remoteConfig = getRemoteConfig(client); | ||
|
|
||
| RemoteConfigTemplate template = remoteConfig.getTemplate(); | ||
|
|
||
| assertEquals(TEST_ETAG, template.getETag()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetTemplateFailure() { | ||
| MockRemoteConfigClient client = MockRemoteConfigClient.fromException(TEST_EXCEPTION); | ||
| FirebaseRemoteConfig remoteConfig = getRemoteConfig(client); | ||
|
|
||
| try { | ||
| remoteConfig.getTemplate(); | ||
| } catch (FirebaseRemoteConfigException e) { | ||
| assertSame(TEST_EXCEPTION, e); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetTemplateAsync() throws Exception { | ||
| MockRemoteConfigClient client = MockRemoteConfigClient.fromTemplate( | ||
| new RemoteConfigTemplate().setETag(TEST_ETAG)); | ||
| FirebaseRemoteConfig remoteConfig = getRemoteConfig(client); | ||
|
|
||
| RemoteConfigTemplate template = remoteConfig.getTemplateAsync().get(); | ||
|
|
||
| assertEquals(TEST_ETAG, template.getETag()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetTemplateAsyncFailure() throws InterruptedException { | ||
| MockRemoteConfigClient client = MockRemoteConfigClient.fromException(TEST_EXCEPTION); | ||
| FirebaseRemoteConfig remoteConfig = getRemoteConfig(client); | ||
|
|
||
| try { | ||
| remoteConfig.getTemplateAsync().get(); | ||
| } catch (ExecutionException e) { | ||
| assertSame(TEST_EXCEPTION, e.getCause()); | ||
| } | ||
| } | ||
|
|
||
| private FirebaseRemoteConfig getRemoteConfig(FirebaseRemoteConfigClient client) { | ||
| FirebaseApp app = FirebaseApp.initializeApp(TEST_OPTIONS); | ||
| return new FirebaseRemoteConfig(app, client); | ||
| } | ||
| } | ||
45 changes: 45 additions & 0 deletions
45
src/test/java/com/google/firebase/remoteconfig/MockRemoteConfigClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * Copyright 2020 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License 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.google.firebase.remoteconfig; | ||
|
|
||
| public class MockRemoteConfigClient implements FirebaseRemoteConfigClient{ | ||
|
|
||
| private RemoteConfigTemplate resultTemplate; | ||
| private FirebaseRemoteConfigException exception; | ||
|
|
||
| private MockRemoteConfigClient(RemoteConfigTemplate resultTemplate, | ||
| FirebaseRemoteConfigException exception) { | ||
| this.resultTemplate = resultTemplate; | ||
| this.exception = exception; | ||
| } | ||
|
|
||
| static MockRemoteConfigClient fromTemplate(RemoteConfigTemplate resultTemplate) { | ||
| return new MockRemoteConfigClient(resultTemplate, null); | ||
| } | ||
|
|
||
| static MockRemoteConfigClient fromException(FirebaseRemoteConfigException exception) { | ||
| return new MockRemoteConfigClient(null, exception); | ||
| } | ||
|
|
||
| @Override | ||
| public RemoteConfigTemplate getTemplate() throws FirebaseRemoteConfigException { | ||
| if (exception != null) { | ||
| throw exception; | ||
| } | ||
| return resultTemplate; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.