Skip to content

Commit

Permalink
Allow prefix in parameter store integration to be empty (spring-attic…
Browse files Browse the repository at this point in the history
…#167)

Fixes spring-attic#167 

Co-authored-by: Foss, Fredrik <[email protected]>
Co-authored-by: MatejNedic <[email protected]>
  • Loading branch information
3 people authored Feb 8, 2022
1 parent d686190 commit cda3d63
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ public class AwsParamStoreProperties implements InitializingBean {

public void afterPropertiesSet() throws Exception {

if (!StringUtils.hasLength(prefix)) {
throw new ValidationException(CONFIG_PREFIX + ".prefix", "prefix should not be empty or null.");
}

if (!StringUtils.hasLength(defaultContext)) {
throw new ValidationException(CONFIG_PREFIX + ".defaultContext",
"defaultContext should not be empty or null.");
Expand All @@ -98,7 +94,7 @@ public void afterPropertiesSet() throws Exception {
"profileSeparator should not be empty or null.");
}

if (!PREFIX_PATTERN.matcher(prefix).matches()) {
if (StringUtils.hasLength(prefix) && !PREFIX_PATTERN.matcher(prefix).matches()) {
throw new ValidationException(CONFIG_PREFIX + ".prefix",
"The prefix must have pattern of: " + PREFIX_PATTERN.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagement;
import org.apache.commons.logging.Log;

import org.springframework.util.StringUtils;

/**
* Is responsible for creating {@link AwsParamStorePropertySource} and determining
* automatic contexts.
Expand Down Expand Up @@ -72,7 +70,7 @@ public List<String> getAutomaticContexts(List<String> profiles) {
}

private String getContext(String prefix, String context) {
if (StringUtils.hasLength(prefix)) {
if (prefix != null) {
return prefix + "/" + context;
}
return context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ void validationSucceedsPrefix() {
assertThatNoException().isThrownBy(properties::afterPropertiesSet);
}

@Test
void validationSucceedsNoPrefix() {
AwsParamStoreProperties properties = new AwsParamStorePropertiesBuilder().withPrefix("")
.withDefaultContext("app").withProfileSeparator("_").build();

assertThatNoException().isThrownBy(properties::afterPropertiesSet);
}

@Test
void acceptsForwardSlashAsProfileSeparator() {
AwsParamStoreProperties properties = new AwsParamStoreProperties();
Expand All @@ -72,7 +80,6 @@ void acceptsBackslashAsProfileSeparator() {

private static Stream<Arguments> invalidProperties() {
return Stream.of(
Arguments.of(new AwsParamStorePropertiesBuilder().withPrefix("").build(), "prefix", "NotEmpty"),
Arguments.of(new AwsParamStorePropertiesBuilder().withPrefix("!.").build(), "prefix", "Pattern"),
Arguments.of(new AwsParamStorePropertiesBuilder().withDefaultContext("").build(), "defaultContext",
"NotEmpty"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,18 @@ void getAutomaticContextsWithoutProfile() {
assertThat(contexts).containsExactly("/config/application/", "/config/messaging-service/");
}

@Test
void getAutomaticContextsWithSingleProfileWithPrefixEmpty() {
AwsParamStoreProperties properties = new AwsParamStoreProperties();
properties.setName("messaging-service");
properties.setPrefix("");
AwsParamStorePropertySources propertySource = new AwsParamStorePropertySources(properties, logMock);

List<String> contexts = propertySource.getAutomaticContexts(Collections.singletonList("production"));

assertThat(contexts.size()).isEqualTo(4);
assertThat(contexts).containsExactly("/application/", "/application_production/", "/messaging-service/",
"/messaging-service_production/");
}

}

0 comments on commit cda3d63

Please sign in to comment.