Skip to content

Commit dd0d997

Browse files
committed
Add testing utility class SpringAiTestAutoConfigurations
Signed-off-by: Issam El-atif <issam.elatif@gmail.com>
1 parent 0fdb911 commit dd0d997

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 org.junit.jupiter.api.Test;
20+
21+
import org.springframework.boot.autoconfigure.AutoConfiguration;
22+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* @author Issam El-atif
28+
*/
29+
class SpringAiTestAutoConfigurationsTests {
30+
31+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
32+
33+
@Test
34+
void shouldLoadProvidedConfiguration() {
35+
this.contextRunner.withConfiguration(SpringAiTestAutoConfigurations.of(SimpleConfiguration.class))
36+
.run(context -> assertThat(context).hasSingleBean(SimpleConfiguration.class));
37+
}
38+
39+
@Test
40+
void shouldIncludeConfigurationsDeclaredInAfterAttribute() {
41+
this.contextRunner.withConfiguration(SpringAiTestAutoConfigurations.of(AfterConfiguration.class))
42+
.run(context -> {
43+
assertThat(context).hasSingleBean(SimpleConfiguration.class);
44+
assertThat(context).hasSingleBean(AfterConfiguration.class);
45+
});
46+
}
47+
48+
@AutoConfiguration
49+
static class SimpleConfiguration {
50+
51+
}
52+
53+
@AutoConfiguration(after = { SimpleConfiguration.class })
54+
static class AfterConfiguration {
55+
56+
}
57+
58+
}

0 commit comments

Comments
 (0)