|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.firebase.remoteconfig; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertSame; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | +import static org.junit.Assert.fail; |
| 23 | + |
| 24 | +import com.google.common.base.Supplier; |
| 25 | +import com.google.common.base.Suppliers; |
| 26 | +import com.google.firebase.ErrorCode; |
| 27 | +import com.google.firebase.FirebaseApp; |
| 28 | +import com.google.firebase.FirebaseOptions; |
| 29 | +import com.google.firebase.TestOnlyImplFirebaseTrampolines; |
| 30 | +import com.google.firebase.auth.MockGoogleCredentials; |
| 31 | +import java.util.concurrent.ExecutionException; |
| 32 | +import org.junit.After; |
| 33 | +import org.junit.Test; |
| 34 | + |
| 35 | +public class FirebaseRemoteConfigTest { |
| 36 | + private static final FirebaseOptions TEST_OPTIONS = FirebaseOptions.builder() |
| 37 | + .setCredentials(new MockGoogleCredentials("test-token")) |
| 38 | + .setProjectId("test-project") |
| 39 | + .build(); |
| 40 | + private static final FirebaseRemoteConfigException TEST_EXCEPTION = |
| 41 | + new FirebaseRemoteConfigException(ErrorCode.INTERNAL, "Test error message"); |
| 42 | + |
| 43 | + @After |
| 44 | + public void tearDown() { |
| 45 | + TestOnlyImplFirebaseTrampolines.clearInstancesForTest(); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testGetInstance() { |
| 50 | + FirebaseApp.initializeApp(TEST_OPTIONS); |
| 51 | + |
| 52 | + FirebaseRemoteConfig remoteConfig = FirebaseRemoteConfig.getInstance(); |
| 53 | + |
| 54 | + assertSame(remoteConfig, FirebaseRemoteConfig.getInstance()); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testGetInstanceByApp() { |
| 59 | + FirebaseApp app = FirebaseApp.initializeApp(TEST_OPTIONS, "custom-app"); |
| 60 | + |
| 61 | + FirebaseRemoteConfig remoteConfig = FirebaseRemoteConfig.getInstance(app); |
| 62 | + |
| 63 | + assertSame(remoteConfig, FirebaseRemoteConfig.getInstance(app)); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testDefaultRemoteConfigClient() { |
| 68 | + FirebaseApp app = FirebaseApp.initializeApp(TEST_OPTIONS, "custom-app"); |
| 69 | + FirebaseRemoteConfig remoteConfig = FirebaseRemoteConfig.getInstance(app); |
| 70 | + |
| 71 | + FirebaseRemoteConfigClient client = remoteConfig.getRemoteConfigClient(); |
| 72 | + |
| 73 | + assertTrue(client instanceof FirebaseRemoteConfigClientImpl); |
| 74 | + assertSame(client, remoteConfig.getRemoteConfigClient()); |
| 75 | + String expectedUrl = "https://firebaseremoteconfig.googleapis.com/v1/projects/test-project/remoteConfig"; |
| 76 | + assertEquals(expectedUrl, ((FirebaseRemoteConfigClientImpl) client).getRemoteConfigUrl()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testPostDeleteApp() { |
| 81 | + FirebaseApp app = FirebaseApp.initializeApp(TEST_OPTIONS, "custom-app"); |
| 82 | + |
| 83 | + app.delete(); |
| 84 | + |
| 85 | + try { |
| 86 | + FirebaseRemoteConfig.getInstance(app); |
| 87 | + fail("No error thrown for deleted app"); |
| 88 | + } catch (IllegalStateException expected) { |
| 89 | + // expected |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testRemoteConfigClientWithoutProjectId() { |
| 95 | + FirebaseOptions options = FirebaseOptions.builder() |
| 96 | + .setCredentials(new MockGoogleCredentials("test-token")) |
| 97 | + .build(); |
| 98 | + FirebaseApp.initializeApp(options); |
| 99 | + FirebaseRemoteConfig remoteConfig = FirebaseRemoteConfig.getInstance(); |
| 100 | + |
| 101 | + try { |
| 102 | + remoteConfig.getRemoteConfigClient(); |
| 103 | + fail("No error thrown for missing project ID"); |
| 104 | + } catch (IllegalArgumentException expected) { |
| 105 | + String message = "Project ID is required to access Remote Config service. Use a service " |
| 106 | + + "account credential or set the project ID explicitly via FirebaseOptions. " |
| 107 | + + "Alternatively you can also set the project ID via the GOOGLE_CLOUD_PROJECT " |
| 108 | + + "environment variable."; |
| 109 | + assertEquals(message, expected.getMessage()); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private static final String TEST_ETAG = "etag-123456789012-1"; |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testGetTemplate() throws FirebaseRemoteConfigException { |
| 117 | + MockRemoteConfigClient client = MockRemoteConfigClient.fromTemplate( |
| 118 | + new RemoteConfigTemplate().setETag(TEST_ETAG)); |
| 119 | + FirebaseRemoteConfig remoteConfig = getRemoteConfig(Suppliers.ofInstance(client)); |
| 120 | + |
| 121 | + RemoteConfigTemplate template = remoteConfig.getTemplate(); |
| 122 | + |
| 123 | + assertEquals(TEST_ETAG, template.getETag()); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testGetTemplateFailure() { |
| 128 | + MockRemoteConfigClient client = MockRemoteConfigClient.fromException(TEST_EXCEPTION); |
| 129 | + FirebaseRemoteConfig remoteConfig = getRemoteConfig(Suppliers.ofInstance(client)); |
| 130 | + |
| 131 | + try { |
| 132 | + remoteConfig.getTemplate(); |
| 133 | + } catch (FirebaseRemoteConfigException e) { |
| 134 | + assertSame(TEST_EXCEPTION, e); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void testGetTemplateAsync() throws Exception { |
| 140 | + MockRemoteConfigClient client = MockRemoteConfigClient.fromTemplate( |
| 141 | + new RemoteConfigTemplate().setETag(TEST_ETAG)); |
| 142 | + FirebaseRemoteConfig remoteConfig = getRemoteConfig(Suppliers.ofInstance(client)); |
| 143 | + |
| 144 | + RemoteConfigTemplate template = remoteConfig.getTemplateAsync().get(); |
| 145 | + |
| 146 | + assertEquals(TEST_ETAG, template.getETag()); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void testGetTemplateAsyncFailure() throws InterruptedException { |
| 151 | + MockRemoteConfigClient client = MockRemoteConfigClient.fromException(TEST_EXCEPTION); |
| 152 | + FirebaseRemoteConfig remoteConfig = getRemoteConfig(Suppliers.ofInstance(client)); |
| 153 | + |
| 154 | + try { |
| 155 | + remoteConfig.getTemplateAsync().get(); |
| 156 | + } catch (ExecutionException e) { |
| 157 | + assertSame(TEST_EXCEPTION, e.getCause()); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private FirebaseRemoteConfig getRemoteConfig( |
| 162 | + Supplier<? extends FirebaseRemoteConfigClient> supplier) { |
| 163 | + FirebaseApp app = FirebaseApp.initializeApp(TEST_OPTIONS); |
| 164 | + return FirebaseRemoteConfig.builder() |
| 165 | + .setFirebaseApp(app) |
| 166 | + .setRemoteConfigClient(supplier) |
| 167 | + .build(); |
| 168 | + } |
| 169 | +} |
0 commit comments