Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow empty responses as well as null response in AppConfig #1673

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading