Skip to content

Commit 25158fb

Browse files
chore: [DevOps] enforce Spring AI optional (#339)
* chore: [DevOps] enforce Spring AI optional * Better message
1 parent 285960b commit 25158fb

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

orchestration/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@
132132
<artifactId>junit-jupiter-params</artifactId>
133133
<scope>test</scope>
134134
</dependency>
135+
<dependency>
136+
<groupId>com.github.javaparser</groupId>
137+
<artifactId>javaparser-core</artifactId>
138+
<scope>test</scope>
139+
</dependency>
135140
</dependencies>
136141

137142
<profiles>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.sap.ai.sdk.orchestration.spring;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import com.github.javaparser.JavaParser;
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
import java.nio.file.Paths;
13+
import java.util.List;
14+
import java.util.stream.Collectors;
15+
import java.util.stream.Stream;
16+
import lombok.val;
17+
import org.junit.jupiter.api.Test;
18+
19+
public class ImportCheckTest {
20+
21+
@Test
22+
public void testSpringAIOptional() throws IOException {
23+
String springAI = "org.springframework.ai";
24+
checkImports("com/sap/ai/sdk/orchestration", springAI, false);
25+
checkImports("com/sap/ai/sdk/orchestration/spring", springAI, true);
26+
}
27+
28+
private void checkImports(String packagePath, String imports, boolean shouldContain)
29+
throws IOException {
30+
List<File> javaFiles = getJavaFiles(packagePath);
31+
32+
boolean hasImport = false;
33+
for (File file : javaFiles) {
34+
val result = new JavaParser().parse(file).getResult();
35+
assertThat(result).isPresent();
36+
37+
hasImport =
38+
hasImport
39+
|| result.get().getImports().stream()
40+
.anyMatch(importDecl -> importDecl.getNameAsString().startsWith(imports));
41+
42+
if (!shouldContain) {
43+
assertFalse(
44+
hasImport, "File " + file.getName() + " contains a prohibited import: " + imports);
45+
}
46+
}
47+
48+
if (shouldContain) {
49+
assertTrue(hasImport, "Orchestration Spring should contain Spring AI imports");
50+
}
51+
}
52+
53+
private List<File> getJavaFiles(String packagePath) throws IOException {
54+
Path orchestrationPackage = Paths.get("src/main/java", packagePath);
55+
try (Stream<Path> files = Files.list(orchestrationPackage)) {
56+
return files
57+
.filter(Files::isRegularFile)
58+
.map(java.nio.file.Path::toFile)
59+
.filter(file -> file.getName().endsWith(".java"))
60+
.collect(Collectors.toList());
61+
}
62+
}
63+
}

pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<reactor-core.version>3.6.12</reactor-core.version>
6969
<dotenv-java.version>3.1.0</dotenv-java.version>
7070
<mockito.version>5.15.2</mockito.version>
71+
<javaparser.version>3.26.3</javaparser.version>
7172
<!-- conflicts resolution -->
7273
<micrometer.version>1.14.2</micrometer.version>
7374
<json.version>20250107</json.version>
@@ -185,6 +186,12 @@
185186
<version>${mockito.version}</version>
186187
<scope>test</scope>
187188
</dependency>
189+
<dependency>
190+
<groupId>com.github.javaparser</groupId>
191+
<artifactId>javaparser-core</artifactId>
192+
<version>${javaparser.version}</version>
193+
<scope>test</scope>
194+
</dependency>
188195
<!-- Modules BoM -->
189196
<dependency>
190197
<groupId>com.sap.ai.sdk</groupId>
@@ -400,6 +407,22 @@
400407
</rules>
401408
</configuration>
402409
</execution>
410+
<execution>
411+
<id>enforce-spring-ai-optional</id>
412+
<goals>
413+
<goal>enforce</goal>
414+
</goals>
415+
<configuration>
416+
<rules>
417+
<bannedDependencies>
418+
<message>Spring AI dependencies should be optional</message>
419+
<includes>
420+
<include>org.springframework.ai:*</include>
421+
</includes>
422+
</bannedDependencies>
423+
</rules>
424+
</configuration>
425+
</execution>
403426
</executions>
404427
</plugin>
405428
<!-- phase: process-sources -->

0 commit comments

Comments
 (0)