|
16 | 16 | package org.springframework.cloud.bootstrap.encrypt;
|
17 | 17 |
|
18 | 18 | import java.util.Collections;
|
| 19 | +import java.util.HashMap; |
19 | 20 | import java.util.Map;
|
20 | 21 |
|
21 | 22 | import org.junit.Test;
|
|
36 | 37 | import static org.hamcrest.Matchers.is;
|
37 | 38 | import static org.junit.Assert.assertEquals;
|
38 | 39 | import static org.junit.Assert.assertThat;
|
39 |
| -import static org.mockito.ArgumentMatchers.anyString; |
40 | 40 | import static org.mockito.Mockito.mock;
|
41 | 41 | import static org.mockito.Mockito.verify;
|
42 | 42 | import static org.mockito.Mockito.verifyNoMoreInteractions;
|
@@ -168,23 +168,31 @@ public void testDecryptNonStandardParent() {
|
168 | 168 |
|
169 | 169 | @Test
|
170 | 170 | public void testDecryptCompositePropertySource() {
|
171 |
| - String expected = "always"; |
172 |
| - TextEncryptor textEncryptor = mock(TextEncryptor.class); |
173 |
| - when(textEncryptor.decrypt(anyString())).thenReturn(expected); |
174 |
| - |
175 | 171 | ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext();
|
176 | 172 | EnvironmentDecryptApplicationInitializer initializer = new EnvironmentDecryptApplicationInitializer(
|
177 |
| - textEncryptor); |
| 173 | + Encryptors.noOpText()); |
178 | 174 |
|
179 |
| - MapPropertySource source = new MapPropertySource("nobody", |
180 |
| - Collections.singletonMap("key", "{cipher}value")); |
181 |
| - CompositePropertySource cps = mock(CompositePropertySource.class); |
182 |
| - when(cps.getPropertyNames()).thenReturn(source.getPropertyNames()); |
183 |
| - when(cps.getPropertySources()).thenReturn(Collections.singleton(source)); |
| 175 | + CompositePropertySource cps = new CompositePropertySource("testCPS"); |
| 176 | + Map<String, Object> map1 = new HashMap<>(); |
| 177 | + map1.put("key1", "{cipher}value1b"); |
| 178 | + map1.put("key2", "value2b"); |
| 179 | + cps.addPropertySource(new MapPropertySource("profile1", map1)); |
| 180 | + Map<String, Object> map2 = new HashMap<>(); |
| 181 | + map2.put("key1", "{cipher}value1"); |
| 182 | + map2.put("key2", "value2"); |
| 183 | + map1.put("key3", "value3"); |
| 184 | + cps.addPropertySource(new MapPropertySource("profile2", map2)); |
| 185 | + // add non-enumerable property source that will fail cps.getPropertyNames() |
| 186 | + cps.addPropertySource(mock(PropertySource.class)); |
184 | 187 | ctx.getEnvironment().getPropertySources().addLast(cps);
|
185 | 188 |
|
186 | 189 | initializer.initialize(ctx);
|
187 |
| - assertEquals(expected, ctx.getEnvironment().getProperty("key")); |
| 190 | + // validate behaviour with encryption |
| 191 | + assertEquals("value1b", ctx.getEnvironment().getProperty("key1")); |
| 192 | + // validate behaviour without encryption |
| 193 | + assertEquals("value2b", ctx.getEnvironment().getProperty("key2")); |
| 194 | + // validate behaviour without override |
| 195 | + assertEquals("value3", ctx.getEnvironment().getProperty("key3")); |
188 | 196 | }
|
189 | 197 |
|
190 | 198 | @Test
|
|
0 commit comments