Skip to content

Commit

Permalink
Fix for property ordering as reported in spring-attic#772
Browse files Browse the repository at this point in the history
  • Loading branch information
peteguyatt committed Apr 28, 2021
1 parent b8004d6 commit 9942a8c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ private AwsParamStorePropertySource create(String context) {

private void addProfiles(Set<String> contexts, String baseContext,
List<String> profiles) {
for (String profile : profiles) {
contexts.add(
baseContext + this.properties.getProfileSeparator() + profile + "/");
for (int index = profiles.size() - 1; index >= 0; --index) {
contexts.add(baseContext + this.properties.getProfileSeparator()
+ profiles.get(index) + "/");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@
import com.amazonaws.services.simplesystemsmanagement.model.Parameter;
import org.junit.Test;

import org.springframework.core.env.PropertySource;
import org.springframework.mock.env.MockEnvironment;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* Unit test for {@link AwsParamStorePropertySourceLocator}.
*
* @author Matej Nedic
* @author Pete Guyatt
*/
public class AwsParamStorePropertySourceLocatorTest {

Expand Down Expand Up @@ -91,28 +94,92 @@ public void contextSpecificOrderExpected() {

AwsParamStorePropertySourceLocator locator = new AwsParamStorePropertySourceLocator(
ssmClient, properties);
env.setActiveProfiles("test");
env.setActiveProfiles("test", "more-specific", "most-specific");
locator.locate(env);

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

assertThat(contextToBeTested.get(0))
.isEqualTo("application/messaging-service_most-specific/");
assertThat(contextToBeTested.get(1))
.isEqualTo("application/messaging-service_more-specific/");
assertThat(contextToBeTested.get(2))
.isEqualTo("application/messaging-service_test/");
assertThat(contextToBeTested.get(1)).isEqualTo("application/messaging-service/");
assertThat(contextToBeTested.get(2)).isEqualTo("application/application_test/");
assertThat(contextToBeTested.get(3)).isEqualTo("application/application/");
assertThat(contextToBeTested.get(3)).isEqualTo("application/messaging-service/");
assertThat(contextToBeTested.get(4))
.isEqualTo("application/application_most-specific/");
assertThat(contextToBeTested.get(5))
.isEqualTo("application/application_more-specific/");
assertThat(contextToBeTested.get(6)).isEqualTo("application/application_test/");
assertThat(contextToBeTested.get(7)).isEqualTo("application/application/");
}

@Test
public void contextWithMultipleProfilesValidatePropertyPrecedence() {
AwsParamStoreProperties properties = new AwsParamStorePropertiesBuilder()
.withDefaultContext("context").withName("application").build();

String[] activeProfiles = { "test", "more-specific", "most-specific" };
String propertyKey = "my-test-value";

when(ssmClient.getParametersByPath(
eq(newGetParametersByPathRequest("context/application/")))).thenReturn(
newGetParametersByPathResult("context/application/" + propertyKey,
""));

for (String profile : activeProfiles) {
String path = String.format("context/application_%s/", profile);
when(ssmClient.getParametersByPath(eq(newGetParametersByPathRequest(path))))
.thenReturn(
newGetParametersByPathResult(path + propertyKey, profile));
}

AwsParamStorePropertySourceLocator locator = new AwsParamStorePropertySourceLocator(
ssmClient, properties);
env.setActiveProfiles(activeProfiles);
PropertySource<?> propertySource = locator.locate(env);

assertThat(propertySource.getProperty(propertyKey))
.isEqualTo(activeProfiles[activeProfiles.length - 1]);

List<String> contexts = locator.getContexts();
assertThat(contexts).hasSize(4);

assertThat(contexts.get(0)).isEqualTo("context/application_most-specific/");
assertThat(contexts.get(1)).isEqualTo("context/application_more-specific/");
assertThat(contexts.get(2)).isEqualTo("context/application_test/");
assertThat(contexts.get(3)).isEqualTo("context/application/");
}

private static GetParametersByPathResult newGetParametersByPathResult(String name,
String value) {
return newGetParametersByPathResult(newParameter(name, value));
}

private static GetParametersByPathResult newGetParametersByPathResult(
Parameter... parameters) {
return new GetParametersByPathResult().withParameters(parameters);
}

private static GetParametersByPathRequest newGetParametersByPathRequest(String path) {
return new GetParametersByPathRequest().withPath(path).withRecursive(true)
.withWithDecryption(true);
}

public static Parameter newParameter(String name, String value) {
return new Parameter().withName(name).withValue(value);
}

private static GetParametersByPathResult getNextResult() {
return new GetParametersByPathResult().withParameters(
new Parameter().withName("/config/myservice/key3").withValue("value3"),
new Parameter().withName("/config/myservice/key4").withValue("value4"));
return newGetParametersByPathResult(
newParameter("/config/myservice/key3", "value3"),
newParameter("/config/myservice/key4", "value3"));
}

private static GetParametersByPathResult getFirstResult() {
return new GetParametersByPathResult().withParameters(
new Parameter().withName("/config/myservice/key3").withValue("value3"),
new Parameter().withName("/config/myservice/key4").withValue("value4"));
return newGetParametersByPathResult(
newParameter("/config/myservice/key3", "value3"),
newParameter("/config/myservice/key4", "value3"));
}

private static final class AwsParamStorePropertiesBuilder {
Expand Down

0 comments on commit 9942a8c

Please sign in to comment.