Skip to content

Commit 221c248

Browse files
author
Varun Rathore
committed
resolved comments
1 parent 43786bd commit 221c248

4 files changed

Lines changed: 48 additions & 3 deletions

File tree

src/main/java/com/google/firebase/remoteconfig/ParameterValue.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,8 @@ ParameterValueResponse toParameterValueResponse() {
448448
.setExperimentValue(
449449
new ExperimentValueResponse()
450450
.setExperimentId(this.experimentId)
451-
.setExperimentVariantValues(variantValueResponses));
451+
.setExperimentVariantValues(variantValueResponses)
452+
.setExposurePercent(this.exposurePercent));
452453
}
453454

454455
@Override

src/main/java/com/google/firebase/remoteconfig/Template.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public Template(String etag) {
6060
this((String) null);
6161
}
6262

63-
Template(@NonNull TemplateResponse templateResponse) {
63+
Template(@NonNull TemplateResponse templateResponse) throws FirebaseRemoteConfigException {
6464
checkNotNull(templateResponse);
6565
this.parameters = new HashMap<>();
6666
this.conditions = new ArrayList<>();
@@ -86,6 +86,7 @@ public Template(String etag) {
8686
if (templateResponse.getVersion() != null) {
8787
this.version = new Version(templateResponse.getVersion());
8888
}
89+
validateExperimentExposurePercents(this.parameters);
8990
this.etag = templateResponse.getEtag();
9091
}
9192

@@ -278,4 +279,28 @@ public boolean equals(Object o) {
278279
public int hashCode() {
279280
return Objects.hash(etag, parameters, conditions, parameterGroups, version);
280281
}
282+
283+
private static void validateExperimentExposurePercents(Map<String, Parameter> parameters)
284+
throws FirebaseRemoteConfigException {
285+
if (parameters == null) {
286+
return;
287+
}
288+
for (Map.Entry<String, Parameter> entry : parameters.entrySet()) {
289+
Parameter parameter = entry.getValue();
290+
if (parameter == null || parameter.getConditionalValues() == null) {
291+
continue;
292+
}
293+
for (ParameterValue value : parameter.getConditionalValues().values()) {
294+
if (value instanceof ParameterValue.ExperimentValue) {
295+
double exposurePercent = ((ParameterValue.ExperimentValue) value).getExposurePercent();
296+
// Validate range [0, 100] and finiteness
297+
if (exposurePercent < 0 || exposurePercent > 100 || !Double.isFinite(exposurePercent)) {
298+
throw new FirebaseRemoteConfigException(
299+
ErrorCode.INVALID_ARGUMENT,
300+
"Experiment exposure percent must be between 0 and 100 (" + entry.getKey() + ")");
301+
}
302+
}
303+
}
304+
}
305+
}
281306
}

src/test/java/com/google/firebase/remoteconfig/ParameterValueTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818

1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertNotEquals;
21+
import static org.junit.Assert.assertTrue;
2122

2223
import com.google.common.collect.ImmutableList;
2324
import com.google.firebase.remoteconfig.ParameterValue.ExperimentVariantValue;
25+
import com.google.firebase.remoteconfig.internal.TemplateResponse.ParameterValueResponse;
26+
2427
import org.junit.Test;
2528

2629
public class ParameterValueTest {
@@ -137,4 +140,20 @@ public void testEquality() {
137140
assertNotEquals(experimentValueOne, experimentValueThree);
138141
assertNotEquals(experimentValueOne, experimentValueFour);
139142
}
143+
144+
@Test
145+
public void testExperimentValueWithZeroExposure() {
146+
ParameterValue.ExperimentValue value = ParameterValue.ofExperiment(
147+
"exp_0", ImmutableList.of(ExperimentVariantValue.of("v1", "foo")), 0.0);
148+
149+
// Test Serialization
150+
ParameterValueResponse response = value.toParameterValueResponse();
151+
assertEquals(0.0, response.getExperimentValue().getExposurePercent(), 0.0);
152+
153+
// Test Deserialization
154+
ParameterValue fromResponse = ParameterValue.fromParameterValueResponse(response);
155+
assertTrue(fromResponse instanceof ParameterValue.ExperimentValue);
156+
assertEquals(0.0, ((ParameterValue.ExperimentValue) fromResponse).getExposurePercent(), 0.0);
157+
}
158+
140159
}

src/test/java/com/google/firebase/remoteconfig/TemplateTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void testConstructorWithETag() {
107107
}
108108

109109
@Test(expected = NullPointerException.class)
110-
public void testConstructorWithNullTemplateResponse() {
110+
public void testConstructorWithNullTemplateResponse() throws FirebaseRemoteConfigException{
111111
new Template((TemplateResponse) null);
112112
}
113113

0 commit comments

Comments
 (0)