14
14
15
15
package software .amazon .lambda .powertools .parameters .appconfig ;
16
16
17
+ import static org .assertj .core .api .Assertions .assertThat ;
17
18
import static org .assertj .core .api .Assertions .assertThatIllegalStateException ;
18
19
import static org .assertj .core .api .Assertions .assertThatRuntimeException ;
19
- import static org .assertj .core .api .Assertions .assertThat ;
20
20
import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
21
21
import static org .mockito .MockitoAnnotations .openMocks ;
22
22
import static software .amazon .lambda .powertools .parameters .transform .Transformer .json ;
27
27
import org .mockito .Captor ;
28
28
import org .mockito .Mock ;
29
29
import org .mockito .Mockito ;
30
+
30
31
import software .amazon .awssdk .core .SdkBytes ;
31
32
import software .amazon .awssdk .services .appconfigdata .AppConfigDataClient ;
32
33
import software .amazon .awssdk .services .appconfigdata .model .GetLatestConfigurationRequest ;
36
37
import software .amazon .lambda .powertools .parameters .cache .CacheManager ;
37
38
import software .amazon .lambda .powertools .parameters .transform .TransformationManager ;
38
39
39
- public class AppConfigProviderTest {
40
+ class AppConfigProviderTest {
40
41
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 " ;
44
45
45
46
@ Mock
46
47
AppConfigDataClient client ;
@@ -53,27 +54,26 @@ public class AppConfigProviderTest {
53
54
private AppConfigProvider provider ;
54
55
55
56
@ BeforeEach
56
- public void init () {
57
+ void init () {
57
58
openMocks (this );
58
59
59
60
provider = AppConfigProvider .builder ()
60
61
.withClient (client )
61
- .withApplication (applicationName )
62
- .withEnvironment (environmentName )
62
+ .withApplication (APPLICATION_NAME )
63
+ .withEnvironment (ENVIRONMENT_NAME )
63
64
.withCacheManager (new CacheManager ())
64
65
.withTransformationManager (new TransformationManager ())
65
66
.build ();
66
67
}
67
68
68
-
69
69
/**
70
70
* Tests repeated calls to the AppConfigProvider for the same key behave correctly. This is more complicated than
71
71
* it seems, as the service itself will return no-data if the value of a property remains unchanged since the
72
72
* start of a session. This means the provider must cache the result and return it again if it gets no data, but
73
73
* subsequent calls should once again return the new data.
74
74
*/
75
75
@ Test
76
- public void getValueRetrievesValue () {
76
+ void getValueRetrievesValue () {
77
77
// Arrange
78
78
StartConfigurationSessionResponse firstSession = StartConfigurationSessionResponse .builder ()
79
79
.initialConfigurationToken ("token1" )
@@ -92,24 +92,32 @@ public void getValueRetrievesValue() {
92
92
GetLatestConfigurationResponse thirdResponse = GetLatestConfigurationResponse .builder ()
93
93
.nextPollConfigurationToken ("token4" )
94
94
.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 ();
95
100
Mockito .when (client .startConfigurationSession (startSessionRequestCaptor .capture ()))
96
101
.thenReturn (firstSession );
97
102
Mockito .when (client .getLatestConfiguration (getLatestConfigurationRequestCaptor .capture ()))
98
- .thenReturn (firstResponse , secondResponse , thirdResponse );
103
+ .thenReturn (firstResponse , secondResponse , thirdResponse , forthResponse );
99
104
100
105
// 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 );
104
110
105
111
// Assert
106
112
assertThat (returnedValue1 ).isEqualTo (firstResponse .configuration ().asUtf8String ());
107
113
assertThat (returnedValue2 ).isEqualTo (secondResponse .configuration ().asUtf8String ());
108
114
assertThat (returnedValue3 ).isEqualTo (secondResponse .configuration ()
109
115
.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 );
113
121
assertThat (getLatestConfigurationRequestCaptor .getAllValues ().get (0 ).configurationToken ()).isEqualTo (
114
122
firstSession .initialConfigurationToken ());
115
123
assertThat (getLatestConfigurationRequestCaptor .getAllValues ().get (1 ).configurationToken ()).isEqualTo (
@@ -119,8 +127,7 @@ public void getValueRetrievesValue() {
119
127
}
120
128
121
129
@ Test
122
- public void getValueNoValueExists () {
123
-
130
+ void getValueNoValueExists () {
124
131
// Arrange
125
132
StartConfigurationSessionResponse session = StartConfigurationSessionResponse .builder ()
126
133
.initialConfigurationToken ("token1" )
@@ -134,19 +141,18 @@ public void getValueNoValueExists() {
134
141
.thenReturn (response );
135
142
136
143
// Act
137
- String returnedValue = provider .getValue (defaultTestKey );
138
-
144
+ String returnedValue = provider .getValue (DEFAULT_TEST_KEY );
139
145
140
146
// Assert
141
- assertThat (returnedValue ).isEqualTo ( null );
147
+ assertThat (returnedValue ).isNull ( );
142
148
}
143
149
144
150
/**
145
151
* If we mix requests for different keys together through the same provider, retrieval should
146
152
* work as expected. This means two separate configuration sessions should be established with AppConfig.
147
153
*/
148
154
@ Test
149
- public void multipleKeysRetrievalWorks () {
155
+ void multipleKeysRetrievalWorks () {
150
156
// Arrange
151
157
String param1Key = "key1" ;
152
158
StartConfigurationSessionResponse param1Session = StartConfigurationSessionResponse .builder ()
@@ -184,49 +190,45 @@ public void multipleKeysRetrievalWorks() {
184
190
param1Session .initialConfigurationToken ());
185
191
assertThat (getLatestConfigurationRequestCaptor .getAllValues ().get (1 ).configurationToken ()).isEqualTo (
186
192
param2Session .initialConfigurationToken ());
187
-
188
193
}
189
194
190
195
@ Test
191
- public void getMultipleValuesThrowsException () {
192
-
196
+ void getMultipleValuesThrowsException () {
193
197
// Act & Assert
194
198
assertThatRuntimeException ().isThrownBy (() -> provider .getMultipleValues ("path" ))
195
199
.withMessage ("Retrieving multiple parameter values is not supported with the AWS App Config Provider" );
196
200
}
197
201
198
202
@ Test
199
- public void testAppConfigProviderBuilderMissingEnvironment_throwsException () {
200
-
203
+ void testAppConfigProviderBuilderMissingEnvironment_throwsException () {
201
204
// Act & Assert
202
205
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 ())
207
210
.withMessage ("No environment provided; please provide one" );
208
211
}
209
212
210
213
@ Test
211
- public void testAppConfigProviderBuilderMissingApplication_throwsException () {
212
-
214
+ void testAppConfigProviderBuilderMissingApplication_throwsException () {
213
215
// Act & Assert
214
216
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 ())
219
221
.withMessage ("No application provided; please provide one" );
220
222
}
221
- @ Test
222
- public void testAppConfigProvider_withoutParameter_shouldHaveDefaultTransformationManager () {
223
223
224
+ @ Test
225
+ void testAppConfigProvider_withoutParameter_shouldHaveDefaultTransformationManager () {
224
226
// Act
225
227
AppConfigProvider appConfigProvider = AppConfigProvider .builder ()
226
228
.withEnvironment ("test" )
227
229
.withApplication ("app" )
228
230
.build ();
229
231
// Assert
230
- assertDoesNotThrow (()-> appConfigProvider .withTransformation (json ));
232
+ assertDoesNotThrow (() -> appConfigProvider .withTransformation (json ));
231
233
}
232
234
}
0 commit comments