|
| 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 | + |
| 17 | +package org.springframework.ai.utils; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Objects; |
| 21 | +import java.util.stream.Stream; |
| 22 | + |
| 23 | +import org.springframework.boot.autoconfigure.AutoConfiguration; |
| 24 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 25 | +import org.springframework.core.annotation.AnnotationUtils; |
| 26 | + |
| 27 | +/** |
| 28 | + * Utility class that creates {@link AutoConfigurations} for testing purpose. |
| 29 | + * <p> |
| 30 | + * This class processes the provided configuration classes, checks for the presence of |
| 31 | + * {@link AutoConfiguration} annotations, and builds an {@link AutoConfigurations} |
| 32 | + * instance that includes both the original classes and those declared in the |
| 33 | + * {@link AutoConfiguration#after()} attribute. |
| 34 | + * </p> |
| 35 | + * |
| 36 | + * @author Issam El-atif |
| 37 | + * @see AutoConfigurations |
| 38 | + */ |
| 39 | +public final class SpringAiTestAutoConfigurations { |
| 40 | + |
| 41 | + private SpringAiTestAutoConfigurations() { |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Creates an {@link AutoConfigurations} instance that includes the provided |
| 46 | + * configuration classes and any autoconfiguration classes referenced in |
| 47 | + * {@link AutoConfiguration#after()} attribute. |
| 48 | + * @param configurations one or more configuration classes that may be annotated with |
| 49 | + * {@link AutoConfiguration} |
| 50 | + * @return a composed {@link AutoConfigurations} instance including all discovered |
| 51 | + * classes |
| 52 | + * @see AutoConfigurations#of(Class[]) |
| 53 | + */ |
| 54 | + public static AutoConfigurations of(Class<?>... configurations) { |
| 55 | + return AutoConfigurations.of(Arrays.stream(configurations) |
| 56 | + .map(c -> AnnotationUtils.findAnnotation(c, AutoConfiguration.class)) |
| 57 | + .filter(Objects::nonNull) |
| 58 | + .map(AutoConfiguration::after) |
| 59 | + .flatMap(ac -> Stream.concat(Stream.of(ac), Stream.of(configurations))) |
| 60 | + .toArray(Class<?>[]::new)); |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments