|
| 1 | +package com.kinde.spring; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.springframework.core.io.ClassPathResource; |
| 5 | + |
| 6 | +import java.io.BufferedReader; |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InputStreamReader; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.util.List; |
| 11 | +import java.util.stream.Collectors; |
| 12 | + |
| 13 | +import static org.assertj.core.api.Assertions.assertThat; |
| 14 | + |
| 15 | +/** |
| 16 | + * Tests for KindeSdkClientAutoConfig. |
| 17 | + * |
| 18 | + * This test class covers: |
| 19 | + * 1. Verification that KindeSdkClientAutoConfig is properly registered in the imports file |
| 20 | + * 2. Verification that the auto-configuration is available for Spring Boot to load |
| 21 | + * 3. Verification that the auto-configuration is properly ordered |
| 22 | + */ |
| 23 | +public class KindeSdkClientAutoConfigTest { |
| 24 | + |
| 25 | + /** |
| 26 | + * Helper method to read the auto-configuration imports file. |
| 27 | + * |
| 28 | + * @return List of lines from the auto-configuration imports file |
| 29 | + * @throws IOException if the file cannot be read |
| 30 | + */ |
| 31 | + private List<String> readAutoConfigurationImports() throws IOException { |
| 32 | + ClassPathResource resource = new ClassPathResource("META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports"); |
| 33 | + assertThat(resource.exists()).isTrue(); |
| 34 | + |
| 35 | + try (var inputStream = resource.getInputStream(); |
| 36 | + var reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) { |
| 37 | + return reader.lines().collect(Collectors.toList()); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testKindeSdkClientAutoConfigIsRegisteredInImportsFile() throws IOException { |
| 43 | + // Test that KindeSdkClientAutoConfig is properly registered in the auto-configuration imports file |
| 44 | + List<String> lines = readAutoConfigurationImports(); |
| 45 | + |
| 46 | + // Verify that KindeSdkClientAutoConfig is registered |
| 47 | + assertThat(lines).contains("com.kinde.spring.KindeSdkClientAutoConfig"); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testKindeSdkClientAutoConfigIsRegisteredBeforeKindeOAuth2AutoConfig() throws IOException { |
| 52 | + // Test that KindeSdkClientAutoConfig is registered before KindeOAuth2AutoConfig |
| 53 | + // This ensures proper ordering so that KindeSdkClient is available for KindeOAuth2AutoConfig |
| 54 | + List<String> lines = readAutoConfigurationImports(); |
| 55 | + |
| 56 | + int kindeSdkClientAutoConfigIndex = lines.indexOf("com.kinde.spring.KindeSdkClientAutoConfig"); |
| 57 | + int kindeOAuth2AutoConfigIndex = lines.indexOf("com.kinde.spring.KindeOAuth2AutoConfig"); |
| 58 | + |
| 59 | + // Verify that KindeSdkClientAutoConfig comes before KindeOAuth2AutoConfig |
| 60 | + assertThat(kindeSdkClientAutoConfigIndex).isGreaterThan(-1); |
| 61 | + assertThat(kindeOAuth2AutoConfigIndex).isGreaterThan(-1); |
| 62 | + assertThat(kindeSdkClientAutoConfigIndex).isLessThan(kindeOAuth2AutoConfigIndex); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testAutoConfigurationImportsFileExists() throws IOException { |
| 67 | + // Test that the auto-configuration imports file exists and contains the necessary configuration |
| 68 | + List<String> lines = readAutoConfigurationImports(); |
| 69 | + assertThat(lines).isNotEmpty(); |
| 70 | + |
| 71 | + // Verify that KindeSdkClientAutoConfig is in the list |
| 72 | + assertThat(lines).contains("com.kinde.spring.KindeSdkClientAutoConfig"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testKindeSdkClientAutoConfigClassExists() { |
| 77 | + // Test that the KindeSdkClientAutoConfig class exists and can be loaded |
| 78 | + try { |
| 79 | + Class<?> autoConfigClass = Class.forName("com.kinde.spring.KindeSdkClientAutoConfig"); |
| 80 | + assertThat(autoConfigClass).isNotNull(); |
| 81 | + assertThat(autoConfigClass.getSimpleName()).isEqualTo("KindeSdkClientAutoConfig"); |
| 82 | + } catch (ClassNotFoundException e) { |
| 83 | + throw new AssertionError("KindeSdkClientAutoConfig class not found", e); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testKindeSdkClientAutoConfigHasCorrectAnnotations() { |
| 89 | + // Test that KindeSdkClientAutoConfig has the correct annotations |
| 90 | + try { |
| 91 | + Class<?> autoConfigClass = Class.forName("com.kinde.spring.KindeSdkClientAutoConfig"); |
| 92 | + |
| 93 | + // Verify that it has @AutoConfiguration annotation |
| 94 | + assertThat(autoConfigClass.isAnnotationPresent(org.springframework.boot.autoconfigure.AutoConfiguration.class)) |
| 95 | + .isTrue(); |
| 96 | + |
| 97 | + // Verify that it has @ConditionalOnKindeClientProperties annotation |
| 98 | + assertThat(autoConfigClass.isAnnotationPresent(com.kinde.spring.ConditionalOnKindeClientProperties.class)) |
| 99 | + .isTrue(); |
| 100 | + |
| 101 | + // Verify that it has @EnableConfigurationProperties annotation |
| 102 | + assertThat(autoConfigClass.isAnnotationPresent(org.springframework.boot.context.properties.EnableConfigurationProperties.class)) |
| 103 | + .isTrue(); |
| 104 | + |
| 105 | + } catch (ClassNotFoundException e) { |
| 106 | + throw new AssertionError("KindeSdkClientAutoConfig class not found", e); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void testKindeSdkClientAutoConfigHasKindeSdkClientBean() { |
| 112 | + // Test that KindeSdkClientAutoConfig has a method that creates KindeSdkClient bean |
| 113 | + try { |
| 114 | + Class<?> autoConfigClass = Class.forName("com.kinde.spring.KindeSdkClientAutoConfig"); |
| 115 | + |
| 116 | + // Look for a method that returns KindeSdkClient |
| 117 | + java.lang.reflect.Method[] methods = autoConfigClass.getDeclaredMethods(); |
| 118 | + boolean hasKindeSdkClientMethod = false; |
| 119 | + |
| 120 | + for (java.lang.reflect.Method method : methods) { |
| 121 | + if (method.getReturnType().getSimpleName().equals("KindeSdkClient")) { |
| 122 | + hasKindeSdkClientMethod = true; |
| 123 | + |
| 124 | + // Verify that it has @Bean annotation |
| 125 | + assertThat(method.isAnnotationPresent(org.springframework.context.annotation.Bean.class)) |
| 126 | + .isTrue(); |
| 127 | + |
| 128 | + // Verify that it has @ConditionalOnMissingBean annotation |
| 129 | + assertThat(method.isAnnotationPresent(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean.class)) |
| 130 | + .isTrue(); |
| 131 | + |
| 132 | + break; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + assertThat(hasKindeSdkClientMethod).isTrue(); |
| 137 | + |
| 138 | + } catch (ClassNotFoundException e) { |
| 139 | + throw new AssertionError("KindeSdkClientAutoConfig class not found", e); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void testCustomerProblemIsResolved() throws IOException { |
| 145 | + // Test that demonstrates the customer's problem has been resolved |
| 146 | + // by showing that the auto-configuration is properly registered |
| 147 | + |
| 148 | + List<String> lines = readAutoConfigurationImports(); |
| 149 | + |
| 150 | + // Before our fix: KindeSdkClient would not be available because: |
| 151 | + // 1. It was only annotated with @Component |
| 152 | + // 2. Spring doesn't scan 3rd party packages for components |
| 153 | + // 3. KindeOAuth2AutoConfig would fail to autowire it |
| 154 | + // 4. Application would fail to start |
| 155 | + |
| 156 | + // After our fix: KindeSdkClientAutoConfig is registered and will be loaded |
| 157 | + // by Spring Boot's auto-configuration system |
| 158 | + assertThat(lines).contains("com.kinde.spring.KindeSdkClientAutoConfig"); |
| 159 | + |
| 160 | + // This means that when a Spring Boot application starts with Kinde properties, |
| 161 | + // KindeSdkClient will be automatically configured and available for KindeOAuth2AutoConfig |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void testNoManualConfigurationRequired() throws IOException { |
| 166 | + // Test that verifies no manual configuration is required |
| 167 | + // The customer mentioned they would need to add: |
| 168 | + // @SpringBootApplication(scanBasePackageClasses = [KindeSdkClient::class]) |
| 169 | + // This test proves that's not necessary anymore |
| 170 | + |
| 171 | + List<String> lines = readAutoConfigurationImports(); |
| 172 | + |
| 173 | + // The fact that KindeSdkClientAutoConfig is registered means that |
| 174 | + // no manual configuration like component scanning is required |
| 175 | + assertThat(lines).contains("com.kinde.spring.KindeSdkClientAutoConfig"); |
| 176 | + } |
| 177 | +} |
0 commit comments