diff --git a/README.md b/README.md index 3785325..5c96cce 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,18 @@ ## Description Spring Auto-configuration library for [Cloud Config Client](https://github.com/Piszmog/cloud-config-client). +__Cloud Config Client AutoConfig 3.x__ is compatible with + +| Dependency | Version | +| :---: | :---: | +| Spring Boot | 2.2.x | +| Spring Cloud Services | 3.x.x.RELEASE | + __Cloud Config Client AutoConfig 2.x__ is compatible with | Dependency | Version | | :---: | :---: | -| Spring Boot | 2.x.x | +| Spring Boot | 2.1.x | | Spring Cloud Services | 2.x.x.RELEASE | __Cloud Config Client AutoConfig 1.x__ is compatible with diff --git a/pom.xml b/pom.xml index 6c72567..b80aa06 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ io.github.piszmog cloud-config-client-autoconfig - 2.5-SNAPSHOT + 3.0-SNAPSHOT jar @@ -30,7 +30,7 @@ UTF-8 UTF-8 - 1.7.1 + 1.8.1 3.8.1 3.1.0 2.22.2 @@ -38,14 +38,14 @@ 1.6 1.6.8 - 2.1.4.RELEASE - 2.1.6.RELEASE + 3.1.0.RELEASE + 2.2.2.RELEASE - 2.5 + 3.0-SNAPSHOT - 3.2.12 - 2.5.7 - 3.0.1 + 3.3.0 + 2.5.8 + 3.1 1.3-groovy-2.5 @@ -77,6 +77,11 @@ spring-cloud-services-starter-config-client provided + + io.pivotal.spring.cloud + spring-cloud-services-config-client-autoconfigure + provided + org.spockframework @@ -172,6 +177,9 @@ org.apache.maven.plugins maven-javadoc-plugin ${maven-plugin.version} + + 1.8 + attach-javadocs diff --git a/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/client/ConfigClientAutoConfiguration.java b/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/client/ConfigClientAutoConfiguration.java index a08ef20..77e0fde 100644 --- a/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/client/ConfigClientAutoConfiguration.java +++ b/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/client/ConfigClientAutoConfiguration.java @@ -19,9 +19,8 @@ * Created by Piszmog on 5/5/2018 */ @Configuration -@ConditionalOnBean( ConfigTemplate.class ) -public class ConfigClientAutoConfiguration -{ +@ConditionalOnBean(ConfigTemplate.class) +public class ConfigClientAutoConfiguration { // ============================================================ // Beans: // ============================================================ @@ -33,10 +32,9 @@ public class ConfigClientAutoConfiguration * @return The decryption client. */ @Bean - @ConditionalOnProperty( prefix = "cloud.config.client", name = "decrypt.enabled", matchIfMissing = true ) - public DecryptConfigClient decryptConfigClient( final ConfigTemplate configTemplate ) - { - return new DecryptConfigClient( configTemplate ); + @ConditionalOnProperty(prefix = "cloud.config.client", name = "decrypt.enabled", matchIfMissing = true) + public DecryptConfigClient decryptConfigClient(final ConfigTemplate configTemplate) { + return new DecryptConfigClient(configTemplate); } /** @@ -46,10 +44,9 @@ public DecryptConfigClient decryptConfigClient( final ConfigTemplate configTempl * @return The encryption client. */ @Bean - @ConditionalOnProperty( prefix = "cloud.config.client", name = "encrypt.enabled", matchIfMissing = true ) - public EncryptConfigClient encryptConfigClient( final ConfigTemplate configTemplate ) - { - return new EncryptConfigClient( configTemplate ); + @ConditionalOnProperty(prefix = "cloud.config.client", name = "encrypt.enabled", matchIfMissing = true) + public EncryptConfigClient encryptConfigClient(final ConfigTemplate configTemplate) { + return new EncryptConfigClient(configTemplate); } /** @@ -59,9 +56,8 @@ public EncryptConfigClient encryptConfigClient( final ConfigTemplate configTempl * @return The public key client. */ @Bean - @ConditionalOnProperty( prefix = "cloud.config.client", name = "publickey.enabled", matchIfMissing = true ) - public PublicKeyClient publicKeyClient( final ConfigTemplate configTemplate ) - { - return new PublicKeyClient( configTemplate ); + @ConditionalOnProperty(prefix = "cloud.config.client", name = "publickey.enabled", matchIfMissing = true) + public PublicKeyClient publicKeyClient(final ConfigTemplate configTemplate) { + return new PublicKeyClient(configTemplate); } } diff --git a/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/env/ConfigPropertySourceLocator.java b/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/env/ConfigPropertySourceLocator.java index 82f0065..41f9d6f 100644 --- a/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/env/ConfigPropertySourceLocator.java +++ b/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/env/ConfigPropertySourceLocator.java @@ -31,9 +31,8 @@ *

* Created by Piszmog on 7/21/2018 */ -public class ConfigPropertySourceLocator -{ - private static final transient Log logger = LogFactory.getLog( ConfigPropertySourceLocator.class ); +public class ConfigPropertySourceLocator { + private static final transient Log logger = LogFactory.getLog(ConfigPropertySourceLocator.class); // ============================================================ // Class Constants: @@ -41,7 +40,7 @@ public class ConfigPropertySourceLocator private static final String PROPERTY_SOURCE_NAME = "jsonResource"; private static final String THREAD_NAME = "ConfigLocator"; - private static final ObjectMapper YAML_MAPPER = new ObjectMapper( new YAMLFactory() ); + private static final ObjectMapper YAML_MAPPER = new ObjectMapper(new YAMLFactory()); private static final JavaPropsMapper PROPERTIES_MAPPER = new JavaPropsMapper(); // ============================================================ @@ -49,11 +48,11 @@ public class ConfigPropertySourceLocator // ============================================================ private final FileConfigClient fileConfigClient; - private final ExecutorService executorService = Executors.newFixedThreadPool( 10, - new BasicThreadFactory.Builder().namingPattern( THREAD_NAME + "-%d" ) - .daemon( true ) - .priority( Thread.MAX_PRIORITY ) - .build() ); + private final ExecutorService executorService = Executors.newFixedThreadPool(10, + new BasicThreadFactory.Builder().namingPattern(THREAD_NAME + "-%d") + .daemon(true) + .priority(Thread.MAX_PRIORITY) + .build()); // ============================================================ // Constructors: @@ -64,8 +63,7 @@ public class ConfigPropertySourceLocator * * @param fileConfigClient the file config client to load files from the config server. */ - public ConfigPropertySourceLocator( final FileConfigClient fileConfigClient ) - { + public ConfigPropertySourceLocator(final FileConfigClient fileConfigClient) { this.fileConfigClient = fileConfigClient; } @@ -79,31 +77,25 @@ public ConfigPropertySourceLocator( final FileConfigClient fileConfigClient ) * @param resources the resources to load * @return The property source with the JSON resources loaded. */ - public PropertySource locateConfigResources( final List resources ) - { - CompositePropertySource composite = new CompositePropertySource( PROPERTY_SOURCE_NAME ); + public PropertySource locateConfigResources(final List resources) { + CompositePropertySource composite = new CompositePropertySource(PROPERTY_SOURCE_NAME); // // Load files asynchronously // List> futures = new ArrayList<>(); - resources.stream().filter( Objects::nonNull ).map( resource -> - getJsonConfiguration( composite, resource.getDirectory(), resource.getFiles() ) ).forEach( futures::addAll ); - final CompletableFuture> listCompletableFuture = CompletableFuture.allOf( futures.toArray( new CompletableFuture[ 0 ] ) ) - .thenApply( future -> futures.stream().map( CompletableFuture::join ).collect( Collectors.toList() ) ); - try - { + resources.stream().filter(Objects::nonNull).map(resource -> + getJsonConfiguration(composite, resource.getDirectory(), resource.getFiles())).forEach(futures::addAll); + final CompletableFuture> listCompletableFuture = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])) + .thenApply(future -> futures.stream().map(CompletableFuture::join).collect(Collectors.toList())); + try { // // Wait for files to complete -- don't want Spring to start setting up Beans without all property sources // listCompletableFuture.get(); - logger.info( "Successfully loaded all external configuration files." ); - } - catch ( InterruptedException | ExecutionException e ) - { - throw new ConfigResourceException( "Failed to retrieve files.", e ); - } - finally - { + logger.info("Successfully loaded all external configuration files."); + } catch (InterruptedException | ExecutionException e) { + throw new ConfigResourceException("Failed to retrieve files.", e); + } finally { executorService.shutdownNow(); } return composite; @@ -113,101 +105,76 @@ public PropertySource locateConfigResources( final List resources ) // Private Methods: // ============================================================ - private List> getJsonConfiguration( final CompositePropertySource composite, - final String directoryPath, - final List files ) - { - return files.stream().filter( Objects::nonNull ).map( file -> CompletableFuture.supplyAsync( () -> - loadConfigurationFile( directoryPath, file ), executorService ).thenAccept( mapPropertySource -> { - if ( mapPropertySource != null ) - { - composite.addPropertySource( mapPropertySource ); + private List> getJsonConfiguration(final CompositePropertySource composite, + final String directoryPath, + final List files) { + return files.stream().filter(Objects::nonNull).map(file -> CompletableFuture.supplyAsync(() -> + loadConfigurationFile(directoryPath, file), executorService).thenAccept(mapPropertySource -> { + if (mapPropertySource != null) { + composite.addPropertySource(mapPropertySource); } - } ) ).collect( Collectors.toList() ); + })).collect(Collectors.toList()); } - @SuppressWarnings( "unchecked" ) - private MapPropertySource loadConfigurationFile( final String directoryPath, final String fileName ) - { - final String filePath = getFilePath( directoryPath, fileName ); + @SuppressWarnings("unchecked") + private MapPropertySource loadConfigurationFile(final String directoryPath, final String fileName) { + final String filePath = getFilePath(directoryPath, fileName); Map file; - try - { - logger.info( "Loading configuration " + filePath + "..." ); - if ( StringUtils.endsWithIgnoreCase( fileName, ".yml" ) || StringUtils.endsWithIgnoreCase( fileName, ".yaml" ) ) - { - file = getYAMLFile( directoryPath, fileName ); - } - else if ( StringUtils.endsWithIgnoreCase( fileName, ".properties" ) ) - { - file = getPropertiesFile( directoryPath, fileName ); - } - else - { - file = getJSONFile( directoryPath, fileName ); + try { + logger.info("Loading configuration " + filePath + "..."); + if (StringUtils.endsWithIgnoreCase(fileName, ".yml") || StringUtils.endsWithIgnoreCase(fileName, ".yaml")) { + file = getYAMLFile(directoryPath, fileName); + } else if (StringUtils.endsWithIgnoreCase(fileName, ".properties")) { + file = getPropertiesFile(directoryPath, fileName); + } else { + file = getJSONFile(directoryPath, fileName); } + } catch (ConfigException e) { + throw new ConfigResourceException("Failed to load " + filePath, e); } - catch ( ConfigException e ) - { - throw new ConfigResourceException( "Failed to load " + filePath, e ); - } - if ( !file.isEmpty() ) - { - final Map flattenMap = MapFlattener.flatten( file ); - return new MapPropertySource( filePath, flattenMap ); + if (!file.isEmpty()) { + final Map flattenMap = MapFlattener.flatten(file); + return new MapPropertySource(filePath, flattenMap); } return null; } - private String getFilePath( final String directoryPath, final String fileName ) - { + private String getFilePath(final String directoryPath, final String fileName) { return directoryPath + "/" + fileName; } - private Map getYAMLFile( final String directoryPath, final String fileName ) throws ConfigException - { + private Map getYAMLFile(final String directoryPath, final String fileName) throws ConfigException { final Map file; - final byte[] yamlFile = fileConfigClient.getFileFromDefaultBranch( fileName, directoryPath, byte[].class ); - if ( yamlFile == null ) - { - throw new ConfigResourceException( "Failed to find file " + fileName + " from the Config Server. Ensure the file exists." ); - } - try - { - file = YAML_MAPPER.readValue( yamlFile, Map.class ); + final byte[] yamlFile = fileConfigClient.getFileFromDefaultBranch(fileName, directoryPath, byte[].class); + if (yamlFile == null) { + throw new ConfigResourceException("Failed to find file " + fileName + " from the Config Server. Ensure the file exists."); } - catch ( IOException e ) - { - throw new ConfigResourceException( "Failed to convert " + fileName + " from YAML format to be loaded in the property sources.", e ); + try { + file = YAML_MAPPER.readValue(yamlFile, Map.class); + } catch (IOException e) { + throw new ConfigResourceException("Failed to convert " + fileName + " from YAML format to be loaded in the property sources.", e); } return file; } - private Map getPropertiesFile( final String directoryPath, final String fileName ) throws ConfigException - { + private Map getPropertiesFile(final String directoryPath, final String fileName) throws ConfigException { final Map file; - final byte[] propertiesFile = fileConfigClient.getFileFromDefaultBranch( fileName, directoryPath, byte[].class ); - if ( propertiesFile == null ) - { - throw new ConfigResourceException( "Failed to find file " + fileName + " from the Config Server. Ensure the file exists." ); - } - try - { - file = PROPERTIES_MAPPER.readValue( propertiesFile, Map.class ); + final byte[] propertiesFile = fileConfigClient.getFileFromDefaultBranch(fileName, directoryPath, byte[].class); + if (propertiesFile == null) { + throw new ConfigResourceException("Failed to find file " + fileName + " from the Config Server. Ensure the file exists."); } - catch ( IOException e ) - { - throw new ConfigResourceException( "Failed to convert " + fileName + " from PROPERTIES format to be loaded in the property sources.", e ); + try { + file = PROPERTIES_MAPPER.readValue(propertiesFile, Map.class); + } catch (IOException e) { + throw new ConfigResourceException("Failed to convert " + fileName + " from PROPERTIES format to be loaded in the property sources.", e); } return file; } - private Map getJSONFile( final String directoryPath, final String fileName ) throws ConfigException - { - final Map jsonFile = fileConfigClient.getFileFromDefaultBranch( fileName, directoryPath, Map.class ); - if ( jsonFile == null ) - { - throw new ConfigResourceException( "Failed to find file " + fileName + " from the Config Server. Ensure the file exists." ); + private Map getJSONFile(final String directoryPath, final String fileName) throws ConfigException { + final Map jsonFile = fileConfigClient.getFileFromDefaultBranch(fileName, directoryPath, Map.class); + if (jsonFile == null) { + throw new ConfigResourceException("Failed to find file " + fileName + " from the Config Server. Ensure the file exists."); } return jsonFile; } diff --git a/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/env/ConfigResourceBootstrapInitializer.java b/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/env/ConfigResourceBootstrapInitializer.java index d63aa72..e2f1e93 100644 --- a/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/env/ConfigResourceBootstrapInitializer.java +++ b/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/env/ConfigResourceBootstrapInitializer.java @@ -32,8 +32,7 @@ * Created by Piszmog on 7/21/2018 */ @Configuration -public class ConfigResourceBootstrapInitializer implements ApplicationContextInitializer, Ordered -{ +public class ConfigResourceBootstrapInitializer implements ApplicationContextInitializer, Ordered { // ============================================================ // Class Constants: // ============================================================ @@ -53,59 +52,49 @@ public class ConfigResourceBootstrapInitializer implements ApplicationContextIni // ============================================================ @Override - public void initialize( final ConfigurableApplicationContext applicationContext ) - { + public void initialize(final ConfigurableApplicationContext applicationContext) { final ConfigurableEnvironment environment = applicationContext.getEnvironment(); - final String isEnabled = environment.getProperty( PROPERTY_ENABLED ); - if ( ( StringUtils.isBlank( isEnabled ) || BooleanUtils.toBoolean( isEnabled ) ) - && configPropertySourceLocator != null ) - { + final String isEnabled = environment.getProperty(PROPERTY_ENABLED); + if ((StringUtils.isBlank(isEnabled) || BooleanUtils.toBoolean(isEnabled)) + && configPropertySourceLocator != null) { final List resources = new ArrayList<>(); - for ( int i = 0; i < MAX_SIZE; i++ ) - { + for (int i = 0; i < MAX_SIZE; i++) { final List files = new ArrayList<>(); - final String directory = environment.getProperty( PROPERTY_FILE + ".resources[" + i + "].directory" ); - for ( int j = 0; j < MAX_SIZE; j++ ) - { - final String fileName = environment.getProperty( PROPERTY_FILE + ".resources[" + i + "].files[" + j + "]" ); - if ( StringUtils.isBlank( fileName ) ) - { + final String directory = environment.getProperty(PROPERTY_FILE + ".resources[" + i + "].directory"); + for (int j = 0; j < MAX_SIZE; j++) { + final String fileName = environment.getProperty(PROPERTY_FILE + ".resources[" + i + "].files[" + j + "]"); + if (StringUtils.isBlank(fileName)) { break; } - if ( StringUtils.isBlank( directory ) && StringUtils.isNotBlank( fileName ) ) - { - throw new ConfigResourceException( "Directory must not be null or blank when file is not blank or null." ); + if (StringUtils.isBlank(directory) && StringUtils.isNotBlank(fileName)) { + throw new ConfigResourceException("Directory must not be null or blank when file is not blank or null."); } - if ( !StringUtils.endsWithIgnoreCase( fileName, ".json" ) - && !StringUtils.endsWithIgnoreCase( fileName, ".yml" ) - && !StringUtils.endsWithIgnoreCase( fileName, ".yaml" ) - && !StringUtils.endsWithIgnoreCase( fileName, ".properties" ) ) - { - throw new ConfigResourceException( "File " + fileName + " is not a configuration file." + - " Only .json, .yml, .yaml, and .properties are accepted." ); + if (!StringUtils.endsWithIgnoreCase(fileName, ".json") + && !StringUtils.endsWithIgnoreCase(fileName, ".yml") + && !StringUtils.endsWithIgnoreCase(fileName, ".yaml") + && !StringUtils.endsWithIgnoreCase(fileName, ".properties")) { + throw new ConfigResourceException("File " + fileName + " is not a configuration file." + + " Only .json, .yml, .yaml, and .properties are accepted."); } - files.add( fileName ); + files.add(fileName); } - if ( StringUtils.isBlank( directory ) && files.isEmpty() ) - { + if (StringUtils.isBlank(directory) && files.isEmpty()) { break; } final Resource resource = new Resource(); - resource.setDirectory( directory ); - resource.setFiles( files ); - resources.add( resource ); + resource.setDirectory(directory); + resource.setFiles(files); + resources.add(resource); } - if ( !resources.isEmpty() ) - { - final PropertySource propertySource = configPropertySourceLocator.locateConfigResources( resources ); - environment.getPropertySources().addFirst( propertySource ); + if (!resources.isEmpty()) { + final PropertySource propertySource = configPropertySourceLocator.locateConfigResources(resources); + environment.getPropertySources().addFirst(propertySource); } } } @Override - public int getOrder() - { + public int getOrder() { // // Want to let all other initializers go first. Potentially the config server needs to be loaded first // before we can load the files. @@ -117,9 +106,8 @@ public int getOrder() // Public Methods: // ============================================================ - @Autowired( required = false ) - public void setConfigPropertySourceLocator( final ConfigPropertySourceLocator configPropertySourceLocator ) - { + @Autowired(required = false) + public void setConfigPropertySourceLocator(final ConfigPropertySourceLocator configPropertySourceLocator) { this.configPropertySourceLocator = configPropertySourceLocator; } } diff --git a/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/env/ConfigResourceException.java b/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/env/ConfigResourceException.java index bcf6fed..7839e63 100644 --- a/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/env/ConfigResourceException.java +++ b/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/env/ConfigResourceException.java @@ -5,13 +5,11 @@ *

* Created by Piszmog on 5/5/2018 */ -public class ConfigResourceException extends RuntimeException -{ +public class ConfigResourceException extends RuntimeException { /** * Creates a new configuration resource exception. */ - public ConfigResourceException() - { + public ConfigResourceException() { } /** @@ -19,9 +17,8 @@ public ConfigResourceException() * * @param message the message associated with the exception */ - public ConfigResourceException( final String message ) - { - super( message ); + public ConfigResourceException(final String message) { + super(message); } /** @@ -30,9 +27,8 @@ public ConfigResourceException( final String message ) * @param message the message associated with the exception * @param cause the cause of th exception */ - public ConfigResourceException( final String message, final Throwable cause ) - { - super( message, cause ); + public ConfigResourceException(final String message, final Throwable cause) { + super(message, cause); } /** @@ -40,9 +36,8 @@ public ConfigResourceException( final String message, final Throwable cause ) * * @param cause the cause of th exception */ - public ConfigResourceException( final Throwable cause ) - { - super( cause ); + public ConfigResourceException(final Throwable cause) { + super(cause); } /** @@ -53,8 +48,7 @@ public ConfigResourceException( final Throwable cause ) * @param enableSuppression determines if suppression is enabled * @param writableStackTrace determines if the stack is writeable */ - public ConfigResourceException( final String message, final Throwable cause, final boolean enableSuppression, final boolean writableStackTrace ) - { - super( message, cause, enableSuppression, writableStackTrace ); + public ConfigResourceException(final String message, final Throwable cause, final boolean enableSuppression, final boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); } } diff --git a/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/env/ResourceBootstrapConfig.java b/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/env/ResourceBootstrapConfig.java index 6a40ab1..c847f3b 100644 --- a/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/env/ResourceBootstrapConfig.java +++ b/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/env/ResourceBootstrapConfig.java @@ -20,10 +20,9 @@ */ @Order @Configuration -@ConditionalOnProperty( prefix = "cloud.config.client", name = "file.enabled", matchIfMissing = true ) -@Import( { LocalConfigTemplateBootstrapConfiguration.class, OAuth2ConfigTemplateBootStrapConfiguration.class } ) -public class ResourceBootstrapConfig -{ +@ConditionalOnProperty(prefix = "cloud.config.client", name = "file.enabled", matchIfMissing = true) +@Import({LocalConfigTemplateBootstrapConfiguration.class, OAuth2ConfigTemplateBootStrapConfiguration.class}) +public class ResourceBootstrapConfig { /** * Create a file client. To disable, the property {@code cloud.config.client.file.enabled} must be set via bootstrap * configuration. @@ -33,9 +32,8 @@ public class ResourceBootstrapConfig */ @Order @Bean - public FileConfigClient fileConfigClient( final ConfigTemplate configTemplate ) - { - return new FileConfigClient( configTemplate ); + public FileConfigClient fileConfigClient(final ConfigTemplate configTemplate) { + return new FileConfigClient(configTemplate); } /** @@ -46,8 +44,7 @@ public FileConfigClient fileConfigClient( final ConfigTemplate configTemplate ) */ @Order @Bean - public ConfigPropertySourceLocator configPropertySourceLocator( final FileConfigClient fileConfigClient ) - { - return new ConfigPropertySourceLocator( fileConfigClient ); + public ConfigPropertySourceLocator configPropertySourceLocator(final FileConfigClient fileConfigClient) { + return new ConfigPropertySourceLocator(fileConfigClient); } } diff --git a/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/env/model/Resource.java b/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/env/model/Resource.java index 417ff58..79833a1 100644 --- a/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/env/model/Resource.java +++ b/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/env/model/Resource.java @@ -7,8 +7,7 @@ *

* Created by Piszmog on 7/21/2018 */ -public class Resource -{ +public class Resource { // ============================================================ // Class Attributes: // ============================================================ @@ -20,23 +19,19 @@ public class Resource // Public Methods: // ============================================================ - public String getDirectory() - { + public String getDirectory() { return directory; } - public void setDirectory( final String directory ) - { + public void setDirectory(final String directory) { this.directory = directory; } - public List getFiles() - { + public List getFiles() { return files; } - public void setFiles( final List files ) - { + public void setFiles(final List files) { this.files = files; } } diff --git a/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/env/support/MapFlattener.java b/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/env/support/MapFlattener.java index 791a622..53b02a8 100644 --- a/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/env/support/MapFlattener.java +++ b/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/env/support/MapFlattener.java @@ -14,8 +14,7 @@ *

* Created by Piszmog on 7/25/2018 */ -public class MapFlattener -{ +public class MapFlattener { // ============================================================ // Static Methods: // ============================================================ @@ -26,10 +25,9 @@ public class MapFlattener * @param map the map to flatten * @return The flatten map with the keys in the Spring properties format. */ - public static Map flatten( Map map ) - { + public static Map flatten(Map map) { Map result = new LinkedHashMap<>(); - flatten( null, result, map ); + flatten(null, result, map); return result; } @@ -37,47 +35,38 @@ public static Map flatten( Map map ) // Private Methods: // ============================================================ - private static void flatten( String prefix, - Map result, - Map map ) - { - String namePrefix = ( prefix != null ? prefix + "." : "" ); - map.forEach( ( key, value ) -> { + private static void flatten(String prefix, + Map result, + Map map) { + String namePrefix = (prefix != null ? prefix + "." : ""); + map.forEach((key, value) -> { String finalKey = key; // // If key has certain characters in it, we want to wrap key in '[' and ']'. // - if ( StringUtils.contains( key, ":" ) - || StringUtils.contains( key, "." ) - || StringUtils.contains( key, "*" ) - || StringUtils.startsWith( key, "$" ) - || StringUtils.startsWith( key, "#" ) ) - { + if (StringUtils.contains(key, ":") + || StringUtils.contains(key, ".") + || StringUtils.contains(key, "*") + || StringUtils.startsWith(key, "$") + || StringUtils.startsWith(key, "#")) { finalKey = "[" + key + "]"; } - extract( namePrefix + finalKey, result, value ); - } ); + extract(namePrefix + finalKey, result, value); + }); } - @SuppressWarnings( "unchecked" ) - private static void extract( String name, Map result, Object value ) - { - if ( value instanceof Map ) - { - flatten( name, result, (Map) value ); - } - else if ( value instanceof Collection ) - { + @SuppressWarnings("unchecked") + private static void extract(String name, Map result, Object value) { + if (value instanceof Map) { + flatten(name, result, (Map) value); + } else if (value instanceof Collection) { int index = 0; - for ( Object object : (Collection) value ) - { - extract( name + "[" + index + "]", result, object ); + for (Object object : (Collection) value) { + extract(name + "[" + index + "]", result, object); index++; } - } - else - { - result.put( name, value ); + } else { + result.put(name, value); } } } diff --git a/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/template/LocalConfigTemplateBootstrapConfiguration.java b/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/template/LocalConfigTemplateBootstrapConfiguration.java index 255bb48..964a973 100644 --- a/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/template/LocalConfigTemplateBootstrapConfiguration.java +++ b/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/template/LocalConfigTemplateBootstrapConfiguration.java @@ -18,10 +18,9 @@ */ @Order @Configuration -@ConditionalOnProperty( value = "spring.cloud.config.client.oauth2.client-id", matchIfMissing = true ) -@Import( ConfigServiceBootstrapConfiguration.class ) -public class LocalConfigTemplateBootstrapConfiguration -{ +@ConditionalOnProperty(value = "spring.cloud.config.client.oauth2.client-id", matchIfMissing = true) +@Import(ConfigServiceBootstrapConfiguration.class) +public class LocalConfigTemplateBootstrapConfiguration { // ============================================================ // Beans: // ============================================================ @@ -35,8 +34,7 @@ public class LocalConfigTemplateBootstrapConfiguration @Order @Bean @ConditionalOnMissingBean - public ConfigTemplate localConfigTemplate( final ConfigClientProperties configClientProperties ) - { - return new LocalConfigTemplate( configClientProperties ); + public ConfigTemplate localConfigTemplate(final ConfigClientProperties configClientProperties) { + return new LocalConfigTemplate(configClientProperties); } } diff --git a/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/template/OAuth2ConfigTemplateBootStrapConfiguration.java b/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/template/OAuth2ConfigTemplateBootStrapConfiguration.java index 4360404..60573d6 100644 --- a/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/template/OAuth2ConfigTemplateBootStrapConfiguration.java +++ b/src/main/java/io/github/piszmog/cloudconfigclient/autoconfig/template/OAuth2ConfigTemplateBootStrapConfiguration.java @@ -2,7 +2,7 @@ import io.github.piszmog.cloudconfig.template.ConfigTemplate; import io.github.piszmog.cloudconfig.template.impl.OAuth2ConfigTemplate; -import io.pivotal.spring.cloud.service.config.ConfigClientOAuth2BootstrapConfiguration; +import io.pivotal.spring.cloud.config.client.ConfigClientOAuth2Properties; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -12,7 +12,6 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.core.annotation.Order; -import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails; /** * Auto-configuration for creating a {@link OAuth2ConfigTemplate}. @@ -21,11 +20,10 @@ */ @Order @Configuration -@ConditionalOnClass( OAuth2ProtectedResourceDetails.class ) -@ConditionalOnProperty( value = "spring.cloud.config.client.oauth2.client-id" ) -@Import( { ConfigServiceBootstrapConfiguration.class, ConfigClientOAuth2BootstrapConfiguration.class } ) -public class OAuth2ConfigTemplateBootStrapConfiguration -{ +@ConditionalOnClass(ConfigClientOAuth2Properties.class) +@ConditionalOnProperty(prefix = "spring.cloud.config.client.oauth2", name = {"client-id", "client-secret", "access-token-uri"}) +@Import({ConfigServiceBootstrapConfiguration.class, ConfigClientOAuth2Properties.class}) +public class OAuth2ConfigTemplateBootStrapConfiguration { // ============================================================ // Beans: // ============================================================ @@ -33,16 +31,15 @@ public class OAuth2ConfigTemplateBootStrapConfiguration /** * Creates a config template using OAuth2 credentials to connect to the config server. * - * @param configClientProperties the config client properties - * @param oAuth2ProtectedResourceDetails the OAuth2 details + * @param configClientProperties the config client properties + * @param configClientOAuth2Properties the OAuth2 details * @return The config template. */ @Order @Bean @ConditionalOnMissingBean - public ConfigTemplate oauth2ConfigTemplate( final ConfigClientProperties configClientProperties, - final OAuth2ProtectedResourceDetails oAuth2ProtectedResourceDetails ) - { - return new OAuth2ConfigTemplate( configClientProperties, oAuth2ProtectedResourceDetails ); + public ConfigTemplate oauth2ConfigTemplate(final ConfigClientProperties configClientProperties, + final ConfigClientOAuth2Properties configClientOAuth2Properties) { + return new OAuth2ConfigTemplate(configClientProperties, configClientOAuth2Properties); } } diff --git a/src/test/groovy/io/github/piszmog/cloudconfigclient/autoconfig/env/ConfigPropertySourceLocatorSpec.groovy b/src/test/groovy/io/github/piszmog/cloudconfigclient/autoconfig/env/ConfigPropertySourceLocatorSpec.groovy index f8a55d8..99c7ad7 100644 --- a/src/test/groovy/io/github/piszmog/cloudconfigclient/autoconfig/env/ConfigPropertySourceLocatorSpec.groovy +++ b/src/test/groovy/io/github/piszmog/cloudconfigclient/autoconfig/env/ConfigPropertySourceLocatorSpec.groovy @@ -7,13 +7,12 @@ import spock.lang.Specification /** * Created by Piszmog on 7/22/2018 */ -class ConfigPropertySourceLocatorSpec extends Specification -{ +class ConfigPropertySourceLocatorSpec extends Specification { // ============================================================ // Class Constants: // ============================================================ - private static final def JSON_FILE_CONTENTS = [ field: "value" ] + private static final def JSON_FILE_CONTENTS = [field: "value"] // ============================================================ // Class Attributes: @@ -25,85 +24,81 @@ class ConfigPropertySourceLocatorSpec extends Specification // Setup: // ============================================================ - def setup() - { - fileConfigClient = Stub( FileConfigClient ) + def setup() { + fileConfigClient = Stub(FileConfigClient) } // ============================================================ // Tests: // ============================================================ - def "load a resource"() - { + def "load a resource"() { given: "a configuration source locator" - def locator = new ConfigPropertySourceLocator( fileConfigClient ) + def locator = new ConfigPropertySourceLocator(fileConfigClient) and: "a resource to load with 1 file" - def resource = createResource( "directory", "file1.json" ) + def resource = createResource("directory", "file1.json") and: "files are found" - fileConfigClient.getFileFromDefaultBranch( _, _, _ ) >> JSON_FILE_CONTENTS + fileConfigClient.getFileFromDefaultBranch(_, _, _) >> JSON_FILE_CONTENTS when: "the locator looks for the resource" - def propertySource = locator.locateConfigResources( Collections.singletonList( resource ) ) + def propertySource = locator.locateConfigResources(Collections.singletonList(resource)) then: "the file is converter to a properties object" - notThrown( ConfigResourceException ) + notThrown(ConfigResourceException) and: "the property source contains the resource" !propertySource.getProperties().isEmpty() } - def "load multiple resource files"() - { + def "load multiple resource files"() { given: "a configuration source locator" - def locator = new ConfigPropertySourceLocator( fileConfigClient ) + def locator = new ConfigPropertySourceLocator(fileConfigClient) and: "a resource to load with multiple files" - def resource = createResource( "directory", "file1.json", "file2.json", "file3.json" ) + def resource = createResource("directory", "file1.json", "file2.json", "file3.json") and: "a file is found" - fileConfigClient.getFileFromDefaultBranch( _, _, _ ) >> JSON_FILE_CONTENTS + fileConfigClient.getFileFromDefaultBranch(_, _, _) >> JSON_FILE_CONTENTS when: "the locator looks for the resource" - def propertySource = locator.locateConfigResources( Collections.singletonList( resource ) ) + def propertySource = locator.locateConfigResources(Collections.singletonList(resource)) then: "the file is converter to a properties object" - notThrown( ConfigResourceException ) + notThrown(ConfigResourceException) and: "the property source contains the resource" !propertySource.getProperties().isEmpty() } - def "load multiple resources"() - { + def "load multiple resources"() { given: "a configuration source locator" - def locator = new ConfigPropertySourceLocator( fileConfigClient ) + def locator = new ConfigPropertySourceLocator(fileConfigClient) and: "resources" List resources = new ArrayList() and: "resource1 to load with 1 file" - def resource1 = createResource( "directory1", "file1.json" ) - resources.add( resource1 ) + def resource1 = createResource("directory1", "file1.json") + resources.add(resource1) and: "resource2 to load with 1 file" - def resource2 = createResource( "directory2", "file2.json" ) - resources.add( resource2 ) + def resource2 = createResource("directory2", "file2.json") + resources.add(resource2) and: "resource3 to load with 1 file" - def resource3 = createResource( "directory3", "file3.json" ) - resources.add( resource3 ) + def resource3 = createResource("directory3", "file3.json") + resources.add(resource3) and: "files are found" - fileConfigClient.getFileFromDefaultBranch( _, _, _ ) >> JSON_FILE_CONTENTS + fileConfigClient.getFileFromDefaultBranch(_, _, _) >> JSON_FILE_CONTENTS when: "the locator looks for the resource" - def propertySource = locator.locateConfigResources( resources ) + def propertySource = locator.locateConfigResources(resources) then: "the file is converter to a properties object" - notThrown( ConfigResourceException ) + notThrown(ConfigResourceException) and: "the property source contains the resource" !propertySource.getProperties().isEmpty() @@ -113,11 +108,10 @@ class ConfigPropertySourceLocatorSpec extends Specification // Private Methods: // ============================================================ - private static Resource createResource( final String directory, final String... files ) - { + private static Resource createResource(final String directory, final String... files) { def resource = new Resource() - resource.setDirectory( directory ) - resource.setFiles( files as List ) + resource.setDirectory(directory) + resource.setFiles(files as List) return resource } } diff --git a/src/test/groovy/io/github/piszmog/cloudconfigclient/autoconfig/env/ConfigResourceBootstrapInitializerSpec.groovy b/src/test/groovy/io/github/piszmog/cloudconfigclient/autoconfig/env/ConfigResourceBootstrapInitializerSpec.groovy index 55a2da5..0fbac4e 100644 --- a/src/test/groovy/io/github/piszmog/cloudconfigclient/autoconfig/env/ConfigResourceBootstrapInitializerSpec.groovy +++ b/src/test/groovy/io/github/piszmog/cloudconfigclient/autoconfig/env/ConfigResourceBootstrapInitializerSpec.groovy @@ -9,8 +9,7 @@ import spock.lang.Specification /** * Created by Piszmog on 7/22/2018 */ -class ConfigResourceBootstrapInitializerSpec extends Specification -{ +class ConfigResourceBootstrapInitializerSpec extends Specification { // ============================================================ // Class Attributes: // ============================================================ @@ -22,313 +21,303 @@ class ConfigResourceBootstrapInitializerSpec extends Specification // Setup: // ============================================================ - def setup() - { - configPropertySourceLocator = Stub( ConfigPropertySourceLocator ) - configurableApplicationContext = Stub( ConfigurableApplicationContext ) + def setup() { + configPropertySourceLocator = Stub(ConfigPropertySourceLocator) + configurableApplicationContext = Stub(ConfigurableApplicationContext) } // ============================================================ // TestS: // ============================================================ - def "configuration initializer adds json"() - { + def "configuration initializer adds json"() { given: "A configuration initializer" def initializer = new ConfigResourceBootstrapInitializer() and: "the locator is set" - initializer.setConfigPropertySourceLocator( configPropertySourceLocator ) + initializer.setConfigPropertySourceLocator(configPropertySourceLocator) and: "the context has an environment" - def environment = Stub( ConfigurableEnvironment ) + def environment = Stub(ConfigurableEnvironment) configurableApplicationContext.getEnvironment() >> environment and: "environment has property sources" def sources = new MutablePropertySources() - environment.getPropertySources( ) >> sources + environment.getPropertySources() >> sources and: "the environment does not find the enabled flag" - environment.getProperty( "cloud.config.client.file.enabled" ) >> null + environment.getProperty("cloud.config.client.file.enabled") >> null and: "directories and files are found" - environment.getProperty( "cloud.config.client.file.resources[0].directory" ) >> "directory" - environment.getProperty( "cloud.config.client.file.resources[0].files[0]" ) >> "file1.json" + environment.getProperty("cloud.config.client.file.resources[0].directory") >> "directory" + environment.getProperty("cloud.config.client.file.resources[0].files[0]") >> "file1.json" and: "and property sources are found" - configPropertySourceLocator.locateConfigResources( _ ) >> new CompositePropertySource( "example" ) + configPropertySourceLocator.locateConfigResources(_) >> new CompositePropertySource("example") when: "the initializer is ran" - initializer.initialize( configurableApplicationContext ) + initializer.initialize(configurableApplicationContext) then: "the locator's property source is in the environment's property sources" - sources.contains( "example" ) + sources.contains("example") } - def "configuration initializer adds yml"() - { + def "configuration initializer adds yml"() { given: "A configuration initializer" def initializer = new ConfigResourceBootstrapInitializer() and: "the locator is set" - initializer.setConfigPropertySourceLocator( configPropertySourceLocator ) + initializer.setConfigPropertySourceLocator(configPropertySourceLocator) and: "the context has an environment" - def environment = Stub( ConfigurableEnvironment ) + def environment = Stub(ConfigurableEnvironment) configurableApplicationContext.getEnvironment() >> environment and: "environment has property sources" def sources = new MutablePropertySources() - environment.getPropertySources( ) >> sources + environment.getPropertySources() >> sources and: "the environment does not find the enabled flag" - environment.getProperty( "cloud.config.client.file.enabled" ) >> null + environment.getProperty("cloud.config.client.file.enabled") >> null and: "directories and files are found" - environment.getProperty( "cloud.config.client.file.resources[0].directory" ) >> "directory" - environment.getProperty( "cloud.config.client.file.resources[0].files[0]" ) >> "file1.yml" + environment.getProperty("cloud.config.client.file.resources[0].directory") >> "directory" + environment.getProperty("cloud.config.client.file.resources[0].files[0]") >> "file1.yml" and: "and property sources are found" - configPropertySourceLocator.locateConfigResources( _ ) >> new CompositePropertySource( "example" ) + configPropertySourceLocator.locateConfigResources(_) >> new CompositePropertySource("example") when: "the initializer is ran" - initializer.initialize( configurableApplicationContext ) + initializer.initialize(configurableApplicationContext) then: "yml files are allowed to be added" - notThrown( ConfigResourceException ) + notThrown(ConfigResourceException) } - def "configuration initializer adds yaml"() - { + def "configuration initializer adds yaml"() { given: "A configuration initializer" def initializer = new ConfigResourceBootstrapInitializer() and: "the locator is set" - initializer.setConfigPropertySourceLocator( configPropertySourceLocator ) + initializer.setConfigPropertySourceLocator(configPropertySourceLocator) and: "the context has an environment" - def environment = Stub( ConfigurableEnvironment ) + def environment = Stub(ConfigurableEnvironment) configurableApplicationContext.getEnvironment() >> environment and: "environment has property sources" def sources = new MutablePropertySources() - environment.getPropertySources( ) >> sources + environment.getPropertySources() >> sources and: "the environment does not find the enabled flag" - environment.getProperty( "cloud.config.client.file.enabled" ) >> null + environment.getProperty("cloud.config.client.file.enabled") >> null and: "directories and files are found" - environment.getProperty( "cloud.config.client.file.resources[0].directory" ) >> "directory" - environment.getProperty( "cloud.config.client.file.resources[0].files[0]" ) >> "file1.yaml" + environment.getProperty("cloud.config.client.file.resources[0].directory") >> "directory" + environment.getProperty("cloud.config.client.file.resources[0].files[0]") >> "file1.yaml" and: "and property sources are found" - configPropertySourceLocator.locateConfigResources( _ ) >> new CompositePropertySource( "example" ) + configPropertySourceLocator.locateConfigResources(_) >> new CompositePropertySource("example") when: "the initializer is ran" - initializer.initialize( configurableApplicationContext ) + initializer.initialize(configurableApplicationContext) then: "yaml files are allowed to be added" - notThrown( ConfigResourceException ) + notThrown(ConfigResourceException) } - def "configuration initializer adds properties"() - { + def "configuration initializer adds properties"() { given: "A configuration initializer" def initializer = new ConfigResourceBootstrapInitializer() and: "the locator is set" - initializer.setConfigPropertySourceLocator( configPropertySourceLocator ) + initializer.setConfigPropertySourceLocator(configPropertySourceLocator) and: "the context has an environment" - def environment = Stub( ConfigurableEnvironment ) + def environment = Stub(ConfigurableEnvironment) configurableApplicationContext.getEnvironment() >> environment and: "environment has property sources" def sources = new MutablePropertySources() - environment.getPropertySources( ) >> sources + environment.getPropertySources() >> sources and: "the environment does not find the enabled flag" - environment.getProperty( "cloud.config.client.file.enabled" ) >> null + environment.getProperty("cloud.config.client.file.enabled") >> null and: "directories and files are found" - environment.getProperty( "cloud.config.client.file.resources[0].directory" ) >> "directory" - environment.getProperty( "cloud.config.client.file.resources[0].files[0]" ) >> "file1.properties" + environment.getProperty("cloud.config.client.file.resources[0].directory") >> "directory" + environment.getProperty("cloud.config.client.file.resources[0].files[0]") >> "file1.properties" and: "and property sources are found" - configPropertySourceLocator.locateConfigResources( _ ) >> new CompositePropertySource( "example" ) + configPropertySourceLocator.locateConfigResources(_) >> new CompositePropertySource("example") when: "the initializer is ran" - initializer.initialize( configurableApplicationContext ) + initializer.initialize(configurableApplicationContext) then: "properties files are allowed to be added" - notThrown( ConfigResourceException ) + notThrown(ConfigResourceException) } - def "configuration initializer is provided a text file to add"() - { + def "configuration initializer is provided a text file to add"() { given: "A configuration initializer" def initializer = new ConfigResourceBootstrapInitializer() and: "the locator is set" - initializer.setConfigPropertySourceLocator( configPropertySourceLocator ) + initializer.setConfigPropertySourceLocator(configPropertySourceLocator) and: "the context has an environment" - def environment = Stub( ConfigurableEnvironment ) + def environment = Stub(ConfigurableEnvironment) configurableApplicationContext.getEnvironment() >> environment and: "environment has property sources" def sources = new MutablePropertySources() - environment.getPropertySources( ) >> sources + environment.getPropertySources() >> sources and: "the environment does not find the enabled flag" - environment.getProperty( "cloud.config.client.file.enabled" ) >> null + environment.getProperty("cloud.config.client.file.enabled") >> null and: "directories and files are found" - environment.getProperty( "cloud.config.client.file.resources[0].directory" ) >> "directory" - environment.getProperty( "cloud.config.client.file.resources[0].files[0]" ) >> "file1.txt" + environment.getProperty("cloud.config.client.file.resources[0].directory") >> "directory" + environment.getProperty("cloud.config.client.file.resources[0].files[0]") >> "file1.txt" when: "the initializer is ran" - initializer.initialize( configurableApplicationContext ) + initializer.initialize(configurableApplicationContext) then: "text files are not allowed to be added" - thrown( ConfigResourceException ) + thrown(ConfigResourceException) } - def "configuration initializer has a directory as blank"() - { + def "configuration initializer has a directory as blank"() { given: "A configuration initializer" def initializer = new ConfigResourceBootstrapInitializer() and: "the locator is set" - initializer.setConfigPropertySourceLocator( configPropertySourceLocator ) + initializer.setConfigPropertySourceLocator(configPropertySourceLocator) and: "the context has an environment" - def environment = Stub( ConfigurableEnvironment ) + def environment = Stub(ConfigurableEnvironment) configurableApplicationContext.getEnvironment() >> environment and: "environment has property sources" def sources = new MutablePropertySources() - environment.getPropertySources( ) >> sources + environment.getPropertySources() >> sources and: "the environment does not find the enabled flag" - environment.getProperty( "cloud.config.client.file.enabled" ) >> null + environment.getProperty("cloud.config.client.file.enabled") >> null and: "directories and files are found" - environment.getProperty( "cloud.config.client.file.resources[0].directory" ) >> "" - environment.getProperty( "cloud.config.client.file.resources[0].files[0]" ) >> "file1.json" + environment.getProperty("cloud.config.client.file.resources[0].directory") >> "" + environment.getProperty("cloud.config.client.file.resources[0].files[0]") >> "file1.json" when: "the initializer is ran" - initializer.initialize( configurableApplicationContext ) + initializer.initialize(configurableApplicationContext) then: "the directory cannot be blank when a file is provided" - thrown( ConfigResourceException ) + thrown(ConfigResourceException) } - def "configuration initializer has a directory as null"() - { + def "configuration initializer has a directory as null"() { given: "A configuration initializer" def initializer = new ConfigResourceBootstrapInitializer() and: "the locator is set" - initializer.setConfigPropertySourceLocator( configPropertySourceLocator ) + initializer.setConfigPropertySourceLocator(configPropertySourceLocator) and: "the context has an environment" - def environment = Stub( ConfigurableEnvironment ) + def environment = Stub(ConfigurableEnvironment) configurableApplicationContext.getEnvironment() >> environment and: "environment has property sources" def sources = new MutablePropertySources() - environment.getPropertySources( ) >> sources + environment.getPropertySources() >> sources and: "the environment does not find the enabled flag" - environment.getProperty( "cloud.config.client.file.enabled" ) >> null + environment.getProperty("cloud.config.client.file.enabled") >> null and: "directories and files are found" - environment.getProperty( "cloud.config.client.file.resources[0].directory" ) >> null - environment.getProperty( "cloud.config.client.file.resources[0].files[0]" ) >> "file1.json" + environment.getProperty("cloud.config.client.file.resources[0].directory") >> null + environment.getProperty("cloud.config.client.file.resources[0].files[0]") >> "file1.json" when: "the initializer is ran" - initializer.initialize( configurableApplicationContext ) + initializer.initialize(configurableApplicationContext) then: "the directory cannot be null when a file is provided" - thrown( ConfigResourceException ) + thrown(ConfigResourceException) } - def "configuration initializer adds many json files"() - { + def "configuration initializer adds many json files"() { given: "A configuration initializer" def initializer = new ConfigResourceBootstrapInitializer() and: "the locator is set" - initializer.setConfigPropertySourceLocator( configPropertySourceLocator ) + initializer.setConfigPropertySourceLocator(configPropertySourceLocator) and: "the context has an environment" - def environment = Stub( ConfigurableEnvironment ) + def environment = Stub(ConfigurableEnvironment) configurableApplicationContext.getEnvironment() >> environment and: "environment has property sources" def sources = new MutablePropertySources() - environment.getPropertySources( ) >> sources + environment.getPropertySources() >> sources and: "the environment does not find the enabled flag" - environment.getProperty( "cloud.config.client.file.enabled" ) >> null + environment.getProperty("cloud.config.client.file.enabled") >> null and: "directories and files are found" - environment.getProperty( "cloud.config.client.file.resources[0].directory" ) >> "directory" - environment.getProperty( "cloud.config.client.file.resources[0].files[0]" ) >> "file1.json" - environment.getProperty( "cloud.config.client.file.resources[0].files[1]" ) >> "file2.json" - environment.getProperty( "cloud.config.client.file.resources[0].files[2]" ) >> "file3.json" + environment.getProperty("cloud.config.client.file.resources[0].directory") >> "directory" + environment.getProperty("cloud.config.client.file.resources[0].files[0]") >> "file1.json" + environment.getProperty("cloud.config.client.file.resources[0].files[1]") >> "file2.json" + environment.getProperty("cloud.config.client.file.resources[0].files[2]") >> "file3.json" and: "and property sources are found" - configPropertySourceLocator.locateConfigResources( _ ) >> new CompositePropertySource( "example" ) + configPropertySourceLocator.locateConfigResources(_) >> new CompositePropertySource("example") when: "the initializer is ran" - initializer.initialize( configurableApplicationContext ) + initializer.initialize(configurableApplicationContext) then: "the locator's property source is in the environment's property sources" - sources.contains( "example" ) + sources.contains("example") } - def "configuration initializer adds many json files in many directories"() - { + def "configuration initializer adds many json files in many directories"() { given: "A configuration initializer" def initializer = new ConfigResourceBootstrapInitializer() and: "the locator is set" - initializer.setConfigPropertySourceLocator( configPropertySourceLocator ) + initializer.setConfigPropertySourceLocator(configPropertySourceLocator) and: "the context has an environment" - def environment = Stub( ConfigurableEnvironment ) + def environment = Stub(ConfigurableEnvironment) configurableApplicationContext.getEnvironment() >> environment and: "environment has property sources" def sources = new MutablePropertySources() - environment.getPropertySources( ) >> sources + environment.getPropertySources() >> sources and: "the environment does not find the enabled flag" - environment.getProperty( "cloud.config.client.file.enabled" ) >> null + environment.getProperty("cloud.config.client.file.enabled") >> null and: "directories and files are found" - environment.getProperty( "cloud.config.client.file.resources[0].directory" ) >> "directory1" - environment.getProperty( "cloud.config.client.file.resources[0].files[0]" ) >> "file1.json" - environment.getProperty( "cloud.config.client.file.resources[0].files[1]" ) >> "file2.json" - environment.getProperty( "cloud.config.client.file.resources[0].files[2]" ) >> "file3.json" - environment.getProperty( "cloud.config.client.file.resources[1].directory" ) >> "directory2" - environment.getProperty( "cloud.config.client.file.resources[1].files[0]" ) >> "file4.json" - environment.getProperty( "cloud.config.client.file.resources[1].files[1]" ) >> "file5.json" - environment.getProperty( "cloud.config.client.file.resources[1].files[2]" ) >> "file6.json" - environment.getProperty( "cloud.config.client.file.resources[2].directory" ) >> "directory3" - environment.getProperty( "cloud.config.client.file.resources[2].files[0]" ) >> "file7.json" - environment.getProperty( "cloud.config.client.file.resources[2].files[1]" ) >> "file8.json" - environment.getProperty( "cloud.config.client.file.resources[2].files[2]" ) >> "file9.json" + environment.getProperty("cloud.config.client.file.resources[0].directory") >> "directory1" + environment.getProperty("cloud.config.client.file.resources[0].files[0]") >> "file1.json" + environment.getProperty("cloud.config.client.file.resources[0].files[1]") >> "file2.json" + environment.getProperty("cloud.config.client.file.resources[0].files[2]") >> "file3.json" + environment.getProperty("cloud.config.client.file.resources[1].directory") >> "directory2" + environment.getProperty("cloud.config.client.file.resources[1].files[0]") >> "file4.json" + environment.getProperty("cloud.config.client.file.resources[1].files[1]") >> "file5.json" + environment.getProperty("cloud.config.client.file.resources[1].files[2]") >> "file6.json" + environment.getProperty("cloud.config.client.file.resources[2].directory") >> "directory3" + environment.getProperty("cloud.config.client.file.resources[2].files[0]") >> "file7.json" + environment.getProperty("cloud.config.client.file.resources[2].files[1]") >> "file8.json" + environment.getProperty("cloud.config.client.file.resources[2].files[2]") >> "file9.json" and: "and property sources are found" - configPropertySourceLocator.locateConfigResources( _ ) >> new CompositePropertySource( "example" ) + configPropertySourceLocator.locateConfigResources(_) >> new CompositePropertySource("example") when: "the initializer is ran" - initializer.initialize( configurableApplicationContext ) + initializer.initialize(configurableApplicationContext) then: "the locator's property source is in the environment's property sources" - sources.contains( "example" ) + sources.contains("example") } } diff --git a/src/test/groovy/io/github/piszmog/cloudconfigclient/autoconfig/env/support/MapFlattenerSpec.groovy b/src/test/groovy/io/github/piszmog/cloudconfigclient/autoconfig/env/support/MapFlattenerSpec.groovy index 956e52c..105f056 100644 --- a/src/test/groovy/io/github/piszmog/cloudconfigclient/autoconfig/env/support/MapFlattenerSpec.groovy +++ b/src/test/groovy/io/github/piszmog/cloudconfigclient/autoconfig/env/support/MapFlattenerSpec.groovy @@ -6,97 +6,92 @@ import spock.lang.Specification /** * Created by Piszmog on 7/29/2018 */ -class MapFlattenerSpec extends Specification -{ +class MapFlattenerSpec extends Specification { // ============================================================ // Tests: // ============================================================ - def "flatten a simple map"() - { + def "flatten a simple map"() { given: "a map" Map topLevelMap = new HashMap() - topLevelMap.put( "key1", "value1" ) - topLevelMap.put( "key2", "value2" ) + topLevelMap.put("key1", "value1") + topLevelMap.put("key2", "value2") when: "the map is flattened" - def flattenedMap = MapFlattener.flatten( topLevelMap ) + def flattenedMap = MapFlattener.flatten(topLevelMap) then: "the map is flattened" - flattenedMap.get( "key1" ) == "value1" - flattenedMap.get( "key2" ) == "value2" + flattenedMap.get("key1") == "value1" + flattenedMap.get("key2") == "value2" } - def "flatten a nested map"() - { + def "flatten a nested map"() { given: "a map" Map level2Map = new HashMap() - level2Map.put( "level2Key1", "value1" ) - level2Map.put( "level2Key2", "value2" ) + level2Map.put("level2Key1", "value1") + level2Map.put("level2Key2", "value2") Map level1Map = new HashMap() - level1Map.put( "level2", level2Map ) + level1Map.put("level2", level2Map) Map topLevelMap = new HashMap() - topLevelMap.put( "level1", level1Map ) + topLevelMap.put("level1", level1Map) when: "the map is flattened" - def flattenMap = MapFlattener.flatten( topLevelMap ) + def flattenMap = MapFlattener.flatten(topLevelMap) then: "the map is flattened" - flattenMap.get( "level1.level2.level2Key1" ) == "value1" - flattenMap.get( "level1.level2.level2Key2" ) == "value2" + flattenMap.get("level1.level2.level2Key1") == "value1" + flattenMap.get("level1.level2.level2Key2") == "value2" } - def "flatten a nested map with a list"() - { + def "flatten a nested map with a list"() { given: "a map" List list = new LinkedList() - list.add( "value1" ) - list.add( "value2" ) + list.add("value1") + list.add("value2") Map level1Map = new HashMap() - level1Map.put( "list", list ) + level1Map.put("list", list) Map topLevelMap = new HashMap() - topLevelMap.put( "level1", level1Map ) + topLevelMap.put("level1", level1Map) when: "the map is flattened" - def flattenMap = MapFlattener.flatten( topLevelMap ) + def flattenMap = MapFlattener.flatten(topLevelMap) then: "the map is flattened" - flattenMap.get( "level1.list[0]" ) == "value1" - flattenMap.get( "level1.list[1]" ) == "value2" + flattenMap.get("level1.list[0]") == "value1" + flattenMap.get("level1.list[1]") == "value2" } - def "flatten a nested map with special characters"() - { + def "flatten a nested map with special characters"() { given: "a map" Map level2Map = new HashMap() - level2Map.put( "level2.Key1", "value1" ) - level2Map.put( "level2:Key2", "value2" ) - level2Map.put( "level2*Key3", "value3" ) - level2Map.put( "\$level2Key4", "value4" ) - level2Map.put( "#level2Key5", "value5" ) - level2Map.put( "%level2Key6", "value6" ) - level2Map.put( "!level2Key7", "value7" ) - level2Map.put( "^level2Key8", "value8" ) - level2Map.put( "@level2Key9", "value9" ) - level2Map.put( "&level2Key10", "value10" ) + level2Map.put("level2.Key1", "value1") + level2Map.put("level2:Key2", "value2") + level2Map.put("level2*Key3", "value3") + level2Map.put("\$level2Key4", "value4") + level2Map.put("#level2Key5", "value5") + level2Map.put("%level2Key6", "value6") + level2Map.put("!level2Key7", "value7") + level2Map.put("^level2Key8", "value8") + level2Map.put("@level2Key9", "value9") + level2Map.put("&level2Key10", "value10") Map level1Map = new HashMap() - level1Map.put( "level2", level2Map ) + level1Map.put("level2", level2Map) Map topLevelMap = new HashMap() - topLevelMap.put( "level1", level1Map ) + topLevelMap.put("level1", level1Map) when: "the map is flattened" - def flattenMap = MapFlattener.flatten( topLevelMap ) + def flattenMap = MapFlattener.flatten(topLevelMap) then: "the map is flattened" - flattenMap.get( "level1.level2.[level2.Key1]" ) == "value1" - flattenMap.get( "level1.level2.[level2:Key2]" ) == "value2" - flattenMap.get( "level1.level2.[level2*Key3]" ) == "value3" - flattenMap.get( "level1.level2.[\$level2Key4]" ) == "value4" - flattenMap.get( "level1.level2.[#level2Key5]" ) == "value5" - flattenMap.get( "level1.level2.%level2Key6" ) == "value6" - flattenMap.get( "level1.level2.!level2Key7" ) == "value7" - flattenMap.get( "level1.level2.^level2Key8" ) == "value8" - flattenMap.get( "level1.level2.@level2Key9" ) == "value9" - flattenMap.get( "level1.level2.&level2Key10" ) == "value10" + flattenMap.get("level1.level2.[level2.Key1]") == "value1" + flattenMap.get("level1.level2.[level2:Key2]") == "value2" + flattenMap.get("level1.level2.[level2*Key3]") == "value3" + flattenMap.get("level1.level2.[\$level2Key4]") == "value4" + flattenMap.get("level1.level2.[#level2Key5]") == "value5" + flattenMap.get("level1.level2.%level2Key6") == "value6" + flattenMap.get("level1.level2.!level2Key7") == "value7" + flattenMap.get("level1.level2.^level2Key8") == "value8" + flattenMap.get("level1.level2.@level2Key9") == "value9" + flattenMap.get("level1.level2.&level2Key10") == "value10" } }