|
| 1 | +/* |
| 2 | + * Copyright 2023 - 2024 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 | +package org.springframework.ai.model.function; |
| 17 | + |
| 18 | +import java.lang.reflect.Type; |
| 19 | +import java.util.function.Function; |
| 20 | + |
| 21 | +import org.junit.jupiter.params.ParameterizedTest; |
| 22 | +import org.junit.jupiter.params.provider.ValueSource; |
| 23 | + |
| 24 | +import org.springframework.beans.factory.annotation.Autowired; |
| 25 | +import org.springframework.boot.SpringBootConfiguration; |
| 26 | +import org.springframework.boot.test.context.SpringBootTest; |
| 27 | +import org.springframework.cloud.function.context.config.FunctionContextUtils; |
| 28 | +import org.springframework.context.annotation.Bean; |
| 29 | +import org.springframework.context.support.GenericApplicationContext; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | + |
| 33 | +@SpringBootTest |
| 34 | +class TypeResolverHelperIT { |
| 35 | + |
| 36 | + @Autowired |
| 37 | + GenericApplicationContext applicationContext; |
| 38 | + |
| 39 | + @ParameterizedTest(name = "{0} : {displayName} ") |
| 40 | + @ValueSource(strings = { "weatherClassDefinition", "weatherFunctionDefinition", "standaloneWeatherFunction" }) |
| 41 | + void beanInputTypeResolutionTest(String beanName) { |
| 42 | + assertThat(applicationContext).isNotNull(); |
| 43 | + Type beanType = FunctionContextUtils.findType(applicationContext.getBeanFactory(), beanName); |
| 44 | + assertThat(beanType).isNotNull(); |
| 45 | + Type functionInputType = TypeResolverHelper.getFunctionArgumentType(beanType, 0); |
| 46 | + assertThat(functionInputType).isNotNull(); |
| 47 | + assertThat(functionInputType.getTypeName()).isEqualTo(WeatherRequest.class.getName()); |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + public record WeatherRequest(String city) { |
| 52 | + } |
| 53 | + |
| 54 | + public record WeatherResponse(float temperatureInCelsius) { |
| 55 | + } |
| 56 | + |
| 57 | + public static class Outer { |
| 58 | + |
| 59 | + public static class InnerWeatherFunction implements Function<WeatherRequest, WeatherResponse> { |
| 60 | + |
| 61 | + @Override |
| 62 | + public WeatherResponse apply(WeatherRequest weatherRequest) { |
| 63 | + return new WeatherResponse(42.0f); |
| 64 | + } |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + @SpringBootConfiguration |
| 71 | + public static class TypeResolverHelperConfiguration { |
| 72 | + |
| 73 | + @Bean() |
| 74 | + Outer.InnerWeatherFunction weatherClassDefinition() { |
| 75 | + return new Outer.InnerWeatherFunction(); |
| 76 | + } |
| 77 | + |
| 78 | + @Bean() |
| 79 | + Function<WeatherRequest, WeatherResponse> weatherFunctionDefinition() { |
| 80 | + return new Outer.InnerWeatherFunction(); |
| 81 | + } |
| 82 | + |
| 83 | + @Bean() |
| 84 | + StandaloneWeatherFunction standaloneWeatherFunction() { |
| 85 | + return new StandaloneWeatherFunction(); |
| 86 | + } |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments