Skip to content

Commit 9b6cd85

Browse files
authored
FFM-6125 - Nullpointer in MetricsProcessor (#130)
* FFM-6125 - Nullpointer in MetricsProcessor What When passing in a null target to an xxxVariation() function we get a NullPointerException. This change fixes the MetricsProcessor to handle nulls correctly and corrects a log statement to use toString() instead of deferencing target directly. Why Logger statement and metrics processing code don't perform any null checks. Testing Unit testing: added new unit tests to check that nulls can be passed in without error. Manual testing with GettingStarted sample
1 parent ea4dd6a commit 9b6cd85

File tree

5 files changed

+48
-15
lines changed

5 files changed

+48
-15
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ For a sample FF Java SDK project, see our [test Java project](https://github.com
2222

2323
To use this SDK, make sure you've:
2424

25-
- Installed[JDK 8](https://openjdk.java.net/install/) or a newer version<br>
25+
- Installed [JDK 8](https://openjdk.java.net/install/) or a newer version<br>
2626
- Installed Maven or Gradle or an alternative build automation tool for your application
2727

2828
To follow along with our test code sample, make sure you’ve:
@@ -69,7 +69,7 @@ The first step is to install the FF SDK as a dependency in your application usin
6969

7070
Refer to the [Harness Feature Flag Java Server SDK](https://mvnrepository.com/artifact/io.harness/ff-java-server-sdk) to identify the latest version for your build automation tool.
7171

72-
This section lists dependencies for Maven and Gradle and uses the 1.1.8 version as an example:
72+
This section lists dependencies for Maven and Gradle and uses the 1.1.9 version as an example:
7373

7474
#### Maven
7575

@@ -78,14 +78,14 @@ Add the following Maven dependency in your project's pom.xml file:
7878
<dependency>
7979
<groupId>io.harness</groupId>
8080
<artifactId>ff-java-server-sdk</artifactId>
81-
<version>1.1.8</version>
81+
<version>1.1.9</version>
8282
</dependency>
8383
```
8484

8585
#### Gradle
8686

8787
```
88-
implementation group: 'io.harness', name: 'ff-java-server-sdk', version: '1.1.8'
88+
implementation group: 'io.harness', name: 'ff-java-server-sdk', version: '1.1.9'
8989
```
9090

9191
### Code Sample

examples/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.harness.featureflags</groupId>
88
<artifactId>examples</artifactId>
9-
<version>1.1.9-SNAPSHOT</version>
9+
<version>1.1.9</version>
1010

1111
<properties>
1212
<maven.compiler.source>8</maven.compiler.source>
@@ -33,7 +33,7 @@
3333
<dependency>
3434
<groupId>io.harness</groupId>
3535
<artifactId>ff-java-server-sdk</artifactId>
36-
<version>1.1.9-SNAPSHOT</version>
36+
<version>1.1.9</version>
3737
</dependency>
3838

3939
<dependency>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.harness</groupId>
88
<artifactId>ff-java-server-sdk</artifactId>
9-
<version>1.1.9-SNAPSHOT</version>
9+
<version>1.1.9</version>
1010
<packaging>jar</packaging>
1111
<name>Harness Feature Flag Java Server SDK</name>
1212
<description>Harness Feature Flag Java Server SDK</description>

src/main/java/io/harness/cf/client/api/MetricsProcessor.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ public synchronized void pushToQueue(Target target, String featureName, Variatio
6868
executor.submit(this::runOneIteration);
6969
}
7070

71-
log.debug(
72-
"Flag: " + featureName + " Target: " + target.getIdentifier() + " Variation: " + variation);
73-
74-
uniqueTargetSet.add(target);
71+
log.debug("Flag: " + featureName + " Target: " + target + " Variation: " + variation);
7572

7673
Target metricTarget = globalTarget;
77-
if (!config.isGlobalTargetEnabled()) {
78-
metricTarget = target;
74+
75+
if (target != null) {
76+
uniqueTargetSet.add(target);
77+
if (!config.isGlobalTargetEnabled()) {
78+
metricTarget = target;
79+
}
7980
}
8081

8182
frequencyMap.incrementAndGet(new MetricEvent(featureName, metricTarget, variation));

src/test/java/io/harness/cf/client/api/CfClientTest.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,19 @@ private static Stream<Arguments> variantsToTest() {
162162
Arguments.of("boolVariation", (Consumer<CfClient>) CfClientTest::testBoolVariant),
163163
Arguments.of("stringVariation", (Consumer<CfClient>) CfClientTest::testStringVariant),
164164
Arguments.of("numberVariation", (Consumer<CfClient>) CfClientTest::testNumberVariant),
165-
Arguments.of("jsonVariant", (Consumer<CfClient>) CfClientTest::testJsonVariant));
165+
Arguments.of("jsonVariant", (Consumer<CfClient>) CfClientTest::testJsonVariant),
166+
Arguments.of(
167+
"boolVariationWithNullTarget",
168+
(Consumer<CfClient>) CfClientTest::testBoolVariantWithNullTarget),
169+
Arguments.of(
170+
"stringVariationWithNullTarget",
171+
(Consumer<CfClient>) CfClientTest::testStringVariantWithNullTarget),
172+
Arguments.of(
173+
"numberVariationWithNullTarget",
174+
(Consumer<CfClient>) CfClientTest::testNumberVariantWithNullTarget),
175+
Arguments.of(
176+
"jsonVariantWithNullTarget",
177+
(Consumer<CfClient>) CfClientTest::testJsonVariantWithNullTarget));
166178
}
167179

168180
@ParameterizedTest(name = "{0}")
@@ -172,7 +184,7 @@ void shouldGetVariantInPollingMode(String description, Consumer<CfClient> flagCa
172184
BaseConfig config =
173185
BaseConfig.builder()
174186
.pollIntervalInSeconds(1)
175-
.analyticsEnabled(false)
187+
.analyticsEnabled(true)
176188
.streamEnabled(false)
177189
.build();
178190

@@ -216,6 +228,26 @@ private static void testJsonVariant(CfClient client) {
216228
assertEquals("on", value.get("value").getAsString());
217229
}
218230

231+
private static void testBoolVariantWithNullTarget(CfClient client) {
232+
boolean value = client.boolVariation("simplebool", null, false);
233+
assertTrue(value);
234+
}
235+
236+
private static void testStringVariantWithNullTarget(CfClient client) {
237+
String value = client.stringVariation("simplestring", null, "DEFAULT");
238+
assertEquals("on-string", value);
239+
}
240+
241+
private static void testNumberVariantWithNullTarget(CfClient client) {
242+
double value = client.numberVariation("simplenumber", null, 0.123);
243+
assertEquals(1, value);
244+
}
245+
246+
private static void testJsonVariantWithNullTarget(CfClient client) {
247+
JsonObject value = client.jsonVariation("simplejson", null, new JsonObject());
248+
assertEquals("on", value.get("value").getAsString());
249+
}
250+
219251
static class TestWebServerDispatcher extends Dispatcher {
220252
private final AtomicInteger version = new AtomicInteger(2);
221253

0 commit comments

Comments
 (0)