1414
1515package software .amazon .lambda .powertools .parameters .appconfig ;
1616
17+ import static org .assertj .core .api .Assertions .assertThat ;
1718import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
1819import static org .assertj .core .api .Assertions .assertThatRuntimeException ;
19- import static org .assertj .core .api .Assertions .assertThat ;
2020import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
2121import static org .mockito .MockitoAnnotations .openMocks ;
2222import static software .amazon .lambda .powertools .parameters .transform .Transformer .json ;
2727import org .mockito .Captor ;
2828import org .mockito .Mock ;
2929import org .mockito .Mockito ;
30+
3031import software .amazon .awssdk .core .SdkBytes ;
3132import software .amazon .awssdk .services .appconfigdata .AppConfigDataClient ;
3233import software .amazon .awssdk .services .appconfigdata .model .GetLatestConfigurationRequest ;
3637import software .amazon .lambda .powertools .parameters .cache .CacheManager ;
3738import software .amazon .lambda .powertools .parameters .transform .TransformationManager ;
3839
39- public class AppConfigProviderTest {
40+ class AppConfigProviderTest {
4041
41- private final String environmentName = "test" ;
42- private final String applicationName = "fakeApp " ;
43- private final String defaultTestKey = "key1 " ;
42+ private static final String ENVIRONMENT_NAME = "test" ;
43+ private static final String DEFAULT_TEST_KEY = "key1 " ;
44+ private static final String APPLICATION_NAME = "fakeApp " ;
4445
4546 @ Mock
4647 AppConfigDataClient client ;
@@ -53,27 +54,26 @@ public class AppConfigProviderTest {
5354 private AppConfigProvider provider ;
5455
5556 @ BeforeEach
56- public void init () {
57+ void init () {
5758 openMocks (this );
5859
5960 provider = AppConfigProvider .builder ()
6061 .withClient (client )
61- .withApplication (applicationName )
62- .withEnvironment (environmentName )
62+ .withApplication (APPLICATION_NAME )
63+ .withEnvironment (ENVIRONMENT_NAME )
6364 .withCacheManager (new CacheManager ())
6465 .withTransformationManager (new TransformationManager ())
6566 .build ();
6667 }
6768
68-
6969 /**
7070 * Tests repeated calls to the AppConfigProvider for the same key behave correctly. This is more complicated than
7171 * it seems, as the service itself will return no-data if the value of a property remains unchanged since the
7272 * start of a session. This means the provider must cache the result and return it again if it gets no data, but
7373 * subsequent calls should once again return the new data.
7474 */
7575 @ Test
76- public void getValueRetrievesValue () {
76+ void getValueRetrievesValue () {
7777 // Arrange
7878 StartConfigurationSessionResponse firstSession = StartConfigurationSessionResponse .builder ()
7979 .initialConfigurationToken ("token1" )
@@ -92,24 +92,32 @@ public void getValueRetrievesValue() {
9292 GetLatestConfigurationResponse thirdResponse = GetLatestConfigurationResponse .builder ()
9393 .nextPollConfigurationToken ("token4" )
9494 .build ();
95+ // Forth response returns empty, which means the provider should yield the previous value again
96+ GetLatestConfigurationResponse forthResponse = GetLatestConfigurationResponse .builder ()
97+ .nextPollConfigurationToken ("token5" )
98+ .configuration (SdkBytes .fromUtf8String ("" ))
99+ .build ();
95100 Mockito .when (client .startConfigurationSession (startSessionRequestCaptor .capture ()))
96101 .thenReturn (firstSession );
97102 Mockito .when (client .getLatestConfiguration (getLatestConfigurationRequestCaptor .capture ()))
98- .thenReturn (firstResponse , secondResponse , thirdResponse );
103+ .thenReturn (firstResponse , secondResponse , thirdResponse , forthResponse );
99104
100105 // Act
101- String returnedValue1 = provider .getValue (defaultTestKey );
102- String returnedValue2 = provider .getValue (defaultTestKey );
103- String returnedValue3 = provider .getValue (defaultTestKey );
106+ String returnedValue1 = provider .getValue (DEFAULT_TEST_KEY );
107+ String returnedValue2 = provider .getValue (DEFAULT_TEST_KEY );
108+ String returnedValue3 = provider .getValue (DEFAULT_TEST_KEY );
109+ String returnedValue4 = provider .getValue (DEFAULT_TEST_KEY );
104110
105111 // Assert
106112 assertThat (returnedValue1 ).isEqualTo (firstResponse .configuration ().asUtf8String ());
107113 assertThat (returnedValue2 ).isEqualTo (secondResponse .configuration ().asUtf8String ());
108114 assertThat (returnedValue3 ).isEqualTo (secondResponse .configuration ()
109115 .asUtf8String ()); // Third response is mocked to return null and should re-use previous value
110- assertThat (startSessionRequestCaptor .getValue ().applicationIdentifier ()).isEqualTo (applicationName );
111- assertThat (startSessionRequestCaptor .getValue ().environmentIdentifier ()).isEqualTo (environmentName );
112- assertThat (startSessionRequestCaptor .getValue ().configurationProfileIdentifier ()).isEqualTo (defaultTestKey );
116+ assertThat (returnedValue4 ).isEqualTo (secondResponse .configuration ()
117+ .asUtf8String ()); // Forth response is mocked to return empty and should re-use previous value
118+ assertThat (startSessionRequestCaptor .getValue ().applicationIdentifier ()).isEqualTo (APPLICATION_NAME );
119+ assertThat (startSessionRequestCaptor .getValue ().environmentIdentifier ()).isEqualTo (ENVIRONMENT_NAME );
120+ assertThat (startSessionRequestCaptor .getValue ().configurationProfileIdentifier ()).isEqualTo (DEFAULT_TEST_KEY );
113121 assertThat (getLatestConfigurationRequestCaptor .getAllValues ().get (0 ).configurationToken ()).isEqualTo (
114122 firstSession .initialConfigurationToken ());
115123 assertThat (getLatestConfigurationRequestCaptor .getAllValues ().get (1 ).configurationToken ()).isEqualTo (
@@ -119,8 +127,7 @@ public void getValueRetrievesValue() {
119127 }
120128
121129 @ Test
122- public void getValueNoValueExists () {
123-
130+ void getValueNoValueExists () {
124131 // Arrange
125132 StartConfigurationSessionResponse session = StartConfigurationSessionResponse .builder ()
126133 .initialConfigurationToken ("token1" )
@@ -134,19 +141,18 @@ public void getValueNoValueExists() {
134141 .thenReturn (response );
135142
136143 // Act
137- String returnedValue = provider .getValue (defaultTestKey );
138-
144+ String returnedValue = provider .getValue (DEFAULT_TEST_KEY );
139145
140146 // Assert
141- assertThat (returnedValue ).isEqualTo ( null );
147+ assertThat (returnedValue ).isNull ( );
142148 }
143149
144150 /**
145151 * If we mix requests for different keys together through the same provider, retrieval should
146152 * work as expected. This means two separate configuration sessions should be established with AppConfig.
147153 */
148154 @ Test
149- public void multipleKeysRetrievalWorks () {
155+ void multipleKeysRetrievalWorks () {
150156 // Arrange
151157 String param1Key = "key1" ;
152158 StartConfigurationSessionResponse param1Session = StartConfigurationSessionResponse .builder ()
@@ -184,49 +190,45 @@ public void multipleKeysRetrievalWorks() {
184190 param1Session .initialConfigurationToken ());
185191 assertThat (getLatestConfigurationRequestCaptor .getAllValues ().get (1 ).configurationToken ()).isEqualTo (
186192 param2Session .initialConfigurationToken ());
187-
188193 }
189194
190195 @ Test
191- public void getMultipleValuesThrowsException () {
192-
196+ void getMultipleValuesThrowsException () {
193197 // Act & Assert
194198 assertThatRuntimeException ().isThrownBy (() -> provider .getMultipleValues ("path" ))
195199 .withMessage ("Retrieving multiple parameter values is not supported with the AWS App Config Provider" );
196200 }
197201
198202 @ Test
199- public void testAppConfigProviderBuilderMissingEnvironment_throwsException () {
200-
203+ void testAppConfigProviderBuilderMissingEnvironment_throwsException () {
201204 // Act & Assert
202205 assertThatIllegalStateException ().isThrownBy (() -> AppConfigProvider .builder ()
203- .withCacheManager (new CacheManager ())
204- .withApplication (applicationName )
205- .withClient (client )
206- .build ())
206+ .withCacheManager (new CacheManager ())
207+ .withApplication (APPLICATION_NAME )
208+ .withClient (client )
209+ .build ())
207210 .withMessage ("No environment provided; please provide one" );
208211 }
209212
210213 @ Test
211- public void testAppConfigProviderBuilderMissingApplication_throwsException () {
212-
214+ void testAppConfigProviderBuilderMissingApplication_throwsException () {
213215 // Act & Assert
214216 assertThatIllegalStateException ().isThrownBy (() -> AppConfigProvider .builder ()
215- .withCacheManager (new CacheManager ())
216- .withEnvironment (environmentName )
217- .withClient (client )
218- .build ())
217+ .withCacheManager (new CacheManager ())
218+ .withEnvironment (ENVIRONMENT_NAME )
219+ .withClient (client )
220+ .build ())
219221 .withMessage ("No application provided; please provide one" );
220222 }
221- @ Test
222- public void testAppConfigProvider_withoutParameter_shouldHaveDefaultTransformationManager () {
223223
224+ @ Test
225+ void testAppConfigProvider_withoutParameter_shouldHaveDefaultTransformationManager () {
224226 // Act
225227 AppConfigProvider appConfigProvider = AppConfigProvider .builder ()
226228 .withEnvironment ("test" )
227229 .withApplication ("app" )
228230 .build ();
229231 // Assert
230- assertDoesNotThrow (()-> appConfigProvider .withTransformation (json ));
232+ assertDoesNotThrow (() -> appConfigProvider .withTransformation (json ));
231233 }
232234}
0 commit comments