Skip to content

Instantly share code, notes, and snippets.

@TharindaW
Last active September 10, 2023 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TharindaW/501bf99a3f977ab1dc991b546b80164e to your computer and use it in GitHub Desktop.
Save TharindaW/501bf99a3f977ab1dc991b546b80164e to your computer and use it in GitHub Desktop.
ApplicationContextRunnerTesting
public enum AppHubFeature {
LOGISTIC_UNIT_UPDATE("Logistic Unit Update", FeatureConfig.LOGISTIC_UNIT_UPDATE),
ORDER_STATUS_UPDATE("Order Status Update", FeatureConfig.ORDER_STATUS_UPDATE),
STOCK_ADJUSTMENT("Stock Adjustment", FeatureConfig.STOCK_ADJUSTMENT);
public static class FeatureConfig {
public static final String LOGISTIC_UNIT_UPDATE = "app-hub.feature.logistic-unit-update";
public static final String ORDER_STATUS_UPDATE = "app-hub.feature.order-line-status-update";
public static final String STOCK_ADJUSTMENT = "app-hub.feature.stock-adjustment";
}
public final String feature;
public final String config;
AppHubFeature(String feature, String config) {
this.feature = feature;
this.config = config;
}
public String config() {
return this.config;
}
public String feature() {
return this.feature;
}
}
Feature: App Integration features should be able to toggle on and off
Scenario Outline: Logistic Unit Update Feature Toggle
Given application functions with feature 'Logistic Unit Update'
When the above feature toggle via <config> with <value>
Then the feature 'Logistic Unit Update' should be <status>
Examples:
| config | value | status |
| 'logistic-unit-update' | 'true' | 'ON' |
| 'logistic-unit-update' | 'false' | 'OFF' |
| 'logistic-unit-update' | '' | 'ON' |
| 'logistic-unit-update' | 'x' | 'ON' |
| 'wrong-config-name' | 'x' | 'ON' |
Scenario Outline: Order Status Update Feature Toggle
Given application functions with feature 'Order Status Update'
When the above feature toggle via <config> with <value>
Then the feature 'Order Status Update' should be <status>
Examples:
| config | value | status |
| 'order-line-status-update' | 'true' | 'ON' |
| 'order-line-status-update' | 'false' | 'OFF' |
| 'order-line-status-update' | '' | 'ON' |
| 'order-line-status-update' | 'x' | 'ON' |
| 'wrong-config-name' | 'x' | 'ON' |
Scenario Outline: Stock Adjustment Feature Toggle
Given application functions with feature 'Stock Adjustment'
When the above feature toggle via <config> with <value>
Then the feature 'Stock Adjustment' should be <status>
Examples:
| config | value | status |
| 'stock-adjustment' | 'true' | 'ON' |
| 'stock-adjustment' | 'false' | 'OFF' |
| 'stock-adjustment' | '' | 'ON' |
| 'stock-adjustment' | 'x' | 'ON' |
| 'wrong-config-name' | 'x' | 'ON' |
@Component
@ConditionalOnExpression("!'false'.equalsIgnoreCase('${" + ORDER_STATUS_UPDATE + ":true}')")
public class OrderStatusUpdateMessageHandler{}
@Component
@ConditionalOnExpression("!'false'.equalsIgnoreCase('${" + LOGISTIC_UNIT_UPDATE + ":true}')")
public class LogisticUnitUpdateMessageHandler{}
@Component
@ConditionalOnExpression("!'false'.equalsIgnoreCase('${" + STOCK_ADJUSTMENT + ":true}')")
public class StockAdjustmentWMSMessageHandler{}
class FeatureToggleSteps {
ApplicationContextRunner contextRunner
def featureMap = [
(SyncHubFeature.LOGISTIC_UNIT_UPDATE.feature()) : LogisticUnitUpdateMessageHandler,
(SyncHubFeature.ORDER_STATUS_UPDATE.feature()) : OrderStatusUpdateMessageHandler,
(SyncHubFeature.STOCK_ADJUSTMENT.feature()) : StockAdjustmentWMSMessageHandler,
]
@TestConfiguration
static class TestConfig {
@Bean
CommandServiceBean commandServiceBean() {
return Mockito.mock(CommandServiceBean.class)
}
}
@Given("application functions with feature {string}")
void applicationFunctionsWithFeature(String feature) {
contextRunner = new ApplicationContextRunner()
.withUserConfiguration(TestConfig.class, featureMap[feature] as Class<?>)
}
@When("the above feature toggle via {string} with {string}")
void theAboveFeatureToggleViaConfigWithValue(String config, String value) {
if (value != "") {
contextRunner = contextRunner.withPropertyValues("sync-hub.feature." + config + "=" + value)
}
}
@When("the above feature toggle via following configs and values")
void theAboveFeatureToggleViaFollowingConfigsAndValues(DataTable dataTable) {
for (i in 0..<dataTable.height()) {
def config = dataTable.row(i).get(0)
def value = dataTable.row(i).get(1)
if (value != null) {
contextRunner = contextRunner.withPropertyValues("sync-hub.feature." + config + "=" + value)
}
}
}
@Then("the feature {string} should be {string}")
void featuresShouldBe(String featureName, String status) {
contextRunner
.run({ context ->
if (status == "ON") {
assertThat(context).hasSingleBean(featureMap[featureName] as Class<?>)
}
if (status == "OFF") {
assertThat(context).doesNotHaveBean(featureMap[featureName] as Class<?>)
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment