Skip to content

Commit 109923a

Browse files
authored
test: Add comprehensive test coverage for Ollama runtime hints registration (#4014)
Signed-off-by: Alex Klimenko <[email protected]>
1 parent 450cac4 commit 109923a

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

models/spring-ai-ollama/src/test/java/org/springframework/ai/ollama/aot/OllamaRuntimeHintsTests.java

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,101 @@ void registerHints() {
5353
assertThat(registeredTypes.contains(TypeReference.of(OllamaOptions.class))).isTrue();
5454
}
5555

56+
@Test
57+
void registerHintsWithNullClassLoader() {
58+
RuntimeHints runtimeHints = new RuntimeHints();
59+
OllamaRuntimeHints ollamaRuntimeHints = new OllamaRuntimeHints();
60+
61+
// Should not throw exception with null ClassLoader
62+
org.assertj.core.api.Assertions.assertThatCode(() -> ollamaRuntimeHints.registerHints(runtimeHints, null))
63+
.doesNotThrowAnyException();
64+
}
65+
66+
@Test
67+
void ensureReflectionHintsAreRegistered() {
68+
RuntimeHints runtimeHints = new RuntimeHints();
69+
OllamaRuntimeHints ollamaRuntimeHints = new OllamaRuntimeHints();
70+
ollamaRuntimeHints.registerHints(runtimeHints, null);
71+
72+
// Ensure reflection hints are properly registered
73+
assertThat(runtimeHints.reflection().typeHints().spliterator().estimateSize()).isGreaterThan(0);
74+
}
75+
76+
@Test
77+
void verifyMultipleRegistrationCallsAreIdempotent() {
78+
RuntimeHints runtimeHints = new RuntimeHints();
79+
OllamaRuntimeHints ollamaRuntimeHints = new OllamaRuntimeHints();
80+
81+
// Register hints multiple times
82+
ollamaRuntimeHints.registerHints(runtimeHints, null);
83+
long firstCount = runtimeHints.reflection().typeHints().spliterator().estimateSize();
84+
85+
ollamaRuntimeHints.registerHints(runtimeHints, null);
86+
long secondCount = runtimeHints.reflection().typeHints().spliterator().estimateSize();
87+
88+
// Should not register duplicate hints
89+
assertThat(firstCount).isEqualTo(secondCount);
90+
}
91+
92+
@Test
93+
void verifyMainApiClassesRegistered() {
94+
RuntimeHints runtimeHints = new RuntimeHints();
95+
OllamaRuntimeHints ollamaRuntimeHints = new OllamaRuntimeHints();
96+
ollamaRuntimeHints.registerHints(runtimeHints, null);
97+
98+
Set<TypeReference> registeredTypes = new HashSet<>();
99+
runtimeHints.reflection().typeHints().forEach(typeHint -> registeredTypes.add(typeHint.getType()));
100+
101+
// Verify that the main classes we already know exist are registered
102+
assertThat(registeredTypes.contains(TypeReference.of(OllamaApi.ChatRequest.class))).isTrue();
103+
assertThat(registeredTypes.contains(TypeReference.of(OllamaApi.Message.class))).isTrue();
104+
assertThat(registeredTypes.contains(TypeReference.of(OllamaOptions.class))).isTrue();
105+
}
106+
107+
@Test
108+
void verifyJsonAnnotatedClassesFromCorrectPackage() {
109+
Set<TypeReference> jsonAnnotatedClasses = findJsonAnnotatedClassesInPackage("org.springframework.ai.ollama");
110+
111+
// Ensure we found some JSON annotated classes in the expected package
112+
assertThat(jsonAnnotatedClasses.spliterator().estimateSize()).isGreaterThan(0);
113+
114+
// Verify all found classes are from the expected package
115+
for (TypeReference classRef : jsonAnnotatedClasses) {
116+
assertThat(classRef.getName()).startsWith("org.springframework.ai.ollama");
117+
}
118+
}
119+
120+
@Test
121+
void verifyNoUnnecessaryHintsRegistered() {
122+
RuntimeHints runtimeHints = new RuntimeHints();
123+
OllamaRuntimeHints ollamaRuntimeHints = new OllamaRuntimeHints();
124+
ollamaRuntimeHints.registerHints(runtimeHints, null);
125+
126+
Set<TypeReference> jsonAnnotatedClasses = findJsonAnnotatedClassesInPackage("org.springframework.ai.ollama");
127+
128+
Set<TypeReference> registeredTypes = new HashSet<>();
129+
runtimeHints.reflection().typeHints().forEach(typeHint -> registeredTypes.add(typeHint.getType()));
130+
131+
// Ensure we don't register significantly more types than needed
132+
// Allow for some additional utility types but prevent hint bloat
133+
assertThat(registeredTypes.size()).isLessThanOrEqualTo(jsonAnnotatedClasses.size() + 15);
134+
}
135+
136+
@Test
137+
void verifyNestedClassHintsAreRegistered() {
138+
RuntimeHints runtimeHints = new RuntimeHints();
139+
OllamaRuntimeHints ollamaRuntimeHints = new OllamaRuntimeHints();
140+
ollamaRuntimeHints.registerHints(runtimeHints, null);
141+
142+
Set<TypeReference> registeredTypes = new HashSet<>();
143+
runtimeHints.reflection().typeHints().forEach(typeHint -> registeredTypes.add(typeHint.getType()));
144+
145+
// Verify nested classes that we know exist from the original test
146+
assertThat(registeredTypes.contains(TypeReference.of(OllamaApi.ChatRequest.Tool.class))).isTrue();
147+
148+
// Count nested classes to ensure comprehensive registration
149+
long nestedClassCount = registeredTypes.stream().filter(typeRef -> typeRef.getName().contains("$")).count();
150+
assertThat(nestedClassCount).isGreaterThan(0);
151+
}
152+
56153
}

0 commit comments

Comments
 (0)