Skip to content

Commit

Permalink
fix: Allow empty responses as well as null response in AppConfig (#1673)
Browse files Browse the repository at this point in the history
- AppConfig responses, where config hasn't changed, seem to be result
in "empty" `SdkBytes` objects rather than `null` `Configuration` in
the response.
  • Loading branch information
chrisclayson authored Aug 20, 2024
1 parent 073091d commit 3ab01b5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

package software.amazon.lambda.powertools.parameters;

import java.util.HashMap;
import java.util.Map;
import software.amazon.awssdk.core.SdkSystemSetting;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.core.client.config.SdkAdvancedClientOption;
Expand All @@ -25,10 +23,14 @@
import software.amazon.awssdk.services.appconfigdata.model.GetLatestConfigurationRequest;
import software.amazon.awssdk.services.appconfigdata.model.GetLatestConfigurationResponse;
import software.amazon.awssdk.services.appconfigdata.model.StartConfigurationSessionRequest;
import software.amazon.awssdk.utils.StringUtils;
import software.amazon.lambda.powertools.core.internal.UserAgentConfigurator;
import software.amazon.lambda.powertools.parameters.cache.CacheManager;
import software.amazon.lambda.powertools.parameters.transform.TransformationManager;

import java.util.HashMap;
import java.util.Map;

/**
* Implements a {@link ParamProvider} on top of the AppConfig service. AppConfig provides
* a mechanism to retrieve and update configuration of applications over time.
Expand Down Expand Up @@ -98,7 +100,7 @@ protected String getValue(String key) {
// Get the value of the key. Note that AppConfig will return null if the value
// has not changed since we last asked for it in this session - in this case
// we return the value we stashed at last request.
String value = response.configuration() != null ?
String value = !(response.configuration() == null || StringUtils.isEmpty(response.configuration().asUtf8String())) ?
response.configuration().asUtf8String() : // if we have a new value, use it
establishedSession != null ?
establishedSession.lastConfigurationValue :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@

package software.amazon.lambda.powertools.parameters;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
import static org.mockito.MockitoAnnotations.openMocks;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
Expand All @@ -34,6 +29,11 @@
import software.amazon.lambda.powertools.parameters.cache.CacheManager;
import software.amazon.lambda.powertools.parameters.transform.TransformationManager;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
import static org.mockito.MockitoAnnotations.openMocks;

public class AppConfigProviderTest {

private final String environmentName = "test";
Expand Down Expand Up @@ -90,21 +90,29 @@ public void getValueRetrievesValue() {
GetLatestConfigurationResponse thirdResponse = GetLatestConfigurationResponse.builder()
.nextPollConfigurationToken("token4")
.build();
// Forth response returns empty, which means the provider should yield the previous value again
GetLatestConfigurationResponse forthResponse = GetLatestConfigurationResponse.builder()
.nextPollConfigurationToken("token5")
.configuration(SdkBytes.fromUtf8String(""))
.build();
Mockito.when(client.startConfigurationSession(startSessionRequestCaptor.capture()))
.thenReturn(firstSession);
Mockito.when(client.getLatestConfiguration(getLatestConfigurationRequestCaptor.capture()))
.thenReturn(firstResponse, secondResponse, thirdResponse);
.thenReturn(firstResponse, secondResponse, thirdResponse, forthResponse);

// Act
String returnedValue1 = provider.getValue(defaultTestKey);
String returnedValue2 = provider.getValue(defaultTestKey);
String returnedValue3 = provider.getValue(defaultTestKey);
String returnedValue4 = provider.getValue(defaultTestKey);

// Assert
assertThat(returnedValue1).isEqualTo(firstResponse.configuration().asUtf8String());
assertThat(returnedValue2).isEqualTo(secondResponse.configuration().asUtf8String());
assertThat(returnedValue3).isEqualTo(secondResponse.configuration()
.asUtf8String()); // Third response is mocked to return null and should re-use previous value
assertThat(returnedValue4).isEqualTo(secondResponse.configuration()
.asUtf8String()); // Forth response is mocked to return empty and should re-use previous value
assertThat(startSessionRequestCaptor.getValue().applicationIdentifier()).isEqualTo(applicationName);
assertThat(startSessionRequestCaptor.getValue().environmentIdentifier()).isEqualTo(environmentName);
assertThat(startSessionRequestCaptor.getValue().configurationProfileIdentifier()).isEqualTo(defaultTestKey);
Expand Down

0 comments on commit 3ab01b5

Please sign in to comment.