Skip to content

Commit

Permalink
Add FieldResolverStrategies utility/factory class
Browse files Browse the repository at this point in the history
* Moved the existing implementations from TestHelpers
* Add a test (even though they are indirectly tested as well)

Closes #279
  • Loading branch information
sleberknight committed Apr 3, 2024
1 parent 2ab195d commit b5948f8
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 77 deletions.
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newEnvVarFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newExplicitValueFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newExternalPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newSupplierFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newSystemPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.SystemPropertyHelper.addSystemProperty;
import static org.kiwiproject.config.provider.util.SystemPropertyHelper.clearAllSystemProperties;
import static org.kiwiproject.config.provider.util.TestHelpers.newEnvVarFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newExplicitValueFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newExternalPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newSupplierFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newSystemPropertyFieldResolverStrategy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.kiwiproject.collect.KiwiMaps.newHashMap;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newEnvVarFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newExplicitValueFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newExternalPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newSupplierFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newSystemPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.SystemPropertyHelper.addSystemProperty;
import static org.kiwiproject.config.provider.util.SystemPropertyHelper.clearAllSystemProperties;
import static org.kiwiproject.config.provider.util.TestHelpers.newEnvVarFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newExplicitValueFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newExternalPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newSupplierFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newSystemPropertyFieldResolverStrategy;
import static org.kiwiproject.test.constants.KiwiTestConstants.JSON_HELPER;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newEnvVarFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newExplicitValueFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newExternalPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newSupplierFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newSystemPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.SystemPropertyHelper.addSystemProperty;
import static org.kiwiproject.config.provider.util.SystemPropertyHelper.clearAllSystemProperties;
import static org.kiwiproject.config.provider.util.TestHelpers.newEnvVarFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newExplicitValueFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newExternalPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newSupplierFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newSystemPropertyFieldResolverStrategy;
import static org.kiwiproject.test.constants.KiwiTestConstants.JSON_HELPER;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newEnvVarFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newExplicitValueFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newExternalPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newSupplierFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newSystemPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.SystemPropertyHelper.addSystemProperty;
import static org.kiwiproject.config.provider.util.SystemPropertyHelper.clearAllSystemProperties;
import static org.kiwiproject.config.provider.util.TestHelpers.newEnvVarFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newExplicitValueFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newExternalPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newSupplierFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newSystemPropertyFieldResolverStrategy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down
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")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newEnvVarFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newExplicitValueFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newExternalPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newSupplierFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newSystemPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.SystemPropertyHelper.addSystemProperty;
import static org.kiwiproject.config.provider.util.SystemPropertyHelper.clearAllSystemProperties;
import static org.kiwiproject.config.provider.util.TestHelpers.newEnvVarFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newExplicitValueFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newExternalPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newSupplierFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newSystemPropertyFieldResolverStrategy;
import static org.kiwiproject.test.constants.KiwiTestConstants.JSON_HELPER;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newEnvVarFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newExplicitValueFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newExternalPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newSupplierFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newSystemPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.SystemPropertyHelper.addSystemProperty;
import static org.kiwiproject.config.provider.util.SystemPropertyHelper.clearAllSystemProperties;
import static org.kiwiproject.config.provider.util.TestHelpers.newEnvVarFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newExplicitValueFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newExternalPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newSupplierFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newSystemPropertyFieldResolverStrategy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newEnvVarFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newExplicitValueFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newExternalPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newSupplierFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newSystemPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.SystemPropertyHelper.addSystemProperty;
import static org.kiwiproject.config.provider.util.SystemPropertyHelper.clearAllSystemProperties;
import static org.kiwiproject.config.provider.util.TestHelpers.newEnvVarFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newExplicitValueFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newExternalPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newSupplierFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newSystemPropertyFieldResolverStrategy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newEnvVarFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newExplicitValueFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newExternalPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newSupplierFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newSystemPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.SystemPropertyHelper.addSystemProperty;
import static org.kiwiproject.config.provider.util.SystemPropertyHelper.clearAllSystemProperties;
import static org.kiwiproject.config.provider.util.TestHelpers.newEnvVarFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newExplicitValueFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newExternalPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newSupplierFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newSystemPropertyFieldResolverStrategy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newEnvVarFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newExplicitValueFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newExternalPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newSupplierFieldResolverStrategy;
import static org.kiwiproject.config.provider.FieldResolverStrategies.newSystemPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.SystemPropertyHelper.addSystemProperty;
import static org.kiwiproject.config.provider.util.SystemPropertyHelper.clearAllSystemProperties;
import static org.kiwiproject.config.provider.util.TestHelpers.newEnvVarFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newExplicitValueFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newExternalPropertyFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newSupplierFieldResolverStrategy;
import static org.kiwiproject.config.provider.util.TestHelpers.newSystemPropertyFieldResolverStrategy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down
Loading

0 comments on commit b5948f8

Please sign in to comment.