|
| 1 | +/* |
| 2 | + * Copyright 2026-2026 the original author or authors. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.modelcontextprotocol.conformance.client.condition; |
| 6 | + |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +import org.jspecify.annotations.Nullable; |
| 12 | + |
| 13 | +import org.springframework.boot.autoconfigure.condition.ConditionMessage; |
| 14 | +import org.springframework.boot.autoconfigure.condition.ConditionOutcome; |
| 15 | +import org.springframework.boot.autoconfigure.condition.SpringBootCondition; |
| 16 | +import org.springframework.context.annotation.ConditionContext; |
| 17 | +import org.springframework.core.type.AnnotatedTypeMetadata; |
| 18 | +import org.springframework.util.Assert; |
| 19 | + |
| 20 | +/** |
| 21 | + * Condition implementation for {@link ConditionalOnScenario}. |
| 22 | + * |
| 23 | + * @author Daniel Garnier-Moiroux |
| 24 | + */ |
| 25 | +class OnScenarioCondition extends SpringBootCondition { |
| 26 | + |
| 27 | + private static final String ENV_VAR = "MCP_CONFORMANCE_SCENARIO"; |
| 28 | + |
| 29 | + @Override |
| 30 | + public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { |
| 31 | + Map<String, @Nullable Object> attributes = metadata |
| 32 | + .getAnnotationAttributes(ConditionalOnScenario.class.getName()); |
| 33 | + Assert.state(attributes != null, "'attributes' must not be null"); |
| 34 | + |
| 35 | + String[] included = (String[]) attributes.get("included"); |
| 36 | + String[] excluded = (String[]) attributes.get("excluded"); |
| 37 | + |
| 38 | + boolean hasIncluded = included != null && included.length > 0; |
| 39 | + boolean hasExcluded = excluded != null && excluded.length > 0; |
| 40 | + |
| 41 | + Assert.state(hasIncluded ^ hasExcluded, |
| 42 | + "@ConditionalOnScenario must have exactly one of 'included' or 'excluded' defined"); |
| 43 | + |
| 44 | + String scenario = System.getenv(ENV_VAR); |
| 45 | + |
| 46 | + if (hasIncluded) { |
| 47 | + List<String> includedList = Arrays.asList(included); |
| 48 | + boolean matches = scenario != null && includedList.contains(scenario); |
| 49 | + ConditionMessage message = ConditionMessage.forCondition(ConditionalOnScenario.class) |
| 50 | + .because("scenario '" + scenario + "' " + (matches ? "is" : "is not") + " in included list " |
| 51 | + + includedList); |
| 52 | + return matches ? ConditionOutcome.match(message) : ConditionOutcome.noMatch(message); |
| 53 | + } |
| 54 | + else { |
| 55 | + List<String> excludedList = Arrays.asList(excluded); |
| 56 | + boolean matches = scenario == null || !excludedList.contains(scenario); |
| 57 | + ConditionMessage message = ConditionMessage.forCondition(ConditionalOnScenario.class) |
| 58 | + .because("scenario '" + scenario + "' " + (matches ? "is not" : "is") + " in excluded list " |
| 59 | + + excludedList); |
| 60 | + return matches ? ConditionOutcome.match(message) : ConditionOutcome.noMatch(message); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | +} |
0 commit comments