|
| 1 | +package io.a2a.integrations.microprofile; |
| 2 | + |
| 3 | +import io.a2a.server.config.A2AConfigProvider; |
| 4 | +import io.quarkus.test.junit.QuarkusTest; |
| 5 | +import jakarta.inject.Inject; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import java.util.Optional; |
| 9 | + |
| 10 | +import static org.junit.jupiter.api.Assertions.*; |
| 11 | + |
| 12 | +/** |
| 13 | + * CDI-based test to verify that MicroProfileConfigProvider is properly selected |
| 14 | + * and works correctly with MicroProfile Config and fallback to defaults. |
| 15 | + */ |
| 16 | +@QuarkusTest |
| 17 | +public class MicroProfileConfigProviderTest { |
| 18 | + |
| 19 | + @Inject |
| 20 | + A2AConfigProvider configProvider; |
| 21 | + |
| 22 | + @Test |
| 23 | + public void testIsMicroProfileConfigProvider() { |
| 24 | + // Verify that when microprofile-config module is on classpath, |
| 25 | + // the injected A2AConfigProvider is the MicroProfile implementation |
| 26 | + assertInstanceOf(MicroProfileConfigProvider.class, configProvider, |
| 27 | + "A2AConfigProvider should be MicroProfileConfigProvider when module is present"); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void testGetValueFromMicroProfileConfig() { |
| 32 | + // Test that values from application.properties override defaults |
| 33 | + // The test application.properties sets a2a.executor.core-pool-size=15 |
| 34 | + String value = configProvider.getValue("a2a.executor.core-pool-size"); |
| 35 | + assertEquals("15", value, "Should get value from MicroProfile Config (application.properties)"); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testGetValueFallbackToDefaults() { |
| 40 | + // Test that values not in application.properties fall back to META-INF/a2a-defaults.properties |
| 41 | + // a2a.executor.max-pool-size is not in test application.properties, so should use default |
| 42 | + String value = configProvider.getValue("a2a.executor.max-pool-size"); |
| 43 | + assertEquals("50", value, "Should fall back to default value from META-INF/a2a-defaults.properties"); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testGetValueAnotherDefault() { |
| 48 | + // Test another default property to ensure fallback works |
| 49 | + String value = configProvider.getValue("a2a.executor.keep-alive-seconds"); |
| 50 | + assertEquals("60", value, "Should fall back to default value"); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testGetOptionalValueFromMicroProfileConfig() { |
| 55 | + // Test optional value that exists in application.properties |
| 56 | + Optional<String> value = configProvider.getOptionalValue("a2a.executor.core-pool-size"); |
| 57 | + assertTrue(value.isPresent(), "Optional value should be present"); |
| 58 | + assertEquals("15", value.get(), "Should get overridden value from MicroProfile Config"); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testGetOptionalValueFallbackToDefaults() { |
| 63 | + // Test optional value that falls back to defaults |
| 64 | + Optional<String> value = configProvider.getOptionalValue("a2a.executor.max-pool-size"); |
| 65 | + assertTrue(value.isPresent(), "Optional value should be present from defaults"); |
| 66 | + assertEquals("50", value.get(), "Should get default value"); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testGetOptionalValueNotFound() { |
| 71 | + // Test optional value that doesn't exist anywhere |
| 72 | + Optional<String> value = configProvider.getOptionalValue("non.existent.property"); |
| 73 | + assertFalse(value.isPresent(), "Optional value should be empty for non-existent property"); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testGetValueThrowsForNonExistent() { |
| 78 | + // Test that required getValue() throws for non-existent property |
| 79 | + assertThrows(IllegalArgumentException.class, |
| 80 | + () -> configProvider.getValue("non.existent.property"), |
| 81 | + "Should throw IllegalArgumentException for non-existent required property"); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testSystemPropertyOverride() { |
| 86 | + // System properties should have higher priority than application.properties |
| 87 | + // Set a system property and verify it's used |
| 88 | + String originalValue = System.getProperty("a2a.test.system.property"); |
| 89 | + try { |
| 90 | + System.setProperty("a2a.test.system.property", "from-system-property"); |
| 91 | + String value = configProvider.getValue("a2a.test.system.property"); |
| 92 | + assertEquals("from-system-property", value, |
| 93 | + "System property should override application.properties"); |
| 94 | + } finally { |
| 95 | + if (originalValue != null) { |
| 96 | + System.setProperty("a2a.test.system.property", originalValue); |
| 97 | + } else { |
| 98 | + System.clearProperty("a2a.test.system.property"); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments