Skip to content

Commit 419e70d

Browse files
committed
Fix context order when loading properties
Fixes gh-169
1 parent eb89670 commit 419e70d

File tree

4 files changed

+82
-6
lines changed

4 files changed

+82
-6
lines changed

spring-cloud-aws-parameter-store-config/src/main/java/io/awspring/cloud/paramstore/AwsParamStorePropertySources.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.awspring.cloud.paramstore;
1818

1919
import java.util.ArrayList;
20+
import java.util.Collections;
2021
import java.util.List;
2122

2223
import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement;
@@ -26,6 +27,7 @@
2627

2728
/**
2829
* @author Eddú Meléndez
30+
* @author Manuel Wessner
2931
* @since 2.3
3032
*/
3133
public class AwsParamStorePropertySources {
@@ -52,6 +54,8 @@ public List<String> getAutomaticContexts(List<String> profiles) {
5254

5355
addProfiles(contexts, defaultContext, profiles);
5456
contexts.add(defaultContext + "/");
57+
58+
Collections.reverse(contexts);
5559
return contexts;
5660
}
5761

spring-cloud-aws-parameter-store-config/src/test/java/io/awspring/cloud/paramstore/AwsParamStorePropertySourceLocatorTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ void contextSpecificOrderExpected() {
100100

101101
List<String> contextToBeTested = new ArrayList<>(locator.getContexts());
102102

103-
assertThat(contextToBeTested.get(0)).isEqualTo("application/messaging-service_test/");
104-
assertThat(contextToBeTested.get(1)).isEqualTo("application/messaging-service/");
105-
assertThat(contextToBeTested.get(2)).isEqualTo("application/application_test/");
106-
assertThat(contextToBeTested.get(3)).isEqualTo("application/application/");
103+
assertThat(contextToBeTested.get(0)).isEqualTo("application/application/");
104+
assertThat(contextToBeTested.get(1)).isEqualTo("application/application_test/");
105+
assertThat(contextToBeTested.get(2)).isEqualTo("application/messaging-service/");
106+
assertThat(contextToBeTested.get(3)).isEqualTo("application/messaging-service_test/");
107107
}
108108

109109
@Test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2013-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.awspring.cloud.paramstore;
18+
19+
import java.util.Collections;
20+
import java.util.List;
21+
22+
import org.apache.commons.logging.Log;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Test;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.mockito.Mockito.mock;
28+
29+
/**
30+
* Unit test for {@link AwsParamStorePropertySourceLocator}.
31+
*
32+
* @author Manuel Wessner
33+
*/
34+
class AwsParamStorePropertySourcesTest {
35+
36+
private final String defaultContextName = "application";
37+
38+
private final String defaultApplicationName = "messaging-service";
39+
40+
private final Log logMock = mock(Log.class);
41+
42+
private AwsParamStoreProperties properties;
43+
44+
@BeforeEach
45+
void setUp() {
46+
properties = new AwsParamStoreProperties();
47+
properties.setDefaultContext(defaultContextName);
48+
properties.setName(defaultApplicationName);
49+
}
50+
51+
@Test
52+
void getAutomaticContextsWithSingleProfile() {
53+
AwsParamStorePropertySources sut = new AwsParamStorePropertySources(properties, logMock);
54+
55+
List<String> contexts = sut.getAutomaticContexts(Collections.singletonList("production"));
56+
57+
assertThat(contexts.size()).isEqualTo(4);
58+
assertThat(contexts).containsExactly("/config/application/", "/config/application_production/",
59+
"/config/messaging-service/", "/config/messaging-service_production/");
60+
}
61+
62+
@Test
63+
void getAutomaticContextsWithoutProfile() {
64+
AwsParamStorePropertySources sut = new AwsParamStorePropertySources(properties, logMock);
65+
66+
List<String> contexts = sut.getAutomaticContexts(Collections.emptyList());
67+
68+
assertThat(contexts.size()).isEqualTo(2);
69+
assertThat(contexts).containsExactly("/config/application/", "/config/messaging-service/");
70+
}
71+
72+
}

spring-cloud-starter-aws-parameter-store-config/src/test/java/io/awspring/cloud/autoconfigure/paramstore/AwsParamStoreConfigDataLocationResolverTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ void testResolveProfileSpecificWithAutomaticPaths() {
4040
String location = "aws-parameterstore:";
4141
List<AwsParamStoreConfigDataResource> locations = testResolveProfileSpecific(location);
4242
assertThat(locations).hasSize(4);
43-
assertThat(toContexts(locations)).containsExactly("/config/testapp_dev/", "/config/testapp/",
44-
"/config/application_dev/", "/config/application/");
43+
assertThat(toContexts(locations)).containsExactly("/config/application/", "/config/application_dev/",
44+
"/config/testapp/", "/config/testapp_dev/");
4545
}
4646

4747
@Test

0 commit comments

Comments
 (0)