From b5f8c2aa20fa0f21942070c27df94ac1e532861b Mon Sep 17 00:00:00 2001 From: Lahiru Maramba Date: Thu, 7 Jan 2021 18:32:17 -0500 Subject: [PATCH] chore(rc): Refactor RC integration tests (#507) * chore(rc): Refactor RC integration tests * Avoid template cloning --- .../remoteconfig/FirebaseRemoteConfigIT.java | 166 +++++++++++++----- 1 file changed, 120 insertions(+), 46 deletions(-) diff --git a/src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigIT.java b/src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigIT.java index 3a13d8d86..bb90c6cb9 100644 --- a/src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigIT.java +++ b/src/test/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigIT.java @@ -42,89 +42,131 @@ public class FirebaseRemoteConfigIT { private static FirebaseRemoteConfig remoteConfig; - private static final long timestamp = System.currentTimeMillis(); - - private static final Map PARAMETERS = ImmutableMap.of( - "welcome_message_text", new Parameter() - .setDefaultValue(ParameterValue - .of(String.format("welcome to app %s", timestamp))) - .setConditionalValues(ImmutableMap.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 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 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) + public void testValidateTemplate() throws FirebaseRemoteConfigException { + final Template inputTemplate = remoteConfig.getTemplate(); + final Map expectedParameters = getParameters(); + final Map expectedParameterGroups = getParameterGroups(); + final Version expectedVersion = getVersion(); + inputTemplate.setParameters(expectedParameters) + .setParameterGroups(expectedParameterGroups) .setConditions(CONDITIONS) - .setVersion(VERSION); + .setVersion(expectedVersion); - // 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()); + + assertEquals(inputTemplate, validatedTemplate); + } + + @Test + public void testPublishTemplate() throws FirebaseRemoteConfigException { + // get template to fetch the active template with correct etag + final Template inputTemplate = remoteConfig.getTemplate(); + final Map parameters = getParameters(); + final Map parameterGroups = getParameterGroups(); + final Version version = getVersion(); + // modify template + inputTemplate.setParameters(parameters) + .setParameterGroups(parameterGroups) + .setConditions(CONDITIONS) + .setVersion(version); // publish template Template publishedTemplate = remoteConfig.publishTemplate(inputTemplate); + assertNotEquals(inputTemplate.getETag(), publishedTemplate.getETag()); - assertEquals(PARAMETERS, publishedTemplate.getParameters()); - assertEquals(PARAMETER_GROUPS, publishedTemplate.getParameterGroups()); + assertEquals(parameters, publishedTemplate.getParameters()); + assertEquals(parameterGroups, publishedTemplate.getParameterGroups()); assertEquals(CONDITIONS, publishedTemplate.getConditions()); - assertNotEquals(VERSION, publishedTemplate.getVersion()); + assertEquals(version.getDescription(), publishedTemplate.getVersion().getDescription()); + assertNotEquals(version, publishedTemplate.getVersion()); + } + + @Test + public void testGetTemplate() throws FirebaseRemoteConfigException { + // get template to fetch the active template with correct etag + // modify and publish a known template to test get template operation. + final Template inputTemplate = remoteConfig.getTemplate(); + final Map parameters = getParameters(); + final Map parameterGroups = getParameterGroups(); + final Version version = getVersion(); + inputTemplate.setParameters(parameters) + .setParameterGroups(parameterGroups) + .setConditions(CONDITIONS) + .setVersion(version); + // publish a known template + Template publishedTemplate = remoteConfig.publishTemplate(inputTemplate); // get template Template currentTemplate = remoteConfig.getTemplate(); + assertEquals(publishedTemplate, currentTemplate); + } + + @Test + public void testGetTemplateAtVersion() throws FirebaseRemoteConfigException { + // get template to fetch the active template with correct etag + // store the template version number + // publish a new template to test get template at version operation. + final Template previousTemplate = remoteConfig.getTemplate(); + final String versionNumber = previousTemplate.getVersion().getVersionNumber(); + final Template inputTemplate = new Template(previousTemplate.getETag()); + final Map parameters = getParameters(); + final Map parameterGroups = getParameterGroups(); + final Version version = getVersion(); + inputTemplate.setParameters(parameters) + .setParameterGroups(parameterGroups) + .setConditions(CONDITIONS) + .setVersion(version); + // publish a new template + Template publishedTemplate = remoteConfig.publishTemplate(inputTemplate); // get template at version Template atVersionTemplate = remoteConfig.getTemplateAtVersion(versionNumber); - assertEquals(oldTemplate, atVersionTemplate); + + assertEquals(previousTemplate, atVersionTemplate); assertEquals(versionNumber, atVersionTemplate.getVersion().getVersionNumber()); + assertNotEquals(publishedTemplate, atVersionTemplate); + } + + @Test + public void testRollbackTemplate() throws FirebaseRemoteConfigException { + // get template to fetch the active template with correct etag. + // store the template version number to rollback. + final Template inputTemplate = remoteConfig.getTemplate(); + final String versionNumber = inputTemplate.getVersion().getVersionNumber(); + final Map parameters = getParameters(); + final Map parameterGroups = getParameterGroups(); + final Version version = getVersion(); + inputTemplate.setParameters(parameters) + .setParameterGroups(parameterGroups) + .setConditions(CONDITIONS) + .setVersion(version); + // publish a new template before rolling back to versionNumber + Template publishedTemplate = remoteConfig.publishTemplate(inputTemplate); // 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); + assertNotEquals(publishedTemplate, activeTemplate); } @Test @@ -196,4 +238,36 @@ public void onSuccess(ListVersionsPage result) { assertEquals(versions.size(), collected.get()); assertNull(error.get()); } + + private Map getParameters() { + final long timestamp = System.currentTimeMillis(); + return ImmutableMap.of( + "welcome_message_text", new Parameter() + .setDefaultValue(ParameterValue + .of(String.format("welcome to app %s", timestamp))) + .setConditionalValues(ImmutableMap.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 Map getParameterGroups() { + final long timestamp = System.currentTimeMillis(); + return 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 Version getVersion() { + final long timestamp = System.currentTimeMillis(); + return Version.withDescription(String.format("promo config %s", timestamp)); + } }