Skip to content

Commit 6d38ecf

Browse files
Merge pull request #80 from Flagsmith/release/5.0.4
Release 5.0.4
2 parents c7ec7d4 + 6dc6761 commit 6d38ecf

4 files changed

Lines changed: 104 additions & 13 deletions

File tree

pom.xml

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

77
<groupId>com.flagsmith</groupId>
88
<artifactId>flagsmith-java-client</artifactId>
9-
<version>5.0.3</version>
9+
<version>5.0.4</version>
1010
<packaging>jar</packaging>
1111

1212
<name>Flagsmith Java Client</name>

src/main/java/com/flagsmith/FlagsmithClient.java

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public class FlagsmithClient {
4343
private FlagsmithSdk flagsmithSdk;
4444
private EnvironmentModel environment;
4545
private PollingManager pollingManager;
46+
private static final String UNABLE_TO_UPDATE_ENVIRONMENT_MESSAGE =
47+
"Unable to update environment from API. Continuing to use previous copy.";
4648

4749
private FlagsmithClient() { }
4850

@@ -54,7 +56,29 @@ public static FlagsmithClient.Builder newBuilder() {
5456
* Load the environment flags in the environment variable from the API.
5557
*/
5658
public void updateEnvironment() {
57-
this.environment = flagsmithSdk.getEnvironment();
59+
try {
60+
EnvironmentModel updatedEnvironment = flagsmithSdk.getEnvironment();
61+
62+
// if we didn't get an environment from the API,
63+
// then don't overwrite the copy we already have.
64+
if (updatedEnvironment != null) {
65+
this.environment = updatedEnvironment;
66+
} else {
67+
logger.error(UNABLE_TO_UPDATE_ENVIRONMENT_MESSAGE);
68+
}
69+
} catch (RuntimeException e) {
70+
// if we already have a copy of the environment, don't throw an error,
71+
// just continue using that one and log an error as it's likely just a
72+
// temporary network issue.
73+
if (this.environment != null) {
74+
logger.error(UNABLE_TO_UPDATE_ENVIRONMENT_MESSAGE);
75+
} else {
76+
// if we don't already have an environment, then we should still throw
77+
// the error since it's likely on client start up and there might be
78+
// something more sinister going on.
79+
throw e;
80+
}
81+
}
5882
}
5983

6084
/**
@@ -248,6 +272,7 @@ public static class Builder {
248272
private String apiKey;
249273
private FlagsmithCacheConfig cacheConfig;
250274
private PollingManager pollingManager;
275+
private FlagsmithApiWrapper flagsmithApiWrapper;
251276

252277
private Builder() {
253278
client = new FlagsmithClient();
@@ -374,6 +399,17 @@ public Builder withPollingManager(PollingManager manager) {
374399
return this;
375400
}
376401

402+
/**
403+
* Set the api wrapper.
404+
*
405+
* @param flagsmithApiWrapper FlagsmithAPIWrapper object
406+
* @return the Builder
407+
*/
408+
public Builder withFlagsmithApiWrapper(FlagsmithApiWrapper flagsmithApiWrapper) {
409+
this.flagsmithApiWrapper = flagsmithApiWrapper;
410+
return this;
411+
}
412+
377413
/**
378414
* Builds a FlagsmithClient.
379415
*
@@ -382,20 +418,22 @@ public Builder withPollingManager(PollingManager manager) {
382418
public FlagsmithClient build() {
383419
final FlagsmithApiWrapper flagsmithApiWrapper;
384420

385-
if (cacheConfig != null) {
421+
if (this.flagsmithApiWrapper != null) {
422+
flagsmithApiWrapper = this.flagsmithApiWrapper;
423+
} else if (cacheConfig != null) {
386424
flagsmithApiWrapper = new FlagsmithApiWrapper(
387-
cacheConfig.getCache(),
388-
this.configuration,
389-
this.customHeaders,
390-
client.logger,
391-
apiKey
425+
cacheConfig.getCache(),
426+
this.configuration,
427+
this.customHeaders,
428+
client.logger,
429+
apiKey
392430
);
393431
} else {
394432
flagsmithApiWrapper = new FlagsmithApiWrapper(
395-
this.configuration,
396-
this.customHeaders,
397-
client.logger,
398-
apiKey
433+
this.configuration,
434+
this.customHeaders,
435+
client.logger,
436+
apiKey
399437
);
400438
}
401439

src/test/java/com/flagsmith/FlagsmithClientTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,4 +462,57 @@ public void testGetIdentitySegmentsWithValidTrait() throws JsonProcessingExcepti
462462
Assert.assertEquals(segments.size(), 1);
463463
Assert.assertEquals(segments.get(0).getName(), "Test segment");
464464
}
465+
466+
@Test(groups = "unit")
467+
public void testUpdateEnvironment_DoesNothing_WhenGetEnvironmentThrowsException() {
468+
// Given
469+
EnvironmentModel environmentModel = FlagsmithTestHelper.environmentModel();
470+
471+
FlagsmithApiWrapper mockApiWrapper = mock(FlagsmithApiWrapper.class);
472+
when(mockApiWrapper.getEnvironment())
473+
.thenReturn(environmentModel)
474+
.thenThrow(RuntimeException.class);
475+
476+
FlagsmithClient client = FlagsmithClient.newBuilder()
477+
.withFlagsmithApiWrapper(mockApiWrapper)
478+
.withConfiguration(FlagsmithConfig.newBuilder().withLocalEvaluation(true).build())
479+
.setApiKey("ser.dummy-key")
480+
.build();
481+
482+
// When
483+
// we call the update environment method twice (1st should be successful, 2nd will do nothing because of error)
484+
client.updateEnvironment();
485+
client.updateEnvironment();
486+
487+
// Then
488+
// No exception is thrown and the client environment remains what was first retrieved from the ApiWrapper
489+
Assert.assertEquals(client.getEnvironment(), environmentModel);
490+
}
491+
492+
@Test(groups = "unit")
493+
public void testUpdateEnvironment_DoesNothing_WhenGetEnvironmentReturnsNull() {
494+
// Given
495+
EnvironmentModel environmentModel = FlagsmithTestHelper.environmentModel();
496+
497+
FlagsmithApiWrapper mockApiWrapper = mock(FlagsmithApiWrapper.class);
498+
when(mockApiWrapper.getEnvironment())
499+
.thenReturn(environmentModel)
500+
.thenReturn(null);
501+
502+
FlagsmithClient client = FlagsmithClient.newBuilder()
503+
.withFlagsmithApiWrapper(mockApiWrapper)
504+
.withConfiguration(FlagsmithConfig.newBuilder().withLocalEvaluation(true).build())
505+
.setApiKey("ser.dummy-key")
506+
.build();
507+
508+
// When
509+
// we call the update environment method twice
510+
// (1st should be successful, 2nd will do nothing because of null return)
511+
client.updateEnvironment();
512+
client.updateEnvironment();
513+
514+
// Then
515+
// The client environment is not overwritten with null
516+
Assert.assertEquals(client.getEnvironment(), environmentModel);
517+
}
465518
}

0 commit comments

Comments
 (0)