Skip to content

Commit 8527e31

Browse files
committed
Enforcer plugin config for optional dependency and replicate import test for spring-ai
- sample app ignored from ban
1 parent f8518ce commit 8527e31

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

foundation-models/openai/pom.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@
8989
<groupId>com.google.guava</groupId>
9090
<artifactId>guava</artifactId>
9191
</dependency>
92+
<dependency>
93+
<groupId>org.springframework.ai</groupId>
94+
<artifactId>spring-ai-core</artifactId>
95+
<optional>true</optional>
96+
</dependency>
9297
<!-- scope "provided" -->
9398
<dependency>
9499
<groupId>org.projectlombok</groupId>
@@ -122,8 +127,9 @@
122127
<scope>test</scope>
123128
</dependency>
124129
<dependency>
125-
<groupId>org.springframework.ai</groupId>
126-
<artifactId>spring-ai-core</artifactId>
130+
<groupId>com.github.javaparser</groupId>
131+
<artifactId>javaparser-core</artifactId>
132+
<scope>test</scope>
127133
</dependency>
128134
</dependencies>
129135
</project>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.sap.ai.sdk.foundationmodels.openai.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+
class ImportCheckTest {
20+
21+
private static final JavaParser PARSER = new JavaParser();
22+
23+
@Test
24+
void testSpringAIOptional() throws IOException {
25+
String springAI = "org.springframework.ai";
26+
checkImports("com/sap/ai/sdk/foundationmodels/openai", springAI, false);
27+
checkImports("com/sap/ai/sdk/foundationmodels/openai/spring", springAI, true);
28+
}
29+
30+
private void checkImports(String packagePath, String imports, boolean shouldContain)
31+
throws IOException {
32+
List<File> javaFiles = getJavaFiles(packagePath);
33+
34+
boolean hasImport = false;
35+
for (File file : javaFiles) {
36+
val result = PARSER.parse(file).getResult();
37+
assertThat(result).isPresent();
38+
39+
hasImport =
40+
hasImport
41+
|| result.get().getImports().stream()
42+
.anyMatch(importDecl -> importDecl.getNameAsString().startsWith(imports));
43+
44+
if (!shouldContain) {
45+
assertFalse(
46+
hasImport, "File " + file.getName() + " contains a prohibited import: " + imports);
47+
}
48+
}
49+
50+
if (shouldContain) {
51+
assertTrue(hasImport, "OpenAI Spring should contain Spring AI imports");
52+
}
53+
}
54+
55+
private List<File> getJavaFiles(String packagePath) throws IOException {
56+
Path openAiPackage = Paths.get("src/main/java", packagePath);
57+
try (Stream<Path> files = Files.list(openAiPackage)) {
58+
return files
59+
.filter(Files::isRegularFile)
60+
.map(Path::toFile)
61+
.filter(file -> file.getName().endsWith(".java"))
62+
.collect(Collectors.toList());
63+
}
64+
}
65+
}

pom.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,11 @@
438438
<rules>
439439
<bannedDependencies>
440440
<message>Spring AI dependencies should be optional</message>
441+
<excludes>
442+
<exclude>org.springframework.ai:*</exclude>
443+
</excludes>
441444
<includes>
442-
<include>org.springframework.ai:*</include>
445+
<include>com.sap.ai.sdk.app:*</include>
443446
</includes>
444447
</bannedDependencies>
445448
</rules>

sample-code/spring-app/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<!-- Allow logging frameworks because this module is not released -->
4242
<enforcer.skipBanLoggingFrameworks>true</enforcer.skipBanLoggingFrameworks>
4343
<enforcer.skipEnforceScopeLoggerBridges>true</enforcer.skipEnforceScopeLoggerBridges>
44+
<enforcer.enforce-spring-ai-optional>false</enforcer.enforce-spring-ai-optional>
4445
</properties>
4546

4647
<dependencies>
@@ -214,6 +215,16 @@
214215
<skipAddThirdParty>true</skipAddThirdParty>
215216
</configuration>
216217
</plugin>
218+
<plugin>
219+
<groupId>org.apache.maven.plugins</groupId>
220+
<artifactId>maven-enforcer-plugin</artifactId>
221+
<executions>
222+
<execution>
223+
<id>enforce-spring-ai-optional</id>
224+
<phase>none</phase>
225+
</execution>
226+
</executions>
227+
</plugin>
217228
</plugins>
218229
</build>
219230
</project>

0 commit comments

Comments
 (0)