|
| 1 | +/* |
| 2 | + * Copyright 2022-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.openfeign.aot; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.function.Consumer; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +import org.springframework.aot.generate.GeneratedClass; |
| 28 | +import org.springframework.aot.generate.GeneratedMethods; |
| 29 | +import org.springframework.aot.generate.GenerationContext; |
| 30 | +import org.springframework.aot.generate.MethodReference; |
| 31 | +import org.springframework.aot.hint.RuntimeHints; |
| 32 | +import org.springframework.aot.test.generate.TestGenerationContext; |
| 33 | +import org.springframework.beans.MutablePropertyValues; |
| 34 | +import org.springframework.beans.PropertyValue; |
| 35 | +import org.springframework.beans.PropertyValues; |
| 36 | +import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution; |
| 37 | +import org.springframework.beans.factory.aot.BeanFactoryInitializationCode; |
| 38 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 39 | +import org.springframework.beans.factory.config.ConstructorArgumentValues; |
| 40 | +import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
| 41 | +import org.springframework.beans.factory.support.GenericBeanDefinition; |
| 42 | +import org.springframework.beans.factory.support.RootBeanDefinition; |
| 43 | +import org.springframework.cloud.openfeign.FeignClient; |
| 44 | +import org.springframework.cloud.openfeign.FeignClientFactory; |
| 45 | +import org.springframework.cloud.openfeign.FeignClientSpecification; |
| 46 | +import org.springframework.context.support.GenericApplicationContext; |
| 47 | +import org.springframework.javapoet.TypeSpec; |
| 48 | +import org.springframework.web.bind.annotation.PostMapping; |
| 49 | + |
| 50 | +import static org.assertj.core.api.Assertions.assertThat; |
| 51 | +import static org.mockito.Mockito.mock; |
| 52 | +import static org.mockito.Mockito.when; |
| 53 | +import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.proxies; |
| 54 | +import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.reflection; |
| 55 | + |
| 56 | +/** |
| 57 | + * Tests for {@link FeignClientBeanFactoryInitializationAotProcessor}. |
| 58 | + * |
| 59 | + * @author Olga Maciaszek-Sharma |
| 60 | + */ |
| 61 | +class FeignClientBeanFactoryInitializationAotProcessorTests { |
| 62 | + |
| 63 | + private static final String BEAN_DEFINITION_CLASS_NAME = "org.springframework.cloud.openfeign.aot.FeignClientBeanFactoryInitializationAotProcessorTests$TestClient"; |
| 64 | + |
| 65 | + private final TestGenerationContext generationContext = new TestGenerationContext(); |
| 66 | + |
| 67 | + private final FeignClientFactory feignClientFactory = mock(FeignClientFactory.class); |
| 68 | + |
| 69 | + private final BeanFactoryInitializationCode beanFactoryInitializationCode = new MockBeanFactoryInitializationCode( |
| 70 | + generationContext); |
| 71 | + |
| 72 | + @BeforeEach |
| 73 | + void setUp() { |
| 74 | + Map<String, FeignClientSpecification> configurations = Map.of("test", |
| 75 | + new FeignClientSpecification("test", TestClient.class.getCanonicalName(), new Class<?>[] {})); |
| 76 | + when(feignClientFactory.getConfigurations()).thenReturn(configurations); |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void shouldGenerateBeanInitializationCodeAndRegisterHints() { |
| 82 | + DefaultListableBeanFactory beanFactory = beanFactory(); |
| 83 | + GenericApplicationContext context = new GenericApplicationContext(beanFactory); |
| 84 | + FeignClientBeanFactoryInitializationAotProcessor processor = new FeignClientBeanFactoryInitializationAotProcessor( |
| 85 | + context, feignClientFactory); |
| 86 | + |
| 87 | + BeanFactoryInitializationAotContribution contribution = processor.processAheadOfTime(beanFactory); |
| 88 | + assertThat(contribution).isNotNull(); |
| 89 | + |
| 90 | + verifyContribution((FeignClientBeanFactoryInitializationAotProcessor.AotContribution) contribution); |
| 91 | + contribution.applyTo(generationContext, beanFactoryInitializationCode); |
| 92 | + |
| 93 | + RuntimeHints hints = generationContext.getRuntimeHints(); |
| 94 | + assertThat(reflection().onType(TestReturnType.class)).accepts(hints); |
| 95 | + assertThat(reflection().onType(TestArgType.class)).accepts(hints); |
| 96 | + assertThat(proxies().forInterfaces(TestClient.class)).accepts(hints); |
| 97 | + } |
| 98 | + |
| 99 | + private static void verifyContribution( |
| 100 | + FeignClientBeanFactoryInitializationAotProcessor.AotContribution contribution) { |
| 101 | + BeanDefinition contributionBeanDefinition = contribution.getFeignClientBeanDefinitions() |
| 102 | + .get(TestClient.class.getCanonicalName()); |
| 103 | + assertThat(contributionBeanDefinition.getBeanClassName()).isEqualTo(BEAN_DEFINITION_CLASS_NAME); |
| 104 | + PropertyValues propertyValues = contributionBeanDefinition.getPropertyValues(); |
| 105 | + assertThat(propertyValues).isNotEmpty(); |
| 106 | + assertThat(((String[]) propertyValues.getPropertyValue("qualifiers").getValue())).containsExactly("test"); |
| 107 | + assertThat(propertyValues.getPropertyValue("type").getValue()).isEqualTo(TestClient.class.getCanonicalName()); |
| 108 | + assertThat(propertyValues.getPropertyValue("fallback").getValue()).isEqualTo(String.class); |
| 109 | + } |
| 110 | + |
| 111 | + private DefaultListableBeanFactory beanFactory() { |
| 112 | + GenericBeanDefinition beanDefinition = new GenericBeanDefinition(new RootBeanDefinition(TestClient.class, |
| 113 | + new ConstructorArgumentValues(), |
| 114 | + new MutablePropertyValues(List.of(new PropertyValue("type", TestClient.class.getCanonicalName()), |
| 115 | + new PropertyValue("qualifiers", new String[] { "test" }), |
| 116 | + new PropertyValue("fallback", String.class), |
| 117 | + new PropertyValue("fallbackFactory", Void.class))))); |
| 118 | + DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); |
| 119 | + beanFactory.registerBeanDefinition(TestClient.class.getCanonicalName(), beanDefinition); |
| 120 | + return beanFactory; |
| 121 | + } |
| 122 | + |
| 123 | + @FeignClient |
| 124 | + interface TestClient { |
| 125 | + |
| 126 | + @PostMapping("/test") |
| 127 | + TestReturnType test(TestArgType arg); |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | + @SuppressWarnings("NullableProblems") |
| 132 | + static class MockBeanFactoryInitializationCode implements BeanFactoryInitializationCode { |
| 133 | + |
| 134 | + private static final Consumer<TypeSpec.Builder> emptyTypeCustomizer = type -> { |
| 135 | + }; |
| 136 | + |
| 137 | + private final GeneratedClass generatedClass; |
| 138 | + |
| 139 | + MockBeanFactoryInitializationCode(GenerationContext generationContext) { |
| 140 | + generatedClass = generationContext.getGeneratedClasses().addForFeature("Test", emptyTypeCustomizer); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public GeneratedMethods getMethods() { |
| 145 | + return generatedClass.getMethods(); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public void addInitializer(MethodReference methodReference) { |
| 150 | + new ArrayList<>(); |
| 151 | + } |
| 152 | + |
| 153 | + } |
| 154 | + |
| 155 | + static class TestReturnType { |
| 156 | + |
| 157 | + private String test; |
| 158 | + |
| 159 | + TestReturnType(String test) { |
| 160 | + this.test = test; |
| 161 | + } |
| 162 | + |
| 163 | + TestReturnType() { |
| 164 | + } |
| 165 | + |
| 166 | + String getTest() { |
| 167 | + return test; |
| 168 | + } |
| 169 | + |
| 170 | + void setTest(String test) { |
| 171 | + this.test = test; |
| 172 | + } |
| 173 | + |
| 174 | + } |
| 175 | + |
| 176 | + static class TestArgType { |
| 177 | + |
| 178 | + private String test; |
| 179 | + |
| 180 | + TestArgType(String test) { |
| 181 | + this.test = test; |
| 182 | + } |
| 183 | + |
| 184 | + TestArgType() { |
| 185 | + } |
| 186 | + |
| 187 | + String getTest() { |
| 188 | + return test; |
| 189 | + } |
| 190 | + |
| 191 | + void setTest(String test) { |
| 192 | + this.test = test; |
| 193 | + } |
| 194 | + |
| 195 | + } |
| 196 | + |
| 197 | +} |
0 commit comments