Skip to content

Commit 6554f3e

Browse files
committed
fix: Allow empty responses as well as null response in AppConfig
- AppConfig responses, where config hasn't changed, seem to be result in "empty" `SdkBytes` objects rather than `null` `Configuration` in the response.
1 parent 3440513 commit 6554f3e

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

powertools-parameters/src/main/java/software/amazon/lambda/powertools/parameters/AppConfigProvider.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
package software.amazon.lambda.powertools.parameters;
1616

17-
import java.util.HashMap;
18-
import java.util.Map;
1917
import software.amazon.awssdk.core.SdkSystemSetting;
2018
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
2119
import software.amazon.awssdk.core.client.config.SdkAdvancedClientOption;
@@ -25,10 +23,14 @@
2523
import software.amazon.awssdk.services.appconfigdata.model.GetLatestConfigurationRequest;
2624
import software.amazon.awssdk.services.appconfigdata.model.GetLatestConfigurationResponse;
2725
import software.amazon.awssdk.services.appconfigdata.model.StartConfigurationSessionRequest;
26+
import software.amazon.awssdk.utils.StringUtils;
2827
import software.amazon.lambda.powertools.core.internal.UserAgentConfigurator;
2928
import software.amazon.lambda.powertools.parameters.cache.CacheManager;
3029
import software.amazon.lambda.powertools.parameters.transform.TransformationManager;
3130

31+
import java.util.HashMap;
32+
import java.util.Map;
33+
3234
/**
3335
* Implements a {@link ParamProvider} on top of the AppConfig service. AppConfig provides
3436
* a mechanism to retrieve and update configuration of applications over time.
@@ -98,7 +100,7 @@ protected String getValue(String key) {
98100
// Get the value of the key. Note that AppConfig will return null if the value
99101
// has not changed since we last asked for it in this session - in this case
100102
// we return the value we stashed at last request.
101-
String value = response.configuration() != null ?
103+
String value = !(response.configuration() == null || StringUtils.isEmpty(response.configuration().asUtf8String())) ?
102104
response.configuration().asUtf8String() : // if we have a new value, use it
103105
establishedSession != null ?
104106
establishedSession.lastConfigurationValue :

powertools-parameters/src/test/java/software/amazon/lambda/powertools/parameters/AppConfigProviderTest.java

+14-6
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@
1414

1515
package software.amazon.lambda.powertools.parameters;
1616

17-
import static org.assertj.core.api.Assertions.assertThat;
18-
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
19-
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
20-
import static org.mockito.MockitoAnnotations.openMocks;
21-
2217
import org.junit.jupiter.api.BeforeEach;
2318
import org.junit.jupiter.api.Test;
2419
import org.mockito.ArgumentCaptor;
@@ -34,6 +29,11 @@
3429
import software.amazon.lambda.powertools.parameters.cache.CacheManager;
3530
import software.amazon.lambda.powertools.parameters.transform.TransformationManager;
3631

32+
import static org.assertj.core.api.Assertions.assertThat;
33+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
34+
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
35+
import static org.mockito.MockitoAnnotations.openMocks;
36+
3737
public class AppConfigProviderTest {
3838

3939
private final String environmentName = "test";
@@ -90,21 +90,29 @@ public void getValueRetrievesValue() {
9090
GetLatestConfigurationResponse thirdResponse = GetLatestConfigurationResponse.builder()
9191
.nextPollConfigurationToken("token4")
9292
.build();
93+
// Forth response returns empty, which means the provider should yield the previous value again
94+
GetLatestConfigurationResponse forthResponse = GetLatestConfigurationResponse.builder()
95+
.nextPollConfigurationToken("token5")
96+
.configuration(SdkBytes.fromUtf8String(""))
97+
.build();
9398
Mockito.when(client.startConfigurationSession(startSessionRequestCaptor.capture()))
9499
.thenReturn(firstSession);
95100
Mockito.when(client.getLatestConfiguration(getLatestConfigurationRequestCaptor.capture()))
96-
.thenReturn(firstResponse, secondResponse, thirdResponse);
101+
.thenReturn(firstResponse, secondResponse, thirdResponse, forthResponse);
97102

98103
// Act
99104
String returnedValue1 = provider.getValue(defaultTestKey);
100105
String returnedValue2 = provider.getValue(defaultTestKey);
101106
String returnedValue3 = provider.getValue(defaultTestKey);
107+
String returnedValue4 = provider.getValue(defaultTestKey);
102108

103109
// Assert
104110
assertThat(returnedValue1).isEqualTo(firstResponse.configuration().asUtf8String());
105111
assertThat(returnedValue2).isEqualTo(secondResponse.configuration().asUtf8String());
106112
assertThat(returnedValue3).isEqualTo(secondResponse.configuration()
107113
.asUtf8String()); // Third response is mocked to return null and should re-use previous value
114+
assertThat(returnedValue4).isEqualTo(secondResponse.configuration()
115+
.asUtf8String()); // Forth response is mocked to return empty and should re-use previous value
108116
assertThat(startSessionRequestCaptor.getValue().applicationIdentifier()).isEqualTo(applicationName);
109117
assertThat(startSessionRequestCaptor.getValue().environmentIdentifier()).isEqualTo(environmentName);
110118
assertThat(startSessionRequestCaptor.getValue().configurationProfileIdentifier()).isEqualTo(defaultTestKey);

0 commit comments

Comments
 (0)