-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FieldResolverStrategies utility/factory class
* Moved the existing implementations from TestHelpers * Add a test (even though they are indirectly tested as well) Closes #279
- Loading branch information
1 parent
2ab195d
commit b5948f8
Showing
14 changed files
with
201 additions
and
77 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
src/main/java/org/kiwiproject/config/provider/FieldResolverStrategies.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.kiwiproject.config.provider; | ||
|
||
import lombok.experimental.UtilityClass; | ||
|
||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Factory class to easily create @link FieldResolverStrategy} instances | ||
* for specific use cases, for example when you only want a resolver | ||
* that uses a single resolution mechanism. | ||
*/ | ||
@UtilityClass | ||
public class FieldResolverStrategies { | ||
|
||
/** | ||
* Create a strategy that only resolves an environment variable. | ||
* | ||
* @param <T> the type to resolve | ||
* @param envVariable the name of the environment variable to resolve | ||
* @return a new instance | ||
*/ | ||
public static <T> FieldResolverStrategy<T> newEnvVarFieldResolverStrategy(String envVariable) { | ||
return FieldResolverStrategy.<T>builder().envVariable(envVariable).build(); | ||
} | ||
|
||
/** | ||
* Create a strategy that only resolves an explicit value. | ||
* | ||
* @param <T> the type to resolve | ||
* @param explicitValue the explicit value to resolve | ||
* @return a new instance | ||
*/ | ||
public static <T> FieldResolverStrategy<T> newExplicitValueFieldResolverStrategy(T explicitValue) { | ||
return FieldResolverStrategy.<T>builder().explicitValue(explicitValue).build(); | ||
} | ||
|
||
/** | ||
* Create a strategy that only resolves an external property. | ||
* | ||
* @param <T> the type to resolve | ||
* @param externalProperty the name of the external property to resolve | ||
* @return a new instance | ||
*/ | ||
public static <T> FieldResolverStrategy<T> newExternalPropertyFieldResolverStrategy(String externalProperty) { | ||
return FieldResolverStrategy.<T>builder().externalProperty(externalProperty).build(); | ||
} | ||
|
||
/** | ||
* Create a strategy that only resolves a system property. | ||
* | ||
* @param <T> the type to resolve | ||
* @param systemPropertyKey the name of the system property to resolve | ||
* @return a new instance | ||
*/ | ||
public static <T> FieldResolverStrategy<T> newSystemPropertyFieldResolverStrategy(String systemPropertyKey) { | ||
return FieldResolverStrategy.<T>builder().systemPropertyKey(systemPropertyKey).build(); | ||
} | ||
|
||
/** | ||
* Create a strategy that only resolves a Supplier. | ||
* | ||
* @param <T> the type to resolve | ||
* @param supplier the Supplier which will resolve the value | ||
* @return a new instance | ||
*/ | ||
public static <T> FieldResolverStrategy<T> newSupplierFieldResolverStrategy(Supplier<T> supplier) { | ||
return FieldResolverStrategy.<T>builder().valueSupplier(supplier).build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/test/java/org/kiwiproject/config/provider/FieldResolverStrategiesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package org.kiwiproject.config.provider; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.function.Supplier; | ||
|
||
@DisplayName("FieldResolverStrategies") | ||
class FieldResolverStrategiesTest { | ||
|
||
@Test | ||
void shouldCreateEnvVarFieldResolverStrateg() { | ||
var strategy = FieldResolverStrategies.newEnvVarFieldResolverStrategy("KIWI_USERNAME"); | ||
|
||
assertAll( | ||
() -> assertThat(strategy.getEnvVariableOrDefault(null)).isEqualTo("KIWI_USERNAME"), | ||
() -> assertThat(strategy.getExplicitValue()).isNull(), | ||
() -> assertThat(strategy.getExternalPropertyOrDefault(null)).isNull(), | ||
() -> assertThat(strategy.getSystemPropertyKeyOrDefault(null)).isNull(), | ||
() -> assertThat(strategy.getValueSupplier()).isNull() | ||
); | ||
} | ||
|
||
@Test | ||
void shouldCreateExplicitValueFieldResolverStrategy() { | ||
var strategy = FieldResolverStrategies.newExplicitValueFieldResolverStrategy("alice"); | ||
|
||
assertAll( | ||
() -> assertThat(strategy.getEnvVariableOrDefault(null)).isNull(), | ||
() -> assertThat(strategy.getExplicitValue()).isEqualTo("alice"), | ||
() -> assertThat(strategy.getExternalPropertyOrDefault(null)).isNull(), | ||
() -> assertThat(strategy.getSystemPropertyKeyOrDefault(null)).isNull(), | ||
() -> assertThat(strategy.getValueSupplier()).isNull() | ||
); | ||
} | ||
|
||
@Test | ||
void shouldCreateExternalPropertyFieldResolverStrategy() { | ||
var strategy = FieldResolverStrategies.newExternalPropertyFieldResolverStrategy("username"); | ||
|
||
assertAll( | ||
() -> assertThat(strategy.getEnvVariableOrDefault(null)).isNull(), | ||
() -> assertThat(strategy.getExplicitValue()).isNull(), | ||
() -> assertThat(strategy.getExternalPropertyOrDefault(null)).isEqualTo("username"), | ||
() -> assertThat(strategy.getSystemPropertyKeyOrDefault(null)).isNull(), | ||
() -> assertThat(strategy.getValueSupplier()).isNull() | ||
); | ||
} | ||
|
||
@Test | ||
void shouldCreateSystemPropertyFieldResolverStrategy() { | ||
var strategy = FieldResolverStrategies.newSystemPropertyFieldResolverStrategy("kiwi.username"); | ||
|
||
assertAll( | ||
() -> assertThat(strategy.getEnvVariableOrDefault(null)).isNull(), | ||
() -> assertThat(strategy.getExplicitValue()).isNull(), | ||
() -> assertThat(strategy.getExternalPropertyOrDefault(null)).isNull(), | ||
() -> assertThat(strategy.getSystemPropertyKeyOrDefault(null)).isEqualTo("kiwi.username"), | ||
() -> assertThat(strategy.getValueSupplier()).isNull() | ||
); | ||
} | ||
|
||
@Test | ||
void shouldCreateSupplierFieldResolverStrategy() { | ||
var strategy = FieldResolverStrategies.newSupplierFieldResolverStrategy(() -> "diane"); | ||
|
||
assertAll( | ||
() -> assertThat(strategy.getEnvVariableOrDefault(null)).isNull(), | ||
() -> assertThat(strategy.getExplicitValue()).isNull(), | ||
() -> assertThat(strategy.getExternalPropertyOrDefault(null)).isNull(), | ||
() -> assertThat(strategy.getSystemPropertyKeyOrDefault(null)).isNull(), | ||
() -> assertThat(strategy.getValueSupplier()).extracting(Supplier::get).isEqualTo("diane") | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.