|
| 1 | +/** |
| 2 | + * |
| 3 | + * Copyright 2017, Optimizely and contributors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package com.optimizely.ab.internal; |
| 18 | + |
| 19 | +import com.optimizely.ab.config.audience.Audience; |
| 20 | +import com.optimizely.ab.config.audience.Condition; |
| 21 | +import com.optimizely.ab.config.Experiment; |
| 22 | +import com.optimizely.ab.config.Experiment.ExperimentStatus; |
| 23 | +import com.optimizely.ab.config.ProjectConfig; |
| 24 | +import com.optimizely.ab.config.TrafficAllocation; |
| 25 | +import com.optimizely.ab.config.Variation; |
| 26 | +import org.junit.BeforeClass; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.Map; |
| 32 | + |
| 33 | +import static com.optimizely.ab.config.ProjectConfigTestUtils.noAudienceProjectConfigV2; |
| 34 | +import static com.optimizely.ab.config.ProjectConfigTestUtils.validProjectConfigV2; |
| 35 | +import static com.optimizely.ab.internal.ExperimentUtils.isExperimentActive; |
| 36 | +import static com.optimizely.ab.internal.ExperimentUtils.isUserInExperiment; |
| 37 | +import static org.junit.Assert.assertFalse; |
| 38 | +import static org.junit.Assert.assertTrue; |
| 39 | + |
| 40 | +/** |
| 41 | + * Test the Experiment Utils methods. |
| 42 | + */ |
| 43 | +public class ExperimentUtilsTest { |
| 44 | + |
| 45 | + private static ProjectConfig projectConfig; |
| 46 | + private static ProjectConfig noAudienceProjectConfig; |
| 47 | + |
| 48 | + @BeforeClass |
| 49 | + public static void setUp() throws IOException { |
| 50 | + projectConfig = validProjectConfigV2(); |
| 51 | + noAudienceProjectConfig = noAudienceProjectConfigV2(); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * If the {@link Experiment#status} is {@link ExperimentStatus#RUNNING}, |
| 56 | + * then {@link ExperimentUtils#isExperimentActive(Experiment)} should return true. |
| 57 | + */ |
| 58 | + @Test |
| 59 | + public void isExperimentActiveReturnsTrueWhenTheExperimentIsRunning() { |
| 60 | + Experiment mockExperiment = makeMockExperimentWithStatus(ExperimentStatus.RUNNING); |
| 61 | + |
| 62 | + assertTrue(isExperimentActive(mockExperiment)); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * If the {@link Experiment#status} is {@link ExperimentStatus#LAUNCHED}, |
| 67 | + * then {@link ExperimentUtils#isExperimentActive(Experiment)} should return true. |
| 68 | + */ |
| 69 | + @Test |
| 70 | + public void isExperimentActiveReturnsTrueWhenTheExperimentIsLaunched() { |
| 71 | + Experiment mockExperiment = makeMockExperimentWithStatus(ExperimentStatus.LAUNCHED); |
| 72 | + |
| 73 | + assertTrue(isExperimentActive(mockExperiment)); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * If the {@link Experiment#status} is {@link ExperimentStatus#PAUSED}, |
| 78 | + * then {@link ExperimentUtils#isExperimentActive(Experiment)} should return false. |
| 79 | + */ |
| 80 | + @Test |
| 81 | + public void isExperimentActiveReturnsFalseWhenTheExperimentIsPaused() { |
| 82 | + Experiment mockExperiment = makeMockExperimentWithStatus(ExperimentStatus.PAUSED); |
| 83 | + |
| 84 | + assertFalse(isExperimentActive(mockExperiment)); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * If the {@link Experiment#status} is {@link ExperimentStatus#ARCHIVED}, |
| 89 | + * then {@link ExperimentUtils#isExperimentActive(Experiment)} should return false. |
| 90 | + */ |
| 91 | + @Test |
| 92 | + public void isExperimentActiveReturnsFalseWhenTheExperimentIsArchived() { |
| 93 | + Experiment mockExperiment = makeMockExperimentWithStatus(ExperimentStatus.ARCHIVED); |
| 94 | + |
| 95 | + assertFalse(isExperimentActive(mockExperiment)); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * If the {@link Experiment#status} is {@link ExperimentStatus#NOT_STARTED}, |
| 100 | + * then {@link ExperimentUtils#isExperimentActive(Experiment)} should return false. |
| 101 | + */ |
| 102 | + @Test |
| 103 | + public void isExperimentActiveReturnsFalseWhenTheExperimentIsNotStarted() { |
| 104 | + Experiment mockExperiment = makeMockExperimentWithStatus(ExperimentStatus.NOT_STARTED); |
| 105 | + |
| 106 | + assertFalse(isExperimentActive(mockExperiment)); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * If the {@link Experiment} does not have any {@link Audience}s, |
| 111 | + * then {@link ExperimentUtils#isUserInExperiment(ProjectConfig, Experiment, Map)} should return true; |
| 112 | + */ |
| 113 | + @Test |
| 114 | + public void isUserInExperimentReturnsTrueIfExperimentHasNoAudiences() { |
| 115 | + Experiment experiment = noAudienceProjectConfig.getExperiments().get(0); |
| 116 | + |
| 117 | + assertTrue(isUserInExperiment(noAudienceProjectConfig, experiment, Collections.<String, String>emptyMap())); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * If the {@link Experiment} contains at least one {@link Audience}, but attributes is empty, |
| 122 | + * then {@link ExperimentUtils#isUserInExperiment(ProjectConfig, Experiment, Map)} should return false. |
| 123 | + */ |
| 124 | + @Test |
| 125 | + public void isUserInExperimentReturnsFalseIfExperimentHasAudiencesButUserHasNoAttributes() { |
| 126 | + Experiment experiment = projectConfig.getExperiments().get(0); |
| 127 | + |
| 128 | + assertFalse(isUserInExperiment(projectConfig, experiment, Collections.<String, String>emptyMap())); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * If the attributes satisfies at least one {@link Condition} in an {@link Audience} of the {@link Experiment}, |
| 133 | + * then {@link ExperimentUtils#isUserInExperiment(ProjectConfig, Experiment, Map)} should return true. |
| 134 | + */ |
| 135 | + @Test |
| 136 | + public void isUserInExperimentReturnsTrueIfUserSatisfiesAnAudience() { |
| 137 | + Experiment experiment = projectConfig.getExperiments().get(0); |
| 138 | + Map<String, String> attributes = Collections.singletonMap("browser_type", "chrome"); |
| 139 | + |
| 140 | + assertTrue(isUserInExperiment(projectConfig, experiment, attributes)); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * If the attributes satisfies no {@link Condition} of any {@link Audience} of the {@link Experiment}, |
| 145 | + * then {@link ExperimentUtils#isUserInExperiment(ProjectConfig, Experiment, Map)} should return false. |
| 146 | + */ |
| 147 | + @Test |
| 148 | + public void isUserInExperimentReturnsTrueIfUserDoesNotSatisfyAnyAudiences() { |
| 149 | + Experiment experiment = projectConfig.getExperiments().get(0); |
| 150 | + Map<String, String> attributes = Collections.singletonMap("browser_type", "firefox"); |
| 151 | + |
| 152 | + assertFalse(isUserInExperiment(projectConfig, experiment, attributes)); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Helper method to create an {@link Experiment} object with the provided status. |
| 157 | + * |
| 158 | + * @param status What the desired {@link Experiment#status} should be. |
| 159 | + * @return The newly created {@link Experiment}. |
| 160 | + */ |
| 161 | + private Experiment makeMockExperimentWithStatus(ExperimentStatus status) { |
| 162 | + return new Experiment("12345", |
| 163 | + "mockExperimentKey", |
| 164 | + status.toString(), |
| 165 | + "layerId", |
| 166 | + Collections.<String>emptyList(), |
| 167 | + Collections.<Variation>emptyList(), |
| 168 | + Collections.<String, String>emptyMap(), |
| 169 | + Collections.<TrafficAllocation>emptyList() |
| 170 | + ); |
| 171 | + } |
| 172 | +} |
0 commit comments