-
Notifications
You must be signed in to change notification settings - Fork 300
Add Remote Config Integration tests #501
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
2 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
199 changes: 199 additions & 0 deletions
199
src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigIT.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,199 @@ | ||
| /* | ||
| * 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.assertNotEquals; | ||
| import static org.junit.Assert.assertNull; | ||
|
|
||
| import com.google.api.core.ApiFuture; | ||
| import com.google.api.core.ApiFutureCallback; | ||
| import com.google.api.core.ApiFutures; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import com.google.common.util.concurrent.MoreExecutors; | ||
| import com.google.firebase.testing.IntegrationTestUtils; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.Semaphore; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
|
|
||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
|
|
||
| public class FirebaseRemoteConfigIT { | ||
|
|
||
| private static FirebaseRemoteConfig remoteConfig; | ||
|
|
||
| private static final long timestamp = System.currentTimeMillis(); | ||
|
|
||
| private static final Map<String, Parameter> PARAMETERS = ImmutableMap.of( | ||
| "welcome_message_text", new Parameter() | ||
| .setDefaultValue(ParameterValue | ||
| .of(String.format("welcome to app %s", timestamp))) | ||
| .setConditionalValues(ImmutableMap.<String, ParameterValue>of( | ||
| "ios_en", | ||
| ParameterValue.of(String.format("welcome to app en %s", timestamp)) | ||
| )) | ||
| .setDescription("text for welcome message!"), | ||
| "header_text", new Parameter() | ||
| .setDefaultValue(ParameterValue.inAppDefault())); | ||
|
|
||
| private static final Map<String, ParameterGroup> PARAMETER_GROUPS = ImmutableMap.of( | ||
| "new menu", new ParameterGroup() | ||
| .setDescription(String.format("New Menu %s", timestamp)) | ||
| .setParameters(ImmutableMap.of( | ||
| "pumpkin_spice_season", new Parameter() | ||
| .setDefaultValue(ParameterValue.of("true")) | ||
| .setDescription("Whether it's currently pumpkin spice season.")) | ||
| )); | ||
|
|
||
| private static final List<Condition> CONDITIONS = ImmutableList.of( | ||
| new Condition("ios_en", "device.os == 'ios' && device.country in ['us', 'uk']") | ||
| .setTagColor(TagColor.INDIGO), | ||
| new Condition("android_en", | ||
| "device.os == 'android' && device.country in ['us', 'uk']")); | ||
|
|
||
| private static final Version VERSION = Version | ||
| .withDescription(String.format("promo config %s", timestamp)); | ||
|
|
||
| @BeforeClass | ||
| public static void setUpClass() { | ||
| remoteConfig = FirebaseRemoteConfig.getInstance(IntegrationTestUtils.ensureDefaultApp()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTemplateOperations() throws FirebaseRemoteConfigException { | ||
| // get template to fetch the active template with correct etag | ||
| final Template oldTemplate = remoteConfig.getTemplate(); | ||
| final Template inputTemplate = Template.fromJSON(oldTemplate.toJSON()); | ||
| final String versionNumber = oldTemplate.getVersion().getVersionNumber(); | ||
|
|
||
| // modify template | ||
| inputTemplate.setParameters(PARAMETERS) | ||
| .setParameterGroups(PARAMETER_GROUPS) | ||
| .setConditions(CONDITIONS) | ||
| .setVersion(VERSION); | ||
|
|
||
| // validate template | ||
| Template validatedTemplate = remoteConfig.validateTemplate(inputTemplate); | ||
| assertEquals(inputTemplate.getETag(), validatedTemplate.getETag()); | ||
| assertEquals(PARAMETERS, validatedTemplate.getParameters()); | ||
| assertEquals(PARAMETER_GROUPS, validatedTemplate.getParameterGroups()); | ||
| assertEquals(CONDITIONS, validatedTemplate.getConditions()); | ||
| assertEquals(VERSION, validatedTemplate.getVersion()); | ||
|
|
||
| // publish template | ||
| Template publishedTemplate = remoteConfig.publishTemplate(inputTemplate); | ||
| assertNotEquals(inputTemplate.getETag(), publishedTemplate.getETag()); | ||
| assertEquals(PARAMETERS, publishedTemplate.getParameters()); | ||
| assertEquals(PARAMETER_GROUPS, publishedTemplate.getParameterGroups()); | ||
| assertEquals(CONDITIONS, publishedTemplate.getConditions()); | ||
| assertNotEquals(VERSION, publishedTemplate.getVersion()); | ||
|
|
||
| // get template | ||
| Template currentTemplate = remoteConfig.getTemplate(); | ||
| assertEquals(publishedTemplate, currentTemplate); | ||
|
|
||
| // get template at version | ||
| Template atVersionTemplate = remoteConfig.getTemplateAtVersion(versionNumber); | ||
| assertEquals(oldTemplate, atVersionTemplate); | ||
| assertEquals(versionNumber, atVersionTemplate.getVersion().getVersionNumber()); | ||
|
|
||
| // rollback template | ||
| Template rolledBackTemplate = remoteConfig.rollback(versionNumber); | ||
| assertEquals(String.format("Rollback to version %s", versionNumber), | ||
| rolledBackTemplate.getVersion().getDescription()); | ||
|
|
||
| // get template to verify rollback | ||
| Template activeTemplate = remoteConfig.getTemplate(); | ||
| assertEquals(rolledBackTemplate, activeTemplate); | ||
| } | ||
|
|
||
| @Test | ||
| public void testListVersions() throws Exception { | ||
|
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. I'd suggest using the sync APIs here like the previous test case. 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. Updated to use the sync api |
||
| final List<String> versions = new ArrayList<>(); | ||
| Template template = remoteConfig.getTemplate(); | ||
| for (int i = 0; i < 3; i++) { | ||
| template = remoteConfig.publishTemplate(template); | ||
| versions.add(template.getVersion().getVersionNumber()); | ||
| } | ||
|
|
||
| // Test list by batches | ||
| final AtomicInteger collected = new AtomicInteger(0); | ||
| ListVersionsPage page = remoteConfig.listVersions(); | ||
| while (page != null) { | ||
| for (Version version : page.getValues()) { | ||
| if (versions.contains(version.getVersionNumber())) { | ||
| collected.incrementAndGet(); | ||
| } | ||
| } | ||
| page = page.getNextPage(); | ||
| } | ||
| assertEquals(versions.size(), collected.get()); | ||
|
|
||
| // Test iterate all | ||
| collected.set(0); | ||
| page = remoteConfig.listVersions(); | ||
| for (Version version : page.iterateAll()) { | ||
| if (versions.contains(version.getVersionNumber())) { | ||
| collected.incrementAndGet(); | ||
| } | ||
| } | ||
| assertEquals(versions.size(), collected.get()); | ||
|
|
||
| // Test with list options | ||
| collected.set(0); | ||
| ListVersionsOptions listOptions = ListVersionsOptions.builder().setPageSize(2).build(); | ||
| page = remoteConfig.listVersions(listOptions); | ||
| for (Version version : page.getValues()) { | ||
| if (versions.contains(version.getVersionNumber())) { | ||
| collected.incrementAndGet(); | ||
| } | ||
| } | ||
| assertEquals(2, collected.get()); | ||
|
|
||
| // Test iterate async | ||
| collected.set(0); | ||
| final Semaphore semaphore = new Semaphore(0); | ||
| final AtomicReference<Throwable> error = new AtomicReference<>(); | ||
| ApiFuture<ListVersionsPage> pageFuture = remoteConfig.listVersionsAsync(); | ||
| ApiFutures.addCallback(pageFuture, new ApiFutureCallback<ListVersionsPage>() { | ||
| @Override | ||
| public void onFailure(Throwable t) { | ||
| error.set(t); | ||
| semaphore.release(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onSuccess(ListVersionsPage result) { | ||
| for (Version version : result.iterateAll()) { | ||
| if (versions.contains(version.getVersionNumber())) { | ||
| collected.incrementAndGet(); | ||
| } | ||
| } | ||
| semaphore.release(); | ||
| } | ||
| }, MoreExecutors.directExecutor()); | ||
| semaphore.acquire(); | ||
| assertEquals(versions.size(), collected.get()); | ||
| assertNull(error.get()); | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Some of these can be split into separate test case with a bit of effort and extra RPCs (I'll leave it to you to decide whether that's worth it)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point! As discussed offline let's address this in a future PR. Thanks!